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 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame^] | 17 | #include "DeviceMgmt.hpp" |
| 18 | #include "HwmonTempSensor.hpp" |
| 19 | #include "Utils.hpp" |
| 20 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 22 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | #include <boost/container/flat_set.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/connection.hpp> |
| 25 | #include <sdbusplus/asio/object_server.hpp> |
| 26 | #include <sdbusplus/bus/match.hpp> |
| 27 | |
| 28 | #include <array> |
Jae Hyun Yoo | 7dd6443 | 2022-03-30 14:28:33 -0700 | [diff] [blame] | 29 | #include <charconv> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 30 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 31 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <functional> |
| 33 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 34 | #include <regex> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 35 | #include <stdexcept> |
| 36 | #include <string> |
| 37 | #include <utility> |
| 38 | #include <variant> |
| 39 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | |
Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 41 | static constexpr float pollRateDefault = 0.5; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 43 | static constexpr double maxValuePressure = 120000; // Pascals |
| 44 | static constexpr double minValuePressure = 30000; // Pascals |
| 45 | |
Bruce Mitchell | 3ec41c5 | 2021-12-10 16:05:17 -0600 | [diff] [blame] | 46 | static constexpr double maxValueRelativeHumidity = 100; // PercentRH |
| 47 | static constexpr double minValueRelativeHumidity = 0; // PercentRH |
| 48 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 49 | static constexpr double maxValueTemperature = 127; // DegreesC |
| 50 | static constexpr double minValueTemperature = -128; // DegreesC |
| 51 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 52 | namespace fs = std::filesystem; |
Zev Weiss | d29f8aa | 2022-08-03 16:24:59 -0700 | [diff] [blame] | 53 | |
| 54 | static const I2CDeviceTypeMap sensorTypes{ |
| 55 | {"DPS310", I2CDeviceType{"dps310", false}}, |
| 56 | {"EMC1412", I2CDeviceType{"emc1412", true}}, |
| 57 | {"EMC1413", I2CDeviceType{"emc1413", true}}, |
| 58 | {"EMC1414", I2CDeviceType{"emc1414", true}}, |
| 59 | {"HDC1080", I2CDeviceType{"hdc1080", false}}, |
| 60 | {"JC42", I2CDeviceType{"jc42", true}}, |
| 61 | {"LM75A", I2CDeviceType{"lm75a", true}}, |
| 62 | {"LM95234", I2CDeviceType{"lm95234", true}}, |
| 63 | {"MAX31725", I2CDeviceType{"max31725", true}}, |
| 64 | {"MAX31730", I2CDeviceType{"max31730", true}}, |
| 65 | {"MAX6581", I2CDeviceType{"max6581", true}}, |
| 66 | {"MAX6654", I2CDeviceType{"max6654", true}}, |
| 67 | {"NCT6779", I2CDeviceType{"nct6779", true}}, |
| 68 | {"NCT7802", I2CDeviceType{"nct7802", true}}, |
| 69 | {"SBTSI", I2CDeviceType{"sbtsi", true}}, |
| 70 | {"SI7020", I2CDeviceType{"si7020", false}}, |
Tim Lee | 9b0a6f6 | 2022-12-23 14:47:02 +0800 | [diff] [blame] | 71 | {"TMP100", I2CDeviceType{"tmp100", true}}, |
Zev Weiss | d29f8aa | 2022-08-03 16:24:59 -0700 | [diff] [blame] | 72 | {"TMP112", I2CDeviceType{"tmp112", true}}, |
| 73 | {"TMP175", I2CDeviceType{"tmp175", true}}, |
| 74 | {"TMP421", I2CDeviceType{"tmp421", true}}, |
| 75 | {"TMP441", I2CDeviceType{"tmp441", true}}, |
| 76 | {"TMP75", I2CDeviceType{"tmp75", true}}, |
| 77 | {"W83773G", I2CDeviceType{"w83773g", true}}, |
| 78 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 79 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 80 | static struct SensorParams |
| 81 | getSensorParameters(const std::filesystem::path& path) |
| 82 | { |
| 83 | // offset is to default to 0 and scale to 1, see lore |
| 84 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 85 | struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature, |
| 86 | .maxValue = maxValueTemperature, |
| 87 | .offsetValue = 0.0, |
| 88 | .scaleValue = 1.0, |
Bruce Mitchell | 5a86e56 | 2021-12-10 12:26:22 -0600 | [diff] [blame] | 89 | .units = |
| 90 | sensor_paths::unitDegreesC, |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 91 | .typeName = "temperature"}; |
| 92 | |
| 93 | // For IIO RAW sensors we get a raw_value, an offset, and scale |
| 94 | // to compute the value = (raw_value + offset) * scale |
| 95 | // with a _raw IIO device we need to get the |
| 96 | // offsetValue and scaleValue from the driver |
| 97 | // these are used to compute the reading in |
| 98 | // units that have yet to be scaled for D-Bus. |
| 99 | const std::string pathStr = path.string(); |
| 100 | if (pathStr.ends_with("_raw")) |
| 101 | { |
| 102 | std::string pathOffsetStr = |
| 103 | pathStr.substr(0, pathStr.size() - 4) + "_offset"; |
| 104 | std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0); |
| 105 | // In case there is nothing to read skip this device |
| 106 | // This is not an error condition see lore |
| 107 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 108 | if (tmpOffsetValue) |
| 109 | { |
| 110 | tmpSensorParameters.offsetValue = *tmpOffsetValue; |
| 111 | } |
| 112 | |
| 113 | std::string pathScaleStr = |
| 114 | pathStr.substr(0, pathStr.size() - 4) + "_scale"; |
| 115 | std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0); |
| 116 | // In case there is nothing to read skip this device |
| 117 | // This is not an error condition see lore |
| 118 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 119 | if (tmpScaleValue) |
| 120 | { |
| 121 | tmpSensorParameters.scaleValue = *tmpScaleValue; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Temperatures are read in milli degrees Celsius, we need |
| 126 | // degrees Celsius. Pressures are read in kilopascal, we need |
| 127 | // Pascals. On D-Bus for Open BMC we use the International |
| 128 | // System of Units without prefixes. Links to the kernel |
| 129 | // documentation: |
| 130 | // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface |
| 131 | // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio |
| 132 | if (path.filename() == "in_pressure_input" || |
| 133 | path.filename() == "in_pressure_raw") |
| 134 | { |
| 135 | tmpSensorParameters.minValue = minValuePressure; |
| 136 | tmpSensorParameters.maxValue = maxValuePressure; |
| 137 | // Pressures are read in kilopascal, we need Pascals. |
| 138 | tmpSensorParameters.scaleValue *= 1000.0; |
| 139 | tmpSensorParameters.typeName = "pressure"; |
Bruce Mitchell | 5a86e56 | 2021-12-10 12:26:22 -0600 | [diff] [blame] | 140 | tmpSensorParameters.units = sensor_paths::unitPascals; |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 141 | } |
Bruce Mitchell | 3ec41c5 | 2021-12-10 16:05:17 -0600 | [diff] [blame] | 142 | else if (path.filename() == "in_humidityrelative_input" || |
| 143 | path.filename() == "in_humidityrelative_raw") |
| 144 | { |
| 145 | tmpSensorParameters.minValue = minValueRelativeHumidity; |
| 146 | tmpSensorParameters.maxValue = maxValueRelativeHumidity; |
| 147 | // Relative Humidity are read in milli-percent, we need percent. |
| 148 | tmpSensorParameters.scaleValue *= 0.001; |
| 149 | tmpSensorParameters.typeName = "humidity"; |
| 150 | tmpSensorParameters.units = "PercentRH"; |
| 151 | } |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 152 | else |
| 153 | { |
| 154 | // Temperatures are read in milli degrees Celsius, |
| 155 | // we need degrees Celsius. |
| 156 | tmpSensorParameters.scaleValue *= 0.001; |
| 157 | } |
| 158 | |
| 159 | return tmpSensorParameters; |
| 160 | } |
| 161 | |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 162 | struct SensorConfigKey |
| 163 | { |
| 164 | uint64_t bus; |
| 165 | uint64_t addr; |
| 166 | bool operator<(const SensorConfigKey& other) const |
| 167 | { |
| 168 | if (bus != other.bus) |
| 169 | { |
| 170 | return bus < other.bus; |
| 171 | } |
| 172 | return addr < other.addr; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | struct SensorConfig |
| 177 | { |
| 178 | std::string sensorPath; |
| 179 | SensorData sensorData; |
| 180 | std::string interface; |
| 181 | SensorBaseConfigMap config; |
| 182 | std::vector<std::string> name; |
| 183 | }; |
| 184 | |
| 185 | using SensorConfigMap = |
| 186 | boost::container::flat_map<SensorConfigKey, SensorConfig>; |
| 187 | |
| 188 | static SensorConfigMap |
| 189 | buildSensorConfigMap(const ManagedObjectType& sensorConfigs) |
| 190 | { |
| 191 | SensorConfigMap configMap; |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 192 | for (const auto& [path, cfgData] : sensorConfigs) |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 193 | { |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 194 | for (const auto& [intf, cfg] : cfgData) |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 195 | { |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 196 | auto busCfg = cfg.find("Bus"); |
| 197 | auto addrCfg = cfg.find("Address"); |
| 198 | if ((busCfg == cfg.end()) || (addrCfg == cfg.end())) |
| 199 | { |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 200 | continue; |
| 201 | } |
| 202 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 203 | if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) || |
| 204 | (std::get_if<uint64_t>(&addrCfg->second) == nullptr)) |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 205 | { |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 206 | std::cerr << path.str << " Bus or Address invalid\n"; |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 207 | continue; |
| 208 | } |
| 209 | |
| 210 | std::vector<std::string> hwmonNames; |
| 211 | auto nameCfg = cfg.find("Name"); |
| 212 | if (nameCfg != cfg.end()) |
| 213 | { |
| 214 | hwmonNames.push_back(std::get<std::string>(nameCfg->second)); |
| 215 | size_t i = 1; |
| 216 | while (true) |
| 217 | { |
| 218 | auto sensorNameCfg = cfg.find("Name" + std::to_string(i)); |
| 219 | if (sensorNameCfg == cfg.end()) |
| 220 | { |
| 221 | break; |
| 222 | } |
| 223 | hwmonNames.push_back( |
| 224 | std::get<std::string>(sensorNameCfg->second)); |
| 225 | i++; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | SensorConfigKey key = {std::get<uint64_t>(busCfg->second), |
| 230 | std::get<uint64_t>(addrCfg->second)}; |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 231 | SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames}; |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 232 | |
| 233 | auto [it, inserted] = configMap.emplace(key, std::move(val)); |
| 234 | if (!inserted) |
| 235 | { |
Zev Weiss | a180833 | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 236 | std::cerr << path.str << ": ignoring duplicate entry for {" |
| 237 | << key.bus << ", 0x" << std::hex << key.addr |
| 238 | << std::dec << "}\n"; |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | return configMap; |
| 243 | } |
| 244 | |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 245 | // returns a {path: <I2CDevice, is_new>} map. is_new indicates if the I2CDevice |
| 246 | // is newly instantiated by this call (true) or was already there (false). |
| 247 | boost::container::flat_map<std::string, |
| 248 | std::pair<std::shared_ptr<I2CDevice>, bool>> |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 249 | instantiateDevices( |
| 250 | const ManagedObjectType& sensorConfigs, |
| 251 | const boost::container::flat_map< |
| 252 | std::string, std::shared_ptr<HwmonTempSensor>>& sensors) |
| 253 | { |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 254 | boost::container::flat_map<std::string, |
| 255 | std::pair<std::shared_ptr<I2CDevice>, bool>> |
| 256 | devices; |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 257 | for (const auto& [path, sensor] : sensorConfigs) |
| 258 | { |
| 259 | for (const auto& [name, cfg] : sensor) |
| 260 | { |
| 261 | PowerState powerState = getPowerState(cfg); |
| 262 | if (!readingStateGood(powerState)) |
| 263 | { |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | auto findSensorName = cfg.find("Name"); |
| 268 | if (findSensorName == cfg.end()) |
| 269 | { |
| 270 | continue; |
| 271 | } |
| 272 | std::string sensorName = |
| 273 | std::get<std::string>(findSensorName->second); |
| 274 | |
| 275 | auto findSensor = sensors.find(sensorName); |
| 276 | if (findSensor != sensors.end() && findSensor->second != nullptr && |
| 277 | findSensor->second->isActive()) |
| 278 | { |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 279 | devices.emplace( |
| 280 | path.str, |
| 281 | std::make_pair(findSensor->second->getI2CDevice(), false)); |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 282 | continue; |
| 283 | } |
| 284 | |
| 285 | std::optional<I2CDeviceParams> params = |
| 286 | getI2CDeviceParams(sensorTypes, cfg); |
| 287 | if (params.has_value() && !params->deviceStatic()) |
| 288 | { |
| 289 | // There exist error cases in which a sensor device that we |
| 290 | // need is already instantiated, but needs to be destroyed and |
| 291 | // re-created in order to be useful (for example if we crash |
| 292 | // after instantiating a device and the sensor device's power |
| 293 | // is cut before we get restarted, leaving it "present" but |
| 294 | // not really usable). To be on the safe side, instantiate a |
| 295 | // temporary device that's immediately destroyed so as to |
| 296 | // ensure that we end up with a fresh instance of it. |
| 297 | if (params->devicePresent()) |
| 298 | { |
| 299 | std::cerr << "Clearing out previous instance for " |
| 300 | << path.str << "\n"; |
| 301 | I2CDevice tmp(*params); |
| 302 | } |
| 303 | |
| 304 | try |
| 305 | { |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 306 | devices.emplace( |
| 307 | path.str, |
| 308 | std::make_pair(std::make_shared<I2CDevice>(*params), |
| 309 | true)); |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 310 | } |
| 311 | catch (std::runtime_error&) |
| 312 | { |
| 313 | std::cerr << "Failed to instantiate " << params->type->name |
| 314 | << " at address " << params->address << " on bus " |
| 315 | << params->bus << "\n"; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 320 | return devices; |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 321 | } |
| 322 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 323 | void createSensors( |
| 324 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 325 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 326 | sensors, |
| 327 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 328 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 329 | sensorsChanged, |
| 330 | bool activateOnly) |
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 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 333 | dbusConnection, |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 334 | [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged, |
| 335 | activateOnly](const ManagedObjectType& sensorConfigurations) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 336 | bool firstScan = sensorsChanged == nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 337 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 338 | SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations); |
Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 339 | |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 340 | auto devices = instantiateDevices(sensorConfigurations, sensors); |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 341 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 342 | // IIO _raw devices look like this on sysfs: |
| 343 | // /sys/bus/iio/devices/iio:device0/in_temp_raw |
| 344 | // /sys/bus/iio/devices/iio:device0/in_temp_offset |
| 345 | // /sys/bus/iio/devices/iio:device0/in_temp_scale |
| 346 | // |
| 347 | // Other IIO devices look like this on sysfs: |
| 348 | // /sys/bus/iio/devices/iio:device1/in_temp_input |
| 349 | // /sys/bus/iio/devices/iio:device1/in_pressure_input |
| 350 | std::vector<fs::path> paths; |
| 351 | fs::path root("/sys/bus/iio/devices"); |
| 352 | findFiles(root, R"(in_temp\d*_(input|raw))", paths); |
| 353 | findFiles(root, R"(in_pressure\d*_(input|raw))", paths); |
| 354 | findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths); |
| 355 | findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 356 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 357 | // iterate through all found temp and pressure sensors, |
| 358 | // and try to match them with configuration |
| 359 | for (auto& path : paths) |
| 360 | { |
| 361 | std::smatch match; |
| 362 | const std::string pathStr = path.string(); |
| 363 | auto directory = path.parent_path(); |
| 364 | fs::path device; |
| 365 | |
| 366 | std::string deviceName; |
| 367 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 368 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 369 | device = fs::canonical(directory); |
| 370 | deviceName = device.parent_path().stem(); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | device = directory / "device"; |
| 375 | deviceName = fs::canonical(device).stem(); |
| 376 | } |
| 377 | auto findHyphen = deviceName.find('-'); |
| 378 | if (findHyphen == std::string::npos) |
| 379 | { |
| 380 | std::cerr << "found bad device " << deviceName << "\n"; |
| 381 | continue; |
| 382 | } |
| 383 | std::string busStr = deviceName.substr(0, findHyphen); |
| 384 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 385 | |
| 386 | uint64_t bus = 0; |
| 387 | uint64_t addr = 0; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 388 | std::from_chars_result res{}; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 389 | res = std::from_chars(busStr.data(), busStr.data() + busStr.size(), |
| 390 | bus); |
| 391 | if (res.ec != std::errc{}) |
| 392 | { |
| 393 | continue; |
| 394 | } |
| 395 | res = std::from_chars(addrStr.data(), |
| 396 | addrStr.data() + addrStr.size(), addr, 16); |
| 397 | if (res.ec != std::errc{}) |
| 398 | { |
| 399 | continue; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 402 | auto thisSensorParameters = getSensorParameters(path); |
| 403 | auto findSensorCfg = configMap.find({bus, addr}); |
| 404 | if (findSensorCfg == configMap.end()) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 405 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 406 | continue; |
| 407 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 408 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 409 | const std::string& interfacePath = findSensorCfg->second.sensorPath; |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 410 | auto findI2CDev = devices.find(interfacePath); |
| 411 | |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 412 | std::shared_ptr<I2CDevice> i2cDev; |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 413 | if (findI2CDev != devices.end()) |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 414 | { |
Joseph Fu | 9bbeff7 | 2022-12-05 19:09:16 +0800 | [diff] [blame] | 415 | // If we're only looking to activate newly-instantiated i2c |
| 416 | // devices and this sensor's underlying device was already there |
| 417 | // before this call, there's nothing more to do here. |
| 418 | if (activateOnly && !findI2CDev->second.second) |
| 419 | { |
| 420 | continue; |
| 421 | } |
Zev Weiss | 7627c86 | 2022-11-17 14:23:04 -0800 | [diff] [blame] | 422 | i2cDev = findI2CDev->second.first; |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 425 | const SensorData& sensorData = findSensorCfg->second.sensorData; |
| 426 | const std::string& sensorType = findSensorCfg->second.interface; |
| 427 | const SensorBaseConfigMap& baseConfigMap = |
| 428 | findSensorCfg->second.config; |
| 429 | std::vector<std::string>& hwmonName = findSensorCfg->second.name; |
| 430 | |
| 431 | // Temperature has "Name", pressure has "Name1" |
| 432 | auto findSensorName = baseConfigMap.find("Name"); |
| 433 | int index = 1; |
| 434 | if (thisSensorParameters.typeName == "pressure" || |
| 435 | thisSensorParameters.typeName == "humidity") |
| 436 | { |
| 437 | findSensorName = baseConfigMap.find("Name1"); |
| 438 | index = 2; |
| 439 | } |
| 440 | |
| 441 | if (findSensorName == baseConfigMap.end()) |
| 442 | { |
| 443 | std::cerr << "could not determine configuration name for " |
| 444 | << deviceName << "\n"; |
| 445 | continue; |
| 446 | } |
| 447 | std::string sensorName = |
| 448 | std::get<std::string>(findSensorName->second); |
| 449 | // on rescans, only update sensors we were signaled by |
| 450 | auto findSensor = sensors.find(sensorName); |
| 451 | if (!firstScan && findSensor != sensors.end()) |
| 452 | { |
| 453 | bool found = false; |
| 454 | auto it = sensorsChanged->begin(); |
| 455 | while (it != sensorsChanged->end()) |
| 456 | { |
Zev Weiss | 6c106d6 | 2022-08-17 20:50:00 -0700 | [diff] [blame] | 457 | if (it->ends_with(findSensor->second->name)) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 458 | { |
| 459 | it = sensorsChanged->erase(it); |
| 460 | findSensor->second = nullptr; |
| 461 | found = true; |
| 462 | break; |
| 463 | } |
| 464 | ++it; |
| 465 | } |
| 466 | if (!found) |
| 467 | { |
| 468 | continue; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | std::vector<thresholds::Threshold> sensorThresholds; |
| 473 | |
| 474 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds, |
| 475 | nullptr, &index)) |
| 476 | { |
| 477 | std::cerr << "error populating thresholds for " << sensorName |
| 478 | << " index " << index << "\n"; |
| 479 | } |
| 480 | |
Zev Weiss | 8569bf2 | 2022-10-11 15:37:44 -0700 | [diff] [blame] | 481 | float pollRate = getPollRate(baseConfigMap, pollRateDefault); |
Zev Weiss | a4d2768 | 2022-07-19 15:30:36 -0700 | [diff] [blame] | 482 | PowerState readState = getPowerState(baseConfigMap); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 483 | |
| 484 | auto permitSet = getPermitSet(baseConfigMap); |
| 485 | auto& sensor = sensors[sensorName]; |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 486 | if (!activateOnly) |
| 487 | { |
| 488 | sensor = nullptr; |
| 489 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 490 | auto hwmonFile = |
| 491 | getFullHwmonFilePath(directory.string(), "temp1", permitSet); |
| 492 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
| 493 | { |
| 494 | hwmonFile = pathStr; |
| 495 | } |
| 496 | if (hwmonFile) |
| 497 | { |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 498 | if (sensor != nullptr) |
| 499 | { |
| 500 | sensor->activate(*hwmonFile, i2cDev); |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | sensor = std::make_shared<HwmonTempSensor>( |
| 505 | *hwmonFile, sensorType, objectServer, dbusConnection, |
| 506 | io, sensorName, std::move(sensorThresholds), |
| 507 | thisSensorParameters, pollRate, interfacePath, |
| 508 | readState, i2cDev); |
| 509 | sensor->setupRead(); |
| 510 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 511 | } |
| 512 | hwmonName.erase( |
| 513 | remove(hwmonName.begin(), hwmonName.end(), sensorName), |
| 514 | hwmonName.end()); |
| 515 | |
| 516 | // Looking for keys like "Name1" for temp2_input, |
| 517 | // "Name2" for temp3_input, etc. |
| 518 | int i = 0; |
| 519 | while (true) |
| 520 | { |
| 521 | ++i; |
| 522 | auto findKey = baseConfigMap.find("Name" + std::to_string(i)); |
| 523 | if (findKey == baseConfigMap.end()) |
| 524 | { |
| 525 | break; |
| 526 | } |
| 527 | std::string sensorName = std::get<std::string>(findKey->second); |
| 528 | hwmonFile = getFullHwmonFilePath(directory.string(), |
| 529 | "temp" + std::to_string(i + 1), |
| 530 | permitSet); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 531 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 532 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 533 | continue; |
| 534 | } |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 535 | if (hwmonFile) |
| 536 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 537 | // To look up thresholds for these additional sensors, |
| 538 | // match on the Index property in the threshold data |
| 539 | // where the index comes from the sysfs file we're on, |
| 540 | // i.e. index = 2 for temp2_input. |
| 541 | int index = i + 1; |
| 542 | std::vector<thresholds::Threshold> thresholds; |
| 543 | |
| 544 | if (!parseThresholdsFromConfig(sensorData, thresholds, |
| 545 | nullptr, &index)) |
| 546 | { |
| 547 | std::cerr << "error populating thresholds for " |
| 548 | << sensorName << " index " << index << "\n"; |
| 549 | } |
| 550 | |
| 551 | auto& sensor = sensors[sensorName]; |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 552 | if (!activateOnly) |
| 553 | { |
| 554 | sensor = nullptr; |
| 555 | } |
| 556 | |
| 557 | if (sensor != nullptr) |
| 558 | { |
| 559 | sensor->activate(*hwmonFile, i2cDev); |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | sensor = std::make_shared<HwmonTempSensor>( |
| 564 | *hwmonFile, sensorType, objectServer, |
| 565 | dbusConnection, io, sensorName, |
| 566 | std::move(thresholds), thisSensorParameters, |
| 567 | pollRate, interfacePath, readState, i2cDev); |
| 568 | sensor->setupRead(); |
| 569 | } |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 570 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 571 | |
Willy Tu | 9eb0cc3 | 2022-04-06 11:03:32 -0700 | [diff] [blame] | 572 | hwmonName.erase( |
| 573 | remove(hwmonName.begin(), hwmonName.end(), sensorName), |
| 574 | hwmonName.end()); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 575 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 576 | if (hwmonName.empty()) |
| 577 | { |
| 578 | configMap.erase(findSensorCfg); |
| 579 | } |
| 580 | } |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 581 | }); |
Zev Weiss | d29f8aa | 2022-08-03 16:24:59 -0700 | [diff] [blame] | 582 | std::vector<std::string> types(sensorTypes.size()); |
| 583 | for (const auto& [type, dt] : sensorTypes) |
| 584 | { |
| 585 | types.push_back(type); |
| 586 | } |
| 587 | getter->getConfiguration(types); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 590 | void interfaceRemoved( |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 591 | sdbusplus::message_t& message, |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 592 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
| 593 | sensors) |
| 594 | { |
| 595 | if (message.is_method_error()) |
| 596 | { |
| 597 | std::cerr << "interfacesRemoved callback method error\n"; |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | sdbusplus::message::object_path path; |
| 602 | std::vector<std::string> interfaces; |
| 603 | |
| 604 | message.read(path, interfaces); |
| 605 | |
| 606 | // If the xyz.openbmc_project.Confguration.X interface was removed |
| 607 | // for one or more sensors, delete those sensor objects. |
| 608 | auto sensorIt = sensors.begin(); |
| 609 | while (sensorIt != sensors.end()) |
| 610 | { |
| 611 | if ((sensorIt->second->configurationPath == path) && |
| 612 | (std::find(interfaces.begin(), interfaces.end(), |
| 613 | sensorIt->second->objectType) != interfaces.end())) |
| 614 | { |
| 615 | sensorIt = sensors.erase(sensorIt); |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | sensorIt++; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 624 | static void powerStateChanged( |
| 625 | PowerState type, bool newState, |
| 626 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
| 627 | sensors, |
| 628 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 629 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 630 | { |
| 631 | if (newState) |
| 632 | { |
| 633 | createSensors(io, objectServer, sensors, dbusConnection, nullptr, true); |
| 634 | } |
| 635 | else |
| 636 | { |
| 637 | for (auto& [path, sensor] : sensors) |
| 638 | { |
| 639 | if (sensor != nullptr && sensor->readState == type) |
| 640 | { |
| 641 | sensor->deactivate(); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 647 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 648 | { |
| 649 | boost::asio::io_service io; |
| 650 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 651 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 652 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 653 | systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 654 | |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 655 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 656 | sensors; |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 657 | auto sensorsChanged = |
| 658 | std::make_shared<boost::container::flat_set<std::string>>(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 659 | |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 660 | auto powerCallBack = [&sensors, &io, &objectServer, |
| 661 | &systemBus](PowerState type, bool state) { |
| 662 | powerStateChanged(type, state, sensors, io, objectServer, systemBus); |
| 663 | }; |
| 664 | setupPowerMatchCallback(systemBus, powerCallBack); |
| 665 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 666 | io.post([&]() { |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 667 | createSensors(io, objectServer, sensors, systemBus, nullptr, false); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 668 | }); |
| 669 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 670 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 671 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 672 | [&](sdbusplus::message_t& message) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 673 | if (message.is_method_error()) |
| 674 | { |
| 675 | std::cerr << "callback method error\n"; |
| 676 | return; |
| 677 | } |
| 678 | sensorsChanged->insert(message.get_path()); |
| 679 | // this implicitly cancels the timer |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 680 | filterTimer.expires_from_now(std::chrono::seconds(1)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 681 | |
| 682 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 683 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 684 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 685 | /* we were canceled*/ |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 686 | return; |
| 687 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 688 | if (ec) |
| 689 | { |
| 690 | std::cerr << "timer error\n"; |
| 691 | return; |
| 692 | } |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 693 | createSensors(io, objectServer, sensors, systemBus, sensorsChanged, |
| 694 | false); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 695 | }); |
| 696 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 697 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 698 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 699 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 700 | setupManufacturingModeMatch(*systemBus); |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 701 | |
| 702 | // Watch for entity-manager to remove configuration interfaces |
| 703 | // so the corresponding sensors can be removed. |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 704 | auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 705 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 706 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 707 | std::string(inventoryPath) + "/'", |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 708 | [&sensors](sdbusplus::message_t& msg) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 709 | interfaceRemoved(msg, sensors); |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 710 | }); |
| 711 | |
| 712 | matches.emplace_back(std::move(ifaceRemovedMatch)); |
| 713 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 714 | io.run(); |
| 715 | } |