blob: 7cc758c5ae739f75a87492026e39fa5815cb4548 [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}},
68 {"NCT6779", I2CDeviceType{"nct6779", true}},
69 {"NCT7802", I2CDeviceType{"nct7802", true}},
70 {"SBTSI", I2CDeviceType{"sbtsi", true}},
71 {"SI7020", I2CDeviceType{"si7020", false}},
Tim Lee9b0a6f62022-12-23 14:47:02 +080072 {"TMP100", I2CDeviceType{"tmp100", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070073 {"TMP112", I2CDeviceType{"tmp112", true}},
74 {"TMP175", I2CDeviceType{"tmp175", true}},
75 {"TMP421", I2CDeviceType{"tmp421", true}},
76 {"TMP441", I2CDeviceType{"tmp441", true}},
Hieu Huynh9a7db6a2023-06-19 11:02:07 +000077 {"TMP464", I2CDeviceType{"tmp464", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070078 {"TMP75", I2CDeviceType{"tmp75", true}},
79 {"W83773G", I2CDeviceType{"w83773g", true}},
80};
James Feist6714a252018-09-10 15:26:18 -070081
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050082static struct SensorParams
83 getSensorParameters(const std::filesystem::path& path)
84{
85 // offset is to default to 0 and scale to 1, see lore
86 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
Patrick Williams779c96a2023-05-10 07:50:42 -050087 struct SensorParams tmpSensorParameters = {
88 .minValue = minValueTemperature,
89 .maxValue = maxValueTemperature,
90 .offsetValue = 0.0,
91 .scaleValue = 1.0,
92 .units = sensor_paths::unitDegreesC,
93 .typeName = "temperature"};
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050094
95 // For IIO RAW sensors we get a raw_value, an offset, and scale
96 // to compute the value = (raw_value + offset) * scale
97 // with a _raw IIO device we need to get the
98 // offsetValue and scaleValue from the driver
99 // these are used to compute the reading in
100 // units that have yet to be scaled for D-Bus.
101 const std::string pathStr = path.string();
102 if (pathStr.ends_with("_raw"))
103 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500104 std::string pathOffsetStr = pathStr.substr(0, pathStr.size() - 4) +
105 "_offset";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500106 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
107 // In case there is nothing to read skip this device
108 // This is not an error condition see lore
109 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
110 if (tmpOffsetValue)
111 {
112 tmpSensorParameters.offsetValue = *tmpOffsetValue;
113 }
114
Patrick Williams779c96a2023-05-10 07:50:42 -0500115 std::string pathScaleStr = pathStr.substr(0, pathStr.size() - 4) +
116 "_scale";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500117 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
118 // In case there is nothing to read skip this device
119 // This is not an error condition see lore
120 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
121 if (tmpScaleValue)
122 {
123 tmpSensorParameters.scaleValue = *tmpScaleValue;
124 }
125 }
126
127 // Temperatures are read in milli degrees Celsius, we need
128 // degrees Celsius. Pressures are read in kilopascal, we need
129 // Pascals. On D-Bus for Open BMC we use the International
130 // System of Units without prefixes. Links to the kernel
131 // documentation:
132 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
133 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
134 if (path.filename() == "in_pressure_input" ||
135 path.filename() == "in_pressure_raw")
136 {
137 tmpSensorParameters.minValue = minValuePressure;
138 tmpSensorParameters.maxValue = maxValuePressure;
139 // Pressures are read in kilopascal, we need Pascals.
140 tmpSensorParameters.scaleValue *= 1000.0;
141 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600142 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500143 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600144 else if (path.filename() == "in_humidityrelative_input" ||
145 path.filename() == "in_humidityrelative_raw")
146 {
147 tmpSensorParameters.minValue = minValueRelativeHumidity;
148 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
149 // Relative Humidity are read in milli-percent, we need percent.
150 tmpSensorParameters.scaleValue *= 0.001;
151 tmpSensorParameters.typeName = "humidity";
152 tmpSensorParameters.units = "PercentRH";
153 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500154 else
155 {
156 // Temperatures are read in milli degrees Celsius,
157 // we need degrees Celsius.
158 tmpSensorParameters.scaleValue *= 0.001;
159 }
160
161 return tmpSensorParameters;
162}
163
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530164struct SensorConfigKey
165{
166 uint64_t bus;
167 uint64_t addr;
168 bool operator<(const SensorConfigKey& other) const
169 {
170 if (bus != other.bus)
171 {
172 return bus < other.bus;
173 }
174 return addr < other.addr;
175 }
176};
177
178struct SensorConfig
179{
180 std::string sensorPath;
181 SensorData sensorData;
182 std::string interface;
183 SensorBaseConfigMap config;
184 std::vector<std::string> name;
185};
186
187using SensorConfigMap =
188 boost::container::flat_map<SensorConfigKey, SensorConfig>;
189
190static SensorConfigMap
191 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
192{
193 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700194 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530195 {
Zev Weissa1808332022-08-12 18:21:01 -0700196 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530197 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530198 auto busCfg = cfg.find("Bus");
199 auto addrCfg = cfg.find("Address");
200 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
201 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530202 continue;
203 }
204
Ed Tanous2049bd22022-07-09 07:20:26 -0700205 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
206 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530207 {
Zev Weissa1808332022-08-12 18:21:01 -0700208 std::cerr << path.str << " Bus or Address invalid\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530209 continue;
210 }
211
212 std::vector<std::string> hwmonNames;
213 auto nameCfg = cfg.find("Name");
214 if (nameCfg != cfg.end())
215 {
216 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
217 size_t i = 1;
218 while (true)
219 {
220 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
221 if (sensorNameCfg == cfg.end())
222 {
223 break;
224 }
225 hwmonNames.push_back(
226 std::get<std::string>(sensorNameCfg->second));
227 i++;
228 }
229 }
230
231 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
232 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700233 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530234
235 auto [it, inserted] = configMap.emplace(key, std::move(val));
236 if (!inserted)
237 {
Zev Weissa1808332022-08-12 18:21:01 -0700238 std::cerr << path.str << ": ignoring duplicate entry for {"
239 << key.bus << ", 0x" << std::hex << key.addr
240 << std::dec << "}\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530241 }
242 }
243 }
244 return configMap;
245}
246
James Feist6714a252018-09-10 15:26:18 -0700247void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800248 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800249 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700250 sensors,
251 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700252 const std::shared_ptr<boost::container::flat_set<std::string>>&
Zev Weissa1456c42022-07-18 16:59:35 -0700253 sensorsChanged,
254 bool activateOnly)
James Feist6714a252018-09-10 15:26:18 -0700255{
James Feistdf515152019-09-18 16:40:40 -0700256 auto getter = std::make_shared<GetSensorConfiguration>(
257 dbusConnection,
Zev Weissa1456c42022-07-18 16:59:35 -0700258 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
259 activateOnly](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700260 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700261
Ed Tanousbb679322022-05-16 16:10:00 -0700262 SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530263
Matt Simmeringc564eda2023-05-12 13:04:43 -0700264 auto devices = instantiateDevices(sensorConfigurations, sensors,
265 sensorTypes);
Zev Weissa1456c42022-07-18 16:59:35 -0700266
Ed Tanousbb679322022-05-16 16:10:00 -0700267 // IIO _raw devices look like this on sysfs:
268 // /sys/bus/iio/devices/iio:device0/in_temp_raw
269 // /sys/bus/iio/devices/iio:device0/in_temp_offset
270 // /sys/bus/iio/devices/iio:device0/in_temp_scale
271 //
272 // Other IIO devices look like this on sysfs:
273 // /sys/bus/iio/devices/iio:device1/in_temp_input
274 // /sys/bus/iio/devices/iio:device1/in_pressure_input
275 std::vector<fs::path> paths;
276 fs::path root("/sys/bus/iio/devices");
277 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
278 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
279 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
280 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500281
Ed Tanousbb679322022-05-16 16:10:00 -0700282 // iterate through all found temp and pressure sensors,
283 // and try to match them with configuration
284 for (auto& path : paths)
285 {
286 std::smatch match;
287 const std::string pathStr = path.string();
288 auto directory = path.parent_path();
289 fs::path device;
290
291 std::string deviceName;
292 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700293 {
Ed Tanousbb679322022-05-16 16:10:00 -0700294 device = fs::canonical(directory);
295 deviceName = device.parent_path().stem();
296 }
297 else
298 {
299 device = directory / "device";
300 deviceName = fs::canonical(device).stem();
301 }
Ed Tanousbb679322022-05-16 16:10:00 -0700302
Akshit Shah03d333e2023-08-23 22:14:28 +0000303 size_t bus = 0;
304 size_t addr = 0;
305 if (!getDeviceBusAddr(deviceName, bus, addr))
Ed Tanousbb679322022-05-16 16:10:00 -0700306 {
307 continue;
James Feistdf515152019-09-18 16:40:40 -0700308 }
309
Ed Tanousbb679322022-05-16 16:10:00 -0700310 auto thisSensorParameters = getSensorParameters(path);
311 auto findSensorCfg = configMap.find({bus, addr});
312 if (findSensorCfg == configMap.end())
James Feistdf515152019-09-18 16:40:40 -0700313 {
Ed Tanousbb679322022-05-16 16:10:00 -0700314 continue;
315 }
James Feistdf515152019-09-18 16:40:40 -0700316
Ed Tanousbb679322022-05-16 16:10:00 -0700317 const std::string& interfacePath = findSensorCfg->second.sensorPath;
Zev Weiss7627c862022-11-17 14:23:04 -0800318 auto findI2CDev = devices.find(interfacePath);
319
Zev Weissa1456c42022-07-18 16:59:35 -0700320 std::shared_ptr<I2CDevice> i2cDev;
Zev Weiss7627c862022-11-17 14:23:04 -0800321 if (findI2CDev != devices.end())
Zev Weissa1456c42022-07-18 16:59:35 -0700322 {
Joseph Fu9bbeff72022-12-05 19:09:16 +0800323 // If we're only looking to activate newly-instantiated i2c
324 // devices and this sensor's underlying device was already there
325 // before this call, there's nothing more to do here.
326 if (activateOnly && !findI2CDev->second.second)
327 {
328 continue;
329 }
Zev Weiss7627c862022-11-17 14:23:04 -0800330 i2cDev = findI2CDev->second.first;
Zev Weissa1456c42022-07-18 16:59:35 -0700331 }
332
Ed Tanousbb679322022-05-16 16:10:00 -0700333 const SensorData& sensorData = findSensorCfg->second.sensorData;
Matt Spinler0489ec22023-06-05 15:08:59 -0500334 std::string sensorType = findSensorCfg->second.interface;
335 auto pos = sensorType.find_last_of('.');
336 if (pos != std::string::npos)
337 {
338 sensorType = sensorType.substr(pos + 1);
339 }
Ed Tanousbb679322022-05-16 16:10:00 -0700340 const SensorBaseConfigMap& baseConfigMap =
341 findSensorCfg->second.config;
342 std::vector<std::string>& hwmonName = findSensorCfg->second.name;
343
344 // Temperature has "Name", pressure has "Name1"
345 auto findSensorName = baseConfigMap.find("Name");
346 int index = 1;
347 if (thisSensorParameters.typeName == "pressure" ||
348 thisSensorParameters.typeName == "humidity")
349 {
350 findSensorName = baseConfigMap.find("Name1");
351 index = 2;
352 }
353
354 if (findSensorName == baseConfigMap.end())
355 {
356 std::cerr << "could not determine configuration name for "
357 << deviceName << "\n";
358 continue;
359 }
360 std::string sensorName =
361 std::get<std::string>(findSensorName->second);
362 // on rescans, only update sensors we were signaled by
363 auto findSensor = sensors.find(sensorName);
364 if (!firstScan && findSensor != sensors.end())
365 {
366 bool found = false;
367 auto it = sensorsChanged->begin();
368 while (it != sensorsChanged->end())
369 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700370 if (it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700371 {
372 it = sensorsChanged->erase(it);
373 findSensor->second = nullptr;
374 found = true;
375 break;
376 }
377 ++it;
378 }
379 if (!found)
380 {
381 continue;
382 }
383 }
384
385 std::vector<thresholds::Threshold> sensorThresholds;
386
387 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
388 nullptr, &index))
389 {
390 std::cerr << "error populating thresholds for " << sensorName
391 << " index " << index << "\n";
392 }
393
Zev Weiss8569bf22022-10-11 15:37:44 -0700394 float pollRate = getPollRate(baseConfigMap, pollRateDefault);
Zev Weissa4d27682022-07-19 15:30:36 -0700395 PowerState readState = getPowerState(baseConfigMap);
Ed Tanousbb679322022-05-16 16:10:00 -0700396
397 auto permitSet = getPermitSet(baseConfigMap);
398 auto& sensor = sensors[sensorName];
Zev Weissa1456c42022-07-18 16:59:35 -0700399 if (!activateOnly)
400 {
401 sensor = nullptr;
402 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500403 auto hwmonFile = getFullHwmonFilePath(directory.string(), "temp1",
404 permitSet);
Ed Tanousbb679322022-05-16 16:10:00 -0700405 if (pathStr.starts_with("/sys/bus/iio/devices"))
406 {
407 hwmonFile = pathStr;
408 }
409 if (hwmonFile)
410 {
Zev Weissa1456c42022-07-18 16:59:35 -0700411 if (sensor != nullptr)
412 {
413 sensor->activate(*hwmonFile, i2cDev);
414 }
415 else
416 {
417 sensor = std::make_shared<HwmonTempSensor>(
418 *hwmonFile, sensorType, objectServer, dbusConnection,
419 io, sensorName, std::move(sensorThresholds),
420 thisSensorParameters, pollRate, interfacePath,
421 readState, i2cDev);
422 sensor->setupRead();
423 }
Ed Tanousbb679322022-05-16 16:10:00 -0700424 }
425 hwmonName.erase(
426 remove(hwmonName.begin(), hwmonName.end(), sensorName),
427 hwmonName.end());
428
429 // Looking for keys like "Name1" for temp2_input,
430 // "Name2" for temp3_input, etc.
431 int i = 0;
432 while (true)
433 {
434 ++i;
435 auto findKey = baseConfigMap.find("Name" + std::to_string(i));
436 if (findKey == baseConfigMap.end())
437 {
438 break;
439 }
440 std::string sensorName = std::get<std::string>(findKey->second);
441 hwmonFile = getFullHwmonFilePath(directory.string(),
442 "temp" + std::to_string(i + 1),
443 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500444 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700445 {
James Feistdf515152019-09-18 16:40:40 -0700446 continue;
447 }
Jason Ling100c20b2020-08-11 14:50:33 -0700448 if (hwmonFile)
449 {
Ed Tanousbb679322022-05-16 16:10:00 -0700450 // To look up thresholds for these additional sensors,
451 // match on the Index property in the threshold data
452 // where the index comes from the sysfs file we're on,
453 // i.e. index = 2 for temp2_input.
454 int index = i + 1;
455 std::vector<thresholds::Threshold> thresholds;
456
457 if (!parseThresholdsFromConfig(sensorData, thresholds,
458 nullptr, &index))
459 {
460 std::cerr << "error populating thresholds for "
461 << sensorName << " index " << index << "\n";
462 }
463
464 auto& sensor = sensors[sensorName];
Zev Weissa1456c42022-07-18 16:59:35 -0700465 if (!activateOnly)
466 {
467 sensor = nullptr;
468 }
469
470 if (sensor != nullptr)
471 {
472 sensor->activate(*hwmonFile, i2cDev);
473 }
474 else
475 {
476 sensor = std::make_shared<HwmonTempSensor>(
477 *hwmonFile, sensorType, objectServer,
478 dbusConnection, io, sensorName,
479 std::move(thresholds), thisSensorParameters,
480 pollRate, interfacePath, readState, i2cDev);
481 sensor->setupRead();
482 }
Jason Ling100c20b2020-08-11 14:50:33 -0700483 }
Ed Tanousbb679322022-05-16 16:10:00 -0700484
Willy Tu9eb0cc32022-04-06 11:03:32 -0700485 hwmonName.erase(
486 remove(hwmonName.begin(), hwmonName.end(), sensorName),
487 hwmonName.end());
James Feistdf515152019-09-18 16:40:40 -0700488 }
Ed Tanousbb679322022-05-16 16:10:00 -0700489 if (hwmonName.empty())
490 {
491 configMap.erase(findSensorCfg);
492 }
493 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700494 });
Zev Weissd29f8aa2022-08-03 16:24:59 -0700495 std::vector<std::string> types(sensorTypes.size());
496 for (const auto& [type, dt] : sensorTypes)
497 {
498 types.push_back(type);
499 }
500 getter->getConfiguration(types);
James Feist6714a252018-09-10 15:26:18 -0700501}
502
Matt Spinler20bf2c12021-09-14 11:07:07 -0500503void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500504 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500505 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
506 sensors)
507{
508 if (message.is_method_error())
509 {
510 std::cerr << "interfacesRemoved callback method error\n";
511 return;
512 }
513
514 sdbusplus::message::object_path path;
515 std::vector<std::string> interfaces;
516
517 message.read(path, interfaces);
518
519 // If the xyz.openbmc_project.Confguration.X interface was removed
520 // for one or more sensors, delete those sensor objects.
521 auto sensorIt = sensors.begin();
522 while (sensorIt != sensors.end())
523 {
Patrick Rudolph6289f5a2023-09-22 11:32:54 +0200524 if (sensorIt->second && (sensorIt->second->configurationPath == path) &&
Matt Spinler20bf2c12021-09-14 11:07:07 -0500525 (std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500526 sensorIt->second->configInterface) != interfaces.end()))
Matt Spinler20bf2c12021-09-14 11:07:07 -0500527 {
528 sensorIt = sensors.erase(sensorIt);
529 }
530 else
531 {
532 sensorIt++;
533 }
534 }
535}
536
Zev Weissa1456c42022-07-18 16:59:35 -0700537static void powerStateChanged(
538 PowerState type, bool newState,
539 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
540 sensors,
Ed Tanous1f978632023-02-28 18:16:39 -0800541 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zev Weissa1456c42022-07-18 16:59:35 -0700542 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
543{
544 if (newState)
545 {
546 createSensors(io, objectServer, sensors, dbusConnection, nullptr, true);
547 }
548 else
549 {
550 for (auto& [path, sensor] : sensors)
551 {
552 if (sensor != nullptr && sensor->readState == type)
553 {
554 sensor->deactivate();
555 }
556 }
557 }
558}
559
James Feistb6c0b912019-07-09 12:21:44 -0700560int main()
James Feist6714a252018-09-10 15:26:18 -0700561{
Ed Tanous1f978632023-02-28 18:16:39 -0800562 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700563 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700564 sdbusplus::asio::object_server objectServer(systemBus, true);
565 objectServer.add_manager("/xyz/openbmc_project/sensors");
James Feist6714a252018-09-10 15:26:18 -0700566 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700567
Yong Lif3fd1912020-03-25 21:35:23 +0800568 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700569 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700570 auto sensorsChanged =
571 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700572
Zev Weissa1456c42022-07-18 16:59:35 -0700573 auto powerCallBack = [&sensors, &io, &objectServer,
574 &systemBus](PowerState type, bool state) {
575 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
576 };
577 setupPowerMatchCallback(systemBus, powerCallBack);
578
Ed Tanous83db50c2023-03-01 10:20:24 -0800579 boost::asio::post(io, [&]() {
Zev Weissa1456c42022-07-18 16:59:35 -0700580 createSensors(io, objectServer, sensors, systemBus, nullptr, false);
James Feist6714a252018-09-10 15:26:18 -0700581 });
582
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700583 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500584 std::function<void(sdbusplus::message_t&)> eventHandler =
585 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700586 if (message.is_method_error())
587 {
588 std::cerr << "callback method error\n";
589 return;
590 }
591 sensorsChanged->insert(message.get_path());
592 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800593 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700594
595 filterTimer.async_wait([&](const boost::system::error_code& ec) {
596 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700597 {
Ed Tanousbb679322022-05-16 16:10:00 -0700598 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700599 return;
600 }
Ed Tanousbb679322022-05-16 16:10:00 -0700601 if (ec)
602 {
603 std::cerr << "timer error\n";
604 return;
605 }
Zev Weissa1456c42022-07-18 16:59:35 -0700606 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
607 false);
Ed Tanousbb679322022-05-16 16:10:00 -0700608 });
609 };
James Feist6714a252018-09-10 15:26:18 -0700610
Zev Weiss214d9712022-08-12 12:54:31 -0700611 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
612 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800613 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500614
615 // Watch for entity-manager to remove configuration interfaces
616 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500617 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
618 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500619 "type='signal',member='InterfacesRemoved',arg0path='" +
620 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500621 [&sensors](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700622 interfaceRemoved(msg, sensors);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500623 });
624
625 matches.emplace_back(std::move(ifaceRemovedMatch));
626
James Feist6714a252018-09-10 15:26:18 -0700627 io.run();
628}