blob: b6bf9107a4818aa39ee5b8d64ae2c74f4873c68e [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>
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",
65 "xyz.openbmc_project.Configuration.SBTSI",
Potin Lai47863342021-12-14 18:58:12 +080066 "xyz.openbmc_project.Configuration.SI7020",
Brandon Kim66558232021-11-09 16:53:08 -080067 "xyz.openbmc_project.Configuration.TMP112",
68 "xyz.openbmc_project.Configuration.TMP175",
69 "xyz.openbmc_project.Configuration.TMP421",
70 "xyz.openbmc_project.Configuration.TMP441",
Brandon Kim66558232021-11-09 16:53:08 -080071 "xyz.openbmc_project.Configuration.TMP75",
Potin Lai47863342021-12-14 18:58:12 +080072 "xyz.openbmc_project.Configuration.W83773G"})};
James Feist6714a252018-09-10 15:26:18 -070073
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050074static struct SensorParams
75 getSensorParameters(const std::filesystem::path& path)
76{
77 // offset is to default to 0 and scale to 1, see lore
78 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
79 struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature,
80 .maxValue = maxValueTemperature,
81 .offsetValue = 0.0,
82 .scaleValue = 1.0,
Bruce Mitchell5a86e562021-12-10 12:26:22 -060083 .units =
84 sensor_paths::unitDegreesC,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050085 .typeName = "temperature"};
86
87 // For IIO RAW sensors we get a raw_value, an offset, and scale
88 // to compute the value = (raw_value + offset) * scale
89 // with a _raw IIO device we need to get the
90 // offsetValue and scaleValue from the driver
91 // these are used to compute the reading in
92 // units that have yet to be scaled for D-Bus.
93 const std::string pathStr = path.string();
94 if (pathStr.ends_with("_raw"))
95 {
96 std::string pathOffsetStr =
97 pathStr.substr(0, pathStr.size() - 4) + "_offset";
98 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
99 // In case there is nothing to read skip this device
100 // This is not an error condition see lore
101 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
102 if (tmpOffsetValue)
103 {
104 tmpSensorParameters.offsetValue = *tmpOffsetValue;
105 }
106
107 std::string pathScaleStr =
108 pathStr.substr(0, pathStr.size() - 4) + "_scale";
109 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
110 // In case there is nothing to read skip this device
111 // This is not an error condition see lore
112 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
113 if (tmpScaleValue)
114 {
115 tmpSensorParameters.scaleValue = *tmpScaleValue;
116 }
117 }
118
119 // Temperatures are read in milli degrees Celsius, we need
120 // degrees Celsius. Pressures are read in kilopascal, we need
121 // Pascals. On D-Bus for Open BMC we use the International
122 // System of Units without prefixes. Links to the kernel
123 // documentation:
124 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
125 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
126 if (path.filename() == "in_pressure_input" ||
127 path.filename() == "in_pressure_raw")
128 {
129 tmpSensorParameters.minValue = minValuePressure;
130 tmpSensorParameters.maxValue = maxValuePressure;
131 // Pressures are read in kilopascal, we need Pascals.
132 tmpSensorParameters.scaleValue *= 1000.0;
133 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600134 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500135 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600136 else if (path.filename() == "in_humidityrelative_input" ||
137 path.filename() == "in_humidityrelative_raw")
138 {
139 tmpSensorParameters.minValue = minValueRelativeHumidity;
140 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
141 // Relative Humidity are read in milli-percent, we need percent.
142 tmpSensorParameters.scaleValue *= 0.001;
143 tmpSensorParameters.typeName = "humidity";
144 tmpSensorParameters.units = "PercentRH";
145 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500146 else
147 {
148 // Temperatures are read in milli degrees Celsius,
149 // we need degrees Celsius.
150 tmpSensorParameters.scaleValue *= 0.001;
151 }
152
153 return tmpSensorParameters;
154}
155
James Feist6714a252018-09-10 15:26:18 -0700156void createSensors(
157 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800158 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700159 sensors,
160 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700161 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -0700162 sensorsChanged)
163{
James Feistdf515152019-09-18 16:40:40 -0700164 auto getter = std::make_shared<GetSensorConfiguration>(
165 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700166 [&io, &objectServer, &sensors, &dbusConnection,
167 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
James Feistdf515152019-09-18 16:40:40 -0700168 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700169
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500170 // IIO _raw devices look like this on sysfs:
171 // /sys/bus/iio/devices/iio:device0/in_temp_raw
172 // /sys/bus/iio/devices/iio:device0/in_temp_offset
173 // /sys/bus/iio/devices/iio:device0/in_temp_scale
174 //
175 // Other IIO devices look like this on sysfs:
176 // /sys/bus/iio/devices/iio:device1/in_temp_input
177 // /sys/bus/iio/devices/iio:device1/in_pressure_input
James Feistdf515152019-09-18 16:40:40 -0700178 std::vector<fs::path> paths;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500179 fs::path root("/sys/bus/iio/devices");
180 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
181 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600182 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500183 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
184
185 if (paths.empty())
James Feist37266ca2018-10-15 15:56:28 -0700186 {
James Feistdf515152019-09-18 16:40:40 -0700187 return;
188 }
189
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500190 // iterate through all found temp and pressure sensors,
191 // and try to match them with configuration
James Feistdf515152019-09-18 16:40:40 -0700192 for (auto& path : paths)
193 {
194 std::smatch match;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500195 const std::string pathStr = path.string();
James Feistdf515152019-09-18 16:40:40 -0700196 auto directory = path.parent_path();
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500197 fs::path device;
James Feistdf515152019-09-18 16:40:40 -0700198
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500199 std::string deviceName;
200 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700201 {
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500202 device = fs::canonical(directory);
203 deviceName = device.parent_path().stem();
James Feistdf515152019-09-18 16:40:40 -0700204 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500205 else
206 {
207 device = directory / "device";
208 deviceName = fs::canonical(device).stem();
209 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700210 auto findHyphen = deviceName.find('-');
James Feistdf515152019-09-18 16:40:40 -0700211 if (findHyphen == std::string::npos)
212 {
213 std::cerr << "found bad device " << deviceName << "\n";
214 continue;
215 }
216 std::string busStr = deviceName.substr(0, findHyphen);
217 std::string addrStr = deviceName.substr(findHyphen + 1);
218
219 size_t bus = 0;
220 size_t addr = 0;
221 try
222 {
223 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700224 addr = std::stoi(addrStr, nullptr, 16);
James Feistdf515152019-09-18 16:40:40 -0700225 }
Patrick Williams26601e82021-10-06 12:43:25 -0500226 catch (const std::invalid_argument&)
James Feistdf515152019-09-18 16:40:40 -0700227 {
228 continue;
229 }
230 const SensorData* sensorData = nullptr;
231 const std::string* interfacePath = nullptr;
232 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800233 const SensorBaseConfiguration* baseConfiguration = nullptr;
234 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700235
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500236 auto thisSensorParameters = getSensorParameters(path);
237
James Feistdf515152019-09-18 16:40:40 -0700238 for (const std::pair<sdbusplus::message::object_path,
239 SensorData>& sensor : sensorConfigurations)
240 {
241 sensorData = &(sensor.second);
242 for (const char* type : sensorTypes)
243 {
244 auto sensorBase = sensorData->find(type);
245 if (sensorBase != sensorData->end())
246 {
247 baseConfiguration = &(*sensorBase);
248 sensorType = type;
249 break;
250 }
251 }
252 if (baseConfiguration == nullptr)
253 {
254 std::cerr << "error finding base configuration for "
255 << deviceName << "\n";
256 continue;
257 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800258 baseConfigMap = &baseConfiguration->second;
259 auto configurationBus = baseConfigMap->find("Bus");
260 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700261
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800262 if (configurationBus == baseConfigMap->end() ||
263 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700264 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800265 std::cerr << "error finding bus or address in "
266 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700267 continue;
268 }
269
270 if (std::get<uint64_t>(configurationBus->second) != bus ||
271 std::get<uint64_t>(configurationAddress->second) !=
272 addr)
273 {
274 continue;
275 }
276
277 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700278 break;
279 }
James Feistdf515152019-09-18 16:40:40 -0700280 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700281 {
James Feistdf515152019-09-18 16:40:40 -0700282 continue;
James Feist6714a252018-09-10 15:26:18 -0700283 }
James Feist6714a252018-09-10 15:26:18 -0700284
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500285 // Temperature has "Name", pressure has "Name1"
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800286 auto findSensorName = baseConfigMap->find("Name");
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600287 if (thisSensorParameters.typeName == "pressure" ||
288 thisSensorParameters.typeName == "humidity")
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500289 {
290 findSensorName = baseConfigMap->find("Name1");
291 }
292
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800293 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700294 {
295 std::cerr << "could not determine configuration name for "
296 << deviceName << "\n";
297 continue;
298 }
299 std::string sensorName =
300 std::get<std::string>(findSensorName->second);
301 // on rescans, only update sensors we were signaled by
302 auto findSensor = sensors.find(sensorName);
303 if (!firstScan && findSensor != sensors.end())
304 {
305 bool found = false;
Bruce Mitchelld653b752021-08-23 13:09:00 -0500306 auto it = sensorsChanged->begin();
307 while (it != sensorsChanged->end())
James Feistdf515152019-09-18 16:40:40 -0700308 {
309 if (boost::ends_with(*it, findSensor->second->name))
310 {
Bruce Mitchelld653b752021-08-23 13:09:00 -0500311 it = sensorsChanged->erase(it);
James Feistdf515152019-09-18 16:40:40 -0700312 findSensor->second = nullptr;
313 found = true;
314 break;
315 }
Bruce Mitchelld653b752021-08-23 13:09:00 -0500316 ++it;
James Feistdf515152019-09-18 16:40:40 -0700317 }
318 if (!found)
319 {
320 continue;
321 }
322 }
Matt Spinler5636d522021-03-17 14:52:18 -0500323
James Feistdf515152019-09-18 16:40:40 -0700324 std::vector<thresholds::Threshold> sensorThresholds;
Matt Spinler5636d522021-03-17 14:52:18 -0500325 int index = 1;
326
327 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
328 nullptr, &index))
James Feistdf515152019-09-18 16:40:40 -0700329 {
330 std::cerr << "error populating thresholds for "
Matt Spinler5636d522021-03-17 14:52:18 -0500331 << sensorName << " index 1\n";
James Feistdf515152019-09-18 16:40:40 -0700332 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800333
334 auto findPollRate = baseConfiguration->second.find("PollRate");
335 float pollRate = pollRateDefault;
336 if (findPollRate != baseConfiguration->second.end())
337 {
338 pollRate = std::visit(VariantToFloatVisitor(),
339 findPollRate->second);
340 if (pollRate <= 0.0f)
341 {
342 pollRate = pollRateDefault; // polling time too short
343 }
344 }
345
James Feistf9b01b62020-01-29 15:21:58 -0800346 auto findPowerOn = baseConfiguration->second.find("PowerState");
347 PowerState readState = PowerState::always;
348 if (findPowerOn != baseConfiguration->second.end())
349 {
350 std::string powerState = std::visit(
351 VariantToStringVisitor(), findPowerOn->second);
352 setReadState(powerState, readState);
353 }
Jason Ling100c20b2020-08-11 14:50:33 -0700354
355 auto permitSet = getPermitSet(*baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800356 auto& sensor = sensors[sensorName];
357 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700358 auto hwmonFile = getFullHwmonFilePath(directory.string(),
359 "temp1", permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500360 if (pathStr.starts_with("/sys/bus/iio/devices"))
361 {
362 hwmonFile = pathStr;
363 }
Jason Ling100c20b2020-08-11 14:50:33 -0700364 if (hwmonFile)
365 {
366 sensor = std::make_shared<HwmonTempSensor>(
367 *hwmonFile, sensorType, objectServer, dbusConnection,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500368 io, sensorName, std::move(sensorThresholds),
369 thisSensorParameters, pollRate, *interfacePath,
370 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700371 sensor->setupRead();
372 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800373 // Looking for keys like "Name1" for temp2_input,
374 // "Name2" for temp3_input, etc.
375 int i = 0;
376 while (true)
James Feistdf515152019-09-18 16:40:40 -0700377 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800378 ++i;
379 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700380 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800381 if (findKey == baseConfigMap->end())
382 {
383 break;
384 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800385 std::string sensorName =
386 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700387 hwmonFile = getFullHwmonFilePath(
388 directory.string(), "temp" + std::to_string(i + 1),
389 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500390 if (pathStr.starts_with("/sys/bus/iio/devices"))
391 {
392 continue;
393 }
Jason Ling100c20b2020-08-11 14:50:33 -0700394 if (hwmonFile)
395 {
Matt Spinler5636d522021-03-17 14:52:18 -0500396 // To look up thresholds for these additional sensors,
397 // match on the Index property in the threshold data
398 // where the index comes from the sysfs file we're on,
399 // i.e. index = 2 for temp2_input.
400 int index = i + 1;
401 std::vector<thresholds::Threshold> thresholds;
402
403 if (!parseThresholdsFromConfig(*sensorData, thresholds,
404 nullptr, &index))
405 {
406 std::cerr << "error populating thresholds for "
407 << sensorName << " index " << index
408 << "\n";
409 }
410
Jason Ling100c20b2020-08-11 14:50:33 -0700411 auto& sensor = sensors[sensorName];
412 sensor = nullptr;
413 sensor = std::make_shared<HwmonTempSensor>(
414 *hwmonFile, sensorType, objectServer,
415 dbusConnection, io, sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500416 std::move(thresholds), thisSensorParameters,
417 pollRate, *interfacePath, readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700418 sensor->setupRead();
419 }
James Feistdf515152019-09-18 16:40:40 -0700420 }
James Feistdf515152019-09-18 16:40:40 -0700421 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700422 });
James Feistdf515152019-09-18 16:40:40 -0700423 getter->getConfiguration(
424 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700425}
426
Matt Spinler20bf2c12021-09-14 11:07:07 -0500427void interfaceRemoved(
428 sdbusplus::message::message& message,
429 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
430 sensors)
431{
432 if (message.is_method_error())
433 {
434 std::cerr << "interfacesRemoved callback method error\n";
435 return;
436 }
437
438 sdbusplus::message::object_path path;
439 std::vector<std::string> interfaces;
440
441 message.read(path, interfaces);
442
443 // If the xyz.openbmc_project.Confguration.X interface was removed
444 // for one or more sensors, delete those sensor objects.
445 auto sensorIt = sensors.begin();
446 while (sensorIt != sensors.end())
447 {
448 if ((sensorIt->second->configurationPath == path) &&
449 (std::find(interfaces.begin(), interfaces.end(),
450 sensorIt->second->objectType) != interfaces.end()))
451 {
452 sensorIt = sensors.erase(sensorIt);
453 }
454 else
455 {
456 sensorIt++;
457 }
458 }
459}
460
James Feistb6c0b912019-07-09 12:21:44 -0700461int main()
James Feist6714a252018-09-10 15:26:18 -0700462{
463 boost::asio::io_service io;
464 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
465 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
466 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800467 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700468 sensors;
469 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700470 auto sensorsChanged =
471 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700472
473 io.post([&]() {
474 createSensors(io, objectServer, sensors, systemBus, nullptr);
475 });
476
477 boost::asio::deadline_timer filterTimer(io);
478 std::function<void(sdbusplus::message::message&)> eventHandler =
479 [&](sdbusplus::message::message& message) {
480 if (message.is_method_error())
481 {
482 std::cerr << "callback method error\n";
483 return;
484 }
485 sensorsChanged->insert(message.get_path());
486 // this implicitly cancels the timer
487 filterTimer.expires_from_now(boost::posix_time::seconds(1));
488
489 filterTimer.async_wait([&](const boost::system::error_code& ec) {
490 if (ec == boost::asio::error::operation_aborted)
491 {
492 /* we were canceled*/
493 return;
494 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700495 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700496 {
497 std::cerr << "timer error\n";
498 return;
499 }
500 createSensors(io, objectServer, sensors, systemBus,
501 sensorsChanged);
502 });
503 };
504
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700505 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700506 {
507 auto match = std::make_unique<sdbusplus::bus::match::match>(
508 static_cast<sdbusplus::bus::bus&>(*systemBus),
509 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700510 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700511 eventHandler);
512 matches.emplace_back(std::move(match));
513 }
514
Bruce Lee1263c3d2021-06-04 15:16:33 +0800515 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500516
517 // Watch for entity-manager to remove configuration interfaces
518 // so the corresponding sensors can be removed.
519 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>(
520 static_cast<sdbusplus::bus::bus&>(*systemBus),
521 "type='signal',member='InterfacesRemoved',arg0path='" +
522 std::string(inventoryPath) + "/'",
523 [&sensors](sdbusplus::message::message& msg) {
524 interfaceRemoved(msg, sensors);
525 });
526
527 matches.emplace_back(std::move(ifaceRemovedMatch));
528
James Feist6714a252018-09-10 15:26:18 -0700529 io.run();
530}