blob: 4073d725bc760056910309baf3feeb31ec805419 [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}},
Joseph Fu15a39df2023-12-27 09:45:49 +080077 {"MAX11607", I2CDeviceType{"max11607", false}},
Matt Simmering786efb82023-01-18 14:09:21 -080078 {"MAX16601", I2CDeviceType{"max16601", true}},
79 {"MAX20710", I2CDeviceType{"max20710", true}},
80 {"MAX20730", I2CDeviceType{"max20730", true}},
81 {"MAX20734", I2CDeviceType{"max20734", true}},
82 {"MAX20796", I2CDeviceType{"max20796", true}},
83 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +080084 {"MP2856", I2CDeviceType{"mp2856", true}},
85 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080086 {"MP2971", I2CDeviceType{"mp2971", true}},
87 {"MP2973", I2CDeviceType{"mp2973", true}},
88 {"MP2975", I2CDeviceType{"mp2975", true}},
89 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +080090 {"MP5990", I2CDeviceType{"mp5990", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080091 {"NCP4200", I2CDeviceType{"ncp4200", true}},
92 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
93 {"pmbus", I2CDeviceType{"pmbus", true}},
94 {"PXE1610", I2CDeviceType{"pxe1610", true}},
95 {"RAA228000", I2CDeviceType{"raa228000", true}},
96 {"RAA228228", I2CDeviceType{"raa228228", true}},
97 {"RAA228620", I2CDeviceType{"raa228620", true}},
98 {"RAA229001", I2CDeviceType{"raa229001", true}},
99 {"RAA229004", I2CDeviceType{"raa229004", true}},
100 {"RAA229126", I2CDeviceType{"raa229126", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200101 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800102 {"TPS53679", I2CDeviceType{"tps53679", true}},
103 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
104 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
105 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800106 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800107};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700108
Joseph Fub844e972023-03-25 10:24:30 +0800109enum class DevTypes
110{
111 Unknown = 0,
112 HWMON,
113 IIO
114};
115
116struct DevParams
117{
118 unsigned int matchIndex = 0;
119 std::string matchRegEx;
120 std::string nameRegEx;
121};
122
Cheng C Yang209ec562019-03-12 16:37:44 +0800123namespace fs = std::filesystem;
124
Yong Libf8b1da2020-04-15 16:32:50 +0800125static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800126 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800127static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
128 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800129static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
130 pwmSensors;
131static boost::container::flat_map<std::string, std::string> sensorTable;
132static boost::container::flat_map<std::string, PSUProperty> labelMatch;
133static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800134static boost::container::flat_map<std::string, std::vector<std::string>>
135 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800136static boost::container::flat_map<
137 std::string,
138 boost::container::flat_map<std::string, std::vector<std::string>>>
139 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800140static boost::container::flat_map<std::string, std::vector<std::string>>
141 limitEventMatch;
142
Josh Lehan74d9bd92019-10-31 08:51:58 -0700143static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800144static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800145static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700146
Cheng C Yang58b2b532019-05-31 00:19:45 +0800147// Function CheckEvent will check each attribute from eventMatch table in the
148// sysfs. If the attributes exists in sysfs, then store the complete path
149// of the attribute into eventPathList.
150void checkEvent(
151 const std::string& directory,
152 const boost::container::flat_map<std::string, std::vector<std::string>>&
153 eventMatch,
154 boost::container::flat_map<std::string, std::vector<std::string>>&
155 eventPathList)
156{
157 for (const auto& match : eventMatch)
158 {
159 const std::vector<std::string>& eventAttrs = match.second;
160 const std::string& eventName = match.first;
161 for (const auto& eventAttr : eventAttrs)
162 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700163 std::string eventPath = directory;
164 eventPath += "/";
165 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800166
167 std::ifstream eventFile(eventPath);
168 if (!eventFile.good())
169 {
170 continue;
171 }
172
173 eventPathList[eventName].push_back(eventPath);
174 }
175 }
176}
177
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800178// Check Group Events which contains more than one targets in each combine
179// events.
180void checkGroupEvent(
181 const std::string& directory,
182 const boost::container::flat_map<
183 std::string,
184 boost::container::flat_map<std::string, std::vector<std::string>>>&
185 groupEventMatch,
186 boost::container::flat_map<
187 std::string,
188 boost::container::flat_map<std::string, std::vector<std::string>>>&
189 groupEventPathList)
190{
191 for (const auto& match : groupEventMatch)
192 {
193 const std::string& groupEventName = match.first;
194 const boost::container::flat_map<std::string, std::vector<std::string>>
195 events = match.second;
196 boost::container::flat_map<std::string, std::vector<std::string>>
197 pathList;
198 for (const auto& match : events)
199 {
200 const std::string& eventName = match.first;
201 const std::vector<std::string>& eventAttrs = match.second;
202 for (const auto& eventAttr : eventAttrs)
203 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700204 std::string eventPath = directory;
205 eventPath += "/";
206 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800207 std::ifstream eventFile(eventPath);
208 if (!eventFile.good())
209 {
210 continue;
211 }
212
213 pathList[eventName].push_back(eventPath);
214 }
215 }
216 groupEventPathList[groupEventName] = pathList;
217 }
218}
219
Cheng C Yang58b2b532019-05-31 00:19:45 +0800220// Function checkEventLimits will check all the psu related xxx_input attributes
221// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
222// xxx_min_alarm exist, then store the existing paths of the alarm attributes
223// to eventPathList.
224void checkEventLimits(
225 const std::string& sensorPathStr,
226 const boost::container::flat_map<std::string, std::vector<std::string>>&
227 limitEventMatch,
228 boost::container::flat_map<std::string, std::vector<std::string>>&
229 eventPathList)
230{
Lei YUa2c7cea2020-12-23 14:07:28 +0800231 auto attributePartPos = sensorPathStr.find_last_of('_');
232 if (attributePartPos == std::string::npos)
233 {
234 // There is no '_' in the string, skip it
235 return;
236 }
237 auto attributePart =
238 std::string_view(sensorPathStr).substr(attributePartPos + 1);
239 if (attributePart != "input")
240 {
241 // If the sensor is not xxx_input, skip it
242 return;
243 }
244
245 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800246 for (const auto& limitMatch : limitEventMatch)
247 {
248 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
249 const std::string& eventName = limitMatch.first;
250 for (const auto& limitEventAttr : limitEventAttrs)
251 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800252 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800253 std::ifstream eventFile(limitEventPath);
254 if (!eventFile.good())
255 {
256 continue;
257 }
258 eventPathList[eventName].push_back(limitEventPath);
259 }
260 }
261}
Cheng C Yang916360b2019-05-07 18:47:16 +0800262
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530263static void
264 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
265 const std::string& interfacePath,
266 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
267 sdbusplus::asio::object_server& objectServer,
268 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800269{
Zev Weiss741f26e2022-08-12 18:21:02 -0700270 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800271 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700272 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800273 {
274 continue;
275 }
276
277 const std::string& sensorPathStr = sensorPath.string();
278 const std::string& pwmPathStr =
279 boost::replace_all_copy(sensorPathStr, "input", "target");
280 std::ifstream pwmFile(pwmPathStr);
281 if (!pwmFile.good())
282 {
283 continue;
284 }
285
286 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
287 if (findPWMSensor != pwmSensors.end())
288 {
289 continue;
290 }
291
Zev Weissd8c293a2022-08-15 18:58:41 -0700292 std::string name = "Pwm_";
293 name += psuName;
294 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700295 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700296
297 std::string objPath = interfacePath;
298 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700299 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700300
Cheng C Yang916360b2019-05-07 18:47:16 +0800301 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700302 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800303 }
304}
305
Zhikui Ren23c96e72020-11-05 22:32:28 -0800306static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800307 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800308 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
309 const ManagedObjectType& sensorConfigs,
310 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800311 sensorsChanged,
312 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800313{
Josh Lehan49cfba92019-10-08 16:50:42 -0700314 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800315 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800316
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800317 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
318
Cheng C Yang209ec562019-03-12 16:37:44 +0800319 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800320 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
321 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
322 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800323 {
324 std::cerr << "No PSU sensors in system\n";
325 return;
326 }
327
328 boost::container::flat_set<std::string> directories;
329 for (const auto& pmbusPath : pmbusPaths)
330 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800331 boost::container::flat_map<std::string, std::vector<std::string>>
332 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800333 boost::container::flat_map<
334 std::string,
335 boost::container::flat_map<std::string, std::vector<std::string>>>
336 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800337
338 std::ifstream nameFile(pmbusPath);
339 if (!nameFile.good())
340 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700341 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800342 continue;
343 }
344
345 std::string pmbusName;
346 std::getline(nameFile, pmbusName);
347 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700348
Matt Simmering786efb82023-01-18 14:09:21 -0800349 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800350 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700351 // To avoid this error message, add your driver name to
352 // the pmbusNames vector at the top of this file.
353 std::cerr << "Driver name " << pmbusName
354 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800355 continue;
356 }
357
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 auto directory = pmbusPath.parent_path();
359
360 auto ret = directories.insert(directory.string());
361 if (!ret.second)
362 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700363 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800364 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 }
366
Joseph Fub844e972023-03-25 10:24:30 +0800367 DevTypes devType = DevTypes::HWMON;
368 std::string deviceName;
369 if (directory.parent_path() == "/sys/class/hwmon")
370 {
371 deviceName = fs::canonical(directory / "device").stem();
372 }
373 else
374 {
375 deviceName = fs::canonical(directory).parent_path().stem();
376 devType = DevTypes::IIO;
377 }
378
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 size_t bus = 0;
380 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000381 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800382 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800383 continue;
384 }
385
Zev Weisse8b97ee2022-08-12 15:23:51 -0700386 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800387 const SensorData* sensorData = nullptr;
388 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800389 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800390 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800391
Zev Weiss741f26e2022-08-12 18:21:02 -0700392 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800393 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700394 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800395 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800396 {
Zev Weiss054aad82022-08-18 01:37:34 -0700397 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 if (sensorBase != sensorData->end())
399 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700400 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800401 sensorType = type;
402 break;
403 }
404 }
405 if (baseConfig == nullptr)
406 {
407 std::cerr << "error finding base configuration for "
408 << deviceName << "\n";
409 continue;
410 }
411
Zev Weisse8b97ee2022-08-12 15:23:51 -0700412 auto configBus = baseConfig->find("Bus");
413 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800414
Zev Weisse8b97ee2022-08-12 15:23:51 -0700415 if (configBus == baseConfig->end() ||
416 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800417 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800418 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 continue;
420 }
421
Matt Simmering6747eba2023-01-18 16:03:55 -0800422 const uint64_t* confBus =
423 std::get_if<uint64_t>(&(configBus->second));
424 const uint64_t* confAddr =
425 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800426 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800427 {
428 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700429 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800430 continue;
431 }
432
433 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800434 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200435 if constexpr (debug)
436 {
437 std::cerr << "Configuration skipping " << *confBus << "-"
438 << *confAddr << " because not " << bus << "-"
439 << addr << "\n";
440 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800441 continue;
442 }
443
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800444 std::vector<thresholds::Threshold> confThresholds;
445 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
446 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800447 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800448 }
449 thresholdConfSize = confThresholds.size();
450
Zev Weiss741f26e2022-08-12 18:21:02 -0700451 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800452 break;
453 }
454 if (interfacePath == nullptr)
455 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700456 // To avoid this error message, add your export map entry,
457 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800458 std::cerr << "failed to find match for " << deviceName << "\n";
459 continue;
460 }
461
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800462 auto findI2CDev = devices.find(*interfacePath);
463
464 std::shared_ptr<I2CDevice> i2cDev;
465 if (findI2CDev != devices.end())
466 {
467 if (activateOnly && !findI2CDev->second.second)
468 {
469 continue;
470 }
471 i2cDev = findI2CDev->second.first;
472 }
473
Zev Weisse8b97ee2022-08-12 15:23:51 -0700474 auto findPSUName = baseConfig->find("Name");
475 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800476 {
477 std::cerr << "could not determine configuration name for "
478 << deviceName << "\n";
479 continue;
480 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800481 const std::string* psuName =
482 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800483 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800484 {
485 std::cerr << "Cannot find psu name, invalid configuration\n";
486 continue;
487 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800488
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800489 auto findCPU = baseConfig->find("CPURequired");
490 if (findCPU != baseConfig->end())
491 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500492 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800493 auto presenceFind = cpuPresence.find(index);
494 if (presenceFind == cpuPresence.end() || !presenceFind->second)
495 {
496 continue;
497 }
498 }
499
Zhikui Ren23c96e72020-11-05 22:32:28 -0800500 // on rescans, only update sensors we were signaled by
501 if (!firstScan)
502 {
Zhikui Renda98f092021-11-01 09:41:08 -0700503 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500504 auto it = std::find_if(sensorsChanged->begin(),
505 sensorsChanged->end(),
506 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800507 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500508 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800509
510 if (it == sensorsChanged->end())
511 {
512 continue;
513 }
514 sensorsChanged->erase(it);
515 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800516 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800517 checkGroupEvent(directory.string(), groupEventMatch,
518 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800519
Zev Weisse8b97ee2022-08-12 15:23:51 -0700520 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000521
Vijay Khemka996bad12019-05-28 15:15:16 -0700522 /* Check if there are more sensors in the same interface */
523 int i = 1;
524 std::vector<std::string> psuNames;
525 do
526 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700527 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700528 psuNames.push_back(
529 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700530 findPSUName = baseConfig->find("Name" + std::to_string(i++));
531 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700532
Cheng C Yange50345b2019-04-02 17:26:15 +0800533 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800534 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
535 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800536 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800537 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800538 continue;
539 }
540
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530541 /* read max value in sysfs for in, curr, power, temp, ... */
542 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
543 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700544 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530545 {
546 std::cerr << "No max name in PSU \n";
547 }
548 }
549
Zev Weiss8569bf22022-10-11 15:37:44 -0700550 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800551
Vijay Khemka996bad12019-05-28 15:15:16 -0700552 /* Find array of labels to be exposed if it is defined in config */
553 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700554 auto findLabelObj = baseConfig->find("Labels");
555 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700556 {
557 findLabels =
558 std::get<std::vector<std::string>>(findLabelObj->second);
559 }
560
Joseph Fub844e972023-03-25 10:24:30 +0800561 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700562 std::smatch matches;
563
Cheng C Yange50345b2019-04-02 17:26:15 +0800564 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800565 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530566 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800567 std::string labelHead;
568 std::string sensorPathStr = sensorPath.string();
569 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700570 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700571 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
572 {
Josh Lehan06494452019-10-31 09:49:16 -0700573 // hwmon *_input filename without number:
574 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800575 // iio in_*_raw filename without number:
576 // voltage, temp, pressure, ...
577 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700578 }
579 else
580 {
Josh Lehan06494452019-10-31 09:49:16 -0700581 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700582 << sensorNameStr;
583 continue;
584 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800585
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530586 std::string labelPath;
587
Joseph Fub844e972023-03-25 10:24:30 +0800588 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530589 {
Joseph Fub844e972023-03-25 10:24:30 +0800590 /* find and differentiate _max and _input to replace "label" */
591 size_t pos = sensorPathStr.find('_');
592 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530593 {
Joseph Fub844e972023-03-25 10:24:30 +0800594 std::string sensorPathStrMax = sensorPathStr.substr(pos);
595 if (sensorPathStrMax == "_max")
596 {
597 labelPath = boost::replace_all_copy(sensorPathStr,
598 "max", "label");
599 maxLabel = true;
600 }
601 else
602 {
603 labelPath = boost::replace_all_copy(sensorPathStr,
604 "input", "label");
605 maxLabel = false;
606 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530607 }
608 else
609 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800610 continue;
611 }
612
Joseph Fub844e972023-03-25 10:24:30 +0800613 std::ifstream labelFile(labelPath);
614 if (!labelFile.good())
615 {
616 if constexpr (debug)
617 {
618 std::cerr << "Input file " << sensorPath
619 << " has no corresponding label file\n";
620 }
621 // hwmon *_input filename with number:
622 // temp1, temp2, temp3, ...
623 labelHead = sensorNameStr.substr(0,
624 sensorNameStr.find('_'));
625 }
626 else
627 {
628 std::string label;
629 std::getline(labelFile, label);
630 labelFile.close();
631 auto findSensor = sensors.find(label);
632 if (findSensor != sensors.end())
633 {
634 continue;
635 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800636
Joseph Fub844e972023-03-25 10:24:30 +0800637 // hwmon corresponding *_label file contents:
638 // vin1, vout1, ...
639 labelHead = label.substr(0, label.find(' '));
640 }
641
642 /* append "max" for labelMatch */
643 if (maxLabel)
644 {
645 labelHead.insert(0, "max");
646 }
647
648 checkPWMSensor(sensorPath, labelHead, *interfacePath,
649 dbusConnection, objectServer, psuNames[0]);
650 }
651 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530652 {
Joseph Fub844e972023-03-25 10:24:30 +0800653 auto findIIOHyphen = sensorNameStr.find_last_of('_');
654 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530655 }
656
Ed Tanous8a57ec02020-10-09 12:46:52 -0700657 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700658 {
659 std::cerr << "Sensor type=\"" << sensorNameSubStr
660 << "\" label=\"" << labelHead << "\"\n";
661 }
662
Vijay Khemka996bad12019-05-28 15:15:16 -0700663 if (!findLabels.empty())
664 {
665 /* Check if this labelHead is enabled in config file */
666 if (std::find(findLabels.begin(), findLabels.end(),
667 labelHead) == findLabels.end())
668 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700669 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800670 {
671 std::cerr << "could not find " << labelHead
672 << " in the Labels list\n";
673 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700674 continue;
675 }
676 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800677
Cheng C Yange50345b2019-04-02 17:26:15 +0800678 auto findProperty = labelMatch.find(labelHead);
679 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800680 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700681 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700682 {
683 std::cerr << "Could not find matching default property for "
684 << labelHead << "\n";
685 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800686 continue;
687 }
688
Josh Lehan74d9bd92019-10-31 08:51:58 -0700689 // Protect the hardcoded labelMatch list from changes,
690 // by making a copy and modifying that instead.
691 // Avoid bleedthrough of one device's customizations to
692 // the next device, as each should be independently customizable.
693 psuProperties.push_back(findProperty->second);
694 auto psuProperty = psuProperties.rbegin();
695
696 // Use label head as prefix for reading from config file,
697 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
698 std::string keyName = labelHead + "_Name";
699 std::string keyScale = labelHead + "_Scale";
700 std::string keyMin = labelHead + "_Min";
701 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800702 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800703 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700704
705 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700706 auto findCustomName = baseConfig->find(keyName);
707 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700708 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700709 try
710 {
711 psuProperty->labelTypeName = std::visit(
712 VariantToStringVisitor(), findCustomName->second);
713 }
Patrick Williams26601e82021-10-06 12:43:25 -0500714 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700715 {
716 std::cerr << "Unable to parse " << keyName << "\n";
717 continue;
718 }
719
720 // All strings are valid, including empty string
721 customizedName = true;
722 }
723
724 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700725 auto findCustomScale = baseConfig->find(keyScale);
726 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700727 {
728 try
729 {
730 psuProperty->sensorScaleFactor = std::visit(
731 VariantToUnsignedIntVisitor(), findCustomScale->second);
732 }
Patrick Williams26601e82021-10-06 12:43:25 -0500733 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700734 {
735 std::cerr << "Unable to parse " << keyScale << "\n";
736 continue;
737 }
738
739 // Avoid later division by zero
740 if (psuProperty->sensorScaleFactor > 0)
741 {
742 customizedScale = true;
743 }
744 else
745 {
746 std::cerr << "Unable to accept " << keyScale << "\n";
747 continue;
748 }
749 }
750
Zev Weisse8b97ee2022-08-12 15:23:51 -0700751 auto findCustomMin = baseConfig->find(keyMin);
752 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700753 {
754 try
755 {
756 psuProperty->minReading = std::visit(
757 VariantToDoubleVisitor(), findCustomMin->second);
758 }
Patrick Williams26601e82021-10-06 12:43:25 -0500759 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700760 {
761 std::cerr << "Unable to parse " << keyMin << "\n";
762 continue;
763 }
764 }
765
Zev Weisse8b97ee2022-08-12 15:23:51 -0700766 auto findCustomMax = baseConfig->find(keyMax);
767 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700768 {
769 try
770 {
771 psuProperty->maxReading = std::visit(
772 VariantToDoubleVisitor(), findCustomMax->second);
773 }
Patrick Williams26601e82021-10-06 12:43:25 -0500774 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700775 {
776 std::cerr << "Unable to parse " << keyMax << "\n";
777 continue;
778 }
779 }
780
Zev Weisse8b97ee2022-08-12 15:23:51 -0700781 auto findCustomOffset = baseConfig->find(keyOffset);
782 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800783 {
784 try
785 {
786 psuProperty->sensorOffset = std::visit(
787 VariantToDoubleVisitor(), findCustomOffset->second);
788 }
Patrick Williams26601e82021-10-06 12:43:25 -0500789 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800790 {
791 std::cerr << "Unable to parse " << keyOffset << "\n";
792 continue;
793 }
794 }
795
Lotus Xucb5af732021-09-10 15:18:50 +0800796 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700797 auto findPowerState = baseConfig->find(keyPowerState);
798 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800799 {
800 std::string powerState = std::visit(VariantToStringVisitor(),
801 findPowerState->second);
802 setReadState(powerState, readState);
803 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700804 if (!(psuProperty->minReading < psuProperty->maxReading))
805 {
806 std::cerr << "Min must be less than Max\n";
807 continue;
808 }
809
810 // If the sensor name is being customized by config file,
811 // then prefix/suffix composition becomes not necessary,
812 // and in fact not wanted, because it gets in the way.
813 std::string psuNameFromIndex;
814 if (!customizedName)
815 {
816 /* Find out sensor name index for this label */
817 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500818 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700819 if (std::regex_search(labelHead, matches, rgx))
820 {
821 nameIndex = std::stoi(matches[1]);
822
823 // Decrement to preserve alignment, because hwmon
824 // human-readable filenames and labels use 1-based
825 // numbering, but the "Name", "Name1", "Name2", etc. naming
826 // convention (the psuNames vector) uses 0-based numbering.
827 if (nameIndex > 0)
828 {
829 --nameIndex;
830 }
831 }
832 else
833 {
834 nameIndex = 0;
835 }
836
837 if (psuNames.size() <= nameIndex)
838 {
839 std::cerr << "Could not pair " << labelHead
840 << " with a Name field\n";
841 continue;
842 }
843
844 psuNameFromIndex = psuNames[nameIndex];
845
Ed Tanous8a57ec02020-10-09 12:46:52 -0700846 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700847 {
848 std::cerr << "Sensor label head " << labelHead
849 << " paired with " << psuNameFromIndex
850 << " at index " << nameIndex << "\n";
851 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700852 }
853
Joseph Fub844e972023-03-25 10:24:30 +0800854 if (devType == DevTypes::HWMON)
855 {
856 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
857 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800858
Josh Lehan74d9bd92019-10-31 08:51:58 -0700859 // Similarly, if sensor scaling factor is being customized,
860 // then the below power-of-10 constraint becomes unnecessary,
861 // as config should be able to specify an arbitrary divisor.
862 unsigned int factor = psuProperty->sensorScaleFactor;
863 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700864 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700865 // Preserve existing usage of hardcoded labelMatch table below
866 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700867
Josh Lehan74d9bd92019-10-31 08:51:58 -0700868 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700869 char firstChar =
870 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700871 std::string strScaleFactor =
872 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
873
874 // Preserve existing configs by accepting earlier syntax,
875 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700876 auto findScaleFactor = baseConfig->find(strScaleFactor);
877 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700878 {
879 factor = std::visit(VariantToIntVisitor(),
880 findScaleFactor->second);
881 }
882
Ed Tanous8a57ec02020-10-09 12:46:52 -0700883 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700884 {
885 std::cerr << "Sensor scaling factor " << factor
886 << " string " << strScaleFactor << "\n";
887 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700888 }
889
Vijay Khemka996bad12019-05-28 15:15:16 -0700890 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530891 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
892 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800893 {
James Feist17ab6e02019-06-25 12:28:13 -0700894 std::cerr << "error populating thresholds for "
895 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800896 }
897
Zev Weiss6b6891c2021-04-22 02:46:21 -0500898 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
899 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800900 {
Jason Ling5747fab2019-10-02 16:46:23 -0700901 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700902 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800903 continue;
904 }
905
Ed Tanous8a57ec02020-10-09 12:46:52 -0700906 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700907 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700908 std::cerr << "Sensor properties: Name \""
909 << psuProperty->labelTypeName << "\" Scale "
910 << psuProperty->sensorScaleFactor << " Min "
911 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800912 << psuProperty->maxReading << " Offset "
913 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700914 }
915
916 std::string sensorName = psuProperty->labelTypeName;
917 if (customizedName)
918 {
919 if (sensorName.empty())
920 {
921 // Allow selective disabling of an individual sensor,
922 // by customizing its name to an empty string.
923 std::cerr << "Sensor disabled, empty string\n";
924 continue;
925 }
926 }
927 else
928 {
929 // Sensor name not customized, do prefix/suffix composition,
930 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500931 sensorName = psuNameFromIndex + " " +
932 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700933 }
934
Ed Tanous8a57ec02020-10-09 12:46:52 -0700935 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700936 {
937 std::cerr << "Sensor name \"" << sensorName << "\" path \""
938 << sensorPathStr << "\" type \"" << sensorType
939 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700940 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800941 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800942
943 auto& sensor = sensors[sensorName];
944 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700945 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800946 sensor = nullptr;
947 }
948
949 if (sensor != nullptr)
950 {
951 sensor->activate(sensorPathStr, i2cDev);
952 }
953 else
954 {
955 sensors[sensorName] = std::make_shared<PSUSensor>(
956 sensorPathStr, sensorType, objectServer, dbusConnection, io,
957 sensorName, std::move(sensorThresholds), *interfacePath,
958 readState, findSensorUnit->second, factor,
959 psuProperty->maxReading, psuProperty->minReading,
960 psuProperty->sensorOffset, labelHead, thresholdConfSize,
961 pollRate, i2cDev);
962 sensors[sensorName]->setupRead();
963 ++numCreated;
964 if constexpr (debug)
965 {
966 std::cerr << "Created " << numCreated
967 << " sensors so far\n";
968 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700969 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800970 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800971
Joseph Fub844e972023-03-25 10:24:30 +0800972 if (devType == DevTypes::HWMON)
973 {
974 // OperationalStatus event
975 combineEvents[*psuName + "OperationalStatus"] = nullptr;
976 combineEvents[*psuName + "OperationalStatus"] =
977 std::make_unique<PSUCombineEvent>(
978 objectServer, dbusConnection, io, *psuName, readState,
979 eventPathList, groupEventPathList, "OperationalStatus",
980 pollRate);
981 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800982 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700983
Ed Tanous8a57ec02020-10-09 12:46:52 -0700984 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700985 {
986 std::cerr << "Created total of " << numCreated << " sensors\n";
987 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800988}
Cheng C Yang209ec562019-03-12 16:37:44 +0800989
Matt Simmering39fadb92023-05-04 21:47:29 -0700990static void
991 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
992{
993 static const int depth = 2;
994 static const int numKeys = 1;
995 GetSubTreeType cpuSubTree;
996
997 try
998 {
999 auto getItems = dbusConnection->new_method_call(
1000 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
1001 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
1002 std::array<const char*, numKeys>{
1003 "xyz.openbmc_project.Inventory.Item"});
1004 auto getItemsResp = dbusConnection->call(getItems);
1005 getItemsResp.read(cpuSubTree);
1006 }
1007 catch (sdbusplus::exception_t& e)
1008 {
1009 std::cerr << "error getting inventory item subtree: " << e.what()
1010 << "\n";
1011 return;
1012 }
1013
1014 for (const auto& [path, objDict] : cpuSubTree)
1015 {
1016 auto obj = sdbusplus::message::object_path(path).filename();
1017 if (!obj.starts_with("cpu") || objDict.empty())
1018 {
1019 continue;
1020 }
1021 const std::string& owner = objDict.begin()->first;
1022
1023 std::variant<bool> respValue;
1024 try
1025 {
1026 auto getPresence = dbusConnection->new_method_call(
1027 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1028 "Get");
1029 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1030 auto resp = dbusConnection->call(getPresence);
1031 resp.read(respValue);
1032 }
1033 catch (sdbusplus::exception_t& e)
1034 {
1035 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1036 continue;
1037 }
1038
1039 auto* present = std::get_if<bool>(&respValue);
1040 if (present != nullptr && *present)
1041 {
1042 int cpuIndex = 0;
1043 try
1044 {
1045 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1046 }
1047 catch (const std::exception& e)
1048 {
1049 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1050 continue;
1051 }
1052 cpuPresence[cpuIndex + 1] = *present;
1053 }
1054 }
1055}
1056
Zhikui Ren23c96e72020-11-05 22:32:28 -08001057void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001058 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001059 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1060 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001061 sensorsChanged,
1062 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001063{
1064 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001065 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1066 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001067 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1068 sensorsChanged, activateOnly);
1069 });
Matt Simmering786efb82023-01-18 14:09:21 -08001070 std::vector<std::string> types(sensorTypes.size());
1071 for (const auto& [type, dt] : sensorTypes)
1072 {
1073 types.push_back(type);
1074 }
1075 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001076}
1077
Cheng C Yang916360b2019-05-07 18:47:16 +08001078void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001079{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001080 sensorTable = {{"power", sensor_paths::unitWatts},
1081 {"curr", sensor_paths::unitAmperes},
1082 {"temp", sensor_paths::unitDegreesC},
1083 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001084 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001085 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001086
Jeff Line41d52f2021-04-07 19:38:51 +08001087 labelMatch = {
1088 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001089 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1090 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001091 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1092 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1093 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1094 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001095 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1096 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1097 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001098 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1099 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001100 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1101 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001102 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001103 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001107 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1117 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1118 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1119 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1125 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1126 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1127 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1128 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1129 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1130 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1131 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1132 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1133 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1134 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1135 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1136 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1137 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1138 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1139 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001140 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001141 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001142 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1143 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1144 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1145 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1146 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1147 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001148 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001149 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1150 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001151 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1152 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1153 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1154 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1155 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1156 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1157 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1158 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1159 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1160 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1161 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1162 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1163 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1164 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1165 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001166 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1167 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1168 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001169 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1170 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1171 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1172 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1173 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1174 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1175 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1176 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1177 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001178 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1179 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1180 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001181
Matt Simmering6747eba2023-01-18 16:03:55 -08001182 pwmTable = {{"fan1", "Fan_1"},
1183 {"fan2", "Fan_2"},
1184 {"fan3", "Fan_3"},
1185 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001186
1187 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1188 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1189
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001190 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1191 {"Failure", {"in2_alarm"}},
1192 {"ACLost", {"in1_beep"}},
1193 {"ConfigureError", {"in1_fault"}}};
1194
1195 groupEventMatch = {{"FanFault",
1196 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001197 {"fan2", {"fan2_alarm", "fan2_fault"}},
1198 {"fan3", {"fan3_alarm", "fan3_fault"}},
1199 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001200
1201 devParamMap = {
1202 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1203 {DevTypes::IIO,
1204 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001205}
1206
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001207static void powerStateChanged(
1208 PowerState type, bool newState,
1209 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1210 sensors,
1211 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1212 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1213{
1214 if (newState)
1215 {
1216 createSensors(io, objectServer, dbusConnection, nullptr, true);
1217 }
1218 else
1219 {
1220 for (auto& [path, sensor] : sensors)
1221 {
1222 if (sensor != nullptr && sensor->readState == type)
1223 {
1224 sensor->deactivate();
1225 }
1226 }
1227 }
1228}
1229
James Feistb6c0b912019-07-09 12:21:44 -07001230int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001231{
Ed Tanous1f978632023-02-28 18:16:39 -08001232 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001233 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1234
Ed Tanous14ed5e92022-07-12 15:50:23 -07001235 sdbusplus::asio::object_server objectServer(systemBus, true);
1236 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001237 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001238 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001239 auto sensorsChanged =
1240 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001241
Cheng C Yang916360b2019-05-07 18:47:16 +08001242 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001243
Patrick Williams597e8422023-10-20 11:19:01 -05001244 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1245 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001246 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1247 };
1248
1249 setupPowerMatchCallback(systemBus, powerCallBack);
1250
1251 boost::asio::post(io, [&]() {
1252 createSensors(io, objectServer, systemBus, nullptr, false);
1253 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001254 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001255 std::function<void(sdbusplus::message_t&)> eventHandler =
1256 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001257 if (message.is_method_error())
1258 {
1259 std::cerr << "callback method error\n";
1260 return;
1261 }
1262 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001263 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001264 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1265 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001266 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001267 return;
1268 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001269 if (ec)
1270 {
1271 std::cerr << "timer error\n";
1272 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001273 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001274 });
1275 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001276
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001277 boost::asio::steady_timer cpuFilterTimer(io);
1278 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1279 [&](sdbusplus::message_t& message) {
1280 std::string path = message.get_path();
1281 boost::to_lower(path);
1282
1283 sdbusplus::message::object_path cpuPath(path);
1284 std::string cpuName = cpuPath.filename();
1285 if (!cpuName.starts_with("cpu"))
1286 {
1287 return;
1288 }
1289 size_t index = 0;
1290 try
1291 {
1292 index = std::stoi(path.substr(path.size() - 1));
1293 }
1294 catch (const std::invalid_argument&)
1295 {
1296 std::cerr << "Found invalid path " << path << "\n";
1297 return;
1298 }
1299
1300 std::string objectName;
1301 boost::container::flat_map<std::string, std::variant<bool>> values;
1302 message.read(objectName, values);
1303 auto findPresence = values.find("Present");
1304 try
1305 {
1306 cpuPresence[index] = std::get<bool>(findPresence->second);
1307 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001308 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001309 {
1310 return;
1311 }
1312
Chris Sides3fce1bf2023-04-03 16:57:05 -05001313 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001314 {
1315 return;
1316 }
1317 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1318 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1319 if (ec == boost::asio::error::operation_aborted)
1320 {
1321 return;
1322 }
1323 if (ec)
1324 {
1325 std::cerr << "timer error\n";
1326 return;
1327 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001328 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001329 });
1330 };
1331
Zev Weiss214d9712022-08-12 12:54:31 -07001332 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1333 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001334
1335 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1336 static_cast<sdbusplus::bus_t&>(*systemBus),
1337 "type='signal',member='PropertiesChanged',path_namespace='" +
1338 std::string(cpuInventoryPath) +
1339 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1340 cpuPresenceHandler));
1341
Matt Simmering39fadb92023-05-04 21:47:29 -07001342 getPresentCpus(systemBus);
1343
Bruce Lee1263c3d2021-06-04 15:16:33 +08001344 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001345 io.run();
1346}