James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 17 | #include "HwmonTempSensor.hpp" |
| 18 | #include "Utils.hpp" |
| 19 | |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 20 | #include <array> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <boost/algorithm/string/predicate.hpp> |
| 22 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | #include <boost/container/flat_set.hpp> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 25 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 26 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 27 | #include <functional> |
| 28 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | #include <regex> |
| 30 | #include <sdbusplus/asio/connection.hpp> |
| 31 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <sdbusplus/bus/match.hpp> |
| 33 | #include <stdexcept> |
| 34 | #include <string> |
| 35 | #include <utility> |
| 36 | #include <variant> |
| 37 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 38 | |
| 39 | static constexpr bool DEBUG = false; |
| 40 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 41 | namespace fs = std::filesystem; |
Patrick Venture | 3546adb | 2019-08-14 18:35:29 -0700 | [diff] [blame] | 42 | static constexpr std::array<const char*, 7> sensorTypes = { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | "xyz.openbmc_project.Configuration.TMP75", |
John Wang | 55ab2af | 2019-04-18 14:22:31 +0800 | [diff] [blame] | 44 | "xyz.openbmc_project.Configuration.TMP421", |
Patrick Venture | 7fa475d | 2019-08-10 08:49:01 -0700 | [diff] [blame] | 45 | "xyz.openbmc_project.Configuration.TMP441", |
John Wang | 55ab2af | 2019-04-18 14:22:31 +0800 | [diff] [blame] | 46 | "xyz.openbmc_project.Configuration.TMP112", |
Patrick Venture | 3546adb | 2019-08-14 18:35:29 -0700 | [diff] [blame] | 47 | "xyz.openbmc_project.Configuration.TMP175", |
Patrick Venture | bd1a9d5 | 2019-08-14 18:10:26 -0700 | [diff] [blame] | 48 | "xyz.openbmc_project.Configuration.EMC1413", |
| 49 | "xyz.openbmc_project.Configuration.MAX31725"}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 50 | |
| 51 | void createSensors( |
| 52 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 53 | boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>& |
| 54 | sensors, |
| 55 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 56 | const std::unique_ptr<boost::container::flat_set<std::string>>& |
| 57 | sensorsChanged) |
| 58 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 59 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 60 | dbusConnection, |
| 61 | std::move([&io, &objectServer, &sensors, &dbusConnection, |
| 62 | &sensorsChanged]( |
| 63 | const ManagedObjectType& sensorConfigurations) { |
| 64 | bool firstScan = sensorsChanged == nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 66 | std::vector<fs::path> paths; |
| 67 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", |
| 68 | paths)) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 69 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 70 | std::cerr << "No temperature sensors in system\n"; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | boost::container::flat_set<std::string> directories; |
| 75 | |
| 76 | // iterate through all found temp sensors, and try to match them |
| 77 | // with configuration |
| 78 | for (auto& path : paths) |
| 79 | { |
| 80 | std::smatch match; |
| 81 | const std::string& pathStr = path.string(); |
| 82 | auto directory = path.parent_path(); |
| 83 | |
| 84 | auto ret = directories.insert(directory.string()); |
| 85 | if (!ret.second) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 86 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 87 | continue; // already searched this path |
| 88 | } |
| 89 | |
| 90 | fs::path device = directory / "device"; |
| 91 | std::string deviceName = fs::canonical(device).stem(); |
| 92 | auto findHyphen = deviceName.find("-"); |
| 93 | if (findHyphen == std::string::npos) |
| 94 | { |
| 95 | std::cerr << "found bad device " << deviceName << "\n"; |
| 96 | continue; |
| 97 | } |
| 98 | std::string busStr = deviceName.substr(0, findHyphen); |
| 99 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 100 | |
| 101 | size_t bus = 0; |
| 102 | size_t addr = 0; |
| 103 | try |
| 104 | { |
| 105 | bus = std::stoi(busStr); |
| 106 | addr = std::stoi(addrStr, 0, 16); |
| 107 | } |
| 108 | catch (std::invalid_argument&) |
| 109 | { |
| 110 | continue; |
| 111 | } |
| 112 | const SensorData* sensorData = nullptr; |
| 113 | const std::string* interfacePath = nullptr; |
| 114 | const char* sensorType = nullptr; |
| 115 | const std::pair< |
| 116 | std::string, |
| 117 | boost::container::flat_map<std::string, BasicVariantType>>* |
| 118 | baseConfiguration = nullptr; |
| 119 | |
| 120 | for (const std::pair<sdbusplus::message::object_path, |
| 121 | SensorData>& sensor : sensorConfigurations) |
| 122 | { |
| 123 | sensorData = &(sensor.second); |
| 124 | for (const char* type : sensorTypes) |
| 125 | { |
| 126 | auto sensorBase = sensorData->find(type); |
| 127 | if (sensorBase != sensorData->end()) |
| 128 | { |
| 129 | baseConfiguration = &(*sensorBase); |
| 130 | sensorType = type; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | if (baseConfiguration == nullptr) |
| 135 | { |
| 136 | std::cerr << "error finding base configuration for " |
| 137 | << deviceName << "\n"; |
| 138 | continue; |
| 139 | } |
| 140 | auto configurationBus = |
| 141 | baseConfiguration->second.find("Bus"); |
| 142 | auto configurationAddress = |
| 143 | baseConfiguration->second.find("Address"); |
| 144 | |
| 145 | if (configurationBus == baseConfiguration->second.end() || |
| 146 | configurationAddress == baseConfiguration->second.end()) |
| 147 | { |
| 148 | std::cerr |
| 149 | << "error finding bus or address in configuration"; |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | if (std::get<uint64_t>(configurationBus->second) != bus || |
| 154 | std::get<uint64_t>(configurationAddress->second) != |
| 155 | addr) |
| 156 | { |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | interfacePath = &(sensor.first.str); |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 161 | break; |
| 162 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 163 | if (interfacePath == nullptr) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 164 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 165 | std::cerr << "failed to find match for " << deviceName |
| 166 | << "\n"; |
| 167 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 169 | |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 170 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 171 | if (findSensorName == baseConfiguration->second.end()) |
| 172 | { |
| 173 | std::cerr << "could not determine configuration name for " |
| 174 | << deviceName << "\n"; |
| 175 | continue; |
| 176 | } |
| 177 | std::string sensorName = |
| 178 | std::get<std::string>(findSensorName->second); |
| 179 | // on rescans, only update sensors we were signaled by |
| 180 | auto findSensor = sensors.find(sensorName); |
| 181 | if (!firstScan && findSensor != sensors.end()) |
| 182 | { |
| 183 | bool found = false; |
| 184 | for (auto it = sensorsChanged->begin(); |
| 185 | it != sensorsChanged->end(); it++) |
| 186 | { |
| 187 | if (boost::ends_with(*it, findSensor->second->name)) |
| 188 | { |
| 189 | sensorsChanged->erase(it); |
| 190 | findSensor->second = nullptr; |
| 191 | found = true; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | if (!found) |
| 196 | { |
| 197 | continue; |
| 198 | } |
| 199 | } |
| 200 | std::vector<thresholds::Threshold> sensorThresholds; |
| 201 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
| 202 | { |
| 203 | std::cerr << "error populating thresholds for " |
| 204 | << sensorName << "\n"; |
| 205 | } |
| 206 | auto& sensor1 = sensors[sensorName]; |
| 207 | sensor1 = nullptr; |
| 208 | sensor1 = std::make_unique<HwmonTempSensor>( |
| 209 | directory.string() + "/temp1_input", sensorType, |
| 210 | objectServer, dbusConnection, io, sensorName, |
| 211 | std::move(sensorThresholds), *interfacePath); |
| 212 | auto findSecondName = baseConfiguration->second.find("Name1"); |
| 213 | if (findSecondName == baseConfiguration->second.end()) |
| 214 | { |
| 215 | continue; |
| 216 | } |
| 217 | sensorName = std::get<std::string>(findSecondName->second); |
| 218 | auto& sensor2 = sensors[sensorName]; |
| 219 | sensor2 = nullptr; |
| 220 | sensor2 = std::make_unique<HwmonTempSensor>( |
| 221 | directory.string() + "/temp2_input", sensorType, |
| 222 | objectServer, dbusConnection, io, sensorName, |
| 223 | std::vector<thresholds::Threshold>(), *interfacePath); |
| 224 | } |
| 225 | })); |
| 226 | getter->getConfiguration( |
| 227 | std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 228 | } |
| 229 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 230 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 231 | { |
| 232 | boost::asio::io_service io; |
| 233 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 234 | systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); |
| 235 | sdbusplus::asio::object_server objectServer(systemBus); |
| 236 | boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>> |
| 237 | sensors; |
| 238 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 239 | std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = |
| 240 | std::make_unique<boost::container::flat_set<std::string>>(); |
| 241 | |
| 242 | io.post([&]() { |
| 243 | createSensors(io, objectServer, sensors, systemBus, nullptr); |
| 244 | }); |
| 245 | |
| 246 | boost::asio::deadline_timer filterTimer(io); |
| 247 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 248 | [&](sdbusplus::message::message& message) { |
| 249 | if (message.is_method_error()) |
| 250 | { |
| 251 | std::cerr << "callback method error\n"; |
| 252 | return; |
| 253 | } |
| 254 | sensorsChanged->insert(message.get_path()); |
| 255 | // this implicitly cancels the timer |
| 256 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 257 | |
| 258 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 259 | if (ec == boost::asio::error::operation_aborted) |
| 260 | { |
| 261 | /* we were canceled*/ |
| 262 | return; |
| 263 | } |
| 264 | else if (ec) |
| 265 | { |
| 266 | std::cerr << "timer error\n"; |
| 267 | return; |
| 268 | } |
| 269 | createSensors(io, objectServer, sensors, systemBus, |
| 270 | sensorsChanged); |
| 271 | }); |
| 272 | }; |
| 273 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 274 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 275 | { |
| 276 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 277 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 278 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 279 | std::string(inventoryPath) + "',arg0namespace='" + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 280 | eventHandler); |
| 281 | matches.emplace_back(std::move(match)); |
| 282 | } |
| 283 | |
| 284 | io.run(); |
| 285 | } |