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