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/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 20 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_set.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 22 | #include <sdbusplus/asio/connection.hpp> |
| 23 | #include <sdbusplus/asio/object_server.hpp> |
| 24 | #include <sdbusplus/bus/match.hpp> |
| 25 | |
| 26 | #include <array> |
Jae Hyun Yoo | 7dd6443 | 2022-03-30 14:28:33 -0700 | [diff] [blame] | 27 | #include <charconv> |
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 | |
Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 39 | static constexpr float pollRateDefault = 0.5; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 41 | static constexpr double maxValuePressure = 120000; // Pascals |
| 42 | static constexpr double minValuePressure = 30000; // Pascals |
| 43 | |
Bruce Mitchell | 3ec41c5 | 2021-12-10 16:05:17 -0600 | [diff] [blame] | 44 | static constexpr double maxValueRelativeHumidity = 100; // PercentRH |
| 45 | static constexpr double minValueRelativeHumidity = 0; // PercentRH |
| 46 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 47 | static constexpr double maxValueTemperature = 127; // DegreesC |
| 48 | static constexpr double minValueTemperature = -128; // DegreesC |
| 49 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 50 | namespace fs = std::filesystem; |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 51 | static auto sensorTypes{std::to_array<const char*>( |
| 52 | {"DPS310", "EMC1412", "EMC1413", "EMC1414", "HDC1080", "JC42", |
| 53 | "LM75A", "LM95234", "MAX31725", "MAX31730", "MAX6581", "MAX6654", |
| 54 | "NCT7802", "NCT6779", "SBTSI", "SI7020", "TMP112", "TMP175", |
| 55 | "TMP421", "TMP441", "TMP75", "W83773G"})}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 57 | static struct SensorParams |
| 58 | getSensorParameters(const std::filesystem::path& path) |
| 59 | { |
| 60 | // offset is to default to 0 and scale to 1, see lore |
| 61 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 62 | struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature, |
| 63 | .maxValue = maxValueTemperature, |
| 64 | .offsetValue = 0.0, |
| 65 | .scaleValue = 1.0, |
Bruce Mitchell | 5a86e56 | 2021-12-10 12:26:22 -0600 | [diff] [blame] | 66 | .units = |
| 67 | sensor_paths::unitDegreesC, |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 68 | .typeName = "temperature"}; |
| 69 | |
| 70 | // For IIO RAW sensors we get a raw_value, an offset, and scale |
| 71 | // to compute the value = (raw_value + offset) * scale |
| 72 | // with a _raw IIO device we need to get the |
| 73 | // offsetValue and scaleValue from the driver |
| 74 | // these are used to compute the reading in |
| 75 | // units that have yet to be scaled for D-Bus. |
| 76 | const std::string pathStr = path.string(); |
| 77 | if (pathStr.ends_with("_raw")) |
| 78 | { |
| 79 | std::string pathOffsetStr = |
| 80 | pathStr.substr(0, pathStr.size() - 4) + "_offset"; |
| 81 | std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0); |
| 82 | // In case there is nothing to read skip this device |
| 83 | // This is not an error condition see lore |
| 84 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 85 | if (tmpOffsetValue) |
| 86 | { |
| 87 | tmpSensorParameters.offsetValue = *tmpOffsetValue; |
| 88 | } |
| 89 | |
| 90 | std::string pathScaleStr = |
| 91 | pathStr.substr(0, pathStr.size() - 4) + "_scale"; |
| 92 | std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0); |
| 93 | // In case there is nothing to read skip this device |
| 94 | // This is not an error condition see lore |
| 95 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 96 | if (tmpScaleValue) |
| 97 | { |
| 98 | tmpSensorParameters.scaleValue = *tmpScaleValue; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Temperatures are read in milli degrees Celsius, we need |
| 103 | // degrees Celsius. Pressures are read in kilopascal, we need |
| 104 | // Pascals. On D-Bus for Open BMC we use the International |
| 105 | // System of Units without prefixes. Links to the kernel |
| 106 | // documentation: |
| 107 | // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface |
| 108 | // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio |
| 109 | if (path.filename() == "in_pressure_input" || |
| 110 | path.filename() == "in_pressure_raw") |
| 111 | { |
| 112 | tmpSensorParameters.minValue = minValuePressure; |
| 113 | tmpSensorParameters.maxValue = maxValuePressure; |
| 114 | // Pressures are read in kilopascal, we need Pascals. |
| 115 | tmpSensorParameters.scaleValue *= 1000.0; |
| 116 | tmpSensorParameters.typeName = "pressure"; |
Bruce Mitchell | 5a86e56 | 2021-12-10 12:26:22 -0600 | [diff] [blame] | 117 | tmpSensorParameters.units = sensor_paths::unitPascals; |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 118 | } |
Bruce Mitchell | 3ec41c5 | 2021-12-10 16:05:17 -0600 | [diff] [blame] | 119 | else if (path.filename() == "in_humidityrelative_input" || |
| 120 | path.filename() == "in_humidityrelative_raw") |
| 121 | { |
| 122 | tmpSensorParameters.minValue = minValueRelativeHumidity; |
| 123 | tmpSensorParameters.maxValue = maxValueRelativeHumidity; |
| 124 | // Relative Humidity are read in milli-percent, we need percent. |
| 125 | tmpSensorParameters.scaleValue *= 0.001; |
| 126 | tmpSensorParameters.typeName = "humidity"; |
| 127 | tmpSensorParameters.units = "PercentRH"; |
| 128 | } |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 129 | else |
| 130 | { |
| 131 | // Temperatures are read in milli degrees Celsius, |
| 132 | // we need degrees Celsius. |
| 133 | tmpSensorParameters.scaleValue *= 0.001; |
| 134 | } |
| 135 | |
| 136 | return tmpSensorParameters; |
| 137 | } |
| 138 | |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 139 | struct SensorConfigKey |
| 140 | { |
| 141 | uint64_t bus; |
| 142 | uint64_t addr; |
| 143 | bool operator<(const SensorConfigKey& other) const |
| 144 | { |
| 145 | if (bus != other.bus) |
| 146 | { |
| 147 | return bus < other.bus; |
| 148 | } |
| 149 | return addr < other.addr; |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | struct SensorConfig |
| 154 | { |
| 155 | std::string sensorPath; |
| 156 | SensorData sensorData; |
| 157 | std::string interface; |
| 158 | SensorBaseConfigMap config; |
| 159 | std::vector<std::string> name; |
| 160 | }; |
| 161 | |
| 162 | using SensorConfigMap = |
| 163 | boost::container::flat_map<SensorConfigKey, SensorConfig>; |
| 164 | |
| 165 | static SensorConfigMap |
| 166 | buildSensorConfigMap(const ManagedObjectType& sensorConfigs) |
| 167 | { |
| 168 | SensorConfigMap configMap; |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 169 | for (const auto& [path, cfgData] : sensorConfigs) |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 170 | { |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 171 | for (const auto& [intf, cfg] : cfgData) |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 172 | { |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 173 | auto busCfg = cfg.find("Bus"); |
| 174 | auto addrCfg = cfg.find("Address"); |
| 175 | if ((busCfg == cfg.end()) || (addrCfg == cfg.end())) |
| 176 | { |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 177 | continue; |
| 178 | } |
| 179 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 180 | if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) || |
| 181 | (std::get_if<uint64_t>(&addrCfg->second) == nullptr)) |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 182 | { |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 183 | std::cerr << path.str << " Bus or Address invalid\n"; |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 184 | continue; |
| 185 | } |
| 186 | |
| 187 | std::vector<std::string> hwmonNames; |
| 188 | auto nameCfg = cfg.find("Name"); |
| 189 | if (nameCfg != cfg.end()) |
| 190 | { |
| 191 | hwmonNames.push_back(std::get<std::string>(nameCfg->second)); |
| 192 | size_t i = 1; |
| 193 | while (true) |
| 194 | { |
| 195 | auto sensorNameCfg = cfg.find("Name" + std::to_string(i)); |
| 196 | if (sensorNameCfg == cfg.end()) |
| 197 | { |
| 198 | break; |
| 199 | } |
| 200 | hwmonNames.push_back( |
| 201 | std::get<std::string>(sensorNameCfg->second)); |
| 202 | i++; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | SensorConfigKey key = {std::get<uint64_t>(busCfg->second), |
| 207 | std::get<uint64_t>(addrCfg->second)}; |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 208 | SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames}; |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 209 | |
| 210 | auto [it, inserted] = configMap.emplace(key, std::move(val)); |
| 211 | if (!inserted) |
| 212 | { |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 213 | std::cerr << path.str << ": ignoring duplicate entry for {" |
| 214 | << key.bus << ", 0x" << std::hex << key.addr |
| 215 | << std::dec << "}\n"; |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | return configMap; |
| 220 | } |
| 221 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 222 | void createSensors( |
| 223 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 224 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 225 | sensors, |
| 226 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 227 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 228 | sensorsChanged) |
| 229 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 230 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 231 | dbusConnection, |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 232 | [&io, &objectServer, &sensors, &dbusConnection, |
| 233 | sensorsChanged](const ManagedObjectType& sensorConfigurations) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 234 | bool firstScan = sensorsChanged == nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 235 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 236 | SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations); |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 237 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 238 | // IIO _raw devices look like this on sysfs: |
| 239 | // /sys/bus/iio/devices/iio:device0/in_temp_raw |
| 240 | // /sys/bus/iio/devices/iio:device0/in_temp_offset |
| 241 | // /sys/bus/iio/devices/iio:device0/in_temp_scale |
| 242 | // |
| 243 | // Other IIO devices look like this on sysfs: |
| 244 | // /sys/bus/iio/devices/iio:device1/in_temp_input |
| 245 | // /sys/bus/iio/devices/iio:device1/in_pressure_input |
| 246 | std::vector<fs::path> paths; |
| 247 | fs::path root("/sys/bus/iio/devices"); |
| 248 | findFiles(root, R"(in_temp\d*_(input|raw))", paths); |
| 249 | findFiles(root, R"(in_pressure\d*_(input|raw))", paths); |
| 250 | findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths); |
| 251 | findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 252 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 253 | if (paths.empty()) |
| 254 | { |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | // iterate through all found temp and pressure sensors, |
| 259 | // and try to match them with configuration |
| 260 | for (auto& path : paths) |
| 261 | { |
| 262 | std::smatch match; |
| 263 | const std::string pathStr = path.string(); |
| 264 | auto directory = path.parent_path(); |
| 265 | fs::path device; |
| 266 | |
| 267 | std::string deviceName; |
| 268 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 269 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 270 | device = fs::canonical(directory); |
| 271 | deviceName = device.parent_path().stem(); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | device = directory / "device"; |
| 276 | deviceName = fs::canonical(device).stem(); |
| 277 | } |
| 278 | auto findHyphen = deviceName.find('-'); |
| 279 | if (findHyphen == std::string::npos) |
| 280 | { |
| 281 | std::cerr << "found bad device " << deviceName << "\n"; |
| 282 | continue; |
| 283 | } |
| 284 | std::string busStr = deviceName.substr(0, findHyphen); |
| 285 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 286 | |
| 287 | uint64_t bus = 0; |
| 288 | uint64_t addr = 0; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 289 | std::from_chars_result res{}; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 290 | res = std::from_chars(busStr.data(), busStr.data() + busStr.size(), |
| 291 | bus); |
| 292 | if (res.ec != std::errc{}) |
| 293 | { |
| 294 | continue; |
| 295 | } |
| 296 | res = std::from_chars(addrStr.data(), |
| 297 | addrStr.data() + addrStr.size(), addr, 16); |
| 298 | if (res.ec != std::errc{}) |
| 299 | { |
| 300 | continue; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 303 | auto thisSensorParameters = getSensorParameters(path); |
| 304 | auto findSensorCfg = configMap.find({bus, addr}); |
| 305 | if (findSensorCfg == configMap.end()) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 306 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 307 | continue; |
| 308 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 309 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 310 | const std::string& interfacePath = findSensorCfg->second.sensorPath; |
| 311 | const SensorData& sensorData = findSensorCfg->second.sensorData; |
| 312 | const std::string& sensorType = findSensorCfg->second.interface; |
| 313 | const SensorBaseConfigMap& baseConfigMap = |
| 314 | findSensorCfg->second.config; |
| 315 | std::vector<std::string>& hwmonName = findSensorCfg->second.name; |
| 316 | |
| 317 | // Temperature has "Name", pressure has "Name1" |
| 318 | auto findSensorName = baseConfigMap.find("Name"); |
| 319 | int index = 1; |
| 320 | if (thisSensorParameters.typeName == "pressure" || |
| 321 | thisSensorParameters.typeName == "humidity") |
| 322 | { |
| 323 | findSensorName = baseConfigMap.find("Name1"); |
| 324 | index = 2; |
| 325 | } |
| 326 | |
| 327 | if (findSensorName == baseConfigMap.end()) |
| 328 | { |
| 329 | std::cerr << "could not determine configuration name for " |
| 330 | << deviceName << "\n"; |
| 331 | continue; |
| 332 | } |
| 333 | std::string sensorName = |
| 334 | std::get<std::string>(findSensorName->second); |
| 335 | // on rescans, only update sensors we were signaled by |
| 336 | auto findSensor = sensors.find(sensorName); |
| 337 | if (!firstScan && findSensor != sensors.end()) |
| 338 | { |
| 339 | bool found = false; |
| 340 | auto it = sensorsChanged->begin(); |
| 341 | while (it != sensorsChanged->end()) |
| 342 | { |
Zev Weiss | 6c106d6 | 2022-08-17 20:50:00 -0700 | [diff] [blame] | 343 | if (it->ends_with(findSensor->second->name)) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 344 | { |
| 345 | it = sensorsChanged->erase(it); |
| 346 | findSensor->second = nullptr; |
| 347 | found = true; |
| 348 | break; |
| 349 | } |
| 350 | ++it; |
| 351 | } |
| 352 | if (!found) |
| 353 | { |
| 354 | continue; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | std::vector<thresholds::Threshold> sensorThresholds; |
| 359 | |
| 360 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds, |
| 361 | nullptr, &index)) |
| 362 | { |
| 363 | std::cerr << "error populating thresholds for " << sensorName |
| 364 | << " index " << index << "\n"; |
| 365 | } |
| 366 | |
| 367 | auto findPollRate = baseConfigMap.find("PollRate"); |
| 368 | float pollRate = pollRateDefault; |
| 369 | if (findPollRate != baseConfigMap.end()) |
| 370 | { |
| 371 | pollRate = |
| 372 | std::visit(VariantToFloatVisitor(), findPollRate->second); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 373 | if (pollRate <= 0.0F) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 374 | { |
| 375 | pollRate = pollRateDefault; // polling time too short |
| 376 | } |
| 377 | } |
| 378 | |
Zev Weiss | a4d2768 | 2022-07-19 15:30:36 -0700 | [diff] [blame] | 379 | PowerState readState = getPowerState(baseConfigMap); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 380 | |
| 381 | auto permitSet = getPermitSet(baseConfigMap); |
| 382 | auto& sensor = sensors[sensorName]; |
| 383 | sensor = nullptr; |
| 384 | auto hwmonFile = |
| 385 | getFullHwmonFilePath(directory.string(), "temp1", permitSet); |
| 386 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
| 387 | { |
| 388 | hwmonFile = pathStr; |
| 389 | } |
| 390 | if (hwmonFile) |
| 391 | { |
| 392 | sensor = std::make_shared<HwmonTempSensor>( |
| 393 | *hwmonFile, sensorType, objectServer, dbusConnection, io, |
| 394 | sensorName, std::move(sensorThresholds), |
| 395 | thisSensorParameters, pollRate, interfacePath, readState); |
| 396 | sensor->setupRead(); |
| 397 | } |
| 398 | hwmonName.erase( |
| 399 | remove(hwmonName.begin(), hwmonName.end(), sensorName), |
| 400 | hwmonName.end()); |
| 401 | |
| 402 | // Looking for keys like "Name1" for temp2_input, |
| 403 | // "Name2" for temp3_input, etc. |
| 404 | int i = 0; |
| 405 | while (true) |
| 406 | { |
| 407 | ++i; |
| 408 | auto findKey = baseConfigMap.find("Name" + std::to_string(i)); |
| 409 | if (findKey == baseConfigMap.end()) |
| 410 | { |
| 411 | break; |
| 412 | } |
| 413 | std::string sensorName = std::get<std::string>(findKey->second); |
| 414 | hwmonFile = getFullHwmonFilePath(directory.string(), |
| 415 | "temp" + std::to_string(i + 1), |
| 416 | permitSet); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 417 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 418 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 419 | continue; |
| 420 | } |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 421 | if (hwmonFile) |
| 422 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 423 | // To look up thresholds for these additional sensors, |
| 424 | // match on the Index property in the threshold data |
| 425 | // where the index comes from the sysfs file we're on, |
| 426 | // i.e. index = 2 for temp2_input. |
| 427 | int index = i + 1; |
| 428 | std::vector<thresholds::Threshold> thresholds; |
| 429 | |
| 430 | if (!parseThresholdsFromConfig(sensorData, thresholds, |
| 431 | nullptr, &index)) |
| 432 | { |
| 433 | std::cerr << "error populating thresholds for " |
| 434 | << sensorName << " index " << index << "\n"; |
| 435 | } |
| 436 | |
| 437 | auto& sensor = sensors[sensorName]; |
| 438 | sensor = nullptr; |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 439 | sensor = std::make_shared<HwmonTempSensor>( |
| 440 | *hwmonFile, sensorType, objectServer, dbusConnection, |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 441 | io, sensorName, std::move(thresholds), |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 442 | thisSensorParameters, pollRate, interfacePath, |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 443 | readState); |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 444 | sensor->setupRead(); |
| 445 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 446 | |
Willy Tu | 9eb0cc3 | 2022-04-06 11:03:32 -0700 | [diff] [blame] | 447 | hwmonName.erase( |
| 448 | remove(hwmonName.begin(), hwmonName.end(), sensorName), |
| 449 | hwmonName.end()); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 450 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 451 | if (hwmonName.empty()) |
| 452 | { |
| 453 | configMap.erase(findSensorCfg); |
| 454 | } |
| 455 | } |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 456 | }); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 457 | getter->getConfiguration( |
| 458 | std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 461 | void interfaceRemoved( |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 462 | sdbusplus::message_t& message, |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 463 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
| 464 | sensors) |
| 465 | { |
| 466 | if (message.is_method_error()) |
| 467 | { |
| 468 | std::cerr << "interfacesRemoved callback method error\n"; |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | sdbusplus::message::object_path path; |
| 473 | std::vector<std::string> interfaces; |
| 474 | |
| 475 | message.read(path, interfaces); |
| 476 | |
| 477 | // If the xyz.openbmc_project.Confguration.X interface was removed |
| 478 | // for one or more sensors, delete those sensor objects. |
| 479 | auto sensorIt = sensors.begin(); |
| 480 | while (sensorIt != sensors.end()) |
| 481 | { |
| 482 | if ((sensorIt->second->configurationPath == path) && |
| 483 | (std::find(interfaces.begin(), interfaces.end(), |
| 484 | sensorIt->second->objectType) != interfaces.end())) |
| 485 | { |
| 486 | sensorIt = sensors.erase(sensorIt); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | sensorIt++; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 495 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 496 | { |
| 497 | boost::asio::io_service io; |
| 498 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 499 | systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); |
| 500 | sdbusplus::asio::object_server objectServer(systemBus); |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 501 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 502 | sensors; |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 503 | auto sensorsChanged = |
| 504 | std::make_shared<boost::container::flat_set<std::string>>(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 505 | |
| 506 | io.post([&]() { |
| 507 | createSensors(io, objectServer, sensors, systemBus, nullptr); |
| 508 | }); |
| 509 | |
| 510 | boost::asio::deadline_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 511 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 512 | [&](sdbusplus::message_t& message) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 513 | if (message.is_method_error()) |
| 514 | { |
| 515 | std::cerr << "callback method error\n"; |
| 516 | return; |
| 517 | } |
| 518 | sensorsChanged->insert(message.get_path()); |
| 519 | // this implicitly cancels the timer |
| 520 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 521 | |
| 522 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 523 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 524 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 525 | /* we were canceled*/ |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 526 | return; |
| 527 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 528 | if (ec) |
| 529 | { |
| 530 | std::cerr << "timer error\n"; |
| 531 | return; |
| 532 | } |
| 533 | createSensors(io, objectServer, sensors, systemBus, sensorsChanged); |
| 534 | }); |
| 535 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 536 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 537 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 538 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 539 | setupManufacturingModeMatch(*systemBus); |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 540 | |
| 541 | // Watch for entity-manager to remove configuration interfaces |
| 542 | // so the corresponding sensors can be removed. |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 543 | auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 544 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 545 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 546 | std::string(inventoryPath) + "/'", |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 547 | [&sensors](sdbusplus::message_t& msg) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 548 | interfaceRemoved(msg, sensors); |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 549 | }); |
| 550 | |
| 551 | matches.emplace_back(std::move(ifaceRemovedMatch)); |
| 552 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 553 | io.run(); |
| 554 | } |