blob: 3f66a90f17a4ddce0a6735c6f24eae20e6a5a966 [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"
20#include "Utils.hpp"
21
Matt Simmeringe4e6a282023-03-03 14:41:04 -080022#include <boost/algorithm/string/case_conv.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080025#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070031#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070032#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080035#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080036#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080038#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <utility>
40#include <variant>
41#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080042
Ed Tanous8a57ec02020-10-09 12:46:52 -070043static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070044
Matt Simmering786efb82023-01-18 14:09:21 -080045static const I2CDeviceTypeMap sensorTypes{
Delphine CC Chiu436c5bd2023-09-22 09:47:43 +080046 {"ADC128D818", I2CDeviceType{"adc128d818", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080047 {"ADM1266", I2CDeviceType{"adm1266", true}},
48 {"ADM1272", I2CDeviceType{"adm1272", true}},
49 {"ADM1275", I2CDeviceType{"adm1275", true}},
50 {"ADM1278", I2CDeviceType{"adm1278", true}},
51 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080052 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080053 {"ADS7830", I2CDeviceType{"ads7830", true}},
54 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
55 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020056 {"cffps", I2CDeviceType{"cffps", true}},
57 {"cffps1", I2CDeviceType{"cffps", true}},
58 {"cffps2", I2CDeviceType{"cffps", true}},
59 {"cffps3", I2CDeviceType{"cffps", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080060 {"DPS800", I2CDeviceType{"dps800", true}},
61 {"INA219", I2CDeviceType{"ina219", true}},
62 {"INA230", I2CDeviceType{"ina230", true}},
Potin Lai5674ac92023-11-20 14:33:56 +080063 {"INA238", I2CDeviceType{"ina238", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080064 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080065 {"IR38060", I2CDeviceType{"ir38060", true}},
66 {"IR38164", I2CDeviceType{"ir38164", true}},
67 {"IR38263", I2CDeviceType{"ir38263", true}},
68 {"ISL68137", I2CDeviceType{"isl68137", true}},
69 {"ISL68220", I2CDeviceType{"isl68220", true}},
70 {"ISL68223", I2CDeviceType{"isl68223", true}},
71 {"ISL69225", I2CDeviceType{"isl69225", true}},
72 {"ISL69243", I2CDeviceType{"isl69243", true}},
73 {"ISL69260", I2CDeviceType{"isl69260", true}},
74 {"LM25066", I2CDeviceType{"lm25066", true}},
Yi-Shum77e87692023-11-30 10:27:45 +080075 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +020076 {"MAX5970", I2CDeviceType{"max5970", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080077 {"MAX16601", I2CDeviceType{"max16601", true}},
78 {"MAX20710", I2CDeviceType{"max20710", true}},
79 {"MAX20730", I2CDeviceType{"max20730", true}},
80 {"MAX20734", I2CDeviceType{"max20734", true}},
81 {"MAX20796", I2CDeviceType{"max20796", true}},
82 {"MAX34451", I2CDeviceType{"max34451", true}},
83 {"MP2971", I2CDeviceType{"mp2971", true}},
84 {"MP2973", I2CDeviceType{"mp2973", true}},
85 {"MP2975", I2CDeviceType{"mp2975", true}},
86 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +080087 {"MP5990", I2CDeviceType{"mp5990", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080088 {"NCP4200", I2CDeviceType{"ncp4200", true}},
89 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
90 {"pmbus", I2CDeviceType{"pmbus", true}},
91 {"PXE1610", I2CDeviceType{"pxe1610", true}},
92 {"RAA228000", I2CDeviceType{"raa228000", true}},
93 {"RAA228228", I2CDeviceType{"raa228228", true}},
94 {"RAA228620", I2CDeviceType{"raa228620", true}},
95 {"RAA229001", I2CDeviceType{"raa229001", true}},
96 {"RAA229004", I2CDeviceType{"raa229004", true}},
97 {"RAA229126", I2CDeviceType{"raa229126", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +020098 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080099 {"TPS53679", I2CDeviceType{"tps53679", true}},
100 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
101 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
102 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800103 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800104};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700105
Joseph Fub844e972023-03-25 10:24:30 +0800106enum class DevTypes
107{
108 Unknown = 0,
109 HWMON,
110 IIO
111};
112
113struct DevParams
114{
115 unsigned int matchIndex = 0;
116 std::string matchRegEx;
117 std::string nameRegEx;
118};
119
Cheng C Yang209ec562019-03-12 16:37:44 +0800120namespace fs = std::filesystem;
121
Yong Libf8b1da2020-04-15 16:32:50 +0800122static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800123 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800124static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
125 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800126static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
127 pwmSensors;
128static boost::container::flat_map<std::string, std::string> sensorTable;
129static boost::container::flat_map<std::string, PSUProperty> labelMatch;
130static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800131static boost::container::flat_map<std::string, std::vector<std::string>>
132 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800133static boost::container::flat_map<
134 std::string,
135 boost::container::flat_map<std::string, std::vector<std::string>>>
136 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800137static boost::container::flat_map<std::string, std::vector<std::string>>
138 limitEventMatch;
139
Josh Lehan74d9bd92019-10-31 08:51:58 -0700140static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800141static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800142static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700143
Cheng C Yang58b2b532019-05-31 00:19:45 +0800144// Function CheckEvent will check each attribute from eventMatch table in the
145// sysfs. If the attributes exists in sysfs, then store the complete path
146// of the attribute into eventPathList.
147void checkEvent(
148 const std::string& directory,
149 const boost::container::flat_map<std::string, std::vector<std::string>>&
150 eventMatch,
151 boost::container::flat_map<std::string, std::vector<std::string>>&
152 eventPathList)
153{
154 for (const auto& match : eventMatch)
155 {
156 const std::vector<std::string>& eventAttrs = match.second;
157 const std::string& eventName = match.first;
158 for (const auto& eventAttr : eventAttrs)
159 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700160 std::string eventPath = directory;
161 eventPath += "/";
162 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800163
164 std::ifstream eventFile(eventPath);
165 if (!eventFile.good())
166 {
167 continue;
168 }
169
170 eventPathList[eventName].push_back(eventPath);
171 }
172 }
173}
174
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800175// Check Group Events which contains more than one targets in each combine
176// events.
177void checkGroupEvent(
178 const std::string& directory,
179 const boost::container::flat_map<
180 std::string,
181 boost::container::flat_map<std::string, std::vector<std::string>>>&
182 groupEventMatch,
183 boost::container::flat_map<
184 std::string,
185 boost::container::flat_map<std::string, std::vector<std::string>>>&
186 groupEventPathList)
187{
188 for (const auto& match : groupEventMatch)
189 {
190 const std::string& groupEventName = match.first;
191 const boost::container::flat_map<std::string, std::vector<std::string>>
192 events = match.second;
193 boost::container::flat_map<std::string, std::vector<std::string>>
194 pathList;
195 for (const auto& match : events)
196 {
197 const std::string& eventName = match.first;
198 const std::vector<std::string>& eventAttrs = match.second;
199 for (const auto& eventAttr : eventAttrs)
200 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700201 std::string eventPath = directory;
202 eventPath += "/";
203 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800204 std::ifstream eventFile(eventPath);
205 if (!eventFile.good())
206 {
207 continue;
208 }
209
210 pathList[eventName].push_back(eventPath);
211 }
212 }
213 groupEventPathList[groupEventName] = pathList;
214 }
215}
216
Cheng C Yang58b2b532019-05-31 00:19:45 +0800217// Function checkEventLimits will check all the psu related xxx_input attributes
218// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
219// xxx_min_alarm exist, then store the existing paths of the alarm attributes
220// to eventPathList.
221void checkEventLimits(
222 const std::string& sensorPathStr,
223 const boost::container::flat_map<std::string, std::vector<std::string>>&
224 limitEventMatch,
225 boost::container::flat_map<std::string, std::vector<std::string>>&
226 eventPathList)
227{
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
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530260static void
261 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
262 const std::string& interfacePath,
263 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
264 sdbusplus::asio::object_server& objectServer,
265 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800266{
Zev Weiss741f26e2022-08-12 18:21:02 -0700267 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800268 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700269 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800270 {
271 continue;
272 }
273
274 const std::string& sensorPathStr = sensorPath.string();
275 const std::string& pwmPathStr =
276 boost::replace_all_copy(sensorPathStr, "input", "target");
277 std::ifstream pwmFile(pwmPathStr);
278 if (!pwmFile.good())
279 {
280 continue;
281 }
282
283 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
284 if (findPWMSensor != pwmSensors.end())
285 {
286 continue;
287 }
288
Zev Weissd8c293a2022-08-15 18:58:41 -0700289 std::string name = "Pwm_";
290 name += psuName;
291 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700292 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700293
294 std::string objPath = interfacePath;
295 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700296 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700297
Cheng C Yang916360b2019-05-07 18:47:16 +0800298 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700299 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800300 }
301}
302
Zhikui Ren23c96e72020-11-05 22:32:28 -0800303static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800304 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800305 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
306 const ManagedObjectType& sensorConfigs,
307 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800308 sensorsChanged,
309 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800310{
Josh Lehan49cfba92019-10-08 16:50:42 -0700311 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800312 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800313
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800314 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
315
Cheng C Yang209ec562019-03-12 16:37:44 +0800316 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800317 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
318 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
319 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800320 {
321 std::cerr << "No PSU sensors in system\n";
322 return;
323 }
324
325 boost::container::flat_set<std::string> directories;
326 for (const auto& pmbusPath : pmbusPaths)
327 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800328 boost::container::flat_map<std::string, std::vector<std::string>>
329 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800330 boost::container::flat_map<
331 std::string,
332 boost::container::flat_map<std::string, std::vector<std::string>>>
333 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800334
335 std::ifstream nameFile(pmbusPath);
336 if (!nameFile.good())
337 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700338 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800339 continue;
340 }
341
342 std::string pmbusName;
343 std::getline(nameFile, pmbusName);
344 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700345
Matt Simmering786efb82023-01-18 14:09:21 -0800346 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800347 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700348 // To avoid this error message, add your driver name to
349 // the pmbusNames vector at the top of this file.
350 std::cerr << "Driver name " << pmbusName
351 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800352 continue;
353 }
354
Cheng C Yang209ec562019-03-12 16:37:44 +0800355 auto directory = pmbusPath.parent_path();
356
357 auto ret = directories.insert(directory.string());
358 if (!ret.second)
359 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700360 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800361 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 }
363
Joseph Fub844e972023-03-25 10:24:30 +0800364 DevTypes devType = DevTypes::HWMON;
365 std::string deviceName;
366 if (directory.parent_path() == "/sys/class/hwmon")
367 {
368 deviceName = fs::canonical(directory / "device").stem();
369 }
370 else
371 {
372 deviceName = fs::canonical(directory).parent_path().stem();
373 devType = DevTypes::IIO;
374 }
375
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 size_t bus = 0;
377 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000378 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 continue;
381 }
382
Zev Weisse8b97ee2022-08-12 15:23:51 -0700383 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800384 const SensorData* sensorData = nullptr;
385 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800386 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800387 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800388
Zev Weiss741f26e2022-08-12 18:21:02 -0700389 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800390 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700391 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800392 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800393 {
Zev Weiss054aad82022-08-18 01:37:34 -0700394 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 if (sensorBase != sensorData->end())
396 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700397 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 sensorType = type;
399 break;
400 }
401 }
402 if (baseConfig == nullptr)
403 {
404 std::cerr << "error finding base configuration for "
405 << deviceName << "\n";
406 continue;
407 }
408
Zev Weisse8b97ee2022-08-12 15:23:51 -0700409 auto configBus = baseConfig->find("Bus");
410 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800411
Zev Weisse8b97ee2022-08-12 15:23:51 -0700412 if (configBus == baseConfig->end() ||
413 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800414 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800415 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 continue;
417 }
418
Matt Simmering6747eba2023-01-18 16:03:55 -0800419 const uint64_t* confBus =
420 std::get_if<uint64_t>(&(configBus->second));
421 const uint64_t* confAddr =
422 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800423 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800424 {
425 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700426 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800427 continue;
428 }
429
430 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800431 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200432 if constexpr (debug)
433 {
434 std::cerr << "Configuration skipping " << *confBus << "-"
435 << *confAddr << " because not " << bus << "-"
436 << addr << "\n";
437 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 continue;
439 }
440
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800441 std::vector<thresholds::Threshold> confThresholds;
442 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
443 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800444 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800445 }
446 thresholdConfSize = confThresholds.size();
447
Zev Weiss741f26e2022-08-12 18:21:02 -0700448 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800449 break;
450 }
451 if (interfacePath == nullptr)
452 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700453 // To avoid this error message, add your export map entry,
454 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800455 std::cerr << "failed to find match for " << deviceName << "\n";
456 continue;
457 }
458
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800459 auto findI2CDev = devices.find(*interfacePath);
460
461 std::shared_ptr<I2CDevice> i2cDev;
462 if (findI2CDev != devices.end())
463 {
464 if (activateOnly && !findI2CDev->second.second)
465 {
466 continue;
467 }
468 i2cDev = findI2CDev->second.first;
469 }
470
Zev Weisse8b97ee2022-08-12 15:23:51 -0700471 auto findPSUName = baseConfig->find("Name");
472 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800473 {
474 std::cerr << "could not determine configuration name for "
475 << deviceName << "\n";
476 continue;
477 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800478 const std::string* psuName =
479 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800480 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800481 {
482 std::cerr << "Cannot find psu name, invalid configuration\n";
483 continue;
484 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800485
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800486 auto findCPU = baseConfig->find("CPURequired");
487 if (findCPU != baseConfig->end())
488 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500489 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800490 auto presenceFind = cpuPresence.find(index);
491 if (presenceFind == cpuPresence.end() || !presenceFind->second)
492 {
493 continue;
494 }
495 }
496
Zhikui Ren23c96e72020-11-05 22:32:28 -0800497 // on rescans, only update sensors we were signaled by
498 if (!firstScan)
499 {
Zhikui Renda98f092021-11-01 09:41:08 -0700500 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500501 auto it = std::find_if(sensorsChanged->begin(),
502 sensorsChanged->end(),
503 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800504 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500505 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800506
507 if (it == sensorsChanged->end())
508 {
509 continue;
510 }
511 sensorsChanged->erase(it);
512 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800513 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800514 checkGroupEvent(directory.string(), groupEventMatch,
515 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800516
Zev Weisse8b97ee2022-08-12 15:23:51 -0700517 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000518
Vijay Khemka996bad12019-05-28 15:15:16 -0700519 /* Check if there are more sensors in the same interface */
520 int i = 1;
521 std::vector<std::string> psuNames;
522 do
523 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700524 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700525 psuNames.push_back(
526 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700527 findPSUName = baseConfig->find("Name" + std::to_string(i++));
528 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700529
Cheng C Yange50345b2019-04-02 17:26:15 +0800530 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800531 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
532 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800533 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800534 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800535 continue;
536 }
537
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530538 /* read max value in sysfs for in, curr, power, temp, ... */
539 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
540 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700541 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530542 {
543 std::cerr << "No max name in PSU \n";
544 }
545 }
546
Zev Weiss8569bf22022-10-11 15:37:44 -0700547 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800548
Vijay Khemka996bad12019-05-28 15:15:16 -0700549 /* Find array of labels to be exposed if it is defined in config */
550 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700551 auto findLabelObj = baseConfig->find("Labels");
552 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700553 {
554 findLabels =
555 std::get<std::vector<std::string>>(findLabelObj->second);
556 }
557
Joseph Fub844e972023-03-25 10:24:30 +0800558 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700559 std::smatch matches;
560
Cheng C Yange50345b2019-04-02 17:26:15 +0800561 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800562 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530563 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800564 std::string labelHead;
565 std::string sensorPathStr = sensorPath.string();
566 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700567 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700568 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
569 {
Josh Lehan06494452019-10-31 09:49:16 -0700570 // hwmon *_input filename without number:
571 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800572 // iio in_*_raw filename without number:
573 // voltage, temp, pressure, ...
574 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700575 }
576 else
577 {
Josh Lehan06494452019-10-31 09:49:16 -0700578 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700579 << sensorNameStr;
580 continue;
581 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800582
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530583 std::string labelPath;
584
Joseph Fub844e972023-03-25 10:24:30 +0800585 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530586 {
Joseph Fub844e972023-03-25 10:24:30 +0800587 /* find and differentiate _max and _input to replace "label" */
588 size_t pos = sensorPathStr.find('_');
589 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530590 {
Joseph Fub844e972023-03-25 10:24:30 +0800591 std::string sensorPathStrMax = sensorPathStr.substr(pos);
592 if (sensorPathStrMax == "_max")
593 {
594 labelPath = boost::replace_all_copy(sensorPathStr,
595 "max", "label");
596 maxLabel = true;
597 }
598 else
599 {
600 labelPath = boost::replace_all_copy(sensorPathStr,
601 "input", "label");
602 maxLabel = false;
603 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530604 }
605 else
606 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800607 continue;
608 }
609
Joseph Fub844e972023-03-25 10:24:30 +0800610 std::ifstream labelFile(labelPath);
611 if (!labelFile.good())
612 {
613 if constexpr (debug)
614 {
615 std::cerr << "Input file " << sensorPath
616 << " has no corresponding label file\n";
617 }
618 // hwmon *_input filename with number:
619 // temp1, temp2, temp3, ...
620 labelHead = sensorNameStr.substr(0,
621 sensorNameStr.find('_'));
622 }
623 else
624 {
625 std::string label;
626 std::getline(labelFile, label);
627 labelFile.close();
628 auto findSensor = sensors.find(label);
629 if (findSensor != sensors.end())
630 {
631 continue;
632 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800633
Joseph Fub844e972023-03-25 10:24:30 +0800634 // hwmon corresponding *_label file contents:
635 // vin1, vout1, ...
636 labelHead = label.substr(0, label.find(' '));
637 }
638
639 /* append "max" for labelMatch */
640 if (maxLabel)
641 {
642 labelHead.insert(0, "max");
643 }
644
645 checkPWMSensor(sensorPath, labelHead, *interfacePath,
646 dbusConnection, objectServer, psuNames[0]);
647 }
648 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530649 {
Joseph Fub844e972023-03-25 10:24:30 +0800650 auto findIIOHyphen = sensorNameStr.find_last_of('_');
651 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530652 }
653
Ed Tanous8a57ec02020-10-09 12:46:52 -0700654 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700655 {
656 std::cerr << "Sensor type=\"" << sensorNameSubStr
657 << "\" label=\"" << labelHead << "\"\n";
658 }
659
Vijay Khemka996bad12019-05-28 15:15:16 -0700660 if (!findLabels.empty())
661 {
662 /* Check if this labelHead is enabled in config file */
663 if (std::find(findLabels.begin(), findLabels.end(),
664 labelHead) == findLabels.end())
665 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700666 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800667 {
668 std::cerr << "could not find " << labelHead
669 << " in the Labels list\n";
670 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700671 continue;
672 }
673 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800674
Cheng C Yange50345b2019-04-02 17:26:15 +0800675 auto findProperty = labelMatch.find(labelHead);
676 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800677 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700678 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700679 {
680 std::cerr << "Could not find matching default property for "
681 << labelHead << "\n";
682 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800683 continue;
684 }
685
Josh Lehan74d9bd92019-10-31 08:51:58 -0700686 // Protect the hardcoded labelMatch list from changes,
687 // by making a copy and modifying that instead.
688 // Avoid bleedthrough of one device's customizations to
689 // the next device, as each should be independently customizable.
690 psuProperties.push_back(findProperty->second);
691 auto psuProperty = psuProperties.rbegin();
692
693 // Use label head as prefix for reading from config file,
694 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
695 std::string keyName = labelHead + "_Name";
696 std::string keyScale = labelHead + "_Scale";
697 std::string keyMin = labelHead + "_Min";
698 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800699 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800700 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701
702 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700703 auto findCustomName = baseConfig->find(keyName);
704 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700705 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700706 try
707 {
708 psuProperty->labelTypeName = std::visit(
709 VariantToStringVisitor(), findCustomName->second);
710 }
Patrick Williams26601e82021-10-06 12:43:25 -0500711 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700712 {
713 std::cerr << "Unable to parse " << keyName << "\n";
714 continue;
715 }
716
717 // All strings are valid, including empty string
718 customizedName = true;
719 }
720
721 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700722 auto findCustomScale = baseConfig->find(keyScale);
723 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700724 {
725 try
726 {
727 psuProperty->sensorScaleFactor = std::visit(
728 VariantToUnsignedIntVisitor(), findCustomScale->second);
729 }
Patrick Williams26601e82021-10-06 12:43:25 -0500730 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700731 {
732 std::cerr << "Unable to parse " << keyScale << "\n";
733 continue;
734 }
735
736 // Avoid later division by zero
737 if (psuProperty->sensorScaleFactor > 0)
738 {
739 customizedScale = true;
740 }
741 else
742 {
743 std::cerr << "Unable to accept " << keyScale << "\n";
744 continue;
745 }
746 }
747
Zev Weisse8b97ee2022-08-12 15:23:51 -0700748 auto findCustomMin = baseConfig->find(keyMin);
749 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700750 {
751 try
752 {
753 psuProperty->minReading = std::visit(
754 VariantToDoubleVisitor(), findCustomMin->second);
755 }
Patrick Williams26601e82021-10-06 12:43:25 -0500756 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700757 {
758 std::cerr << "Unable to parse " << keyMin << "\n";
759 continue;
760 }
761 }
762
Zev Weisse8b97ee2022-08-12 15:23:51 -0700763 auto findCustomMax = baseConfig->find(keyMax);
764 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700765 {
766 try
767 {
768 psuProperty->maxReading = std::visit(
769 VariantToDoubleVisitor(), findCustomMax->second);
770 }
Patrick Williams26601e82021-10-06 12:43:25 -0500771 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700772 {
773 std::cerr << "Unable to parse " << keyMax << "\n";
774 continue;
775 }
776 }
777
Zev Weisse8b97ee2022-08-12 15:23:51 -0700778 auto findCustomOffset = baseConfig->find(keyOffset);
779 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800780 {
781 try
782 {
783 psuProperty->sensorOffset = std::visit(
784 VariantToDoubleVisitor(), findCustomOffset->second);
785 }
Patrick Williams26601e82021-10-06 12:43:25 -0500786 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800787 {
788 std::cerr << "Unable to parse " << keyOffset << "\n";
789 continue;
790 }
791 }
792
Lotus Xucb5af732021-09-10 15:18:50 +0800793 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700794 auto findPowerState = baseConfig->find(keyPowerState);
795 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800796 {
797 std::string powerState = std::visit(VariantToStringVisitor(),
798 findPowerState->second);
799 setReadState(powerState, readState);
800 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700801 if (!(psuProperty->minReading < psuProperty->maxReading))
802 {
803 std::cerr << "Min must be less than Max\n";
804 continue;
805 }
806
807 // If the sensor name is being customized by config file,
808 // then prefix/suffix composition becomes not necessary,
809 // and in fact not wanted, because it gets in the way.
810 std::string psuNameFromIndex;
811 if (!customizedName)
812 {
813 /* Find out sensor name index for this label */
814 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500815 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700816 if (std::regex_search(labelHead, matches, rgx))
817 {
818 nameIndex = std::stoi(matches[1]);
819
820 // Decrement to preserve alignment, because hwmon
821 // human-readable filenames and labels use 1-based
822 // numbering, but the "Name", "Name1", "Name2", etc. naming
823 // convention (the psuNames vector) uses 0-based numbering.
824 if (nameIndex > 0)
825 {
826 --nameIndex;
827 }
828 }
829 else
830 {
831 nameIndex = 0;
832 }
833
834 if (psuNames.size() <= nameIndex)
835 {
836 std::cerr << "Could not pair " << labelHead
837 << " with a Name field\n";
838 continue;
839 }
840
841 psuNameFromIndex = psuNames[nameIndex];
842
Ed Tanous8a57ec02020-10-09 12:46:52 -0700843 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700844 {
845 std::cerr << "Sensor label head " << labelHead
846 << " paired with " << psuNameFromIndex
847 << " at index " << nameIndex << "\n";
848 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700849 }
850
Joseph Fub844e972023-03-25 10:24:30 +0800851 if (devType == DevTypes::HWMON)
852 {
853 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
854 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800855
Josh Lehan74d9bd92019-10-31 08:51:58 -0700856 // Similarly, if sensor scaling factor is being customized,
857 // then the below power-of-10 constraint becomes unnecessary,
858 // as config should be able to specify an arbitrary divisor.
859 unsigned int factor = psuProperty->sensorScaleFactor;
860 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700861 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700862 // Preserve existing usage of hardcoded labelMatch table below
863 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700864
Josh Lehan74d9bd92019-10-31 08:51:58 -0700865 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700866 char firstChar =
867 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700868 std::string strScaleFactor =
869 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
870
871 // Preserve existing configs by accepting earlier syntax,
872 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700873 auto findScaleFactor = baseConfig->find(strScaleFactor);
874 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700875 {
876 factor = std::visit(VariantToIntVisitor(),
877 findScaleFactor->second);
878 }
879
Ed Tanous8a57ec02020-10-09 12:46:52 -0700880 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700881 {
882 std::cerr << "Sensor scaling factor " << factor
883 << " string " << strScaleFactor << "\n";
884 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700885 }
886
Vijay Khemka996bad12019-05-28 15:15:16 -0700887 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530888 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
889 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800890 {
James Feist17ab6e02019-06-25 12:28:13 -0700891 std::cerr << "error populating thresholds for "
892 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800893 }
894
Zev Weiss6b6891c2021-04-22 02:46:21 -0500895 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
896 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800897 {
Jason Ling5747fab2019-10-02 16:46:23 -0700898 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700899 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800900 continue;
901 }
902
Ed Tanous8a57ec02020-10-09 12:46:52 -0700903 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700904 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700905 std::cerr << "Sensor properties: Name \""
906 << psuProperty->labelTypeName << "\" Scale "
907 << psuProperty->sensorScaleFactor << " Min "
908 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800909 << psuProperty->maxReading << " Offset "
910 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700911 }
912
913 std::string sensorName = psuProperty->labelTypeName;
914 if (customizedName)
915 {
916 if (sensorName.empty())
917 {
918 // Allow selective disabling of an individual sensor,
919 // by customizing its name to an empty string.
920 std::cerr << "Sensor disabled, empty string\n";
921 continue;
922 }
923 }
924 else
925 {
926 // Sensor name not customized, do prefix/suffix composition,
927 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500928 sensorName = psuNameFromIndex + " " +
929 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700930 }
931
Ed Tanous8a57ec02020-10-09 12:46:52 -0700932 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700933 {
934 std::cerr << "Sensor name \"" << sensorName << "\" path \""
935 << sensorPathStr << "\" type \"" << sensorType
936 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700937 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800938 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800939
940 auto& sensor = sensors[sensorName];
941 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700942 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800943 sensor = nullptr;
944 }
945
946 if (sensor != nullptr)
947 {
948 sensor->activate(sensorPathStr, i2cDev);
949 }
950 else
951 {
952 sensors[sensorName] = std::make_shared<PSUSensor>(
953 sensorPathStr, sensorType, objectServer, dbusConnection, io,
954 sensorName, std::move(sensorThresholds), *interfacePath,
955 readState, findSensorUnit->second, factor,
956 psuProperty->maxReading, psuProperty->minReading,
957 psuProperty->sensorOffset, labelHead, thresholdConfSize,
958 pollRate, i2cDev);
959 sensors[sensorName]->setupRead();
960 ++numCreated;
961 if constexpr (debug)
962 {
963 std::cerr << "Created " << numCreated
964 << " sensors so far\n";
965 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700966 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800967 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800968
Joseph Fub844e972023-03-25 10:24:30 +0800969 if (devType == DevTypes::HWMON)
970 {
971 // OperationalStatus event
972 combineEvents[*psuName + "OperationalStatus"] = nullptr;
973 combineEvents[*psuName + "OperationalStatus"] =
974 std::make_unique<PSUCombineEvent>(
975 objectServer, dbusConnection, io, *psuName, readState,
976 eventPathList, groupEventPathList, "OperationalStatus",
977 pollRate);
978 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800979 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700980
Ed Tanous8a57ec02020-10-09 12:46:52 -0700981 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700982 {
983 std::cerr << "Created total of " << numCreated << " sensors\n";
984 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800985}
Cheng C Yang209ec562019-03-12 16:37:44 +0800986
Matt Simmering39fadb92023-05-04 21:47:29 -0700987static void
988 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
989{
990 static const int depth = 2;
991 static const int numKeys = 1;
992 GetSubTreeType cpuSubTree;
993
994 try
995 {
996 auto getItems = dbusConnection->new_method_call(
997 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
998 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
999 std::array<const char*, numKeys>{
1000 "xyz.openbmc_project.Inventory.Item"});
1001 auto getItemsResp = dbusConnection->call(getItems);
1002 getItemsResp.read(cpuSubTree);
1003 }
1004 catch (sdbusplus::exception_t& e)
1005 {
1006 std::cerr << "error getting inventory item subtree: " << e.what()
1007 << "\n";
1008 return;
1009 }
1010
1011 for (const auto& [path, objDict] : cpuSubTree)
1012 {
1013 auto obj = sdbusplus::message::object_path(path).filename();
1014 if (!obj.starts_with("cpu") || objDict.empty())
1015 {
1016 continue;
1017 }
1018 const std::string& owner = objDict.begin()->first;
1019
1020 std::variant<bool> respValue;
1021 try
1022 {
1023 auto getPresence = dbusConnection->new_method_call(
1024 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1025 "Get");
1026 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1027 auto resp = dbusConnection->call(getPresence);
1028 resp.read(respValue);
1029 }
1030 catch (sdbusplus::exception_t& e)
1031 {
1032 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1033 continue;
1034 }
1035
1036 auto* present = std::get_if<bool>(&respValue);
1037 if (present != nullptr && *present)
1038 {
1039 int cpuIndex = 0;
1040 try
1041 {
1042 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1043 }
1044 catch (const std::exception& e)
1045 {
1046 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1047 continue;
1048 }
1049 cpuPresence[cpuIndex + 1] = *present;
1050 }
1051 }
1052}
1053
Zhikui Ren23c96e72020-11-05 22:32:28 -08001054void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001055 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001056 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1057 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001058 sensorsChanged,
1059 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001060{
1061 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001062 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1063 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001064 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1065 sensorsChanged, activateOnly);
1066 });
Matt Simmering786efb82023-01-18 14:09:21 -08001067 std::vector<std::string> types(sensorTypes.size());
1068 for (const auto& [type, dt] : sensorTypes)
1069 {
1070 types.push_back(type);
1071 }
1072 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001073}
1074
Cheng C Yang916360b2019-05-07 18:47:16 +08001075void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001076{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001077 sensorTable = {{"power", sensor_paths::unitWatts},
1078 {"curr", sensor_paths::unitAmperes},
1079 {"temp", sensor_paths::unitDegreesC},
1080 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001081 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001082 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001083
Jeff Line41d52f2021-04-07 19:38:51 +08001084 labelMatch = {
1085 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001086 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1087 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001088 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1089 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1090 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1091 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001092 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1093 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1094 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001095 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1096 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001097 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1098 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001099 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001100 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1101 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1102 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1103 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001104 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1117 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1118 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1119 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1125 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1126 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1127 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1128 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1129 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1130 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1131 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1132 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1133 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1134 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1135 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1136 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001137 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001138 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001139 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1140 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1141 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1142 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1143 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1144 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001145 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001146 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1147 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001148 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1149 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1150 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1151 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1152 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1153 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1154 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1155 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1156 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1157 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1158 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1159 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1160 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1161 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1162 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001163 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1164 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1165 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001166 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1167 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1168 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1169 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1170 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1171 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1172 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1173 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1174 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001175 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1176 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1177 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001178
Matt Simmering6747eba2023-01-18 16:03:55 -08001179 pwmTable = {{"fan1", "Fan_1"},
1180 {"fan2", "Fan_2"},
1181 {"fan3", "Fan_3"},
1182 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001183
1184 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1185 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1186
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001187 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1188 {"Failure", {"in2_alarm"}},
1189 {"ACLost", {"in1_beep"}},
1190 {"ConfigureError", {"in1_fault"}}};
1191
1192 groupEventMatch = {{"FanFault",
1193 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001194 {"fan2", {"fan2_alarm", "fan2_fault"}},
1195 {"fan3", {"fan3_alarm", "fan3_fault"}},
1196 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001197
1198 devParamMap = {
1199 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1200 {DevTypes::IIO,
1201 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001202}
1203
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001204static void powerStateChanged(
1205 PowerState type, bool newState,
1206 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1207 sensors,
1208 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1209 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1210{
1211 if (newState)
1212 {
1213 createSensors(io, objectServer, dbusConnection, nullptr, true);
1214 }
1215 else
1216 {
1217 for (auto& [path, sensor] : sensors)
1218 {
1219 if (sensor != nullptr && sensor->readState == type)
1220 {
1221 sensor->deactivate();
1222 }
1223 }
1224 }
1225}
1226
James Feistb6c0b912019-07-09 12:21:44 -07001227int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001228{
Ed Tanous1f978632023-02-28 18:16:39 -08001229 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001230 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1231
Ed Tanous14ed5e92022-07-12 15:50:23 -07001232 sdbusplus::asio::object_server objectServer(systemBus, true);
1233 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001234 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001235 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001236 auto sensorsChanged =
1237 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001238
Cheng C Yang916360b2019-05-07 18:47:16 +08001239 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001240
Patrick Williams597e8422023-10-20 11:19:01 -05001241 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1242 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001243 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1244 };
1245
1246 setupPowerMatchCallback(systemBus, powerCallBack);
1247
1248 boost::asio::post(io, [&]() {
1249 createSensors(io, objectServer, systemBus, nullptr, false);
1250 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001251 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001252 std::function<void(sdbusplus::message_t&)> eventHandler =
1253 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001254 if (message.is_method_error())
1255 {
1256 std::cerr << "callback method error\n";
1257 return;
1258 }
1259 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001260 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001261 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1262 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001263 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001264 return;
1265 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001266 if (ec)
1267 {
1268 std::cerr << "timer error\n";
1269 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001270 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001271 });
1272 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001273
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001274 boost::asio::steady_timer cpuFilterTimer(io);
1275 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1276 [&](sdbusplus::message_t& message) {
1277 std::string path = message.get_path();
1278 boost::to_lower(path);
1279
1280 sdbusplus::message::object_path cpuPath(path);
1281 std::string cpuName = cpuPath.filename();
1282 if (!cpuName.starts_with("cpu"))
1283 {
1284 return;
1285 }
1286 size_t index = 0;
1287 try
1288 {
1289 index = std::stoi(path.substr(path.size() - 1));
1290 }
1291 catch (const std::invalid_argument&)
1292 {
1293 std::cerr << "Found invalid path " << path << "\n";
1294 return;
1295 }
1296
1297 std::string objectName;
1298 boost::container::flat_map<std::string, std::variant<bool>> values;
1299 message.read(objectName, values);
1300 auto findPresence = values.find("Present");
1301 try
1302 {
1303 cpuPresence[index] = std::get<bool>(findPresence->second);
1304 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001305 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001306 {
1307 return;
1308 }
1309
Chris Sides3fce1bf2023-04-03 16:57:05 -05001310 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001311 {
1312 return;
1313 }
1314 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1315 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1316 if (ec == boost::asio::error::operation_aborted)
1317 {
1318 return;
1319 }
1320 if (ec)
1321 {
1322 std::cerr << "timer error\n";
1323 return;
1324 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001325 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001326 });
1327 };
1328
Zev Weiss214d9712022-08-12 12:54:31 -07001329 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1330 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001331
1332 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1333 static_cast<sdbusplus::bus_t&>(*systemBus),
1334 "type='signal',member='PropertiesChanged',path_namespace='" +
1335 std::string(cpuInventoryPath) +
1336 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1337 cpuPresenceHandler));
1338
Matt Simmering39fadb92023-05-04 21:47:29 -07001339 getPresentCpus(systemBus);
1340
Bruce Lee1263c3d2021-06-04 15:16:33 +08001341 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001342 io.run();
1343}