blob: c168e1eb731f3511248e28d2dc49265d787dc0c2 [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"
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
28#include <array>
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -070029#include <charconv>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
33#include <memory>
James Feist6714a252018-09-10 15:26:18 -070034#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <stdexcept>
36#include <string>
37#include <utility>
38#include <variant>
39#include <vector>
James Feist6714a252018-09-10 15:26:18 -070040
Jeff Lin87bc67f2020-12-04 20:58:01 +080041static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070042
Zhikui Rencb359da2023-08-03 16:45:44 -070043static constexpr double maxValuePressure = 120000; // Pascals
44static constexpr double minValuePressure = 30000; // Pascals
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050045
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060046static constexpr double maxValueRelativeHumidity = 100; // PercentRH
47static constexpr double minValueRelativeHumidity = 0; // PercentRH
48
Zhikui Rencb359da2023-08-03 16:45:44 -070049static constexpr double maxValueTemperature = 127; // DegreesC
50static constexpr double minValueTemperature = -128; // DegreesC
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050051
James Feistcf3bce62019-01-08 10:07:19 -080052namespace fs = std::filesystem;
Zev Weissd29f8aa2022-08-03 16:24:59 -070053
54static const I2CDeviceTypeMap sensorTypes{
Patrick Rudolph76d36762023-09-20 16:53:38 +020055 {"ADM1021", I2CDeviceType{"adm1021", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070056 {"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 Rudolphf3862ee2023-10-23 14:13:06 +020068 {"MAX6639", I2CDeviceType{"max6639", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070069 {"NCT6779", I2CDeviceType{"nct6779", true}},
70 {"NCT7802", I2CDeviceType{"nct7802", true}},
71 {"SBTSI", I2CDeviceType{"sbtsi", true}},
72 {"SI7020", I2CDeviceType{"si7020", false}},
Tim Lee9b0a6f62022-12-23 14:47:02 +080073 {"TMP100", I2CDeviceType{"tmp100", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070074 {"TMP112", I2CDeviceType{"tmp112", true}},
75 {"TMP175", I2CDeviceType{"tmp175", true}},
76 {"TMP421", I2CDeviceType{"tmp421", true}},
77 {"TMP441", I2CDeviceType{"tmp441", true}},
Hieu Huynh9a7db6a2023-06-19 11:02:07 +000078 {"TMP464", I2CDeviceType{"tmp464", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070079 {"TMP75", I2CDeviceType{"tmp75", true}},
80 {"W83773G", I2CDeviceType{"w83773g", true}},
81};
James Feist6714a252018-09-10 15:26:18 -070082
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050083static struct SensorParams
84 getSensorParameters(const std::filesystem::path& path)
85{
86 // offset is to default to 0 and scale to 1, see lore
87 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
Patrick Williams779c96a2023-05-10 07:50:42 -050088 struct SensorParams tmpSensorParameters = {
89 .minValue = minValueTemperature,
90 .maxValue = maxValueTemperature,
91 .offsetValue = 0.0,
92 .scaleValue = 1.0,
93 .units = sensor_paths::unitDegreesC,
94 .typeName = "temperature"};
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050095
96 // For IIO RAW sensors we get a raw_value, an offset, and scale
97 // to compute the value = (raw_value + offset) * scale
98 // with a _raw IIO device we need to get the
99 // offsetValue and scaleValue from the driver
100 // these are used to compute the reading in
101 // units that have yet to be scaled for D-Bus.
102 const std::string pathStr = path.string();
103 if (pathStr.ends_with("_raw"))
104 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500105 std::string pathOffsetStr = pathStr.substr(0, pathStr.size() - 4) +
106 "_offset";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500107 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
108 // In case there is nothing to read skip this device
109 // This is not an error condition see lore
110 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
111 if (tmpOffsetValue)
112 {
113 tmpSensorParameters.offsetValue = *tmpOffsetValue;
114 }
115
Patrick Williams779c96a2023-05-10 07:50:42 -0500116 std::string pathScaleStr = pathStr.substr(0, pathStr.size() - 4) +
117 "_scale";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500118 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
119 // In case there is nothing to read skip this device
120 // This is not an error condition see lore
121 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
122 if (tmpScaleValue)
123 {
124 tmpSensorParameters.scaleValue = *tmpScaleValue;
125 }
126 }
127
128 // Temperatures are read in milli degrees Celsius, we need
129 // degrees Celsius. Pressures are read in kilopascal, we need
130 // Pascals. On D-Bus for Open BMC we use the International
131 // System of Units without prefixes. Links to the kernel
132 // documentation:
133 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
134 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
135 if (path.filename() == "in_pressure_input" ||
136 path.filename() == "in_pressure_raw")
137 {
138 tmpSensorParameters.minValue = minValuePressure;
139 tmpSensorParameters.maxValue = maxValuePressure;
140 // Pressures are read in kilopascal, we need Pascals.
141 tmpSensorParameters.scaleValue *= 1000.0;
142 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600143 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500144 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600145 else if (path.filename() == "in_humidityrelative_input" ||
146 path.filename() == "in_humidityrelative_raw")
147 {
148 tmpSensorParameters.minValue = minValueRelativeHumidity;
149 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
150 // Relative Humidity are read in milli-percent, we need percent.
151 tmpSensorParameters.scaleValue *= 0.001;
152 tmpSensorParameters.typeName = "humidity";
153 tmpSensorParameters.units = "PercentRH";
154 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500155 else
156 {
157 // Temperatures are read in milli degrees Celsius,
158 // we need degrees Celsius.
159 tmpSensorParameters.scaleValue *= 0.001;
160 }
161
162 return tmpSensorParameters;
163}
164
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530165struct SensorConfigKey
166{
167 uint64_t bus;
168 uint64_t addr;
169 bool operator<(const SensorConfigKey& other) const
170 {
171 if (bus != other.bus)
172 {
173 return bus < other.bus;
174 }
175 return addr < other.addr;
176 }
177};
178
179struct SensorConfig
180{
181 std::string sensorPath;
182 SensorData sensorData;
183 std::string interface;
184 SensorBaseConfigMap config;
185 std::vector<std::string> name;
186};
187
188using SensorConfigMap =
189 boost::container::flat_map<SensorConfigKey, SensorConfig>;
190
191static SensorConfigMap
192 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
193{
194 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700195 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530196 {
Zev Weissa1808332022-08-12 18:21:01 -0700197 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530198 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530199 auto busCfg = cfg.find("Bus");
200 auto addrCfg = cfg.find("Address");
201 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
202 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530203 continue;
204 }
205
Ed Tanous2049bd22022-07-09 07:20:26 -0700206 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
207 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530208 {
Zev Weissa1808332022-08-12 18:21:01 -0700209 std::cerr << path.str << " Bus or Address invalid\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530210 continue;
211 }
212
213 std::vector<std::string> hwmonNames;
214 auto nameCfg = cfg.find("Name");
215 if (nameCfg != cfg.end())
216 {
217 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
218 size_t i = 1;
219 while (true)
220 {
221 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
222 if (sensorNameCfg == cfg.end())
223 {
224 break;
225 }
226 hwmonNames.push_back(
227 std::get<std::string>(sensorNameCfg->second));
228 i++;
229 }
230 }
231
232 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
233 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700234 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530235
236 auto [it, inserted] = configMap.emplace(key, std::move(val));
237 if (!inserted)
238 {
Zev Weissa1808332022-08-12 18:21:01 -0700239 std::cerr << path.str << ": ignoring duplicate entry for {"
240 << key.bus << ", 0x" << std::hex << key.addr
241 << std::dec << "}\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530242 }
243 }
244 }
245 return configMap;
246}
247
James Feist6714a252018-09-10 15:26:18 -0700248void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800249 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800250 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700251 sensors,
252 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700253 const std::shared_ptr<boost::container::flat_set<std::string>>&
Zev Weissa1456c42022-07-18 16:59:35 -0700254 sensorsChanged,
255 bool activateOnly)
James Feist6714a252018-09-10 15:26:18 -0700256{
James Feistdf515152019-09-18 16:40:40 -0700257 auto getter = std::make_shared<GetSensorConfiguration>(
258 dbusConnection,
Zev Weissa1456c42022-07-18 16:59:35 -0700259 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
260 activateOnly](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700261 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700262
Ed Tanousbb679322022-05-16 16:10:00 -0700263 SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530264
Matt Simmeringc564eda2023-05-12 13:04:43 -0700265 auto devices = instantiateDevices(sensorConfigurations, sensors,
266 sensorTypes);
Zev Weissa1456c42022-07-18 16:59:35 -0700267
Ed Tanousbb679322022-05-16 16:10:00 -0700268 // IIO _raw devices look like this on sysfs:
269 // /sys/bus/iio/devices/iio:device0/in_temp_raw
270 // /sys/bus/iio/devices/iio:device0/in_temp_offset
271 // /sys/bus/iio/devices/iio:device0/in_temp_scale
272 //
273 // Other IIO devices look like this on sysfs:
274 // /sys/bus/iio/devices/iio:device1/in_temp_input
275 // /sys/bus/iio/devices/iio:device1/in_pressure_input
276 std::vector<fs::path> paths;
277 fs::path root("/sys/bus/iio/devices");
278 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
279 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
280 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
281 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500282
Ed Tanousbb679322022-05-16 16:10:00 -0700283 // iterate through all found temp and pressure sensors,
284 // and try to match them with configuration
285 for (auto& path : paths)
286 {
287 std::smatch match;
288 const std::string pathStr = path.string();
289 auto directory = path.parent_path();
290 fs::path device;
291
292 std::string deviceName;
293 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700294 {
Ed Tanousbb679322022-05-16 16:10:00 -0700295 device = fs::canonical(directory);
296 deviceName = device.parent_path().stem();
297 }
298 else
299 {
300 device = directory / "device";
301 deviceName = fs::canonical(device).stem();
302 }
Ed Tanousbb679322022-05-16 16:10:00 -0700303
Akshit Shah03d333e2023-08-23 22:14:28 +0000304 size_t bus = 0;
305 size_t addr = 0;
306 if (!getDeviceBusAddr(deviceName, bus, addr))
Ed Tanousbb679322022-05-16 16:10:00 -0700307 {
308 continue;
James Feistdf515152019-09-18 16:40:40 -0700309 }
310
Ed Tanousbb679322022-05-16 16:10:00 -0700311 auto thisSensorParameters = getSensorParameters(path);
312 auto findSensorCfg = configMap.find({bus, addr});
313 if (findSensorCfg == configMap.end())
James Feistdf515152019-09-18 16:40:40 -0700314 {
Ed Tanousbb679322022-05-16 16:10:00 -0700315 continue;
316 }
James Feistdf515152019-09-18 16:40:40 -0700317
Ed Tanousbb679322022-05-16 16:10:00 -0700318 const std::string& interfacePath = findSensorCfg->second.sensorPath;
Zev Weiss7627c862022-11-17 14:23:04 -0800319 auto findI2CDev = devices.find(interfacePath);
320
Zev Weissa1456c42022-07-18 16:59:35 -0700321 std::shared_ptr<I2CDevice> i2cDev;
Zev Weiss7627c862022-11-17 14:23:04 -0800322 if (findI2CDev != devices.end())
Zev Weissa1456c42022-07-18 16:59:35 -0700323 {
Joseph Fu9bbeff72022-12-05 19:09:16 +0800324 // If we're only looking to activate newly-instantiated i2c
325 // devices and this sensor's underlying device was already there
326 // before this call, there's nothing more to do here.
327 if (activateOnly && !findI2CDev->second.second)
328 {
329 continue;
330 }
Zev Weiss7627c862022-11-17 14:23:04 -0800331 i2cDev = findI2CDev->second.first;
Zev Weissa1456c42022-07-18 16:59:35 -0700332 }
333
Ed Tanousbb679322022-05-16 16:10:00 -0700334 const SensorData& sensorData = findSensorCfg->second.sensorData;
Matt Spinler0489ec22023-06-05 15:08:59 -0500335 std::string sensorType = findSensorCfg->second.interface;
336 auto pos = sensorType.find_last_of('.');
337 if (pos != std::string::npos)
338 {
339 sensorType = sensorType.substr(pos + 1);
340 }
Ed Tanousbb679322022-05-16 16:10:00 -0700341 const SensorBaseConfigMap& baseConfigMap =
342 findSensorCfg->second.config;
343 std::vector<std::string>& hwmonName = findSensorCfg->second.name;
344
345 // Temperature has "Name", pressure has "Name1"
346 auto findSensorName = baseConfigMap.find("Name");
347 int index = 1;
348 if (thisSensorParameters.typeName == "pressure" ||
349 thisSensorParameters.typeName == "humidity")
350 {
351 findSensorName = baseConfigMap.find("Name1");
352 index = 2;
353 }
354
355 if (findSensorName == baseConfigMap.end())
356 {
357 std::cerr << "could not determine configuration name for "
358 << deviceName << "\n";
359 continue;
360 }
361 std::string sensorName =
362 std::get<std::string>(findSensorName->second);
363 // on rescans, only update sensors we were signaled by
364 auto findSensor = sensors.find(sensorName);
365 if (!firstScan && findSensor != sensors.end())
366 {
367 bool found = false;
368 auto it = sensorsChanged->begin();
369 while (it != sensorsChanged->end())
370 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700371 if (it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700372 {
373 it = sensorsChanged->erase(it);
374 findSensor->second = nullptr;
375 found = true;
376 break;
377 }
378 ++it;
379 }
380 if (!found)
381 {
382 continue;
383 }
384 }
385
386 std::vector<thresholds::Threshold> sensorThresholds;
387
388 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
389 nullptr, &index))
390 {
391 std::cerr << "error populating thresholds for " << sensorName
392 << " index " << index << "\n";
393 }
394
Zev Weiss8569bf22022-10-11 15:37:44 -0700395 float pollRate = getPollRate(baseConfigMap, pollRateDefault);
Zev Weissa4d27682022-07-19 15:30:36 -0700396 PowerState readState = getPowerState(baseConfigMap);
Ed Tanousbb679322022-05-16 16:10:00 -0700397
398 auto permitSet = getPermitSet(baseConfigMap);
399 auto& sensor = sensors[sensorName];
Zev Weissa1456c42022-07-18 16:59:35 -0700400 if (!activateOnly)
401 {
402 sensor = nullptr;
403 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500404 auto hwmonFile = getFullHwmonFilePath(directory.string(), "temp1",
405 permitSet);
Ed Tanousbb679322022-05-16 16:10:00 -0700406 if (pathStr.starts_with("/sys/bus/iio/devices"))
407 {
408 hwmonFile = pathStr;
409 }
410 if (hwmonFile)
411 {
Zev Weissa1456c42022-07-18 16:59:35 -0700412 if (sensor != nullptr)
413 {
414 sensor->activate(*hwmonFile, i2cDev);
415 }
416 else
417 {
418 sensor = std::make_shared<HwmonTempSensor>(
419 *hwmonFile, sensorType, objectServer, dbusConnection,
420 io, sensorName, std::move(sensorThresholds),
421 thisSensorParameters, pollRate, interfacePath,
422 readState, i2cDev);
423 sensor->setupRead();
424 }
Ed Tanousbb679322022-05-16 16:10:00 -0700425 }
426 hwmonName.erase(
427 remove(hwmonName.begin(), hwmonName.end(), sensorName),
428 hwmonName.end());
429
430 // Looking for keys like "Name1" for temp2_input,
431 // "Name2" for temp3_input, etc.
432 int i = 0;
433 while (true)
434 {
435 ++i;
436 auto findKey = baseConfigMap.find("Name" + std::to_string(i));
437 if (findKey == baseConfigMap.end())
438 {
439 break;
440 }
441 std::string sensorName = std::get<std::string>(findKey->second);
442 hwmonFile = getFullHwmonFilePath(directory.string(),
443 "temp" + std::to_string(i + 1),
444 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500445 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700446 {
James Feistdf515152019-09-18 16:40:40 -0700447 continue;
448 }
Jason Ling100c20b2020-08-11 14:50:33 -0700449 if (hwmonFile)
450 {
Ed Tanousbb679322022-05-16 16:10:00 -0700451 // To look up thresholds for these additional sensors,
452 // match on the Index property in the threshold data
453 // where the index comes from the sysfs file we're on,
454 // i.e. index = 2 for temp2_input.
455 int index = i + 1;
456 std::vector<thresholds::Threshold> thresholds;
457
458 if (!parseThresholdsFromConfig(sensorData, thresholds,
459 nullptr, &index))
460 {
461 std::cerr << "error populating thresholds for "
462 << sensorName << " index " << index << "\n";
463 }
464
465 auto& sensor = sensors[sensorName];
Zev Weissa1456c42022-07-18 16:59:35 -0700466 if (!activateOnly)
467 {
468 sensor = nullptr;
469 }
470
471 if (sensor != nullptr)
472 {
473 sensor->activate(*hwmonFile, i2cDev);
474 }
475 else
476 {
477 sensor = std::make_shared<HwmonTempSensor>(
478 *hwmonFile, sensorType, objectServer,
479 dbusConnection, io, sensorName,
480 std::move(thresholds), thisSensorParameters,
481 pollRate, interfacePath, readState, i2cDev);
482 sensor->setupRead();
483 }
Jason Ling100c20b2020-08-11 14:50:33 -0700484 }
Ed Tanousbb679322022-05-16 16:10:00 -0700485
Willy Tu9eb0cc32022-04-06 11:03:32 -0700486 hwmonName.erase(
487 remove(hwmonName.begin(), hwmonName.end(), sensorName),
488 hwmonName.end());
James Feistdf515152019-09-18 16:40:40 -0700489 }
Ed Tanousbb679322022-05-16 16:10:00 -0700490 if (hwmonName.empty())
491 {
492 configMap.erase(findSensorCfg);
493 }
494 }
Patrick Williams597e8422023-10-20 11:19:01 -0500495 });
Zev Weissd29f8aa2022-08-03 16:24:59 -0700496 std::vector<std::string> types(sensorTypes.size());
497 for (const auto& [type, dt] : sensorTypes)
498 {
499 types.push_back(type);
500 }
501 getter->getConfiguration(types);
James Feist6714a252018-09-10 15:26:18 -0700502}
503
Matt Spinler20bf2c12021-09-14 11:07:07 -0500504void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500505 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500506 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
507 sensors)
508{
509 if (message.is_method_error())
510 {
511 std::cerr << "interfacesRemoved callback method error\n";
512 return;
513 }
514
515 sdbusplus::message::object_path path;
516 std::vector<std::string> interfaces;
517
518 message.read(path, interfaces);
519
520 // If the xyz.openbmc_project.Confguration.X interface was removed
521 // for one or more sensors, delete those sensor objects.
522 auto sensorIt = sensors.begin();
523 while (sensorIt != sensors.end())
524 {
Patrick Rudolph6289f5a2023-09-22 11:32:54 +0200525 if (sensorIt->second && (sensorIt->second->configurationPath == path) &&
Matt Spinler20bf2c12021-09-14 11:07:07 -0500526 (std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500527 sensorIt->second->configInterface) != interfaces.end()))
Matt Spinler20bf2c12021-09-14 11:07:07 -0500528 {
529 sensorIt = sensors.erase(sensorIt);
530 }
531 else
532 {
533 sensorIt++;
534 }
535 }
536}
537
Zev Weissa1456c42022-07-18 16:59:35 -0700538static void powerStateChanged(
539 PowerState type, bool newState,
540 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
541 sensors,
Ed Tanous1f978632023-02-28 18:16:39 -0800542 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zev Weissa1456c42022-07-18 16:59:35 -0700543 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
544{
545 if (newState)
546 {
547 createSensors(io, objectServer, sensors, dbusConnection, nullptr, true);
548 }
549 else
550 {
551 for (auto& [path, sensor] : sensors)
552 {
553 if (sensor != nullptr && sensor->readState == type)
554 {
555 sensor->deactivate();
556 }
557 }
558 }
559}
560
James Feistb6c0b912019-07-09 12:21:44 -0700561int main()
James Feist6714a252018-09-10 15:26:18 -0700562{
Ed Tanous1f978632023-02-28 18:16:39 -0800563 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700564 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700565 sdbusplus::asio::object_server objectServer(systemBus, true);
566 objectServer.add_manager("/xyz/openbmc_project/sensors");
James Feist6714a252018-09-10 15:26:18 -0700567 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700568
Yong Lif3fd1912020-03-25 21:35:23 +0800569 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700570 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700571 auto sensorsChanged =
572 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700573
Zev Weissa1456c42022-07-18 16:59:35 -0700574 auto powerCallBack = [&sensors, &io, &objectServer,
575 &systemBus](PowerState type, bool state) {
576 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
577 };
578 setupPowerMatchCallback(systemBus, powerCallBack);
579
Ed Tanous83db50c2023-03-01 10:20:24 -0800580 boost::asio::post(io, [&]() {
Zev Weissa1456c42022-07-18 16:59:35 -0700581 createSensors(io, objectServer, sensors, systemBus, nullptr, false);
James Feist6714a252018-09-10 15:26:18 -0700582 });
583
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700584 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500585 std::function<void(sdbusplus::message_t&)> eventHandler =
586 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700587 if (message.is_method_error())
588 {
589 std::cerr << "callback method error\n";
590 return;
591 }
592 sensorsChanged->insert(message.get_path());
593 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800594 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700595
596 filterTimer.async_wait([&](const boost::system::error_code& ec) {
597 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700598 {
Ed Tanousbb679322022-05-16 16:10:00 -0700599 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700600 return;
601 }
Ed Tanousbb679322022-05-16 16:10:00 -0700602 if (ec)
603 {
604 std::cerr << "timer error\n";
605 return;
606 }
Zev Weissa1456c42022-07-18 16:59:35 -0700607 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
608 false);
Ed Tanousbb679322022-05-16 16:10:00 -0700609 });
610 };
James Feist6714a252018-09-10 15:26:18 -0700611
Zev Weiss214d9712022-08-12 12:54:31 -0700612 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
613 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800614 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500615
616 // Watch for entity-manager to remove configuration interfaces
617 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500618 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
619 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500620 "type='signal',member='InterfacesRemoved',arg0path='" +
621 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500622 [&sensors](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700623 interfaceRemoved(msg, sensors);
Patrick Williams597e8422023-10-20 11:19:01 -0500624 });
Matt Spinler20bf2c12021-09-14 11:07:07 -0500625
626 matches.emplace_back(std::move(ifaceRemovedMatch));
627
James Feist6714a252018-09-10 15:26:18 -0700628 io.run();
629}