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