blob: 1ec199fa204bdcb522c1429ca0b918989190827d [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"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "SensorPaths.hpp"
20#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103021#include "Utils.hpp"
22
Ed Tanouseacbfdd2024-04-04 12:00:24 -070023#include <boost/asio/error.hpp>
24#include <boost/asio/io_context.hpp>
25#include <boost/asio/post.hpp>
26#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070028#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070029#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070031#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070032#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <sdbusplus/message.hpp>
34#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070035
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070037#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <chrono>
39#include <cstddef>
40#include <cstdint>
James Feist24f02f22019-04-15 11:05:39 -070041#include <filesystem>
Patrick Venture96e97db2019-10-31 13:44:38 -070042#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070043#include <ios>
44#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070045#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#include <optional>
James Feist6714a252018-09-10 15:26:18 -070047#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070048#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070049#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070050#include <utility>
51#include <variant>
52#include <vector>
James Feist6714a252018-09-10 15:26:18 -070053
Jeff Lin87bc67f2020-12-04 20:58:01 +080054static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070055
Zhikui Rencb359da2023-08-03 16:45:44 -070056static constexpr double maxValuePressure = 120000; // Pascals
57static constexpr double minValuePressure = 30000; // Pascals
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050058
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060059static constexpr double maxValueRelativeHumidity = 100; // PercentRH
60static constexpr double minValueRelativeHumidity = 0; // PercentRH
61
Zhikui Rencb359da2023-08-03 16:45:44 -070062static constexpr double maxValueTemperature = 127; // DegreesC
63static constexpr double minValueTemperature = -128; // DegreesC
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050064
Zev Weissd29f8aa2022-08-03 16:24:59 -070065static const I2CDeviceTypeMap sensorTypes{
Patrick Rudolph76d36762023-09-20 16:53:38 +020066 {"ADM1021", I2CDeviceType{"adm1021", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070067 {"DPS310", I2CDeviceType{"dps310", false}},
Delphine CC Chiu53896db2024-06-07 18:36:13 +080068 {"EMC1403", I2CDeviceType{"emc1403", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070069 {"EMC1412", I2CDeviceType{"emc1412", true}},
70 {"EMC1413", I2CDeviceType{"emc1413", true}},
71 {"EMC1414", I2CDeviceType{"emc1414", true}},
72 {"HDC1080", I2CDeviceType{"hdc1080", false}},
73 {"JC42", I2CDeviceType{"jc42", true}},
74 {"LM75A", I2CDeviceType{"lm75a", true}},
75 {"LM95234", I2CDeviceType{"lm95234", true}},
76 {"MAX31725", I2CDeviceType{"max31725", true}},
77 {"MAX31730", I2CDeviceType{"max31730", true}},
78 {"MAX6581", I2CDeviceType{"max6581", true}},
79 {"MAX6654", I2CDeviceType{"max6654", true}},
Patrick Rudolphf3862ee2023-10-23 14:13:06 +020080 {"MAX6639", I2CDeviceType{"max6639", true}},
Potin Lai28b88232023-12-13 16:41:01 +080081 {"MCP9600", I2CDeviceType{"mcp9600", false}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070082 {"NCT6779", I2CDeviceType{"nct6779", true}},
83 {"NCT7802", I2CDeviceType{"nct7802", true}},
Potin Laifb011cc2024-02-22 19:51:48 +080084 {"PT5161L", I2CDeviceType{"pt5161l", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070085 {"SBTSI", I2CDeviceType{"sbtsi", true}},
86 {"SI7020", I2CDeviceType{"si7020", false}},
Tim Lee9b0a6f62022-12-23 14:47:02 +080087 {"TMP100", I2CDeviceType{"tmp100", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070088 {"TMP112", I2CDeviceType{"tmp112", true}},
89 {"TMP175", I2CDeviceType{"tmp175", true}},
90 {"TMP421", I2CDeviceType{"tmp421", true}},
Andrew Geissler8800c042024-07-23 17:19:17 -030091 {"TMP432", I2CDeviceType{"tmp432", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070092 {"TMP441", I2CDeviceType{"tmp441", true}},
Khang Kieuf4c3fad2023-11-27 22:39:01 +000093 {"TMP461", I2CDeviceType{"tmp461", true}},
Hieu Huynh9a7db6a2023-06-19 11:02:07 +000094 {"TMP464", I2CDeviceType{"tmp464", true}},
Hieu Huynh10480772024-09-13 11:23:46 +000095 {"TMP468", I2CDeviceType{"tmp468", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070096 {"TMP75", I2CDeviceType{"tmp75", true}},
97 {"W83773G", I2CDeviceType{"w83773g", true}},
98};
James Feist6714a252018-09-10 15:26:18 -070099
Patrick Williams556e04b2025-02-01 08:22:22 -0500100static struct SensorParams getSensorParameters(
101 const std::filesystem::path& path)
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500102{
103 // offset is to default to 0 and scale to 1, see lore
104 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
Patrick Williams779c96a2023-05-10 07:50:42 -0500105 struct SensorParams tmpSensorParameters = {
106 .minValue = minValueTemperature,
107 .maxValue = maxValueTemperature,
108 .offsetValue = 0.0,
109 .scaleValue = 1.0,
110 .units = sensor_paths::unitDegreesC,
111 .typeName = "temperature"};
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500112
113 // For IIO RAW sensors we get a raw_value, an offset, and scale
114 // to compute the value = (raw_value + offset) * scale
115 // with a _raw IIO device we need to get the
116 // offsetValue and scaleValue from the driver
117 // these are used to compute the reading in
118 // units that have yet to be scaled for D-Bus.
119 const std::string pathStr = path.string();
120 if (pathStr.ends_with("_raw"))
121 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400122 std::string pathOffsetStr =
123 pathStr.substr(0, pathStr.size() - 4) + "_offset";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500124 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
125 // In case there is nothing to read skip this device
126 // This is not an error condition see lore
127 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
128 if (tmpOffsetValue)
129 {
130 tmpSensorParameters.offsetValue = *tmpOffsetValue;
131 }
132
Patrick Williams2aaf7172024-08-16 15:20:40 -0400133 std::string pathScaleStr =
134 pathStr.substr(0, pathStr.size() - 4) + "_scale";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500135 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
136 // In case there is nothing to read skip this device
137 // This is not an error condition see lore
138 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
139 if (tmpScaleValue)
140 {
141 tmpSensorParameters.scaleValue = *tmpScaleValue;
142 }
143 }
144
145 // Temperatures are read in milli degrees Celsius, we need
146 // degrees Celsius. Pressures are read in kilopascal, we need
147 // Pascals. On D-Bus for Open BMC we use the International
148 // System of Units without prefixes. Links to the kernel
149 // documentation:
150 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
151 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
152 if (path.filename() == "in_pressure_input" ||
153 path.filename() == "in_pressure_raw")
154 {
155 tmpSensorParameters.minValue = minValuePressure;
156 tmpSensorParameters.maxValue = maxValuePressure;
157 // Pressures are read in kilopascal, we need Pascals.
158 tmpSensorParameters.scaleValue *= 1000.0;
159 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600160 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500161 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600162 else if (path.filename() == "in_humidityrelative_input" ||
163 path.filename() == "in_humidityrelative_raw")
164 {
165 tmpSensorParameters.minValue = minValueRelativeHumidity;
166 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
167 // Relative Humidity are read in milli-percent, we need percent.
168 tmpSensorParameters.scaleValue *= 0.001;
169 tmpSensorParameters.typeName = "humidity";
Potin Laia9c1bec2024-04-19 08:35:56 +0800170 tmpSensorParameters.units = sensor_paths::unitPercentRH;
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600171 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500172 else
173 {
174 // Temperatures are read in milli degrees Celsius,
175 // we need degrees Celsius.
176 tmpSensorParameters.scaleValue *= 0.001;
177 }
178
179 return tmpSensorParameters;
180}
181
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530182struct SensorConfigKey
183{
184 uint64_t bus;
185 uint64_t addr;
186 bool operator<(const SensorConfigKey& other) const
187 {
188 if (bus != other.bus)
189 {
190 return bus < other.bus;
191 }
192 return addr < other.addr;
193 }
194};
195
196struct SensorConfig
197{
198 std::string sensorPath;
199 SensorData sensorData;
200 std::string interface;
201 SensorBaseConfigMap config;
202 std::vector<std::string> name;
203};
204
205using SensorConfigMap =
206 boost::container::flat_map<SensorConfigKey, SensorConfig>;
207
Patrick Williams556e04b2025-02-01 08:22:22 -0500208static SensorConfigMap buildSensorConfigMap(
209 const ManagedObjectType& sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530210{
211 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700212 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530213 {
Zev Weissa1808332022-08-12 18:21:01 -0700214 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530215 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530216 auto busCfg = cfg.find("Bus");
217 auto addrCfg = cfg.find("Address");
218 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
219 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530220 continue;
221 }
222
Ed Tanous2049bd22022-07-09 07:20:26 -0700223 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
224 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530225 {
Zev Weissa1808332022-08-12 18:21:01 -0700226 std::cerr << path.str << " Bus or Address invalid\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530227 continue;
228 }
229
230 std::vector<std::string> hwmonNames;
231 auto nameCfg = cfg.find("Name");
232 if (nameCfg != cfg.end())
233 {
234 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
235 size_t i = 1;
236 while (true)
237 {
238 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
239 if (sensorNameCfg == cfg.end())
240 {
241 break;
242 }
243 hwmonNames.push_back(
244 std::get<std::string>(sensorNameCfg->second));
245 i++;
246 }
247 }
248
249 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
250 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700251 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530252
253 auto [it, inserted] = configMap.emplace(key, std::move(val));
254 if (!inserted)
255 {
Zev Weissa1808332022-08-12 18:21:01 -0700256 std::cerr << path.str << ": ignoring duplicate entry for {"
257 << key.bus << ", 0x" << std::hex << key.addr
258 << std::dec << "}\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530259 }
260 }
261 }
262 return configMap;
263}
264
James Feist6714a252018-09-10 15:26:18 -0700265void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800266 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800267 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700268 sensors,
269 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700270 const std::shared_ptr<boost::container::flat_set<std::string>>&
Zev Weissa1456c42022-07-18 16:59:35 -0700271 sensorsChanged,
272 bool activateOnly)
James Feist6714a252018-09-10 15:26:18 -0700273{
James Feistdf515152019-09-18 16:40:40 -0700274 auto getter = std::make_shared<GetSensorConfiguration>(
275 dbusConnection,
Zev Weissa1456c42022-07-18 16:59:35 -0700276 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
277 activateOnly](const ManagedObjectType& sensorConfigurations) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400278 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700279
Patrick Williams2aaf7172024-08-16 15:20:40 -0400280 SensorConfigMap configMap =
281 buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530282
Patrick Williams2aaf7172024-08-16 15:20:40 -0400283 auto devices =
284 instantiateDevices(sensorConfigurations, sensors, sensorTypes);
Zev Weissa1456c42022-07-18 16:59:35 -0700285
Patrick Williams2aaf7172024-08-16 15:20:40 -0400286 // IIO _raw devices look like this on sysfs:
287 // /sys/bus/iio/devices/iio:device0/in_temp_raw
288 // /sys/bus/iio/devices/iio:device0/in_temp_offset
289 // /sys/bus/iio/devices/iio:device0/in_temp_scale
290 //
291 // Other IIO devices look like this on sysfs:
292 // /sys/bus/iio/devices/iio:device1/in_temp_input
293 // /sys/bus/iio/devices/iio:device1/in_pressure_input
Ed Tanous2e466962025-01-30 10:59:49 -0800294 std::vector<std::filesystem::path> paths;
295 std::filesystem::path root("/sys/bus/iio/devices");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400296 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
297 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
298 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
Ed Tanous2e466962025-01-30 10:59:49 -0800299 findFiles(std::filesystem::path("/sys/class/hwmon"),
300 R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500301
Patrick Williams2aaf7172024-08-16 15:20:40 -0400302 // iterate through all found temp and pressure sensors,
303 // and try to match them with configuration
304 for (auto& path : paths)
James Feist37266ca2018-10-15 15:56:28 -0700305 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400306 std::smatch match;
307 const std::string pathStr = path.string();
308 auto directory = path.parent_path();
Ed Tanous2e466962025-01-30 10:59:49 -0800309 std::filesystem::path device;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400310
311 std::string deviceName;
312 std::error_code ec;
313 if (pathStr.starts_with("/sys/bus/iio/devices"))
Zhikui Renae463962024-01-02 15:35:31 -0800314 {
Ed Tanous2e466962025-01-30 10:59:49 -0800315 device = std::filesystem::canonical(directory, ec);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400316 if (ec)
Ed Tanousbb679322022-05-16 16:10:00 -0700317 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400318 std::cerr << "Fail to find device in path [" << pathStr
319 << "]\n";
320 continue;
Ed Tanousbb679322022-05-16 16:10:00 -0700321 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400322 deviceName = device.parent_path().stem();
Zev Weissa1456c42022-07-18 16:59:35 -0700323 }
324 else
325 {
Ed Tanous2e466962025-01-30 10:59:49 -0800326 device =
327 std::filesystem::canonical(directory / "device", ec);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400328 if (ec)
329 {
330 std::cerr << "Fail to find device in path [" << pathStr
331 << "]\n";
332 continue;
333 }
334 deviceName = device.stem();
Zev Weissa1456c42022-07-18 16:59:35 -0700335 }
Ed Tanousbb679322022-05-16 16:10:00 -0700336
Patrick Williams2aaf7172024-08-16 15:20:40 -0400337 uint64_t bus = 0;
338 uint64_t addr = 0;
339 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feist37266ca2018-10-15 15:56:28 -0700340 {
James Feistdf515152019-09-18 16:40:40 -0700341 continue;
342 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400343
344 auto thisSensorParameters = getSensorParameters(path);
345 auto findSensorCfg = configMap.find({bus, addr});
346 if (findSensorCfg == configMap.end())
347 {
348 continue;
349 }
350
351 const std::string& interfacePath =
352 findSensorCfg->second.sensorPath;
353 auto findI2CDev = devices.find(interfacePath);
354
355 std::shared_ptr<I2CDevice> i2cDev;
356 if (findI2CDev != devices.end())
357 {
358 // If we're only looking to activate newly-instantiated i2c
359 // devices and this sensor's underlying device was already
360 // there before this call, there's nothing more to do here.
361 if (activateOnly && !findI2CDev->second.second)
362 {
363 continue;
364 }
365 i2cDev = findI2CDev->second.first;
366 }
367
368 const SensorData& sensorData = findSensorCfg->second.sensorData;
369 std::string sensorType = findSensorCfg->second.interface;
370 auto pos = sensorType.find_last_of('.');
371 if (pos != std::string::npos)
372 {
373 sensorType = sensorType.substr(pos + 1);
374 }
375 const SensorBaseConfigMap& baseConfigMap =
376 findSensorCfg->second.config;
377 std::vector<std::string>& hwmonName =
378 findSensorCfg->second.name;
379
380 // Temperature has "Name", pressure has "Name1"
381 auto findSensorName = baseConfigMap.find("Name");
382 int index = 1;
383 if (thisSensorParameters.typeName == "pressure" ||
384 thisSensorParameters.typeName == "humidity")
385 {
386 findSensorName = baseConfigMap.find("Name1");
387 index = 2;
388 }
389
390 if (findSensorName == baseConfigMap.end())
391 {
392 std::cerr << "could not determine configuration name for "
393 << deviceName << "\n";
394 continue;
395 }
396 std::string sensorName =
397 std::get<std::string>(findSensorName->second);
398 // on rescans, only update sensors we were signaled by
399 auto findSensor = sensors.find(sensorName);
400 if (!firstScan && findSensor != sensors.end())
401 {
402 bool found = false;
403 auto it = sensorsChanged->begin();
404 while (it != sensorsChanged->end())
405 {
406 if (it->ends_with(findSensor->second->name))
407 {
408 it = sensorsChanged->erase(it);
409 findSensor->second = nullptr;
410 found = true;
411 break;
412 }
413 ++it;
414 }
415 if (!found)
416 {
417 continue;
418 }
419 }
420
421 std::vector<thresholds::Threshold> sensorThresholds;
422
423 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
424 nullptr, &index))
425 {
426 std::cerr << "error populating thresholds for "
427 << sensorName << " index " << index << "\n";
428 }
429
430 float pollRate = getPollRate(baseConfigMap, pollRateDefault);
431 PowerState readState = getPowerState(baseConfigMap);
432
433 auto permitSet = getPermitSet(baseConfigMap);
434 auto& sensor = sensors[sensorName];
435 if (!activateOnly)
436 {
437 sensor = nullptr;
438 }
439 auto hwmonFile = getFullHwmonFilePath(directory.string(),
440 "temp1", permitSet);
441 if (pathStr.starts_with("/sys/bus/iio/devices"))
442 {
443 hwmonFile = pathStr;
444 }
Jason Ling100c20b2020-08-11 14:50:33 -0700445 if (hwmonFile)
446 {
Zev Weissa1456c42022-07-18 16:59:35 -0700447 if (sensor != nullptr)
448 {
449 sensor->activate(*hwmonFile, i2cDev);
450 }
451 else
452 {
453 sensor = std::make_shared<HwmonTempSensor>(
454 *hwmonFile, sensorType, objectServer,
455 dbusConnection, io, sensorName,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400456 std::move(sensorThresholds), thisSensorParameters,
Zev Weissa1456c42022-07-18 16:59:35 -0700457 pollRate, interfacePath, readState, i2cDev);
458 sensor->setupRead();
459 }
Jason Ling100c20b2020-08-11 14:50:33 -0700460 }
Willy Tu9eb0cc32022-04-06 11:03:32 -0700461 hwmonName.erase(
462 remove(hwmonName.begin(), hwmonName.end(), sensorName),
463 hwmonName.end());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400464
465 // Looking for keys like "Name1" for temp2_input,
466 // "Name2" for temp3_input, etc.
467 int i = 0;
468 while (true)
469 {
470 ++i;
471 auto findKey =
472 baseConfigMap.find("Name" + std::to_string(i));
473 if (findKey == baseConfigMap.end())
474 {
475 break;
476 }
477 std::string sensorName =
478 std::get<std::string>(findKey->second);
479 hwmonFile = getFullHwmonFilePath(
480 directory.string(), "temp" + std::to_string(i + 1),
481 permitSet);
482 if (pathStr.starts_with("/sys/bus/iio/devices"))
483 {
484 continue;
485 }
486 if (hwmonFile)
487 {
488 // To look up thresholds for these additional sensors,
489 // match on the Index property in the threshold data
490 // where the index comes from the sysfs file we're on,
491 // i.e. index = 2 for temp2_input.
492 int index = i + 1;
493 std::vector<thresholds::Threshold> thresholds;
494
495 if (!parseThresholdsFromConfig(sensorData, thresholds,
496 nullptr, &index))
497 {
498 std::cerr
499 << "error populating thresholds for "
500 << sensorName << " index " << index << "\n";
501 }
502
503 auto& sensor = sensors[sensorName];
504 if (!activateOnly)
505 {
506 sensor = nullptr;
507 }
508
509 if (sensor != nullptr)
510 {
511 sensor->activate(*hwmonFile, i2cDev);
512 }
513 else
514 {
515 sensor = std::make_shared<HwmonTempSensor>(
516 *hwmonFile, sensorType, objectServer,
517 dbusConnection, io, sensorName,
518 std::move(thresholds), thisSensorParameters,
519 pollRate, interfacePath, readState, i2cDev);
520 sensor->setupRead();
521 }
522 }
523
524 hwmonName.erase(
525 remove(hwmonName.begin(), hwmonName.end(), sensorName),
526 hwmonName.end());
527 }
528 if (hwmonName.empty())
529 {
530 configMap.erase(findSensorCfg);
531 }
James Feistdf515152019-09-18 16:40:40 -0700532 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400533 });
Zev Weissd29f8aa2022-08-03 16:24:59 -0700534 std::vector<std::string> types(sensorTypes.size());
535 for (const auto& [type, dt] : sensorTypes)
536 {
537 types.push_back(type);
538 }
539 getter->getConfiguration(types);
James Feist6714a252018-09-10 15:26:18 -0700540}
541
Matt Spinler20bf2c12021-09-14 11:07:07 -0500542void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500543 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500544 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
545 sensors)
546{
547 if (message.is_method_error())
548 {
549 std::cerr << "interfacesRemoved callback method error\n";
550 return;
551 }
552
553 sdbusplus::message::object_path path;
554 std::vector<std::string> interfaces;
555
556 message.read(path, interfaces);
557
558 // If the xyz.openbmc_project.Confguration.X interface was removed
559 // for one or more sensors, delete those sensor objects.
560 auto sensorIt = sensors.begin();
561 while (sensorIt != sensors.end())
562 {
Patrick Rudolph6289f5a2023-09-22 11:32:54 +0200563 if (sensorIt->second && (sensorIt->second->configurationPath == path) &&
Matt Spinler20bf2c12021-09-14 11:07:07 -0500564 (std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500565 sensorIt->second->configInterface) != interfaces.end()))
Matt Spinler20bf2c12021-09-14 11:07:07 -0500566 {
567 sensorIt = sensors.erase(sensorIt);
568 }
569 else
570 {
571 sensorIt++;
572 }
573 }
574}
575
Zev Weissa1456c42022-07-18 16:59:35 -0700576static void powerStateChanged(
577 PowerState type, bool newState,
578 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
579 sensors,
Ed Tanous1f978632023-02-28 18:16:39 -0800580 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zev Weissa1456c42022-07-18 16:59:35 -0700581 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
582{
583 if (newState)
584 {
585 createSensors(io, objectServer, sensors, dbusConnection, nullptr, true);
586 }
587 else
588 {
589 for (auto& [path, sensor] : sensors)
590 {
591 if (sensor != nullptr && sensor->readState == type)
592 {
593 sensor->deactivate();
594 }
595 }
596 }
597}
598
James Feistb6c0b912019-07-09 12:21:44 -0700599int main()
James Feist6714a252018-09-10 15:26:18 -0700600{
Ed Tanous1f978632023-02-28 18:16:39 -0800601 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700602 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700603 sdbusplus::asio::object_server objectServer(systemBus, true);
604 objectServer.add_manager("/xyz/openbmc_project/sensors");
James Feist6714a252018-09-10 15:26:18 -0700605 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700606
Yong Lif3fd1912020-03-25 21:35:23 +0800607 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700608 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700609 auto sensorsChanged =
610 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700611
Zev Weissa1456c42022-07-18 16:59:35 -0700612 auto powerCallBack = [&sensors, &io, &objectServer,
613 &systemBus](PowerState type, bool state) {
614 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
615 };
616 setupPowerMatchCallback(systemBus, powerCallBack);
617
Ed Tanous83db50c2023-03-01 10:20:24 -0800618 boost::asio::post(io, [&]() {
Zev Weissa1456c42022-07-18 16:59:35 -0700619 createSensors(io, objectServer, sensors, systemBus, nullptr, false);
James Feist6714a252018-09-10 15:26:18 -0700620 });
621
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700622 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500623 std::function<void(sdbusplus::message_t&)> eventHandler =
624 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400625 if (message.is_method_error())
626 {
627 std::cerr << "callback method error\n";
628 return;
629 }
630 sensorsChanged->insert(message.get_path());
631 // this implicitly cancels the timer
632 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700633
Patrick Williams2aaf7172024-08-16 15:20:40 -0400634 filterTimer.async_wait([&](const boost::system::error_code& ec) {
635 if (ec == boost::asio::error::operation_aborted)
636 {
637 /* we were canceled*/
638 return;
639 }
640 if (ec)
641 {
642 std::cerr << "timer error\n";
643 return;
644 }
645 createSensors(io, objectServer, sensors, systemBus,
646 sensorsChanged, false);
647 });
648 };
James Feist6714a252018-09-10 15:26:18 -0700649
Zev Weiss214d9712022-08-12 12:54:31 -0700650 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
651 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800652 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500653
654 // Watch for entity-manager to remove configuration interfaces
655 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500656 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
657 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500658 "type='signal',member='InterfacesRemoved',arg0path='" +
659 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500660 [&sensors](sdbusplus::message_t& msg) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400661 interfaceRemoved(msg, sensors);
662 });
Matt Spinler20bf2c12021-09-14 11:07:07 -0500663
664 matches.emplace_back(std::move(ifaceRemovedMatch));
665
James Feist6714a252018-09-10 15:26:18 -0700666 io.run();
667}