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 17ca44b2f3SPatrick Venture #include "HwmonTempSensor.hpp" 18ca44b2f3SPatrick Venture #include "Utils.hpp" 19ca44b2f3SPatrick Venture 206714a25aSJames Feist #include <boost/algorithm/string/predicate.hpp> 216714a25aSJames Feist #include <boost/algorithm/string/replace.hpp> 2296e97db7SPatrick Venture #include <boost/container/flat_map.hpp> 236714a25aSJames Feist #include <boost/container/flat_set.hpp> 2438fb5983SJames Feist #include <sdbusplus/asio/connection.hpp> 2538fb5983SJames Feist #include <sdbusplus/asio/object_server.hpp> 2638fb5983SJames Feist #include <sdbusplus/bus/match.hpp> 2738fb5983SJames Feist 2838fb5983SJames Feist #include <array> 2924f02f24SJames Feist #include <filesystem> 306714a25aSJames Feist #include <fstream> 3196e97db7SPatrick Venture #include <functional> 3296e97db7SPatrick Venture #include <memory> 336714a25aSJames Feist #include <regex> 3496e97db7SPatrick Venture #include <stdexcept> 3596e97db7SPatrick Venture #include <string> 3696e97db7SPatrick Venture #include <utility> 3796e97db7SPatrick Venture #include <variant> 3896e97db7SPatrick Venture #include <vector> 396714a25aSJames Feist 406714a25aSJames Feist static constexpr bool DEBUG = false; 416714a25aSJames Feist 42cf3bce6eSJames Feist namespace fs = std::filesystem; 438fb0a013SJosh Lehan static constexpr std::array<const char*, 11> sensorTypes = { 448b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.EMC1413", 458b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.MAX31725", 468b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.MAX31730", 4716e7af1dSJason Ling "xyz.openbmc_project.Configuration.MAX6581", 483840d0adSJosh Lehan "xyz.openbmc_project.Configuration.MAX6654", 498fb0a013SJosh Lehan "xyz.openbmc_project.Configuration.SBTSI", 5055ab2afbSJohn Wang "xyz.openbmc_project.Configuration.TMP112", 513546adb9SPatrick Venture "xyz.openbmc_project.Configuration.TMP175", 528b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.TMP421", 538b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.TMP441", 548b3f7d40SAlex Qiu "xyz.openbmc_project.Configuration.TMP75"}; 556714a25aSJames Feist 566714a25aSJames Feist void createSensors( 576714a25aSJames Feist boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, 58f3fd1915SYong Li boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& 596714a25aSJames Feist sensors, 606714a25aSJames Feist std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 615591cf08SJames Feist const std::shared_ptr<boost::container::flat_set<std::string>>& 626714a25aSJames Feist sensorsChanged) 636714a25aSJames Feist { 64df515159SJames Feist auto getter = std::make_shared<GetSensorConfiguration>( 65df515159SJames Feist dbusConnection, 66df515159SJames Feist std::move([&io, &objectServer, &sensors, &dbusConnection, 675591cf08SJames Feist sensorsChanged]( 68df515159SJames Feist const ManagedObjectType& sensorConfigurations) { 696714a25aSJames Feist bool firstScan = sensorsChanged == nullptr; 70df515159SJames Feist 716714a25aSJames Feist std::vector<fs::path> paths; 72df515159SJames Feist if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", 73df515159SJames Feist paths)) 746714a25aSJames Feist { 756714a25aSJames Feist std::cerr << "No temperature sensors in system\n"; 766714a25aSJames Feist return; 776714a25aSJames Feist } 786714a25aSJames Feist 7937266ca9SJames Feist boost::container::flat_set<std::string> directories; 8037266ca9SJames Feist 81df515159SJames Feist // iterate through all found temp sensors, and try to match them 82df515159SJames Feist // with configuration 836714a25aSJames Feist for (auto& path : paths) 846714a25aSJames Feist { 856714a25aSJames Feist std::smatch match; 8637266ca9SJames Feist const std::string& pathStr = path.string(); 876714a25aSJames Feist auto directory = path.parent_path(); 886714a25aSJames Feist 8937266ca9SJames Feist auto ret = directories.insert(directory.string()); 9037266ca9SJames Feist if (!ret.second) 916714a25aSJames Feist { 9237266ca9SJames Feist continue; // already searched this path 9337266ca9SJames Feist } 9437266ca9SJames Feist 95b6c0b914SJames Feist fs::path device = directory / "device"; 9637266ca9SJames Feist std::string deviceName = fs::canonical(device).stem(); 9737266ca9SJames Feist auto findHyphen = deviceName.find("-"); 9837266ca9SJames Feist if (findHyphen == std::string::npos) 9937266ca9SJames Feist { 10037266ca9SJames Feist std::cerr << "found bad device " << deviceName << "\n"; 1016714a25aSJames Feist continue; 1026714a25aSJames Feist } 10337266ca9SJames Feist std::string busStr = deviceName.substr(0, findHyphen); 10437266ca9SJames Feist std::string addrStr = deviceName.substr(findHyphen + 1); 10537266ca9SJames Feist 10637266ca9SJames Feist size_t bus = 0; 10737266ca9SJames Feist size_t addr = 0; 10837266ca9SJames Feist try 1096714a25aSJames Feist { 11037266ca9SJames Feist bus = std::stoi(busStr); 11137266ca9SJames Feist addr = std::stoi(addrStr, 0, 16); 11237266ca9SJames Feist } 113b6c0b914SJames Feist catch (std::invalid_argument&) 11437266ca9SJames Feist { 1156714a25aSJames Feist continue; 1166714a25aSJames Feist } 1176714a25aSJames Feist const SensorData* sensorData = nullptr; 1186714a25aSJames Feist const std::string* interfacePath = nullptr; 11937266ca9SJames Feist const char* sensorType = nullptr; 1208b3f7d40SAlex Qiu const SensorBaseConfiguration* baseConfiguration = nullptr; 1218b3f7d40SAlex Qiu const SensorBaseConfigMap* baseConfigMap = nullptr; 12237266ca9SJames Feist 123df515159SJames Feist for (const std::pair<sdbusplus::message::object_path, 124df515159SJames Feist SensorData>& sensor : sensorConfigurations) 12537266ca9SJames Feist { 12637266ca9SJames Feist sensorData = &(sensor.second); 1279ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 1286714a25aSJames Feist { 1296714a25aSJames Feist auto sensorBase = sensorData->find(type); 1306714a25aSJames Feist if (sensorBase != sensorData->end()) 1316714a25aSJames Feist { 1326714a25aSJames Feist baseConfiguration = &(*sensorBase); 1336714a25aSJames Feist sensorType = type; 1346714a25aSJames Feist break; 1356714a25aSJames Feist } 1366714a25aSJames Feist } 1376714a25aSJames Feist if (baseConfiguration == nullptr) 1386714a25aSJames Feist { 13937266ca9SJames Feist std::cerr << "error finding base configuration for " 14037266ca9SJames Feist << deviceName << "\n"; 14137266ca9SJames Feist continue; 14237266ca9SJames Feist } 1438b3f7d40SAlex Qiu baseConfigMap = &baseConfiguration->second; 1448b3f7d40SAlex Qiu auto configurationBus = baseConfigMap->find("Bus"); 1458b3f7d40SAlex Qiu auto configurationAddress = baseConfigMap->find("Address"); 14637266ca9SJames Feist 1478b3f7d40SAlex Qiu if (configurationBus == baseConfigMap->end() || 1488b3f7d40SAlex Qiu configurationAddress == baseConfigMap->end()) 14937266ca9SJames Feist { 1508b3f7d40SAlex Qiu std::cerr << "error finding bus or address in " 1518b3f7d40SAlex Qiu "configuration\n"; 15237266ca9SJames Feist continue; 15337266ca9SJames Feist } 15437266ca9SJames Feist 1553eb82629SJames Feist if (std::get<uint64_t>(configurationBus->second) != bus || 156df515159SJames Feist std::get<uint64_t>(configurationAddress->second) != 157df515159SJames Feist addr) 15837266ca9SJames Feist { 15937266ca9SJames Feist continue; 16037266ca9SJames Feist } 16137266ca9SJames Feist 16237266ca9SJames Feist interfacePath = &(sensor.first.str); 16337266ca9SJames Feist break; 16437266ca9SJames Feist } 16537266ca9SJames Feist if (interfacePath == nullptr) 16637266ca9SJames Feist { 167df515159SJames Feist std::cerr << "failed to find match for " << deviceName 168df515159SJames Feist << "\n"; 1696714a25aSJames Feist continue; 1706714a25aSJames Feist } 1716714a25aSJames Feist 1728b3f7d40SAlex Qiu auto findSensorName = baseConfigMap->find("Name"); 1738b3f7d40SAlex Qiu if (findSensorName == baseConfigMap->end()) 1746714a25aSJames Feist { 1756714a25aSJames Feist std::cerr << "could not determine configuration name for " 17637266ca9SJames Feist << deviceName << "\n"; 1776714a25aSJames Feist continue; 1786714a25aSJames Feist } 179df515159SJames Feist std::string sensorName = 180df515159SJames Feist std::get<std::string>(findSensorName->second); 1816714a25aSJames Feist // on rescans, only update sensors we were signaled by 1826714a25aSJames Feist auto findSensor = sensors.find(sensorName); 1836714a25aSJames Feist if (!firstScan && findSensor != sensors.end()) 1846714a25aSJames Feist { 1856714a25aSJames Feist bool found = false; 186df515159SJames Feist for (auto it = sensorsChanged->begin(); 187df515159SJames Feist it != sensorsChanged->end(); it++) 1886714a25aSJames Feist { 1896714a25aSJames Feist if (boost::ends_with(*it, findSensor->second->name)) 1906714a25aSJames Feist { 1916714a25aSJames Feist sensorsChanged->erase(it); 1926714a25aSJames Feist findSensor->second = nullptr; 1936714a25aSJames Feist found = true; 1946714a25aSJames Feist break; 1956714a25aSJames Feist } 1966714a25aSJames Feist } 1976714a25aSJames Feist if (!found) 1986714a25aSJames Feist { 1996714a25aSJames Feist continue; 2006714a25aSJames Feist } 2016714a25aSJames Feist } 2026714a25aSJames Feist std::vector<thresholds::Threshold> sensorThresholds; 2039ced0a38SJae Hyun Yoo if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) 2046714a25aSJames Feist { 205df515159SJames Feist std::cerr << "error populating thresholds for " 206df515159SJames Feist << sensorName << "\n"; 2076714a25aSJames Feist } 208f9b01b6dSJames Feist auto findPowerOn = baseConfiguration->second.find("PowerState"); 209f9b01b6dSJames Feist PowerState readState = PowerState::always; 210f9b01b6dSJames Feist if (findPowerOn != baseConfiguration->second.end()) 211f9b01b6dSJames Feist { 212f9b01b6dSJames Feist std::string powerState = std::visit( 213f9b01b6dSJames Feist VariantToStringVisitor(), findPowerOn->second); 214f9b01b6dSJames Feist setReadState(powerState, readState); 215f9b01b6dSJames Feist } 216*100c20bfSJason Ling 217*100c20bfSJason Ling auto permitSet = getPermitSet(*baseConfigMap); 2188b3f7d40SAlex Qiu auto& sensor = sensors[sensorName]; 2198b3f7d40SAlex Qiu sensor = nullptr; 220*100c20bfSJason Ling auto hwmonFile = getFullHwmonFilePath(directory.string(), 221*100c20bfSJason Ling "temp1", permitSet); 222*100c20bfSJason Ling if (hwmonFile) 223*100c20bfSJason Ling { 224f3fd1915SYong Li sensor = std::make_shared<HwmonTempSensor>( 225*100c20bfSJason Ling *hwmonFile, sensorType, objectServer, dbusConnection, 226*100c20bfSJason Ling io, sensorName, std::move(sensorThresholds), 227*100c20bfSJason Ling *interfacePath, readState); 228f3fd1915SYong Li sensor->setupRead(); 229*100c20bfSJason Ling } 2308b3f7d40SAlex Qiu // Looking for keys like "Name1" for temp2_input, 2318b3f7d40SAlex Qiu // "Name2" for temp3_input, etc. 2328b3f7d40SAlex Qiu int i = 0; 2338b3f7d40SAlex Qiu while (true) 23437266ca9SJames Feist { 2358b3f7d40SAlex Qiu ++i; 2368b3f7d40SAlex Qiu auto findKey = 2378b8bcc87SJason Ling baseConfigMap->find("Name" + std::to_string(i)); 2388b3f7d40SAlex Qiu if (findKey == baseConfigMap->end()) 2398b3f7d40SAlex Qiu { 2408b3f7d40SAlex Qiu break; 24137266ca9SJames Feist } 2428b3f7d40SAlex Qiu std::string sensorName = 2438b3f7d40SAlex Qiu std::get<std::string>(findKey->second); 244*100c20bfSJason Ling hwmonFile = getFullHwmonFilePath( 245*100c20bfSJason Ling directory.string(), "temp" + std::to_string(i + 1), 246*100c20bfSJason Ling permitSet); 247*100c20bfSJason Ling if (hwmonFile) 248*100c20bfSJason Ling { 2498b3f7d40SAlex Qiu auto& sensor = sensors[sensorName]; 2508b3f7d40SAlex Qiu sensor = nullptr; 251f3fd1915SYong Li sensor = std::make_shared<HwmonTempSensor>( 252*100c20bfSJason Ling *hwmonFile, sensorType, objectServer, 253*100c20bfSJason Ling dbusConnection, io, sensorName, 254*100c20bfSJason Ling std::vector<thresholds::Threshold>(), 255f9b01b6dSJames Feist *interfacePath, readState); 256f3fd1915SYong Li sensor->setupRead(); 2578b3f7d40SAlex Qiu } 2586714a25aSJames Feist } 259*100c20bfSJason Ling } 260df515159SJames Feist })); 261df515159SJames Feist getter->getConfiguration( 262df515159SJames Feist std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); 2636714a25aSJames Feist } 2646714a25aSJames Feist 265b6c0b914SJames Feist int main() 2666714a25aSJames Feist { 2676714a25aSJames Feist boost::asio::io_service io; 2686714a25aSJames Feist auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); 2696714a25aSJames Feist systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); 2706714a25aSJames Feist sdbusplus::asio::object_server objectServer(systemBus); 271f3fd1915SYong Li boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> 2726714a25aSJames Feist sensors; 2736714a25aSJames Feist std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; 2745591cf08SJames Feist auto sensorsChanged = 2755591cf08SJames Feist std::make_shared<boost::container::flat_set<std::string>>(); 2766714a25aSJames Feist 2776714a25aSJames Feist io.post([&]() { 2786714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, nullptr); 2796714a25aSJames Feist }); 2806714a25aSJames Feist 2816714a25aSJames Feist boost::asio::deadline_timer filterTimer(io); 2826714a25aSJames Feist std::function<void(sdbusplus::message::message&)> eventHandler = 2836714a25aSJames Feist [&](sdbusplus::message::message& message) { 2846714a25aSJames Feist if (message.is_method_error()) 2856714a25aSJames Feist { 2866714a25aSJames Feist std::cerr << "callback method error\n"; 2876714a25aSJames Feist return; 2886714a25aSJames Feist } 2896714a25aSJames Feist sensorsChanged->insert(message.get_path()); 2906714a25aSJames Feist // this implicitly cancels the timer 2916714a25aSJames Feist filterTimer.expires_from_now(boost::posix_time::seconds(1)); 2926714a25aSJames Feist 2936714a25aSJames Feist filterTimer.async_wait([&](const boost::system::error_code& ec) { 2946714a25aSJames Feist if (ec == boost::asio::error::operation_aborted) 2956714a25aSJames Feist { 2966714a25aSJames Feist /* we were canceled*/ 2976714a25aSJames Feist return; 2986714a25aSJames Feist } 2996714a25aSJames Feist else if (ec) 3006714a25aSJames Feist { 3016714a25aSJames Feist std::cerr << "timer error\n"; 3026714a25aSJames Feist return; 3036714a25aSJames Feist } 3046714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, 3056714a25aSJames Feist sensorsChanged); 3066714a25aSJames Feist }); 3076714a25aSJames Feist }; 3086714a25aSJames Feist 3099ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 3106714a25aSJames Feist { 3116714a25aSJames Feist auto match = std::make_unique<sdbusplus::bus::match::match>( 3126714a25aSJames Feist static_cast<sdbusplus::bus::bus&>(*systemBus), 3136714a25aSJames Feist "type='signal',member='PropertiesChanged',path_namespace='" + 3149ced0a38SJae Hyun Yoo std::string(inventoryPath) + "',arg0namespace='" + type + "'", 3156714a25aSJames Feist eventHandler); 3166714a25aSJames Feist matches.emplace_back(std::move(match)); 3176714a25aSJames Feist } 3186714a25aSJames Feist 3196714a25aSJames Feist io.run(); 3206714a25aSJames Feist } 321