blob: c157b194971b11c060f5c89a942b0aec444ceac5 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "DeviceMgmt.hpp"
18#include "HwmonTempSensor.hpp"
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
28#include <array>
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -070029#include <charconv>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
33#include <memory>
James Feist6714a252018-09-10 15:26:18 -070034#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <stdexcept>
36#include <string>
37#include <utility>
38#include <variant>
39#include <vector>
James Feist6714a252018-09-10 15:26:18 -070040
Jeff Lin87bc67f2020-12-04 20:58:01 +080041static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070042
Zhikui Rencb359da2023-08-03 16:45:44 -070043static constexpr double maxValuePressure = 120000; // Pascals
44static constexpr double minValuePressure = 30000; // Pascals
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050045
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060046static constexpr double maxValueRelativeHumidity = 100; // PercentRH
47static constexpr double minValueRelativeHumidity = 0; // PercentRH
48
Zhikui Rencb359da2023-08-03 16:45:44 -070049static constexpr double maxValueTemperature = 127; // DegreesC
50static constexpr double minValueTemperature = -128; // DegreesC
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050051
James Feistcf3bce62019-01-08 10:07:19 -080052namespace fs = std::filesystem;
Zev Weissd29f8aa2022-08-03 16:24:59 -070053
54static const I2CDeviceTypeMap sensorTypes{
Patrick Rudolph76d36762023-09-20 16:53:38 +020055 {"ADM1021", I2CDeviceType{"adm1021", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070056 {"DPS310", I2CDeviceType{"dps310", false}},
57 {"EMC1412", I2CDeviceType{"emc1412", true}},
58 {"EMC1413", I2CDeviceType{"emc1413", true}},
59 {"EMC1414", I2CDeviceType{"emc1414", true}},
60 {"HDC1080", I2CDeviceType{"hdc1080", false}},
61 {"JC42", I2CDeviceType{"jc42", true}},
62 {"LM75A", I2CDeviceType{"lm75a", true}},
63 {"LM95234", I2CDeviceType{"lm95234", true}},
64 {"MAX31725", I2CDeviceType{"max31725", true}},
65 {"MAX31730", I2CDeviceType{"max31730", true}},
66 {"MAX6581", I2CDeviceType{"max6581", true}},
67 {"MAX6654", I2CDeviceType{"max6654", true}},
Patrick Rudolphf3862ee2023-10-23 14:13:06 +020068 {"MAX6639", I2CDeviceType{"max6639", true}},
Potin Lai28b88232023-12-13 16:41:01 +080069 {"MCP9600", I2CDeviceType{"mcp9600", false}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070070 {"NCT6779", I2CDeviceType{"nct6779", true}},
71 {"NCT7802", I2CDeviceType{"nct7802", true}},
72 {"SBTSI", I2CDeviceType{"sbtsi", true}},
73 {"SI7020", I2CDeviceType{"si7020", false}},
Tim Lee9b0a6f62022-12-23 14:47:02 +080074 {"TMP100", I2CDeviceType{"tmp100", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070075 {"TMP112", I2CDeviceType{"tmp112", true}},
76 {"TMP175", I2CDeviceType{"tmp175", true}},
77 {"TMP421", I2CDeviceType{"tmp421", true}},
78 {"TMP441", I2CDeviceType{"tmp441", true}},
Khang Kieuf4c3fad2023-11-27 22:39:01 +000079 {"TMP461", I2CDeviceType{"tmp461", true}},
Hieu Huynh9a7db6a2023-06-19 11:02:07 +000080 {"TMP464", I2CDeviceType{"tmp464", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070081 {"TMP75", I2CDeviceType{"tmp75", true}},
82 {"W83773G", I2CDeviceType{"w83773g", true}},
83};
James Feist6714a252018-09-10 15:26:18 -070084
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050085static struct SensorParams
86 getSensorParameters(const std::filesystem::path& path)
87{
88 // offset is to default to 0 and scale to 1, see lore
89 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
Patrick Williams779c96a2023-05-10 07:50:42 -050090 struct SensorParams tmpSensorParameters = {
91 .minValue = minValueTemperature,
92 .maxValue = maxValueTemperature,
93 .offsetValue = 0.0,
94 .scaleValue = 1.0,
95 .units = sensor_paths::unitDegreesC,
96 .typeName = "temperature"};
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050097
98 // For IIO RAW sensors we get a raw_value, an offset, and scale
99 // to compute the value = (raw_value + offset) * scale
100 // with a _raw IIO device we need to get the
101 // offsetValue and scaleValue from the driver
102 // these are used to compute the reading in
103 // units that have yet to be scaled for D-Bus.
104 const std::string pathStr = path.string();
105 if (pathStr.ends_with("_raw"))
106 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500107 std::string pathOffsetStr = pathStr.substr(0, pathStr.size() - 4) +
108 "_offset";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500109 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 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 (tmpOffsetValue)
114 {
115 tmpSensorParameters.offsetValue = *tmpOffsetValue;
116 }
117
Patrick Williams779c96a2023-05-10 07:50:42 -0500118 std::string pathScaleStr = pathStr.substr(0, pathStr.size() - 4) +
119 "_scale";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500120 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
121 // In case there is nothing to read skip this device
122 // This is not an error condition see lore
123 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
124 if (tmpScaleValue)
125 {
126 tmpSensorParameters.scaleValue = *tmpScaleValue;
127 }
128 }
129
130 // Temperatures are read in milli degrees Celsius, we need
131 // degrees Celsius. Pressures are read in kilopascal, we need
132 // Pascals. On D-Bus for Open BMC we use the International
133 // System of Units without prefixes. Links to the kernel
134 // documentation:
135 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
136 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
137 if (path.filename() == "in_pressure_input" ||
138 path.filename() == "in_pressure_raw")
139 {
140 tmpSensorParameters.minValue = minValuePressure;
141 tmpSensorParameters.maxValue = maxValuePressure;
142 // Pressures are read in kilopascal, we need Pascals.
143 tmpSensorParameters.scaleValue *= 1000.0;
144 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600145 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500146 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600147 else if (path.filename() == "in_humidityrelative_input" ||
148 path.filename() == "in_humidityrelative_raw")
149 {
150 tmpSensorParameters.minValue = minValueRelativeHumidity;
151 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
152 // Relative Humidity are read in milli-percent, we need percent.
153 tmpSensorParameters.scaleValue *= 0.001;
154 tmpSensorParameters.typeName = "humidity";
155 tmpSensorParameters.units = "PercentRH";
156 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500157 else
158 {
159 // Temperatures are read in milli degrees Celsius,
160 // we need degrees Celsius.
161 tmpSensorParameters.scaleValue *= 0.001;
162 }
163
164 return tmpSensorParameters;
165}
166
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530167struct SensorConfigKey
168{
169 uint64_t bus;
170 uint64_t addr;
171 bool operator<(const SensorConfigKey& other) const
172 {
173 if (bus != other.bus)
174 {
175 return bus < other.bus;
176 }
177 return addr < other.addr;
178 }
179};
180
181struct SensorConfig
182{
183 std::string sensorPath;
184 SensorData sensorData;
185 std::string interface;
186 SensorBaseConfigMap config;
187 std::vector<std::string> name;
188};
189
190using SensorConfigMap =
191 boost::container::flat_map<SensorConfigKey, SensorConfig>;
192
193static SensorConfigMap
194 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
195{
196 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700197 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530198 {
Zev Weissa1808332022-08-12 18:21:01 -0700199 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530200 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530201 auto busCfg = cfg.find("Bus");
202 auto addrCfg = cfg.find("Address");
203 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
204 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530205 continue;
206 }
207
Ed Tanous2049bd22022-07-09 07:20:26 -0700208 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
209 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530210 {
Zev Weissa1808332022-08-12 18:21:01 -0700211 std::cerr << path.str << " Bus or Address invalid\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530212 continue;
213 }
214
215 std::vector<std::string> hwmonNames;
216 auto nameCfg = cfg.find("Name");
217 if (nameCfg != cfg.end())
218 {
219 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
220 size_t i = 1;
221 while (true)
222 {
223 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
224 if (sensorNameCfg == cfg.end())
225 {
226 break;
227 }
228 hwmonNames.push_back(
229 std::get<std::string>(sensorNameCfg->second));
230 i++;
231 }
232 }
233
234 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
235 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700236 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530237
238 auto [it, inserted] = configMap.emplace(key, std::move(val));
239 if (!inserted)
240 {
Zev Weissa1808332022-08-12 18:21:01 -0700241 std::cerr << path.str << ": ignoring duplicate entry for {"
242 << key.bus << ", 0x" << std::hex << key.addr
243 << std::dec << "}\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530244 }
245 }
246 }
247 return configMap;
248}
249
James Feist6714a252018-09-10 15:26:18 -0700250void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800251 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800252 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700253 sensors,
254 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700255 const std::shared_ptr<boost::container::flat_set<std::string>>&
Zev Weissa1456c42022-07-18 16:59:35 -0700256 sensorsChanged,
257 bool activateOnly)
James Feist6714a252018-09-10 15:26:18 -0700258{
James Feistdf515152019-09-18 16:40:40 -0700259 auto getter = std::make_shared<GetSensorConfiguration>(
260 dbusConnection,
Zev Weissa1456c42022-07-18 16:59:35 -0700261 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
262 activateOnly](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700263 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700264
Ed Tanousbb679322022-05-16 16:10:00 -0700265 SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530266
Matt Simmeringc564eda2023-05-12 13:04:43 -0700267 auto devices = instantiateDevices(sensorConfigurations, sensors,
268 sensorTypes);
Zev Weissa1456c42022-07-18 16:59:35 -0700269
Ed Tanousbb679322022-05-16 16:10:00 -0700270 // IIO _raw devices look like this on sysfs:
271 // /sys/bus/iio/devices/iio:device0/in_temp_raw
272 // /sys/bus/iio/devices/iio:device0/in_temp_offset
273 // /sys/bus/iio/devices/iio:device0/in_temp_scale
274 //
275 // Other IIO devices look like this on sysfs:
276 // /sys/bus/iio/devices/iio:device1/in_temp_input
277 // /sys/bus/iio/devices/iio:device1/in_pressure_input
278 std::vector<fs::path> paths;
279 fs::path root("/sys/bus/iio/devices");
280 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
281 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
282 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
283 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500284
Ed Tanousbb679322022-05-16 16:10:00 -0700285 // iterate through all found temp and pressure sensors,
286 // and try to match them with configuration
287 for (auto& path : paths)
288 {
289 std::smatch match;
290 const std::string pathStr = path.string();
291 auto directory = path.parent_path();
292 fs::path device;
293
294 std::string deviceName;
295 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700296 {
Ed Tanousbb679322022-05-16 16:10:00 -0700297 device = fs::canonical(directory);
298 deviceName = device.parent_path().stem();
299 }
300 else
301 {
302 device = directory / "device";
303 deviceName = fs::canonical(device).stem();
304 }
Ed Tanousbb679322022-05-16 16:10:00 -0700305
Tom Tung278e1772023-12-12 09:20:40 +0800306 uint64_t bus = 0;
307 uint64_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000308 if (!getDeviceBusAddr(deviceName, bus, addr))
Ed Tanousbb679322022-05-16 16:10:00 -0700309 {
310 continue;
James Feistdf515152019-09-18 16:40:40 -0700311 }
312
Ed Tanousbb679322022-05-16 16:10:00 -0700313 auto thisSensorParameters = getSensorParameters(path);
314 auto findSensorCfg = configMap.find({bus, addr});
315 if (findSensorCfg == configMap.end())
James Feistdf515152019-09-18 16:40:40 -0700316 {
Ed Tanousbb679322022-05-16 16:10:00 -0700317 continue;
318 }
James Feistdf515152019-09-18 16:40:40 -0700319
Ed Tanousbb679322022-05-16 16:10:00 -0700320 const std::string& interfacePath = findSensorCfg->second.sensorPath;
Zev Weiss7627c862022-11-17 14:23:04 -0800321 auto findI2CDev = devices.find(interfacePath);
322
Zev Weissa1456c42022-07-18 16:59:35 -0700323 std::shared_ptr<I2CDevice> i2cDev;
Zev Weiss7627c862022-11-17 14:23:04 -0800324 if (findI2CDev != devices.end())
Zev Weissa1456c42022-07-18 16:59:35 -0700325 {
Joseph Fu9bbeff72022-12-05 19:09:16 +0800326 // If we're only looking to activate newly-instantiated i2c
327 // devices and this sensor's underlying device was already there
328 // before this call, there's nothing more to do here.
329 if (activateOnly && !findI2CDev->second.second)
330 {
331 continue;
332 }
Zev Weiss7627c862022-11-17 14:23:04 -0800333 i2cDev = findI2CDev->second.first;
Zev Weissa1456c42022-07-18 16:59:35 -0700334 }
335
Ed Tanousbb679322022-05-16 16:10:00 -0700336 const SensorData& sensorData = findSensorCfg->second.sensorData;
Matt Spinler0489ec22023-06-05 15:08:59 -0500337 std::string sensorType = findSensorCfg->second.interface;
338 auto pos = sensorType.find_last_of('.');
339 if (pos != std::string::npos)
340 {
341 sensorType = sensorType.substr(pos + 1);
342 }
Ed Tanousbb679322022-05-16 16:10:00 -0700343 const SensorBaseConfigMap& baseConfigMap =
344 findSensorCfg->second.config;
345 std::vector<std::string>& hwmonName = findSensorCfg->second.name;
346
347 // Temperature has "Name", pressure has "Name1"
348 auto findSensorName = baseConfigMap.find("Name");
349 int index = 1;
350 if (thisSensorParameters.typeName == "pressure" ||
351 thisSensorParameters.typeName == "humidity")
352 {
353 findSensorName = baseConfigMap.find("Name1");
354 index = 2;
355 }
356
357 if (findSensorName == baseConfigMap.end())
358 {
359 std::cerr << "could not determine configuration name for "
360 << deviceName << "\n";
361 continue;
362 }
363 std::string sensorName =
364 std::get<std::string>(findSensorName->second);
365 // on rescans, only update sensors we were signaled by
366 auto findSensor = sensors.find(sensorName);
367 if (!firstScan && findSensor != sensors.end())
368 {
369 bool found = false;
370 auto it = sensorsChanged->begin();
371 while (it != sensorsChanged->end())
372 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700373 if (it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700374 {
375 it = sensorsChanged->erase(it);
376 findSensor->second = nullptr;
377 found = true;
378 break;
379 }
380 ++it;
381 }
382 if (!found)
383 {
384 continue;
385 }
386 }
387
388 std::vector<thresholds::Threshold> sensorThresholds;
389
390 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
391 nullptr, &index))
392 {
393 std::cerr << "error populating thresholds for " << sensorName
394 << " index " << index << "\n";
395 }
396
Zev Weiss8569bf22022-10-11 15:37:44 -0700397 float pollRate = getPollRate(baseConfigMap, pollRateDefault);
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];
Zev Weissa1456c42022-07-18 16:59:35 -0700402 if (!activateOnly)
403 {
404 sensor = nullptr;
405 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500406 auto hwmonFile = getFullHwmonFilePath(directory.string(), "temp1",
407 permitSet);
Ed Tanousbb679322022-05-16 16:10:00 -0700408 if (pathStr.starts_with("/sys/bus/iio/devices"))
409 {
410 hwmonFile = pathStr;
411 }
412 if (hwmonFile)
413 {
Zev Weissa1456c42022-07-18 16:59:35 -0700414 if (sensor != nullptr)
415 {
416 sensor->activate(*hwmonFile, i2cDev);
417 }
418 else
419 {
420 sensor = std::make_shared<HwmonTempSensor>(
421 *hwmonFile, sensorType, objectServer, dbusConnection,
422 io, sensorName, std::move(sensorThresholds),
423 thisSensorParameters, pollRate, interfacePath,
424 readState, i2cDev);
425 sensor->setupRead();
426 }
Ed Tanousbb679322022-05-16 16:10:00 -0700427 }
428 hwmonName.erase(
429 remove(hwmonName.begin(), hwmonName.end(), sensorName),
430 hwmonName.end());
431
432 // Looking for keys like "Name1" for temp2_input,
433 // "Name2" for temp3_input, etc.
434 int i = 0;
435 while (true)
436 {
437 ++i;
438 auto findKey = baseConfigMap.find("Name" + std::to_string(i));
439 if (findKey == baseConfigMap.end())
440 {
441 break;
442 }
443 std::string sensorName = std::get<std::string>(findKey->second);
444 hwmonFile = getFullHwmonFilePath(directory.string(),
445 "temp" + std::to_string(i + 1),
446 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500447 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700448 {
James Feistdf515152019-09-18 16:40:40 -0700449 continue;
450 }
Jason Ling100c20b2020-08-11 14:50:33 -0700451 if (hwmonFile)
452 {
Ed Tanousbb679322022-05-16 16:10:00 -0700453 // To look up thresholds for these additional sensors,
454 // match on the Index property in the threshold data
455 // where the index comes from the sysfs file we're on,
456 // i.e. index = 2 for temp2_input.
457 int index = i + 1;
458 std::vector<thresholds::Threshold> thresholds;
459
460 if (!parseThresholdsFromConfig(sensorData, thresholds,
461 nullptr, &index))
462 {
463 std::cerr << "error populating thresholds for "
464 << sensorName << " index " << index << "\n";
465 }
466
467 auto& sensor = sensors[sensorName];
Zev Weissa1456c42022-07-18 16:59:35 -0700468 if (!activateOnly)
469 {
470 sensor = nullptr;
471 }
472
473 if (sensor != nullptr)
474 {
475 sensor->activate(*hwmonFile, i2cDev);
476 }
477 else
478 {
479 sensor = std::make_shared<HwmonTempSensor>(
480 *hwmonFile, sensorType, objectServer,
481 dbusConnection, io, sensorName,
482 std::move(thresholds), thisSensorParameters,
483 pollRate, interfacePath, readState, i2cDev);
484 sensor->setupRead();
485 }
Jason Ling100c20b2020-08-11 14:50:33 -0700486 }
Ed Tanousbb679322022-05-16 16:10:00 -0700487
Willy Tu9eb0cc32022-04-06 11:03:32 -0700488 hwmonName.erase(
489 remove(hwmonName.begin(), hwmonName.end(), sensorName),
490 hwmonName.end());
James Feistdf515152019-09-18 16:40:40 -0700491 }
Ed Tanousbb679322022-05-16 16:10:00 -0700492 if (hwmonName.empty())
493 {
494 configMap.erase(findSensorCfg);
495 }
496 }
Patrick Williams597e8422023-10-20 11:19:01 -0500497 });
Zev Weissd29f8aa2022-08-03 16:24:59 -0700498 std::vector<std::string> types(sensorTypes.size());
499 for (const auto& [type, dt] : sensorTypes)
500 {
501 types.push_back(type);
502 }
503 getter->getConfiguration(types);
James Feist6714a252018-09-10 15:26:18 -0700504}
505
Matt Spinler20bf2c12021-09-14 11:07:07 -0500506void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500507 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500508 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
509 sensors)
510{
511 if (message.is_method_error())
512 {
513 std::cerr << "interfacesRemoved callback method error\n";
514 return;
515 }
516
517 sdbusplus::message::object_path path;
518 std::vector<std::string> interfaces;
519
520 message.read(path, interfaces);
521
522 // If the xyz.openbmc_project.Confguration.X interface was removed
523 // for one or more sensors, delete those sensor objects.
524 auto sensorIt = sensors.begin();
525 while (sensorIt != sensors.end())
526 {
Patrick Rudolph6289f5a2023-09-22 11:32:54 +0200527 if (sensorIt->second && (sensorIt->second->configurationPath == path) &&
Matt Spinler20bf2c12021-09-14 11:07:07 -0500528 (std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500529 sensorIt->second->configInterface) != interfaces.end()))
Matt Spinler20bf2c12021-09-14 11:07:07 -0500530 {
531 sensorIt = sensors.erase(sensorIt);
532 }
533 else
534 {
535 sensorIt++;
536 }
537 }
538}
539
Zev Weissa1456c42022-07-18 16:59:35 -0700540static void powerStateChanged(
541 PowerState type, bool newState,
542 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
543 sensors,
Ed Tanous1f978632023-02-28 18:16:39 -0800544 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zev Weissa1456c42022-07-18 16:59:35 -0700545 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
546{
547 if (newState)
548 {
549 createSensors(io, objectServer, sensors, dbusConnection, nullptr, true);
550 }
551 else
552 {
553 for (auto& [path, sensor] : sensors)
554 {
555 if (sensor != nullptr && sensor->readState == type)
556 {
557 sensor->deactivate();
558 }
559 }
560 }
561}
562
James Feistb6c0b912019-07-09 12:21:44 -0700563int main()
James Feist6714a252018-09-10 15:26:18 -0700564{
Ed Tanous1f978632023-02-28 18:16:39 -0800565 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700566 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700567 sdbusplus::asio::object_server objectServer(systemBus, true);
568 objectServer.add_manager("/xyz/openbmc_project/sensors");
James Feist6714a252018-09-10 15:26:18 -0700569 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700570
Yong Lif3fd1912020-03-25 21:35:23 +0800571 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700572 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700573 auto sensorsChanged =
574 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700575
Zev Weissa1456c42022-07-18 16:59:35 -0700576 auto powerCallBack = [&sensors, &io, &objectServer,
577 &systemBus](PowerState type, bool state) {
578 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
579 };
580 setupPowerMatchCallback(systemBus, powerCallBack);
581
Ed Tanous83db50c2023-03-01 10:20:24 -0800582 boost::asio::post(io, [&]() {
Zev Weissa1456c42022-07-18 16:59:35 -0700583 createSensors(io, objectServer, sensors, systemBus, nullptr, false);
James Feist6714a252018-09-10 15:26:18 -0700584 });
585
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700586 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500587 std::function<void(sdbusplus::message_t&)> eventHandler =
588 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700589 if (message.is_method_error())
590 {
591 std::cerr << "callback method error\n";
592 return;
593 }
594 sensorsChanged->insert(message.get_path());
595 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800596 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700597
598 filterTimer.async_wait([&](const boost::system::error_code& ec) {
599 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700600 {
Ed Tanousbb679322022-05-16 16:10:00 -0700601 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700602 return;
603 }
Ed Tanousbb679322022-05-16 16:10:00 -0700604 if (ec)
605 {
606 std::cerr << "timer error\n";
607 return;
608 }
Zev Weissa1456c42022-07-18 16:59:35 -0700609 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
610 false);
Ed Tanousbb679322022-05-16 16:10:00 -0700611 });
612 };
James Feist6714a252018-09-10 15:26:18 -0700613
Zev Weiss214d9712022-08-12 12:54:31 -0700614 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
615 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800616 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500617
618 // Watch for entity-manager to remove configuration interfaces
619 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500620 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
621 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500622 "type='signal',member='InterfacesRemoved',arg0path='" +
623 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500624 [&sensors](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700625 interfaceRemoved(msg, sensors);
Patrick Williams597e8422023-10-20 11:19:01 -0500626 });
Matt Spinler20bf2c12021-09-14 11:07:07 -0500627
628 matches.emplace_back(std::move(ifaceRemovedMatch));
629
James Feist6714a252018-09-10 15:26:18 -0700630 io.run();
631}