blob: cf98f9b4d96d7b16bfd25ab781fc33e1e66b5313 [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;
George Liu5b3542e2023-10-31 17:27:12 +0800134static EventPathList eventMatch;
135static GroupEventPathList groupEventMatch;
136static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800137
Josh Lehan74d9bd92019-10-31 08:51:58 -0700138static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800139static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800140static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700141
Cheng C Yang58b2b532019-05-31 00:19:45 +0800142// Function CheckEvent will check each attribute from eventMatch table in the
143// sysfs. If the attributes exists in sysfs, then store the complete path
144// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800145void checkEvent(const std::string& directory, const EventPathList& eventMatch,
146 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800147{
148 for (const auto& match : eventMatch)
149 {
150 const std::vector<std::string>& eventAttrs = match.second;
151 const std::string& eventName = match.first;
152 for (const auto& eventAttr : eventAttrs)
153 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700154 std::string eventPath = directory;
155 eventPath += "/";
156 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800157
158 std::ifstream eventFile(eventPath);
159 if (!eventFile.good())
160 {
161 continue;
162 }
163
164 eventPathList[eventName].push_back(eventPath);
165 }
166 }
167}
168
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800169// Check Group Events which contains more than one targets in each combine
170// events.
George Liu5b3542e2023-10-31 17:27:12 +0800171void checkGroupEvent(const std::string& directory,
172 const GroupEventPathList& groupEventMatch,
173 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800174{
175 for (const auto& match : groupEventMatch)
176 {
177 const std::string& groupEventName = match.first;
George Liu5b3542e2023-10-31 17:27:12 +0800178 const EventPathList events = match.second;
179 EventPathList pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800180 for (const auto& match : events)
181 {
182 const std::string& eventName = match.first;
183 const std::vector<std::string>& eventAttrs = match.second;
184 for (const auto& eventAttr : eventAttrs)
185 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700186 std::string eventPath = directory;
187 eventPath += "/";
188 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800189 std::ifstream eventFile(eventPath);
190 if (!eventFile.good())
191 {
192 continue;
193 }
194
195 pathList[eventName].push_back(eventPath);
196 }
197 }
198 groupEventPathList[groupEventName] = pathList;
199 }
200}
201
Cheng C Yang58b2b532019-05-31 00:19:45 +0800202// Function checkEventLimits will check all the psu related xxx_input attributes
203// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
204// xxx_min_alarm exist, then store the existing paths of the alarm attributes
205// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800206void checkEventLimits(const std::string& sensorPathStr,
207 const EventPathList& limitEventMatch,
208 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800209{
Lei YUa2c7cea2020-12-23 14:07:28 +0800210 auto attributePartPos = sensorPathStr.find_last_of('_');
211 if (attributePartPos == std::string::npos)
212 {
213 // There is no '_' in the string, skip it
214 return;
215 }
216 auto attributePart =
217 std::string_view(sensorPathStr).substr(attributePartPos + 1);
218 if (attributePart != "input")
219 {
220 // If the sensor is not xxx_input, skip it
221 return;
222 }
223
224 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800225 for (const auto& limitMatch : limitEventMatch)
226 {
227 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
228 const std::string& eventName = limitMatch.first;
229 for (const auto& limitEventAttr : limitEventAttrs)
230 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800231 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800232 std::ifstream eventFile(limitEventPath);
233 if (!eventFile.good())
234 {
235 continue;
236 }
237 eventPathList[eventName].push_back(limitEventPath);
238 }
239 }
240}
Cheng C Yang916360b2019-05-07 18:47:16 +0800241
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530242static void
243 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
244 const std::string& interfacePath,
245 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
246 sdbusplus::asio::object_server& objectServer,
247 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800248{
Zev Weiss741f26e2022-08-12 18:21:02 -0700249 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800250 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700251 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800252 {
253 continue;
254 }
255
256 const std::string& sensorPathStr = sensorPath.string();
257 const std::string& pwmPathStr =
258 boost::replace_all_copy(sensorPathStr, "input", "target");
259 std::ifstream pwmFile(pwmPathStr);
260 if (!pwmFile.good())
261 {
262 continue;
263 }
264
265 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
266 if (findPWMSensor != pwmSensors.end())
267 {
268 continue;
269 }
270
Zev Weissd8c293a2022-08-15 18:58:41 -0700271 std::string name = "Pwm_";
272 name += psuName;
273 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700274 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700275
276 std::string objPath = interfacePath;
277 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700278 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700279
Cheng C Yang916360b2019-05-07 18:47:16 +0800280 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700281 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800282 }
283}
284
Zhikui Ren23c96e72020-11-05 22:32:28 -0800285static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800286 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800287 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
288 const ManagedObjectType& sensorConfigs,
289 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800290 sensorsChanged,
291 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800292{
Josh Lehan49cfba92019-10-08 16:50:42 -0700293 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800294 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800295
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800296 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
297
Cheng C Yang209ec562019-03-12 16:37:44 +0800298 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800299 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
300 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
301 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800302 {
303 std::cerr << "No PSU sensors in system\n";
304 return;
305 }
306
307 boost::container::flat_set<std::string> directories;
308 for (const auto& pmbusPath : pmbusPaths)
309 {
George Liu5b3542e2023-10-31 17:27:12 +0800310 EventPathList eventPathList;
311 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800312
313 std::ifstream nameFile(pmbusPath);
314 if (!nameFile.good())
315 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700316 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800317 continue;
318 }
319
320 std::string pmbusName;
321 std::getline(nameFile, pmbusName);
322 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700323
Matt Simmering786efb82023-01-18 14:09:21 -0800324 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800325 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700326 // To avoid this error message, add your driver name to
327 // the pmbusNames vector at the top of this file.
328 std::cerr << "Driver name " << pmbusName
329 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800330 continue;
331 }
332
Cheng C Yang209ec562019-03-12 16:37:44 +0800333 auto directory = pmbusPath.parent_path();
334
335 auto ret = directories.insert(directory.string());
336 if (!ret.second)
337 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700338 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800339 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800340 }
341
Joseph Fub844e972023-03-25 10:24:30 +0800342 DevTypes devType = DevTypes::HWMON;
343 std::string deviceName;
344 if (directory.parent_path() == "/sys/class/hwmon")
345 {
346 deviceName = fs::canonical(directory / "device").stem();
347 }
348 else
349 {
350 deviceName = fs::canonical(directory).parent_path().stem();
351 devType = DevTypes::IIO;
352 }
353
Cheng C Yang209ec562019-03-12 16:37:44 +0800354 size_t bus = 0;
355 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000356 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800357 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 continue;
359 }
360
Zev Weisse8b97ee2022-08-12 15:23:51 -0700361 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 const SensorData* sensorData = nullptr;
363 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800364 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800365 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800366
Zev Weiss741f26e2022-08-12 18:21:02 -0700367 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700369 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800370 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800371 {
Zev Weiss054aad82022-08-18 01:37:34 -0700372 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800373 if (sensorBase != sensorData->end())
374 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700375 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 sensorType = type;
377 break;
378 }
379 }
380 if (baseConfig == nullptr)
381 {
382 std::cerr << "error finding base configuration for "
383 << deviceName << "\n";
384 continue;
385 }
386
Zev Weisse8b97ee2022-08-12 15:23:51 -0700387 auto configBus = baseConfig->find("Bus");
388 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800389
Zev Weisse8b97ee2022-08-12 15:23:51 -0700390 if (configBus == baseConfig->end() ||
391 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800392 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800393 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 continue;
395 }
396
Matt Simmering6747eba2023-01-18 16:03:55 -0800397 const uint64_t* confBus =
398 std::get_if<uint64_t>(&(configBus->second));
399 const uint64_t* confAddr =
400 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800401 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800402 {
403 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700404 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800405 continue;
406 }
407
408 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800409 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200410 if constexpr (debug)
411 {
412 std::cerr << "Configuration skipping " << *confBus << "-"
413 << *confAddr << " because not " << bus << "-"
414 << addr << "\n";
415 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 continue;
417 }
418
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800419 std::vector<thresholds::Threshold> confThresholds;
420 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
421 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800422 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800423 }
424 thresholdConfSize = confThresholds.size();
425
Zev Weiss741f26e2022-08-12 18:21:02 -0700426 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800427 break;
428 }
429 if (interfacePath == nullptr)
430 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700431 // To avoid this error message, add your export map entry,
432 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800433 std::cerr << "failed to find match for " << deviceName << "\n";
434 continue;
435 }
436
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800437 auto findI2CDev = devices.find(*interfacePath);
438
439 std::shared_ptr<I2CDevice> i2cDev;
440 if (findI2CDev != devices.end())
441 {
442 if (activateOnly && !findI2CDev->second.second)
443 {
444 continue;
445 }
446 i2cDev = findI2CDev->second.first;
447 }
448
Zev Weisse8b97ee2022-08-12 15:23:51 -0700449 auto findPSUName = baseConfig->find("Name");
450 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800451 {
452 std::cerr << "could not determine configuration name for "
453 << deviceName << "\n";
454 continue;
455 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800456 const std::string* psuName =
457 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800458 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800459 {
460 std::cerr << "Cannot find psu name, invalid configuration\n";
461 continue;
462 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800463
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800464 auto findCPU = baseConfig->find("CPURequired");
465 if (findCPU != baseConfig->end())
466 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500467 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800468 auto presenceFind = cpuPresence.find(index);
469 if (presenceFind == cpuPresence.end() || !presenceFind->second)
470 {
471 continue;
472 }
473 }
474
Zhikui Ren23c96e72020-11-05 22:32:28 -0800475 // on rescans, only update sensors we were signaled by
476 if (!firstScan)
477 {
Zhikui Renda98f092021-11-01 09:41:08 -0700478 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500479 auto it = std::find_if(sensorsChanged->begin(),
480 sensorsChanged->end(),
481 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800482 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500483 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800484
485 if (it == sensorsChanged->end())
486 {
487 continue;
488 }
489 sensorsChanged->erase(it);
490 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800491 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800492 checkGroupEvent(directory.string(), groupEventMatch,
493 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800494
Zev Weisse8b97ee2022-08-12 15:23:51 -0700495 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000496
Vijay Khemka996bad12019-05-28 15:15:16 -0700497 /* Check if there are more sensors in the same interface */
498 int i = 1;
499 std::vector<std::string> psuNames;
500 do
501 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700502 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700503 psuNames.push_back(
504 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700505 findPSUName = baseConfig->find("Name" + std::to_string(i++));
506 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700507
Cheng C Yange50345b2019-04-02 17:26:15 +0800508 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800509 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
510 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800511 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800512 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800513 continue;
514 }
515
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530516 /* read max value in sysfs for in, curr, power, temp, ... */
517 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
518 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700519 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530520 {
521 std::cerr << "No max name in PSU \n";
522 }
523 }
524
Zev Weiss8569bf22022-10-11 15:37:44 -0700525 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800526
Vijay Khemka996bad12019-05-28 15:15:16 -0700527 /* Find array of labels to be exposed if it is defined in config */
528 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700529 auto findLabelObj = baseConfig->find("Labels");
530 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700531 {
532 findLabels =
533 std::get<std::vector<std::string>>(findLabelObj->second);
534 }
535
Joseph Fub844e972023-03-25 10:24:30 +0800536 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700537 std::smatch matches;
538
Cheng C Yange50345b2019-04-02 17:26:15 +0800539 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800540 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530541 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800542 std::string labelHead;
543 std::string sensorPathStr = sensorPath.string();
544 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700545 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700546 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
547 {
Josh Lehan06494452019-10-31 09:49:16 -0700548 // hwmon *_input filename without number:
549 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800550 // iio in_*_raw filename without number:
551 // voltage, temp, pressure, ...
552 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700553 }
554 else
555 {
Josh Lehan06494452019-10-31 09:49:16 -0700556 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700557 << sensorNameStr;
558 continue;
559 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800560
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530561 std::string labelPath;
562
Joseph Fub844e972023-03-25 10:24:30 +0800563 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530564 {
Joseph Fub844e972023-03-25 10:24:30 +0800565 /* find and differentiate _max and _input to replace "label" */
566 size_t pos = sensorPathStr.find('_');
567 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530568 {
Joseph Fub844e972023-03-25 10:24:30 +0800569 std::string sensorPathStrMax = sensorPathStr.substr(pos);
570 if (sensorPathStrMax == "_max")
571 {
572 labelPath = boost::replace_all_copy(sensorPathStr,
573 "max", "label");
574 maxLabel = true;
575 }
576 else
577 {
578 labelPath = boost::replace_all_copy(sensorPathStr,
579 "input", "label");
580 maxLabel = false;
581 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530582 }
583 else
584 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800585 continue;
586 }
587
Joseph Fub844e972023-03-25 10:24:30 +0800588 std::ifstream labelFile(labelPath);
589 if (!labelFile.good())
590 {
591 if constexpr (debug)
592 {
593 std::cerr << "Input file " << sensorPath
594 << " has no corresponding label file\n";
595 }
596 // hwmon *_input filename with number:
597 // temp1, temp2, temp3, ...
598 labelHead = sensorNameStr.substr(0,
599 sensorNameStr.find('_'));
600 }
601 else
602 {
603 std::string label;
604 std::getline(labelFile, label);
605 labelFile.close();
606 auto findSensor = sensors.find(label);
607 if (findSensor != sensors.end())
608 {
609 continue;
610 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800611
Joseph Fub844e972023-03-25 10:24:30 +0800612 // hwmon corresponding *_label file contents:
613 // vin1, vout1, ...
614 labelHead = label.substr(0, label.find(' '));
615 }
616
617 /* append "max" for labelMatch */
618 if (maxLabel)
619 {
620 labelHead.insert(0, "max");
621 }
622
623 checkPWMSensor(sensorPath, labelHead, *interfacePath,
624 dbusConnection, objectServer, psuNames[0]);
625 }
626 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530627 {
Joseph Fub844e972023-03-25 10:24:30 +0800628 auto findIIOHyphen = sensorNameStr.find_last_of('_');
629 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530630 }
631
Ed Tanous8a57ec02020-10-09 12:46:52 -0700632 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700633 {
634 std::cerr << "Sensor type=\"" << sensorNameSubStr
635 << "\" label=\"" << labelHead << "\"\n";
636 }
637
Vijay Khemka996bad12019-05-28 15:15:16 -0700638 if (!findLabels.empty())
639 {
640 /* Check if this labelHead is enabled in config file */
641 if (std::find(findLabels.begin(), findLabels.end(),
642 labelHead) == findLabels.end())
643 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700644 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800645 {
646 std::cerr << "could not find " << labelHead
647 << " in the Labels list\n";
648 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700649 continue;
650 }
651 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800652
Cheng C Yange50345b2019-04-02 17:26:15 +0800653 auto findProperty = labelMatch.find(labelHead);
654 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800655 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700656 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700657 {
658 std::cerr << "Could not find matching default property for "
659 << labelHead << "\n";
660 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800661 continue;
662 }
663
Josh Lehan74d9bd92019-10-31 08:51:58 -0700664 // Protect the hardcoded labelMatch list from changes,
665 // by making a copy and modifying that instead.
666 // Avoid bleedthrough of one device's customizations to
667 // the next device, as each should be independently customizable.
668 psuProperties.push_back(findProperty->second);
669 auto psuProperty = psuProperties.rbegin();
670
671 // Use label head as prefix for reading from config file,
672 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
673 std::string keyName = labelHead + "_Name";
674 std::string keyScale = labelHead + "_Scale";
675 std::string keyMin = labelHead + "_Min";
676 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800677 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800678 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700679
680 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700681 auto findCustomName = baseConfig->find(keyName);
682 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700683 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700684 try
685 {
686 psuProperty->labelTypeName = std::visit(
687 VariantToStringVisitor(), findCustomName->second);
688 }
Patrick Williams26601e82021-10-06 12:43:25 -0500689 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700690 {
691 std::cerr << "Unable to parse " << keyName << "\n";
692 continue;
693 }
694
695 // All strings are valid, including empty string
696 customizedName = true;
697 }
698
699 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700700 auto findCustomScale = baseConfig->find(keyScale);
701 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700702 {
703 try
704 {
705 psuProperty->sensorScaleFactor = std::visit(
706 VariantToUnsignedIntVisitor(), findCustomScale->second);
707 }
Patrick Williams26601e82021-10-06 12:43:25 -0500708 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700709 {
710 std::cerr << "Unable to parse " << keyScale << "\n";
711 continue;
712 }
713
714 // Avoid later division by zero
715 if (psuProperty->sensorScaleFactor > 0)
716 {
717 customizedScale = true;
718 }
719 else
720 {
721 std::cerr << "Unable to accept " << keyScale << "\n";
722 continue;
723 }
724 }
725
Zev Weisse8b97ee2022-08-12 15:23:51 -0700726 auto findCustomMin = baseConfig->find(keyMin);
727 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700728 {
729 try
730 {
731 psuProperty->minReading = std::visit(
732 VariantToDoubleVisitor(), findCustomMin->second);
733 }
Patrick Williams26601e82021-10-06 12:43:25 -0500734 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700735 {
736 std::cerr << "Unable to parse " << keyMin << "\n";
737 continue;
738 }
739 }
740
Zev Weisse8b97ee2022-08-12 15:23:51 -0700741 auto findCustomMax = baseConfig->find(keyMax);
742 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700743 {
744 try
745 {
746 psuProperty->maxReading = std::visit(
747 VariantToDoubleVisitor(), findCustomMax->second);
748 }
Patrick Williams26601e82021-10-06 12:43:25 -0500749 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700750 {
751 std::cerr << "Unable to parse " << keyMax << "\n";
752 continue;
753 }
754 }
755
Zev Weisse8b97ee2022-08-12 15:23:51 -0700756 auto findCustomOffset = baseConfig->find(keyOffset);
757 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800758 {
759 try
760 {
761 psuProperty->sensorOffset = std::visit(
762 VariantToDoubleVisitor(), findCustomOffset->second);
763 }
Patrick Williams26601e82021-10-06 12:43:25 -0500764 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800765 {
766 std::cerr << "Unable to parse " << keyOffset << "\n";
767 continue;
768 }
769 }
770
Lotus Xucb5af732021-09-10 15:18:50 +0800771 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700772 auto findPowerState = baseConfig->find(keyPowerState);
773 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800774 {
775 std::string powerState = std::visit(VariantToStringVisitor(),
776 findPowerState->second);
777 setReadState(powerState, readState);
778 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700779 if (!(psuProperty->minReading < psuProperty->maxReading))
780 {
781 std::cerr << "Min must be less than Max\n";
782 continue;
783 }
784
785 // If the sensor name is being customized by config file,
786 // then prefix/suffix composition becomes not necessary,
787 // and in fact not wanted, because it gets in the way.
788 std::string psuNameFromIndex;
789 if (!customizedName)
790 {
791 /* Find out sensor name index for this label */
792 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500793 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700794 if (std::regex_search(labelHead, matches, rgx))
795 {
796 nameIndex = std::stoi(matches[1]);
797
798 // Decrement to preserve alignment, because hwmon
799 // human-readable filenames and labels use 1-based
800 // numbering, but the "Name", "Name1", "Name2", etc. naming
801 // convention (the psuNames vector) uses 0-based numbering.
802 if (nameIndex > 0)
803 {
804 --nameIndex;
805 }
806 }
807 else
808 {
809 nameIndex = 0;
810 }
811
812 if (psuNames.size() <= nameIndex)
813 {
814 std::cerr << "Could not pair " << labelHead
815 << " with a Name field\n";
816 continue;
817 }
818
819 psuNameFromIndex = psuNames[nameIndex];
820
Ed Tanous8a57ec02020-10-09 12:46:52 -0700821 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700822 {
823 std::cerr << "Sensor label head " << labelHead
824 << " paired with " << psuNameFromIndex
825 << " at index " << nameIndex << "\n";
826 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700827 }
828
Joseph Fub844e972023-03-25 10:24:30 +0800829 if (devType == DevTypes::HWMON)
830 {
831 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
832 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800833
Josh Lehan74d9bd92019-10-31 08:51:58 -0700834 // Similarly, if sensor scaling factor is being customized,
835 // then the below power-of-10 constraint becomes unnecessary,
836 // as config should be able to specify an arbitrary divisor.
837 unsigned int factor = psuProperty->sensorScaleFactor;
838 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700839 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700840 // Preserve existing usage of hardcoded labelMatch table below
841 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700842
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700844 char firstChar =
845 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700846 std::string strScaleFactor =
847 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
848
849 // Preserve existing configs by accepting earlier syntax,
850 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700851 auto findScaleFactor = baseConfig->find(strScaleFactor);
852 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700853 {
854 factor = std::visit(VariantToIntVisitor(),
855 findScaleFactor->second);
856 }
857
Ed Tanous8a57ec02020-10-09 12:46:52 -0700858 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700859 {
860 std::cerr << "Sensor scaling factor " << factor
861 << " string " << strScaleFactor << "\n";
862 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700863 }
864
Vijay Khemka996bad12019-05-28 15:15:16 -0700865 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530866 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
867 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800868 {
James Feist17ab6e02019-06-25 12:28:13 -0700869 std::cerr << "error populating thresholds for "
870 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800871 }
872
Zev Weiss6b6891c2021-04-22 02:46:21 -0500873 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
874 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800875 {
Jason Ling5747fab2019-10-02 16:46:23 -0700876 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700877 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800878 continue;
879 }
880
Ed Tanous8a57ec02020-10-09 12:46:52 -0700881 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700882 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700883 std::cerr << "Sensor properties: Name \""
884 << psuProperty->labelTypeName << "\" Scale "
885 << psuProperty->sensorScaleFactor << " Min "
886 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800887 << psuProperty->maxReading << " Offset "
888 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700889 }
890
891 std::string sensorName = psuProperty->labelTypeName;
892 if (customizedName)
893 {
894 if (sensorName.empty())
895 {
896 // Allow selective disabling of an individual sensor,
897 // by customizing its name to an empty string.
898 std::cerr << "Sensor disabled, empty string\n";
899 continue;
900 }
901 }
902 else
903 {
904 // Sensor name not customized, do prefix/suffix composition,
905 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500906 sensorName = psuNameFromIndex + " " +
907 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700908 }
909
Ed Tanous8a57ec02020-10-09 12:46:52 -0700910 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700911 {
912 std::cerr << "Sensor name \"" << sensorName << "\" path \""
913 << sensorPathStr << "\" type \"" << sensorType
914 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700915 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800916 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800917
918 auto& sensor = sensors[sensorName];
919 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700920 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800921 sensor = nullptr;
922 }
923
924 if (sensor != nullptr)
925 {
926 sensor->activate(sensorPathStr, i2cDev);
927 }
928 else
929 {
930 sensors[sensorName] = std::make_shared<PSUSensor>(
931 sensorPathStr, sensorType, objectServer, dbusConnection, io,
932 sensorName, std::move(sensorThresholds), *interfacePath,
933 readState, findSensorUnit->second, factor,
934 psuProperty->maxReading, psuProperty->minReading,
935 psuProperty->sensorOffset, labelHead, thresholdConfSize,
936 pollRate, i2cDev);
937 sensors[sensorName]->setupRead();
938 ++numCreated;
939 if constexpr (debug)
940 {
941 std::cerr << "Created " << numCreated
942 << " sensors so far\n";
943 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700944 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800945 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800946
Joseph Fub844e972023-03-25 10:24:30 +0800947 if (devType == DevTypes::HWMON)
948 {
949 // OperationalStatus event
950 combineEvents[*psuName + "OperationalStatus"] = nullptr;
951 combineEvents[*psuName + "OperationalStatus"] =
952 std::make_unique<PSUCombineEvent>(
953 objectServer, dbusConnection, io, *psuName, readState,
954 eventPathList, groupEventPathList, "OperationalStatus",
955 pollRate);
956 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800957 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700958
Ed Tanous8a57ec02020-10-09 12:46:52 -0700959 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700960 {
961 std::cerr << "Created total of " << numCreated << " sensors\n";
962 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800963}
Cheng C Yang209ec562019-03-12 16:37:44 +0800964
Matt Simmering39fadb92023-05-04 21:47:29 -0700965static void
966 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
967{
968 static const int depth = 2;
969 static const int numKeys = 1;
970 GetSubTreeType cpuSubTree;
971
972 try
973 {
974 auto getItems = dbusConnection->new_method_call(
975 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
976 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
977 std::array<const char*, numKeys>{
978 "xyz.openbmc_project.Inventory.Item"});
979 auto getItemsResp = dbusConnection->call(getItems);
980 getItemsResp.read(cpuSubTree);
981 }
982 catch (sdbusplus::exception_t& e)
983 {
984 std::cerr << "error getting inventory item subtree: " << e.what()
985 << "\n";
986 return;
987 }
988
989 for (const auto& [path, objDict] : cpuSubTree)
990 {
991 auto obj = sdbusplus::message::object_path(path).filename();
992 if (!obj.starts_with("cpu") || objDict.empty())
993 {
994 continue;
995 }
996 const std::string& owner = objDict.begin()->first;
997
998 std::variant<bool> respValue;
999 try
1000 {
1001 auto getPresence = dbusConnection->new_method_call(
1002 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1003 "Get");
1004 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1005 auto resp = dbusConnection->call(getPresence);
1006 resp.read(respValue);
1007 }
1008 catch (sdbusplus::exception_t& e)
1009 {
1010 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1011 continue;
1012 }
1013
1014 auto* present = std::get_if<bool>(&respValue);
1015 if (present != nullptr && *present)
1016 {
1017 int cpuIndex = 0;
1018 try
1019 {
1020 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1021 }
1022 catch (const std::exception& e)
1023 {
1024 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1025 continue;
1026 }
1027 cpuPresence[cpuIndex + 1] = *present;
1028 }
1029 }
1030}
1031
Zhikui Ren23c96e72020-11-05 22:32:28 -08001032void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001033 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001034 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1035 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001036 sensorsChanged,
1037 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001038{
1039 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001040 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1041 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001042 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1043 sensorsChanged, activateOnly);
1044 });
Matt Simmering786efb82023-01-18 14:09:21 -08001045 std::vector<std::string> types(sensorTypes.size());
1046 for (const auto& [type, dt] : sensorTypes)
1047 {
1048 types.push_back(type);
1049 }
1050 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001051}
1052
Cheng C Yang916360b2019-05-07 18:47:16 +08001053void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001054{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001055 sensorTable = {{"power", sensor_paths::unitWatts},
1056 {"curr", sensor_paths::unitAmperes},
1057 {"temp", sensor_paths::unitDegreesC},
1058 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001059 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001060 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001061
Jeff Line41d52f2021-04-07 19:38:51 +08001062 labelMatch = {
1063 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001064 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1065 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001066 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1067 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1068 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1069 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001070 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1071 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1072 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001073 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1074 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001075 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1076 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001077 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001078 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1079 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1080 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1081 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001082 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1083 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1084 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1085 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1086 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1087 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1088 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1089 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1090 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1091 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1092 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1093 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1094 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1095 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1096 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1097 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1098 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1099 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1100 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1101 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1102 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1103 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001115 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001116 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001117 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1118 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1119 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001123 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001124 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1125 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001126 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1127 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1128 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1129 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1130 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1131 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1132 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1133 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1134 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1135 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1136 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1137 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1138 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1139 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1140 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001141 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1142 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1143 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001144 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1145 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1146 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1147 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1148 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1149 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1150 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1151 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1152 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001153 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1154 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1155 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001156
Matt Simmering6747eba2023-01-18 16:03:55 -08001157 pwmTable = {{"fan1", "Fan_1"},
1158 {"fan2", "Fan_2"},
1159 {"fan3", "Fan_3"},
1160 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001161
1162 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1163 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1164
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001165 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1166 {"Failure", {"in2_alarm"}},
1167 {"ACLost", {"in1_beep"}},
1168 {"ConfigureError", {"in1_fault"}}};
1169
1170 groupEventMatch = {{"FanFault",
1171 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001172 {"fan2", {"fan2_alarm", "fan2_fault"}},
1173 {"fan3", {"fan3_alarm", "fan3_fault"}},
1174 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001175
1176 devParamMap = {
1177 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1178 {DevTypes::IIO,
1179 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001180}
1181
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001182static void powerStateChanged(
1183 PowerState type, bool newState,
1184 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1185 sensors,
1186 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1187 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1188{
1189 if (newState)
1190 {
1191 createSensors(io, objectServer, dbusConnection, nullptr, true);
1192 }
1193 else
1194 {
1195 for (auto& [path, sensor] : sensors)
1196 {
1197 if (sensor != nullptr && sensor->readState == type)
1198 {
1199 sensor->deactivate();
1200 }
1201 }
1202 }
1203}
1204
James Feistb6c0b912019-07-09 12:21:44 -07001205int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001206{
Ed Tanous1f978632023-02-28 18:16:39 -08001207 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001208 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1209
Ed Tanous14ed5e92022-07-12 15:50:23 -07001210 sdbusplus::asio::object_server objectServer(systemBus, true);
1211 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001212 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001213 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001214 auto sensorsChanged =
1215 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001216
Cheng C Yang916360b2019-05-07 18:47:16 +08001217 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001218
Patrick Williams597e8422023-10-20 11:19:01 -05001219 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1220 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001221 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1222 };
1223
1224 setupPowerMatchCallback(systemBus, powerCallBack);
1225
1226 boost::asio::post(io, [&]() {
1227 createSensors(io, objectServer, systemBus, nullptr, false);
1228 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001229 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001230 std::function<void(sdbusplus::message_t&)> eventHandler =
1231 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001232 if (message.is_method_error())
1233 {
1234 std::cerr << "callback method error\n";
1235 return;
1236 }
1237 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001238 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001239 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1240 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001241 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001242 return;
1243 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001244 if (ec)
1245 {
1246 std::cerr << "timer error\n";
1247 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001248 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001249 });
1250 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001251
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001252 boost::asio::steady_timer cpuFilterTimer(io);
1253 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1254 [&](sdbusplus::message_t& message) {
1255 std::string path = message.get_path();
1256 boost::to_lower(path);
1257
1258 sdbusplus::message::object_path cpuPath(path);
1259 std::string cpuName = cpuPath.filename();
1260 if (!cpuName.starts_with("cpu"))
1261 {
1262 return;
1263 }
1264 size_t index = 0;
1265 try
1266 {
1267 index = std::stoi(path.substr(path.size() - 1));
1268 }
1269 catch (const std::invalid_argument&)
1270 {
1271 std::cerr << "Found invalid path " << path << "\n";
1272 return;
1273 }
1274
1275 std::string objectName;
1276 boost::container::flat_map<std::string, std::variant<bool>> values;
1277 message.read(objectName, values);
1278 auto findPresence = values.find("Present");
1279 try
1280 {
1281 cpuPresence[index] = std::get<bool>(findPresence->second);
1282 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001283 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001284 {
1285 return;
1286 }
1287
Chris Sides3fce1bf2023-04-03 16:57:05 -05001288 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001289 {
1290 return;
1291 }
1292 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1293 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1294 if (ec == boost::asio::error::operation_aborted)
1295 {
1296 return;
1297 }
1298 if (ec)
1299 {
1300 std::cerr << "timer error\n";
1301 return;
1302 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001303 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001304 });
1305 };
1306
Zev Weiss214d9712022-08-12 12:54:31 -07001307 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1308 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001309
1310 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1311 static_cast<sdbusplus::bus_t&>(*systemBus),
1312 "type='signal',member='PropertiesChanged',path_namespace='" +
1313 std::string(cpuInventoryPath) +
1314 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1315 cpuPresenceHandler));
1316
Matt Simmering39fadb92023-05-04 21:47:29 -07001317 getPresentCpus(systemBus);
1318
Bruce Lee1263c3d2021-06-04 15:16:33 +08001319 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001320 io.run();
1321}