blob: f1f4a1fb78f2596f345bc17df38e678688b10be9 [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 {
197 std::cerr << sensor.first.str << " missing Bus or Address\n";
198 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
311 size_t bus = 0;
312 size_t addr = 0;
313 try
314 {
315 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700316 addr = std::stoi(addrStr, nullptr, 16);
James Feistdf515152019-09-18 16:40:40 -0700317 }
Patrick Williams26601e82021-10-06 12:43:25 -0500318 catch (const std::invalid_argument&)
James Feistdf515152019-09-18 16:40:40 -0700319 {
320 continue;
321 }
James Feistdf515152019-09-18 16:40:40 -0700322
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500323 auto thisSensorParameters = getSensorParameters(path);
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530324 auto findSensorCfg = configMap.find({bus, addr});
325 if (findSensorCfg == configMap.end())
James Feist6714a252018-09-10 15:26:18 -0700326 {
James Feistdf515152019-09-18 16:40:40 -0700327 continue;
James Feist6714a252018-09-10 15:26:18 -0700328 }
James Feist6714a252018-09-10 15:26:18 -0700329
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530330 const std::string& interfacePath =
331 findSensorCfg->second.sensorPath;
332 const SensorData& sensorData = findSensorCfg->second.sensorData;
333 const std::string& sensorType = findSensorCfg->second.interface;
334 const SensorBaseConfigMap& baseConfigMap =
335 findSensorCfg->second.config;
336 std::vector<std::string>& hwmonName =
337 findSensorCfg->second.name;
338
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500339 // Temperature has "Name", pressure has "Name1"
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530340 auto findSensorName = baseConfigMap.find("Name");
Bruce Mitchell3ec41c52021-12-10 16:05:17 -0600341 if (thisSensorParameters.typeName == "pressure" ||
342 thisSensorParameters.typeName == "humidity")
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500343 {
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530344 findSensorName = baseConfigMap.find("Name1");
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500345 }
346
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530347 if (findSensorName == baseConfigMap.end())
James Feistdf515152019-09-18 16:40:40 -0700348 {
349 std::cerr << "could not determine configuration name for "
350 << deviceName << "\n";
351 continue;
352 }
353 std::string sensorName =
354 std::get<std::string>(findSensorName->second);
355 // on rescans, only update sensors we were signaled by
356 auto findSensor = sensors.find(sensorName);
357 if (!firstScan && findSensor != sensors.end())
358 {
359 bool found = false;
Bruce Mitchelld653b752021-08-23 13:09:00 -0500360 auto it = sensorsChanged->begin();
361 while (it != sensorsChanged->end())
James Feistdf515152019-09-18 16:40:40 -0700362 {
363 if (boost::ends_with(*it, findSensor->second->name))
364 {
Bruce Mitchelld653b752021-08-23 13:09:00 -0500365 it = sensorsChanged->erase(it);
James Feistdf515152019-09-18 16:40:40 -0700366 findSensor->second = nullptr;
367 found = true;
368 break;
369 }
Bruce Mitchelld653b752021-08-23 13:09:00 -0500370 ++it;
James Feistdf515152019-09-18 16:40:40 -0700371 }
372 if (!found)
373 {
374 continue;
375 }
376 }
Matt Spinler5636d522021-03-17 14:52:18 -0500377
James Feistdf515152019-09-18 16:40:40 -0700378 std::vector<thresholds::Threshold> sensorThresholds;
Matt Spinler5636d522021-03-17 14:52:18 -0500379 int index = 1;
380
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530381 if (!parseThresholdsFromConfig(sensorData, sensorThresholds,
Matt Spinler5636d522021-03-17 14:52:18 -0500382 nullptr, &index))
James Feistdf515152019-09-18 16:40:40 -0700383 {
384 std::cerr << "error populating thresholds for "
Matt Spinler5636d522021-03-17 14:52:18 -0500385 << sensorName << " index 1\n";
James Feistdf515152019-09-18 16:40:40 -0700386 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800387
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530388 auto findPollRate = baseConfigMap.find("PollRate");
Jeff Lin87bc67f2020-12-04 20:58:01 +0800389 float pollRate = pollRateDefault;
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530390 if (findPollRate != baseConfigMap.end())
Jeff Lin87bc67f2020-12-04 20:58:01 +0800391 {
392 pollRate = std::visit(VariantToFloatVisitor(),
393 findPollRate->second);
394 if (pollRate <= 0.0f)
395 {
396 pollRate = pollRateDefault; // polling time too short
397 }
398 }
399
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530400 auto findPowerOn = baseConfigMap.find("PowerState");
James Feistf9b01b62020-01-29 15:21:58 -0800401 PowerState readState = PowerState::always;
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530402 if (findPowerOn != baseConfigMap.end())
James Feistf9b01b62020-01-29 15:21:58 -0800403 {
404 std::string powerState = std::visit(
405 VariantToStringVisitor(), findPowerOn->second);
406 setReadState(powerState, readState);
407 }
Jason Ling100c20b2020-08-11 14:50:33 -0700408
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530409 auto permitSet = getPermitSet(baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800410 auto& sensor = sensors[sensorName];
411 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700412 auto hwmonFile = getFullHwmonFilePath(directory.string(),
413 "temp1", permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500414 if (pathStr.starts_with("/sys/bus/iio/devices"))
415 {
416 hwmonFile = pathStr;
417 }
Jason Ling100c20b2020-08-11 14:50:33 -0700418 if (hwmonFile)
419 {
420 sensor = std::make_shared<HwmonTempSensor>(
421 *hwmonFile, sensorType, objectServer, dbusConnection,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500422 io, sensorName, std::move(sensorThresholds),
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530423 thisSensorParameters, pollRate, interfacePath,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500424 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700425 sensor->setupRead();
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530426 hwmonName.erase(
427 remove(hwmonName.begin(), hwmonName.end(), sensorName),
428 hwmonName.end());
Jason Ling100c20b2020-08-11 14:50:33 -0700429 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800430 // Looking for keys like "Name1" for temp2_input,
431 // "Name2" for temp3_input, etc.
432 int i = 0;
433 while (true)
James Feistdf515152019-09-18 16:40:40 -0700434 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800435 ++i;
436 auto findKey =
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530437 baseConfigMap.find("Name" + std::to_string(i));
438 if (findKey == baseConfigMap.end())
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800439 {
440 break;
441 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800442 std::string sensorName =
443 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700444 hwmonFile = getFullHwmonFilePath(
445 directory.string(), "temp" + std::to_string(i + 1),
446 permitSet);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500447 if (pathStr.starts_with("/sys/bus/iio/devices"))
448 {
449 continue;
450 }
Jason Ling100c20b2020-08-11 14:50:33 -0700451 if (hwmonFile)
452 {
Matt Spinler5636d522021-03-17 14:52:18 -0500453 // To look up thresholds for these additional sensors,
454 // match on the Index property in the threshold data
455 // where the index comes from the sysfs file we're on,
456 // i.e. index = 2 for temp2_input.
457 int index = i + 1;
458 std::vector<thresholds::Threshold> thresholds;
459
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530460 if (!parseThresholdsFromConfig(sensorData, thresholds,
Matt Spinler5636d522021-03-17 14:52:18 -0500461 nullptr, &index))
462 {
463 std::cerr << "error populating thresholds for "
464 << sensorName << " index " << index
465 << "\n";
466 }
467
Jason Ling100c20b2020-08-11 14:50:33 -0700468 auto& sensor = sensors[sensorName];
469 sensor = nullptr;
470 sensor = std::make_shared<HwmonTempSensor>(
471 *hwmonFile, sensorType, objectServer,
472 dbusConnection, io, sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500473 std::move(thresholds), thisSensorParameters,
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530474 pollRate, interfacePath, readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700475 sensor->setupRead();
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530476 hwmonName.erase(remove(hwmonName.begin(),
477 hwmonName.end(), sensorName),
478 hwmonName.end());
Jason Ling100c20b2020-08-11 14:50:33 -0700479 }
James Feistdf515152019-09-18 16:40:40 -0700480 }
Jayashree Dhanapal9f3a74e2022-01-06 12:05:06 +0530481 if (hwmonName.empty())
482 {
483 configMap.erase(findSensorCfg);
484 }
James Feistdf515152019-09-18 16:40:40 -0700485 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700486 });
James Feistdf515152019-09-18 16:40:40 -0700487 getter->getConfiguration(
488 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700489}
490
Matt Spinler20bf2c12021-09-14 11:07:07 -0500491void interfaceRemoved(
492 sdbusplus::message::message& message,
493 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
494 sensors)
495{
496 if (message.is_method_error())
497 {
498 std::cerr << "interfacesRemoved callback method error\n";
499 return;
500 }
501
502 sdbusplus::message::object_path path;
503 std::vector<std::string> interfaces;
504
505 message.read(path, interfaces);
506
507 // If the xyz.openbmc_project.Confguration.X interface was removed
508 // for one or more sensors, delete those sensor objects.
509 auto sensorIt = sensors.begin();
510 while (sensorIt != sensors.end())
511 {
512 if ((sensorIt->second->configurationPath == path) &&
513 (std::find(interfaces.begin(), interfaces.end(),
514 sensorIt->second->objectType) != interfaces.end()))
515 {
516 sensorIt = sensors.erase(sensorIt);
517 }
518 else
519 {
520 sensorIt++;
521 }
522 }
523}
524
James Feistb6c0b912019-07-09 12:21:44 -0700525int main()
James Feist6714a252018-09-10 15:26:18 -0700526{
527 boost::asio::io_service io;
528 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
529 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
530 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800531 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700532 sensors;
533 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700534 auto sensorsChanged =
535 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700536
537 io.post([&]() {
538 createSensors(io, objectServer, sensors, systemBus, nullptr);
539 });
540
541 boost::asio::deadline_timer filterTimer(io);
542 std::function<void(sdbusplus::message::message&)> eventHandler =
543 [&](sdbusplus::message::message& message) {
544 if (message.is_method_error())
545 {
546 std::cerr << "callback method error\n";
547 return;
548 }
549 sensorsChanged->insert(message.get_path());
550 // this implicitly cancels the timer
551 filterTimer.expires_from_now(boost::posix_time::seconds(1));
552
553 filterTimer.async_wait([&](const boost::system::error_code& ec) {
554 if (ec == boost::asio::error::operation_aborted)
555 {
556 /* we were canceled*/
557 return;
558 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700559 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700560 {
561 std::cerr << "timer error\n";
562 return;
563 }
564 createSensors(io, objectServer, sensors, systemBus,
565 sensorsChanged);
566 });
567 };
568
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700569 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700570 {
571 auto match = std::make_unique<sdbusplus::bus::match::match>(
572 static_cast<sdbusplus::bus::bus&>(*systemBus),
573 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700574 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700575 eventHandler);
576 matches.emplace_back(std::move(match));
577 }
578
Bruce Lee1263c3d2021-06-04 15:16:33 +0800579 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500580
581 // Watch for entity-manager to remove configuration interfaces
582 // so the corresponding sensors can be removed.
583 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>(
584 static_cast<sdbusplus::bus::bus&>(*systemBus),
585 "type='signal',member='InterfacesRemoved',arg0path='" +
586 std::string(inventoryPath) + "/'",
587 [&sensors](sdbusplus::message::message& msg) {
588 interfaceRemoved(msg, sensors);
589 });
590
591 matches.emplace_back(std::move(ifaceRemovedMatch));
592
James Feist6714a252018-09-10 15:26:18 -0700593 io.run();
594}