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