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