blob: 8df8a8c53c34577952e8da16f29305992cabb532 [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>
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -070028#include <charconv>
James Feist24f02f22019-04-15 11:05:39 -070029#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070030#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <functional>
32#include <memory>
James Feist6714a252018-09-10 15:26:18 -070033#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <stdexcept>
35#include <string>
36#include <utility>
37#include <variant>
38#include <vector>
James Feist6714a252018-09-10 15:26:18 -070039
Jeff Lin87bc67f2020-12-04 20:58:01 +080040static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070041
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050042static constexpr double maxValuePressure = 120000; // Pascals
43static constexpr double minValuePressure = 30000; // Pascals
44
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060045static constexpr double maxValueRelativeHumidity = 100; // PercentRH
46static constexpr double minValueRelativeHumidity = 0; // PercentRH
47
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050048static constexpr double maxValueTemperature = 127; // DegreesC
49static constexpr double minValueTemperature = -128; // DegreesC
50
James Feistcf3bce62019-01-08 10:07:19 -080051namespace fs = std::filesystem;
Brandon Kim66558232021-11-09 16:53:08 -080052static auto sensorTypes{
Potin Lai47863342021-12-14 18:58:12 +080053 std::to_array<const char*>({"xyz.openbmc_project.Configuration.DPS310",
54 "xyz.openbmc_project.Configuration.EMC1412",
Brandon Kim66558232021-11-09 16:53:08 -080055 "xyz.openbmc_project.Configuration.EMC1413",
56 "xyz.openbmc_project.Configuration.EMC1414",
Potin Lai47863342021-12-14 18:58:12 +080057 "xyz.openbmc_project.Configuration.HDC1080",
58 "xyz.openbmc_project.Configuration.JC42",
59 "xyz.openbmc_project.Configuration.LM75A",
60 "xyz.openbmc_project.Configuration.LM95234",
Brandon Kim66558232021-11-09 16:53:08 -080061 "xyz.openbmc_project.Configuration.MAX31725",
62 "xyz.openbmc_project.Configuration.MAX31730",
63 "xyz.openbmc_project.Configuration.MAX6581",
64 "xyz.openbmc_project.Configuration.MAX6654",
65 "xyz.openbmc_project.Configuration.NCT7802",
66 "xyz.openbmc_project.Configuration.SBTSI",
Potin Lai47863342021-12-14 18:58:12 +080067 "xyz.openbmc_project.Configuration.SI7020",
Brandon Kim66558232021-11-09 16:53:08 -080068 "xyz.openbmc_project.Configuration.TMP112",
69 "xyz.openbmc_project.Configuration.TMP175",
70 "xyz.openbmc_project.Configuration.TMP421",
71 "xyz.openbmc_project.Configuration.TMP441",
Brandon Kim66558232021-11-09 16:53:08 -080072 "xyz.openbmc_project.Configuration.TMP75",
Potin Lai47863342021-12-14 18:58:12 +080073 "xyz.openbmc_project.Configuration.W83773G"})};
James Feist6714a252018-09-10 15:26:18 -070074
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050075static struct SensorParams
76 getSensorParameters(const std::filesystem::path& path)
77{
78 // offset is to default to 0 and scale to 1, see lore
79 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
80 struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature,
81 .maxValue = maxValueTemperature,
82 .offsetValue = 0.0,
83 .scaleValue = 1.0,
Bruce Mitchell5a86e562021-12-10 12:26:22 -060084 .units =
85 sensor_paths::unitDegreesC,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050086 .typeName = "temperature"};
87
88 // For IIO RAW sensors we get a raw_value, an offset, and scale
89 // to compute the value = (raw_value + offset) * scale
90 // with a _raw IIO device we need to get the
91 // offsetValue and scaleValue from the driver
92 // these are used to compute the reading in
93 // units that have yet to be scaled for D-Bus.
94 const std::string pathStr = path.string();
95 if (pathStr.ends_with("_raw"))
96 {
97 std::string pathOffsetStr =
98 pathStr.substr(0, pathStr.size() - 4) + "_offset";
99 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
100 // In case there is nothing to read skip this device
101 // This is not an error condition see lore
102 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
103 if (tmpOffsetValue)
104 {
105 tmpSensorParameters.offsetValue = *tmpOffsetValue;
106 }
107
108 std::string pathScaleStr =
109 pathStr.substr(0, pathStr.size() - 4) + "_scale";
110 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
111 // In case there is nothing to read skip this device
112 // This is not an error condition see lore
113 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
114 if (tmpScaleValue)
115 {
116 tmpSensorParameters.scaleValue = *tmpScaleValue;
117 }
118 }
119
120 // Temperatures are read in milli degrees Celsius, we need
121 // degrees Celsius. Pressures are read in kilopascal, we need
122 // Pascals. On D-Bus for Open BMC we use the International
123 // System of Units without prefixes. Links to the kernel
124 // documentation:
125 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
126 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
127 if (path.filename() == "in_pressure_input" ||
128 path.filename() == "in_pressure_raw")
129 {
130 tmpSensorParameters.minValue = minValuePressure;
131 tmpSensorParameters.maxValue = maxValuePressure;
132 // Pressures are read in kilopascal, we need Pascals.
133 tmpSensorParameters.scaleValue *= 1000.0;
134 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600135 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500136 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600137 else if (path.filename() == "in_humidityrelative_input" ||
138 path.filename() == "in_humidityrelative_raw")
139 {
140 tmpSensorParameters.minValue = minValueRelativeHumidity;
141 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
142 // Relative Humidity are read in milli-percent, we need percent.
143 tmpSensorParameters.scaleValue *= 0.001;
144 tmpSensorParameters.typeName = "humidity";
145 tmpSensorParameters.units = "PercentRH";
146 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500147 else
148 {
149 // Temperatures are read in milli degrees Celsius,
150 // we need degrees Celsius.
151 tmpSensorParameters.scaleValue *= 0.001;
152 }
153
154 return tmpSensorParameters;
155}
156
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530157struct SensorConfigKey
158{
159 uint64_t bus;
160 uint64_t addr;
161 bool operator<(const SensorConfigKey& other) const
162 {
163 if (bus != other.bus)
164 {
165 return bus < other.bus;
166 }
167 return addr < other.addr;
168 }
169};
170
171struct SensorConfig
172{
173 std::string sensorPath;
174 SensorData sensorData;
175 std::string interface;
176 SensorBaseConfigMap config;
177 std::vector<std::string> name;
178};
179
180using SensorConfigMap =
181 boost::container::flat_map<SensorConfigKey, SensorConfig>;
182
183static SensorConfigMap
184 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
185{
186 SensorConfigMap configMap;
187 for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
188 sensorConfigs)
189 {
190 for (const std::pair<std::string, SensorBaseConfigMap>& cfgmap :
191 sensor.second)
192 {
193 const SensorBaseConfigMap& cfg = cfgmap.second;
194 auto busCfg = cfg.find("Bus");
195 auto addrCfg = cfg.find("Address");
196 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
197 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530198 continue;
199 }
200
201 if ((!std::get_if<uint64_t>(&busCfg->second)) ||
202 (!std::get_if<uint64_t>(&addrCfg->second)))
203 {
204 std::cerr << sensor.first.str << " Bus or Address invalid\n";
205 continue;
206 }
207
208 std::vector<std::string> hwmonNames;
209 auto nameCfg = cfg.find("Name");
210 if (nameCfg != cfg.end())
211 {
212 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
213 size_t i = 1;
214 while (true)
215 {
216 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
217 if (sensorNameCfg == cfg.end())
218 {
219 break;
220 }
221 hwmonNames.push_back(
222 std::get<std::string>(sensorNameCfg->second));
223 i++;
224 }
225 }
226
227 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
228 std::get<uint64_t>(addrCfg->second)};
229 SensorConfig val = {sensor.first.str, sensor.second, cfgmap.first,
230 cfg, hwmonNames};
231
232 auto [it, inserted] = configMap.emplace(key, std::move(val));
233 if (!inserted)
234 {
235 std::cerr << sensor.first.str
236 << ": ignoring duplicate entry for {" << key.bus
237 << ", 0x" << std::hex << key.addr << std::dec
238 << "}\n";
239 }
240 }
241 }
242 return configMap;
243}
244
James Feist6714a252018-09-10 15:26:18 -0700245void createSensors(
246 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800247 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700248 sensors,
249 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700250 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -0700251 sensorsChanged)
252{
James Feistdf515152019-09-18 16:40:40 -0700253 auto getter = std::make_shared<GetSensorConfiguration>(
254 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700255 [&io, &objectServer, &sensors, &dbusConnection,
256 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
James Feistdf515152019-09-18 16:40:40 -0700257 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700258
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530259 SensorConfigMap configMap =
260 buildSensorConfigMap(sensorConfigurations);
261
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500262 // IIO _raw devices look like this on sysfs:
263 // /sys/bus/iio/devices/iio:device0/in_temp_raw
264 // /sys/bus/iio/devices/iio:device0/in_temp_offset
265 // /sys/bus/iio/devices/iio:device0/in_temp_scale
266 //
267 // Other IIO devices look like this on sysfs:
268 // /sys/bus/iio/devices/iio:device1/in_temp_input
269 // /sys/bus/iio/devices/iio:device1/in_pressure_input
James Feistdf515152019-09-18 16:40:40 -0700270 std::vector<fs::path> paths;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500271 fs::path root("/sys/bus/iio/devices");
272 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
273 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600274 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500275 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
276
277 if (paths.empty())
James Feist37266ca2018-10-15 15:56:28 -0700278 {
James Feistdf515152019-09-18 16:40:40 -0700279 return;
280 }
281
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500282 // iterate through all found temp and pressure sensors,
283 // and try to match them with configuration
James Feistdf515152019-09-18 16:40:40 -0700284 for (auto& path : paths)
285 {
286 std::smatch match;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500287 const std::string pathStr = path.string();
James Feistdf515152019-09-18 16:40:40 -0700288 auto directory = path.parent_path();
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500289 fs::path device;
James Feistdf515152019-09-18 16:40:40 -0700290
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500291 std::string deviceName;
292 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700293 {
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500294 device = fs::canonical(directory);
295 deviceName = device.parent_path().stem();
James Feistdf515152019-09-18 16:40:40 -0700296 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500297 else
298 {
299 device = directory / "device";
300 deviceName = fs::canonical(device).stem();
301 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700302 auto findHyphen = deviceName.find('-');
James Feistdf515152019-09-18 16:40:40 -0700303 if (findHyphen == std::string::npos)
304 {
305 std::cerr << "found bad device " << deviceName << "\n";
306 continue;
307 }
308 std::string busStr = deviceName.substr(0, findHyphen);
309 std::string addrStr = deviceName.substr(findHyphen + 1);
310
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -0700311 uint64_t bus = 0;
312 uint64_t addr = 0;
313 std::from_chars_result res;
314 res = std::from_chars(busStr.data(),
315 busStr.data() + busStr.size(), bus);
316 if (res.ec != std::errc{})
James Feistdf515152019-09-18 16:40:40 -0700317 {
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -0700318 continue;
James Feistdf515152019-09-18 16:40:40 -0700319 }
Jae Hyun Yoo7dd64432022-03-30 14:28:33 -0700320 res = std::from_chars(
321 addrStr.data(), addrStr.data() + addrStr.size(), addr, 16);
322 if (res.ec != std::errc{})
James Feistdf515152019-09-18 16:40:40 -0700323 {
324 continue;
325 }
James Feistdf515152019-09-18 16:40:40 -0700326
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500327 auto thisSensorParameters = getSensorParameters(path);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530328 auto findSensorCfg = configMap.find({bus, addr});
329 if (findSensorCfg == configMap.end())
James Feist6714a252018-09-10 15:26:18 -0700330 {
James Feistdf515152019-09-18 16:40:40 -0700331 continue;
James Feist6714a252018-09-10 15:26:18 -0700332 }
James Feist6714a252018-09-10 15:26:18 -0700333
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530334 const std::string& interfacePath =
335 findSensorCfg->second.sensorPath;
336 const SensorData& sensorData = findSensorCfg->second.sensorData;
337 const std::string& sensorType = findSensorCfg->second.interface;
338 const SensorBaseConfigMap& baseConfigMap =
339 findSensorCfg->second.config;
340 std::vector<std::string>& hwmonName =
341 findSensorCfg->second.name;
342
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500343 // Temperature has "Name", pressure has "Name1"
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530344 auto findSensorName = baseConfigMap.find("Name");
Potin Laifefdbe72022-02-19 00:30:12 +0800345 int index = 1;
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600346 if (thisSensorParameters.typeName == "pressure" ||
347 thisSensorParameters.typeName == "humidity")
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500348 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530349 findSensorName = baseConfigMap.find("Name1");
Potin Laifefdbe72022-02-19 00:30:12 +0800350 index = 2;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500351 }
352
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530353 if (findSensorName == baseConfigMap.end())
James Feistdf515152019-09-18 16:40:40 -0700354 {
355 std::cerr << "could not determine configuration name for "
356 << deviceName << "\n";
357 continue;
358 }
359 std::string sensorName =
360 std::get<std::string>(findSensorName->second);
361 // on rescans, only update sensors we were signaled by
362 auto findSensor = sensors.find(sensorName);
363 if (!firstScan && findSensor != sensors.end())
364 {
365 bool found = false;
Bruce Mitchelld653b752021-08-23 13:09:00 -0500366 auto it = sensorsChanged->begin();
367 while (it != sensorsChanged->end())
James Feistdf515152019-09-18 16:40:40 -0700368 {
369 if (boost::ends_with(*it, findSensor->second->name))
370 {
Bruce Mitchelld653b752021-08-23 13:09:00 -0500371 it = sensorsChanged->erase(it);
James Feistdf515152019-09-18 16:40:40 -0700372 findSensor->second = nullptr;
373 found = true;
374 break;
375 }
Bruce Mitchelld653b752021-08-23 13:09:00 -0500376 ++it;
James Feistdf515152019-09-18 16:40:40 -0700377 }
378 if (!found)
379 {
380 continue;
381 }
382 }
Matt Spinler5636d522021-03-17 14:52:18 -0500383
James Feistdf515152019-09-18 16:40:40 -0700384 std::vector<thresholds::Threshold> sensorThresholds;
Matt Spinler5636d522021-03-17 14:52:18 -0500385
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530386 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
Matt Spinler5636d522021-03-17 14:52:18 -0500387 nullptr, &index))
James Feistdf515152019-09-18 16:40:40 -0700388 {
389 std::cerr << "error populating thresholds for "
Potin Laifefdbe72022-02-19 00:30:12 +0800390 << sensorName << " index " << index << "\n";
James Feistdf515152019-09-18 16:40:40 -0700391 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800392
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530393 auto findPollRate = baseConfigMap.find("PollRate");
Jeff Lin87bc67f2020-12-04 20:58:01 +0800394 float pollRate = pollRateDefault;
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530395 if (findPollRate != baseConfigMap.end())
Jeff Lin87bc67f2020-12-04 20:58:01 +0800396 {
397 pollRate = std::visit(VariantToFloatVisitor(),
398 findPollRate->second);
399 if (pollRate <= 0.0f)
400 {
401 pollRate = pollRateDefault; // polling time too short
402 }
403 }
404
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530405 auto findPowerOn = baseConfigMap.find("PowerState");
James Feistf9b01b62020-01-29 15:21:58 -0800406 PowerState readState = PowerState::always;
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530407 if (findPowerOn != baseConfigMap.end())
James Feistf9b01b62020-01-29 15:21:58 -0800408 {
409 std::string powerState = std::visit(
410 VariantToStringVisitor(), findPowerOn->second);
411 setReadState(powerState, readState);
412 }
Jason Ling100c20b2020-08-11 14:50:33 -0700413
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530414 auto permitSet = getPermitSet(baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800415 auto& sensor = sensors[sensorName];
416 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700417 auto hwmonFile = getFullHwmonFilePath(directory.string(),
418 "temp1", permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500419 if (pathStr.starts_with("/sys/bus/iio/devices"))
420 {
421 hwmonFile = pathStr;
422 }
Jason Ling100c20b2020-08-11 14:50:33 -0700423 if (hwmonFile)
424 {
425 sensor = std::make_shared<HwmonTempSensor>(
426 *hwmonFile, sensorType, objectServer, dbusConnection,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500427 io, sensorName, std::move(sensorThresholds),
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530428 thisSensorParameters, pollRate, interfacePath,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500429 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700430 sensor->setupRead();
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530431 hwmonName.erase(
432 remove(hwmonName.begin(), hwmonName.end(), sensorName),
433 hwmonName.end());
Jason Ling100c20b2020-08-11 14:50:33 -0700434 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800435 // Looking for keys like "Name1" for temp2_input,
436 // "Name2" for temp3_input, etc.
437 int i = 0;
438 while (true)
James Feistdf515152019-09-18 16:40:40 -0700439 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800440 ++i;
441 auto findKey =
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530442 baseConfigMap.find("Name" + std::to_string(i));
443 if (findKey == baseConfigMap.end())
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800444 {
445 break;
446 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800447 std::string sensorName =
448 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700449 hwmonFile = getFullHwmonFilePath(
450 directory.string(), "temp" + std::to_string(i + 1),
451 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500452 if (pathStr.starts_with("/sys/bus/iio/devices"))
453 {
454 continue;
455 }
Jason Ling100c20b2020-08-11 14:50:33 -0700456 if (hwmonFile)
457 {
Matt Spinler5636d522021-03-17 14:52:18 -0500458 // To look up thresholds for these additional sensors,
459 // match on the Index property in the threshold data
460 // where the index comes from the sysfs file we're on,
461 // i.e. index = 2 for temp2_input.
462 int index = i + 1;
463 std::vector<thresholds::Threshold> thresholds;
464
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530465 if (!parseThresholdsFromConfig(sensorData, thresholds,
Matt Spinler5636d522021-03-17 14:52:18 -0500466 nullptr, &index))
467 {
468 std::cerr << "error populating thresholds for "
469 << sensorName << " index " << index
470 << "\n";
471 }
472
Jason Ling100c20b2020-08-11 14:50:33 -0700473 auto& sensor = sensors[sensorName];
474 sensor = nullptr;
475 sensor = std::make_shared<HwmonTempSensor>(
476 *hwmonFile, sensorType, objectServer,
477 dbusConnection, io, sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500478 std::move(thresholds), thisSensorParameters,
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530479 pollRate, interfacePath, readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700480 sensor->setupRead();
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530481 hwmonName.erase(remove(hwmonName.begin(),
482 hwmonName.end(), sensorName),
483 hwmonName.end());
Jason Ling100c20b2020-08-11 14:50:33 -0700484 }
James Feistdf515152019-09-18 16:40:40 -0700485 }
Matt Spinler31ec7db2022-04-11 16:49:40 -0500486 if (hwmonName.empty())
487 {
488 configMap.erase(findSensorCfg);
489 }
James Feistdf515152019-09-18 16:40:40 -0700490 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700491 });
James Feistdf515152019-09-18 16:40:40 -0700492 getter->getConfiguration(
493 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700494}
495
Matt Spinler20bf2c12021-09-14 11:07:07 -0500496void interfaceRemoved(
497 sdbusplus::message::message& message,
498 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
499 sensors)
500{
501 if (message.is_method_error())
502 {
503 std::cerr << "interfacesRemoved callback method error\n";
504 return;
505 }
506
507 sdbusplus::message::object_path path;
508 std::vector<std::string> interfaces;
509
510 message.read(path, interfaces);
511
512 // If the xyz.openbmc_project.Confguration.X interface was removed
513 // for one or more sensors, delete those sensor objects.
514 auto sensorIt = sensors.begin();
515 while (sensorIt != sensors.end())
516 {
517 if ((sensorIt->second->configurationPath == path) &&
518 (std::find(interfaces.begin(), interfaces.end(),
519 sensorIt->second->objectType) != interfaces.end()))
520 {
521 sensorIt = sensors.erase(sensorIt);
522 }
523 else
524 {
525 sensorIt++;
526 }
527 }
528}
529
James Feistb6c0b912019-07-09 12:21:44 -0700530int main()
James Feist6714a252018-09-10 15:26:18 -0700531{
532 boost::asio::io_service io;
533 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
534 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
535 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800536 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700537 sensors;
538 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700539 auto sensorsChanged =
540 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700541
542 io.post([&]() {
543 createSensors(io, objectServer, sensors, systemBus, nullptr);
544 });
545
546 boost::asio::deadline_timer filterTimer(io);
547 std::function<void(sdbusplus::message::message&)> eventHandler =
548 [&](sdbusplus::message::message& message) {
549 if (message.is_method_error())
550 {
551 std::cerr << "callback method error\n";
552 return;
553 }
554 sensorsChanged->insert(message.get_path());
555 // this implicitly cancels the timer
556 filterTimer.expires_from_now(boost::posix_time::seconds(1));
557
558 filterTimer.async_wait([&](const boost::system::error_code& ec) {
559 if (ec == boost::asio::error::operation_aborted)
560 {
561 /* we were canceled*/
562 return;
563 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700564 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700565 {
566 std::cerr << "timer error\n";
567 return;
568 }
569 createSensors(io, objectServer, sensors, systemBus,
570 sensorsChanged);
571 });
572 };
573
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700574 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700575 {
576 auto match = std::make_unique<sdbusplus::bus::match::match>(
577 static_cast<sdbusplus::bus::bus&>(*systemBus),
578 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700579 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700580 eventHandler);
581 matches.emplace_back(std::move(match));
582 }
583
Bruce Lee1263c3d2021-06-04 15:16:33 +0800584 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500585
586 // Watch for entity-manager to remove configuration interfaces
587 // so the corresponding sensors can be removed.
588 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>(
589 static_cast<sdbusplus::bus::bus&>(*systemBus),
590 "type='signal',member='InterfacesRemoved',arg0path='" +
591 std::string(inventoryPath) + "/'",
592 [&sensors](sdbusplus::message::message& msg) {
593 interfaceRemoved(msg, sensors);
594 });
595
596 matches.emplace_back(std::move(ifaceRemovedMatch));
597
James Feist6714a252018-09-10 15:26:18 -0700598 io.run();
599}