blob: 47f2f0764e00db31ba72be6a60d6c5514eadbdd9 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <HwmonTempSensor.hpp>
18#include <Utils.hpp>
James Feist6714a252018-09-10 15:26:18 -070019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
27#include <array>
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -070028#include <charconv>
James Feist24f02f22019-04-15 11:05:39 -070029#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070030#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <functional>
32#include <memory>
James Feist6714a252018-09-10 15:26:18 -070033#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <stdexcept>
35#include <string>
36#include <utility>
37#include <variant>
38#include <vector>
James Feist6714a252018-09-10 15:26:18 -070039
Jeff Lin87bc67f2020-12-04 20:58:01 +080040static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070041
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050042static constexpr double maxValuePressure = 120000; // Pascals
43static constexpr double minValuePressure = 30000; // Pascals
44
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060045static constexpr double maxValueRelativeHumidity = 100; // PercentRH
46static constexpr double minValueRelativeHumidity = 0; // PercentRH
47
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050048static constexpr double maxValueTemperature = 127; // DegreesC
49static constexpr double minValueTemperature = -128; // DegreesC
50
James Feistcf3bce62019-01-08 10:07:19 -080051namespace fs = std::filesystem;
Brandon Kim66558232021-11-09 16:53:08 -080052static auto sensorTypes{
Potin Lai47863342021-12-14 18:58:12 +080053 std::to_array<const char*>({"xyz.openbmc_project.Configuration.DPS310",
54 "xyz.openbmc_project.Configuration.EMC1412",
Brandon Kim66558232021-11-09 16:53:08 -080055 "xyz.openbmc_project.Configuration.EMC1413",
56 "xyz.openbmc_project.Configuration.EMC1414",
Potin Lai47863342021-12-14 18:58:12 +080057 "xyz.openbmc_project.Configuration.HDC1080",
58 "xyz.openbmc_project.Configuration.JC42",
59 "xyz.openbmc_project.Configuration.LM75A",
60 "xyz.openbmc_project.Configuration.LM95234",
Brandon Kim66558232021-11-09 16:53:08 -080061 "xyz.openbmc_project.Configuration.MAX31725",
62 "xyz.openbmc_project.Configuration.MAX31730",
63 "xyz.openbmc_project.Configuration.MAX6581",
64 "xyz.openbmc_project.Configuration.MAX6654",
65 "xyz.openbmc_project.Configuration.NCT7802",
Zev Weiss7c977302022-05-10 01:57:53 +000066 "xyz.openbmc_project.Configuration.NCT6779",
Brandon Kim66558232021-11-09 16:53:08 -080067 "xyz.openbmc_project.Configuration.SBTSI",
Potin Lai47863342021-12-14 18:58:12 +080068 "xyz.openbmc_project.Configuration.SI7020",
Brandon Kim66558232021-11-09 16:53:08 -080069 "xyz.openbmc_project.Configuration.TMP112",
70 "xyz.openbmc_project.Configuration.TMP175",
71 "xyz.openbmc_project.Configuration.TMP421",
72 "xyz.openbmc_project.Configuration.TMP441",
Brandon Kim66558232021-11-09 16:53:08 -080073 "xyz.openbmc_project.Configuration.TMP75",
Potin Lai47863342021-12-14 18:58:12 +080074 "xyz.openbmc_project.Configuration.W83773G"})};
James Feist6714a252018-09-10 15:26:18 -070075
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050076static struct SensorParams
77 getSensorParameters(const std::filesystem::path& path)
78{
79 // offset is to default to 0 and scale to 1, see lore
80 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
81 struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature,
82 .maxValue = maxValueTemperature,
83 .offsetValue = 0.0,
84 .scaleValue = 1.0,
Bruce Mitchell5a86e562021-12-10 12:26:22 -060085 .units =
86 sensor_paths::unitDegreesC,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050087 .typeName = "temperature"};
88
89 // For IIO RAW sensors we get a raw_value, an offset, and scale
90 // to compute the value = (raw_value + offset) * scale
91 // with a _raw IIO device we need to get the
92 // offsetValue and scaleValue from the driver
93 // these are used to compute the reading in
94 // units that have yet to be scaled for D-Bus.
95 const std::string pathStr = path.string();
96 if (pathStr.ends_with("_raw"))
97 {
98 std::string pathOffsetStr =
99 pathStr.substr(0, pathStr.size() - 4) + "_offset";
100 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
101 // In case there is nothing to read skip this device
102 // This is not an error condition see lore
103 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
104 if (tmpOffsetValue)
105 {
106 tmpSensorParameters.offsetValue = *tmpOffsetValue;
107 }
108
109 std::string pathScaleStr =
110 pathStr.substr(0, pathStr.size() - 4) + "_scale";
111 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
112 // In case there is nothing to read skip this device
113 // This is not an error condition see lore
114 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
115 if (tmpScaleValue)
116 {
117 tmpSensorParameters.scaleValue = *tmpScaleValue;
118 }
119 }
120
121 // Temperatures are read in milli degrees Celsius, we need
122 // degrees Celsius. Pressures are read in kilopascal, we need
123 // Pascals. On D-Bus for Open BMC we use the International
124 // System of Units without prefixes. Links to the kernel
125 // documentation:
126 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
127 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
128 if (path.filename() == "in_pressure_input" ||
129 path.filename() == "in_pressure_raw")
130 {
131 tmpSensorParameters.minValue = minValuePressure;
132 tmpSensorParameters.maxValue = maxValuePressure;
133 // Pressures are read in kilopascal, we need Pascals.
134 tmpSensorParameters.scaleValue *= 1000.0;
135 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600136 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500137 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600138 else if (path.filename() == "in_humidityrelative_input" ||
139 path.filename() == "in_humidityrelative_raw")
140 {
141 tmpSensorParameters.minValue = minValueRelativeHumidity;
142 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
143 // Relative Humidity are read in milli-percent, we need percent.
144 tmpSensorParameters.scaleValue *= 0.001;
145 tmpSensorParameters.typeName = "humidity";
146 tmpSensorParameters.units = "PercentRH";
147 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500148 else
149 {
150 // Temperatures are read in milli degrees Celsius,
151 // we need degrees Celsius.
152 tmpSensorParameters.scaleValue *= 0.001;
153 }
154
155 return tmpSensorParameters;
156}
157
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530158struct SensorConfigKey
159{
160 uint64_t bus;
161 uint64_t addr;
162 bool operator<(const SensorConfigKey& other) const
163 {
164 if (bus != other.bus)
165 {
166 return bus < other.bus;
167 }
168 return addr < other.addr;
169 }
170};
171
172struct SensorConfig
173{
174 std::string sensorPath;
175 SensorData sensorData;
176 std::string interface;
177 SensorBaseConfigMap config;
178 std::vector<std::string> name;
179};
180
181using SensorConfigMap =
182 boost::container::flat_map<SensorConfigKey, SensorConfig>;
183
184static SensorConfigMap
185 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
186{
187 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700188 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530189 {
Zev Weissa1808332022-08-12 18:21:01 -0700190 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530191 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530192 auto busCfg = cfg.find("Bus");
193 auto addrCfg = cfg.find("Address");
194 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
195 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530196 continue;
197 }
198
Ed Tanous2049bd22022-07-09 07:20:26 -0700199 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
200 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530201 {
Zev Weissa1808332022-08-12 18:21:01 -0700202 std::cerr << path.str << " Bus or Address invalid\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530203 continue;
204 }
205
206 std::vector<std::string> hwmonNames;
207 auto nameCfg = cfg.find("Name");
208 if (nameCfg != cfg.end())
209 {
210 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
211 size_t i = 1;
212 while (true)
213 {
214 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
215 if (sensorNameCfg == cfg.end())
216 {
217 break;
218 }
219 hwmonNames.push_back(
220 std::get<std::string>(sensorNameCfg->second));
221 i++;
222 }
223 }
224
225 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
226 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700227 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530228
229 auto [it, inserted] = configMap.emplace(key, std::move(val));
230 if (!inserted)
231 {
Zev Weissa1808332022-08-12 18:21:01 -0700232 std::cerr << path.str << ": ignoring duplicate entry for {"
233 << key.bus << ", 0x" << std::hex << key.addr
234 << std::dec << "}\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530235 }
236 }
237 }
238 return configMap;
239}
240
James Feist6714a252018-09-10 15:26:18 -0700241void createSensors(
242 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800243 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700244 sensors,
245 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700246 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -0700247 sensorsChanged)
248{
James Feistdf515152019-09-18 16:40:40 -0700249 auto getter = std::make_shared<GetSensorConfiguration>(
250 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700251 [&io, &objectServer, &sensors, &dbusConnection,
252 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700253 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700254
Ed Tanousbb679322022-05-16 16:10:00 -0700255 SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530256
Ed Tanousbb679322022-05-16 16:10:00 -0700257 // IIO _raw devices look like this on sysfs:
258 // /sys/bus/iio/devices/iio:device0/in_temp_raw
259 // /sys/bus/iio/devices/iio:device0/in_temp_offset
260 // /sys/bus/iio/devices/iio:device0/in_temp_scale
261 //
262 // Other IIO devices look like this on sysfs:
263 // /sys/bus/iio/devices/iio:device1/in_temp_input
264 // /sys/bus/iio/devices/iio:device1/in_pressure_input
265 std::vector<fs::path> paths;
266 fs::path root("/sys/bus/iio/devices");
267 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
268 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
269 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
270 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500271
Ed Tanousbb679322022-05-16 16:10:00 -0700272 if (paths.empty())
273 {
274 return;
275 }
276
277 // iterate through all found temp and pressure sensors,
278 // and try to match them with configuration
279 for (auto& path : paths)
280 {
281 std::smatch match;
282 const std::string pathStr = path.string();
283 auto directory = path.parent_path();
284 fs::path device;
285
286 std::string deviceName;
287 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700288 {
Ed Tanousbb679322022-05-16 16:10:00 -0700289 device = fs::canonical(directory);
290 deviceName = device.parent_path().stem();
291 }
292 else
293 {
294 device = directory / "device";
295 deviceName = fs::canonical(device).stem();
296 }
297 auto findHyphen = deviceName.find('-');
298 if (findHyphen == std::string::npos)
299 {
300 std::cerr << "found bad device " << deviceName << "\n";
301 continue;
302 }
303 std::string busStr = deviceName.substr(0, findHyphen);
304 std::string addrStr = deviceName.substr(findHyphen + 1);
305
306 uint64_t bus = 0;
307 uint64_t addr = 0;
Ed Tanous2049bd22022-07-09 07:20:26 -0700308 std::from_chars_result res{};
Ed Tanousbb679322022-05-16 16:10:00 -0700309 res = std::from_chars(busStr.data(), busStr.data() + busStr.size(),
310 bus);
311 if (res.ec != std::errc{})
312 {
313 continue;
314 }
315 res = std::from_chars(addrStr.data(),
316 addrStr.data() + addrStr.size(), addr, 16);
317 if (res.ec != std::errc{})
318 {
319 continue;
James Feistdf515152019-09-18 16:40:40 -0700320 }
321
Ed Tanousbb679322022-05-16 16:10:00 -0700322 auto thisSensorParameters = getSensorParameters(path);
323 auto findSensorCfg = configMap.find({bus, addr});
324 if (findSensorCfg == configMap.end())
James Feistdf515152019-09-18 16:40:40 -0700325 {
Ed Tanousbb679322022-05-16 16:10:00 -0700326 continue;
327 }
James Feistdf515152019-09-18 16:40:40 -0700328
Ed Tanousbb679322022-05-16 16:10:00 -0700329 const std::string& interfacePath = findSensorCfg->second.sensorPath;
330 const SensorData& sensorData = findSensorCfg->second.sensorData;
331 const std::string& sensorType = findSensorCfg->second.interface;
332 const SensorBaseConfigMap& baseConfigMap =
333 findSensorCfg->second.config;
334 std::vector<std::string>& hwmonName = findSensorCfg->second.name;
335
336 // Temperature has "Name", pressure has "Name1"
337 auto findSensorName = baseConfigMap.find("Name");
338 int index = 1;
339 if (thisSensorParameters.typeName == "pressure" ||
340 thisSensorParameters.typeName == "humidity")
341 {
342 findSensorName = baseConfigMap.find("Name1");
343 index = 2;
344 }
345
346 if (findSensorName == baseConfigMap.end())
347 {
348 std::cerr << "could not determine configuration name for "
349 << deviceName << "\n";
350 continue;
351 }
352 std::string sensorName =
353 std::get<std::string>(findSensorName->second);
354 // on rescans, only update sensors we were signaled by
355 auto findSensor = sensors.find(sensorName);
356 if (!firstScan && findSensor != sensors.end())
357 {
358 bool found = false;
359 auto it = sensorsChanged->begin();
360 while (it != sensorsChanged->end())
361 {
362 if (boost::ends_with(*it, findSensor->second->name))
363 {
364 it = sensorsChanged->erase(it);
365 findSensor->second = nullptr;
366 found = true;
367 break;
368 }
369 ++it;
370 }
371 if (!found)
372 {
373 continue;
374 }
375 }
376
377 std::vector<thresholds::Threshold> sensorThresholds;
378
379 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
380 nullptr, &index))
381 {
382 std::cerr << "error populating thresholds for " << sensorName
383 << " index " << index << "\n";
384 }
385
386 auto findPollRate = baseConfigMap.find("PollRate");
387 float pollRate = pollRateDefault;
388 if (findPollRate != baseConfigMap.end())
389 {
390 pollRate =
391 std::visit(VariantToFloatVisitor(), findPollRate->second);
Ed Tanous2049bd22022-07-09 07:20:26 -0700392 if (pollRate <= 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700393 {
394 pollRate = pollRateDefault; // polling time too short
395 }
396 }
397
Zev Weissa4d27682022-07-19 15:30:36 -0700398 PowerState readState = getPowerState(baseConfigMap);
Ed Tanousbb679322022-05-16 16:10:00 -0700399
400 auto permitSet = getPermitSet(baseConfigMap);
401 auto& sensor = sensors[sensorName];
402 sensor = nullptr;
403 auto hwmonFile =
404 getFullHwmonFilePath(directory.string(), "temp1", permitSet);
405 if (pathStr.starts_with("/sys/bus/iio/devices"))
406 {
407 hwmonFile = pathStr;
408 }
409 if (hwmonFile)
410 {
411 sensor = std::make_shared<HwmonTempSensor>(
412 *hwmonFile, sensorType, objectServer, dbusConnection, io,
413 sensorName, std::move(sensorThresholds),
414 thisSensorParameters, pollRate, interfacePath, readState);
415 sensor->setupRead();
416 }
417 hwmonName.erase(
418 remove(hwmonName.begin(), hwmonName.end(), sensorName),
419 hwmonName.end());
420
421 // Looking for keys like "Name1" for temp2_input,
422 // "Name2" for temp3_input, etc.
423 int i = 0;
424 while (true)
425 {
426 ++i;
427 auto findKey = baseConfigMap.find("Name" + std::to_string(i));
428 if (findKey == baseConfigMap.end())
429 {
430 break;
431 }
432 std::string sensorName = std::get<std::string>(findKey->second);
433 hwmonFile = getFullHwmonFilePath(directory.string(),
434 "temp" + std::to_string(i + 1),
435 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500436 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700437 {
James Feistdf515152019-09-18 16:40:40 -0700438 continue;
439 }
Jason Ling100c20b2020-08-11 14:50:33 -0700440 if (hwmonFile)
441 {
Ed Tanousbb679322022-05-16 16:10:00 -0700442 // To look up thresholds for these additional sensors,
443 // match on the Index property in the threshold data
444 // where the index comes from the sysfs file we're on,
445 // i.e. index = 2 for temp2_input.
446 int index = i + 1;
447 std::vector<thresholds::Threshold> thresholds;
448
449 if (!parseThresholdsFromConfig(sensorData, thresholds,
450 nullptr, &index))
451 {
452 std::cerr << "error populating thresholds for "
453 << sensorName << " index " << index << "\n";
454 }
455
456 auto& sensor = sensors[sensorName];
457 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700458 sensor = std::make_shared<HwmonTempSensor>(
459 *hwmonFile, sensorType, objectServer, dbusConnection,
Ed Tanousbb679322022-05-16 16:10:00 -0700460 io, sensorName, std::move(thresholds),
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530461 thisSensorParameters, pollRate, interfacePath,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500462 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700463 sensor->setupRead();
464 }
Ed Tanousbb679322022-05-16 16:10:00 -0700465
Willy Tu9eb0cc32022-04-06 11:03:32 -0700466 hwmonName.erase(
467 remove(hwmonName.begin(), hwmonName.end(), sensorName),
468 hwmonName.end());
James Feistdf515152019-09-18 16:40:40 -0700469 }
Ed Tanousbb679322022-05-16 16:10:00 -0700470 if (hwmonName.empty())
471 {
472 configMap.erase(findSensorCfg);
473 }
474 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700475 });
James Feistdf515152019-09-18 16:40:40 -0700476 getter->getConfiguration(
477 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700478}
479
Matt Spinler20bf2c12021-09-14 11:07:07 -0500480void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500481 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500482 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
483 sensors)
484{
485 if (message.is_method_error())
486 {
487 std::cerr << "interfacesRemoved callback method error\n";
488 return;
489 }
490
491 sdbusplus::message::object_path path;
492 std::vector<std::string> interfaces;
493
494 message.read(path, interfaces);
495
496 // If the xyz.openbmc_project.Confguration.X interface was removed
497 // for one or more sensors, delete those sensor objects.
498 auto sensorIt = sensors.begin();
499 while (sensorIt != sensors.end())
500 {
501 if ((sensorIt->second->configurationPath == path) &&
502 (std::find(interfaces.begin(), interfaces.end(),
503 sensorIt->second->objectType) != interfaces.end()))
504 {
505 sensorIt = sensors.erase(sensorIt);
506 }
507 else
508 {
509 sensorIt++;
510 }
511 }
512}
513
James Feistb6c0b912019-07-09 12:21:44 -0700514int main()
James Feist6714a252018-09-10 15:26:18 -0700515{
516 boost::asio::io_service io;
517 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
518 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
519 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800520 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700521 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700522 auto sensorsChanged =
523 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700524
525 io.post([&]() {
526 createSensors(io, objectServer, sensors, systemBus, nullptr);
527 });
528
529 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500530 std::function<void(sdbusplus::message_t&)> eventHandler =
531 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700532 if (message.is_method_error())
533 {
534 std::cerr << "callback method error\n";
535 return;
536 }
537 sensorsChanged->insert(message.get_path());
538 // this implicitly cancels the timer
539 filterTimer.expires_from_now(boost::posix_time::seconds(1));
540
541 filterTimer.async_wait([&](const boost::system::error_code& ec) {
542 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700543 {
Ed Tanousbb679322022-05-16 16:10:00 -0700544 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700545 return;
546 }
Ed Tanousbb679322022-05-16 16:10:00 -0700547 if (ec)
548 {
549 std::cerr << "timer error\n";
550 return;
551 }
552 createSensors(io, objectServer, sensors, systemBus, sensorsChanged);
553 });
554 };
James Feist6714a252018-09-10 15:26:18 -0700555
Zev Weiss214d9712022-08-12 12:54:31 -0700556 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
557 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800558 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500559
560 // Watch for entity-manager to remove configuration interfaces
561 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500562 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
563 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500564 "type='signal',member='InterfacesRemoved',arg0path='" +
565 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500566 [&sensors](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700567 interfaceRemoved(msg, sensors);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500568 });
569
570 matches.emplace_back(std::move(ifaceRemovedMatch));
571
James Feist6714a252018-09-10 15:26:18 -0700572 io.run();
573}