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