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 17cf3bce6eSJames Feist #include "filesystem.hpp" 18cf3bce6eSJames Feist 196714a25aSJames Feist #include <HwmonTempSensor.hpp> 206714a25aSJames Feist #include <Utils.hpp> 216714a25aSJames Feist #include <boost/algorithm/string/predicate.hpp> 226714a25aSJames Feist #include <boost/algorithm/string/replace.hpp> 236714a25aSJames Feist #include <boost/container/flat_set.hpp> 246714a25aSJames Feist #include <fstream> 256714a25aSJames Feist #include <regex> 266714a25aSJames Feist #include <sdbusplus/asio/connection.hpp> 276714a25aSJames Feist #include <sdbusplus/asio/object_server.hpp> 286714a25aSJames Feist 296714a25aSJames Feist static constexpr bool DEBUG = false; 306714a25aSJames Feist 31cf3bce6eSJames Feist namespace fs = std::filesystem; 329ced0a38SJae Hyun Yoo static constexpr std::array<const char*, 2> sensorTypes = { 336714a25aSJames Feist "xyz.openbmc_project.Configuration.TMP75", 346714a25aSJames Feist "xyz.openbmc_project.Configuration.TMP421"}; 356714a25aSJames Feist 366714a25aSJames Feist void createSensors( 376714a25aSJames Feist boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, 386714a25aSJames Feist boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>& 396714a25aSJames Feist sensors, 406714a25aSJames Feist std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 416714a25aSJames Feist const std::unique_ptr<boost::container::flat_set<std::string>>& 426714a25aSJames Feist sensorsChanged) 436714a25aSJames Feist { 446714a25aSJames Feist bool firstScan = sensorsChanged == nullptr; 456714a25aSJames Feist // use new data the first time, then refresh 466714a25aSJames Feist ManagedObjectType sensorConfigurations; 476714a25aSJames Feist bool useCache = false; 489ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 496714a25aSJames Feist { 506714a25aSJames Feist if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations, 516714a25aSJames Feist useCache)) 526714a25aSJames Feist { 536714a25aSJames Feist std::cerr << "error communicating to entity manager\n"; 546714a25aSJames Feist return; 556714a25aSJames Feist } 566714a25aSJames Feist useCache = true; 576714a25aSJames Feist } 586714a25aSJames Feist std::vector<fs::path> paths; 599ced0a38SJae Hyun Yoo if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths)) 606714a25aSJames Feist { 616714a25aSJames Feist std::cerr << "No temperature sensors in system\n"; 626714a25aSJames Feist return; 636714a25aSJames Feist } 646714a25aSJames Feist 6537266ca9SJames Feist boost::container::flat_set<std::string> directories; 6637266ca9SJames Feist 676714a25aSJames Feist // iterate through all found temp sensors, and try to match them with 686714a25aSJames Feist // configuration 696714a25aSJames Feist for (auto& path : paths) 706714a25aSJames Feist { 716714a25aSJames Feist std::smatch match; 7237266ca9SJames Feist const std::string& pathStr = path.string(); 736714a25aSJames Feist auto directory = path.parent_path(); 746714a25aSJames Feist 7537266ca9SJames Feist auto ret = directories.insert(directory.string()); 7637266ca9SJames Feist if (!ret.second) 776714a25aSJames Feist { 7837266ca9SJames Feist continue; // already searched this path 7937266ca9SJames Feist } 8037266ca9SJames Feist 8137266ca9SJames Feist auto device = fs::path(directory / "device"); 8237266ca9SJames Feist std::string deviceName = fs::canonical(device).stem(); 8337266ca9SJames Feist auto findHyphen = deviceName.find("-"); 8437266ca9SJames Feist if (findHyphen == std::string::npos) 8537266ca9SJames Feist { 8637266ca9SJames Feist std::cerr << "found bad device " << deviceName << "\n"; 876714a25aSJames Feist continue; 886714a25aSJames Feist } 8937266ca9SJames Feist std::string busStr = deviceName.substr(0, findHyphen); 9037266ca9SJames Feist std::string addrStr = deviceName.substr(findHyphen + 1); 9137266ca9SJames Feist 9237266ca9SJames Feist size_t bus = 0; 9337266ca9SJames Feist size_t addr = 0; 9437266ca9SJames Feist try 956714a25aSJames Feist { 9637266ca9SJames Feist bus = std::stoi(busStr); 9737266ca9SJames Feist addr = std::stoi(addrStr, 0, 16); 9837266ca9SJames Feist } 9937266ca9SJames Feist catch (std::invalid_argument) 10037266ca9SJames Feist { 1016714a25aSJames Feist continue; 1026714a25aSJames Feist } 1036714a25aSJames Feist const SensorData* sensorData = nullptr; 1046714a25aSJames Feist const std::string* interfacePath = nullptr; 10537266ca9SJames Feist const char* sensorType = nullptr; 1066714a25aSJames Feist const std::pair<std::string, boost::container::flat_map< 1076714a25aSJames Feist std::string, BasicVariantType>>* 1086714a25aSJames Feist baseConfiguration = nullptr; 10937266ca9SJames Feist 11037266ca9SJames Feist for (const std::pair<sdbusplus::message::object_path, SensorData>& 11137266ca9SJames Feist sensor : sensorConfigurations) 11237266ca9SJames Feist { 11337266ca9SJames Feist sensorData = &(sensor.second); 1149ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 1156714a25aSJames Feist { 1166714a25aSJames Feist auto sensorBase = sensorData->find(type); 1176714a25aSJames Feist if (sensorBase != sensorData->end()) 1186714a25aSJames Feist { 1196714a25aSJames Feist baseConfiguration = &(*sensorBase); 1206714a25aSJames Feist sensorType = type; 1216714a25aSJames Feist break; 1226714a25aSJames Feist } 1236714a25aSJames Feist } 1246714a25aSJames Feist if (baseConfiguration == nullptr) 1256714a25aSJames Feist { 12637266ca9SJames Feist std::cerr << "error finding base configuration for " 12737266ca9SJames Feist << deviceName << "\n"; 12837266ca9SJames Feist continue; 12937266ca9SJames Feist } 13037266ca9SJames Feist auto configurationBus = baseConfiguration->second.find("Bus"); 13137266ca9SJames Feist auto configurationAddress = 13237266ca9SJames Feist baseConfiguration->second.find("Address"); 13337266ca9SJames Feist 13437266ca9SJames Feist if (configurationBus == baseConfiguration->second.end() || 13537266ca9SJames Feist configurationAddress == baseConfiguration->second.end()) 13637266ca9SJames Feist { 13737266ca9SJames Feist std::cerr << "error finding bus or address in configuration"; 13837266ca9SJames Feist continue; 13937266ca9SJames Feist } 14037266ca9SJames Feist 141*3eb82629SJames Feist if (std::get<uint64_t>(configurationBus->second) != bus || 142*3eb82629SJames Feist std::get<uint64_t>(configurationAddress->second) != addr) 14337266ca9SJames Feist { 14437266ca9SJames Feist continue; 14537266ca9SJames Feist } 14637266ca9SJames Feist 14737266ca9SJames Feist interfacePath = &(sensor.first.str); 14837266ca9SJames Feist break; 14937266ca9SJames Feist } 15037266ca9SJames Feist if (interfacePath == nullptr) 15137266ca9SJames Feist { 15237266ca9SJames Feist std::cerr << "failed to find match for " << deviceName << "\n"; 1536714a25aSJames Feist continue; 1546714a25aSJames Feist } 1556714a25aSJames Feist 1566714a25aSJames Feist auto findSensorName = baseConfiguration->second.find("Name"); 1576714a25aSJames Feist if (findSensorName == baseConfiguration->second.end()) 1586714a25aSJames Feist { 1596714a25aSJames Feist std::cerr << "could not determine configuration name for " 16037266ca9SJames Feist << deviceName << "\n"; 1616714a25aSJames Feist continue; 1626714a25aSJames Feist } 163*3eb82629SJames Feist std::string sensorName = std::get<std::string>(findSensorName->second); 1646714a25aSJames Feist // on rescans, only update sensors we were signaled by 1656714a25aSJames Feist auto findSensor = sensors.find(sensorName); 1666714a25aSJames Feist if (!firstScan && findSensor != sensors.end()) 1676714a25aSJames Feist { 1686714a25aSJames Feist bool found = false; 1696714a25aSJames Feist for (auto it = sensorsChanged->begin(); it != sensorsChanged->end(); 1706714a25aSJames Feist it++) 1716714a25aSJames Feist { 1726714a25aSJames Feist if (boost::ends_with(*it, findSensor->second->name)) 1736714a25aSJames Feist { 1746714a25aSJames Feist sensorsChanged->erase(it); 1756714a25aSJames Feist findSensor->second = nullptr; 1766714a25aSJames Feist found = true; 1776714a25aSJames Feist break; 1786714a25aSJames Feist } 1796714a25aSJames Feist } 1806714a25aSJames Feist if (!found) 1816714a25aSJames Feist { 1826714a25aSJames Feist continue; 1836714a25aSJames Feist } 1846714a25aSJames Feist } 1856714a25aSJames Feist std::vector<thresholds::Threshold> sensorThresholds; 1869ced0a38SJae Hyun Yoo if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) 1876714a25aSJames Feist { 1886714a25aSJames Feist std::cerr << "error populating thresholds for " << sensorName 1896714a25aSJames Feist << "\n"; 1906714a25aSJames Feist } 1916714a25aSJames Feist 1926714a25aSJames Feist sensors[sensorName] = std::make_unique<HwmonTempSensor>( 19337266ca9SJames Feist directory.string() + "/temp1_input", sensorType, objectServer, 19437266ca9SJames Feist dbusConnection, io, sensorName, std::move(sensorThresholds), 19537266ca9SJames Feist *interfacePath); 19637266ca9SJames Feist auto findSecondName = baseConfiguration->second.find("Name1"); 19737266ca9SJames Feist if (findSecondName == baseConfiguration->second.end()) 19837266ca9SJames Feist { 19937266ca9SJames Feist continue; 20037266ca9SJames Feist } 20137266ca9SJames Feist 202*3eb82629SJames Feist sensorName = std::get<std::string>(findSecondName->second); 20337266ca9SJames Feist sensors[sensorName] = std::make_unique<HwmonTempSensor>( 20437266ca9SJames Feist directory.string() + "/temp2_input", sensorType, objectServer, 20537266ca9SJames Feist dbusConnection, io, sensorName, 20637266ca9SJames Feist std::vector<thresholds::Threshold>(), *interfacePath); 2076714a25aSJames Feist } 2086714a25aSJames Feist } 2096714a25aSJames Feist 2106714a25aSJames Feist int main(int argc, char** argv) 2116714a25aSJames Feist { 2126714a25aSJames Feist boost::asio::io_service io; 2136714a25aSJames Feist auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); 2146714a25aSJames Feist systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); 2156714a25aSJames Feist sdbusplus::asio::object_server objectServer(systemBus); 2166714a25aSJames Feist boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>> 2176714a25aSJames Feist sensors; 2186714a25aSJames Feist std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; 2196714a25aSJames Feist std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = 2206714a25aSJames Feist std::make_unique<boost::container::flat_set<std::string>>(); 2216714a25aSJames Feist 2226714a25aSJames Feist io.post([&]() { 2236714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, nullptr); 2246714a25aSJames Feist }); 2256714a25aSJames Feist 2266714a25aSJames Feist boost::asio::deadline_timer filterTimer(io); 2276714a25aSJames Feist std::function<void(sdbusplus::message::message&)> eventHandler = 2286714a25aSJames Feist [&](sdbusplus::message::message& message) { 2296714a25aSJames Feist if (message.is_method_error()) 2306714a25aSJames Feist { 2316714a25aSJames Feist std::cerr << "callback method error\n"; 2326714a25aSJames Feist return; 2336714a25aSJames Feist } 2346714a25aSJames Feist sensorsChanged->insert(message.get_path()); 2356714a25aSJames Feist // this implicitly cancels the timer 2366714a25aSJames Feist filterTimer.expires_from_now(boost::posix_time::seconds(1)); 2376714a25aSJames Feist 2386714a25aSJames Feist filterTimer.async_wait([&](const boost::system::error_code& ec) { 2396714a25aSJames Feist if (ec == boost::asio::error::operation_aborted) 2406714a25aSJames Feist { 2416714a25aSJames Feist /* we were canceled*/ 2426714a25aSJames Feist return; 2436714a25aSJames Feist } 2446714a25aSJames Feist else if (ec) 2456714a25aSJames Feist { 2466714a25aSJames Feist std::cerr << "timer error\n"; 2476714a25aSJames Feist return; 2486714a25aSJames Feist } 2496714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, 2506714a25aSJames Feist sensorsChanged); 2516714a25aSJames Feist }); 2526714a25aSJames Feist }; 2536714a25aSJames Feist 2549ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 2556714a25aSJames Feist { 2566714a25aSJames Feist auto match = std::make_unique<sdbusplus::bus::match::match>( 2576714a25aSJames Feist static_cast<sdbusplus::bus::bus&>(*systemBus), 2586714a25aSJames Feist "type='signal',member='PropertiesChanged',path_namespace='" + 2599ced0a38SJae Hyun Yoo std::string(inventoryPath) + "',arg0namespace='" + type + "'", 2606714a25aSJames Feist eventHandler); 2616714a25aSJames Feist matches.emplace_back(std::move(match)); 2626714a25aSJames Feist } 2636714a25aSJames Feist 2646714a25aSJames Feist io.run(); 2656714a25aSJames Feist } 266