blob: 58f017fc80efa78464143b1b07f2d0b57b509171 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
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 Jefferye73bd0a2023-01-25 10:39:57 +103017#include "DeviceMgmt.hpp"
18#include "HwmonTempSensor.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "SensorPaths.hpp"
20#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103021#include "Utils.hpp"
22
Ed Tanouseacbfdd2024-04-04 12:00:24 -070023#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 Venture96e97db2019-10-31 13:44:38 -070027#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070028#include <boost/container/flat_set.hpp>
George Liu8b065182025-02-20 14:08:47 +080029#include <phosphor-logging/lg2.hpp>
30#include <phosphor-logging/lg2/flags.hpp>
James Feist38fb5982020-05-28 10:09:54 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <sdbusplus/message.hpp>
36#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070037
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070039#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070040#include <chrono>
41#include <cstddef>
42#include <cstdint>
James Feist24f02f22019-04-15 11:05:39 -070043#include <filesystem>
Patrick Venture96e97db2019-10-31 13:44:38 -070044#include <functional>
45#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#include <optional>
James Feist6714a252018-09-10 15:26:18 -070047#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070048#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070049#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070050#include <utility>
51#include <variant>
52#include <vector>
James Feist6714a252018-09-10 15:26:18 -070053
Jeff Lin87bc67f2020-12-04 20:58:01 +080054static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070055
Zhikui Rencb359da2023-08-03 16:45:44 -070056static constexpr double maxValuePressure = 120000; // Pascals
57static constexpr double minValuePressure = 30000; // Pascals
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050058
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060059static constexpr double maxValueRelativeHumidity = 100; // PercentRH
60static constexpr double minValueRelativeHumidity = 0; // PercentRH
61
Zhikui Rencb359da2023-08-03 16:45:44 -070062static constexpr double maxValueTemperature = 127; // DegreesC
63static constexpr double minValueTemperature = -128; // DegreesC
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050064
Zev Weissd29f8aa2022-08-03 16:24:59 -070065static const I2CDeviceTypeMap sensorTypes{
Patrick Rudolph76d36762023-09-20 16:53:38 +020066 {"ADM1021", I2CDeviceType{"adm1021", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070067 {"DPS310", I2CDeviceType{"dps310", false}},
Delphine CC Chiu53896db2024-06-07 18:36:13 +080068 {"EMC1403", I2CDeviceType{"emc1403", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070069 {"EMC1412", I2CDeviceType{"emc1412", true}},
70 {"EMC1413", I2CDeviceType{"emc1413", true}},
71 {"EMC1414", I2CDeviceType{"emc1414", true}},
72 {"HDC1080", I2CDeviceType{"hdc1080", false}},
73 {"JC42", I2CDeviceType{"jc42", true}},
74 {"LM75A", I2CDeviceType{"lm75a", true}},
75 {"LM95234", I2CDeviceType{"lm95234", true}},
76 {"MAX31725", I2CDeviceType{"max31725", true}},
77 {"MAX31730", I2CDeviceType{"max31730", true}},
78 {"MAX6581", I2CDeviceType{"max6581", true}},
79 {"MAX6654", I2CDeviceType{"max6654", true}},
Patrick Rudolphf3862ee2023-10-23 14:13:06 +020080 {"MAX6639", I2CDeviceType{"max6639", true}},
Potin Lai28b88232023-12-13 16:41:01 +080081 {"MCP9600", I2CDeviceType{"mcp9600", false}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070082 {"NCT6779", I2CDeviceType{"nct6779", true}},
83 {"NCT7802", I2CDeviceType{"nct7802", true}},
Potin Laifb011cc2024-02-22 19:51:48 +080084 {"PT5161L", I2CDeviceType{"pt5161l", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070085 {"SBTSI", I2CDeviceType{"sbtsi", true}},
86 {"SI7020", I2CDeviceType{"si7020", false}},
Tim Lee9b0a6f62022-12-23 14:47:02 +080087 {"TMP100", I2CDeviceType{"tmp100", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070088 {"TMP112", I2CDeviceType{"tmp112", true}},
89 {"TMP175", I2CDeviceType{"tmp175", true}},
Chris Sides40806112025-02-27 12:06:08 -060090 {"TMP411", I2CDeviceType{"tmp411", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070091 {"TMP421", I2CDeviceType{"tmp421", true}},
Andrew Geissler8800c042024-07-23 17:19:17 -030092 {"TMP432", I2CDeviceType{"tmp432", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070093 {"TMP441", I2CDeviceType{"tmp441", true}},
Potin Lai06747882025-05-27 15:46:26 +080094 {"TMP451", I2CDeviceType{"tmp451", true}},
Khang Kieuf4c3fad2023-11-27 22:39:01 +000095 {"TMP461", I2CDeviceType{"tmp461", true}},
Hieu Huynh9a7db6a2023-06-19 11:02:07 +000096 {"TMP464", I2CDeviceType{"tmp464", true}},
Hieu Huynh10480772024-09-13 11:23:46 +000097 {"TMP468", I2CDeviceType{"tmp468", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070098 {"TMP75", I2CDeviceType{"tmp75", true}},
99 {"W83773G", I2CDeviceType{"w83773g", true}},
100};
James Feist6714a252018-09-10 15:26:18 -0700101
Patrick Williams556e04b2025-02-01 08:22:22 -0500102static struct SensorParams getSensorParameters(
103 const std::filesystem::path& path)
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500104{
105 // offset is to default to 0 and scale to 1, see lore
106 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
Patrick Williams779c96a2023-05-10 07:50:42 -0500107 struct SensorParams tmpSensorParameters = {
108 .minValue = minValueTemperature,
109 .maxValue = maxValueTemperature,
110 .offsetValue = 0.0,
111 .scaleValue = 1.0,
112 .units = sensor_paths::unitDegreesC,
113 .typeName = "temperature"};
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500114
115 // For IIO RAW sensors we get a raw_value, an offset, and scale
116 // to compute the value = (raw_value + offset) * scale
117 // with a _raw IIO device we need to get the
118 // offsetValue and scaleValue from the driver
119 // these are used to compute the reading in
120 // units that have yet to be scaled for D-Bus.
121 const std::string pathStr = path.string();
122 if (pathStr.ends_with("_raw"))
123 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400124 std::string pathOffsetStr =
125 pathStr.substr(0, pathStr.size() - 4) + "_offset";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500126 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
127 // In case there is nothing to read skip this device
128 // This is not an error condition see lore
129 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
130 if (tmpOffsetValue)
131 {
132 tmpSensorParameters.offsetValue = *tmpOffsetValue;
133 }
134
Patrick Williams2aaf7172024-08-16 15:20:40 -0400135 std::string pathScaleStr =
136 pathStr.substr(0, pathStr.size() - 4) + "_scale";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500137 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
138 // In case there is nothing to read skip this device
139 // This is not an error condition see lore
140 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
141 if (tmpScaleValue)
142 {
143 tmpSensorParameters.scaleValue = *tmpScaleValue;
144 }
145 }
146
147 // Temperatures are read in milli degrees Celsius, we need
148 // degrees Celsius. Pressures are read in kilopascal, we need
149 // Pascals. On D-Bus for Open BMC we use the International
150 // System of Units without prefixes. Links to the kernel
151 // documentation:
152 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
153 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
154 if (path.filename() == "in_pressure_input" ||
155 path.filename() == "in_pressure_raw")
156 {
157 tmpSensorParameters.minValue = minValuePressure;
158 tmpSensorParameters.maxValue = maxValuePressure;
159 // Pressures are read in kilopascal, we need Pascals.
160 tmpSensorParameters.scaleValue *= 1000.0;
161 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600162 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500163 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600164 else if (path.filename() == "in_humidityrelative_input" ||
165 path.filename() == "in_humidityrelative_raw")
166 {
167 tmpSensorParameters.minValue = minValueRelativeHumidity;
168 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
169 // Relative Humidity are read in milli-percent, we need percent.
170 tmpSensorParameters.scaleValue *= 0.001;
171 tmpSensorParameters.typeName = "humidity";
Potin Laia9c1bec2024-04-19 08:35:56 +0800172 tmpSensorParameters.units = sensor_paths::unitPercentRH;
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600173 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500174 else
175 {
176 // Temperatures are read in milli degrees Celsius,
177 // we need degrees Celsius.
178 tmpSensorParameters.scaleValue *= 0.001;
179 }
180
181 return tmpSensorParameters;
182}
183
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530184struct SensorConfigKey
185{
186 uint64_t bus;
187 uint64_t addr;
188 bool operator<(const SensorConfigKey& other) const
189 {
190 if (bus != other.bus)
191 {
192 return bus < other.bus;
193 }
194 return addr < other.addr;
195 }
196};
197
198struct SensorConfig
199{
200 std::string sensorPath;
201 SensorData sensorData;
202 std::string interface;
203 SensorBaseConfigMap config;
204 std::vector<std::string> name;
205};
206
207using SensorConfigMap =
208 boost::container::flat_map<SensorConfigKey, SensorConfig>;
209
Patrick Williams556e04b2025-02-01 08:22:22 -0500210static SensorConfigMap buildSensorConfigMap(
211 const ManagedObjectType& sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530212{
213 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700214 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530215 {
Zev Weissa1808332022-08-12 18:21:01 -0700216 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530217 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530218 auto busCfg = cfg.find("Bus");
219 auto addrCfg = cfg.find("Address");
220 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
221 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530222 continue;
223 }
224
Ed Tanous2049bd22022-07-09 07:20:26 -0700225 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
226 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530227 {
George Liu8b065182025-02-20 14:08:47 +0800228 lg2::error("'{PATH}' Bus or Address invalid", "PATH", path.str);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530229 continue;
230 }
231
232 std::vector<std::string> hwmonNames;
233 auto nameCfg = cfg.find("Name");
234 if (nameCfg != cfg.end())
235 {
236 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
237 size_t i = 1;
238 while (true)
239 {
240 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
241 if (sensorNameCfg == cfg.end())
242 {
243 break;
244 }
245 hwmonNames.push_back(
246 std::get<std::string>(sensorNameCfg->second));
247 i++;
248 }
249 }
250
251 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
252 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700253 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530254
255 auto [it, inserted] = configMap.emplace(key, std::move(val));
256 if (!inserted)
257 {
George Liu8b065182025-02-20 14:08:47 +0800258 lg2::error(
259 "'{PATH}': ignoring duplicate entry for '{BUS}', '{ADDR}'",
260 "PATH", path.str, "BUS", key.bus, "ADDR", lg2::hex,
261 key.addr);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530262 }
263 }
264 }
265 return configMap;
266}
267
James Feist6714a252018-09-10 15:26:18 -0700268void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800269 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800270 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700271 sensors,
272 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700273 const std::shared_ptr<boost::container::flat_set<std::string>>&
Zev Weissa1456c42022-07-18 16:59:35 -0700274 sensorsChanged,
275 bool activateOnly)
James Feist6714a252018-09-10 15:26:18 -0700276{
James Feistdf515152019-09-18 16:40:40 -0700277 auto getter = std::make_shared<GetSensorConfiguration>(
278 dbusConnection,
Zev Weissa1456c42022-07-18 16:59:35 -0700279 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
280 activateOnly](const ManagedObjectType& sensorConfigurations) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400281 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700282
Patrick Williams2aaf7172024-08-16 15:20:40 -0400283 SensorConfigMap configMap =
284 buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530285
Patrick Williams2aaf7172024-08-16 15:20:40 -0400286 auto devices =
287 instantiateDevices(sensorConfigurations, sensors, sensorTypes);
Zev Weissa1456c42022-07-18 16:59:35 -0700288
Patrick Williams2aaf7172024-08-16 15:20:40 -0400289 // IIO _raw devices look like this on sysfs:
290 // /sys/bus/iio/devices/iio:device0/in_temp_raw
291 // /sys/bus/iio/devices/iio:device0/in_temp_offset
292 // /sys/bus/iio/devices/iio:device0/in_temp_scale
293 //
294 // Other IIO devices look like this on sysfs:
295 // /sys/bus/iio/devices/iio:device1/in_temp_input
296 // /sys/bus/iio/devices/iio:device1/in_pressure_input
Ed Tanous2e466962025-01-30 10:59:49 -0800297 std::vector<std::filesystem::path> paths;
298 std::filesystem::path root("/sys/bus/iio/devices");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400299 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
300 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
301 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
Ed Tanous2e466962025-01-30 10:59:49 -0800302 findFiles(std::filesystem::path("/sys/class/hwmon"),
303 R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500304
Patrick Williams2aaf7172024-08-16 15:20:40 -0400305 // iterate through all found temp and pressure sensors,
306 // and try to match them with configuration
307 for (auto& path : paths)
James Feist37266ca2018-10-15 15:56:28 -0700308 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400309 std::smatch match;
310 const std::string pathStr = path.string();
311 auto directory = path.parent_path();
Ed Tanous2e466962025-01-30 10:59:49 -0800312 std::filesystem::path device;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400313
314 std::string deviceName;
315 std::error_code ec;
316 if (pathStr.starts_with("/sys/bus/iio/devices"))
Zhikui Renae463962024-01-02 15:35:31 -0800317 {
Ed Tanous2e466962025-01-30 10:59:49 -0800318 device = std::filesystem::canonical(directory, ec);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400319 if (ec)
Ed Tanousbb679322022-05-16 16:10:00 -0700320 {
George Liu8b065182025-02-20 14:08:47 +0800321 lg2::error("Fail to find device in '{PATH}'", "PATH",
322 pathStr);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400323 continue;
Ed Tanousbb679322022-05-16 16:10:00 -0700324 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400325 deviceName = device.parent_path().stem();
Zev Weissa1456c42022-07-18 16:59:35 -0700326 }
327 else
328 {
Ed Tanous2e466962025-01-30 10:59:49 -0800329 device =
330 std::filesystem::canonical(directory / "device", ec);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400331 if (ec)
332 {
George Liu8b065182025-02-20 14:08:47 +0800333 lg2::error("Fail to find device in '{PATH}'", "PATH",
334 pathStr);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400335 continue;
336 }
337 deviceName = device.stem();
Zev Weissa1456c42022-07-18 16:59:35 -0700338 }
Ed Tanousbb679322022-05-16 16:10:00 -0700339
Patrick Williams2aaf7172024-08-16 15:20:40 -0400340 uint64_t bus = 0;
341 uint64_t addr = 0;
342 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feist37266ca2018-10-15 15:56:28 -0700343 {
James Feistdf515152019-09-18 16:40:40 -0700344 continue;
345 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400346
347 auto thisSensorParameters = getSensorParameters(path);
348 auto findSensorCfg = configMap.find({bus, addr});
349 if (findSensorCfg == configMap.end())
350 {
351 continue;
352 }
353
354 const std::string& interfacePath =
355 findSensorCfg->second.sensorPath;
356 auto findI2CDev = devices.find(interfacePath);
357
358 std::shared_ptr<I2CDevice> i2cDev;
359 if (findI2CDev != devices.end())
360 {
361 // If we're only looking to activate newly-instantiated i2c
362 // devices and this sensor's underlying device was already
363 // there before this call, there's nothing more to do here.
364 if (activateOnly && !findI2CDev->second.second)
365 {
366 continue;
367 }
368 i2cDev = findI2CDev->second.first;
369 }
370
371 const SensorData& sensorData = findSensorCfg->second.sensorData;
372 std::string sensorType = findSensorCfg->second.interface;
373 auto pos = sensorType.find_last_of('.');
374 if (pos != std::string::npos)
375 {
376 sensorType = sensorType.substr(pos + 1);
377 }
378 const SensorBaseConfigMap& baseConfigMap =
379 findSensorCfg->second.config;
380 std::vector<std::string>& hwmonName =
381 findSensorCfg->second.name;
382
383 // Temperature has "Name", pressure has "Name1"
384 auto findSensorName = baseConfigMap.find("Name");
385 int index = 1;
386 if (thisSensorParameters.typeName == "pressure" ||
387 thisSensorParameters.typeName == "humidity")
388 {
389 findSensorName = baseConfigMap.find("Name1");
390 index = 2;
391 }
392
393 if (findSensorName == baseConfigMap.end())
394 {
George Liu8b065182025-02-20 14:08:47 +0800395 lg2::error(
396 "could not determine configuration name for '{NAME}'",
397 "NAME", deviceName);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400398 continue;
399 }
400 std::string sensorName =
401 std::get<std::string>(findSensorName->second);
402 // on rescans, only update sensors we were signaled by
403 auto findSensor = sensors.find(sensorName);
404 if (!firstScan && findSensor != sensors.end())
405 {
406 bool found = false;
407 auto it = sensorsChanged->begin();
408 while (it != sensorsChanged->end())
409 {
410 if (it->ends_with(findSensor->second->name))
411 {
412 it = sensorsChanged->erase(it);
413 findSensor->second = nullptr;
414 found = true;
415 break;
416 }
417 ++it;
418 }
419 if (!found)
420 {
421 continue;
422 }
423 }
424
425 std::vector<thresholds::Threshold> sensorThresholds;
426
427 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
428 nullptr, &index))
429 {
George Liu8b065182025-02-20 14:08:47 +0800430 lg2::error("error populating thresholds for "
431 "'{NAME}', index: '{INDEX}'",
432 "NAME", sensorName, "INDEX", index);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400433 }
434
435 float pollRate = getPollRate(baseConfigMap, pollRateDefault);
436 PowerState readState = getPowerState(baseConfigMap);
437
438 auto permitSet = getPermitSet(baseConfigMap);
439 auto& sensor = sensors[sensorName];
440 if (!activateOnly)
441 {
442 sensor = nullptr;
443 }
444 auto hwmonFile = getFullHwmonFilePath(directory.string(),
445 "temp1", permitSet);
446 if (pathStr.starts_with("/sys/bus/iio/devices"))
447 {
448 hwmonFile = pathStr;
449 }
Jason Ling100c20b2020-08-11 14:50:33 -0700450 if (hwmonFile)
451 {
Zev Weissa1456c42022-07-18 16:59:35 -0700452 if (sensor != nullptr)
453 {
454 sensor->activate(*hwmonFile, i2cDev);
455 }
456 else
457 {
458 sensor = std::make_shared<HwmonTempSensor>(
459 *hwmonFile, sensorType, objectServer,
460 dbusConnection, io, sensorName,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400461 std::move(sensorThresholds), thisSensorParameters,
Zev Weissa1456c42022-07-18 16:59:35 -0700462 pollRate, interfacePath, readState, i2cDev);
463 sensor->setupRead();
464 }
Jason Ling100c20b2020-08-11 14:50:33 -0700465 }
Willy Tu9eb0cc32022-04-06 11:03:32 -0700466 hwmonName.erase(
467 remove(hwmonName.begin(), hwmonName.end(), sensorName),
468 hwmonName.end());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400469
470 // Looking for keys like "Name1" for temp2_input,
471 // "Name2" for temp3_input, etc.
472 int i = 0;
473 while (true)
474 {
475 ++i;
476 auto findKey =
477 baseConfigMap.find("Name" + std::to_string(i));
478 if (findKey == baseConfigMap.end())
479 {
480 break;
481 }
482 std::string sensorName =
483 std::get<std::string>(findKey->second);
484 hwmonFile = getFullHwmonFilePath(
485 directory.string(), "temp" + std::to_string(i + 1),
486 permitSet);
487 if (pathStr.starts_with("/sys/bus/iio/devices"))
488 {
489 continue;
490 }
491 if (hwmonFile)
492 {
493 // To look up thresholds for these additional sensors,
494 // match on the Index property in the threshold data
495 // where the index comes from the sysfs file we're on,
496 // i.e. index = 2 for temp2_input.
497 int index = i + 1;
498 std::vector<thresholds::Threshold> thresholds;
499
500 if (!parseThresholdsFromConfig(sensorData, thresholds,
501 nullptr, &index))
502 {
George Liu8b065182025-02-20 14:08:47 +0800503 lg2::error("error populating thresholds for "
504 "'{NAME}', index: '{INDEX}'",
505 "NAME", sensorName, "INDEX", index);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400506 }
507
508 auto& sensor = sensors[sensorName];
509 if (!activateOnly)
510 {
511 sensor = nullptr;
512 }
513
514 if (sensor != nullptr)
515 {
516 sensor->activate(*hwmonFile, i2cDev);
517 }
518 else
519 {
520 sensor = std::make_shared<HwmonTempSensor>(
521 *hwmonFile, sensorType, objectServer,
522 dbusConnection, io, sensorName,
523 std::move(thresholds), thisSensorParameters,
524 pollRate, interfacePath, readState, i2cDev);
525 sensor->setupRead();
526 }
527 }
528
529 hwmonName.erase(
530 remove(hwmonName.begin(), hwmonName.end(), sensorName),
531 hwmonName.end());
532 }
533 if (hwmonName.empty())
534 {
535 configMap.erase(findSensorCfg);
536 }
James Feistdf515152019-09-18 16:40:40 -0700537 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400538 });
Zev Weissd29f8aa2022-08-03 16:24:59 -0700539 std::vector<std::string> types(sensorTypes.size());
540 for (const auto& [type, dt] : sensorTypes)
541 {
542 types.push_back(type);
543 }
544 getter->getConfiguration(types);
James Feist6714a252018-09-10 15:26:18 -0700545}
546
Matt Spinler20bf2c12021-09-14 11:07:07 -0500547void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500548 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500549 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
550 sensors)
551{
552 if (message.is_method_error())
553 {
George Liu8b065182025-02-20 14:08:47 +0800554 lg2::error("interfacesRemoved callback method error");
Matt Spinler20bf2c12021-09-14 11:07:07 -0500555 return;
556 }
557
558 sdbusplus::message::object_path path;
559 std::vector<std::string> interfaces;
560
561 message.read(path, interfaces);
562
563 // If the xyz.openbmc_project.Confguration.X interface was removed
564 // for one or more sensors, delete those sensor objects.
565 auto sensorIt = sensors.begin();
566 while (sensorIt != sensors.end())
567 {
Patrick Rudolph6289f5a2023-09-22 11:32:54 +0200568 if (sensorIt->second && (sensorIt->second->configurationPath == path) &&
Matt Spinler20bf2c12021-09-14 11:07:07 -0500569 (std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500570 sensorIt->second->configInterface) != interfaces.end()))
Matt Spinler20bf2c12021-09-14 11:07:07 -0500571 {
572 sensorIt = sensors.erase(sensorIt);
573 }
574 else
575 {
576 sensorIt++;
577 }
578 }
579}
580
Zev Weissa1456c42022-07-18 16:59:35 -0700581static void powerStateChanged(
582 PowerState type, bool newState,
583 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
584 sensors,
Ed Tanous1f978632023-02-28 18:16:39 -0800585 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zev Weissa1456c42022-07-18 16:59:35 -0700586 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
587{
588 if (newState)
589 {
590 createSensors(io, objectServer, sensors, dbusConnection, nullptr, true);
591 }
592 else
593 {
594 for (auto& [path, sensor] : sensors)
595 {
596 if (sensor != nullptr && sensor->readState == type)
597 {
598 sensor->deactivate();
599 }
600 }
601 }
602}
603
James Feistb6c0b912019-07-09 12:21:44 -0700604int main()
James Feist6714a252018-09-10 15:26:18 -0700605{
Ed Tanous1f978632023-02-28 18:16:39 -0800606 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700607 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700608 sdbusplus::asio::object_server objectServer(systemBus, true);
609 objectServer.add_manager("/xyz/openbmc_project/sensors");
James Feist6714a252018-09-10 15:26:18 -0700610 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700611
Yong Lif3fd1912020-03-25 21:35:23 +0800612 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700613 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700614 auto sensorsChanged =
615 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700616
Zev Weissa1456c42022-07-18 16:59:35 -0700617 auto powerCallBack = [&sensors, &io, &objectServer,
618 &systemBus](PowerState type, bool state) {
619 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
620 };
621 setupPowerMatchCallback(systemBus, powerCallBack);
622
Ed Tanous83db50c2023-03-01 10:20:24 -0800623 boost::asio::post(io, [&]() {
Zev Weissa1456c42022-07-18 16:59:35 -0700624 createSensors(io, objectServer, sensors, systemBus, nullptr, false);
James Feist6714a252018-09-10 15:26:18 -0700625 });
626
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700627 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500628 std::function<void(sdbusplus::message_t&)> eventHandler =
629 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400630 if (message.is_method_error())
631 {
George Liu8b065182025-02-20 14:08:47 +0800632 lg2::error("callback method error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400633 return;
634 }
635 sensorsChanged->insert(message.get_path());
636 // this implicitly cancels the timer
637 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700638
Patrick Williams2aaf7172024-08-16 15:20:40 -0400639 filterTimer.async_wait([&](const boost::system::error_code& ec) {
640 if (ec == boost::asio::error::operation_aborted)
641 {
642 /* we were canceled*/
643 return;
644 }
645 if (ec)
646 {
George Liu8b065182025-02-20 14:08:47 +0800647 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400648 return;
649 }
650 createSensors(io, objectServer, sensors, systemBus,
651 sensorsChanged, false);
652 });
653 };
James Feist6714a252018-09-10 15:26:18 -0700654
Zev Weiss214d9712022-08-12 12:54:31 -0700655 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
656 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800657 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500658
659 // Watch for entity-manager to remove configuration interfaces
660 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500661 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
662 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500663 "type='signal',member='InterfacesRemoved',arg0path='" +
664 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500665 [&sensors](sdbusplus::message_t& msg) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400666 interfaceRemoved(msg, sensors);
667 });
Matt Spinler20bf2c12021-09-14 11:07:07 -0500668
669 matches.emplace_back(std::move(ifaceRemovedMatch));
670
James Feist6714a252018-09-10 15:26:18 -0700671 io.run();
672}