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 176714a25aSJames Feist #include <HwmonTempSensor.hpp> 186714a25aSJames Feist #include <Utils.hpp> 196714a25aSJames Feist #include <boost/algorithm/string/predicate.hpp> 206714a25aSJames Feist #include <boost/algorithm/string/replace.hpp> 216714a25aSJames Feist #include <boost/container/flat_set.hpp> 2224f02f24SJames Feist #include <filesystem> 236714a25aSJames Feist #include <fstream> 246714a25aSJames Feist #include <regex> 256714a25aSJames Feist #include <sdbusplus/asio/connection.hpp> 266714a25aSJames Feist #include <sdbusplus/asio/object_server.hpp> 276714a25aSJames Feist 286714a25aSJames Feist static constexpr bool DEBUG = false; 296714a25aSJames Feist 30cf3bce6eSJames Feist namespace fs = std::filesystem; 31*7fa475d3SPatrick Venture static constexpr std::array<const char*, 5> sensorTypes = { 326714a25aSJames Feist "xyz.openbmc_project.Configuration.TMP75", 3355ab2afbSJohn Wang "xyz.openbmc_project.Configuration.TMP421", 34*7fa475d3SPatrick Venture "xyz.openbmc_project.Configuration.TMP441", 3555ab2afbSJohn Wang "xyz.openbmc_project.Configuration.TMP112", 3655ab2afbSJohn Wang "xyz.openbmc_project.Configuration.EMC1413"}; 376714a25aSJames Feist 386714a25aSJames Feist void createSensors( 396714a25aSJames Feist boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, 406714a25aSJames Feist boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>& 416714a25aSJames Feist sensors, 426714a25aSJames Feist std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 436714a25aSJames Feist const std::unique_ptr<boost::container::flat_set<std::string>>& 446714a25aSJames Feist sensorsChanged) 456714a25aSJames Feist { 466714a25aSJames Feist bool firstScan = sensorsChanged == nullptr; 476714a25aSJames Feist // use new data the first time, then refresh 486714a25aSJames Feist ManagedObjectType sensorConfigurations; 496714a25aSJames Feist bool useCache = false; 509ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 516714a25aSJames Feist { 526714a25aSJames Feist if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations, 536714a25aSJames Feist useCache)) 546714a25aSJames Feist { 556714a25aSJames Feist std::cerr << "error communicating to entity manager\n"; 566714a25aSJames Feist return; 576714a25aSJames Feist } 586714a25aSJames Feist useCache = true; 596714a25aSJames Feist } 606714a25aSJames Feist std::vector<fs::path> paths; 619ced0a38SJae Hyun Yoo if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths)) 626714a25aSJames Feist { 636714a25aSJames Feist std::cerr << "No temperature sensors in system\n"; 646714a25aSJames Feist return; 656714a25aSJames Feist } 666714a25aSJames Feist 6737266ca9SJames Feist boost::container::flat_set<std::string> directories; 6837266ca9SJames Feist 696714a25aSJames Feist // iterate through all found temp sensors, and try to match them with 706714a25aSJames Feist // configuration 716714a25aSJames Feist for (auto& path : paths) 726714a25aSJames Feist { 736714a25aSJames Feist std::smatch match; 7437266ca9SJames Feist const std::string& pathStr = path.string(); 756714a25aSJames Feist auto directory = path.parent_path(); 766714a25aSJames Feist 7737266ca9SJames Feist auto ret = directories.insert(directory.string()); 7837266ca9SJames Feist if (!ret.second) 796714a25aSJames Feist { 8037266ca9SJames Feist continue; // already searched this path 8137266ca9SJames Feist } 8237266ca9SJames Feist 83b6c0b914SJames Feist fs::path device = directory / "device"; 8437266ca9SJames Feist std::string deviceName = fs::canonical(device).stem(); 8537266ca9SJames Feist auto findHyphen = deviceName.find("-"); 8637266ca9SJames Feist if (findHyphen == std::string::npos) 8737266ca9SJames Feist { 8837266ca9SJames Feist std::cerr << "found bad device " << deviceName << "\n"; 896714a25aSJames Feist continue; 906714a25aSJames Feist } 9137266ca9SJames Feist std::string busStr = deviceName.substr(0, findHyphen); 9237266ca9SJames Feist std::string addrStr = deviceName.substr(findHyphen + 1); 9337266ca9SJames Feist 9437266ca9SJames Feist size_t bus = 0; 9537266ca9SJames Feist size_t addr = 0; 9637266ca9SJames Feist try 976714a25aSJames Feist { 9837266ca9SJames Feist bus = std::stoi(busStr); 9937266ca9SJames Feist addr = std::stoi(addrStr, 0, 16); 10037266ca9SJames Feist } 101b6c0b914SJames Feist catch (std::invalid_argument&) 10237266ca9SJames Feist { 1036714a25aSJames Feist continue; 1046714a25aSJames Feist } 1056714a25aSJames Feist const SensorData* sensorData = nullptr; 1066714a25aSJames Feist const std::string* interfacePath = nullptr; 10737266ca9SJames Feist const char* sensorType = nullptr; 1086714a25aSJames Feist const std::pair<std::string, boost::container::flat_map< 1096714a25aSJames Feist std::string, BasicVariantType>>* 1106714a25aSJames Feist baseConfiguration = nullptr; 11137266ca9SJames Feist 11237266ca9SJames Feist for (const std::pair<sdbusplus::message::object_path, SensorData>& 11337266ca9SJames Feist sensor : sensorConfigurations) 11437266ca9SJames Feist { 11537266ca9SJames Feist sensorData = &(sensor.second); 1169ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 1176714a25aSJames Feist { 1186714a25aSJames Feist auto sensorBase = sensorData->find(type); 1196714a25aSJames Feist if (sensorBase != sensorData->end()) 1206714a25aSJames Feist { 1216714a25aSJames Feist baseConfiguration = &(*sensorBase); 1226714a25aSJames Feist sensorType = type; 1236714a25aSJames Feist break; 1246714a25aSJames Feist } 1256714a25aSJames Feist } 1266714a25aSJames Feist if (baseConfiguration == nullptr) 1276714a25aSJames Feist { 12837266ca9SJames Feist std::cerr << "error finding base configuration for " 12937266ca9SJames Feist << deviceName << "\n"; 13037266ca9SJames Feist continue; 13137266ca9SJames Feist } 13237266ca9SJames Feist auto configurationBus = baseConfiguration->second.find("Bus"); 13337266ca9SJames Feist auto configurationAddress = 13437266ca9SJames Feist baseConfiguration->second.find("Address"); 13537266ca9SJames Feist 13637266ca9SJames Feist if (configurationBus == baseConfiguration->second.end() || 13737266ca9SJames Feist configurationAddress == baseConfiguration->second.end()) 13837266ca9SJames Feist { 13937266ca9SJames Feist std::cerr << "error finding bus or address in configuration"; 14037266ca9SJames Feist continue; 14137266ca9SJames Feist } 14237266ca9SJames Feist 1433eb82629SJames Feist if (std::get<uint64_t>(configurationBus->second) != bus || 1443eb82629SJames Feist std::get<uint64_t>(configurationAddress->second) != addr) 14537266ca9SJames Feist { 14637266ca9SJames Feist continue; 14737266ca9SJames Feist } 14837266ca9SJames Feist 14937266ca9SJames Feist interfacePath = &(sensor.first.str); 15037266ca9SJames Feist break; 15137266ca9SJames Feist } 15237266ca9SJames Feist if (interfacePath == nullptr) 15337266ca9SJames Feist { 15437266ca9SJames Feist std::cerr << "failed to find match for " << deviceName << "\n"; 1556714a25aSJames Feist continue; 1566714a25aSJames Feist } 1576714a25aSJames Feist 1586714a25aSJames Feist auto findSensorName = baseConfiguration->second.find("Name"); 1596714a25aSJames Feist if (findSensorName == baseConfiguration->second.end()) 1606714a25aSJames Feist { 1616714a25aSJames Feist std::cerr << "could not determine configuration name for " 16237266ca9SJames Feist << deviceName << "\n"; 1636714a25aSJames Feist continue; 1646714a25aSJames Feist } 1653eb82629SJames Feist std::string sensorName = std::get<std::string>(findSensorName->second); 1666714a25aSJames Feist // on rescans, only update sensors we were signaled by 1676714a25aSJames Feist auto findSensor = sensors.find(sensorName); 1686714a25aSJames Feist if (!firstScan && findSensor != sensors.end()) 1696714a25aSJames Feist { 1706714a25aSJames Feist bool found = false; 1716714a25aSJames Feist for (auto it = sensorsChanged->begin(); it != sensorsChanged->end(); 1726714a25aSJames Feist it++) 1736714a25aSJames Feist { 1746714a25aSJames Feist if (boost::ends_with(*it, findSensor->second->name)) 1756714a25aSJames Feist { 1766714a25aSJames Feist sensorsChanged->erase(it); 1776714a25aSJames Feist findSensor->second = nullptr; 1786714a25aSJames Feist found = true; 1796714a25aSJames Feist break; 1806714a25aSJames Feist } 1816714a25aSJames Feist } 1826714a25aSJames Feist if (!found) 1836714a25aSJames Feist { 1846714a25aSJames Feist continue; 1856714a25aSJames Feist } 1866714a25aSJames Feist } 1876714a25aSJames Feist std::vector<thresholds::Threshold> sensorThresholds; 1889ced0a38SJae Hyun Yoo if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) 1896714a25aSJames Feist { 1906714a25aSJames Feist std::cerr << "error populating thresholds for " << sensorName 1916714a25aSJames Feist << "\n"; 1926714a25aSJames Feist } 1936714a25aSJames Feist 1946714a25aSJames Feist sensors[sensorName] = std::make_unique<HwmonTempSensor>( 19537266ca9SJames Feist directory.string() + "/temp1_input", sensorType, objectServer, 19637266ca9SJames Feist dbusConnection, io, sensorName, std::move(sensorThresholds), 19737266ca9SJames Feist *interfacePath); 19837266ca9SJames Feist auto findSecondName = baseConfiguration->second.find("Name1"); 19937266ca9SJames Feist if (findSecondName == baseConfiguration->second.end()) 20037266ca9SJames Feist { 20137266ca9SJames Feist continue; 20237266ca9SJames Feist } 20337266ca9SJames Feist 2043eb82629SJames Feist sensorName = std::get<std::string>(findSecondName->second); 20537266ca9SJames Feist sensors[sensorName] = std::make_unique<HwmonTempSensor>( 20637266ca9SJames Feist directory.string() + "/temp2_input", sensorType, objectServer, 20737266ca9SJames Feist dbusConnection, io, sensorName, 20837266ca9SJames Feist std::vector<thresholds::Threshold>(), *interfacePath); 2096714a25aSJames Feist } 2106714a25aSJames Feist } 2116714a25aSJames Feist 212b6c0b914SJames Feist int main() 2136714a25aSJames Feist { 2146714a25aSJames Feist boost::asio::io_service io; 2156714a25aSJames Feist auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); 2166714a25aSJames Feist systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); 2176714a25aSJames Feist sdbusplus::asio::object_server objectServer(systemBus); 2186714a25aSJames Feist boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>> 2196714a25aSJames Feist sensors; 2206714a25aSJames Feist std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; 2216714a25aSJames Feist std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = 2226714a25aSJames Feist std::make_unique<boost::container::flat_set<std::string>>(); 2236714a25aSJames Feist 2246714a25aSJames Feist io.post([&]() { 2256714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, nullptr); 2266714a25aSJames Feist }); 2276714a25aSJames Feist 2286714a25aSJames Feist boost::asio::deadline_timer filterTimer(io); 2296714a25aSJames Feist std::function<void(sdbusplus::message::message&)> eventHandler = 2306714a25aSJames Feist [&](sdbusplus::message::message& message) { 2316714a25aSJames Feist if (message.is_method_error()) 2326714a25aSJames Feist { 2336714a25aSJames Feist std::cerr << "callback method error\n"; 2346714a25aSJames Feist return; 2356714a25aSJames Feist } 2366714a25aSJames Feist sensorsChanged->insert(message.get_path()); 2376714a25aSJames Feist // this implicitly cancels the timer 2386714a25aSJames Feist filterTimer.expires_from_now(boost::posix_time::seconds(1)); 2396714a25aSJames Feist 2406714a25aSJames Feist filterTimer.async_wait([&](const boost::system::error_code& ec) { 2416714a25aSJames Feist if (ec == boost::asio::error::operation_aborted) 2426714a25aSJames Feist { 2436714a25aSJames Feist /* we were canceled*/ 2446714a25aSJames Feist return; 2456714a25aSJames Feist } 2466714a25aSJames Feist else if (ec) 2476714a25aSJames Feist { 2486714a25aSJames Feist std::cerr << "timer error\n"; 2496714a25aSJames Feist return; 2506714a25aSJames Feist } 2516714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, 2526714a25aSJames Feist sensorsChanged); 2536714a25aSJames Feist }); 2546714a25aSJames Feist }; 2556714a25aSJames Feist 2569ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 2576714a25aSJames Feist { 2586714a25aSJames Feist auto match = std::make_unique<sdbusplus::bus::match::match>( 2596714a25aSJames Feist static_cast<sdbusplus::bus::bus&>(*systemBus), 2606714a25aSJames Feist "type='signal',member='PropertiesChanged',path_namespace='" + 2619ced0a38SJae Hyun Yoo std::string(inventoryPath) + "',arg0namespace='" + type + "'", 2626714a25aSJames Feist eventHandler); 2636714a25aSJames Feist matches.emplace_back(std::move(match)); 2646714a25aSJames Feist } 2656714a25aSJames Feist 2666714a25aSJames Feist io.run(); 2676714a25aSJames Feist } 268