blob: d12547039235f2b542e5ae5397d2ec4389917994 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 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*/
Josh Lehan0830c7b2019-10-08 16:35:09 -070016
Matt Simmering786efb82023-01-18 14:09:21 -080017#include "DeviceMgmt.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103018#include "PSUEvent.hpp"
19#include "PSUSensor.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "PwmSensor.hpp"
21#include "SensorPaths.hpp"
22#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103023#include "Utils.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include "VariantVisitors.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103025
Matt Simmeringe4e6a282023-03-03 14:41:04 -080026#include <boost/algorithm/string/case_conv.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080027#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <boost/asio/error.hpp>
29#include <boost/asio/io_context.hpp>
30#include <boost/asio/post.hpp>
31#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034#include <sdbusplus/asio/connection.hpp>
35#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070037#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <sdbusplus/exception.hpp>
39#include <sdbusplus/message.hpp>
40#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070041
Ed Tanouseacbfdd2024-04-04 12:00:24 -070042#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070043#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070044#include <cctype>
45#include <chrono>
Josh Lehan74d9bd92019-10-31 08:51:58 -070046#include <cmath>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070047#include <cstddef>
48#include <cstdint>
49#include <exception>
James Feist24f02f22019-04-15 11:05:39 -070050#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080051#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070052#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080053#include <iostream>
Jayaprakash Mutyalaa205c6f2025-02-13 06:09:04 +000054#include <iterator>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070055#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080056#include <regex>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070057#include <stdexcept>
Patrick Venture96e97db2019-10-31 13:44:38 -070058#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080059#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070060#include <utility>
61#include <variant>
62#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080063
Ed Tanous8a57ec02020-10-09 12:46:52 -070064static constexpr bool debug = false;
Chau Lyc4ddf642024-07-09 05:01:06 +000065static std::regex i2cDevRegex(R"((\/i2c\-\d+\/\d+-[a-fA-F0-9]{4,4})(\/|$))");
Josh Lehan49cfba92019-10-08 16:50:42 -070066
Matt Simmering786efb82023-01-18 14:09:21 -080067static const I2CDeviceTypeMap sensorTypes{
Delphine CC Chiu436c5bd2023-09-22 09:47:43 +080068 {"ADC128D818", I2CDeviceType{"adc128d818", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080069 {"ADM1266", I2CDeviceType{"adm1266", true}},
70 {"ADM1272", I2CDeviceType{"adm1272", true}},
71 {"ADM1275", I2CDeviceType{"adm1275", true}},
72 {"ADM1278", I2CDeviceType{"adm1278", true}},
73 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080074 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080075 {"ADS7830", I2CDeviceType{"ads7830", true}},
76 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
77 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020078 {"cffps", I2CDeviceType{"cffps", true}},
79 {"cffps1", I2CDeviceType{"cffps", true}},
80 {"cffps2", I2CDeviceType{"cffps", true}},
81 {"cffps3", I2CDeviceType{"cffps", true}},
Andrew Geisslerb2392ef2024-12-10 09:39:19 -060082 {"CRPS185", I2CDeviceType{"crps185", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080083 {"DPS800", I2CDeviceType{"dps800", true}},
84 {"INA219", I2CDeviceType{"ina219", true}},
85 {"INA230", I2CDeviceType{"ina230", true}},
Potin Lai5674ac92023-11-20 14:33:56 +080086 {"INA238", I2CDeviceType{"ina238", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080087 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080088 {"IR38060", I2CDeviceType{"ir38060", true}},
89 {"IR38164", I2CDeviceType{"ir38164", true}},
90 {"IR38263", I2CDeviceType{"ir38263", true}},
Yikai Tsai5b74aba2024-09-06 13:58:28 +080091 {"ISL28022", I2CDeviceType{"isl28022", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080092 {"ISL68137", I2CDeviceType{"isl68137", true}},
93 {"ISL68220", I2CDeviceType{"isl68220", true}},
94 {"ISL68223", I2CDeviceType{"isl68223", true}},
95 {"ISL69225", I2CDeviceType{"isl69225", true}},
96 {"ISL69243", I2CDeviceType{"isl69243", true}},
97 {"ISL69260", I2CDeviceType{"isl69260", true}},
98 {"LM25066", I2CDeviceType{"lm25066", true}},
Yi-Shum77e87692023-11-30 10:27:45 +080099 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Potin Lai43847a82024-02-27 16:29:45 +0800100 {"LTC4286", I2CDeviceType{"ltc4286", true}},
101 {"LTC4287", I2CDeviceType{"ltc4287", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +0200102 {"MAX5970", I2CDeviceType{"max5970", true}},
Joseph Fu15a39df2023-12-27 09:45:49 +0800103 {"MAX11607", I2CDeviceType{"max11607", false}},
Ian Chienc82a4092024-07-24 10:52:59 +0800104 {"MAX11615", I2CDeviceType{"max11615", false}},
Allen.Wang8c13c282024-07-03 09:42:54 +0800105 {"MAX11617", I2CDeviceType{"max11617", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800106 {"MAX16601", I2CDeviceType{"max16601", true}},
107 {"MAX20710", I2CDeviceType{"max20710", true}},
108 {"MAX20730", I2CDeviceType{"max20730", true}},
109 {"MAX20734", I2CDeviceType{"max20734", true}},
110 {"MAX20796", I2CDeviceType{"max20796", true}},
111 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800112 {"MP2856", I2CDeviceType{"mp2856", true}},
113 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800114 {"MP2971", I2CDeviceType{"mp2971", true}},
115 {"MP2973", I2CDeviceType{"mp2973", true}},
116 {"MP2975", I2CDeviceType{"mp2975", true}},
117 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800118 {"MP5990", I2CDeviceType{"mp5990", true}},
Jeff Lind8e48df2024-06-07 09:06:56 +0800119 {"MPQ8785", I2CDeviceType{"mpq8785", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800120 {"NCP4200", I2CDeviceType{"ncp4200", true}},
121 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
122 {"pmbus", I2CDeviceType{"pmbus", true}},
123 {"PXE1610", I2CDeviceType{"pxe1610", true}},
124 {"RAA228000", I2CDeviceType{"raa228000", true}},
Potin Lai6e6618f2024-05-16 15:02:56 +0800125 {"RAA228004", I2CDeviceType{"raa228004", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800126 {"RAA228228", I2CDeviceType{"raa228228", true}},
127 {"RAA228620", I2CDeviceType{"raa228620", true}},
128 {"RAA229001", I2CDeviceType{"raa229001", true}},
129 {"RAA229004", I2CDeviceType{"raa229004", true}},
130 {"RAA229126", I2CDeviceType{"raa229126", true}},
Ian Chien982d99f2024-08-20 17:16:56 +0800131 {"RTQ6056", I2CDeviceType{"rtq6056", false}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800132 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Chau Lyc4ddf642024-07-09 05:01:06 +0000133 {"smpro_hwmon", I2CDeviceType{"smpro", false}},
Rush Chena9ea8e52025-01-02 11:28:57 +0800134 {"SY24655", I2CDeviceType{"sy24655", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200135 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800136 {"TPS53679", I2CDeviceType{"tps53679", true}},
137 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800138 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800139 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
140 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800141 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800142};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700143
Joseph Fub844e972023-03-25 10:24:30 +0800144enum class DevTypes
145{
146 Unknown = 0,
147 HWMON,
148 IIO
149};
150
151struct DevParams
152{
153 unsigned int matchIndex = 0;
154 std::string matchRegEx;
155 std::string nameRegEx;
156};
157
Yong Libf8b1da2020-04-15 16:32:50 +0800158static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800159 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800160static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
161 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800162static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
163 pwmSensors;
164static boost::container::flat_map<std::string, std::string> sensorTable;
165static boost::container::flat_map<std::string, PSUProperty> labelMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800166static EventPathList eventMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800167static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800168
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800169static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800170static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700171
Cheng C Yang58b2b532019-05-31 00:19:45 +0800172// Function CheckEvent will check each attribute from eventMatch table in the
173// sysfs. If the attributes exists in sysfs, then store the complete path
174// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800175void checkEvent(const std::string& directory, const EventPathList& eventMatch,
176 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800177{
178 for (const auto& match : eventMatch)
179 {
180 const std::vector<std::string>& eventAttrs = match.second;
181 const std::string& eventName = match.first;
182 for (const auto& eventAttr : eventAttrs)
183 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700184 std::string eventPath = directory;
185 eventPath += "/";
186 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800187
188 std::ifstream eventFile(eventPath);
189 if (!eventFile.good())
190 {
191 continue;
192 }
193
194 eventPathList[eventName].push_back(eventPath);
195 }
196 }
197}
198
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800199// Check Group Events which contains more than one targets in each combine
200// events.
George Liu5b3542e2023-10-31 17:27:12 +0800201void checkGroupEvent(const std::string& directory,
George Liu5b3542e2023-10-31 17:27:12 +0800202 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800203{
cchouxe8a14e92023-10-25 23:26:27 +0800204 EventPathList pathList;
Ed Tanous2e466962025-01-30 10:59:49 -0800205 std::vector<std::filesystem::path> eventPaths;
206 if (!findFiles(std::filesystem::path(directory), R"(fan\d+_(alarm|fault))",
207 eventPaths))
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800208 {
cchouxe8a14e92023-10-25 23:26:27 +0800209 return;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800210 }
cchouxe8a14e92023-10-25 23:26:27 +0800211
212 for (const auto& eventPath : eventPaths)
213 {
214 std::string attrName = eventPath.filename();
215 pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
216 }
217 groupEventPathList["FanFault"] = pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800218}
219
Cheng C Yang58b2b532019-05-31 00:19:45 +0800220// Function checkEventLimits will check all the psu related xxx_input attributes
221// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
222// xxx_min_alarm exist, then store the existing paths of the alarm attributes
223// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800224void checkEventLimits(const std::string& sensorPathStr,
225 const EventPathList& limitEventMatch,
226 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800227{
Lei YUa2c7cea2020-12-23 14:07:28 +0800228 auto attributePartPos = sensorPathStr.find_last_of('_');
229 if (attributePartPos == std::string::npos)
230 {
231 // There is no '_' in the string, skip it
232 return;
233 }
234 auto attributePart =
235 std::string_view(sensorPathStr).substr(attributePartPos + 1);
236 if (attributePart != "input")
237 {
238 // If the sensor is not xxx_input, skip it
239 return;
240 }
241
242 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800243 for (const auto& limitMatch : limitEventMatch)
244 {
245 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
246 const std::string& eventName = limitMatch.first;
247 for (const auto& limitEventAttr : limitEventAttrs)
248 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800249 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800250 std::ifstream eventFile(limitEventPath);
251 if (!eventFile.good())
252 {
253 continue;
254 }
255 eventPathList[eventName].push_back(limitEventPath);
256 }
257 }
258}
Cheng C Yang916360b2019-05-07 18:47:16 +0800259
Patrick Williams2aaf7172024-08-16 15:20:40 -0400260static void checkPWMSensor(
Ed Tanous2e466962025-01-30 10:59:49 -0800261 const std::filesystem::path& sensorPath, std::string& labelHead,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400262 const std::string& interfacePath,
263 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
264 sdbusplus::asio::object_server& objectServer, const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800265{
cchouxe8a14e92023-10-25 23:26:27 +0800266 if (!labelHead.starts_with("fan"))
Cheng C Yang916360b2019-05-07 18:47:16 +0800267 {
cchouxe8a14e92023-10-25 23:26:27 +0800268 return;
Cheng C Yang916360b2019-05-07 18:47:16 +0800269 }
cchouxe8a14e92023-10-25 23:26:27 +0800270 std::string labelHeadIndex = labelHead.substr(3);
271
272 const std::string& sensorPathStr = sensorPath.string();
Patrick Williams2aaf7172024-08-16 15:20:40 -0400273 const std::string& pwmPathStr =
274 boost::replace_all_copy(sensorPathStr, "input", "target");
cchouxe8a14e92023-10-25 23:26:27 +0800275 std::ifstream pwmFile(pwmPathStr);
276 if (!pwmFile.good())
277 {
278 return;
279 }
280
281 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
282 if (findPWMSensor != pwmSensors.end())
283 {
284 return;
285 }
286
287 std::string name = "Pwm_";
288 name += psuName;
289 name += "_Fan_";
290 name += labelHeadIndex;
291
292 std::string objPath = interfacePath;
293 objPath += "_Fan_";
294 objPath += labelHeadIndex;
295
296 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
297 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800298}
299
Zhikui Ren23c96e72020-11-05 22:32:28 -0800300static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800301 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800302 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
303 const ManagedObjectType& sensorConfigs,
304 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800305 sensorsChanged,
306 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800307{
Josh Lehan49cfba92019-10-08 16:50:42 -0700308 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800309 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800310
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800311 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
312
Ed Tanous2e466962025-01-30 10:59:49 -0800313 std::vector<std::filesystem::path> pmbusPaths;
314 findFiles(std::filesystem::path("/sys/bus/iio/devices"), "name",
315 pmbusPaths);
316 findFiles(std::filesystem::path("/sys/class/hwmon"), "name", pmbusPaths);
Joseph Fub844e972023-03-25 10:24:30 +0800317 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800318 {
319 std::cerr << "No PSU sensors in system\n";
320 return;
321 }
322
323 boost::container::flat_set<std::string> directories;
324 for (const auto& pmbusPath : pmbusPaths)
325 {
George Liu5b3542e2023-10-31 17:27:12 +0800326 EventPathList eventPathList;
327 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800328
329 std::ifstream nameFile(pmbusPath);
330 if (!nameFile.good())
331 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700332 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800333 continue;
334 }
335
336 std::string pmbusName;
337 std::getline(nameFile, pmbusName);
338 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700339
Matt Simmering786efb82023-01-18 14:09:21 -0800340 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800341 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700342 // To avoid this error message, add your driver name to
343 // the pmbusNames vector at the top of this file.
344 std::cerr << "Driver name " << pmbusName
345 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800346 continue;
347 }
348
Cheng C Yang209ec562019-03-12 16:37:44 +0800349 auto directory = pmbusPath.parent_path();
350
351 auto ret = directories.insert(directory.string());
352 if (!ret.second)
353 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700354 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800355 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 }
357
Joseph Fub844e972023-03-25 10:24:30 +0800358 DevTypes devType = DevTypes::HWMON;
359 std::string deviceName;
360 if (directory.parent_path() == "/sys/class/hwmon")
361 {
Ed Tanous2e466962025-01-30 10:59:49 -0800362 std::string devicePath =
363 std::filesystem::canonical(directory / "device");
Chau Lyc4ddf642024-07-09 05:01:06 +0000364 std::smatch match;
365 // Find /i2c-<bus>/<bus>-<address> match in device path
366 std::regex_search(devicePath, match, i2cDevRegex);
367 if (match.empty())
368 {
369 std::cerr << "Found bad device path " << devicePath << "\n";
370 continue;
371 }
372 // Extract <bus>-<address>
373 std::string matchStr = match[1];
374 deviceName = matchStr.substr(matchStr.find_last_of('/') + 1);
Joseph Fub844e972023-03-25 10:24:30 +0800375 }
376 else
377 {
Ed Tanous2e466962025-01-30 10:59:49 -0800378 deviceName =
379 std::filesystem::canonical(directory).parent_path().stem();
Joseph Fub844e972023-03-25 10:24:30 +0800380 devType = DevTypes::IIO;
381 }
382
Cheng C Yang209ec562019-03-12 16:37:44 +0800383 size_t bus = 0;
384 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000385 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800386 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800387 continue;
388 }
389
Zev Weisse8b97ee2022-08-12 15:23:51 -0700390 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800391 const SensorData* sensorData = nullptr;
392 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800393 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800394 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800395
Zev Weiss741f26e2022-08-12 18:21:02 -0700396 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800397 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700398 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800399 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800400 {
Zev Weiss054aad82022-08-18 01:37:34 -0700401 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800402 if (sensorBase != sensorData->end())
403 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700404 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800405 sensorType = type;
406 break;
407 }
408 }
409 if (baseConfig == nullptr)
410 {
411 std::cerr << "error finding base configuration for "
412 << deviceName << "\n";
413 continue;
414 }
415
Zev Weisse8b97ee2022-08-12 15:23:51 -0700416 auto configBus = baseConfig->find("Bus");
417 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800418
Zev Weisse8b97ee2022-08-12 15:23:51 -0700419 if (configBus == baseConfig->end() ||
420 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800422 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800423 continue;
424 }
425
Matt Simmering6747eba2023-01-18 16:03:55 -0800426 const uint64_t* confBus =
427 std::get_if<uint64_t>(&(configBus->second));
428 const uint64_t* confAddr =
429 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800430 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800431 {
432 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700433 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800434 continue;
435 }
436
437 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200439 if constexpr (debug)
440 {
441 std::cerr << "Configuration skipping " << *confBus << "-"
442 << *confAddr << " because not " << bus << "-"
443 << addr << "\n";
444 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800445 continue;
446 }
447
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800448 std::vector<thresholds::Threshold> confThresholds;
449 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
450 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800451 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800452 }
453 thresholdConfSize = confThresholds.size();
454
Zev Weiss741f26e2022-08-12 18:21:02 -0700455 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800456 break;
457 }
458 if (interfacePath == nullptr)
459 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700460 // To avoid this error message, add your export map entry,
461 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800462 std::cerr << "failed to find match for " << deviceName << "\n";
463 continue;
464 }
465
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800466 auto findI2CDev = devices.find(*interfacePath);
467
468 std::shared_ptr<I2CDevice> i2cDev;
469 if (findI2CDev != devices.end())
470 {
471 if (activateOnly && !findI2CDev->second.second)
472 {
473 continue;
474 }
475 i2cDev = findI2CDev->second.first;
476 }
477
Zev Weisse8b97ee2022-08-12 15:23:51 -0700478 auto findPSUName = baseConfig->find("Name");
479 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800480 {
481 std::cerr << "could not determine configuration name for "
482 << deviceName << "\n";
483 continue;
484 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800485 const std::string* psuName =
486 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800487 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800488 {
489 std::cerr << "Cannot find psu name, invalid configuration\n";
490 continue;
491 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800492
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800493 auto findCPU = baseConfig->find("CPURequired");
494 if (findCPU != baseConfig->end())
495 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500496 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800497 auto presenceFind = cpuPresence.find(index);
498 if (presenceFind == cpuPresence.end() || !presenceFind->second)
499 {
500 continue;
501 }
502 }
503
Zhikui Ren23c96e72020-11-05 22:32:28 -0800504 // on rescans, only update sensors we were signaled by
505 if (!firstScan)
506 {
Zhikui Renda98f092021-11-01 09:41:08 -0700507 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400508 auto it =
509 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
510 [psuNameStr](std::string& s) {
511 return s.ends_with(psuNameStr);
512 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800513
514 if (it == sensorsChanged->end())
515 {
516 continue;
517 }
518 sensorsChanged->erase(it);
519 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800520 checkEvent(directory.string(), eventMatch, eventPathList);
cchouxe8a14e92023-10-25 23:26:27 +0800521 checkGroupEvent(directory.string(), groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800522
Zev Weisse8b97ee2022-08-12 15:23:51 -0700523 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000524
Vijay Khemka996bad12019-05-28 15:15:16 -0700525 /* Check if there are more sensors in the same interface */
526 int i = 1;
527 std::vector<std::string> psuNames;
528 do
529 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700530 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700531 psuNames.push_back(
532 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700533 findPSUName = baseConfig->find("Name" + std::to_string(i++));
534 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700535
Ed Tanous2e466962025-01-30 10:59:49 -0800536 std::vector<std::filesystem::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800537 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
538 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800539 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800540 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800541 continue;
542 }
543
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530544 /* read max value in sysfs for in, curr, power, temp, ... */
545 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
546 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700547 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530548 {
549 std::cerr << "No max name in PSU \n";
550 }
551 }
552
Zev Weiss8569bf22022-10-11 15:37:44 -0700553 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800554
Vijay Khemka996bad12019-05-28 15:15:16 -0700555 /* Find array of labels to be exposed if it is defined in config */
556 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700557 auto findLabelObj = baseConfig->find("Labels");
558 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700559 {
560 findLabels =
561 std::get<std::vector<std::string>>(findLabelObj->second);
562 }
563
Joseph Fub844e972023-03-25 10:24:30 +0800564 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700565 std::smatch matches;
566
Cheng C Yange50345b2019-04-02 17:26:15 +0800567 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800568 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530569 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800570 std::string labelHead;
571 std::string sensorPathStr = sensorPath.string();
572 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700573 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700574 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
575 {
Josh Lehan06494452019-10-31 09:49:16 -0700576 // hwmon *_input filename without number:
577 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800578 // iio in_*_raw filename without number:
579 // voltage, temp, pressure, ...
580 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700581 }
582 else
583 {
Josh Lehan06494452019-10-31 09:49:16 -0700584 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700585 << sensorNameStr;
586 continue;
587 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800588
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530589 std::string labelPath;
590
Joseph Fub844e972023-03-25 10:24:30 +0800591 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530592 {
Joseph Fub844e972023-03-25 10:24:30 +0800593 /* find and differentiate _max and _input to replace "label" */
594 size_t pos = sensorPathStr.find('_');
595 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530596 {
Joseph Fub844e972023-03-25 10:24:30 +0800597 std::string sensorPathStrMax = sensorPathStr.substr(pos);
598 if (sensorPathStrMax == "_max")
599 {
600 labelPath = boost::replace_all_copy(sensorPathStr,
601 "max", "label");
602 maxLabel = true;
603 }
604 else
605 {
606 labelPath = boost::replace_all_copy(sensorPathStr,
607 "input", "label");
608 maxLabel = false;
609 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530610 }
611 else
612 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800613 continue;
614 }
615
Joseph Fub844e972023-03-25 10:24:30 +0800616 std::ifstream labelFile(labelPath);
617 if (!labelFile.good())
618 {
619 if constexpr (debug)
620 {
621 std::cerr << "Input file " << sensorPath
622 << " has no corresponding label file\n";
623 }
624 // hwmon *_input filename with number:
625 // temp1, temp2, temp3, ...
Patrick Williams2aaf7172024-08-16 15:20:40 -0400626 labelHead =
627 sensorNameStr.substr(0, sensorNameStr.find('_'));
Joseph Fub844e972023-03-25 10:24:30 +0800628 }
629 else
630 {
631 std::string label;
632 std::getline(labelFile, label);
633 labelFile.close();
634 auto findSensor = sensors.find(label);
635 if (findSensor != sensors.end())
636 {
637 continue;
638 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800639
Joseph Fub844e972023-03-25 10:24:30 +0800640 // hwmon corresponding *_label file contents:
641 // vin1, vout1, ...
642 labelHead = label.substr(0, label.find(' '));
643 }
644
645 /* append "max" for labelMatch */
646 if (maxLabel)
647 {
648 labelHead.insert(0, "max");
649 }
650
651 checkPWMSensor(sensorPath, labelHead, *interfacePath,
652 dbusConnection, objectServer, psuNames[0]);
653 }
654 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530655 {
Joseph Fub844e972023-03-25 10:24:30 +0800656 auto findIIOHyphen = sensorNameStr.find_last_of('_');
657 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530658 }
659
Ed Tanous8a57ec02020-10-09 12:46:52 -0700660 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700661 {
662 std::cerr << "Sensor type=\"" << sensorNameSubStr
663 << "\" label=\"" << labelHead << "\"\n";
664 }
665
Vijay Khemka996bad12019-05-28 15:15:16 -0700666 if (!findLabels.empty())
667 {
668 /* Check if this labelHead is enabled in config file */
669 if (std::find(findLabels.begin(), findLabels.end(),
670 labelHead) == findLabels.end())
671 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700672 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800673 {
674 std::cerr << "could not find " << labelHead
675 << " in the Labels list\n";
676 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700677 continue;
678 }
679 }
Jayaprakash Mutyalaa205c6f2025-02-13 06:09:04 +0000680 auto it = std::find_if(labelHead.begin(), labelHead.end(),
681 static_cast<int (*)(int)>(std::isdigit));
682 std::string_view labelHeadView(
683 labelHead.data(), std::distance(labelHead.begin(), it));
684 auto findProperty =
685 labelMatch.find(static_cast<std::string>(labelHeadView));
Cheng C Yange50345b2019-04-02 17:26:15 +0800686 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800687 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700688 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700689 {
690 std::cerr << "Could not find matching default property for "
Jayaprakash Mutyalaa205c6f2025-02-13 06:09:04 +0000691 << labelHead << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800693 continue;
694 }
695
Josh Lehan74d9bd92019-10-31 08:51:58 -0700696 // Protect the hardcoded labelMatch list from changes,
697 // by making a copy and modifying that instead.
698 // Avoid bleedthrough of one device's customizations to
699 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800700 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701
702 // Use label head as prefix for reading from config file,
703 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
704 std::string keyName = labelHead + "_Name";
705 std::string keyScale = labelHead + "_Scale";
706 std::string keyMin = labelHead + "_Min";
707 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800708 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800709 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700710
711 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700712 auto findCustomName = baseConfig->find(keyName);
713 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700714 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700715 try
716 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800717 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700718 VariantToStringVisitor(), findCustomName->second);
719 }
Patrick Williams26601e82021-10-06 12:43:25 -0500720 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700721 {
722 std::cerr << "Unable to parse " << keyName << "\n";
723 continue;
724 }
725
726 // All strings are valid, including empty string
727 customizedName = true;
728 }
729
730 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700731 auto findCustomScale = baseConfig->find(keyScale);
732 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700733 {
734 try
735 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800736 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 VariantToUnsignedIntVisitor(), findCustomScale->second);
738 }
Patrick Williams26601e82021-10-06 12:43:25 -0500739 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700740 {
741 std::cerr << "Unable to parse " << keyScale << "\n";
742 continue;
743 }
744
745 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800746 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700747 {
748 customizedScale = true;
749 }
750 else
751 {
752 std::cerr << "Unable to accept " << keyScale << "\n";
753 continue;
754 }
755 }
756
Zev Weisse8b97ee2022-08-12 15:23:51 -0700757 auto findCustomMin = baseConfig->find(keyMin);
758 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700759 {
760 try
761 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800762 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700763 VariantToDoubleVisitor(), findCustomMin->second);
764 }
Patrick Williams26601e82021-10-06 12:43:25 -0500765 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700766 {
767 std::cerr << "Unable to parse " << keyMin << "\n";
768 continue;
769 }
770 }
771
Zev Weisse8b97ee2022-08-12 15:23:51 -0700772 auto findCustomMax = baseConfig->find(keyMax);
773 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700774 {
775 try
776 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800777 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700778 VariantToDoubleVisitor(), findCustomMax->second);
779 }
Patrick Williams26601e82021-10-06 12:43:25 -0500780 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700781 {
782 std::cerr << "Unable to parse " << keyMax << "\n";
783 continue;
784 }
785 }
786
Zev Weisse8b97ee2022-08-12 15:23:51 -0700787 auto findCustomOffset = baseConfig->find(keyOffset);
788 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800789 {
790 try
791 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800792 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800793 VariantToDoubleVisitor(), findCustomOffset->second);
794 }
Patrick Williams26601e82021-10-06 12:43:25 -0500795 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800796 {
797 std::cerr << "Unable to parse " << keyOffset << "\n";
798 continue;
799 }
800 }
801
Lotus Xucb5af732021-09-10 15:18:50 +0800802 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700803 auto findPowerState = baseConfig->find(keyPowerState);
804 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800805 {
806 std::string powerState = std::visit(VariantToStringVisitor(),
807 findPowerState->second);
808 setReadState(powerState, readState);
809 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800810 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700811 {
812 std::cerr << "Min must be less than Max\n";
813 continue;
814 }
815
816 // If the sensor name is being customized by config file,
817 // then prefix/suffix composition becomes not necessary,
818 // and in fact not wanted, because it gets in the way.
819 std::string psuNameFromIndex;
cchouxe8a14e92023-10-25 23:26:27 +0800820 std::string nameIndexStr = "1";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700821 if (!customizedName)
822 {
823 /* Find out sensor name index for this label */
824 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500825 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 if (std::regex_search(labelHead, matches, rgx))
827 {
cchouxe8a14e92023-10-25 23:26:27 +0800828 nameIndexStr = matches[1];
829 nameIndex = std::stoi(nameIndexStr);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700830
831 // Decrement to preserve alignment, because hwmon
832 // human-readable filenames and labels use 1-based
833 // numbering, but the "Name", "Name1", "Name2", etc. naming
834 // convention (the psuNames vector) uses 0-based numbering.
835 if (nameIndex > 0)
836 {
837 --nameIndex;
838 }
839 }
840 else
841 {
842 nameIndex = 0;
843 }
844
845 if (psuNames.size() <= nameIndex)
846 {
847 std::cerr << "Could not pair " << labelHead
848 << " with a Name field\n";
849 continue;
850 }
851
852 psuNameFromIndex = psuNames[nameIndex];
853
Ed Tanous8a57ec02020-10-09 12:46:52 -0700854 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700855 {
856 std::cerr << "Sensor label head " << labelHead
857 << " paired with " << psuNameFromIndex
858 << " at index " << nameIndex << "\n";
859 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700860 }
861
Joseph Fub844e972023-03-25 10:24:30 +0800862 if (devType == DevTypes::HWMON)
863 {
864 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
865 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800866
Josh Lehan74d9bd92019-10-31 08:51:58 -0700867 // Similarly, if sensor scaling factor is being customized,
868 // then the below power-of-10 constraint becomes unnecessary,
869 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800870 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700871 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700872 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700873 // Preserve existing usage of hardcoded labelMatch table below
874 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700875
Josh Lehan74d9bd92019-10-31 08:51:58 -0700876 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700877 char firstChar =
878 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700879 std::string strScaleFactor =
880 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
881
882 // Preserve existing configs by accepting earlier syntax,
883 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700884 auto findScaleFactor = baseConfig->find(strScaleFactor);
885 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700886 {
887 factor = std::visit(VariantToIntVisitor(),
888 findScaleFactor->second);
889 }
890
Ed Tanous8a57ec02020-10-09 12:46:52 -0700891 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700892 {
893 std::cerr << "Sensor scaling factor " << factor
894 << " string " << strScaleFactor << "\n";
895 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700896 }
897
Vijay Khemka996bad12019-05-28 15:15:16 -0700898 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530899 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
900 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800901 {
James Feist17ab6e02019-06-25 12:28:13 -0700902 std::cerr << "error populating thresholds for "
903 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800904 }
905
Zev Weiss6b6891c2021-04-22 02:46:21 -0500906 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
907 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800908 {
Jason Ling5747fab2019-10-02 16:46:23 -0700909 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700910 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800911 continue;
912 }
913
Ed Tanous8a57ec02020-10-09 12:46:52 -0700914 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700915 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700916 std::cerr << "Sensor properties: Name \""
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800917 << psuProperty.labelTypeName << "\" Scale "
918 << psuProperty.sensorScaleFactor << " Min "
919 << psuProperty.minReading << " Max "
920 << psuProperty.maxReading << " Offset "
921 << psuProperty.sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700922 }
923
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800924 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700925 if (customizedName)
926 {
927 if (sensorName.empty())
928 {
929 // Allow selective disabling of an individual sensor,
930 // by customizing its name to an empty string.
931 std::cerr << "Sensor disabled, empty string\n";
932 continue;
933 }
934 }
935 else
936 {
937 // Sensor name not customized, do prefix/suffix composition,
938 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800939 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
cchouxe8a14e92023-10-25 23:26:27 +0800940
941 // The labelTypeName of a fan can be:
942 // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
943 if (labelHead == "fan" + nameIndexStr)
944 {
945 sensorName += nameIndexStr;
946 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700947 }
948
Ed Tanous8a57ec02020-10-09 12:46:52 -0700949 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700950 {
951 std::cerr << "Sensor name \"" << sensorName << "\" path \""
952 << sensorPathStr << "\" type \"" << sensorType
953 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700954 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800955 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800956
957 auto& sensor = sensors[sensorName];
958 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700959 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800960 sensor = nullptr;
961 }
962
963 if (sensor != nullptr)
964 {
965 sensor->activate(sensorPathStr, i2cDev);
966 }
967 else
968 {
969 sensors[sensorName] = std::make_shared<PSUSensor>(
970 sensorPathStr, sensorType, objectServer, dbusConnection, io,
971 sensorName, std::move(sensorThresholds), *interfacePath,
972 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800973 psuProperty.maxReading, psuProperty.minReading,
974 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800975 pollRate, i2cDev);
976 sensors[sensorName]->setupRead();
977 ++numCreated;
978 if constexpr (debug)
979 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400980 std::cerr
981 << "Created " << numCreated << " sensors so far\n";
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800982 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700983 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800984 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800985
Joseph Fub844e972023-03-25 10:24:30 +0800986 if (devType == DevTypes::HWMON)
987 {
988 // OperationalStatus event
989 combineEvents[*psuName + "OperationalStatus"] = nullptr;
990 combineEvents[*psuName + "OperationalStatus"] =
991 std::make_unique<PSUCombineEvent>(
992 objectServer, dbusConnection, io, *psuName, readState,
993 eventPathList, groupEventPathList, "OperationalStatus",
994 pollRate);
995 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800996 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700997
Ed Tanous8a57ec02020-10-09 12:46:52 -0700998 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700999 {
1000 std::cerr << "Created total of " << numCreated << " sensors\n";
1001 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001002}
Cheng C Yang209ec562019-03-12 16:37:44 +08001003
Patrick Williams556e04b2025-02-01 08:22:22 -05001004static void getPresentCpus(
1005 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Matt Simmering39fadb92023-05-04 21:47:29 -07001006{
1007 static const int depth = 2;
1008 static const int numKeys = 1;
1009 GetSubTreeType cpuSubTree;
1010
1011 try
1012 {
1013 auto getItems = dbusConnection->new_method_call(
1014 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
1015 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
1016 std::array<const char*, numKeys>{
1017 "xyz.openbmc_project.Inventory.Item"});
1018 auto getItemsResp = dbusConnection->call(getItems);
1019 getItemsResp.read(cpuSubTree);
1020 }
1021 catch (sdbusplus::exception_t& e)
1022 {
1023 std::cerr << "error getting inventory item subtree: " << e.what()
1024 << "\n";
1025 return;
1026 }
1027
1028 for (const auto& [path, objDict] : cpuSubTree)
1029 {
1030 auto obj = sdbusplus::message::object_path(path).filename();
Patrick Rudolph457715b2024-04-10 14:51:35 +02001031 boost::to_lower(obj);
1032
Matt Simmering39fadb92023-05-04 21:47:29 -07001033 if (!obj.starts_with("cpu") || objDict.empty())
1034 {
1035 continue;
1036 }
1037 const std::string& owner = objDict.begin()->first;
1038
1039 std::variant<bool> respValue;
1040 try
1041 {
1042 auto getPresence = dbusConnection->new_method_call(
1043 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1044 "Get");
1045 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1046 auto resp = dbusConnection->call(getPresence);
1047 resp.read(respValue);
1048 }
1049 catch (sdbusplus::exception_t& e)
1050 {
1051 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1052 continue;
1053 }
1054
1055 auto* present = std::get_if<bool>(&respValue);
1056 if (present != nullptr && *present)
1057 {
1058 int cpuIndex = 0;
1059 try
1060 {
Patrick Rudolph457715b2024-04-10 14:51:35 +02001061 cpuIndex = std::stoi(obj.substr(obj.size() - 1));
Matt Simmering39fadb92023-05-04 21:47:29 -07001062 }
1063 catch (const std::exception& e)
1064 {
1065 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1066 continue;
1067 }
Patrick Rudolph457715b2024-04-10 14:51:35 +02001068 cpuPresence[cpuIndex] = *present;
Matt Simmering39fadb92023-05-04 21:47:29 -07001069 }
1070 }
1071}
1072
Zhikui Ren23c96e72020-11-05 22:32:28 -08001073void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001074 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001075 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1076 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001077 sensorsChanged,
1078 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001079{
1080 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001081 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1082 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001083 createSensorsCallback(io, objectServer, dbusConnection,
1084 sensorConfigs, sensorsChanged, activateOnly);
1085 });
Matt Simmering786efb82023-01-18 14:09:21 -08001086 std::vector<std::string> types(sensorTypes.size());
1087 for (const auto& [type, dt] : sensorTypes)
1088 {
1089 types.push_back(type);
1090 }
1091 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001092}
1093
Ed Tanous201a1012024-04-03 18:07:28 -07001094void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001095{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001096 sensorTable = {{"power", sensor_paths::unitWatts},
1097 {"curr", sensor_paths::unitAmperes},
1098 {"temp", sensor_paths::unitDegreesC},
1099 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001100 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001101 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001102
Jeff Line41d52f2021-04-07 19:38:51 +08001103 labelMatch = {
1104 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001105 {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
1106 {"power", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001107 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1108 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1109 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001110 {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Ian Chienc82a4092024-07-24 10:52:59 +08001111 {"voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001112 {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001113 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001114 {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001115 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001116 {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
1117 {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
1118 {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1119 {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
1120 {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1121 {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001122
1123 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1124 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1125
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001126 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1127 {"Failure", {"in2_alarm"}},
1128 {"ACLost", {"in1_beep"}},
1129 {"ConfigureError", {"in1_fault"}}};
1130
Joseph Fub844e972023-03-25 10:24:30 +08001131 devParamMap = {
1132 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1133 {DevTypes::IIO,
1134 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001135}
1136
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001137static void powerStateChanged(
1138 PowerState type, bool newState,
1139 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1140 sensors,
1141 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1142 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1143{
1144 if (newState)
1145 {
1146 createSensors(io, objectServer, dbusConnection, nullptr, true);
1147 }
1148 else
1149 {
1150 for (auto& [path, sensor] : sensors)
1151 {
1152 if (sensor != nullptr && sensor->readState == type)
1153 {
1154 sensor->deactivate();
1155 }
1156 }
1157 }
1158}
1159
James Feistb6c0b912019-07-09 12:21:44 -07001160int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001161{
Ed Tanous1f978632023-02-28 18:16:39 -08001162 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001163 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1164
Ed Tanous14ed5e92022-07-12 15:50:23 -07001165 sdbusplus::asio::object_server objectServer(systemBus, true);
1166 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001167 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001168 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001169 auto sensorsChanged =
1170 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001171
Cheng C Yang916360b2019-05-07 18:47:16 +08001172 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001173
Patrick Williams2aaf7172024-08-16 15:20:40 -04001174 auto powerCallBack = [&io, &objectServer,
1175 &systemBus](PowerState type, bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001176 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1177 };
1178
1179 setupPowerMatchCallback(systemBus, powerCallBack);
1180
1181 boost::asio::post(io, [&]() {
1182 createSensors(io, objectServer, systemBus, nullptr, false);
1183 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001184 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001185 std::function<void(sdbusplus::message_t&)> eventHandler =
1186 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001187 if (message.is_method_error())
Cheng C Yang209ec562019-03-12 16:37:44 +08001188 {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001189 std::cerr << "callback method error\n";
Cheng C Yang209ec562019-03-12 16:37:44 +08001190 return;
1191 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001192 sensorsChanged->insert(message.get_path());
1193 filterTimer.expires_after(std::chrono::seconds(3));
1194 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1195 if (ec == boost::asio::error::operation_aborted)
1196 {
1197 return;
1198 }
1199 if (ec)
1200 {
1201 std::cerr << "timer error\n";
1202 }
1203 createSensors(io, objectServer, systemBus, sensorsChanged,
1204 false);
1205 });
1206 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001207
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001208 boost::asio::steady_timer cpuFilterTimer(io);
1209 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1210 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001211 std::string path = message.get_path();
1212 boost::to_lower(path);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001213
Patrick Williams2aaf7172024-08-16 15:20:40 -04001214 sdbusplus::message::object_path cpuPath(path);
1215 std::string cpuName = cpuPath.filename();
1216 if (!cpuName.starts_with("cpu"))
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001217 {
1218 return;
1219 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001220 size_t index = 0;
1221 try
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001222 {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001223 index = std::stoi(path.substr(path.size() - 1));
1224 }
1225 catch (const std::invalid_argument&)
1226 {
1227 std::cerr << "Found invalid path " << path << "\n";
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001228 return;
1229 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001230
1231 std::string objectName;
1232 boost::container::flat_map<std::string, std::variant<bool>> values;
1233 message.read(objectName, values);
1234 auto findPresence = values.find("Present");
Vikash Chandolaf2fc1b32024-08-29 15:59:27 +05301235 if (findPresence == values.end())
1236 {
1237 return;
1238 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001239 try
1240 {
1241 cpuPresence[index] = std::get<bool>(findPresence->second);
1242 }
1243 catch (const std::bad_variant_access& err)
1244 {
1245 return;
1246 }
1247
1248 if (!cpuPresence[index])
1249 {
1250 return;
1251 }
1252 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1253 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1254 if (ec == boost::asio::error::operation_aborted)
1255 {
1256 return;
1257 }
1258 if (ec)
1259 {
1260 std::cerr << "timer error\n";
1261 return;
1262 }
1263 createSensors(io, objectServer, systemBus, nullptr, false);
1264 });
1265 };
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001266
Zev Weiss214d9712022-08-12 12:54:31 -07001267 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1268 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001269
1270 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1271 static_cast<sdbusplus::bus_t&>(*systemBus),
1272 "type='signal',member='PropertiesChanged',path_namespace='" +
1273 std::string(cpuInventoryPath) +
1274 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1275 cpuPresenceHandler));
1276
Matt Simmering39fadb92023-05-04 21:47:29 -07001277 getPresentCpus(systemBus);
1278
Bruce Lee1263c3d2021-06-04 15:16:33 +08001279 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001280 io.run();
1281}