blob: d6bc84c4daf8b245455209233bee00aa2cbfeaf2 [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}},
Potin Lai43847a82024-02-27 16:29:45 +080076 {"LTC4286", I2CDeviceType{"ltc4286", true}},
77 {"LTC4287", I2CDeviceType{"ltc4287", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +020078 {"MAX5970", I2CDeviceType{"max5970", true}},
Joseph Fu15a39df2023-12-27 09:45:49 +080079 {"MAX11607", I2CDeviceType{"max11607", false}},
Matt Simmering786efb82023-01-18 14:09:21 -080080 {"MAX16601", I2CDeviceType{"max16601", true}},
81 {"MAX20710", I2CDeviceType{"max20710", true}},
82 {"MAX20730", I2CDeviceType{"max20730", true}},
83 {"MAX20734", I2CDeviceType{"max20734", true}},
84 {"MAX20796", I2CDeviceType{"max20796", true}},
85 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +080086 {"MP2856", I2CDeviceType{"mp2856", true}},
87 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080088 {"MP2971", I2CDeviceType{"mp2971", true}},
89 {"MP2973", I2CDeviceType{"mp2973", true}},
90 {"MP2975", I2CDeviceType{"mp2975", true}},
91 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +080092 {"MP5990", I2CDeviceType{"mp5990", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080093 {"NCP4200", I2CDeviceType{"ncp4200", true}},
94 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
95 {"pmbus", I2CDeviceType{"pmbus", true}},
96 {"PXE1610", I2CDeviceType{"pxe1610", true}},
97 {"RAA228000", I2CDeviceType{"raa228000", true}},
98 {"RAA228228", I2CDeviceType{"raa228228", true}},
99 {"RAA228620", I2CDeviceType{"raa228620", true}},
100 {"RAA229001", I2CDeviceType{"raa229001", true}},
101 {"RAA229004", I2CDeviceType{"raa229004", true}},
102 {"RAA229126", I2CDeviceType{"raa229126", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200103 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800104 {"TPS53679", I2CDeviceType{"tps53679", true}},
105 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
106 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
107 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800108 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800109};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700110
Joseph Fub844e972023-03-25 10:24:30 +0800111enum class DevTypes
112{
113 Unknown = 0,
114 HWMON,
115 IIO
116};
117
118struct DevParams
119{
120 unsigned int matchIndex = 0;
121 std::string matchRegEx;
122 std::string nameRegEx;
123};
124
Cheng C Yang209ec562019-03-12 16:37:44 +0800125namespace fs = std::filesystem;
126
Yong Libf8b1da2020-04-15 16:32:50 +0800127static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800128 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800129static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
130 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800131static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
132 pwmSensors;
133static boost::container::flat_map<std::string, std::string> sensorTable;
134static boost::container::flat_map<std::string, PSUProperty> labelMatch;
135static boost::container::flat_map<std::string, std::string> pwmTable;
George Liu5b3542e2023-10-31 17:27:12 +0800136static EventPathList eventMatch;
137static GroupEventPathList groupEventMatch;
138static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800139
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.
George Liu5b3542e2023-10-31 17:27:12 +0800147void checkEvent(const std::string& directory, const EventPathList& eventMatch,
148 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800149{
150 for (const auto& match : eventMatch)
151 {
152 const std::vector<std::string>& eventAttrs = match.second;
153 const std::string& eventName = match.first;
154 for (const auto& eventAttr : eventAttrs)
155 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700156 std::string eventPath = directory;
157 eventPath += "/";
158 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800159
160 std::ifstream eventFile(eventPath);
161 if (!eventFile.good())
162 {
163 continue;
164 }
165
166 eventPathList[eventName].push_back(eventPath);
167 }
168 }
169}
170
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800171// Check Group Events which contains more than one targets in each combine
172// events.
George Liu5b3542e2023-10-31 17:27:12 +0800173void checkGroupEvent(const std::string& directory,
174 const GroupEventPathList& groupEventMatch,
175 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800176{
177 for (const auto& match : groupEventMatch)
178 {
179 const std::string& groupEventName = match.first;
George Liu5b3542e2023-10-31 17:27:12 +0800180 const EventPathList events = match.second;
181 EventPathList pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800182 for (const auto& match : events)
183 {
184 const std::string& eventName = match.first;
185 const std::vector<std::string>& eventAttrs = match.second;
186 for (const auto& eventAttr : eventAttrs)
187 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700188 std::string eventPath = directory;
189 eventPath += "/";
190 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800191 std::ifstream eventFile(eventPath);
192 if (!eventFile.good())
193 {
194 continue;
195 }
196
197 pathList[eventName].push_back(eventPath);
198 }
199 }
200 groupEventPathList[groupEventName] = pathList;
201 }
202}
203
Cheng C Yang58b2b532019-05-31 00:19:45 +0800204// Function checkEventLimits will check all the psu related xxx_input attributes
205// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
206// xxx_min_alarm exist, then store the existing paths of the alarm attributes
207// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800208void checkEventLimits(const std::string& sensorPathStr,
209 const EventPathList& limitEventMatch,
210 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800211{
Lei YUa2c7cea2020-12-23 14:07:28 +0800212 auto attributePartPos = sensorPathStr.find_last_of('_');
213 if (attributePartPos == std::string::npos)
214 {
215 // There is no '_' in the string, skip it
216 return;
217 }
218 auto attributePart =
219 std::string_view(sensorPathStr).substr(attributePartPos + 1);
220 if (attributePart != "input")
221 {
222 // If the sensor is not xxx_input, skip it
223 return;
224 }
225
226 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800227 for (const auto& limitMatch : limitEventMatch)
228 {
229 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
230 const std::string& eventName = limitMatch.first;
231 for (const auto& limitEventAttr : limitEventAttrs)
232 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800233 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800234 std::ifstream eventFile(limitEventPath);
235 if (!eventFile.good())
236 {
237 continue;
238 }
239 eventPathList[eventName].push_back(limitEventPath);
240 }
241 }
242}
Cheng C Yang916360b2019-05-07 18:47:16 +0800243
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530244static void
245 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
246 const std::string& interfacePath,
247 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
248 sdbusplus::asio::object_server& objectServer,
249 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800250{
Zev Weiss741f26e2022-08-12 18:21:02 -0700251 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800252 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700253 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800254 {
255 continue;
256 }
257
258 const std::string& sensorPathStr = sensorPath.string();
259 const std::string& pwmPathStr =
260 boost::replace_all_copy(sensorPathStr, "input", "target");
261 std::ifstream pwmFile(pwmPathStr);
262 if (!pwmFile.good())
263 {
264 continue;
265 }
266
267 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
268 if (findPWMSensor != pwmSensors.end())
269 {
270 continue;
271 }
272
Zev Weissd8c293a2022-08-15 18:58:41 -0700273 std::string name = "Pwm_";
274 name += psuName;
275 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700276 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700277
278 std::string objPath = interfacePath;
279 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700280 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700281
Cheng C Yang916360b2019-05-07 18:47:16 +0800282 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700283 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800284 }
285}
286
Zhikui Ren23c96e72020-11-05 22:32:28 -0800287static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800288 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800289 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
290 const ManagedObjectType& sensorConfigs,
291 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800292 sensorsChanged,
293 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800294{
Josh Lehan49cfba92019-10-08 16:50:42 -0700295 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800296 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800297
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800298 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
299
Cheng C Yang209ec562019-03-12 16:37:44 +0800300 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800301 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
302 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
303 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800304 {
305 std::cerr << "No PSU sensors in system\n";
306 return;
307 }
308
309 boost::container::flat_set<std::string> directories;
310 for (const auto& pmbusPath : pmbusPaths)
311 {
George Liu5b3542e2023-10-31 17:27:12 +0800312 EventPathList eventPathList;
313 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800314
315 std::ifstream nameFile(pmbusPath);
316 if (!nameFile.good())
317 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700318 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800319 continue;
320 }
321
322 std::string pmbusName;
323 std::getline(nameFile, pmbusName);
324 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700325
Matt Simmering786efb82023-01-18 14:09:21 -0800326 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800327 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700328 // To avoid this error message, add your driver name to
329 // the pmbusNames vector at the top of this file.
330 std::cerr << "Driver name " << pmbusName
331 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800332 continue;
333 }
334
Cheng C Yang209ec562019-03-12 16:37:44 +0800335 auto directory = pmbusPath.parent_path();
336
337 auto ret = directories.insert(directory.string());
338 if (!ret.second)
339 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700340 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800341 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800342 }
343
Joseph Fub844e972023-03-25 10:24:30 +0800344 DevTypes devType = DevTypes::HWMON;
345 std::string deviceName;
346 if (directory.parent_path() == "/sys/class/hwmon")
347 {
348 deviceName = fs::canonical(directory / "device").stem();
349 }
350 else
351 {
352 deviceName = fs::canonical(directory).parent_path().stem();
353 devType = DevTypes::IIO;
354 }
355
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 size_t bus = 0;
357 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000358 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 continue;
361 }
362
Zev Weisse8b97ee2022-08-12 15:23:51 -0700363 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 const SensorData* sensorData = nullptr;
365 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800366 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800367 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800368
Zev Weiss741f26e2022-08-12 18:21:02 -0700369 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800370 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700371 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800372 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800373 {
Zev Weiss054aad82022-08-18 01:37:34 -0700374 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800375 if (sensorBase != sensorData->end())
376 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700377 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800378 sensorType = type;
379 break;
380 }
381 }
382 if (baseConfig == nullptr)
383 {
384 std::cerr << "error finding base configuration for "
385 << deviceName << "\n";
386 continue;
387 }
388
Zev Weisse8b97ee2022-08-12 15:23:51 -0700389 auto configBus = baseConfig->find("Bus");
390 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800391
Zev Weisse8b97ee2022-08-12 15:23:51 -0700392 if (configBus == baseConfig->end() ||
393 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800395 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800396 continue;
397 }
398
Matt Simmering6747eba2023-01-18 16:03:55 -0800399 const uint64_t* confBus =
400 std::get_if<uint64_t>(&(configBus->second));
401 const uint64_t* confAddr =
402 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800403 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800404 {
405 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700406 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800407 continue;
408 }
409
410 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800411 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200412 if constexpr (debug)
413 {
414 std::cerr << "Configuration skipping " << *confBus << "-"
415 << *confAddr << " because not " << bus << "-"
416 << addr << "\n";
417 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800418 continue;
419 }
420
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800421 std::vector<thresholds::Threshold> confThresholds;
422 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
423 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800424 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800425 }
426 thresholdConfSize = confThresholds.size();
427
Zev Weiss741f26e2022-08-12 18:21:02 -0700428 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800429 break;
430 }
431 if (interfacePath == nullptr)
432 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700433 // To avoid this error message, add your export map entry,
434 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800435 std::cerr << "failed to find match for " << deviceName << "\n";
436 continue;
437 }
438
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800439 auto findI2CDev = devices.find(*interfacePath);
440
441 std::shared_ptr<I2CDevice> i2cDev;
442 if (findI2CDev != devices.end())
443 {
444 if (activateOnly && !findI2CDev->second.second)
445 {
446 continue;
447 }
448 i2cDev = findI2CDev->second.first;
449 }
450
Zev Weisse8b97ee2022-08-12 15:23:51 -0700451 auto findPSUName = baseConfig->find("Name");
452 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800453 {
454 std::cerr << "could not determine configuration name for "
455 << deviceName << "\n";
456 continue;
457 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800458 const std::string* psuName =
459 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800460 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800461 {
462 std::cerr << "Cannot find psu name, invalid configuration\n";
463 continue;
464 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800465
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800466 auto findCPU = baseConfig->find("CPURequired");
467 if (findCPU != baseConfig->end())
468 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500469 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800470 auto presenceFind = cpuPresence.find(index);
471 if (presenceFind == cpuPresence.end() || !presenceFind->second)
472 {
473 continue;
474 }
475 }
476
Zhikui Ren23c96e72020-11-05 22:32:28 -0800477 // on rescans, only update sensors we were signaled by
478 if (!firstScan)
479 {
Zhikui Renda98f092021-11-01 09:41:08 -0700480 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500481 auto it = std::find_if(sensorsChanged->begin(),
482 sensorsChanged->end(),
483 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800484 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500485 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800486
487 if (it == sensorsChanged->end())
488 {
489 continue;
490 }
491 sensorsChanged->erase(it);
492 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800493 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800494 checkGroupEvent(directory.string(), groupEventMatch,
495 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800496
Zev Weisse8b97ee2022-08-12 15:23:51 -0700497 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000498
Vijay Khemka996bad12019-05-28 15:15:16 -0700499 /* Check if there are more sensors in the same interface */
500 int i = 1;
501 std::vector<std::string> psuNames;
502 do
503 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700504 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700505 psuNames.push_back(
506 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700507 findPSUName = baseConfig->find("Name" + std::to_string(i++));
508 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700509
Cheng C Yange50345b2019-04-02 17:26:15 +0800510 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800511 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
512 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800513 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800514 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800515 continue;
516 }
517
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530518 /* read max value in sysfs for in, curr, power, temp, ... */
519 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
520 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700521 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530522 {
523 std::cerr << "No max name in PSU \n";
524 }
525 }
526
Zev Weiss8569bf22022-10-11 15:37:44 -0700527 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800528
Vijay Khemka996bad12019-05-28 15:15:16 -0700529 /* Find array of labels to be exposed if it is defined in config */
530 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700531 auto findLabelObj = baseConfig->find("Labels");
532 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700533 {
534 findLabels =
535 std::get<std::vector<std::string>>(findLabelObj->second);
536 }
537
Joseph Fub844e972023-03-25 10:24:30 +0800538 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700539 std::smatch matches;
540
Cheng C Yange50345b2019-04-02 17:26:15 +0800541 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800542 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530543 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800544 std::string labelHead;
545 std::string sensorPathStr = sensorPath.string();
546 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700547 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700548 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
549 {
Josh Lehan06494452019-10-31 09:49:16 -0700550 // hwmon *_input filename without number:
551 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800552 // iio in_*_raw filename without number:
553 // voltage, temp, pressure, ...
554 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700555 }
556 else
557 {
Josh Lehan06494452019-10-31 09:49:16 -0700558 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700559 << sensorNameStr;
560 continue;
561 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800562
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530563 std::string labelPath;
564
Joseph Fub844e972023-03-25 10:24:30 +0800565 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530566 {
Joseph Fub844e972023-03-25 10:24:30 +0800567 /* find and differentiate _max and _input to replace "label" */
568 size_t pos = sensorPathStr.find('_');
569 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530570 {
Joseph Fub844e972023-03-25 10:24:30 +0800571 std::string sensorPathStrMax = sensorPathStr.substr(pos);
572 if (sensorPathStrMax == "_max")
573 {
574 labelPath = boost::replace_all_copy(sensorPathStr,
575 "max", "label");
576 maxLabel = true;
577 }
578 else
579 {
580 labelPath = boost::replace_all_copy(sensorPathStr,
581 "input", "label");
582 maxLabel = false;
583 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530584 }
585 else
586 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800587 continue;
588 }
589
Joseph Fub844e972023-03-25 10:24:30 +0800590 std::ifstream labelFile(labelPath);
591 if (!labelFile.good())
592 {
593 if constexpr (debug)
594 {
595 std::cerr << "Input file " << sensorPath
596 << " has no corresponding label file\n";
597 }
598 // hwmon *_input filename with number:
599 // temp1, temp2, temp3, ...
600 labelHead = sensorNameStr.substr(0,
601 sensorNameStr.find('_'));
602 }
603 else
604 {
605 std::string label;
606 std::getline(labelFile, label);
607 labelFile.close();
608 auto findSensor = sensors.find(label);
609 if (findSensor != sensors.end())
610 {
611 continue;
612 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800613
Joseph Fub844e972023-03-25 10:24:30 +0800614 // hwmon corresponding *_label file contents:
615 // vin1, vout1, ...
616 labelHead = label.substr(0, label.find(' '));
617 }
618
619 /* append "max" for labelMatch */
620 if (maxLabel)
621 {
622 labelHead.insert(0, "max");
623 }
624
625 checkPWMSensor(sensorPath, labelHead, *interfacePath,
626 dbusConnection, objectServer, psuNames[0]);
627 }
628 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530629 {
Joseph Fub844e972023-03-25 10:24:30 +0800630 auto findIIOHyphen = sensorNameStr.find_last_of('_');
631 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530632 }
633
Ed Tanous8a57ec02020-10-09 12:46:52 -0700634 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700635 {
636 std::cerr << "Sensor type=\"" << sensorNameSubStr
637 << "\" label=\"" << labelHead << "\"\n";
638 }
639
Vijay Khemka996bad12019-05-28 15:15:16 -0700640 if (!findLabels.empty())
641 {
642 /* Check if this labelHead is enabled in config file */
643 if (std::find(findLabels.begin(), findLabels.end(),
644 labelHead) == findLabels.end())
645 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700646 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800647 {
648 std::cerr << "could not find " << labelHead
649 << " in the Labels list\n";
650 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700651 continue;
652 }
653 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800654
Cheng C Yange50345b2019-04-02 17:26:15 +0800655 auto findProperty = labelMatch.find(labelHead);
656 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800657 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700658 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700659 {
660 std::cerr << "Could not find matching default property for "
661 << labelHead << "\n";
662 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800663 continue;
664 }
665
Josh Lehan74d9bd92019-10-31 08:51:58 -0700666 // Protect the hardcoded labelMatch list from changes,
667 // by making a copy and modifying that instead.
668 // Avoid bleedthrough of one device's customizations to
669 // the next device, as each should be independently customizable.
670 psuProperties.push_back(findProperty->second);
671 auto psuProperty = psuProperties.rbegin();
672
673 // Use label head as prefix for reading from config file,
674 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
675 std::string keyName = labelHead + "_Name";
676 std::string keyScale = labelHead + "_Scale";
677 std::string keyMin = labelHead + "_Min";
678 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800679 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800680 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700681
682 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700683 auto findCustomName = baseConfig->find(keyName);
684 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700685 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700686 try
687 {
688 psuProperty->labelTypeName = std::visit(
689 VariantToStringVisitor(), findCustomName->second);
690 }
Patrick Williams26601e82021-10-06 12:43:25 -0500691 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 {
693 std::cerr << "Unable to parse " << keyName << "\n";
694 continue;
695 }
696
697 // All strings are valid, including empty string
698 customizedName = true;
699 }
700
701 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700702 auto findCustomScale = baseConfig->find(keyScale);
703 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700704 {
705 try
706 {
707 psuProperty->sensorScaleFactor = std::visit(
708 VariantToUnsignedIntVisitor(), findCustomScale->second);
709 }
Patrick Williams26601e82021-10-06 12:43:25 -0500710 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700711 {
712 std::cerr << "Unable to parse " << keyScale << "\n";
713 continue;
714 }
715
716 // Avoid later division by zero
717 if (psuProperty->sensorScaleFactor > 0)
718 {
719 customizedScale = true;
720 }
721 else
722 {
723 std::cerr << "Unable to accept " << keyScale << "\n";
724 continue;
725 }
726 }
727
Zev Weisse8b97ee2022-08-12 15:23:51 -0700728 auto findCustomMin = baseConfig->find(keyMin);
729 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700730 {
731 try
732 {
733 psuProperty->minReading = std::visit(
734 VariantToDoubleVisitor(), findCustomMin->second);
735 }
Patrick Williams26601e82021-10-06 12:43:25 -0500736 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 {
738 std::cerr << "Unable to parse " << keyMin << "\n";
739 continue;
740 }
741 }
742
Zev Weisse8b97ee2022-08-12 15:23:51 -0700743 auto findCustomMax = baseConfig->find(keyMax);
744 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700745 {
746 try
747 {
748 psuProperty->maxReading = std::visit(
749 VariantToDoubleVisitor(), findCustomMax->second);
750 }
Patrick Williams26601e82021-10-06 12:43:25 -0500751 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700752 {
753 std::cerr << "Unable to parse " << keyMax << "\n";
754 continue;
755 }
756 }
757
Zev Weisse8b97ee2022-08-12 15:23:51 -0700758 auto findCustomOffset = baseConfig->find(keyOffset);
759 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800760 {
761 try
762 {
763 psuProperty->sensorOffset = std::visit(
764 VariantToDoubleVisitor(), findCustomOffset->second);
765 }
Patrick Williams26601e82021-10-06 12:43:25 -0500766 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800767 {
768 std::cerr << "Unable to parse " << keyOffset << "\n";
769 continue;
770 }
771 }
772
Lotus Xucb5af732021-09-10 15:18:50 +0800773 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700774 auto findPowerState = baseConfig->find(keyPowerState);
775 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800776 {
777 std::string powerState = std::visit(VariantToStringVisitor(),
778 findPowerState->second);
779 setReadState(powerState, readState);
780 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700781 if (!(psuProperty->minReading < psuProperty->maxReading))
782 {
783 std::cerr << "Min must be less than Max\n";
784 continue;
785 }
786
787 // If the sensor name is being customized by config file,
788 // then prefix/suffix composition becomes not necessary,
789 // and in fact not wanted, because it gets in the way.
790 std::string psuNameFromIndex;
791 if (!customizedName)
792 {
793 /* Find out sensor name index for this label */
794 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500795 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700796 if (std::regex_search(labelHead, matches, rgx))
797 {
798 nameIndex = std::stoi(matches[1]);
799
800 // Decrement to preserve alignment, because hwmon
801 // human-readable filenames and labels use 1-based
802 // numbering, but the "Name", "Name1", "Name2", etc. naming
803 // convention (the psuNames vector) uses 0-based numbering.
804 if (nameIndex > 0)
805 {
806 --nameIndex;
807 }
808 }
809 else
810 {
811 nameIndex = 0;
812 }
813
814 if (psuNames.size() <= nameIndex)
815 {
816 std::cerr << "Could not pair " << labelHead
817 << " with a Name field\n";
818 continue;
819 }
820
821 psuNameFromIndex = psuNames[nameIndex];
822
Ed Tanous8a57ec02020-10-09 12:46:52 -0700823 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700824 {
825 std::cerr << "Sensor label head " << labelHead
826 << " paired with " << psuNameFromIndex
827 << " at index " << nameIndex << "\n";
828 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700829 }
830
Joseph Fub844e972023-03-25 10:24:30 +0800831 if (devType == DevTypes::HWMON)
832 {
833 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
834 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800835
Josh Lehan74d9bd92019-10-31 08:51:58 -0700836 // Similarly, if sensor scaling factor is being customized,
837 // then the below power-of-10 constraint becomes unnecessary,
838 // as config should be able to specify an arbitrary divisor.
839 unsigned int factor = psuProperty->sensorScaleFactor;
840 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700841 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700842 // Preserve existing usage of hardcoded labelMatch table below
843 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700844
Josh Lehan74d9bd92019-10-31 08:51:58 -0700845 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700846 char firstChar =
847 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700848 std::string strScaleFactor =
849 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
850
851 // Preserve existing configs by accepting earlier syntax,
852 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700853 auto findScaleFactor = baseConfig->find(strScaleFactor);
854 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700855 {
856 factor = std::visit(VariantToIntVisitor(),
857 findScaleFactor->second);
858 }
859
Ed Tanous8a57ec02020-10-09 12:46:52 -0700860 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700861 {
862 std::cerr << "Sensor scaling factor " << factor
863 << " string " << strScaleFactor << "\n";
864 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700865 }
866
Vijay Khemka996bad12019-05-28 15:15:16 -0700867 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530868 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
869 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800870 {
James Feist17ab6e02019-06-25 12:28:13 -0700871 std::cerr << "error populating thresholds for "
872 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800873 }
874
Zev Weiss6b6891c2021-04-22 02:46:21 -0500875 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
876 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800877 {
Jason Ling5747fab2019-10-02 16:46:23 -0700878 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700879 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800880 continue;
881 }
882
Ed Tanous8a57ec02020-10-09 12:46:52 -0700883 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700884 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700885 std::cerr << "Sensor properties: Name \""
886 << psuProperty->labelTypeName << "\" Scale "
887 << psuProperty->sensorScaleFactor << " Min "
888 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800889 << psuProperty->maxReading << " Offset "
890 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700891 }
892
893 std::string sensorName = psuProperty->labelTypeName;
894 if (customizedName)
895 {
896 if (sensorName.empty())
897 {
898 // Allow selective disabling of an individual sensor,
899 // by customizing its name to an empty string.
900 std::cerr << "Sensor disabled, empty string\n";
901 continue;
902 }
903 }
904 else
905 {
906 // Sensor name not customized, do prefix/suffix composition,
907 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500908 sensorName = psuNameFromIndex + " " +
909 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700910 }
911
Ed Tanous8a57ec02020-10-09 12:46:52 -0700912 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700913 {
914 std::cerr << "Sensor name \"" << sensorName << "\" path \""
915 << sensorPathStr << "\" type \"" << sensorType
916 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700917 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800918 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800919
920 auto& sensor = sensors[sensorName];
921 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700922 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800923 sensor = nullptr;
924 }
925
926 if (sensor != nullptr)
927 {
928 sensor->activate(sensorPathStr, i2cDev);
929 }
930 else
931 {
932 sensors[sensorName] = std::make_shared<PSUSensor>(
933 sensorPathStr, sensorType, objectServer, dbusConnection, io,
934 sensorName, std::move(sensorThresholds), *interfacePath,
935 readState, findSensorUnit->second, factor,
936 psuProperty->maxReading, psuProperty->minReading,
937 psuProperty->sensorOffset, labelHead, thresholdConfSize,
938 pollRate, i2cDev);
939 sensors[sensorName]->setupRead();
940 ++numCreated;
941 if constexpr (debug)
942 {
943 std::cerr << "Created " << numCreated
944 << " sensors so far\n";
945 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700946 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800947 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800948
Joseph Fub844e972023-03-25 10:24:30 +0800949 if (devType == DevTypes::HWMON)
950 {
951 // OperationalStatus event
952 combineEvents[*psuName + "OperationalStatus"] = nullptr;
953 combineEvents[*psuName + "OperationalStatus"] =
954 std::make_unique<PSUCombineEvent>(
955 objectServer, dbusConnection, io, *psuName, readState,
956 eventPathList, groupEventPathList, "OperationalStatus",
957 pollRate);
958 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800959 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700960
Ed Tanous8a57ec02020-10-09 12:46:52 -0700961 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700962 {
963 std::cerr << "Created total of " << numCreated << " sensors\n";
964 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800965}
Cheng C Yang209ec562019-03-12 16:37:44 +0800966
Matt Simmering39fadb92023-05-04 21:47:29 -0700967static void
968 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
969{
970 static const int depth = 2;
971 static const int numKeys = 1;
972 GetSubTreeType cpuSubTree;
973
974 try
975 {
976 auto getItems = dbusConnection->new_method_call(
977 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
978 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
979 std::array<const char*, numKeys>{
980 "xyz.openbmc_project.Inventory.Item"});
981 auto getItemsResp = dbusConnection->call(getItems);
982 getItemsResp.read(cpuSubTree);
983 }
984 catch (sdbusplus::exception_t& e)
985 {
986 std::cerr << "error getting inventory item subtree: " << e.what()
987 << "\n";
988 return;
989 }
990
991 for (const auto& [path, objDict] : cpuSubTree)
992 {
993 auto obj = sdbusplus::message::object_path(path).filename();
994 if (!obj.starts_with("cpu") || objDict.empty())
995 {
996 continue;
997 }
998 const std::string& owner = objDict.begin()->first;
999
1000 std::variant<bool> respValue;
1001 try
1002 {
1003 auto getPresence = dbusConnection->new_method_call(
1004 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1005 "Get");
1006 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1007 auto resp = dbusConnection->call(getPresence);
1008 resp.read(respValue);
1009 }
1010 catch (sdbusplus::exception_t& e)
1011 {
1012 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1013 continue;
1014 }
1015
1016 auto* present = std::get_if<bool>(&respValue);
1017 if (present != nullptr && *present)
1018 {
1019 int cpuIndex = 0;
1020 try
1021 {
1022 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1023 }
1024 catch (const std::exception& e)
1025 {
1026 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1027 continue;
1028 }
1029 cpuPresence[cpuIndex + 1] = *present;
1030 }
1031 }
1032}
1033
Zhikui Ren23c96e72020-11-05 22:32:28 -08001034void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001035 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001036 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1037 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001038 sensorsChanged,
1039 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001040{
1041 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001042 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1043 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001044 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1045 sensorsChanged, activateOnly);
1046 });
Matt Simmering786efb82023-01-18 14:09:21 -08001047 std::vector<std::string> types(sensorTypes.size());
1048 for (const auto& [type, dt] : sensorTypes)
1049 {
1050 types.push_back(type);
1051 }
1052 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001053}
1054
Cheng C Yang916360b2019-05-07 18:47:16 +08001055void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001056{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001057 sensorTable = {{"power", sensor_paths::unitWatts},
1058 {"curr", sensor_paths::unitAmperes},
1059 {"temp", sensor_paths::unitDegreesC},
1060 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001061 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001062 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001063
Jeff Line41d52f2021-04-07 19:38:51 +08001064 labelMatch = {
1065 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001066 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1067 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001068 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1069 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1070 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1071 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001072 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1073 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1074 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001075 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1076 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001077 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1078 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001079 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001080 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1081 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1082 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1083 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001084 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1085 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1086 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1087 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1088 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1089 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1090 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1091 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1092 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1093 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1094 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1095 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1096 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1097 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1098 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1099 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1100 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1101 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1102 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1103 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001117 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001118 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001119 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001125 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001126 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1127 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001128 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1129 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1130 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1131 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1132 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1133 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1134 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1135 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1136 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1137 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1138 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1139 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1140 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1141 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1142 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001143 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1144 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1145 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001146 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1147 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1148 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1149 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1150 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1151 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1152 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1153 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1154 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001155 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1156 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1157 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001158
Matt Simmering6747eba2023-01-18 16:03:55 -08001159 pwmTable = {{"fan1", "Fan_1"},
1160 {"fan2", "Fan_2"},
1161 {"fan3", "Fan_3"},
1162 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001163
1164 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1165 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1166
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001167 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1168 {"Failure", {"in2_alarm"}},
1169 {"ACLost", {"in1_beep"}},
1170 {"ConfigureError", {"in1_fault"}}};
1171
1172 groupEventMatch = {{"FanFault",
1173 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001174 {"fan2", {"fan2_alarm", "fan2_fault"}},
1175 {"fan3", {"fan3_alarm", "fan3_fault"}},
1176 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001177
1178 devParamMap = {
1179 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1180 {DevTypes::IIO,
1181 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001182}
1183
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001184static void powerStateChanged(
1185 PowerState type, bool newState,
1186 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1187 sensors,
1188 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1189 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1190{
1191 if (newState)
1192 {
1193 createSensors(io, objectServer, dbusConnection, nullptr, true);
1194 }
1195 else
1196 {
1197 for (auto& [path, sensor] : sensors)
1198 {
1199 if (sensor != nullptr && sensor->readState == type)
1200 {
1201 sensor->deactivate();
1202 }
1203 }
1204 }
1205}
1206
James Feistb6c0b912019-07-09 12:21:44 -07001207int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001208{
Ed Tanous1f978632023-02-28 18:16:39 -08001209 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001210 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1211
Ed Tanous14ed5e92022-07-12 15:50:23 -07001212 sdbusplus::asio::object_server objectServer(systemBus, true);
1213 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001214 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001215 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001216 auto sensorsChanged =
1217 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001218
Cheng C Yang916360b2019-05-07 18:47:16 +08001219 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001220
Patrick Williams597e8422023-10-20 11:19:01 -05001221 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1222 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001223 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1224 };
1225
1226 setupPowerMatchCallback(systemBus, powerCallBack);
1227
1228 boost::asio::post(io, [&]() {
1229 createSensors(io, objectServer, systemBus, nullptr, false);
1230 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001231 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001232 std::function<void(sdbusplus::message_t&)> eventHandler =
1233 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001234 if (message.is_method_error())
1235 {
1236 std::cerr << "callback method error\n";
1237 return;
1238 }
1239 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001240 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001241 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1242 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001243 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001244 return;
1245 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001246 if (ec)
1247 {
1248 std::cerr << "timer error\n";
1249 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001250 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001251 });
1252 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001253
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001254 boost::asio::steady_timer cpuFilterTimer(io);
1255 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1256 [&](sdbusplus::message_t& message) {
1257 std::string path = message.get_path();
1258 boost::to_lower(path);
1259
1260 sdbusplus::message::object_path cpuPath(path);
1261 std::string cpuName = cpuPath.filename();
1262 if (!cpuName.starts_with("cpu"))
1263 {
1264 return;
1265 }
1266 size_t index = 0;
1267 try
1268 {
1269 index = std::stoi(path.substr(path.size() - 1));
1270 }
1271 catch (const std::invalid_argument&)
1272 {
1273 std::cerr << "Found invalid path " << path << "\n";
1274 return;
1275 }
1276
1277 std::string objectName;
1278 boost::container::flat_map<std::string, std::variant<bool>> values;
1279 message.read(objectName, values);
1280 auto findPresence = values.find("Present");
1281 try
1282 {
1283 cpuPresence[index] = std::get<bool>(findPresence->second);
1284 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001285 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001286 {
1287 return;
1288 }
1289
Chris Sides3fce1bf2023-04-03 16:57:05 -05001290 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001291 {
1292 return;
1293 }
1294 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1295 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1296 if (ec == boost::asio::error::operation_aborted)
1297 {
1298 return;
1299 }
1300 if (ec)
1301 {
1302 std::cerr << "timer error\n";
1303 return;
1304 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001305 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001306 });
1307 };
1308
Zev Weiss214d9712022-08-12 12:54:31 -07001309 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1310 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001311
1312 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1313 static_cast<sdbusplus::bus_t&>(*systemBus),
1314 "type='signal',member='PropertiesChanged',path_namespace='" +
1315 std::string(cpuInventoryPath) +
1316 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1317 cpuPresenceHandler));
1318
Matt Simmering39fadb92023-05-04 21:47:29 -07001319 getPresentCpus(systemBus);
1320
Bruce Lee1263c3d2021-06-04 15:16:33 +08001321 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001322 io.run();
1323}