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> 226714a25aSJames Feist #include <experimental/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 306714a25aSJames Feist namespace fs = std::experimental::filesystem; 31*9ced0a38SJae Hyun Yoo static constexpr std::array<const char*, 2> sensorTypes = { 326714a25aSJames Feist "xyz.openbmc_project.Configuration.TMP75", 336714a25aSJames Feist "xyz.openbmc_project.Configuration.TMP421"}; 346714a25aSJames Feist 356714a25aSJames Feist void createSensors( 366714a25aSJames Feist boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, 376714a25aSJames Feist boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>& 386714a25aSJames Feist sensors, 396714a25aSJames Feist std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 406714a25aSJames Feist const std::unique_ptr<boost::container::flat_set<std::string>>& 416714a25aSJames Feist sensorsChanged) 426714a25aSJames Feist { 436714a25aSJames Feist bool firstScan = sensorsChanged == nullptr; 446714a25aSJames Feist // use new data the first time, then refresh 456714a25aSJames Feist ManagedObjectType sensorConfigurations; 466714a25aSJames Feist bool useCache = false; 47*9ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 486714a25aSJames Feist { 496714a25aSJames Feist if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations, 506714a25aSJames Feist useCache)) 516714a25aSJames Feist { 526714a25aSJames Feist std::cerr << "error communicating to entity manager\n"; 536714a25aSJames Feist return; 546714a25aSJames Feist } 556714a25aSJames Feist useCache = true; 566714a25aSJames Feist } 576714a25aSJames Feist std::vector<fs::path> paths; 58*9ced0a38SJae Hyun Yoo if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths)) 596714a25aSJames Feist { 606714a25aSJames Feist std::cerr << "No temperature sensors in system\n"; 616714a25aSJames Feist return; 626714a25aSJames Feist } 636714a25aSJames Feist 6437266ca9SJames Feist boost::container::flat_set<std::string> directories; 6537266ca9SJames Feist 666714a25aSJames Feist // iterate through all found temp sensors, and try to match them with 676714a25aSJames Feist // configuration 686714a25aSJames Feist for (auto& path : paths) 696714a25aSJames Feist { 706714a25aSJames Feist std::smatch match; 7137266ca9SJames Feist const std::string& pathStr = path.string(); 726714a25aSJames Feist auto directory = path.parent_path(); 736714a25aSJames Feist 7437266ca9SJames Feist auto ret = directories.insert(directory.string()); 7537266ca9SJames Feist if (!ret.second) 766714a25aSJames Feist { 7737266ca9SJames Feist continue; // already searched this path 7837266ca9SJames Feist } 7937266ca9SJames Feist 8037266ca9SJames Feist auto device = fs::path(directory / "device"); 8137266ca9SJames Feist std::string deviceName = fs::canonical(device).stem(); 8237266ca9SJames Feist auto findHyphen = deviceName.find("-"); 8337266ca9SJames Feist if (findHyphen == std::string::npos) 8437266ca9SJames Feist { 8537266ca9SJames Feist std::cerr << "found bad device " << deviceName << "\n"; 866714a25aSJames Feist continue; 876714a25aSJames Feist } 8837266ca9SJames Feist std::string busStr = deviceName.substr(0, findHyphen); 8937266ca9SJames Feist std::string addrStr = deviceName.substr(findHyphen + 1); 9037266ca9SJames Feist 9137266ca9SJames Feist size_t bus = 0; 9237266ca9SJames Feist size_t addr = 0; 9337266ca9SJames Feist try 946714a25aSJames Feist { 9537266ca9SJames Feist bus = std::stoi(busStr); 9637266ca9SJames Feist addr = std::stoi(addrStr, 0, 16); 9737266ca9SJames Feist } 9837266ca9SJames Feist catch (std::invalid_argument) 9937266ca9SJames Feist { 1006714a25aSJames Feist continue; 1016714a25aSJames Feist } 1026714a25aSJames Feist const SensorData* sensorData = nullptr; 1036714a25aSJames Feist const std::string* interfacePath = nullptr; 10437266ca9SJames Feist const char* sensorType = nullptr; 1056714a25aSJames Feist const std::pair<std::string, boost::container::flat_map< 1066714a25aSJames Feist std::string, BasicVariantType>>* 1076714a25aSJames Feist baseConfiguration = nullptr; 10837266ca9SJames Feist 10937266ca9SJames Feist for (const std::pair<sdbusplus::message::object_path, SensorData>& 11037266ca9SJames Feist sensor : sensorConfigurations) 11137266ca9SJames Feist { 11237266ca9SJames Feist sensorData = &(sensor.second); 113*9ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 1146714a25aSJames Feist { 1156714a25aSJames Feist auto sensorBase = sensorData->find(type); 1166714a25aSJames Feist if (sensorBase != sensorData->end()) 1176714a25aSJames Feist { 1186714a25aSJames Feist baseConfiguration = &(*sensorBase); 1196714a25aSJames Feist sensorType = type; 1206714a25aSJames Feist break; 1216714a25aSJames Feist } 1226714a25aSJames Feist } 1236714a25aSJames Feist if (baseConfiguration == nullptr) 1246714a25aSJames Feist { 12537266ca9SJames Feist std::cerr << "error finding base configuration for " 12637266ca9SJames Feist << deviceName << "\n"; 12737266ca9SJames Feist continue; 12837266ca9SJames Feist } 12937266ca9SJames Feist auto configurationBus = baseConfiguration->second.find("Bus"); 13037266ca9SJames Feist auto configurationAddress = 13137266ca9SJames Feist baseConfiguration->second.find("Address"); 13237266ca9SJames Feist 13337266ca9SJames Feist if (configurationBus == baseConfiguration->second.end() || 13437266ca9SJames Feist configurationAddress == baseConfiguration->second.end()) 13537266ca9SJames Feist { 13637266ca9SJames Feist std::cerr << "error finding bus or address in configuration"; 13737266ca9SJames Feist continue; 13837266ca9SJames Feist } 13937266ca9SJames Feist 14037266ca9SJames Feist if (sdbusplus::message::variant_ns::get<uint64_t>( 14137266ca9SJames Feist configurationBus->second) != bus || 14237266ca9SJames Feist sdbusplus::message::variant_ns::get<uint64_t>( 14337266ca9SJames Feist configurationAddress->second) != addr) 14437266ca9SJames Feist { 14537266ca9SJames Feist continue; 14637266ca9SJames Feist } 14737266ca9SJames Feist 14837266ca9SJames Feist interfacePath = &(sensor.first.str); 14937266ca9SJames Feist break; 15037266ca9SJames Feist } 15137266ca9SJames Feist if (interfacePath == nullptr) 15237266ca9SJames Feist { 15337266ca9SJames Feist std::cerr << "failed to find match for " << deviceName << "\n"; 1546714a25aSJames Feist continue; 1556714a25aSJames Feist } 1566714a25aSJames Feist 1576714a25aSJames Feist auto findSensorName = baseConfiguration->second.find("Name"); 1586714a25aSJames Feist if (findSensorName == baseConfiguration->second.end()) 1596714a25aSJames Feist { 1606714a25aSJames Feist std::cerr << "could not determine configuration name for " 16137266ca9SJames Feist << deviceName << "\n"; 1626714a25aSJames Feist continue; 1636714a25aSJames Feist } 1646714a25aSJames Feist std::string sensorName = 1656714a25aSJames Feist sdbusplus::message::variant_ns::get<std::string>( 1666714a25aSJames Feist findSensorName->second); 1676714a25aSJames Feist // on rescans, only update sensors we were signaled by 1686714a25aSJames Feist auto findSensor = sensors.find(sensorName); 1696714a25aSJames Feist if (!firstScan && findSensor != sensors.end()) 1706714a25aSJames Feist { 1716714a25aSJames Feist bool found = false; 1726714a25aSJames Feist for (auto it = sensorsChanged->begin(); it != sensorsChanged->end(); 1736714a25aSJames Feist it++) 1746714a25aSJames Feist { 1756714a25aSJames Feist if (boost::ends_with(*it, findSensor->second->name)) 1766714a25aSJames Feist { 1776714a25aSJames Feist sensorsChanged->erase(it); 1786714a25aSJames Feist findSensor->second = nullptr; 1796714a25aSJames Feist found = true; 1806714a25aSJames Feist break; 1816714a25aSJames Feist } 1826714a25aSJames Feist } 1836714a25aSJames Feist if (!found) 1846714a25aSJames Feist { 1856714a25aSJames Feist continue; 1866714a25aSJames Feist } 1876714a25aSJames Feist } 1886714a25aSJames Feist std::vector<thresholds::Threshold> sensorThresholds; 189*9ced0a38SJae Hyun Yoo if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) 1906714a25aSJames Feist { 1916714a25aSJames Feist std::cerr << "error populating thresholds for " << sensorName 1926714a25aSJames Feist << "\n"; 1936714a25aSJames Feist } 1946714a25aSJames Feist 1956714a25aSJames Feist sensors[sensorName] = std::make_unique<HwmonTempSensor>( 19637266ca9SJames Feist directory.string() + "/temp1_input", sensorType, objectServer, 19737266ca9SJames Feist dbusConnection, io, sensorName, std::move(sensorThresholds), 19837266ca9SJames Feist *interfacePath); 19937266ca9SJames Feist auto findSecondName = baseConfiguration->second.find("Name1"); 20037266ca9SJames Feist if (findSecondName == baseConfiguration->second.end()) 20137266ca9SJames Feist { 20237266ca9SJames Feist continue; 20337266ca9SJames Feist } 20437266ca9SJames Feist 20537266ca9SJames Feist sensorName = sdbusplus::message::variant_ns::get<std::string>( 20637266ca9SJames Feist findSecondName->second); 20737266ca9SJames Feist sensors[sensorName] = std::make_unique<HwmonTempSensor>( 20837266ca9SJames Feist directory.string() + "/temp2_input", sensorType, objectServer, 20937266ca9SJames Feist dbusConnection, io, sensorName, 21037266ca9SJames Feist std::vector<thresholds::Threshold>(), *interfacePath); 2116714a25aSJames Feist } 2126714a25aSJames Feist } 2136714a25aSJames Feist 2146714a25aSJames Feist int main(int argc, char** argv) 2156714a25aSJames Feist { 2166714a25aSJames Feist boost::asio::io_service io; 2176714a25aSJames Feist auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); 2186714a25aSJames Feist systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); 2196714a25aSJames Feist sdbusplus::asio::object_server objectServer(systemBus); 2206714a25aSJames Feist boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>> 2216714a25aSJames Feist sensors; 2226714a25aSJames Feist std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; 2236714a25aSJames Feist std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = 2246714a25aSJames Feist std::make_unique<boost::container::flat_set<std::string>>(); 2256714a25aSJames Feist 2266714a25aSJames Feist io.post([&]() { 2276714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, nullptr); 2286714a25aSJames Feist }); 2296714a25aSJames Feist 2306714a25aSJames Feist boost::asio::deadline_timer filterTimer(io); 2316714a25aSJames Feist std::function<void(sdbusplus::message::message&)> eventHandler = 2326714a25aSJames Feist [&](sdbusplus::message::message& message) { 2336714a25aSJames Feist if (message.is_method_error()) 2346714a25aSJames Feist { 2356714a25aSJames Feist std::cerr << "callback method error\n"; 2366714a25aSJames Feist return; 2376714a25aSJames Feist } 2386714a25aSJames Feist sensorsChanged->insert(message.get_path()); 2396714a25aSJames Feist // this implicitly cancels the timer 2406714a25aSJames Feist filterTimer.expires_from_now(boost::posix_time::seconds(1)); 2416714a25aSJames Feist 2426714a25aSJames Feist filterTimer.async_wait([&](const boost::system::error_code& ec) { 2436714a25aSJames Feist if (ec == boost::asio::error::operation_aborted) 2446714a25aSJames Feist { 2456714a25aSJames Feist /* we were canceled*/ 2466714a25aSJames Feist return; 2476714a25aSJames Feist } 2486714a25aSJames Feist else if (ec) 2496714a25aSJames Feist { 2506714a25aSJames Feist std::cerr << "timer error\n"; 2516714a25aSJames Feist return; 2526714a25aSJames Feist } 2536714a25aSJames Feist createSensors(io, objectServer, sensors, systemBus, 2546714a25aSJames Feist sensorsChanged); 2556714a25aSJames Feist }); 2566714a25aSJames Feist }; 2576714a25aSJames Feist 258*9ced0a38SJae Hyun Yoo for (const char* type : sensorTypes) 2596714a25aSJames Feist { 2606714a25aSJames Feist auto match = std::make_unique<sdbusplus::bus::match::match>( 2616714a25aSJames Feist static_cast<sdbusplus::bus::bus&>(*systemBus), 2626714a25aSJames Feist "type='signal',member='PropertiesChanged',path_namespace='" + 263*9ced0a38SJae Hyun Yoo std::string(inventoryPath) + "',arg0namespace='" + type + "'", 2646714a25aSJames Feist eventHandler); 2656714a25aSJames Feist matches.emplace_back(std::move(match)); 2666714a25aSJames Feist } 2676714a25aSJames Feist 2686714a25aSJames Feist io.run(); 2696714a25aSJames Feist } 270