blob: 96eab9be882a1ba8d04a9a3335d3eb89831aa083 [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
James Feistcf3bce62019-01-08 10:07:19 -080065namespace fs = std::filesystem;
Zev Weissd29f8aa2022-08-03 16:24:59 -070066
67static const I2CDeviceTypeMap sensorTypes{
Patrick Rudolph76d36762023-09-20 16:53:38 +020068 {"ADM1021", I2CDeviceType{"adm1021", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070069 {"DPS310", I2CDeviceType{"dps310", false}},
70 {"EMC1412", I2CDeviceType{"emc1412", true}},
71 {"EMC1413", I2CDeviceType{"emc1413", true}},
72 {"EMC1414", I2CDeviceType{"emc1414", true}},
73 {"HDC1080", I2CDeviceType{"hdc1080", false}},
74 {"JC42", I2CDeviceType{"jc42", true}},
75 {"LM75A", I2CDeviceType{"lm75a", true}},
76 {"LM95234", I2CDeviceType{"lm95234", true}},
77 {"MAX31725", I2CDeviceType{"max31725", true}},
78 {"MAX31730", I2CDeviceType{"max31730", true}},
79 {"MAX6581", I2CDeviceType{"max6581", true}},
80 {"MAX6654", I2CDeviceType{"max6654", true}},
Patrick Rudolphf3862ee2023-10-23 14:13:06 +020081 {"MAX6639", I2CDeviceType{"max6639", true}},
Potin Lai28b88232023-12-13 16:41:01 +080082 {"MCP9600", I2CDeviceType{"mcp9600", false}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070083 {"NCT6779", I2CDeviceType{"nct6779", true}},
84 {"NCT7802", I2CDeviceType{"nct7802", true}},
Potin Laifb011cc2024-02-22 19:51:48 +080085 {"PT5161L", I2CDeviceType{"pt5161l", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070086 {"SBTSI", I2CDeviceType{"sbtsi", true}},
87 {"SI7020", I2CDeviceType{"si7020", false}},
Tim Lee9b0a6f62022-12-23 14:47:02 +080088 {"TMP100", I2CDeviceType{"tmp100", true}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070089 {"TMP112", I2CDeviceType{"tmp112", true}},
90 {"TMP175", I2CDeviceType{"tmp175", true}},
91 {"TMP421", I2CDeviceType{"tmp421", true}},
92 {"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}},
Zev Weissd29f8aa2022-08-03 16:24:59 -070095 {"TMP75", I2CDeviceType{"tmp75", true}},
96 {"W83773G", I2CDeviceType{"w83773g", true}},
97};
James Feist6714a252018-09-10 15:26:18 -070098
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050099static struct SensorParams
100 getSensorParameters(const std::filesystem::path& path)
101{
102 // offset is to default to 0 and scale to 1, see lore
103 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
Patrick Williams779c96a2023-05-10 07:50:42 -0500104 struct SensorParams tmpSensorParameters = {
105 .minValue = minValueTemperature,
106 .maxValue = maxValueTemperature,
107 .offsetValue = 0.0,
108 .scaleValue = 1.0,
109 .units = sensor_paths::unitDegreesC,
110 .typeName = "temperature"};
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500111
112 // For IIO RAW sensors we get a raw_value, an offset, and scale
113 // to compute the value = (raw_value + offset) * scale
114 // with a _raw IIO device we need to get the
115 // offsetValue and scaleValue from the driver
116 // these are used to compute the reading in
117 // units that have yet to be scaled for D-Bus.
118 const std::string pathStr = path.string();
119 if (pathStr.ends_with("_raw"))
120 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500121 std::string pathOffsetStr = pathStr.substr(0, pathStr.size() - 4) +
122 "_offset";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500123 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
124 // In case there is nothing to read skip this device
125 // This is not an error condition see lore
126 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
127 if (tmpOffsetValue)
128 {
129 tmpSensorParameters.offsetValue = *tmpOffsetValue;
130 }
131
Patrick Williams779c96a2023-05-10 07:50:42 -0500132 std::string pathScaleStr = pathStr.substr(0, pathStr.size() - 4) +
133 "_scale";
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500134 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
135 // In case there is nothing to read skip this device
136 // This is not an error condition see lore
137 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
138 if (tmpScaleValue)
139 {
140 tmpSensorParameters.scaleValue = *tmpScaleValue;
141 }
142 }
143
144 // Temperatures are read in milli degrees Celsius, we need
145 // degrees Celsius. Pressures are read in kilopascal, we need
146 // Pascals. On D-Bus for Open BMC we use the International
147 // System of Units without prefixes. Links to the kernel
148 // documentation:
149 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
150 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
151 if (path.filename() == "in_pressure_input" ||
152 path.filename() == "in_pressure_raw")
153 {
154 tmpSensorParameters.minValue = minValuePressure;
155 tmpSensorParameters.maxValue = maxValuePressure;
156 // Pressures are read in kilopascal, we need Pascals.
157 tmpSensorParameters.scaleValue *= 1000.0;
158 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600159 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500160 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600161 else if (path.filename() == "in_humidityrelative_input" ||
162 path.filename() == "in_humidityrelative_raw")
163 {
164 tmpSensorParameters.minValue = minValueRelativeHumidity;
165 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
166 // Relative Humidity are read in milli-percent, we need percent.
167 tmpSensorParameters.scaleValue *= 0.001;
168 tmpSensorParameters.typeName = "humidity";
Potin Laia9c1bec2024-04-19 08:35:56 +0800169 tmpSensorParameters.units = sensor_paths::unitPercentRH;
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600170 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500171 else
172 {
173 // Temperatures are read in milli degrees Celsius,
174 // we need degrees Celsius.
175 tmpSensorParameters.scaleValue *= 0.001;
176 }
177
178 return tmpSensorParameters;
179}
180
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530181struct SensorConfigKey
182{
183 uint64_t bus;
184 uint64_t addr;
185 bool operator<(const SensorConfigKey& other) const
186 {
187 if (bus != other.bus)
188 {
189 return bus < other.bus;
190 }
191 return addr < other.addr;
192 }
193};
194
195struct SensorConfig
196{
197 std::string sensorPath;
198 SensorData sensorData;
199 std::string interface;
200 SensorBaseConfigMap config;
201 std::vector<std::string> name;
202};
203
204using SensorConfigMap =
205 boost::container::flat_map<SensorConfigKey, SensorConfig>;
206
207static SensorConfigMap
208 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
209{
210 SensorConfigMap configMap;
Zev Weissa1808332022-08-12 18:21:01 -0700211 for (const auto& [path, cfgData] : sensorConfigs)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530212 {
Zev Weissa1808332022-08-12 18:21:01 -0700213 for (const auto& [intf, cfg] : cfgData)
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530214 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530215 auto busCfg = cfg.find("Bus");
216 auto addrCfg = cfg.find("Address");
217 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
218 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530219 continue;
220 }
221
Ed Tanous2049bd22022-07-09 07:20:26 -0700222 if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) ||
223 (std::get_if<uint64_t>(&addrCfg->second) == nullptr))
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530224 {
Zev Weissa1808332022-08-12 18:21:01 -0700225 std::cerr << path.str << " Bus or Address invalid\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530226 continue;
227 }
228
229 std::vector<std::string> hwmonNames;
230 auto nameCfg = cfg.find("Name");
231 if (nameCfg != cfg.end())
232 {
233 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
234 size_t i = 1;
235 while (true)
236 {
237 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
238 if (sensorNameCfg == cfg.end())
239 {
240 break;
241 }
242 hwmonNames.push_back(
243 std::get<std::string>(sensorNameCfg->second));
244 i++;
245 }
246 }
247
248 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
249 std::get<uint64_t>(addrCfg->second)};
Zev Weissa1808332022-08-12 18:21:01 -0700250 SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames};
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530251
252 auto [it, inserted] = configMap.emplace(key, std::move(val));
253 if (!inserted)
254 {
Zev Weissa1808332022-08-12 18:21:01 -0700255 std::cerr << path.str << ": ignoring duplicate entry for {"
256 << key.bus << ", 0x" << std::hex << key.addr
257 << std::dec << "}\n";
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530258 }
259 }
260 }
261 return configMap;
262}
263
James Feist6714a252018-09-10 15:26:18 -0700264void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800265 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800266 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700267 sensors,
268 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700269 const std::shared_ptr<boost::container::flat_set<std::string>>&
Zev Weissa1456c42022-07-18 16:59:35 -0700270 sensorsChanged,
271 bool activateOnly)
James Feist6714a252018-09-10 15:26:18 -0700272{
James Feistdf515152019-09-18 16:40:40 -0700273 auto getter = std::make_shared<GetSensorConfiguration>(
274 dbusConnection,
Zev Weissa1456c42022-07-18 16:59:35 -0700275 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
276 activateOnly](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700277 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700278
Ed Tanousbb679322022-05-16 16:10:00 -0700279 SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530280
Matt Simmeringc564eda2023-05-12 13:04:43 -0700281 auto devices = instantiateDevices(sensorConfigurations, sensors,
282 sensorTypes);
Zev Weissa1456c42022-07-18 16:59:35 -0700283
Ed Tanousbb679322022-05-16 16:10:00 -0700284 // IIO _raw devices look like this on sysfs:
285 // /sys/bus/iio/devices/iio:device0/in_temp_raw
286 // /sys/bus/iio/devices/iio:device0/in_temp_offset
287 // /sys/bus/iio/devices/iio:device0/in_temp_scale
288 //
289 // Other IIO devices look like this on sysfs:
290 // /sys/bus/iio/devices/iio:device1/in_temp_input
291 // /sys/bus/iio/devices/iio:device1/in_pressure_input
292 std::vector<fs::path> paths;
293 fs::path root("/sys/bus/iio/devices");
294 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
295 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
296 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
297 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500298
Ed Tanousbb679322022-05-16 16:10:00 -0700299 // iterate through all found temp and pressure sensors,
300 // and try to match them with configuration
301 for (auto& path : paths)
302 {
303 std::smatch match;
304 const std::string pathStr = path.string();
305 auto directory = path.parent_path();
306 fs::path device;
307
308 std::string deviceName;
Zhikui Renae463962024-01-02 15:35:31 -0800309 std::error_code ec;
Ed Tanousbb679322022-05-16 16:10:00 -0700310 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700311 {
Zhikui Renae463962024-01-02 15:35:31 -0800312 device = fs::canonical(directory, ec);
313 if (ec)
314 {
315 std::cerr << "Fail to find device in path [" << pathStr
316 << "]\n";
317 continue;
318 }
Ed Tanousbb679322022-05-16 16:10:00 -0700319 deviceName = device.parent_path().stem();
320 }
321 else
322 {
Zhikui Renae463962024-01-02 15:35:31 -0800323 device = fs::canonical(directory / "device", ec);
324 if (ec)
325 {
326 std::cerr << "Fail to find device in path [" << pathStr
327 << "]\n";
328 continue;
329 }
330 deviceName = device.stem();
Ed Tanousbb679322022-05-16 16:10:00 -0700331 }
Ed Tanousbb679322022-05-16 16:10:00 -0700332
Tom Tung278e1772023-12-12 09:20:40 +0800333 uint64_t bus = 0;
334 uint64_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000335 if (!getDeviceBusAddr(deviceName, bus, addr))
Ed Tanousbb679322022-05-16 16:10:00 -0700336 {
337 continue;
James Feistdf515152019-09-18 16:40:40 -0700338 }
339
Ed Tanousbb679322022-05-16 16:10:00 -0700340 auto thisSensorParameters = getSensorParameters(path);
341 auto findSensorCfg = configMap.find({bus, addr});
342 if (findSensorCfg == configMap.end())
James Feistdf515152019-09-18 16:40:40 -0700343 {
Ed Tanousbb679322022-05-16 16:10:00 -0700344 continue;
345 }
James Feistdf515152019-09-18 16:40:40 -0700346
Ed Tanousbb679322022-05-16 16:10:00 -0700347 const std::string& interfacePath = findSensorCfg->second.sensorPath;
Zev Weiss7627c862022-11-17 14:23:04 -0800348 auto findI2CDev = devices.find(interfacePath);
349
Zev Weissa1456c42022-07-18 16:59:35 -0700350 std::shared_ptr<I2CDevice> i2cDev;
Zev Weiss7627c862022-11-17 14:23:04 -0800351 if (findI2CDev != devices.end())
Zev Weissa1456c42022-07-18 16:59:35 -0700352 {
Joseph Fu9bbeff72022-12-05 19:09:16 +0800353 // If we're only looking to activate newly-instantiated i2c
354 // devices and this sensor's underlying device was already there
355 // before this call, there's nothing more to do here.
356 if (activateOnly && !findI2CDev->second.second)
357 {
358 continue;
359 }
Zev Weiss7627c862022-11-17 14:23:04 -0800360 i2cDev = findI2CDev->second.first;
Zev Weissa1456c42022-07-18 16:59:35 -0700361 }
362
Ed Tanousbb679322022-05-16 16:10:00 -0700363 const SensorData& sensorData = findSensorCfg->second.sensorData;
Matt Spinler0489ec22023-06-05 15:08:59 -0500364 std::string sensorType = findSensorCfg->second.interface;
365 auto pos = sensorType.find_last_of('.');
366 if (pos != std::string::npos)
367 {
368 sensorType = sensorType.substr(pos + 1);
369 }
Ed Tanousbb679322022-05-16 16:10:00 -0700370 const SensorBaseConfigMap& baseConfigMap =
371 findSensorCfg->second.config;
372 std::vector<std::string>& hwmonName = findSensorCfg->second.name;
373
374 // Temperature has "Name", pressure has "Name1"
375 auto findSensorName = baseConfigMap.find("Name");
376 int index = 1;
377 if (thisSensorParameters.typeName == "pressure" ||
378 thisSensorParameters.typeName == "humidity")
379 {
380 findSensorName = baseConfigMap.find("Name1");
381 index = 2;
382 }
383
384 if (findSensorName == baseConfigMap.end())
385 {
386 std::cerr << "could not determine configuration name for "
387 << deviceName << "\n";
388 continue;
389 }
390 std::string sensorName =
391 std::get<std::string>(findSensorName->second);
392 // on rescans, only update sensors we were signaled by
393 auto findSensor = sensors.find(sensorName);
394 if (!firstScan && findSensor != sensors.end())
395 {
396 bool found = false;
397 auto it = sensorsChanged->begin();
398 while (it != sensorsChanged->end())
399 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700400 if (it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700401 {
402 it = sensorsChanged->erase(it);
403 findSensor->second = nullptr;
404 found = true;
405 break;
406 }
407 ++it;
408 }
409 if (!found)
410 {
411 continue;
412 }
413 }
414
415 std::vector<thresholds::Threshold> sensorThresholds;
416
417 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
418 nullptr, &index))
419 {
420 std::cerr << "error populating thresholds for " << sensorName
421 << " index " << index << "\n";
422 }
423
Zev Weiss8569bf22022-10-11 15:37:44 -0700424 float pollRate = getPollRate(baseConfigMap, pollRateDefault);
Zev Weissa4d27682022-07-19 15:30:36 -0700425 PowerState readState = getPowerState(baseConfigMap);
Ed Tanousbb679322022-05-16 16:10:00 -0700426
427 auto permitSet = getPermitSet(baseConfigMap);
428 auto& sensor = sensors[sensorName];
Zev Weissa1456c42022-07-18 16:59:35 -0700429 if (!activateOnly)
430 {
431 sensor = nullptr;
432 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500433 auto hwmonFile = getFullHwmonFilePath(directory.string(), "temp1",
434 permitSet);
Ed Tanousbb679322022-05-16 16:10:00 -0700435 if (pathStr.starts_with("/sys/bus/iio/devices"))
436 {
437 hwmonFile = pathStr;
438 }
439 if (hwmonFile)
440 {
Zev Weissa1456c42022-07-18 16:59:35 -0700441 if (sensor != nullptr)
442 {
443 sensor->activate(*hwmonFile, i2cDev);
444 }
445 else
446 {
447 sensor = std::make_shared<HwmonTempSensor>(
448 *hwmonFile, sensorType, objectServer, dbusConnection,
449 io, sensorName, std::move(sensorThresholds),
450 thisSensorParameters, pollRate, interfacePath,
451 readState, i2cDev);
452 sensor->setupRead();
453 }
Ed Tanousbb679322022-05-16 16:10:00 -0700454 }
455 hwmonName.erase(
456 remove(hwmonName.begin(), hwmonName.end(), sensorName),
457 hwmonName.end());
458
459 // Looking for keys like "Name1" for temp2_input,
460 // "Name2" for temp3_input, etc.
461 int i = 0;
462 while (true)
463 {
464 ++i;
465 auto findKey = baseConfigMap.find("Name" + std::to_string(i));
466 if (findKey == baseConfigMap.end())
467 {
468 break;
469 }
470 std::string sensorName = std::get<std::string>(findKey->second);
471 hwmonFile = getFullHwmonFilePath(directory.string(),
472 "temp" + std::to_string(i + 1),
473 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500474 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700475 {
James Feistdf515152019-09-18 16:40:40 -0700476 continue;
477 }
Jason Ling100c20b2020-08-11 14:50:33 -0700478 if (hwmonFile)
479 {
Ed Tanousbb679322022-05-16 16:10:00 -0700480 // To look up thresholds for these additional sensors,
481 // match on the Index property in the threshold data
482 // where the index comes from the sysfs file we're on,
483 // i.e. index = 2 for temp2_input.
484 int index = i + 1;
485 std::vector<thresholds::Threshold> thresholds;
486
487 if (!parseThresholdsFromConfig(sensorData, thresholds,
488 nullptr, &index))
489 {
490 std::cerr << "error populating thresholds for "
491 << sensorName << " index " << index << "\n";
492 }
493
494 auto& sensor = sensors[sensorName];
Zev Weissa1456c42022-07-18 16:59:35 -0700495 if (!activateOnly)
496 {
497 sensor = nullptr;
498 }
499
500 if (sensor != nullptr)
501 {
502 sensor->activate(*hwmonFile, i2cDev);
503 }
504 else
505 {
506 sensor = std::make_shared<HwmonTempSensor>(
507 *hwmonFile, sensorType, objectServer,
508 dbusConnection, io, sensorName,
509 std::move(thresholds), thisSensorParameters,
510 pollRate, interfacePath, readState, i2cDev);
511 sensor->setupRead();
512 }
Jason Ling100c20b2020-08-11 14:50:33 -0700513 }
Ed Tanousbb679322022-05-16 16:10:00 -0700514
Willy Tu9eb0cc32022-04-06 11:03:32 -0700515 hwmonName.erase(
516 remove(hwmonName.begin(), hwmonName.end(), sensorName),
517 hwmonName.end());
James Feistdf515152019-09-18 16:40:40 -0700518 }
Ed Tanousbb679322022-05-16 16:10:00 -0700519 if (hwmonName.empty())
520 {
521 configMap.erase(findSensorCfg);
522 }
523 }
Patrick Williams597e8422023-10-20 11:19:01 -0500524 });
Zev Weissd29f8aa2022-08-03 16:24:59 -0700525 std::vector<std::string> types(sensorTypes.size());
526 for (const auto& [type, dt] : sensorTypes)
527 {
528 types.push_back(type);
529 }
530 getter->getConfiguration(types);
James Feist6714a252018-09-10 15:26:18 -0700531}
532
Matt Spinler20bf2c12021-09-14 11:07:07 -0500533void interfaceRemoved(
Patrick Williams92f8f512022-07-22 19:26:55 -0500534 sdbusplus::message_t& message,
Matt Spinler20bf2c12021-09-14 11:07:07 -0500535 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
536 sensors)
537{
538 if (message.is_method_error())
539 {
540 std::cerr << "interfacesRemoved callback method error\n";
541 return;
542 }
543
544 sdbusplus::message::object_path path;
545 std::vector<std::string> interfaces;
546
547 message.read(path, interfaces);
548
549 // If the xyz.openbmc_project.Confguration.X interface was removed
550 // for one or more sensors, delete those sensor objects.
551 auto sensorIt = sensors.begin();
552 while (sensorIt != sensors.end())
553 {
Patrick Rudolph6289f5a2023-09-22 11:32:54 +0200554 if (sensorIt->second && (sensorIt->second->configurationPath == path) &&
Matt Spinler20bf2c12021-09-14 11:07:07 -0500555 (std::find(interfaces.begin(), interfaces.end(),
Matt Spinler55832f32023-06-07 10:24:00 -0500556 sensorIt->second->configInterface) != interfaces.end()))
Matt Spinler20bf2c12021-09-14 11:07:07 -0500557 {
558 sensorIt = sensors.erase(sensorIt);
559 }
560 else
561 {
562 sensorIt++;
563 }
564 }
565}
566
Zev Weissa1456c42022-07-18 16:59:35 -0700567static void powerStateChanged(
568 PowerState type, bool newState,
569 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
570 sensors,
Ed Tanous1f978632023-02-28 18:16:39 -0800571 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zev Weissa1456c42022-07-18 16:59:35 -0700572 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
573{
574 if (newState)
575 {
576 createSensors(io, objectServer, sensors, dbusConnection, nullptr, true);
577 }
578 else
579 {
580 for (auto& [path, sensor] : sensors)
581 {
582 if (sensor != nullptr && sensor->readState == type)
583 {
584 sensor->deactivate();
585 }
586 }
587 }
588}
589
James Feistb6c0b912019-07-09 12:21:44 -0700590int main()
James Feist6714a252018-09-10 15:26:18 -0700591{
Ed Tanous1f978632023-02-28 18:16:39 -0800592 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700593 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700594 sdbusplus::asio::object_server objectServer(systemBus, true);
595 objectServer.add_manager("/xyz/openbmc_project/sensors");
James Feist6714a252018-09-10 15:26:18 -0700596 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700597
Yong Lif3fd1912020-03-25 21:35:23 +0800598 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700599 sensors;
James Feist5591cf082020-07-15 16:44:54 -0700600 auto sensorsChanged =
601 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700602
Zev Weissa1456c42022-07-18 16:59:35 -0700603 auto powerCallBack = [&sensors, &io, &objectServer,
604 &systemBus](PowerState type, bool state) {
605 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
606 };
607 setupPowerMatchCallback(systemBus, powerCallBack);
608
Ed Tanous83db50c2023-03-01 10:20:24 -0800609 boost::asio::post(io, [&]() {
Zev Weissa1456c42022-07-18 16:59:35 -0700610 createSensors(io, objectServer, sensors, systemBus, nullptr, false);
James Feist6714a252018-09-10 15:26:18 -0700611 });
612
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700613 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500614 std::function<void(sdbusplus::message_t&)> eventHandler =
615 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700616 if (message.is_method_error())
617 {
618 std::cerr << "callback method error\n";
619 return;
620 }
621 sensorsChanged->insert(message.get_path());
622 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800623 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700624
625 filterTimer.async_wait([&](const boost::system::error_code& ec) {
626 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700627 {
Ed Tanousbb679322022-05-16 16:10:00 -0700628 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700629 return;
630 }
Ed Tanousbb679322022-05-16 16:10:00 -0700631 if (ec)
632 {
633 std::cerr << "timer error\n";
634 return;
635 }
Zev Weissa1456c42022-07-18 16:59:35 -0700636 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
637 false);
Ed Tanousbb679322022-05-16 16:10:00 -0700638 });
639 };
James Feist6714a252018-09-10 15:26:18 -0700640
Zev Weiss214d9712022-08-12 12:54:31 -0700641 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
642 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800643 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500644
645 // Watch for entity-manager to remove configuration interfaces
646 // so the corresponding sensors can be removed.
Patrick Williams92f8f512022-07-22 19:26:55 -0500647 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
648 static_cast<sdbusplus::bus_t&>(*systemBus),
Matt Spinler20bf2c12021-09-14 11:07:07 -0500649 "type='signal',member='InterfacesRemoved',arg0path='" +
650 std::string(inventoryPath) + "/'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500651 [&sensors](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700652 interfaceRemoved(msg, sensors);
Patrick Williams597e8422023-10-20 11:19:01 -0500653 });
Matt Spinler20bf2c12021-09-14 11:07:07 -0500654
655 matches.emplace_back(std::move(ifaceRemovedMatch));
656
James Feist6714a252018-09-10 15:26:18 -0700657 io.run();
658}