16714a25aSJames Feist /* 26714a25aSJames Feist // Copyright (c) 2017 Intel Corporation 36714a25aSJames Feist // 46714a25aSJames Feist // Licensed under the Apache License, Version 2.0 (the "License"); 56714a25aSJames Feist // you may not use this file except in compliance with the License. 66714a25aSJames Feist // You may obtain a copy of the License at 76714a25aSJames Feist // 86714a25aSJames Feist // http://www.apache.org/licenses/LICENSE-2.0 96714a25aSJames Feist // 106714a25aSJames Feist // Unless required by applicable law or agreed to in writing, software 116714a25aSJames Feist // distributed under the License is distributed on an "AS IS" BASIS, 126714a25aSJames Feist // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136714a25aSJames Feist // See the License for the specific language governing permissions and 146714a25aSJames Feist // limitations under the License. 156714a25aSJames Feist */ 166714a25aSJames Feist 178a57ec09SEd Tanous #include <HwmonTempSensor.hpp> 188a57ec09SEd Tanous #include <Utils.hpp> 196714a25aSJames Feist #include <boost/algorithm/string/predicate.hpp> 206714a25aSJames Feist #include <boost/algorithm/string/replace.hpp> 2196e97db7SPatrick Venture #include <boost/container/flat_map.hpp> 226714a25aSJames Feist #include <boost/container/flat_set.hpp> 2338fb5983SJames Feist #include <sdbusplus/asio/connection.hpp> 2438fb5983SJames Feist #include <sdbusplus/asio/object_server.hpp> 2538fb5983SJames Feist #include <sdbusplus/bus/match.hpp> 2638fb5983SJames Feist 2738fb5983SJames Feist #include <array> 2824f02f24SJames Feist #include <filesystem> 296714a25aSJames Feist #include <fstream> 3096e97db7SPatrick Venture #include <functional> 3196e97db7SPatrick Venture #include <memory> 326714a25aSJames Feist #include <regex> 3396e97db7SPatrick Venture #include <stdexcept> 3496e97db7SPatrick Venture #include <string> 3596e97db7SPatrick Venture #include <utility> 3696e97db7SPatrick Venture #include <variant> 3796e97db7SPatrick Venture #include <vector> 386714a25aSJames Feist 398a57ec09SEd Tanous static constexpr bool debug = false; 4087bc67f7SJeff Lin static constexpr float pollRateDefault = 0.5; 416714a25aSJames Feist 42cf3bce6eSJames Feist namespace fs = std::filesystem; 43*5770a6fdSOskar Senft static constexpr std::array<const char*, 17> sensorTypes = { 44381636e2SGilbert Chen "xyz.openbmc_project.Configuration.EMC1412", 458b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.EMC1413", 46381636e2SGilbert Chen "xyz.openbmc_project.Configuration.EMC1414", 478b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.MAX31725", 488b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.MAX31730", 4916e7af1dSJason Ling "xyz.openbmc_project.Configuration.MAX6581", 503840d0adSJosh Lehan "xyz.openbmc_project.Configuration.MAX6654", 51*5770a6fdSOskar Senft "xyz.openbmc_project.Configuration.NCT7802", 528fb0a013SJosh Lehan "xyz.openbmc_project.Configuration.SBTSI", 535a3b3f8aSKonstantin Aladyshev "xyz.openbmc_project.Configuration.LM95234", 5455ab2afbSJohn Wang "xyz.openbmc_project.Configuration.TMP112", 553546adb9SPatrick Venture "xyz.openbmc_project.Configuration.TMP175", 568b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.TMP421", 578b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.TMP441", 5834fc75a7SKonstantin Aladyshev "xyz.openbmc_project.Configuration.LM75A", 597ea918f2SZev Weiss "xyz.openbmc_project.Configuration.TMP75", 607ea918f2SZev Weiss "xyz.openbmc_project.Configuration.W83773G"}; 616714a25aSJames Feist 626714a25aSJames Feist void createSensors( 636714a25aSJames Feist boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, 64f3fd1915SYong Li boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& 656714a25aSJames Feist sensors, 666714a25aSJames Feist std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 675591cf08SJames Feist const std::shared_ptr<boost::container::flat_set<std::string>>& 686714a25aSJames Feist sensorsChanged) 696714a25aSJames Feist { 70df515159SJames Feist auto getter = std::make_shared<GetSensorConfiguration>( 71df515159SJames Feist dbusConnection, 72df515159SJames Feist std::move([&io, &objectServer, &sensors, &dbusConnection, 735591cf08SJames Feist sensorsChanged]( 74df515159SJames Feist const ManagedObjectType& sensorConfigurations) { 756714a25aSJames Feist bool firstScan = sensorsChanged == nullptr; 76df515159SJames Feist 776714a25aSJames Feist std::vector<fs::path> paths; 78df515159SJames Feist if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", 79df515159SJames Feist paths)) 806714a25aSJames Feist { 816714a25aSJames Feist std::cerr << "No temperature sensors in system\n"; 826714a25aSJames Feist return; 836714a25aSJames Feist } 846714a25aSJames Feist 8537266ca9SJames Feist boost::container::flat_set<std::string> directories; 8637266ca9SJames Feist 87df515159SJames Feist // iterate through all found temp sensors, and try to match them 88df515159SJames Feist // with configuration 896714a25aSJames Feist for (auto& path : paths) 906714a25aSJames Feist { 916714a25aSJames Feist std::smatch match; 9237266ca9SJames Feist const std::string& pathStr = path.string(); 936714a25aSJames Feist auto directory = path.parent_path(); 946714a25aSJames Feist 9537266ca9SJames Feist auto ret = directories.insert(directory.string()); 9637266ca9SJames Feist if (!ret.second) 976714a25aSJames Feist { 9837266ca9SJames Feist continue; // already searched this path 9937266ca9SJames Feist } 10037266ca9SJames Feist 101b6c0b914SJames Feist fs::path device = directory / "device"; 10237266ca9SJames Feist std::string deviceName = fs::canonical(device).stem(); 1038a57ec09SEd Tanous auto findHyphen = deviceName.find('-'); 10437266ca9SJames Feist if (findHyphen == std::string::npos) 10537266ca9SJames Feist { 10637266ca9SJames Feist std::cerr << "found bad device " << deviceName << "\n"; 1076714a25aSJames Feist continue; 1086714a25aSJames Feist } 10937266ca9SJames Feist std::string busStr = deviceName.substr(0, findHyphen); 11037266ca9SJames Feist std::string addrStr = deviceName.substr(findHyphen + 1); 11137266ca9SJames Feist 11237266ca9SJames Feist size_t bus = 0; 11337266ca9SJames Feist size_t addr = 0; 11437266ca9SJames Feist try 1156714a25aSJames Feist { 11637266ca9SJames Feist bus = std::stoi(busStr); 1178a57ec09SEd Tanous addr = std::stoi(addrStr, nullptr, 16); 11837266ca9SJames Feist } 119b6c0b914SJames Feist catch (std::invalid_argument&) 12037266ca9SJames Feist { 1216714a25aSJames Feist continue; 1226714a25aSJames Feist } 1236714a25aSJames Feist const SensorData* sensorData = nullptr; 1246714a25aSJames Feist const std::string* interfacePath = nullptr; 12537266ca9SJames Feist const char* sensorType = nullptr; 1268b3f7d40SAlex Qiu const SensorBaseConfiguration* baseConfiguration = nullptr; 1278b3f7d40SAlex Qiu const SensorBaseConfigMap* baseConfigMap = nullptr; 12837266ca9SJames Feist 129df515159SJames Feist for (const std::pair<sdbusplus::message::object_path, 130df515159SJames Feist SensorData>& sensor : sensorConfigurations) 13137266ca9SJames Feist { 13237266ca9SJames Feist sensorData = &(sensor.second); 1339ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 1346714a25aSJames Feist { 1356714a25aSJames Feist auto sensorBase = sensorData->find(type); 1366714a25aSJames Feist if (sensorBase != sensorData->end()) 1376714a25aSJames Feist { 1386714a25aSJames Feist baseConfiguration = &(*sensorBase); 1396714a25aSJames Feist sensorType = type; 1406714a25aSJames Feist break; 1416714a25aSJames Feist } 1426714a25aSJames Feist } 1436714a25aSJames Feist if (baseConfiguration == nullptr) 1446714a25aSJames Feist { 14537266ca9SJames Feist std::cerr << "error finding base configuration for " 14637266ca9SJames Feist << deviceName << "\n"; 14737266ca9SJames Feist continue; 14837266ca9SJames Feist } 1498b3f7d40SAlex Qiu baseConfigMap = &baseConfiguration->second; 1508b3f7d40SAlex Qiu auto configurationBus = baseConfigMap->find("Bus"); 1518b3f7d40SAlex Qiu auto configurationAddress = baseConfigMap->find("Address"); 15237266ca9SJames Feist 1538b3f7d40SAlex Qiu if (configurationBus == baseConfigMap->end() || 1548b3f7d40SAlex Qiu configurationAddress == baseConfigMap->end()) 15537266ca9SJames Feist { 1568b3f7d40SAlex Qiu std::cerr << "error finding bus or address in " 1578b3f7d40SAlex Qiu "configuration\n"; 15837266ca9SJames Feist continue; 15937266ca9SJames Feist } 16037266ca9SJames Feist 1613eb82629SJames Feist if (std::get<uint64_t>(configurationBus->second) != bus || 162df515159SJames Feist std::get<uint64_t>(configurationAddress->second) != 163df515159SJames Feist addr) 16437266ca9SJames Feist { 16537266ca9SJames Feist continue; 16637266ca9SJames Feist } 16737266ca9SJames Feist 16837266ca9SJames Feist interfacePath = &(sensor.first.str); 16937266ca9SJames Feist break; 17037266ca9SJames Feist } 17137266ca9SJames Feist if (interfacePath == nullptr) 17237266ca9SJames Feist { 173df515159SJames Feist std::cerr << "failed to find match for " << deviceName 174df515159SJames Feist << "\n"; 1756714a25aSJames Feist continue; 1766714a25aSJames Feist } 1776714a25aSJames Feist 1788b3f7d40SAlex Qiu auto findSensorName = baseConfigMap->find("Name"); 1798b3f7d40SAlex Qiu if (findSensorName == baseConfigMap->end()) 1806714a25aSJames Feist { 1816714a25aSJames Feist std::cerr << "could not determine configuration name for " 18237266ca9SJames Feist << deviceName << "\n"; 1836714a25aSJames Feist continue; 1846714a25aSJames Feist } 185df515159SJames Feist std::string sensorName = 186df515159SJames Feist std::get<std::string>(findSensorName->second); 1876714a25aSJames Feist // on rescans, only update sensors we were signaled by 1886714a25aSJames Feist auto findSensor = sensors.find(sensorName); 1896714a25aSJames Feist if (!firstScan && findSensor != sensors.end()) 1906714a25aSJames Feist { 1916714a25aSJames Feist bool found = false; 192d653b75cSBruce Mitchell auto it = sensorsChanged->begin(); 193d653b75cSBruce Mitchell while (it != sensorsChanged->end()) 1946714a25aSJames Feist { 1956714a25aSJames Feist if (boost::ends_with(*it, findSensor->second->name)) 1966714a25aSJames Feist { 197d653b75cSBruce Mitchell it = sensorsChanged->erase(it); 1986714a25aSJames Feist findSensor->second = nullptr; 1996714a25aSJames Feist found = true; 2006714a25aSJames Feist break; 2016714a25aSJames Feist } 202d653b75cSBruce Mitchell ++it; 2036714a25aSJames Feist } 2046714a25aSJames Feist if (!found) 2056714a25aSJames Feist { 2066714a25aSJames Feist continue; 2076714a25aSJames Feist } 2086714a25aSJames Feist } 2095636d52bSMatt Spinler 2106714a25aSJames Feist std::vector<thresholds::Threshold> sensorThresholds; 2115636d52bSMatt Spinler int index = 1; 2125636d52bSMatt Spinler 2135636d52bSMatt Spinler if (!parseThresholdsFromConfig(*sensorData, sensorThresholds, 2145636d52bSMatt Spinler nullptr, &index)) 2156714a25aSJames Feist { 216df515159SJames Feist std::cerr << "error populating thresholds for " 2175636d52bSMatt Spinler << sensorName << " index 1\n"; 2186714a25aSJames Feist } 21987bc67f7SJeff Lin 22087bc67f7SJeff Lin auto findPollRate = baseConfiguration->second.find("PollRate"); 22187bc67f7SJeff Lin float pollRate = pollRateDefault; 22287bc67f7SJeff Lin if (findPollRate != baseConfiguration->second.end()) 22387bc67f7SJeff Lin { 22487bc67f7SJeff Lin pollRate = std::visit(VariantToFloatVisitor(), 22587bc67f7SJeff Lin findPollRate->second); 22687bc67f7SJeff Lin if (pollRate <= 0.0f) 22787bc67f7SJeff Lin { 22887bc67f7SJeff Lin pollRate = pollRateDefault; // polling time too short 22987bc67f7SJeff Lin } 23087bc67f7SJeff Lin } 23187bc67f7SJeff Lin 232f9b01b6dSJames Feist auto findPowerOn = baseConfiguration->second.find("PowerState"); 233f9b01b6dSJames Feist PowerState readState = PowerState::always; 234f9b01b6dSJames Feist if (findPowerOn != baseConfiguration->second.end()) 235f9b01b6dSJames Feist { 236f9b01b6dSJames Feist std::string powerState = std::visit( 237f9b01b6dSJames Feist VariantToStringVisitor(), findPowerOn->second); 238f9b01b6dSJames Feist setReadState(powerState, readState); 239f9b01b6dSJames Feist } 240100c20bfSJason Ling 241100c20bfSJason Ling auto permitSet = getPermitSet(*baseConfigMap); 2428b3f7d40SAlex Qiu auto& sensor = sensors[sensorName]; 2438b3f7d40SAlex Qiu sensor = nullptr; 244100c20bfSJason Ling auto hwmonFile = getFullHwmonFilePath(directory.string(), 245100c20bfSJason Ling "temp1", permitSet); 246100c20bfSJason Ling if (hwmonFile) 247100c20bfSJason Ling { 248f3fd1915SYong Li sensor = std::make_shared<HwmonTempSensor>( 249100c20bfSJason Ling *hwmonFile, sensorType, objectServer, dbusConnection, 25087bc67f7SJeff Lin io, sensorName, std::move(sensorThresholds), pollRate, 251100c20bfSJason Ling *interfacePath, readState); 252f3fd1915SYong Li sensor->setupRead(); 253100c20bfSJason Ling } 2548b3f7d40SAlex Qiu // Looking for keys like "Name1" for temp2_input, 2558b3f7d40SAlex Qiu // "Name2" for temp3_input, etc. 2568b3f7d40SAlex Qiu int i = 0; 2578b3f7d40SAlex Qiu while (true) 25837266ca9SJames Feist { 2598b3f7d40SAlex Qiu ++i; 2608b3f7d40SAlex Qiu auto findKey = 2618b8bcc87SJason Ling baseConfigMap->find("Name" + std::to_string(i)); 2628b3f7d40SAlex Qiu if (findKey == baseConfigMap->end()) 2638b3f7d40SAlex Qiu { 2648b3f7d40SAlex Qiu break; 26537266ca9SJames Feist } 2668b3f7d40SAlex Qiu std::string sensorName = 2678b3f7d40SAlex Qiu std::get<std::string>(findKey->second); 268100c20bfSJason Ling hwmonFile = getFullHwmonFilePath( 269100c20bfSJason Ling directory.string(), "temp" + std::to_string(i + 1), 270100c20bfSJason Ling permitSet); 271100c20bfSJason Ling if (hwmonFile) 272100c20bfSJason Ling { 2735636d52bSMatt Spinler // To look up thresholds for these additional sensors, 2745636d52bSMatt Spinler // match on the Index property in the threshold data 2755636d52bSMatt Spinler // where the index comes from the sysfs file we're on, 2765636d52bSMatt Spinler // i.e. index = 2 for temp2_input. 2775636d52bSMatt Spinler int index = i + 1; 2785636d52bSMatt Spinler std::vector<thresholds::Threshold> thresholds; 2795636d52bSMatt Spinler 2805636d52bSMatt Spinler if (!parseThresholdsFromConfig(*sensorData, thresholds, 2815636d52bSMatt Spinler nullptr, &index)) 2825636d52bSMatt Spinler { 2835636d52bSMatt Spinler std::cerr << "error populating thresholds for " 2845636d52bSMatt Spinler << sensorName << " index " << index 2855636d52bSMatt Spinler << "\n"; 2865636d52bSMatt Spinler } 2875636d52bSMatt Spinler 2888b3f7d40SAlex Qiu auto& sensor = sensors[sensorName]; 2898b3f7d40SAlex Qiu sensor = nullptr; 290f3fd1915SYong Li sensor = std::make_shared<HwmonTempSensor>( 291100c20bfSJason Ling *hwmonFile, sensorType, objectServer, 292100c20bfSJason Ling dbusConnection, io, sensorName, 2935636d52bSMatt Spinler std::move(thresholds), pollRate, *interfacePath, 2945636d52bSMatt Spinler readState); 295f3fd1915SYong Li sensor->setupRead(); 2968b3f7d40SAlex Qiu } 2976714a25aSJames Feist } 298100c20bfSJason Ling } 299df515159SJames Feist })); 300df515159SJames Feist getter->getConfiguration( 301df515159SJames Feist std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); 3026714a25aSJames Feist } 3036714a25aSJames Feist 304b6c0b914SJames Feist int main() 3056714a25aSJames Feist { 3066714a25aSJames Feist boost::asio::io_service io; 3076714a25aSJames Feist auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); 3086714a25aSJames Feist systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); 3096714a25aSJames Feist sdbusplus::asio::object_server objectServer(systemBus); 310f3fd1915SYong Li boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> 3116714a25aSJames Feist sensors; 3126714a25aSJames Feist std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; 3135591cf08SJames Feist auto sensorsChanged = 3145591cf08SJames Feist std::make_shared<boost::container::flat_set<std::string>>(); 3156714a25aSJames Feist 3166714a25aSJames Feist io.post([&]() { 3176714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, nullptr); 3186714a25aSJames Feist }); 3196714a25aSJames Feist 3206714a25aSJames Feist boost::asio::deadline_timer filterTimer(io); 3216714a25aSJames Feist std::function<void(sdbusplus::message::message&)> eventHandler = 3226714a25aSJames Feist [&](sdbusplus::message::message& message) { 3236714a25aSJames Feist if (message.is_method_error()) 3246714a25aSJames Feist { 3256714a25aSJames Feist std::cerr << "callback method error\n"; 3266714a25aSJames Feist return; 3276714a25aSJames Feist } 3286714a25aSJames Feist sensorsChanged->insert(message.get_path()); 3296714a25aSJames Feist // this implicitly cancels the timer 3306714a25aSJames Feist filterTimer.expires_from_now(boost::posix_time::seconds(1)); 3316714a25aSJames Feist 3326714a25aSJames Feist filterTimer.async_wait([&](const boost::system::error_code& ec) { 3336714a25aSJames Feist if (ec == boost::asio::error::operation_aborted) 3346714a25aSJames Feist { 3356714a25aSJames Feist /* we were canceled*/ 3366714a25aSJames Feist return; 3376714a25aSJames Feist } 3388a57ec09SEd Tanous if (ec) 3396714a25aSJames Feist { 3406714a25aSJames Feist std::cerr << "timer error\n"; 3416714a25aSJames Feist return; 3426714a25aSJames Feist } 3436714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, 3446714a25aSJames Feist sensorsChanged); 3456714a25aSJames Feist }); 3466714a25aSJames Feist }; 3476714a25aSJames Feist 3489ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 3496714a25aSJames Feist { 3506714a25aSJames Feist auto match = std::make_unique<sdbusplus::bus::match::match>( 3516714a25aSJames Feist static_cast<sdbusplus::bus::bus&>(*systemBus), 3526714a25aSJames Feist "type='signal',member='PropertiesChanged',path_namespace='" + 3539ced0a38SJae Hyun Yoo std::string(inventoryPath) + "',arg0namespace='" + type + "'", 3546714a25aSJames Feist eventHandler); 3556714a25aSJames Feist matches.emplace_back(std::move(match)); 3566714a25aSJames Feist } 3576714a25aSJames Feist 3581263c3daSBruce Lee setupManufacturingModeMatch(*systemBus); 3596714a25aSJames Feist io.run(); 3606714a25aSJames Feist } 361