blob: 14315086cd8152fdcae342c46a5cd904cb02618c [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/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070020#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070022#include <sdbusplus/asio/connection.hpp>
23#include <sdbusplus/asio/object_server.hpp>
24#include <sdbusplus/bus/match.hpp>
25
26#include <array>
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -070027#include <charconv>
James Feist24f02f22019-04-15 11:05:39 -070028#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <functional>
31#include <memory>
James Feist6714a252018-09-10 15:26:18 -070032#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <stdexcept>
34#include <string>
35#include <utility>
36#include <variant>
37#include <vector>
James Feist6714a252018-09-10 15:26:18 -070038
Jeff Lin87bc67f2020-12-04 20:58:01 +080039static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070040
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050041static constexpr double maxValuePressure = 120000; // Pascals
42static constexpr double minValuePressure = 30000; // Pascals
43
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060044static constexpr double maxValueRelativeHumidity = 100; // PercentRH
45static constexpr double minValueRelativeHumidity = 0; // PercentRH
46
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050047static constexpr double maxValueTemperature = 127; // DegreesC
48static constexpr double minValueTemperature = -128; // DegreesC
49
James Feistcf3bce62019-01-08 10:07:19 -080050namespace fs = std::filesystem;
Brandon Kim66558232021-11-09 16:53:08 -080051static auto sensorTypes{
Potin Lai47863342021-12-14 18:58:12 +080052 std::to_array<const char*>({"xyz.openbmc_project.Configuration.DPS310",
53 "xyz.openbmc_project.Configuration.EMC1412",
Brandon Kim66558232021-11-09 16:53:08 -080054 "xyz.openbmc_project.Configuration.EMC1413",
55 "xyz.openbmc_project.Configuration.EMC1414",
Potin Lai47863342021-12-14 18:58:12 +080056 "xyz.openbmc_project.Configuration.HDC1080",
57 "xyz.openbmc_project.Configuration.JC42",
58 "xyz.openbmc_project.Configuration.LM75A",
59 "xyz.openbmc_project.Configuration.LM95234",
Brandon Kim66558232021-11-09 16:53:08 -080060 "xyz.openbmc_project.Configuration.MAX31725",
61 "xyz.openbmc_project.Configuration.MAX31730",
62 "xyz.openbmc_project.Configuration.MAX6581",
63 "xyz.openbmc_project.Configuration.MAX6654",
64 "xyz.openbmc_project.Configuration.NCT7802",
Zev Weiss7c977302022-05-10 01:57:53 +000065 "xyz.openbmc_project.Configuration.NCT6779",
Brandon Kim66558232021-11-09 16:53:08 -080066 "xyz.openbmc_project.Configuration.SBTSI",
Potin Lai47863342021-12-14 18:58:12 +080067 "xyz.openbmc_project.Configuration.SI7020",
Brandon Kim66558232021-11-09 16:53:08 -080068 "xyz.openbmc_project.Configuration.TMP112",
69 "xyz.openbmc_project.Configuration.TMP175",
70 "xyz.openbmc_project.Configuration.TMP421",
71 "xyz.openbmc_project.Configuration.TMP441",
Brandon Kim66558232021-11-09 16:53:08 -080072 "xyz.openbmc_project.Configuration.TMP75",
Potin Lai47863342021-12-14 18:58:12 +080073 "xyz.openbmc_project.Configuration.W83773G"})};
James Feist6714a252018-09-10 15:26:18 -070074
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050075static struct SensorParams
76 getSensorParameters(const std::filesystem::path& path)
77{
78 // offset is to default to 0 and scale to 1, see lore
79 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
80 struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature,
81 .maxValue = maxValueTemperature,
82 .offsetValue = 0.0,
83 .scaleValue = 1.0,
Bruce Mitchell5a86e562021-12-10 12:26:22 -060084 .units =
85 sensor_paths::unitDegreesC,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050086 .typeName = "temperature"};
87
88 // For IIO RAW sensors we get a raw_value, an offset, and scale
89 // to compute the value = (raw_value + offset) * scale
90 // with a _raw IIO device we need to get the
91 // offsetValue and scaleValue from the driver
92 // these are used to compute the reading in
93 // units that have yet to be scaled for D-Bus.
94 const std::string pathStr = path.string();
95 if (pathStr.ends_with("_raw"))
96 {
97 std::string pathOffsetStr =
98 pathStr.substr(0, pathStr.size() - 4) + "_offset";
99 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
100 // In case there is nothing to read skip this device
101 // This is not an error condition see lore
102 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
103 if (tmpOffsetValue)
104 {
105 tmpSensorParameters.offsetValue = *tmpOffsetValue;
106 }
107
108 std::string pathScaleStr =
109 pathStr.substr(0, pathStr.size() - 4) + "_scale";
110 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
111 // In case there is nothing to read skip this device
112 // This is not an error condition see lore
113 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
114 if (tmpScaleValue)
115 {
116 tmpSensorParameters.scaleValue = *tmpScaleValue;
117 }
118 }
119
120 // Temperatures are read in milli degrees Celsius, we need
121 // degrees Celsius. Pressures are read in kilopascal, we need
122 // Pascals. On D-Bus for Open BMC we use the International
123 // System of Units without prefixes. Links to the kernel
124 // documentation:
125 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
126 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
127 if (path.filename() == "in_pressure_input" ||
128 path.filename() == "in_pressure_raw")
129 {
130 tmpSensorParameters.minValue = minValuePressure;
131 tmpSensorParameters.maxValue = maxValuePressure;
132 // Pressures are read in kilopascal, we need Pascals.
133 tmpSensorParameters.scaleValue *= 1000.0;
134 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600135 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500136 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600137 else if (path.filename() == "in_humidityrelative_input" ||
138 path.filename() == "in_humidityrelative_raw")
139 {
140 tmpSensorParameters.minValue = minValueRelativeHumidity;
141 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
142 // Relative Humidity are read in milli-percent, we need percent.
143 tmpSensorParameters.scaleValue *= 0.001;
144 tmpSensorParameters.typeName = "humidity";
145 tmpSensorParameters.units = "PercentRH";
146 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500147 else
148 {
149 // Temperatures are read in milli degrees Celsius,
150 // we need degrees Celsius.
151 tmpSensorParameters.scaleValue *= 0.001;
152 }
153
154 return tmpSensorParameters;
155}
156
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530157struct SensorConfigKey
158{
159 uint64_t bus;
160 uint64_t addr;
161 bool operator<(const SensorConfigKey& other) const
162 {
163 if (bus != other.bus)
164 {
165 return bus < other.bus;
166 }
167 return addr < other.addr;
168 }
169};
170
171struct SensorConfig
172{
173 std::string sensorPath;
174 SensorData sensorData;
175 std::string interface;
176 SensorBaseConfigMap config;
177 std::vector<std::string> name;
178};
179
180using SensorConfigMap =
181 boost::container::flat_map<SensorConfigKey, SensorConfig>;
182
183static SensorConfigMap
184 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
185{
186 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700187 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530188 {
Zev Weissa1808332022-08-12 18:21:01 -0700189 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530190 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530191 auto busCfg = cfg.find("Bus");
192 auto addrCfg = cfg.find("Address");
193 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
194 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530195 continue;
196 }
197
Ed Tanous2049bd22022-07-09 07:20:26 -0700198 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
199 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530200 {
Zev Weissa1808332022-08-12 18:21:01 -0700201 std::cerr << path.str << " Bus or Address invalid\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530202 continue;
203 }
204
205 std::vector<std::string> hwmonNames;
206 auto nameCfg = cfg.find("Name");
207 if (nameCfg != cfg.end())
208 {
209 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
210 size_t i = 1;
211 while (true)
212 {
213 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
214 if (sensorNameCfg == cfg.end())
215 {
216 break;
217 }
218 hwmonNames.push_back(
219 std::get<std::string>(sensorNameCfg->second));
220 i++;
221 }
222 }
223
224 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
225 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700226 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530227
228 auto [it, inserted] = configMap.emplace(key, std::move(val));
229 if (!inserted)
230 {
Zev Weissa1808332022-08-12 18:21:01 -0700231 std::cerr << path.str << ": ignoring duplicate entry for {"
232 << key.bus << ", 0x" << std::hex << key.addr
233 << std::dec << "}\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530234 }
235 }
236 }
237 return configMap;
238}
239
James Feist6714a252018-09-10 15:26:18 -0700240void createSensors(
241 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800242 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700243 sensors,
244 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700245 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -0700246 sensorsChanged)
247{
James Feistdf515152019-09-18 16:40:40 -0700248 auto getter = std::make_shared<GetSensorConfiguration>(
249 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700250 [&io, &objectServer, &sensors, &dbusConnection,
251 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700252 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700253
Ed Tanousbb679322022-05-16 16:10:00 -0700254 SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530255
Ed Tanousbb679322022-05-16 16:10:00 -0700256 // IIO _raw devices look like this on sysfs:
257 // /sys/bus/iio/devices/iio:device0/in_temp_raw
258 // /sys/bus/iio/devices/iio:device0/in_temp_offset
259 // /sys/bus/iio/devices/iio:device0/in_temp_scale
260 //
261 // Other IIO devices look like this on sysfs:
262 // /sys/bus/iio/devices/iio:device1/in_temp_input
263 // /sys/bus/iio/devices/iio:device1/in_pressure_input
264 std::vector<fs::path> paths;
265 fs::path root("/sys/bus/iio/devices");
266 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
267 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
268 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
269 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500270
Ed Tanousbb679322022-05-16 16:10:00 -0700271 if (paths.empty())
272 {
273 return;
274 }
275
276 // iterate through all found temp and pressure sensors,
277 // and try to match them with configuration
278 for (auto& path : paths)
279 {
280 std::smatch match;
281 const std::string pathStr = path.string();
282 auto directory = path.parent_path();
283 fs::path device;
284
285 std::string deviceName;
286 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700287 {
Ed Tanousbb679322022-05-16 16:10:00 -0700288 device = fs::canonical(directory);
289 deviceName = device.parent_path().stem();
290 }
291 else
292 {
293 device = directory / "device";
294 deviceName = fs::canonical(device).stem();
295 }
296 auto findHyphen = deviceName.find('-');
297 if (findHyphen == std::string::npos)
298 {
299 std::cerr << "found bad device " << deviceName << "\n";
300 continue;
301 }
302 std::string busStr = deviceName.substr(0, findHyphen);
303 std::string addrStr = deviceName.substr(findHyphen + 1);
304
305 uint64_t bus = 0;
306 uint64_t addr = 0;
Ed Tanous2049bd22022-07-09 07:20:26 -0700307 std::from_chars_result res{};
Ed Tanousbb679322022-05-16 16:10:00 -0700308 res = std::from_chars(busStr.data(), busStr.data() + busStr.size(),
309 bus);
310 if (res.ec != std::errc{})
311 {
312 continue;
313 }
314 res = std::from_chars(addrStr.data(),
315 addrStr.data() + addrStr.size(), addr, 16);
316 if (res.ec != std::errc{})
317 {
318 continue;
James Feistdf515152019-09-18 16:40:40 -0700319 }
320
Ed Tanousbb679322022-05-16 16:10:00 -0700321 auto thisSensorParameters = getSensorParameters(path);
322 auto findSensorCfg = configMap.find({bus, addr});
323 if (findSensorCfg == configMap.end())
James Feistdf515152019-09-18 16:40:40 -0700324 {
Ed Tanousbb679322022-05-16 16:10:00 -0700325 continue;
326 }
James Feistdf515152019-09-18 16:40:40 -0700327
Ed Tanousbb679322022-05-16 16:10:00 -0700328 const std::string& interfacePath = findSensorCfg->second.sensorPath;
329 const SensorData& sensorData = findSensorCfg->second.sensorData;
330 const std::string& sensorType = findSensorCfg->second.interface;
331 const SensorBaseConfigMap& baseConfigMap =
332 findSensorCfg->second.config;
333 std::vector<std::string>& hwmonName = findSensorCfg->second.name;
334
335 // Temperature has "Name", pressure has "Name1"
336 auto findSensorName = baseConfigMap.find("Name");
337 int index = 1;
338 if (thisSensorParameters.typeName == "pressure" ||
339 thisSensorParameters.typeName == "humidity")
340 {
341 findSensorName = baseConfigMap.find("Name1");
342 index = 2;
343 }
344
345 if (findSensorName == baseConfigMap.end())
346 {
347 std::cerr << "could not determine configuration name for "
348 << deviceName << "\n";
349 continue;
350 }
351 std::string sensorName =
352 std::get<std::string>(findSensorName->second);
353 // on rescans, only update sensors we were signaled by
354 auto findSensor = sensors.find(sensorName);
355 if (!firstScan && findSensor != sensors.end())
356 {
357 bool found = false;
358 auto it = sensorsChanged->begin();
359 while (it != sensorsChanged->end())
360 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700361 if (it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700362 {
363 it = sensorsChanged->erase(it);
364 findSensor->second = nullptr;
365 found = true;
366 break;
367 }
368 ++it;
369 }
370 if (!found)
371 {
372 continue;
373 }
374 }
375
376 std::vector<thresholds::Threshold> sensorThresholds;
377
378 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
379 nullptr, &index))
380 {
381 std::cerr << "error populating thresholds for " << sensorName
382 << " index " << index << "\n";
383 }
384
385 auto findPollRate = baseConfigMap.find("PollRate");
386 float pollRate = pollRateDefault;
387 if (findPollRate != baseConfigMap.end())
388 {
389 pollRate =
390 std::visit(VariantToFloatVisitor(), findPollRate->second);
Ed Tanous2049bd22022-07-09 07:20:26 -0700391 if (pollRate <= 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700392 {
393 pollRate = pollRateDefault; // polling time too short
394 }
395 }
396
Zev Weissa4d27682022-07-19 15:30:36 -0700397 PowerState readState = getPowerState(baseConfigMap);
Ed Tanousbb679322022-05-16 16:10:00 -0700398
399 auto permitSet = getPermitSet(baseConfigMap);
400 auto& sensor = sensors[sensorName];
401 sensor = nullptr;
402 auto hwmonFile =
403 getFullHwmonFilePath(directory.string(), "temp1", permitSet);
404 if (pathStr.starts_with("/sys/bus/iio/devices"))
405 {
406 hwmonFile = pathStr;
407 }
408 if (hwmonFile)
409 {
410 sensor = std::make_shared<HwmonTempSensor>(
411 *hwmonFile, sensorType, objectServer, dbusConnection, io,
412 sensorName, std::move(sensorThresholds),
413 thisSensorParameters, pollRate, interfacePath, readState);
414 sensor->setupRead();
415 }
416 hwmonName.erase(
417 remove(hwmonName.begin(), hwmonName.end(), sensorName),
418 hwmonName.end());
419
420 // Looking for keys like "Name1" for temp2_input,
421 // "Name2" for temp3_input, etc.
422 int i = 0;
423 while (true)
424 {
425 ++i;
426 auto findKey = baseConfigMap.find("Name" + std::to_string(i));
427 if (findKey == baseConfigMap.end())
428 {
429 break;
430 }
431 std::string sensorName = std::get<std::string>(findKey->second);
432 hwmonFile = getFullHwmonFilePath(directory.string(),
433 "temp" + std::to_string(i + 1),
434 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500435 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700436 {
James Feistdf515152019-09-18 16:40:40 -0700437 continue;
438 }
Jason Ling100c20b2020-08-11 14:50:33 -0700439 if (hwmonFile)
440 {
Ed Tanousbb679322022-05-16 16:10:00 -0700441 // To look up thresholds for these additional sensors,
442 // match on the Index property in the threshold data
443 // where the index comes from the sysfs file we're on,
444 // i.e. index = 2 for temp2_input.
445 int index = i + 1;
446 std::vector<thresholds::Threshold> thresholds;
447
448 if (!parseThresholdsFromConfig(sensorData, thresholds,
449 nullptr, &index))
450 {
451 std::cerr << "error populating thresholds for "
452 << sensorName << " index " << index << "\n";
453 }
454
455 auto& sensor = sensors[sensorName];
456 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700457 sensor = std::make_shared<HwmonTempSensor>(
458 *hwmonFile, sensorType, objectServer, dbusConnection,
Ed Tanousbb679322022-05-16 16:10:00 -0700459 io, sensorName, std::move(thresholds),
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530460 thisSensorParameters, pollRate, interfacePath,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500461 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700462 sensor->setupRead();
463 }
Ed Tanousbb679322022-05-16 16:10:00 -0700464
Willy Tu9eb0cc32022-04-06 11:03:32 -0700465 hwmonName.erase(
466 remove(hwmonName.begin(), hwmonName.end(), sensorName),
467 hwmonName.end());
James Feistdf515152019-09-18 16:40:40 -0700468 }
Ed Tanousbb679322022-05-16 16:10:00 -0700469 if (hwmonName.empty())
470 {
471 configMap.erase(findSensorCfg);
472 }
473 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700474 });
James Feistdf515152019-09-18 16:40:40 -0700475 getter->getConfiguration(
476 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700477}
478
Matt Spinler20bf2c12021-09-14 11:07:07 -0500479void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500480 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500481 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
482 sensors)
483{
484 if (message.is_method_error())
485 {
486 std::cerr << "interfacesRemoved callback method error\n";
487 return;
488 }
489
490 sdbusplus::message::object_path path;
491 std::vector<std::string> interfaces;
492
493 message.read(path, interfaces);
494
495 // If the xyz.openbmc_project.Confguration.X interface was removed
496 // for one or more sensors, delete those sensor objects.
497 auto sensorIt = sensors.begin();
498 while (sensorIt != sensors.end())
499 {
500 if ((sensorIt->second->configurationPath == path) &&
501 (std::find(interfaces.begin(), interfaces.end(),
502 sensorIt->second->objectType) != interfaces.end()))
503 {
504 sensorIt = sensors.erase(sensorIt);
505 }
506 else
507 {
508 sensorIt++;
509 }
510 }
511}
512
James Feistb6c0b912019-07-09 12:21:44 -0700513int main()
James Feist6714a252018-09-10 15:26:18 -0700514{
515 boost::asio::io_service io;
516 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
517 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
518 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800519 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700520 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700521 auto sensorsChanged =
522 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700523
524 io.post([&]() {
525 createSensors(io, objectServer, sensors, systemBus, nullptr);
526 });
527
528 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500529 std::function<void(sdbusplus::message_t&)> eventHandler =
530 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700531 if (message.is_method_error())
532 {
533 std::cerr << "callback method error\n";
534 return;
535 }
536 sensorsChanged->insert(message.get_path());
537 // this implicitly cancels the timer
538 filterTimer.expires_from_now(boost::posix_time::seconds(1));
539
540 filterTimer.async_wait([&](const boost::system::error_code& ec) {
541 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700542 {
Ed Tanousbb679322022-05-16 16:10:00 -0700543 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700544 return;
545 }
Ed Tanousbb679322022-05-16 16:10:00 -0700546 if (ec)
547 {
548 std::cerr << "timer error\n";
549 return;
550 }
551 createSensors(io, objectServer, sensors, systemBus, sensorsChanged);
552 });
553 };
James Feist6714a252018-09-10 15:26:18 -0700554
Zev Weiss214d9712022-08-12 12:54:31 -0700555 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
556 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800557 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500558
559 // Watch for entity-manager to remove configuration interfaces
560 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500561 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
562 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500563 "type='signal',member='InterfacesRemoved',arg0path='" +
564 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500565 [&sensors](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700566 interfaceRemoved(msg, sensors);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500567 });
568
569 matches.emplace_back(std::move(ifaceRemovedMatch));
570
James Feist6714a252018-09-10 15:26:18 -0700571 io.run();
572}