blob: 89ae91381173e83f5879f45f52aeabc1af6f5465 [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}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800103 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200104 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800105 {"TPS53679", I2CDeviceType{"tps53679", true}},
106 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
107 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
108 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800109 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800110};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700111
Joseph Fub844e972023-03-25 10:24:30 +0800112enum class DevTypes
113{
114 Unknown = 0,
115 HWMON,
116 IIO
117};
118
119struct DevParams
120{
121 unsigned int matchIndex = 0;
122 std::string matchRegEx;
123 std::string nameRegEx;
124};
125
Cheng C Yang209ec562019-03-12 16:37:44 +0800126namespace fs = std::filesystem;
127
Yong Libf8b1da2020-04-15 16:32:50 +0800128static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800129 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800130static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
131 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800132static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
133 pwmSensors;
134static boost::container::flat_map<std::string, std::string> sensorTable;
135static boost::container::flat_map<std::string, PSUProperty> labelMatch;
136static boost::container::flat_map<std::string, std::string> pwmTable;
George Liu5b3542e2023-10-31 17:27:12 +0800137static EventPathList eventMatch;
138static GroupEventPathList groupEventMatch;
139static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800140
Josh Lehan74d9bd92019-10-31 08:51:58 -0700141static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800142static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800143static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700144
Cheng C Yang58b2b532019-05-31 00:19:45 +0800145// Function CheckEvent will check each attribute from eventMatch table in the
146// sysfs. If the attributes exists in sysfs, then store the complete path
147// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800148void checkEvent(const std::string& directory, const EventPathList& eventMatch,
149 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800150{
151 for (const auto& match : eventMatch)
152 {
153 const std::vector<std::string>& eventAttrs = match.second;
154 const std::string& eventName = match.first;
155 for (const auto& eventAttr : eventAttrs)
156 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700157 std::string eventPath = directory;
158 eventPath += "/";
159 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800160
161 std::ifstream eventFile(eventPath);
162 if (!eventFile.good())
163 {
164 continue;
165 }
166
167 eventPathList[eventName].push_back(eventPath);
168 }
169 }
170}
171
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800172// Check Group Events which contains more than one targets in each combine
173// events.
George Liu5b3542e2023-10-31 17:27:12 +0800174void checkGroupEvent(const std::string& directory,
175 const GroupEventPathList& groupEventMatch,
176 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800177{
178 for (const auto& match : groupEventMatch)
179 {
180 const std::string& groupEventName = match.first;
George Liu5b3542e2023-10-31 17:27:12 +0800181 const EventPathList events = match.second;
182 EventPathList pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800183 for (const auto& match : events)
184 {
185 const std::string& eventName = match.first;
186 const std::vector<std::string>& eventAttrs = match.second;
187 for (const auto& eventAttr : eventAttrs)
188 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700189 std::string eventPath = directory;
190 eventPath += "/";
191 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800192 std::ifstream eventFile(eventPath);
193 if (!eventFile.good())
194 {
195 continue;
196 }
197
198 pathList[eventName].push_back(eventPath);
199 }
200 }
201 groupEventPathList[groupEventName] = pathList;
202 }
203}
204
Cheng C Yang58b2b532019-05-31 00:19:45 +0800205// Function checkEventLimits will check all the psu related xxx_input attributes
206// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
207// xxx_min_alarm exist, then store the existing paths of the alarm attributes
208// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800209void checkEventLimits(const std::string& sensorPathStr,
210 const EventPathList& limitEventMatch,
211 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800212{
Lei YUa2c7cea2020-12-23 14:07:28 +0800213 auto attributePartPos = sensorPathStr.find_last_of('_');
214 if (attributePartPos == std::string::npos)
215 {
216 // There is no '_' in the string, skip it
217 return;
218 }
219 auto attributePart =
220 std::string_view(sensorPathStr).substr(attributePartPos + 1);
221 if (attributePart != "input")
222 {
223 // If the sensor is not xxx_input, skip it
224 return;
225 }
226
227 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800228 for (const auto& limitMatch : limitEventMatch)
229 {
230 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
231 const std::string& eventName = limitMatch.first;
232 for (const auto& limitEventAttr : limitEventAttrs)
233 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800234 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800235 std::ifstream eventFile(limitEventPath);
236 if (!eventFile.good())
237 {
238 continue;
239 }
240 eventPathList[eventName].push_back(limitEventPath);
241 }
242 }
243}
Cheng C Yang916360b2019-05-07 18:47:16 +0800244
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530245static void
246 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
247 const std::string& interfacePath,
248 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
249 sdbusplus::asio::object_server& objectServer,
250 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800251{
Zev Weiss741f26e2022-08-12 18:21:02 -0700252 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800253 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700254 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800255 {
256 continue;
257 }
258
259 const std::string& sensorPathStr = sensorPath.string();
260 const std::string& pwmPathStr =
261 boost::replace_all_copy(sensorPathStr, "input", "target");
262 std::ifstream pwmFile(pwmPathStr);
263 if (!pwmFile.good())
264 {
265 continue;
266 }
267
268 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
269 if (findPWMSensor != pwmSensors.end())
270 {
271 continue;
272 }
273
Zev Weissd8c293a2022-08-15 18:58:41 -0700274 std::string name = "Pwm_";
275 name += psuName;
276 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700277 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700278
279 std::string objPath = interfacePath;
280 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700281 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700282
Cheng C Yang916360b2019-05-07 18:47:16 +0800283 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700284 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800285 }
286}
287
Zhikui Ren23c96e72020-11-05 22:32:28 -0800288static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800289 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800290 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
291 const ManagedObjectType& sensorConfigs,
292 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800293 sensorsChanged,
294 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800295{
Josh Lehan49cfba92019-10-08 16:50:42 -0700296 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800297 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800298
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800299 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
300
Cheng C Yang209ec562019-03-12 16:37:44 +0800301 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800302 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
303 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
304 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800305 {
306 std::cerr << "No PSU sensors in system\n";
307 return;
308 }
309
310 boost::container::flat_set<std::string> directories;
311 for (const auto& pmbusPath : pmbusPaths)
312 {
George Liu5b3542e2023-10-31 17:27:12 +0800313 EventPathList eventPathList;
314 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800315
316 std::ifstream nameFile(pmbusPath);
317 if (!nameFile.good())
318 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700319 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800320 continue;
321 }
322
323 std::string pmbusName;
324 std::getline(nameFile, pmbusName);
325 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700326
Matt Simmering786efb82023-01-18 14:09:21 -0800327 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800328 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700329 // To avoid this error message, add your driver name to
330 // the pmbusNames vector at the top of this file.
331 std::cerr << "Driver name " << pmbusName
332 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800333 continue;
334 }
335
Cheng C Yang209ec562019-03-12 16:37:44 +0800336 auto directory = pmbusPath.parent_path();
337
338 auto ret = directories.insert(directory.string());
339 if (!ret.second)
340 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700341 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800342 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800343 }
344
Joseph Fub844e972023-03-25 10:24:30 +0800345 DevTypes devType = DevTypes::HWMON;
346 std::string deviceName;
347 if (directory.parent_path() == "/sys/class/hwmon")
348 {
349 deviceName = fs::canonical(directory / "device").stem();
350 }
351 else
352 {
353 deviceName = fs::canonical(directory).parent_path().stem();
354 devType = DevTypes::IIO;
355 }
356
Cheng C Yang209ec562019-03-12 16:37:44 +0800357 size_t bus = 0;
358 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000359 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 continue;
362 }
363
Zev Weisse8b97ee2022-08-12 15:23:51 -0700364 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 const SensorData* sensorData = nullptr;
366 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800367 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800368 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800369
Zev Weiss741f26e2022-08-12 18:21:02 -0700370 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800371 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700372 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800373 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800374 {
Zev Weiss054aad82022-08-18 01:37:34 -0700375 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 if (sensorBase != sensorData->end())
377 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700378 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 sensorType = type;
380 break;
381 }
382 }
383 if (baseConfig == nullptr)
384 {
385 std::cerr << "error finding base configuration for "
386 << deviceName << "\n";
387 continue;
388 }
389
Zev Weisse8b97ee2022-08-12 15:23:51 -0700390 auto configBus = baseConfig->find("Bus");
391 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800392
Zev Weisse8b97ee2022-08-12 15:23:51 -0700393 if (configBus == baseConfig->end() ||
394 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800396 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800397 continue;
398 }
399
Matt Simmering6747eba2023-01-18 16:03:55 -0800400 const uint64_t* confBus =
401 std::get_if<uint64_t>(&(configBus->second));
402 const uint64_t* confAddr =
403 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800404 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800405 {
406 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700407 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800408 continue;
409 }
410
411 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800412 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200413 if constexpr (debug)
414 {
415 std::cerr << "Configuration skipping " << *confBus << "-"
416 << *confAddr << " because not " << bus << "-"
417 << addr << "\n";
418 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 continue;
420 }
421
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800422 std::vector<thresholds::Threshold> confThresholds;
423 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
424 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800425 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800426 }
427 thresholdConfSize = confThresholds.size();
428
Zev Weiss741f26e2022-08-12 18:21:02 -0700429 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800430 break;
431 }
432 if (interfacePath == nullptr)
433 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700434 // To avoid this error message, add your export map entry,
435 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800436 std::cerr << "failed to find match for " << deviceName << "\n";
437 continue;
438 }
439
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800440 auto findI2CDev = devices.find(*interfacePath);
441
442 std::shared_ptr<I2CDevice> i2cDev;
443 if (findI2CDev != devices.end())
444 {
445 if (activateOnly && !findI2CDev->second.second)
446 {
447 continue;
448 }
449 i2cDev = findI2CDev->second.first;
450 }
451
Zev Weisse8b97ee2022-08-12 15:23:51 -0700452 auto findPSUName = baseConfig->find("Name");
453 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800454 {
455 std::cerr << "could not determine configuration name for "
456 << deviceName << "\n";
457 continue;
458 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800459 const std::string* psuName =
460 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800461 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800462 {
463 std::cerr << "Cannot find psu name, invalid configuration\n";
464 continue;
465 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800466
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800467 auto findCPU = baseConfig->find("CPURequired");
468 if (findCPU != baseConfig->end())
469 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500470 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800471 auto presenceFind = cpuPresence.find(index);
472 if (presenceFind == cpuPresence.end() || !presenceFind->second)
473 {
474 continue;
475 }
476 }
477
Zhikui Ren23c96e72020-11-05 22:32:28 -0800478 // on rescans, only update sensors we were signaled by
479 if (!firstScan)
480 {
Zhikui Renda98f092021-11-01 09:41:08 -0700481 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500482 auto it = std::find_if(sensorsChanged->begin(),
483 sensorsChanged->end(),
484 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800485 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500486 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800487
488 if (it == sensorsChanged->end())
489 {
490 continue;
491 }
492 sensorsChanged->erase(it);
493 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800494 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800495 checkGroupEvent(directory.string(), groupEventMatch,
496 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800497
Zev Weisse8b97ee2022-08-12 15:23:51 -0700498 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000499
Vijay Khemka996bad12019-05-28 15:15:16 -0700500 /* Check if there are more sensors in the same interface */
501 int i = 1;
502 std::vector<std::string> psuNames;
503 do
504 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700505 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700506 psuNames.push_back(
507 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700508 findPSUName = baseConfig->find("Name" + std::to_string(i++));
509 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700510
Cheng C Yange50345b2019-04-02 17:26:15 +0800511 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800512 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
513 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800514 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800515 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800516 continue;
517 }
518
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530519 /* read max value in sysfs for in, curr, power, temp, ... */
520 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
521 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700522 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530523 {
524 std::cerr << "No max name in PSU \n";
525 }
526 }
527
Zev Weiss8569bf22022-10-11 15:37:44 -0700528 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800529
Vijay Khemka996bad12019-05-28 15:15:16 -0700530 /* Find array of labels to be exposed if it is defined in config */
531 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700532 auto findLabelObj = baseConfig->find("Labels");
533 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700534 {
535 findLabels =
536 std::get<std::vector<std::string>>(findLabelObj->second);
537 }
538
Joseph Fub844e972023-03-25 10:24:30 +0800539 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700540 std::smatch matches;
541
Cheng C Yange50345b2019-04-02 17:26:15 +0800542 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800543 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530544 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800545 std::string labelHead;
546 std::string sensorPathStr = sensorPath.string();
547 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700548 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700549 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
550 {
Josh Lehan06494452019-10-31 09:49:16 -0700551 // hwmon *_input filename without number:
552 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800553 // iio in_*_raw filename without number:
554 // voltage, temp, pressure, ...
555 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700556 }
557 else
558 {
Josh Lehan06494452019-10-31 09:49:16 -0700559 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700560 << sensorNameStr;
561 continue;
562 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800563
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530564 std::string labelPath;
565
Joseph Fub844e972023-03-25 10:24:30 +0800566 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530567 {
Joseph Fub844e972023-03-25 10:24:30 +0800568 /* find and differentiate _max and _input to replace "label" */
569 size_t pos = sensorPathStr.find('_');
570 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530571 {
Joseph Fub844e972023-03-25 10:24:30 +0800572 std::string sensorPathStrMax = sensorPathStr.substr(pos);
573 if (sensorPathStrMax == "_max")
574 {
575 labelPath = boost::replace_all_copy(sensorPathStr,
576 "max", "label");
577 maxLabel = true;
578 }
579 else
580 {
581 labelPath = boost::replace_all_copy(sensorPathStr,
582 "input", "label");
583 maxLabel = false;
584 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530585 }
586 else
587 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800588 continue;
589 }
590
Joseph Fub844e972023-03-25 10:24:30 +0800591 std::ifstream labelFile(labelPath);
592 if (!labelFile.good())
593 {
594 if constexpr (debug)
595 {
596 std::cerr << "Input file " << sensorPath
597 << " has no corresponding label file\n";
598 }
599 // hwmon *_input filename with number:
600 // temp1, temp2, temp3, ...
601 labelHead = sensorNameStr.substr(0,
602 sensorNameStr.find('_'));
603 }
604 else
605 {
606 std::string label;
607 std::getline(labelFile, label);
608 labelFile.close();
609 auto findSensor = sensors.find(label);
610 if (findSensor != sensors.end())
611 {
612 continue;
613 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800614
Joseph Fub844e972023-03-25 10:24:30 +0800615 // hwmon corresponding *_label file contents:
616 // vin1, vout1, ...
617 labelHead = label.substr(0, label.find(' '));
618 }
619
620 /* append "max" for labelMatch */
621 if (maxLabel)
622 {
623 labelHead.insert(0, "max");
624 }
625
626 checkPWMSensor(sensorPath, labelHead, *interfacePath,
627 dbusConnection, objectServer, psuNames[0]);
628 }
629 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530630 {
Joseph Fub844e972023-03-25 10:24:30 +0800631 auto findIIOHyphen = sensorNameStr.find_last_of('_');
632 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530633 }
634
Ed Tanous8a57ec02020-10-09 12:46:52 -0700635 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700636 {
637 std::cerr << "Sensor type=\"" << sensorNameSubStr
638 << "\" label=\"" << labelHead << "\"\n";
639 }
640
Vijay Khemka996bad12019-05-28 15:15:16 -0700641 if (!findLabels.empty())
642 {
643 /* Check if this labelHead is enabled in config file */
644 if (std::find(findLabels.begin(), findLabels.end(),
645 labelHead) == findLabels.end())
646 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700647 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800648 {
649 std::cerr << "could not find " << labelHead
650 << " in the Labels list\n";
651 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700652 continue;
653 }
654 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800655
Cheng C Yange50345b2019-04-02 17:26:15 +0800656 auto findProperty = labelMatch.find(labelHead);
657 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800658 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700659 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700660 {
661 std::cerr << "Could not find matching default property for "
662 << labelHead << "\n";
663 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800664 continue;
665 }
666
Josh Lehan74d9bd92019-10-31 08:51:58 -0700667 // Protect the hardcoded labelMatch list from changes,
668 // by making a copy and modifying that instead.
669 // Avoid bleedthrough of one device's customizations to
670 // the next device, as each should be independently customizable.
671 psuProperties.push_back(findProperty->second);
672 auto psuProperty = psuProperties.rbegin();
673
674 // Use label head as prefix for reading from config file,
675 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
676 std::string keyName = labelHead + "_Name";
677 std::string keyScale = labelHead + "_Scale";
678 std::string keyMin = labelHead + "_Min";
679 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800680 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800681 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700682
683 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700684 auto findCustomName = baseConfig->find(keyName);
685 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700686 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700687 try
688 {
689 psuProperty->labelTypeName = std::visit(
690 VariantToStringVisitor(), findCustomName->second);
691 }
Patrick Williams26601e82021-10-06 12:43:25 -0500692 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700693 {
694 std::cerr << "Unable to parse " << keyName << "\n";
695 continue;
696 }
697
698 // All strings are valid, including empty string
699 customizedName = true;
700 }
701
702 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700703 auto findCustomScale = baseConfig->find(keyScale);
704 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700705 {
706 try
707 {
708 psuProperty->sensorScaleFactor = std::visit(
709 VariantToUnsignedIntVisitor(), findCustomScale->second);
710 }
Patrick Williams26601e82021-10-06 12:43:25 -0500711 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700712 {
713 std::cerr << "Unable to parse " << keyScale << "\n";
714 continue;
715 }
716
717 // Avoid later division by zero
718 if (psuProperty->sensorScaleFactor > 0)
719 {
720 customizedScale = true;
721 }
722 else
723 {
724 std::cerr << "Unable to accept " << keyScale << "\n";
725 continue;
726 }
727 }
728
Zev Weisse8b97ee2022-08-12 15:23:51 -0700729 auto findCustomMin = baseConfig->find(keyMin);
730 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700731 {
732 try
733 {
734 psuProperty->minReading = std::visit(
735 VariantToDoubleVisitor(), findCustomMin->second);
736 }
Patrick Williams26601e82021-10-06 12:43:25 -0500737 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700738 {
739 std::cerr << "Unable to parse " << keyMin << "\n";
740 continue;
741 }
742 }
743
Zev Weisse8b97ee2022-08-12 15:23:51 -0700744 auto findCustomMax = baseConfig->find(keyMax);
745 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700746 {
747 try
748 {
749 psuProperty->maxReading = std::visit(
750 VariantToDoubleVisitor(), findCustomMax->second);
751 }
Patrick Williams26601e82021-10-06 12:43:25 -0500752 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700753 {
754 std::cerr << "Unable to parse " << keyMax << "\n";
755 continue;
756 }
757 }
758
Zev Weisse8b97ee2022-08-12 15:23:51 -0700759 auto findCustomOffset = baseConfig->find(keyOffset);
760 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800761 {
762 try
763 {
764 psuProperty->sensorOffset = std::visit(
765 VariantToDoubleVisitor(), findCustomOffset->second);
766 }
Patrick Williams26601e82021-10-06 12:43:25 -0500767 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800768 {
769 std::cerr << "Unable to parse " << keyOffset << "\n";
770 continue;
771 }
772 }
773
Lotus Xucb5af732021-09-10 15:18:50 +0800774 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700775 auto findPowerState = baseConfig->find(keyPowerState);
776 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800777 {
778 std::string powerState = std::visit(VariantToStringVisitor(),
779 findPowerState->second);
780 setReadState(powerState, readState);
781 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700782 if (!(psuProperty->minReading < psuProperty->maxReading))
783 {
784 std::cerr << "Min must be less than Max\n";
785 continue;
786 }
787
788 // If the sensor name is being customized by config file,
789 // then prefix/suffix composition becomes not necessary,
790 // and in fact not wanted, because it gets in the way.
791 std::string psuNameFromIndex;
792 if (!customizedName)
793 {
794 /* Find out sensor name index for this label */
795 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500796 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700797 if (std::regex_search(labelHead, matches, rgx))
798 {
799 nameIndex = std::stoi(matches[1]);
800
801 // Decrement to preserve alignment, because hwmon
802 // human-readable filenames and labels use 1-based
803 // numbering, but the "Name", "Name1", "Name2", etc. naming
804 // convention (the psuNames vector) uses 0-based numbering.
805 if (nameIndex > 0)
806 {
807 --nameIndex;
808 }
809 }
810 else
811 {
812 nameIndex = 0;
813 }
814
815 if (psuNames.size() <= nameIndex)
816 {
817 std::cerr << "Could not pair " << labelHead
818 << " with a Name field\n";
819 continue;
820 }
821
822 psuNameFromIndex = psuNames[nameIndex];
823
Ed Tanous8a57ec02020-10-09 12:46:52 -0700824 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700825 {
826 std::cerr << "Sensor label head " << labelHead
827 << " paired with " << psuNameFromIndex
828 << " at index " << nameIndex << "\n";
829 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700830 }
831
Joseph Fub844e972023-03-25 10:24:30 +0800832 if (devType == DevTypes::HWMON)
833 {
834 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
835 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800836
Josh Lehan74d9bd92019-10-31 08:51:58 -0700837 // Similarly, if sensor scaling factor is being customized,
838 // then the below power-of-10 constraint becomes unnecessary,
839 // as config should be able to specify an arbitrary divisor.
840 unsigned int factor = psuProperty->sensorScaleFactor;
841 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700842 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 // Preserve existing usage of hardcoded labelMatch table below
844 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700845
Josh Lehan74d9bd92019-10-31 08:51:58 -0700846 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700847 char firstChar =
848 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700849 std::string strScaleFactor =
850 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
851
852 // Preserve existing configs by accepting earlier syntax,
853 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700854 auto findScaleFactor = baseConfig->find(strScaleFactor);
855 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700856 {
857 factor = std::visit(VariantToIntVisitor(),
858 findScaleFactor->second);
859 }
860
Ed Tanous8a57ec02020-10-09 12:46:52 -0700861 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700862 {
863 std::cerr << "Sensor scaling factor " << factor
864 << " string " << strScaleFactor << "\n";
865 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700866 }
867
Vijay Khemka996bad12019-05-28 15:15:16 -0700868 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530869 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
870 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800871 {
James Feist17ab6e02019-06-25 12:28:13 -0700872 std::cerr << "error populating thresholds for "
873 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800874 }
875
Zev Weiss6b6891c2021-04-22 02:46:21 -0500876 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
877 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800878 {
Jason Ling5747fab2019-10-02 16:46:23 -0700879 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700880 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800881 continue;
882 }
883
Ed Tanous8a57ec02020-10-09 12:46:52 -0700884 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700885 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700886 std::cerr << "Sensor properties: Name \""
887 << psuProperty->labelTypeName << "\" Scale "
888 << psuProperty->sensorScaleFactor << " Min "
889 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800890 << psuProperty->maxReading << " Offset "
891 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700892 }
893
894 std::string sensorName = psuProperty->labelTypeName;
895 if (customizedName)
896 {
897 if (sensorName.empty())
898 {
899 // Allow selective disabling of an individual sensor,
900 // by customizing its name to an empty string.
901 std::cerr << "Sensor disabled, empty string\n";
902 continue;
903 }
904 }
905 else
906 {
907 // Sensor name not customized, do prefix/suffix composition,
908 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500909 sensorName = psuNameFromIndex + " " +
910 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700911 }
912
Ed Tanous8a57ec02020-10-09 12:46:52 -0700913 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700914 {
915 std::cerr << "Sensor name \"" << sensorName << "\" path \""
916 << sensorPathStr << "\" type \"" << sensorType
917 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700918 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800919 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800920
921 auto& sensor = sensors[sensorName];
922 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700923 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800924 sensor = nullptr;
925 }
926
927 if (sensor != nullptr)
928 {
929 sensor->activate(sensorPathStr, i2cDev);
930 }
931 else
932 {
933 sensors[sensorName] = std::make_shared<PSUSensor>(
934 sensorPathStr, sensorType, objectServer, dbusConnection, io,
935 sensorName, std::move(sensorThresholds), *interfacePath,
936 readState, findSensorUnit->second, factor,
937 psuProperty->maxReading, psuProperty->minReading,
938 psuProperty->sensorOffset, labelHead, thresholdConfSize,
939 pollRate, i2cDev);
940 sensors[sensorName]->setupRead();
941 ++numCreated;
942 if constexpr (debug)
943 {
944 std::cerr << "Created " << numCreated
945 << " sensors so far\n";
946 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700947 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800948 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800949
Joseph Fub844e972023-03-25 10:24:30 +0800950 if (devType == DevTypes::HWMON)
951 {
952 // OperationalStatus event
953 combineEvents[*psuName + "OperationalStatus"] = nullptr;
954 combineEvents[*psuName + "OperationalStatus"] =
955 std::make_unique<PSUCombineEvent>(
956 objectServer, dbusConnection, io, *psuName, readState,
957 eventPathList, groupEventPathList, "OperationalStatus",
958 pollRate);
959 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800960 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700961
Ed Tanous8a57ec02020-10-09 12:46:52 -0700962 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700963 {
964 std::cerr << "Created total of " << numCreated << " sensors\n";
965 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800966}
Cheng C Yang209ec562019-03-12 16:37:44 +0800967
Matt Simmering39fadb92023-05-04 21:47:29 -0700968static void
969 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
970{
971 static const int depth = 2;
972 static const int numKeys = 1;
973 GetSubTreeType cpuSubTree;
974
975 try
976 {
977 auto getItems = dbusConnection->new_method_call(
978 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
979 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
980 std::array<const char*, numKeys>{
981 "xyz.openbmc_project.Inventory.Item"});
982 auto getItemsResp = dbusConnection->call(getItems);
983 getItemsResp.read(cpuSubTree);
984 }
985 catch (sdbusplus::exception_t& e)
986 {
987 std::cerr << "error getting inventory item subtree: " << e.what()
988 << "\n";
989 return;
990 }
991
992 for (const auto& [path, objDict] : cpuSubTree)
993 {
994 auto obj = sdbusplus::message::object_path(path).filename();
995 if (!obj.starts_with("cpu") || objDict.empty())
996 {
997 continue;
998 }
999 const std::string& owner = objDict.begin()->first;
1000
1001 std::variant<bool> respValue;
1002 try
1003 {
1004 auto getPresence = dbusConnection->new_method_call(
1005 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1006 "Get");
1007 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1008 auto resp = dbusConnection->call(getPresence);
1009 resp.read(respValue);
1010 }
1011 catch (sdbusplus::exception_t& e)
1012 {
1013 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1014 continue;
1015 }
1016
1017 auto* present = std::get_if<bool>(&respValue);
1018 if (present != nullptr && *present)
1019 {
1020 int cpuIndex = 0;
1021 try
1022 {
1023 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1024 }
1025 catch (const std::exception& e)
1026 {
1027 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1028 continue;
1029 }
1030 cpuPresence[cpuIndex + 1] = *present;
1031 }
1032 }
1033}
1034
Zhikui Ren23c96e72020-11-05 22:32:28 -08001035void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001036 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001037 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1038 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001039 sensorsChanged,
1040 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001041{
1042 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001043 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1044 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001045 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1046 sensorsChanged, activateOnly);
1047 });
Matt Simmering786efb82023-01-18 14:09:21 -08001048 std::vector<std::string> types(sensorTypes.size());
1049 for (const auto& [type, dt] : sensorTypes)
1050 {
1051 types.push_back(type);
1052 }
1053 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001054}
1055
Cheng C Yang916360b2019-05-07 18:47:16 +08001056void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001057{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001058 sensorTable = {{"power", sensor_paths::unitWatts},
1059 {"curr", sensor_paths::unitAmperes},
1060 {"temp", sensor_paths::unitDegreesC},
1061 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001062 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001063 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001064
Jeff Line41d52f2021-04-07 19:38:51 +08001065 labelMatch = {
1066 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001067 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1068 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001069 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1070 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1071 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1072 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001073 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1074 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1075 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001076 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1077 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001078 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1079 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001080 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001081 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1082 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1083 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1084 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001085 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1086 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1087 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1088 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1089 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1090 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1091 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1092 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1093 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1094 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1095 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1096 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1097 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1098 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1099 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1100 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1101 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1102 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1103 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1117 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001118 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001119 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001120 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1125 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001126 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001127 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1128 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001129 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1130 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1131 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1132 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1133 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1134 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1135 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1136 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1137 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1138 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1139 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1140 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1141 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1142 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1143 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001144 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1145 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1146 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001147 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1148 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1149 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1150 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1151 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1152 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1153 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1154 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1155 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001156 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1157 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1158 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001159
Matt Simmering6747eba2023-01-18 16:03:55 -08001160 pwmTable = {{"fan1", "Fan_1"},
1161 {"fan2", "Fan_2"},
1162 {"fan3", "Fan_3"},
1163 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001164
1165 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1166 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1167
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001168 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1169 {"Failure", {"in2_alarm"}},
1170 {"ACLost", {"in1_beep"}},
1171 {"ConfigureError", {"in1_fault"}}};
1172
1173 groupEventMatch = {{"FanFault",
1174 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001175 {"fan2", {"fan2_alarm", "fan2_fault"}},
1176 {"fan3", {"fan3_alarm", "fan3_fault"}},
1177 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001178
1179 devParamMap = {
1180 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1181 {DevTypes::IIO,
1182 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001183}
1184
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001185static void powerStateChanged(
1186 PowerState type, bool newState,
1187 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1188 sensors,
1189 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1190 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1191{
1192 if (newState)
1193 {
1194 createSensors(io, objectServer, dbusConnection, nullptr, true);
1195 }
1196 else
1197 {
1198 for (auto& [path, sensor] : sensors)
1199 {
1200 if (sensor != nullptr && sensor->readState == type)
1201 {
1202 sensor->deactivate();
1203 }
1204 }
1205 }
1206}
1207
James Feistb6c0b912019-07-09 12:21:44 -07001208int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001209{
Ed Tanous1f978632023-02-28 18:16:39 -08001210 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001211 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1212
Ed Tanous14ed5e92022-07-12 15:50:23 -07001213 sdbusplus::asio::object_server objectServer(systemBus, true);
1214 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001215 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001216 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001217 auto sensorsChanged =
1218 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001219
Cheng C Yang916360b2019-05-07 18:47:16 +08001220 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001221
Patrick Williams597e8422023-10-20 11:19:01 -05001222 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1223 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001224 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1225 };
1226
1227 setupPowerMatchCallback(systemBus, powerCallBack);
1228
1229 boost::asio::post(io, [&]() {
1230 createSensors(io, objectServer, systemBus, nullptr, false);
1231 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001232 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001233 std::function<void(sdbusplus::message_t&)> eventHandler =
1234 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001235 if (message.is_method_error())
1236 {
1237 std::cerr << "callback method error\n";
1238 return;
1239 }
1240 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001241 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001242 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1243 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001244 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001245 return;
1246 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001247 if (ec)
1248 {
1249 std::cerr << "timer error\n";
1250 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001251 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001252 });
1253 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001254
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001255 boost::asio::steady_timer cpuFilterTimer(io);
1256 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1257 [&](sdbusplus::message_t& message) {
1258 std::string path = message.get_path();
1259 boost::to_lower(path);
1260
1261 sdbusplus::message::object_path cpuPath(path);
1262 std::string cpuName = cpuPath.filename();
1263 if (!cpuName.starts_with("cpu"))
1264 {
1265 return;
1266 }
1267 size_t index = 0;
1268 try
1269 {
1270 index = std::stoi(path.substr(path.size() - 1));
1271 }
1272 catch (const std::invalid_argument&)
1273 {
1274 std::cerr << "Found invalid path " << path << "\n";
1275 return;
1276 }
1277
1278 std::string objectName;
1279 boost::container::flat_map<std::string, std::variant<bool>> values;
1280 message.read(objectName, values);
1281 auto findPresence = values.find("Present");
1282 try
1283 {
1284 cpuPresence[index] = std::get<bool>(findPresence->second);
1285 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001286 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001287 {
1288 return;
1289 }
1290
Chris Sides3fce1bf2023-04-03 16:57:05 -05001291 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001292 {
1293 return;
1294 }
1295 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1296 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1297 if (ec == boost::asio::error::operation_aborted)
1298 {
1299 return;
1300 }
1301 if (ec)
1302 {
1303 std::cerr << "timer error\n";
1304 return;
1305 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001306 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001307 });
1308 };
1309
Zev Weiss214d9712022-08-12 12:54:31 -07001310 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1311 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001312
1313 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1314 static_cast<sdbusplus::bus_t&>(*systemBus),
1315 "type='signal',member='PropertiesChanged',path_namespace='" +
1316 std::string(cpuInventoryPath) +
1317 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1318 cpuPresenceHandler));
1319
Matt Simmering39fadb92023-05-04 21:47:29 -07001320 getPresentCpus(systemBus);
1321
Bruce Lee1263c3d2021-06-04 15:16:33 +08001322 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001323 io.run();
1324}