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