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 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string/predicate.hpp> |
| 21 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 22 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | #include <boost/container/flat_set.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/connection.hpp> |
| 25 | #include <sdbusplus/asio/object_server.hpp> |
| 26 | #include <sdbusplus/bus/match.hpp> |
| 27 | |
| 28 | #include <array> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 29 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 30 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 31 | #include <functional> |
| 32 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | #include <regex> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 34 | #include <stdexcept> |
| 35 | #include <string> |
| 36 | #include <utility> |
| 37 | #include <variant> |
| 38 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | |
| 40 | static constexpr bool DEBUG = false; |
| 41 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 42 | namespace fs = std::filesystem; |
Josh Lehan | 8fb0a01 | 2020-06-26 19:28:16 -0700 | [diff] [blame] | 43 | static constexpr std::array<const char*, 11> sensorTypes = { |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 44 | "xyz.openbmc_project.Configuration.EMC1413", |
| 45 | "xyz.openbmc_project.Configuration.MAX31725", |
| 46 | "xyz.openbmc_project.Configuration.MAX31730", |
Jason Ling | 16e7af1 | 2020-06-05 17:41:01 -0700 | [diff] [blame] | 47 | "xyz.openbmc_project.Configuration.MAX6581", |
Josh Lehan | 3840d0a | 2020-05-13 16:13:45 -0700 | [diff] [blame] | 48 | "xyz.openbmc_project.Configuration.MAX6654", |
Josh Lehan | 8fb0a01 | 2020-06-26 19:28:16 -0700 | [diff] [blame] | 49 | "xyz.openbmc_project.Configuration.SBTSI", |
John Wang | 55ab2af | 2019-04-18 14:22:31 +0800 | [diff] [blame] | 50 | "xyz.openbmc_project.Configuration.TMP112", |
Patrick Venture | 3546adb | 2019-08-14 18:35:29 -0700 | [diff] [blame] | 51 | "xyz.openbmc_project.Configuration.TMP175", |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 52 | "xyz.openbmc_project.Configuration.TMP421", |
| 53 | "xyz.openbmc_project.Configuration.TMP441", |
| 54 | "xyz.openbmc_project.Configuration.TMP75"}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | |
| 56 | void createSensors( |
| 57 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 58 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | sensors, |
| 60 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 61 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | sensorsChanged) |
| 63 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 64 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 65 | dbusConnection, |
| 66 | std::move([&io, &objectServer, &sensors, &dbusConnection, |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 67 | sensorsChanged]( |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 68 | const ManagedObjectType& sensorConfigurations) { |
| 69 | bool firstScan = sensorsChanged == nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 70 | |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 71 | std::vector<fs::path> paths; |
| 72 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", |
| 73 | paths)) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 74 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 75 | std::cerr << "No temperature sensors in system\n"; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | boost::container::flat_set<std::string> directories; |
| 80 | |
| 81 | // iterate through all found temp sensors, and try to match them |
| 82 | // with configuration |
| 83 | for (auto& path : paths) |
| 84 | { |
| 85 | std::smatch match; |
| 86 | const std::string& pathStr = path.string(); |
| 87 | auto directory = path.parent_path(); |
| 88 | |
| 89 | auto ret = directories.insert(directory.string()); |
| 90 | if (!ret.second) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 91 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 92 | continue; // already searched this path |
| 93 | } |
| 94 | |
| 95 | fs::path device = directory / "device"; |
| 96 | std::string deviceName = fs::canonical(device).stem(); |
| 97 | auto findHyphen = deviceName.find("-"); |
| 98 | if (findHyphen == std::string::npos) |
| 99 | { |
| 100 | std::cerr << "found bad device " << deviceName << "\n"; |
| 101 | continue; |
| 102 | } |
| 103 | std::string busStr = deviceName.substr(0, findHyphen); |
| 104 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 105 | |
| 106 | size_t bus = 0; |
| 107 | size_t addr = 0; |
| 108 | try |
| 109 | { |
| 110 | bus = std::stoi(busStr); |
| 111 | addr = std::stoi(addrStr, 0, 16); |
| 112 | } |
| 113 | catch (std::invalid_argument&) |
| 114 | { |
| 115 | continue; |
| 116 | } |
| 117 | const SensorData* sensorData = nullptr; |
| 118 | const std::string* interfacePath = nullptr; |
| 119 | const char* sensorType = nullptr; |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 120 | const SensorBaseConfiguration* baseConfiguration = nullptr; |
| 121 | const SensorBaseConfigMap* baseConfigMap = nullptr; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 122 | |
| 123 | for (const std::pair<sdbusplus::message::object_path, |
| 124 | SensorData>& sensor : sensorConfigurations) |
| 125 | { |
| 126 | sensorData = &(sensor.second); |
| 127 | for (const char* type : sensorTypes) |
| 128 | { |
| 129 | auto sensorBase = sensorData->find(type); |
| 130 | if (sensorBase != sensorData->end()) |
| 131 | { |
| 132 | baseConfiguration = &(*sensorBase); |
| 133 | sensorType = type; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | if (baseConfiguration == nullptr) |
| 138 | { |
| 139 | std::cerr << "error finding base configuration for " |
| 140 | << deviceName << "\n"; |
| 141 | continue; |
| 142 | } |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 143 | baseConfigMap = &baseConfiguration->second; |
| 144 | auto configurationBus = baseConfigMap->find("Bus"); |
| 145 | auto configurationAddress = baseConfigMap->find("Address"); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 146 | |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 147 | if (configurationBus == baseConfigMap->end() || |
| 148 | configurationAddress == baseConfigMap->end()) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 149 | { |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 150 | std::cerr << "error finding bus or address in " |
| 151 | "configuration\n"; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 152 | continue; |
| 153 | } |
| 154 | |
| 155 | if (std::get<uint64_t>(configurationBus->second) != bus || |
| 156 | std::get<uint64_t>(configurationAddress->second) != |
| 157 | addr) |
| 158 | { |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | interfacePath = &(sensor.first.str); |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 163 | break; |
| 164 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 165 | if (interfacePath == nullptr) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 166 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 167 | std::cerr << "failed to find match for " << deviceName |
| 168 | << "\n"; |
| 169 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 170 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 171 | |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 172 | auto findSensorName = baseConfigMap->find("Name"); |
| 173 | if (findSensorName == baseConfigMap->end()) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 174 | { |
| 175 | std::cerr << "could not determine configuration name for " |
| 176 | << deviceName << "\n"; |
| 177 | continue; |
| 178 | } |
| 179 | std::string sensorName = |
| 180 | std::get<std::string>(findSensorName->second); |
| 181 | // on rescans, only update sensors we were signaled by |
| 182 | auto findSensor = sensors.find(sensorName); |
| 183 | if (!firstScan && findSensor != sensors.end()) |
| 184 | { |
| 185 | bool found = false; |
| 186 | for (auto it = sensorsChanged->begin(); |
| 187 | it != sensorsChanged->end(); it++) |
| 188 | { |
| 189 | if (boost::ends_with(*it, findSensor->second->name)) |
| 190 | { |
| 191 | sensorsChanged->erase(it); |
| 192 | findSensor->second = nullptr; |
| 193 | found = true; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | if (!found) |
| 198 | { |
| 199 | continue; |
| 200 | } |
| 201 | } |
| 202 | std::vector<thresholds::Threshold> sensorThresholds; |
| 203 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
| 204 | { |
| 205 | std::cerr << "error populating thresholds for " |
| 206 | << sensorName << "\n"; |
| 207 | } |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 208 | auto findPowerOn = baseConfiguration->second.find("PowerState"); |
| 209 | PowerState readState = PowerState::always; |
| 210 | if (findPowerOn != baseConfiguration->second.end()) |
| 211 | { |
| 212 | std::string powerState = std::visit( |
| 213 | VariantToStringVisitor(), findPowerOn->second); |
| 214 | setReadState(powerState, readState); |
| 215 | } |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame^] | 216 | |
| 217 | auto permitSet = getPermitSet(*baseConfigMap); |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 218 | auto& sensor = sensors[sensorName]; |
| 219 | sensor = nullptr; |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame^] | 220 | auto hwmonFile = getFullHwmonFilePath(directory.string(), |
| 221 | "temp1", permitSet); |
| 222 | if (hwmonFile) |
| 223 | { |
| 224 | sensor = std::make_shared<HwmonTempSensor>( |
| 225 | *hwmonFile, sensorType, objectServer, dbusConnection, |
| 226 | io, sensorName, std::move(sensorThresholds), |
| 227 | *interfacePath, readState); |
| 228 | sensor->setupRead(); |
| 229 | } |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 230 | // Looking for keys like "Name1" for temp2_input, |
| 231 | // "Name2" for temp3_input, etc. |
| 232 | int i = 0; |
| 233 | while (true) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 234 | { |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 235 | ++i; |
| 236 | auto findKey = |
Jason Ling | 8b8bcc8 | 2020-08-04 19:33:22 -0700 | [diff] [blame] | 237 | baseConfigMap->find("Name" + std::to_string(i)); |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 238 | if (findKey == baseConfigMap->end()) |
| 239 | { |
| 240 | break; |
| 241 | } |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 242 | std::string sensorName = |
| 243 | std::get<std::string>(findKey->second); |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame^] | 244 | hwmonFile = getFullHwmonFilePath( |
| 245 | directory.string(), "temp" + std::to_string(i + 1), |
| 246 | permitSet); |
| 247 | if (hwmonFile) |
| 248 | { |
| 249 | auto& sensor = sensors[sensorName]; |
| 250 | sensor = nullptr; |
| 251 | sensor = std::make_shared<HwmonTempSensor>( |
| 252 | *hwmonFile, sensorType, objectServer, |
| 253 | dbusConnection, io, sensorName, |
| 254 | std::vector<thresholds::Threshold>(), |
| 255 | *interfacePath, readState); |
| 256 | sensor->setupRead(); |
| 257 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 258 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 259 | } |
| 260 | })); |
| 261 | getter->getConfiguration( |
| 262 | std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 263 | } |
| 264 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 265 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 266 | { |
| 267 | boost::asio::io_service io; |
| 268 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 269 | systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); |
| 270 | sdbusplus::asio::object_server objectServer(systemBus); |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 271 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 272 | sensors; |
| 273 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 274 | auto sensorsChanged = |
| 275 | std::make_shared<boost::container::flat_set<std::string>>(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 276 | |
| 277 | io.post([&]() { |
| 278 | createSensors(io, objectServer, sensors, systemBus, nullptr); |
| 279 | }); |
| 280 | |
| 281 | boost::asio::deadline_timer filterTimer(io); |
| 282 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 283 | [&](sdbusplus::message::message& message) { |
| 284 | if (message.is_method_error()) |
| 285 | { |
| 286 | std::cerr << "callback method error\n"; |
| 287 | return; |
| 288 | } |
| 289 | sensorsChanged->insert(message.get_path()); |
| 290 | // this implicitly cancels the timer |
| 291 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 292 | |
| 293 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 294 | if (ec == boost::asio::error::operation_aborted) |
| 295 | { |
| 296 | /* we were canceled*/ |
| 297 | return; |
| 298 | } |
| 299 | else if (ec) |
| 300 | { |
| 301 | std::cerr << "timer error\n"; |
| 302 | return; |
| 303 | } |
| 304 | createSensors(io, objectServer, sensors, systemBus, |
| 305 | sensorsChanged); |
| 306 | }); |
| 307 | }; |
| 308 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 309 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 310 | { |
| 311 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 312 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 313 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 314 | std::string(inventoryPath) + "',arg0namespace='" + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 315 | eventHandler); |
| 316 | matches.emplace_back(std::move(match)); |
| 317 | } |
| 318 | |
| 319 | io.run(); |
| 320 | } |