blob: 0f33a5dab50c2dcead207c60df0f6176aa6cb37f [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}},
Potin Lai4cfa64f2023-12-18 13:35:05 +080083 {"MP2856", I2CDeviceType{"mp2856", true}},
84 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080085 {"MP2971", I2CDeviceType{"mp2971", true}},
86 {"MP2973", I2CDeviceType{"mp2973", true}},
87 {"MP2975", I2CDeviceType{"mp2975", true}},
88 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +080089 {"MP5990", I2CDeviceType{"mp5990", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080090 {"NCP4200", I2CDeviceType{"ncp4200", true}},
91 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
92 {"pmbus", I2CDeviceType{"pmbus", true}},
93 {"PXE1610", I2CDeviceType{"pxe1610", true}},
94 {"RAA228000", I2CDeviceType{"raa228000", true}},
95 {"RAA228228", I2CDeviceType{"raa228228", true}},
96 {"RAA228620", I2CDeviceType{"raa228620", true}},
97 {"RAA229001", I2CDeviceType{"raa229001", true}},
98 {"RAA229004", I2CDeviceType{"raa229004", true}},
99 {"RAA229126", I2CDeviceType{"raa229126", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200100 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800101 {"TPS53679", I2CDeviceType{"tps53679", true}},
102 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
103 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
104 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800105 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800106};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700107
Joseph Fub844e972023-03-25 10:24:30 +0800108enum class DevTypes
109{
110 Unknown = 0,
111 HWMON,
112 IIO
113};
114
115struct DevParams
116{
117 unsigned int matchIndex = 0;
118 std::string matchRegEx;
119 std::string nameRegEx;
120};
121
Cheng C Yang209ec562019-03-12 16:37:44 +0800122namespace fs = std::filesystem;
123
Yong Libf8b1da2020-04-15 16:32:50 +0800124static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800125 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800126static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
127 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800128static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
129 pwmSensors;
130static boost::container::flat_map<std::string, std::string> sensorTable;
131static boost::container::flat_map<std::string, PSUProperty> labelMatch;
132static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800133static boost::container::flat_map<std::string, std::vector<std::string>>
134 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800135static boost::container::flat_map<
136 std::string,
137 boost::container::flat_map<std::string, std::vector<std::string>>>
138 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800139static boost::container::flat_map<std::string, std::vector<std::string>>
140 limitEventMatch;
141
Josh Lehan74d9bd92019-10-31 08:51:58 -0700142static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800143static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800144static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700145
Cheng C Yang58b2b532019-05-31 00:19:45 +0800146// Function CheckEvent will check each attribute from eventMatch table in the
147// sysfs. If the attributes exists in sysfs, then store the complete path
148// of the attribute into eventPathList.
149void checkEvent(
150 const std::string& directory,
151 const boost::container::flat_map<std::string, std::vector<std::string>>&
152 eventMatch,
153 boost::container::flat_map<std::string, std::vector<std::string>>&
154 eventPathList)
155{
156 for (const auto& match : eventMatch)
157 {
158 const std::vector<std::string>& eventAttrs = match.second;
159 const std::string& eventName = match.first;
160 for (const auto& eventAttr : eventAttrs)
161 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700162 std::string eventPath = directory;
163 eventPath += "/";
164 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800165
166 std::ifstream eventFile(eventPath);
167 if (!eventFile.good())
168 {
169 continue;
170 }
171
172 eventPathList[eventName].push_back(eventPath);
173 }
174 }
175}
176
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800177// Check Group Events which contains more than one targets in each combine
178// events.
179void checkGroupEvent(
180 const std::string& directory,
181 const boost::container::flat_map<
182 std::string,
183 boost::container::flat_map<std::string, std::vector<std::string>>>&
184 groupEventMatch,
185 boost::container::flat_map<
186 std::string,
187 boost::container::flat_map<std::string, std::vector<std::string>>>&
188 groupEventPathList)
189{
190 for (const auto& match : groupEventMatch)
191 {
192 const std::string& groupEventName = match.first;
193 const boost::container::flat_map<std::string, std::vector<std::string>>
194 events = match.second;
195 boost::container::flat_map<std::string, std::vector<std::string>>
196 pathList;
197 for (const auto& match : events)
198 {
199 const std::string& eventName = match.first;
200 const std::vector<std::string>& eventAttrs = match.second;
201 for (const auto& eventAttr : eventAttrs)
202 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700203 std::string eventPath = directory;
204 eventPath += "/";
205 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800206 std::ifstream eventFile(eventPath);
207 if (!eventFile.good())
208 {
209 continue;
210 }
211
212 pathList[eventName].push_back(eventPath);
213 }
214 }
215 groupEventPathList[groupEventName] = pathList;
216 }
217}
218
Cheng C Yang58b2b532019-05-31 00:19:45 +0800219// Function checkEventLimits will check all the psu related xxx_input attributes
220// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
221// xxx_min_alarm exist, then store the existing paths of the alarm attributes
222// to eventPathList.
223void checkEventLimits(
224 const std::string& sensorPathStr,
225 const boost::container::flat_map<std::string, std::vector<std::string>>&
226 limitEventMatch,
227 boost::container::flat_map<std::string, std::vector<std::string>>&
228 eventPathList)
229{
Lei YUa2c7cea2020-12-23 14:07:28 +0800230 auto attributePartPos = sensorPathStr.find_last_of('_');
231 if (attributePartPos == std::string::npos)
232 {
233 // There is no '_' in the string, skip it
234 return;
235 }
236 auto attributePart =
237 std::string_view(sensorPathStr).substr(attributePartPos + 1);
238 if (attributePart != "input")
239 {
240 // If the sensor is not xxx_input, skip it
241 return;
242 }
243
244 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800245 for (const auto& limitMatch : limitEventMatch)
246 {
247 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
248 const std::string& eventName = limitMatch.first;
249 for (const auto& limitEventAttr : limitEventAttrs)
250 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800251 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800252 std::ifstream eventFile(limitEventPath);
253 if (!eventFile.good())
254 {
255 continue;
256 }
257 eventPathList[eventName].push_back(limitEventPath);
258 }
259 }
260}
Cheng C Yang916360b2019-05-07 18:47:16 +0800261
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530262static void
263 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
264 const std::string& interfacePath,
265 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
266 sdbusplus::asio::object_server& objectServer,
267 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800268{
Zev Weiss741f26e2022-08-12 18:21:02 -0700269 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800270 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700271 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800272 {
273 continue;
274 }
275
276 const std::string& sensorPathStr = sensorPath.string();
277 const std::string& pwmPathStr =
278 boost::replace_all_copy(sensorPathStr, "input", "target");
279 std::ifstream pwmFile(pwmPathStr);
280 if (!pwmFile.good())
281 {
282 continue;
283 }
284
285 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
286 if (findPWMSensor != pwmSensors.end())
287 {
288 continue;
289 }
290
Zev Weissd8c293a2022-08-15 18:58:41 -0700291 std::string name = "Pwm_";
292 name += psuName;
293 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700294 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700295
296 std::string objPath = interfacePath;
297 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700298 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700299
Cheng C Yang916360b2019-05-07 18:47:16 +0800300 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700301 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800302 }
303}
304
Zhikui Ren23c96e72020-11-05 22:32:28 -0800305static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800306 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800307 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
308 const ManagedObjectType& sensorConfigs,
309 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800310 sensorsChanged,
311 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800312{
Josh Lehan49cfba92019-10-08 16:50:42 -0700313 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800314 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800315
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800316 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
317
Cheng C Yang209ec562019-03-12 16:37:44 +0800318 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800319 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
320 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
321 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800322 {
323 std::cerr << "No PSU sensors in system\n";
324 return;
325 }
326
327 boost::container::flat_set<std::string> directories;
328 for (const auto& pmbusPath : pmbusPaths)
329 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800330 boost::container::flat_map<std::string, std::vector<std::string>>
331 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800332 boost::container::flat_map<
333 std::string,
334 boost::container::flat_map<std::string, std::vector<std::string>>>
335 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800336
337 std::ifstream nameFile(pmbusPath);
338 if (!nameFile.good())
339 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700340 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800341 continue;
342 }
343
344 std::string pmbusName;
345 std::getline(nameFile, pmbusName);
346 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700347
Matt Simmering786efb82023-01-18 14:09:21 -0800348 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800349 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700350 // To avoid this error message, add your driver name to
351 // the pmbusNames vector at the top of this file.
352 std::cerr << "Driver name " << pmbusName
353 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800354 continue;
355 }
356
Cheng C Yang209ec562019-03-12 16:37:44 +0800357 auto directory = pmbusPath.parent_path();
358
359 auto ret = directories.insert(directory.string());
360 if (!ret.second)
361 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700362 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800363 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 }
365
Joseph Fub844e972023-03-25 10:24:30 +0800366 DevTypes devType = DevTypes::HWMON;
367 std::string deviceName;
368 if (directory.parent_path() == "/sys/class/hwmon")
369 {
370 deviceName = fs::canonical(directory / "device").stem();
371 }
372 else
373 {
374 deviceName = fs::canonical(directory).parent_path().stem();
375 devType = DevTypes::IIO;
376 }
377
Cheng C Yang209ec562019-03-12 16:37:44 +0800378 size_t bus = 0;
379 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000380 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800381 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800382 continue;
383 }
384
Zev Weisse8b97ee2022-08-12 15:23:51 -0700385 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800386 const SensorData* sensorData = nullptr;
387 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800388 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800389 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800390
Zev Weiss741f26e2022-08-12 18:21:02 -0700391 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800392 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700393 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800394 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 {
Zev Weiss054aad82022-08-18 01:37:34 -0700396 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800397 if (sensorBase != sensorData->end())
398 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700399 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800400 sensorType = type;
401 break;
402 }
403 }
404 if (baseConfig == nullptr)
405 {
406 std::cerr << "error finding base configuration for "
407 << deviceName << "\n";
408 continue;
409 }
410
Zev Weisse8b97ee2022-08-12 15:23:51 -0700411 auto configBus = baseConfig->find("Bus");
412 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800413
Zev Weisse8b97ee2022-08-12 15:23:51 -0700414 if (configBus == baseConfig->end() ||
415 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800417 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800418 continue;
419 }
420
Matt Simmering6747eba2023-01-18 16:03:55 -0800421 const uint64_t* confBus =
422 std::get_if<uint64_t>(&(configBus->second));
423 const uint64_t* confAddr =
424 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800425 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800426 {
427 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700428 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800429 continue;
430 }
431
432 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800433 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200434 if constexpr (debug)
435 {
436 std::cerr << "Configuration skipping " << *confBus << "-"
437 << *confAddr << " because not " << bus << "-"
438 << addr << "\n";
439 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800440 continue;
441 }
442
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800443 std::vector<thresholds::Threshold> confThresholds;
444 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
445 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800446 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800447 }
448 thresholdConfSize = confThresholds.size();
449
Zev Weiss741f26e2022-08-12 18:21:02 -0700450 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800451 break;
452 }
453 if (interfacePath == nullptr)
454 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700455 // To avoid this error message, add your export map entry,
456 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800457 std::cerr << "failed to find match for " << deviceName << "\n";
458 continue;
459 }
460
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800461 auto findI2CDev = devices.find(*interfacePath);
462
463 std::shared_ptr<I2CDevice> i2cDev;
464 if (findI2CDev != devices.end())
465 {
466 if (activateOnly && !findI2CDev->second.second)
467 {
468 continue;
469 }
470 i2cDev = findI2CDev->second.first;
471 }
472
Zev Weisse8b97ee2022-08-12 15:23:51 -0700473 auto findPSUName = baseConfig->find("Name");
474 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800475 {
476 std::cerr << "could not determine configuration name for "
477 << deviceName << "\n";
478 continue;
479 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800480 const std::string* psuName =
481 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800482 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800483 {
484 std::cerr << "Cannot find psu name, invalid configuration\n";
485 continue;
486 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800487
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800488 auto findCPU = baseConfig->find("CPURequired");
489 if (findCPU != baseConfig->end())
490 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500491 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800492 auto presenceFind = cpuPresence.find(index);
493 if (presenceFind == cpuPresence.end() || !presenceFind->second)
494 {
495 continue;
496 }
497 }
498
Zhikui Ren23c96e72020-11-05 22:32:28 -0800499 // on rescans, only update sensors we were signaled by
500 if (!firstScan)
501 {
Zhikui Renda98f092021-11-01 09:41:08 -0700502 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500503 auto it = std::find_if(sensorsChanged->begin(),
504 sensorsChanged->end(),
505 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800506 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500507 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800508
509 if (it == sensorsChanged->end())
510 {
511 continue;
512 }
513 sensorsChanged->erase(it);
514 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800515 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800516 checkGroupEvent(directory.string(), groupEventMatch,
517 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800518
Zev Weisse8b97ee2022-08-12 15:23:51 -0700519 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000520
Vijay Khemka996bad12019-05-28 15:15:16 -0700521 /* Check if there are more sensors in the same interface */
522 int i = 1;
523 std::vector<std::string> psuNames;
524 do
525 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700526 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700527 psuNames.push_back(
528 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700529 findPSUName = baseConfig->find("Name" + std::to_string(i++));
530 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700531
Cheng C Yange50345b2019-04-02 17:26:15 +0800532 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800533 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
534 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800535 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800536 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800537 continue;
538 }
539
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530540 /* read max value in sysfs for in, curr, power, temp, ... */
541 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
542 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700543 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530544 {
545 std::cerr << "No max name in PSU \n";
546 }
547 }
548
Zev Weiss8569bf22022-10-11 15:37:44 -0700549 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800550
Vijay Khemka996bad12019-05-28 15:15:16 -0700551 /* Find array of labels to be exposed if it is defined in config */
552 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700553 auto findLabelObj = baseConfig->find("Labels");
554 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700555 {
556 findLabels =
557 std::get<std::vector<std::string>>(findLabelObj->second);
558 }
559
Joseph Fub844e972023-03-25 10:24:30 +0800560 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700561 std::smatch matches;
562
Cheng C Yange50345b2019-04-02 17:26:15 +0800563 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800564 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530565 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800566 std::string labelHead;
567 std::string sensorPathStr = sensorPath.string();
568 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700569 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700570 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
571 {
Josh Lehan06494452019-10-31 09:49:16 -0700572 // hwmon *_input filename without number:
573 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800574 // iio in_*_raw filename without number:
575 // voltage, temp, pressure, ...
576 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700577 }
578 else
579 {
Josh Lehan06494452019-10-31 09:49:16 -0700580 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700581 << sensorNameStr;
582 continue;
583 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800584
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530585 std::string labelPath;
586
Joseph Fub844e972023-03-25 10:24:30 +0800587 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530588 {
Joseph Fub844e972023-03-25 10:24:30 +0800589 /* find and differentiate _max and _input to replace "label" */
590 size_t pos = sensorPathStr.find('_');
591 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530592 {
Joseph Fub844e972023-03-25 10:24:30 +0800593 std::string sensorPathStrMax = sensorPathStr.substr(pos);
594 if (sensorPathStrMax == "_max")
595 {
596 labelPath = boost::replace_all_copy(sensorPathStr,
597 "max", "label");
598 maxLabel = true;
599 }
600 else
601 {
602 labelPath = boost::replace_all_copy(sensorPathStr,
603 "input", "label");
604 maxLabel = false;
605 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530606 }
607 else
608 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800609 continue;
610 }
611
Joseph Fub844e972023-03-25 10:24:30 +0800612 std::ifstream labelFile(labelPath);
613 if (!labelFile.good())
614 {
615 if constexpr (debug)
616 {
617 std::cerr << "Input file " << sensorPath
618 << " has no corresponding label file\n";
619 }
620 // hwmon *_input filename with number:
621 // temp1, temp2, temp3, ...
622 labelHead = sensorNameStr.substr(0,
623 sensorNameStr.find('_'));
624 }
625 else
626 {
627 std::string label;
628 std::getline(labelFile, label);
629 labelFile.close();
630 auto findSensor = sensors.find(label);
631 if (findSensor != sensors.end())
632 {
633 continue;
634 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800635
Joseph Fub844e972023-03-25 10:24:30 +0800636 // hwmon corresponding *_label file contents:
637 // vin1, vout1, ...
638 labelHead = label.substr(0, label.find(' '));
639 }
640
641 /* append "max" for labelMatch */
642 if (maxLabel)
643 {
644 labelHead.insert(0, "max");
645 }
646
647 checkPWMSensor(sensorPath, labelHead, *interfacePath,
648 dbusConnection, objectServer, psuNames[0]);
649 }
650 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530651 {
Joseph Fub844e972023-03-25 10:24:30 +0800652 auto findIIOHyphen = sensorNameStr.find_last_of('_');
653 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530654 }
655
Ed Tanous8a57ec02020-10-09 12:46:52 -0700656 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700657 {
658 std::cerr << "Sensor type=\"" << sensorNameSubStr
659 << "\" label=\"" << labelHead << "\"\n";
660 }
661
Vijay Khemka996bad12019-05-28 15:15:16 -0700662 if (!findLabels.empty())
663 {
664 /* Check if this labelHead is enabled in config file */
665 if (std::find(findLabels.begin(), findLabels.end(),
666 labelHead) == findLabels.end())
667 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700668 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800669 {
670 std::cerr << "could not find " << labelHead
671 << " in the Labels list\n";
672 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700673 continue;
674 }
675 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800676
Cheng C Yange50345b2019-04-02 17:26:15 +0800677 auto findProperty = labelMatch.find(labelHead);
678 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800679 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700680 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700681 {
682 std::cerr << "Could not find matching default property for "
683 << labelHead << "\n";
684 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800685 continue;
686 }
687
Josh Lehan74d9bd92019-10-31 08:51:58 -0700688 // Protect the hardcoded labelMatch list from changes,
689 // by making a copy and modifying that instead.
690 // Avoid bleedthrough of one device's customizations to
691 // the next device, as each should be independently customizable.
692 psuProperties.push_back(findProperty->second);
693 auto psuProperty = psuProperties.rbegin();
694
695 // Use label head as prefix for reading from config file,
696 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
697 std::string keyName = labelHead + "_Name";
698 std::string keyScale = labelHead + "_Scale";
699 std::string keyMin = labelHead + "_Min";
700 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800701 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800702 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700703
704 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700705 auto findCustomName = baseConfig->find(keyName);
706 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700707 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700708 try
709 {
710 psuProperty->labelTypeName = std::visit(
711 VariantToStringVisitor(), findCustomName->second);
712 }
Patrick Williams26601e82021-10-06 12:43:25 -0500713 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700714 {
715 std::cerr << "Unable to parse " << keyName << "\n";
716 continue;
717 }
718
719 // All strings are valid, including empty string
720 customizedName = true;
721 }
722
723 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700724 auto findCustomScale = baseConfig->find(keyScale);
725 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700726 {
727 try
728 {
729 psuProperty->sensorScaleFactor = std::visit(
730 VariantToUnsignedIntVisitor(), findCustomScale->second);
731 }
Patrick Williams26601e82021-10-06 12:43:25 -0500732 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700733 {
734 std::cerr << "Unable to parse " << keyScale << "\n";
735 continue;
736 }
737
738 // Avoid later division by zero
739 if (psuProperty->sensorScaleFactor > 0)
740 {
741 customizedScale = true;
742 }
743 else
744 {
745 std::cerr << "Unable to accept " << keyScale << "\n";
746 continue;
747 }
748 }
749
Zev Weisse8b97ee2022-08-12 15:23:51 -0700750 auto findCustomMin = baseConfig->find(keyMin);
751 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700752 {
753 try
754 {
755 psuProperty->minReading = std::visit(
756 VariantToDoubleVisitor(), findCustomMin->second);
757 }
Patrick Williams26601e82021-10-06 12:43:25 -0500758 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700759 {
760 std::cerr << "Unable to parse " << keyMin << "\n";
761 continue;
762 }
763 }
764
Zev Weisse8b97ee2022-08-12 15:23:51 -0700765 auto findCustomMax = baseConfig->find(keyMax);
766 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700767 {
768 try
769 {
770 psuProperty->maxReading = std::visit(
771 VariantToDoubleVisitor(), findCustomMax->second);
772 }
Patrick Williams26601e82021-10-06 12:43:25 -0500773 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700774 {
775 std::cerr << "Unable to parse " << keyMax << "\n";
776 continue;
777 }
778 }
779
Zev Weisse8b97ee2022-08-12 15:23:51 -0700780 auto findCustomOffset = baseConfig->find(keyOffset);
781 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800782 {
783 try
784 {
785 psuProperty->sensorOffset = std::visit(
786 VariantToDoubleVisitor(), findCustomOffset->second);
787 }
Patrick Williams26601e82021-10-06 12:43:25 -0500788 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800789 {
790 std::cerr << "Unable to parse " << keyOffset << "\n";
791 continue;
792 }
793 }
794
Lotus Xucb5af732021-09-10 15:18:50 +0800795 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700796 auto findPowerState = baseConfig->find(keyPowerState);
797 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800798 {
799 std::string powerState = std::visit(VariantToStringVisitor(),
800 findPowerState->second);
801 setReadState(powerState, readState);
802 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700803 if (!(psuProperty->minReading < psuProperty->maxReading))
804 {
805 std::cerr << "Min must be less than Max\n";
806 continue;
807 }
808
809 // If the sensor name is being customized by config file,
810 // then prefix/suffix composition becomes not necessary,
811 // and in fact not wanted, because it gets in the way.
812 std::string psuNameFromIndex;
813 if (!customizedName)
814 {
815 /* Find out sensor name index for this label */
816 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500817 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700818 if (std::regex_search(labelHead, matches, rgx))
819 {
820 nameIndex = std::stoi(matches[1]);
821
822 // Decrement to preserve alignment, because hwmon
823 // human-readable filenames and labels use 1-based
824 // numbering, but the "Name", "Name1", "Name2", etc. naming
825 // convention (the psuNames vector) uses 0-based numbering.
826 if (nameIndex > 0)
827 {
828 --nameIndex;
829 }
830 }
831 else
832 {
833 nameIndex = 0;
834 }
835
836 if (psuNames.size() <= nameIndex)
837 {
838 std::cerr << "Could not pair " << labelHead
839 << " with a Name field\n";
840 continue;
841 }
842
843 psuNameFromIndex = psuNames[nameIndex];
844
Ed Tanous8a57ec02020-10-09 12:46:52 -0700845 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700846 {
847 std::cerr << "Sensor label head " << labelHead
848 << " paired with " << psuNameFromIndex
849 << " at index " << nameIndex << "\n";
850 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700851 }
852
Joseph Fub844e972023-03-25 10:24:30 +0800853 if (devType == DevTypes::HWMON)
854 {
855 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
856 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800857
Josh Lehan74d9bd92019-10-31 08:51:58 -0700858 // Similarly, if sensor scaling factor is being customized,
859 // then the below power-of-10 constraint becomes unnecessary,
860 // as config should be able to specify an arbitrary divisor.
861 unsigned int factor = psuProperty->sensorScaleFactor;
862 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700863 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700864 // Preserve existing usage of hardcoded labelMatch table below
865 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700866
Josh Lehan74d9bd92019-10-31 08:51:58 -0700867 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700868 char firstChar =
869 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700870 std::string strScaleFactor =
871 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
872
873 // Preserve existing configs by accepting earlier syntax,
874 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700875 auto findScaleFactor = baseConfig->find(strScaleFactor);
876 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700877 {
878 factor = std::visit(VariantToIntVisitor(),
879 findScaleFactor->second);
880 }
881
Ed Tanous8a57ec02020-10-09 12:46:52 -0700882 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700883 {
884 std::cerr << "Sensor scaling factor " << factor
885 << " string " << strScaleFactor << "\n";
886 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700887 }
888
Vijay Khemka996bad12019-05-28 15:15:16 -0700889 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530890 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
891 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800892 {
James Feist17ab6e02019-06-25 12:28:13 -0700893 std::cerr << "error populating thresholds for "
894 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800895 }
896
Zev Weiss6b6891c2021-04-22 02:46:21 -0500897 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
898 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800899 {
Jason Ling5747fab2019-10-02 16:46:23 -0700900 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700901 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800902 continue;
903 }
904
Ed Tanous8a57ec02020-10-09 12:46:52 -0700905 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700906 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700907 std::cerr << "Sensor properties: Name \""
908 << psuProperty->labelTypeName << "\" Scale "
909 << psuProperty->sensorScaleFactor << " Min "
910 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800911 << psuProperty->maxReading << " Offset "
912 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700913 }
914
915 std::string sensorName = psuProperty->labelTypeName;
916 if (customizedName)
917 {
918 if (sensorName.empty())
919 {
920 // Allow selective disabling of an individual sensor,
921 // by customizing its name to an empty string.
922 std::cerr << "Sensor disabled, empty string\n";
923 continue;
924 }
925 }
926 else
927 {
928 // Sensor name not customized, do prefix/suffix composition,
929 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500930 sensorName = psuNameFromIndex + " " +
931 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700932 }
933
Ed Tanous8a57ec02020-10-09 12:46:52 -0700934 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700935 {
936 std::cerr << "Sensor name \"" << sensorName << "\" path \""
937 << sensorPathStr << "\" type \"" << sensorType
938 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700939 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800940 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800941
942 auto& sensor = sensors[sensorName];
943 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700944 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800945 sensor = nullptr;
946 }
947
948 if (sensor != nullptr)
949 {
950 sensor->activate(sensorPathStr, i2cDev);
951 }
952 else
953 {
954 sensors[sensorName] = std::make_shared<PSUSensor>(
955 sensorPathStr, sensorType, objectServer, dbusConnection, io,
956 sensorName, std::move(sensorThresholds), *interfacePath,
957 readState, findSensorUnit->second, factor,
958 psuProperty->maxReading, psuProperty->minReading,
959 psuProperty->sensorOffset, labelHead, thresholdConfSize,
960 pollRate, i2cDev);
961 sensors[sensorName]->setupRead();
962 ++numCreated;
963 if constexpr (debug)
964 {
965 std::cerr << "Created " << numCreated
966 << " sensors so far\n";
967 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700968 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800969 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800970
Joseph Fub844e972023-03-25 10:24:30 +0800971 if (devType == DevTypes::HWMON)
972 {
973 // OperationalStatus event
974 combineEvents[*psuName + "OperationalStatus"] = nullptr;
975 combineEvents[*psuName + "OperationalStatus"] =
976 std::make_unique<PSUCombineEvent>(
977 objectServer, dbusConnection, io, *psuName, readState,
978 eventPathList, groupEventPathList, "OperationalStatus",
979 pollRate);
980 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800981 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700982
Ed Tanous8a57ec02020-10-09 12:46:52 -0700983 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700984 {
985 std::cerr << "Created total of " << numCreated << " sensors\n";
986 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800987}
Cheng C Yang209ec562019-03-12 16:37:44 +0800988
Matt Simmering39fadb92023-05-04 21:47:29 -0700989static void
990 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
991{
992 static const int depth = 2;
993 static const int numKeys = 1;
994 GetSubTreeType cpuSubTree;
995
996 try
997 {
998 auto getItems = dbusConnection->new_method_call(
999 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
1000 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
1001 std::array<const char*, numKeys>{
1002 "xyz.openbmc_project.Inventory.Item"});
1003 auto getItemsResp = dbusConnection->call(getItems);
1004 getItemsResp.read(cpuSubTree);
1005 }
1006 catch (sdbusplus::exception_t& e)
1007 {
1008 std::cerr << "error getting inventory item subtree: " << e.what()
1009 << "\n";
1010 return;
1011 }
1012
1013 for (const auto& [path, objDict] : cpuSubTree)
1014 {
1015 auto obj = sdbusplus::message::object_path(path).filename();
1016 if (!obj.starts_with("cpu") || objDict.empty())
1017 {
1018 continue;
1019 }
1020 const std::string& owner = objDict.begin()->first;
1021
1022 std::variant<bool> respValue;
1023 try
1024 {
1025 auto getPresence = dbusConnection->new_method_call(
1026 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1027 "Get");
1028 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1029 auto resp = dbusConnection->call(getPresence);
1030 resp.read(respValue);
1031 }
1032 catch (sdbusplus::exception_t& e)
1033 {
1034 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1035 continue;
1036 }
1037
1038 auto* present = std::get_if<bool>(&respValue);
1039 if (present != nullptr && *present)
1040 {
1041 int cpuIndex = 0;
1042 try
1043 {
1044 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1045 }
1046 catch (const std::exception& e)
1047 {
1048 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1049 continue;
1050 }
1051 cpuPresence[cpuIndex + 1] = *present;
1052 }
1053 }
1054}
1055
Zhikui Ren23c96e72020-11-05 22:32:28 -08001056void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001057 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001058 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1059 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001060 sensorsChanged,
1061 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001062{
1063 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001064 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1065 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001066 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1067 sensorsChanged, activateOnly);
1068 });
Matt Simmering786efb82023-01-18 14:09:21 -08001069 std::vector<std::string> types(sensorTypes.size());
1070 for (const auto& [type, dt] : sensorTypes)
1071 {
1072 types.push_back(type);
1073 }
1074 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001075}
1076
Cheng C Yang916360b2019-05-07 18:47:16 +08001077void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001078{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001079 sensorTable = {{"power", sensor_paths::unitWatts},
1080 {"curr", sensor_paths::unitAmperes},
1081 {"temp", sensor_paths::unitDegreesC},
1082 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001083 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001084 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001085
Jeff Line41d52f2021-04-07 19:38:51 +08001086 labelMatch = {
1087 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001088 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1089 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001090 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1091 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1092 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1093 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001094 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1095 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1096 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001097 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1098 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001099 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1100 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001101 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001102 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1103 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001106 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1117 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1118 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1119 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1125 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1126 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1127 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1128 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1129 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1130 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1131 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1132 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1133 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1134 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1135 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1136 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1137 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1138 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001139 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001140 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001141 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1142 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1143 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1144 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1145 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1146 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001147 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001148 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1149 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001150 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1151 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1152 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1153 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1154 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1155 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1156 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1157 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1158 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1159 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1160 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1161 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1162 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1163 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1164 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001165 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1166 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1167 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001168 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1169 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1170 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1171 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1172 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1173 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1174 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1175 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1176 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001177 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1178 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1179 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001180
Matt Simmering6747eba2023-01-18 16:03:55 -08001181 pwmTable = {{"fan1", "Fan_1"},
1182 {"fan2", "Fan_2"},
1183 {"fan3", "Fan_3"},
1184 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001185
1186 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1187 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1188
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001189 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1190 {"Failure", {"in2_alarm"}},
1191 {"ACLost", {"in1_beep"}},
1192 {"ConfigureError", {"in1_fault"}}};
1193
1194 groupEventMatch = {{"FanFault",
1195 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001196 {"fan2", {"fan2_alarm", "fan2_fault"}},
1197 {"fan3", {"fan3_alarm", "fan3_fault"}},
1198 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001199
1200 devParamMap = {
1201 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1202 {DevTypes::IIO,
1203 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001204}
1205
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001206static void powerStateChanged(
1207 PowerState type, bool newState,
1208 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1209 sensors,
1210 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1211 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1212{
1213 if (newState)
1214 {
1215 createSensors(io, objectServer, dbusConnection, nullptr, true);
1216 }
1217 else
1218 {
1219 for (auto& [path, sensor] : sensors)
1220 {
1221 if (sensor != nullptr && sensor->readState == type)
1222 {
1223 sensor->deactivate();
1224 }
1225 }
1226 }
1227}
1228
James Feistb6c0b912019-07-09 12:21:44 -07001229int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001230{
Ed Tanous1f978632023-02-28 18:16:39 -08001231 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001232 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1233
Ed Tanous14ed5e92022-07-12 15:50:23 -07001234 sdbusplus::asio::object_server objectServer(systemBus, true);
1235 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001236 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001237 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001238 auto sensorsChanged =
1239 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001240
Cheng C Yang916360b2019-05-07 18:47:16 +08001241 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001242
Patrick Williams597e8422023-10-20 11:19:01 -05001243 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1244 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001245 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1246 };
1247
1248 setupPowerMatchCallback(systemBus, powerCallBack);
1249
1250 boost::asio::post(io, [&]() {
1251 createSensors(io, objectServer, systemBus, nullptr, false);
1252 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001253 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001254 std::function<void(sdbusplus::message_t&)> eventHandler =
1255 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001256 if (message.is_method_error())
1257 {
1258 std::cerr << "callback method error\n";
1259 return;
1260 }
1261 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001262 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001263 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1264 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001265 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001266 return;
1267 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001268 if (ec)
1269 {
1270 std::cerr << "timer error\n";
1271 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001272 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001273 });
1274 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001275
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001276 boost::asio::steady_timer cpuFilterTimer(io);
1277 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1278 [&](sdbusplus::message_t& message) {
1279 std::string path = message.get_path();
1280 boost::to_lower(path);
1281
1282 sdbusplus::message::object_path cpuPath(path);
1283 std::string cpuName = cpuPath.filename();
1284 if (!cpuName.starts_with("cpu"))
1285 {
1286 return;
1287 }
1288 size_t index = 0;
1289 try
1290 {
1291 index = std::stoi(path.substr(path.size() - 1));
1292 }
1293 catch (const std::invalid_argument&)
1294 {
1295 std::cerr << "Found invalid path " << path << "\n";
1296 return;
1297 }
1298
1299 std::string objectName;
1300 boost::container::flat_map<std::string, std::variant<bool>> values;
1301 message.read(objectName, values);
1302 auto findPresence = values.find("Present");
1303 try
1304 {
1305 cpuPresence[index] = std::get<bool>(findPresence->second);
1306 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001307 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001308 {
1309 return;
1310 }
1311
Chris Sides3fce1bf2023-04-03 16:57:05 -05001312 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001313 {
1314 return;
1315 }
1316 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1317 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1318 if (ec == boost::asio::error::operation_aborted)
1319 {
1320 return;
1321 }
1322 if (ec)
1323 {
1324 std::cerr << "timer error\n";
1325 return;
1326 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001327 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001328 });
1329 };
1330
Zev Weiss214d9712022-08-12 12:54:31 -07001331 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1332 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001333
1334 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1335 static_cast<sdbusplus::bus_t&>(*systemBus),
1336 "type='signal',member='PropertiesChanged',path_namespace='" +
1337 std::string(cpuInventoryPath) +
1338 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1339 cpuPresenceHandler));
1340
Matt Simmering39fadb92023-05-04 21:47:29 -07001341 getPresentCpus(systemBus);
1342
Bruce Lee1263c3d2021-06-04 15:16:33 +08001343 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001344 io.run();
1345}