blob: e5671b160898b6ba01bc7cf5257909e7cbb2d9dd [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <HwmonTempSensor.hpp>
18#include <Utils.hpp>
James Feist6714a252018-09-10 15:26:18 -070019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
27#include <array>
James Feist24f02f22019-04-15 11:05:39 -070028#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <functional>
31#include <memory>
James Feist6714a252018-09-10 15:26:18 -070032#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <stdexcept>
34#include <string>
35#include <utility>
36#include <variant>
37#include <vector>
James Feist6714a252018-09-10 15:26:18 -070038
Jeff Lin87bc67f2020-12-04 20:58:01 +080039static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070040
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050041static constexpr double maxValuePressure = 120000; // Pascals
42static constexpr double minValuePressure = 30000; // Pascals
43
Bruce Mitchell3ec41c52021-12-10 16:05:17 -060044static constexpr double maxValueRelativeHumidity = 100; // PercentRH
45static constexpr double minValueRelativeHumidity = 0; // PercentRH
46
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050047static constexpr double maxValueTemperature = 127; // DegreesC
48static constexpr double minValueTemperature = -128; // DegreesC
49
James Feistcf3bce62019-01-08 10:07:19 -080050namespace fs = std::filesystem;
Brandon Kim66558232021-11-09 16:53:08 -080051static auto sensorTypes{
Potin Lai47863342021-12-14 18:58:12 +080052 std::to_array<const char*>({"xyz.openbmc_project.Configuration.DPS310",
53 "xyz.openbmc_project.Configuration.EMC1412",
Brandon Kim66558232021-11-09 16:53:08 -080054 "xyz.openbmc_project.Configuration.EMC1413",
55 "xyz.openbmc_project.Configuration.EMC1414",
Potin Lai47863342021-12-14 18:58:12 +080056 "xyz.openbmc_project.Configuration.HDC1080",
57 "xyz.openbmc_project.Configuration.JC42",
58 "xyz.openbmc_project.Configuration.LM75A",
59 "xyz.openbmc_project.Configuration.LM95234",
Brandon Kim66558232021-11-09 16:53:08 -080060 "xyz.openbmc_project.Configuration.MAX31725",
61 "xyz.openbmc_project.Configuration.MAX31730",
62 "xyz.openbmc_project.Configuration.MAX6581",
63 "xyz.openbmc_project.Configuration.MAX6654",
64 "xyz.openbmc_project.Configuration.NCT7802",
65 "xyz.openbmc_project.Configuration.SBTSI",
Potin Lai47863342021-12-14 18:58:12 +080066 "xyz.openbmc_project.Configuration.SI7020",
Brandon Kim66558232021-11-09 16:53:08 -080067 "xyz.openbmc_project.Configuration.TMP112",
68 "xyz.openbmc_project.Configuration.TMP175",
69 "xyz.openbmc_project.Configuration.TMP421",
70 "xyz.openbmc_project.Configuration.TMP441",
Brandon Kim66558232021-11-09 16:53:08 -080071 "xyz.openbmc_project.Configuration.TMP75",
Potin Lai47863342021-12-14 18:58:12 +080072 "xyz.openbmc_project.Configuration.W83773G"})};
James Feist6714a252018-09-10 15:26:18 -070073
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050074static struct SensorParams
75 getSensorParameters(const std::filesystem::path& path)
76{
77 // offset is to default to 0 and scale to 1, see lore
78 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
79 struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature,
80 .maxValue = maxValueTemperature,
81 .offsetValue = 0.0,
82 .scaleValue = 1.0,
Bruce Mitchell5a86e562021-12-10 12:26:22 -060083 .units =
84 sensor_paths::unitDegreesC,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050085 .typeName = "temperature"};
86
87 // For IIO RAW sensors we get a raw_value, an offset, and scale
88 // to compute the value = (raw_value + offset) * scale
89 // with a _raw IIO device we need to get the
90 // offsetValue and scaleValue from the driver
91 // these are used to compute the reading in
92 // units that have yet to be scaled for D-Bus.
93 const std::string pathStr = path.string();
94 if (pathStr.ends_with("_raw"))
95 {
96 std::string pathOffsetStr =
97 pathStr.substr(0, pathStr.size() - 4) + "_offset";
98 std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0);
99 // In case there is nothing to read skip this device
100 // This is not an error condition see lore
101 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
102 if (tmpOffsetValue)
103 {
104 tmpSensorParameters.offsetValue = *tmpOffsetValue;
105 }
106
107 std::string pathScaleStr =
108 pathStr.substr(0, pathStr.size() - 4) + "_scale";
109 std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0);
110 // In case there is nothing to read skip this device
111 // This is not an error condition see lore
112 // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/
113 if (tmpScaleValue)
114 {
115 tmpSensorParameters.scaleValue = *tmpScaleValue;
116 }
117 }
118
119 // Temperatures are read in milli degrees Celsius, we need
120 // degrees Celsius. Pressures are read in kilopascal, we need
121 // Pascals. On D-Bus for Open BMC we use the International
122 // System of Units without prefixes. Links to the kernel
123 // documentation:
124 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
125 // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
126 if (path.filename() == "in_pressure_input" ||
127 path.filename() == "in_pressure_raw")
128 {
129 tmpSensorParameters.minValue = minValuePressure;
130 tmpSensorParameters.maxValue = maxValuePressure;
131 // Pressures are read in kilopascal, we need Pascals.
132 tmpSensorParameters.scaleValue *= 1000.0;
133 tmpSensorParameters.typeName = "pressure";
Bruce Mitchell5a86e562021-12-10 12:26:22 -0600134 tmpSensorParameters.units = sensor_paths::unitPascals;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500135 }
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600136 else if (path.filename() == "in_humidityrelative_input" ||
137 path.filename() == "in_humidityrelative_raw")
138 {
139 tmpSensorParameters.minValue = minValueRelativeHumidity;
140 tmpSensorParameters.maxValue = maxValueRelativeHumidity;
141 // Relative Humidity are read in milli-percent, we need percent.
142 tmpSensorParameters.scaleValue *= 0.001;
143 tmpSensorParameters.typeName = "humidity";
144 tmpSensorParameters.units = "PercentRH";
145 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500146 else
147 {
148 // Temperatures are read in milli degrees Celsius,
149 // we need degrees Celsius.
150 tmpSensorParameters.scaleValue *= 0.001;
151 }
152
153 return tmpSensorParameters;
154}
155
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530156struct SensorConfigKey
157{
158 uint64_t bus;
159 uint64_t addr;
160 bool operator<(const SensorConfigKey& other) const
161 {
162 if (bus != other.bus)
163 {
164 return bus < other.bus;
165 }
166 return addr < other.addr;
167 }
168};
169
170struct SensorConfig
171{
172 std::string sensorPath;
173 SensorData sensorData;
174 std::string interface;
175 SensorBaseConfigMap config;
176 std::vector<std::string> name;
177};
178
179using SensorConfigMap =
180 boost::container::flat_map<SensorConfigKey, SensorConfig>;
181
182static SensorConfigMap
183 buildSensorConfigMap(const ManagedObjectType& sensorConfigs)
184{
185 SensorConfigMap configMap;
186 for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
187 sensorConfigs)
188 {
189 for (const std::pair<std::string, SensorBaseConfigMap>& cfgmap :
190 sensor.second)
191 {
192 const SensorBaseConfigMap& cfg = cfgmap.second;
193 auto busCfg = cfg.find("Bus");
194 auto addrCfg = cfg.find("Address");
195 if ((busCfg == cfg.end()) || (addrCfg == cfg.end()))
196 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530197 continue;
198 }
199
200 if ((!std::get_if<uint64_t>(&busCfg->second)) ||
201 (!std::get_if<uint64_t>(&addrCfg->second)))
202 {
203 std::cerr << sensor.first.str << " Bus or Address invalid\n";
204 continue;
205 }
206
207 std::vector<std::string> hwmonNames;
208 auto nameCfg = cfg.find("Name");
209 if (nameCfg != cfg.end())
210 {
211 hwmonNames.push_back(std::get<std::string>(nameCfg->second));
212 size_t i = 1;
213 while (true)
214 {
215 auto sensorNameCfg = cfg.find("Name" + std::to_string(i));
216 if (sensorNameCfg == cfg.end())
217 {
218 break;
219 }
220 hwmonNames.push_back(
221 std::get<std::string>(sensorNameCfg->second));
222 i++;
223 }
224 }
225
226 SensorConfigKey key = {std::get<uint64_t>(busCfg->second),
227 std::get<uint64_t>(addrCfg->second)};
228 SensorConfig val = {sensor.first.str, sensor.second, cfgmap.first,
229 cfg, hwmonNames};
230
231 auto [it, inserted] = configMap.emplace(key, std::move(val));
232 if (!inserted)
233 {
234 std::cerr << sensor.first.str
235 << ": ignoring duplicate entry for {" << key.bus
236 << ", 0x" << std::hex << key.addr << std::dec
237 << "}\n";
238 }
239 }
240 }
241 return configMap;
242}
243
James Feist6714a252018-09-10 15:26:18 -0700244void createSensors(
245 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +0800246 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700247 sensors,
248 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700249 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -0700250 sensorsChanged)
251{
James Feistdf515152019-09-18 16:40:40 -0700252 auto getter = std::make_shared<GetSensorConfiguration>(
253 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700254 [&io, &objectServer, &sensors, &dbusConnection,
255 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
James Feistdf515152019-09-18 16:40:40 -0700256 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -0700257
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530258 SensorConfigMap configMap =
259 buildSensorConfigMap(sensorConfigurations);
260
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500261 // IIO _raw devices look like this on sysfs:
262 // /sys/bus/iio/devices/iio:device0/in_temp_raw
263 // /sys/bus/iio/devices/iio:device0/in_temp_offset
264 // /sys/bus/iio/devices/iio:device0/in_temp_scale
265 //
266 // Other IIO devices look like this on sysfs:
267 // /sys/bus/iio/devices/iio:device1/in_temp_input
268 // /sys/bus/iio/devices/iio:device1/in_pressure_input
James Feistdf515152019-09-18 16:40:40 -0700269 std::vector<fs::path> paths;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500270 fs::path root("/sys/bus/iio/devices");
271 findFiles(root, R"(in_temp\d*_(input|raw))", paths);
272 findFiles(root, R"(in_pressure\d*_(input|raw))", paths);
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600273 findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500274 findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths);
275
276 if (paths.empty())
James Feist37266ca2018-10-15 15:56:28 -0700277 {
James Feistdf515152019-09-18 16:40:40 -0700278 return;
279 }
280
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500281 // iterate through all found temp and pressure sensors,
282 // and try to match them with configuration
James Feistdf515152019-09-18 16:40:40 -0700283 for (auto& path : paths)
284 {
285 std::smatch match;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500286 const std::string pathStr = path.string();
James Feistdf515152019-09-18 16:40:40 -0700287 auto directory = path.parent_path();
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500288 fs::path device;
James Feistdf515152019-09-18 16:40:40 -0700289
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500290 std::string deviceName;
291 if (pathStr.starts_with("/sys/bus/iio/devices"))
James Feist37266ca2018-10-15 15:56:28 -0700292 {
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500293 device = fs::canonical(directory);
294 deviceName = device.parent_path().stem();
James Feistdf515152019-09-18 16:40:40 -0700295 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500296 else
297 {
298 device = directory / "device";
299 deviceName = fs::canonical(device).stem();
300 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700301 auto findHyphen = deviceName.find('-');
James Feistdf515152019-09-18 16:40:40 -0700302 if (findHyphen == std::string::npos)
303 {
304 std::cerr << "found bad device " << deviceName << "\n";
305 continue;
306 }
307 std::string busStr = deviceName.substr(0, findHyphen);
308 std::string addrStr = deviceName.substr(findHyphen + 1);
309
310 size_t bus = 0;
311 size_t addr = 0;
312 try
313 {
314 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700315 addr = std::stoi(addrStr, nullptr, 16);
James Feistdf515152019-09-18 16:40:40 -0700316 }
Patrick Williams26601e82021-10-06 12:43:25 -0500317 catch (const std::invalid_argument&)
James Feistdf515152019-09-18 16:40:40 -0700318 {
319 continue;
320 }
James Feistdf515152019-09-18 16:40:40 -0700321
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500322 auto thisSensorParameters = getSensorParameters(path);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530323 auto findSensorCfg = configMap.find({bus, addr});
324 if (findSensorCfg == configMap.end())
James Feist6714a252018-09-10 15:26:18 -0700325 {
James Feistdf515152019-09-18 16:40:40 -0700326 continue;
James Feist6714a252018-09-10 15:26:18 -0700327 }
James Feist6714a252018-09-10 15:26:18 -0700328
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530329 const std::string& interfacePath =
330 findSensorCfg->second.sensorPath;
331 const SensorData& sensorData = findSensorCfg->second.sensorData;
332 const std::string& sensorType = findSensorCfg->second.interface;
333 const SensorBaseConfigMap& baseConfigMap =
334 findSensorCfg->second.config;
335 std::vector<std::string>& hwmonName =
336 findSensorCfg->second.name;
337
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500338 // Temperature has "Name", pressure has "Name1"
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530339 auto findSensorName = baseConfigMap.find("Name");
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600340 if (thisSensorParameters.typeName == "pressure" ||
341 thisSensorParameters.typeName == "humidity")
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500342 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530343 findSensorName = baseConfigMap.find("Name1");
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500344 }
345
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530346 if (findSensorName == baseConfigMap.end())
James Feistdf515152019-09-18 16:40:40 -0700347 {
348 std::cerr << "could not determine configuration name for "
349 << deviceName << "\n";
350 continue;
351 }
352 std::string sensorName =
353 std::get<std::string>(findSensorName->second);
354 // on rescans, only update sensors we were signaled by
355 auto findSensor = sensors.find(sensorName);
356 if (!firstScan && findSensor != sensors.end())
357 {
358 bool found = false;
Bruce Mitchelld653b752021-08-23 13:09:00 -0500359 auto it = sensorsChanged->begin();
360 while (it != sensorsChanged->end())
James Feistdf515152019-09-18 16:40:40 -0700361 {
362 if (boost::ends_with(*it, findSensor->second->name))
363 {
Bruce Mitchelld653b752021-08-23 13:09:00 -0500364 it = sensorsChanged->erase(it);
James Feistdf515152019-09-18 16:40:40 -0700365 findSensor->second = nullptr;
366 found = true;
367 break;
368 }
Bruce Mitchelld653b752021-08-23 13:09:00 -0500369 ++it;
James Feistdf515152019-09-18 16:40:40 -0700370 }
371 if (!found)
372 {
373 continue;
374 }
375 }
Matt Spinler5636d522021-03-17 14:52:18 -0500376
James Feistdf515152019-09-18 16:40:40 -0700377 std::vector<thresholds::Threshold> sensorThresholds;
Matt Spinler5636d522021-03-17 14:52:18 -0500378 int index = 1;
379
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530380 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
Matt Spinler5636d522021-03-17 14:52:18 -0500381 nullptr, &index))
James Feistdf515152019-09-18 16:40:40 -0700382 {
383 std::cerr << "error populating thresholds for "
Matt Spinler5636d522021-03-17 14:52:18 -0500384 << sensorName << " index 1\n";
James Feistdf515152019-09-18 16:40:40 -0700385 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800386
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530387 auto findPollRate = baseConfigMap.find("PollRate");
Jeff Lin87bc67f2020-12-04 20:58:01 +0800388 float pollRate = pollRateDefault;
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530389 if (findPollRate != baseConfigMap.end())
Jeff Lin87bc67f2020-12-04 20:58:01 +0800390 {
391 pollRate = std::visit(VariantToFloatVisitor(),
392 findPollRate->second);
393 if (pollRate <= 0.0f)
394 {
395 pollRate = pollRateDefault; // polling time too short
396 }
397 }
398
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530399 auto findPowerOn = baseConfigMap.find("PowerState");
James Feistf9b01b62020-01-29 15:21:58 -0800400 PowerState readState = PowerState::always;
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530401 if (findPowerOn != baseConfigMap.end())
James Feistf9b01b62020-01-29 15:21:58 -0800402 {
403 std::string powerState = std::visit(
404 VariantToStringVisitor(), findPowerOn->second);
405 setReadState(powerState, readState);
406 }
Jason Ling100c20b2020-08-11 14:50:33 -0700407
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530408 auto permitSet = getPermitSet(baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800409 auto& sensor = sensors[sensorName];
410 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700411 auto hwmonFile = getFullHwmonFilePath(directory.string(),
412 "temp1", permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500413 if (pathStr.starts_with("/sys/bus/iio/devices"))
414 {
415 hwmonFile = pathStr;
416 }
Jason Ling100c20b2020-08-11 14:50:33 -0700417 if (hwmonFile)
418 {
419 sensor = std::make_shared<HwmonTempSensor>(
420 *hwmonFile, sensorType, objectServer, dbusConnection,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500421 io, sensorName, std::move(sensorThresholds),
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530422 thisSensorParameters, pollRate, interfacePath,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500423 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700424 sensor->setupRead();
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530425 hwmonName.erase(
426 remove(hwmonName.begin(), hwmonName.end(), sensorName),
427 hwmonName.end());
Jason Ling100c20b2020-08-11 14:50:33 -0700428 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800429 // Looking for keys like "Name1" for temp2_input,
430 // "Name2" for temp3_input, etc.
431 int i = 0;
432 while (true)
James Feistdf515152019-09-18 16:40:40 -0700433 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800434 ++i;
435 auto findKey =
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530436 baseConfigMap.find("Name" + std::to_string(i));
437 if (findKey == baseConfigMap.end())
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800438 {
439 break;
440 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800441 std::string sensorName =
442 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700443 hwmonFile = getFullHwmonFilePath(
444 directory.string(), "temp" + std::to_string(i + 1),
445 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500446 if (pathStr.starts_with("/sys/bus/iio/devices"))
447 {
448 continue;
449 }
Jason Ling100c20b2020-08-11 14:50:33 -0700450 if (hwmonFile)
451 {
Matt Spinler5636d522021-03-17 14:52:18 -0500452 // To look up thresholds for these additional sensors,
453 // match on the Index property in the threshold data
454 // where the index comes from the sysfs file we're on,
455 // i.e. index = 2 for temp2_input.
456 int index = i + 1;
457 std::vector<thresholds::Threshold> thresholds;
458
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530459 if (!parseThresholdsFromConfig(sensorData, thresholds,
Matt Spinler5636d522021-03-17 14:52:18 -0500460 nullptr, &index))
461 {
462 std::cerr << "error populating thresholds for "
463 << sensorName << " index " << index
464 << "\n";
465 }
466
Jason Ling100c20b2020-08-11 14:50:33 -0700467 auto& sensor = sensors[sensorName];
468 sensor = nullptr;
469 sensor = std::make_shared<HwmonTempSensor>(
470 *hwmonFile, sensorType, objectServer,
471 dbusConnection, io, sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500472 std::move(thresholds), thisSensorParameters,
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530473 pollRate, interfacePath, readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700474 sensor->setupRead();
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530475 hwmonName.erase(remove(hwmonName.begin(),
476 hwmonName.end(), sensorName),
477 hwmonName.end());
Jason Ling100c20b2020-08-11 14:50:33 -0700478 }
James Feistdf515152019-09-18 16:40:40 -0700479 }
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530480 if (hwmonName.empty())
481 {
482 configMap.erase(findSensorCfg);
483 }
James Feistdf515152019-09-18 16:40:40 -0700484 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700485 });
James Feistdf515152019-09-18 16:40:40 -0700486 getter->getConfiguration(
487 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700488}
489
Matt Spinler20bf2c12021-09-14 11:07:07 -0500490void interfaceRemoved(
491 sdbusplus::message::message& message,
492 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
493 sensors)
494{
495 if (message.is_method_error())
496 {
497 std::cerr << "interfacesRemoved callback method error\n";
498 return;
499 }
500
501 sdbusplus::message::object_path path;
502 std::vector<std::string> interfaces;
503
504 message.read(path, interfaces);
505
506 // If the xyz.openbmc_project.Confguration.X interface was removed
507 // for one or more sensors, delete those sensor objects.
508 auto sensorIt = sensors.begin();
509 while (sensorIt != sensors.end())
510 {
511 if ((sensorIt->second->configurationPath == path) &&
512 (std::find(interfaces.begin(), interfaces.end(),
513 sensorIt->second->objectType) != interfaces.end()))
514 {
515 sensorIt = sensors.erase(sensorIt);
516 }
517 else
518 {
519 sensorIt++;
520 }
521 }
522}
523
James Feistb6c0b912019-07-09 12:21:44 -0700524int main()
James Feist6714a252018-09-10 15:26:18 -0700525{
526 boost::asio::io_service io;
527 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
528 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
529 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800530 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700531 sensors;
532 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700533 auto sensorsChanged =
534 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700535
536 io.post([&]() {
537 createSensors(io, objectServer, sensors, systemBus, nullptr);
538 });
539
540 boost::asio::deadline_timer filterTimer(io);
541 std::function<void(sdbusplus::message::message&)> eventHandler =
542 [&](sdbusplus::message::message& message) {
543 if (message.is_method_error())
544 {
545 std::cerr << "callback method error\n";
546 return;
547 }
548 sensorsChanged->insert(message.get_path());
549 // this implicitly cancels the timer
550 filterTimer.expires_from_now(boost::posix_time::seconds(1));
551
552 filterTimer.async_wait([&](const boost::system::error_code& ec) {
553 if (ec == boost::asio::error::operation_aborted)
554 {
555 /* we were canceled*/
556 return;
557 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700558 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700559 {
560 std::cerr << "timer error\n";
561 return;
562 }
563 createSensors(io, objectServer, sensors, systemBus,
564 sensorsChanged);
565 });
566 };
567
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700568 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700569 {
570 auto match = std::make_unique<sdbusplus::bus::match::match>(
571 static_cast<sdbusplus::bus::bus&>(*systemBus),
572 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700573 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700574 eventHandler);
575 matches.emplace_back(std::move(match));
576 }
577
Bruce Lee1263c3d2021-06-04 15:16:33 +0800578 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500579
580 // Watch for entity-manager to remove configuration interfaces
581 // so the corresponding sensors can be removed.
582 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>(
583 static_cast<sdbusplus::bus::bus&>(*systemBus),
584 "type='signal',member='InterfacesRemoved',arg0path='" +
585 std::string(inventoryPath) + "/'",
586 [&sensors](sdbusplus::message::message& msg) {
587 interfaceRemoved(msg, sensors);
588 });
589
590 matches.emplace_back(std::move(ifaceRemovedMatch));
591
James Feist6714a252018-09-10 15:26:18 -0700592 io.run();
593}