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*55ab2afbSJohn Wang static constexpr std::array<const char*, 4> sensorTypes = {
326714a25aSJames Feist     "xyz.openbmc_project.Configuration.TMP75",
33*55ab2afbSJohn Wang     "xyz.openbmc_project.Configuration.TMP421",
34*55ab2afbSJohn Wang     "xyz.openbmc_project.Configuration.TMP112",
35*55ab2afbSJohn Wang     "xyz.openbmc_project.Configuration.EMC1413"};
366714a25aSJames Feist 
376714a25aSJames Feist void createSensors(
386714a25aSJames Feist     boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
396714a25aSJames Feist     boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>&
406714a25aSJames Feist         sensors,
416714a25aSJames Feist     std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
426714a25aSJames Feist     const std::unique_ptr<boost::container::flat_set<std::string>>&
436714a25aSJames Feist         sensorsChanged)
446714a25aSJames Feist {
456714a25aSJames Feist     bool firstScan = sensorsChanged == nullptr;
466714a25aSJames Feist     // use new data the first time, then refresh
476714a25aSJames Feist     ManagedObjectType sensorConfigurations;
486714a25aSJames Feist     bool useCache = false;
499ced0a38SJae Hyun Yoo     for (const char* type : sensorTypes)
506714a25aSJames Feist     {
516714a25aSJames Feist         if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
526714a25aSJames Feist                                     useCache))
536714a25aSJames Feist         {
546714a25aSJames Feist             std::cerr << "error communicating to entity manager\n";
556714a25aSJames Feist             return;
566714a25aSJames Feist         }
576714a25aSJames Feist         useCache = true;
586714a25aSJames Feist     }
596714a25aSJames Feist     std::vector<fs::path> paths;
609ced0a38SJae Hyun Yoo     if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths))
616714a25aSJames Feist     {
626714a25aSJames Feist         std::cerr << "No temperature sensors in system\n";
636714a25aSJames Feist         return;
646714a25aSJames Feist     }
656714a25aSJames Feist 
6637266ca9SJames Feist     boost::container::flat_set<std::string> directories;
6737266ca9SJames Feist 
686714a25aSJames Feist     // iterate through all found temp sensors, and try to match them with
696714a25aSJames Feist     // configuration
706714a25aSJames Feist     for (auto& path : paths)
716714a25aSJames Feist     {
726714a25aSJames Feist         std::smatch match;
7337266ca9SJames Feist         const std::string& pathStr = path.string();
746714a25aSJames Feist         auto directory = path.parent_path();
756714a25aSJames Feist 
7637266ca9SJames Feist         auto ret = directories.insert(directory.string());
7737266ca9SJames Feist         if (!ret.second)
786714a25aSJames Feist         {
7937266ca9SJames Feist             continue; // already searched this path
8037266ca9SJames Feist         }
8137266ca9SJames Feist 
8237266ca9SJames Feist         auto device = fs::path(directory / "device");
8337266ca9SJames Feist         std::string deviceName = fs::canonical(device).stem();
8437266ca9SJames Feist         auto findHyphen = deviceName.find("-");
8537266ca9SJames Feist         if (findHyphen == std::string::npos)
8637266ca9SJames Feist         {
8737266ca9SJames Feist             std::cerr << "found bad device " << deviceName << "\n";
886714a25aSJames Feist             continue;
896714a25aSJames Feist         }
9037266ca9SJames Feist         std::string busStr = deviceName.substr(0, findHyphen);
9137266ca9SJames Feist         std::string addrStr = deviceName.substr(findHyphen + 1);
9237266ca9SJames Feist 
9337266ca9SJames Feist         size_t bus = 0;
9437266ca9SJames Feist         size_t addr = 0;
9537266ca9SJames Feist         try
966714a25aSJames Feist         {
9737266ca9SJames Feist             bus = std::stoi(busStr);
9837266ca9SJames Feist             addr = std::stoi(addrStr, 0, 16);
9937266ca9SJames Feist         }
10037266ca9SJames Feist         catch (std::invalid_argument)
10137266ca9SJames Feist         {
1026714a25aSJames Feist             continue;
1036714a25aSJames Feist         }
1046714a25aSJames Feist         const SensorData* sensorData = nullptr;
1056714a25aSJames Feist         const std::string* interfacePath = nullptr;
10637266ca9SJames Feist         const char* sensorType = nullptr;
1076714a25aSJames Feist         const std::pair<std::string, boost::container::flat_map<
1086714a25aSJames Feist                                          std::string, BasicVariantType>>*
1096714a25aSJames Feist             baseConfiguration = nullptr;
11037266ca9SJames Feist 
11137266ca9SJames Feist         for (const std::pair<sdbusplus::message::object_path, SensorData>&
11237266ca9SJames Feist                  sensor : sensorConfigurations)
11337266ca9SJames Feist         {
11437266ca9SJames Feist             sensorData = &(sensor.second);
1159ced0a38SJae Hyun Yoo             for (const char* type : sensorTypes)
1166714a25aSJames Feist             {
1176714a25aSJames Feist                 auto sensorBase = sensorData->find(type);
1186714a25aSJames Feist                 if (sensorBase != sensorData->end())
1196714a25aSJames Feist                 {
1206714a25aSJames Feist                     baseConfiguration = &(*sensorBase);
1216714a25aSJames Feist                     sensorType = type;
1226714a25aSJames Feist                     break;
1236714a25aSJames Feist                 }
1246714a25aSJames Feist             }
1256714a25aSJames Feist             if (baseConfiguration == nullptr)
1266714a25aSJames Feist             {
12737266ca9SJames Feist                 std::cerr << "error finding base configuration for "
12837266ca9SJames Feist                           << deviceName << "\n";
12937266ca9SJames Feist                 continue;
13037266ca9SJames Feist             }
13137266ca9SJames Feist             auto configurationBus = baseConfiguration->second.find("Bus");
13237266ca9SJames Feist             auto configurationAddress =
13337266ca9SJames Feist                 baseConfiguration->second.find("Address");
13437266ca9SJames Feist 
13537266ca9SJames Feist             if (configurationBus == baseConfiguration->second.end() ||
13637266ca9SJames Feist                 configurationAddress == baseConfiguration->second.end())
13737266ca9SJames Feist             {
13837266ca9SJames Feist                 std::cerr << "error finding bus or address in configuration";
13937266ca9SJames Feist                 continue;
14037266ca9SJames Feist             }
14137266ca9SJames Feist 
1423eb82629SJames Feist             if (std::get<uint64_t>(configurationBus->second) != bus ||
1433eb82629SJames Feist                 std::get<uint64_t>(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         }
1643eb82629SJames Feist         std::string sensorName = std::get<std::string>(findSensorName->second);
1656714a25aSJames Feist         // on rescans, only update sensors we were signaled by
1666714a25aSJames Feist         auto findSensor = sensors.find(sensorName);
1676714a25aSJames Feist         if (!firstScan && findSensor != sensors.end())
1686714a25aSJames Feist         {
1696714a25aSJames Feist             bool found = false;
1706714a25aSJames Feist             for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
1716714a25aSJames Feist                  it++)
1726714a25aSJames Feist             {
1736714a25aSJames Feist                 if (boost::ends_with(*it, findSensor->second->name))
1746714a25aSJames Feist                 {
1756714a25aSJames Feist                     sensorsChanged->erase(it);
1766714a25aSJames Feist                     findSensor->second = nullptr;
1776714a25aSJames Feist                     found = true;
1786714a25aSJames Feist                     break;
1796714a25aSJames Feist                 }
1806714a25aSJames Feist             }
1816714a25aSJames Feist             if (!found)
1826714a25aSJames Feist             {
1836714a25aSJames Feist                 continue;
1846714a25aSJames Feist             }
1856714a25aSJames Feist         }
1866714a25aSJames Feist         std::vector<thresholds::Threshold> sensorThresholds;
1879ced0a38SJae Hyun Yoo         if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
1886714a25aSJames Feist         {
1896714a25aSJames Feist             std::cerr << "error populating thresholds for " << sensorName
1906714a25aSJames Feist                       << "\n";
1916714a25aSJames Feist         }
1926714a25aSJames Feist 
1936714a25aSJames Feist         sensors[sensorName] = std::make_unique<HwmonTempSensor>(
19437266ca9SJames Feist             directory.string() + "/temp1_input", sensorType, objectServer,
19537266ca9SJames Feist             dbusConnection, io, sensorName, std::move(sensorThresholds),
19637266ca9SJames Feist             *interfacePath);
19737266ca9SJames Feist         auto findSecondName = baseConfiguration->second.find("Name1");
19837266ca9SJames Feist         if (findSecondName == baseConfiguration->second.end())
19937266ca9SJames Feist         {
20037266ca9SJames Feist             continue;
20137266ca9SJames Feist         }
20237266ca9SJames Feist 
2033eb82629SJames Feist         sensorName = std::get<std::string>(findSecondName->second);
20437266ca9SJames Feist         sensors[sensorName] = std::make_unique<HwmonTempSensor>(
20537266ca9SJames Feist             directory.string() + "/temp2_input", sensorType, objectServer,
20637266ca9SJames Feist             dbusConnection, io, sensorName,
20737266ca9SJames Feist             std::vector<thresholds::Threshold>(), *interfacePath);
2086714a25aSJames Feist     }
2096714a25aSJames Feist }
2106714a25aSJames Feist 
2116714a25aSJames Feist int main(int argc, char** argv)
2126714a25aSJames Feist {
2136714a25aSJames Feist     boost::asio::io_service io;
2146714a25aSJames Feist     auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
2156714a25aSJames Feist     systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
2166714a25aSJames Feist     sdbusplus::asio::object_server objectServer(systemBus);
2176714a25aSJames Feist     boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
2186714a25aSJames Feist         sensors;
2196714a25aSJames Feist     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
2206714a25aSJames Feist     std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
2216714a25aSJames Feist         std::make_unique<boost::container::flat_set<std::string>>();
2226714a25aSJames Feist 
2236714a25aSJames Feist     io.post([&]() {
2246714a25aSJames Feist         createSensors(io, objectServer, sensors, systemBus, nullptr);
2256714a25aSJames Feist     });
2266714a25aSJames Feist 
2276714a25aSJames Feist     boost::asio::deadline_timer filterTimer(io);
2286714a25aSJames Feist     std::function<void(sdbusplus::message::message&)> eventHandler =
2296714a25aSJames Feist         [&](sdbusplus::message::message& message) {
2306714a25aSJames Feist             if (message.is_method_error())
2316714a25aSJames Feist             {
2326714a25aSJames Feist                 std::cerr << "callback method error\n";
2336714a25aSJames Feist                 return;
2346714a25aSJames Feist             }
2356714a25aSJames Feist             sensorsChanged->insert(message.get_path());
2366714a25aSJames Feist             // this implicitly cancels the timer
2376714a25aSJames Feist             filterTimer.expires_from_now(boost::posix_time::seconds(1));
2386714a25aSJames Feist 
2396714a25aSJames Feist             filterTimer.async_wait([&](const boost::system::error_code& ec) {
2406714a25aSJames Feist                 if (ec == boost::asio::error::operation_aborted)
2416714a25aSJames Feist                 {
2426714a25aSJames Feist                     /* we were canceled*/
2436714a25aSJames Feist                     return;
2446714a25aSJames Feist                 }
2456714a25aSJames Feist                 else if (ec)
2466714a25aSJames Feist                 {
2476714a25aSJames Feist                     std::cerr << "timer error\n";
2486714a25aSJames Feist                     return;
2496714a25aSJames Feist                 }
2506714a25aSJames Feist                 createSensors(io, objectServer, sensors, systemBus,
2516714a25aSJames Feist                               sensorsChanged);
2526714a25aSJames Feist             });
2536714a25aSJames Feist         };
2546714a25aSJames Feist 
2559ced0a38SJae Hyun Yoo     for (const char* type : sensorTypes)
2566714a25aSJames Feist     {
2576714a25aSJames Feist         auto match = std::make_unique<sdbusplus::bus::match::match>(
2586714a25aSJames Feist             static_cast<sdbusplus::bus::bus&>(*systemBus),
2596714a25aSJames Feist             "type='signal',member='PropertiesChanged',path_namespace='" +
2609ced0a38SJae Hyun Yoo                 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
2616714a25aSJames Feist             eventHandler);
2626714a25aSJames Feist         matches.emplace_back(std::move(match));
2636714a25aSJames Feist     }
2646714a25aSJames Feist 
2656714a25aSJames Feist     io.run();
2666714a25aSJames Feist }
267