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