blob: 0550138254b4218c9be7d7d6529e8e721ce6d325 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Josh Lehan0830c7b2019-10-08 16:35:09 -070016
Matt Simmering786efb82023-01-18 14:09:21 -080017#include "DeviceMgmt.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103018#include "PSUEvent.hpp"
19#include "PSUSensor.hpp"
20#include "Utils.hpp"
21
Matt Simmeringe4e6a282023-03-03 14:41:04 -080022#include <boost/algorithm/string/case_conv.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080025#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070031#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070032#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080035#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080036#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080038#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <utility>
40#include <variant>
41#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080042
Ed Tanous8a57ec02020-10-09 12:46:52 -070043static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070044
Matt Simmering786efb82023-01-18 14:09:21 -080045static const I2CDeviceTypeMap sensorTypes{
Delphine CC Chiu436c5bd2023-09-22 09:47:43 +080046 {"ADC128D818", I2CDeviceType{"adc128d818", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080047 {"ADM1266", I2CDeviceType{"adm1266", true}},
48 {"ADM1272", I2CDeviceType{"adm1272", true}},
49 {"ADM1275", I2CDeviceType{"adm1275", true}},
50 {"ADM1278", I2CDeviceType{"adm1278", true}},
51 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080052 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080053 {"ADS7830", I2CDeviceType{"ads7830", true}},
54 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
55 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020056 {"cffps", I2CDeviceType{"cffps", true}},
57 {"cffps1", I2CDeviceType{"cffps", true}},
58 {"cffps2", I2CDeviceType{"cffps", true}},
59 {"cffps3", I2CDeviceType{"cffps", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080060 {"DPS800", I2CDeviceType{"dps800", true}},
61 {"INA219", I2CDeviceType{"ina219", true}},
62 {"INA230", I2CDeviceType{"ina230", true}},
Potin Lai5674ac92023-11-20 14:33:56 +080063 {"INA238", I2CDeviceType{"ina238", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080064 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080065 {"IR38060", I2CDeviceType{"ir38060", true}},
66 {"IR38164", I2CDeviceType{"ir38164", true}},
67 {"IR38263", I2CDeviceType{"ir38263", true}},
68 {"ISL68137", I2CDeviceType{"isl68137", true}},
69 {"ISL68220", I2CDeviceType{"isl68220", true}},
70 {"ISL68223", I2CDeviceType{"isl68223", true}},
71 {"ISL69225", I2CDeviceType{"isl69225", true}},
72 {"ISL69243", I2CDeviceType{"isl69243", true}},
73 {"ISL69260", I2CDeviceType{"isl69260", true}},
74 {"LM25066", I2CDeviceType{"lm25066", true}},
Yi-Shum77e87692023-11-30 10:27:45 +080075 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +020076 {"MAX5970", I2CDeviceType{"max5970", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080077 {"MAX16601", I2CDeviceType{"max16601", true}},
78 {"MAX20710", I2CDeviceType{"max20710", true}},
79 {"MAX20730", I2CDeviceType{"max20730", true}},
80 {"MAX20734", I2CDeviceType{"max20734", true}},
81 {"MAX20796", I2CDeviceType{"max20796", true}},
82 {"MAX34451", I2CDeviceType{"max34451", true}},
83 {"MP2971", I2CDeviceType{"mp2971", true}},
84 {"MP2973", I2CDeviceType{"mp2973", true}},
85 {"MP2975", I2CDeviceType{"mp2975", true}},
86 {"MP5023", I2CDeviceType{"mp5023", true}},
87 {"NCP4200", I2CDeviceType{"ncp4200", true}},
88 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
89 {"pmbus", I2CDeviceType{"pmbus", true}},
90 {"PXE1610", I2CDeviceType{"pxe1610", true}},
91 {"RAA228000", I2CDeviceType{"raa228000", true}},
92 {"RAA228228", I2CDeviceType{"raa228228", true}},
93 {"RAA228620", I2CDeviceType{"raa228620", true}},
94 {"RAA229001", I2CDeviceType{"raa229001", true}},
95 {"RAA229004", I2CDeviceType{"raa229004", true}},
96 {"RAA229126", I2CDeviceType{"raa229126", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +020097 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080098 {"TPS53679", I2CDeviceType{"tps53679", true}},
99 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
100 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
101 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800102 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800103};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700104
Joseph Fub844e972023-03-25 10:24:30 +0800105enum class DevTypes
106{
107 Unknown = 0,
108 HWMON,
109 IIO
110};
111
112struct DevParams
113{
114 unsigned int matchIndex = 0;
115 std::string matchRegEx;
116 std::string nameRegEx;
117};
118
Cheng C Yang209ec562019-03-12 16:37:44 +0800119namespace fs = std::filesystem;
120
Yong Libf8b1da2020-04-15 16:32:50 +0800121static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800122 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800123static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
124 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800125static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
126 pwmSensors;
127static boost::container::flat_map<std::string, std::string> sensorTable;
128static boost::container::flat_map<std::string, PSUProperty> labelMatch;
129static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800130static boost::container::flat_map<std::string, std::vector<std::string>>
131 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800132static boost::container::flat_map<
133 std::string,
134 boost::container::flat_map<std::string, std::vector<std::string>>>
135 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800136static boost::container::flat_map<std::string, std::vector<std::string>>
137 limitEventMatch;
138
Josh Lehan74d9bd92019-10-31 08:51:58 -0700139static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800140static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800141static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700142
Cheng C Yang58b2b532019-05-31 00:19:45 +0800143// Function CheckEvent will check each attribute from eventMatch table in the
144// sysfs. If the attributes exists in sysfs, then store the complete path
145// of the attribute into eventPathList.
146void checkEvent(
147 const std::string& directory,
148 const boost::container::flat_map<std::string, std::vector<std::string>>&
149 eventMatch,
150 boost::container::flat_map<std::string, std::vector<std::string>>&
151 eventPathList)
152{
153 for (const auto& match : eventMatch)
154 {
155 const std::vector<std::string>& eventAttrs = match.second;
156 const std::string& eventName = match.first;
157 for (const auto& eventAttr : eventAttrs)
158 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700159 std::string eventPath = directory;
160 eventPath += "/";
161 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800162
163 std::ifstream eventFile(eventPath);
164 if (!eventFile.good())
165 {
166 continue;
167 }
168
169 eventPathList[eventName].push_back(eventPath);
170 }
171 }
172}
173
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800174// Check Group Events which contains more than one targets in each combine
175// events.
176void checkGroupEvent(
177 const std::string& directory,
178 const boost::container::flat_map<
179 std::string,
180 boost::container::flat_map<std::string, std::vector<std::string>>>&
181 groupEventMatch,
182 boost::container::flat_map<
183 std::string,
184 boost::container::flat_map<std::string, std::vector<std::string>>>&
185 groupEventPathList)
186{
187 for (const auto& match : groupEventMatch)
188 {
189 const std::string& groupEventName = match.first;
190 const boost::container::flat_map<std::string, std::vector<std::string>>
191 events = match.second;
192 boost::container::flat_map<std::string, std::vector<std::string>>
193 pathList;
194 for (const auto& match : events)
195 {
196 const std::string& eventName = match.first;
197 const std::vector<std::string>& eventAttrs = match.second;
198 for (const auto& eventAttr : eventAttrs)
199 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700200 std::string eventPath = directory;
201 eventPath += "/";
202 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800203 std::ifstream eventFile(eventPath);
204 if (!eventFile.good())
205 {
206 continue;
207 }
208
209 pathList[eventName].push_back(eventPath);
210 }
211 }
212 groupEventPathList[groupEventName] = pathList;
213 }
214}
215
Cheng C Yang58b2b532019-05-31 00:19:45 +0800216// Function checkEventLimits will check all the psu related xxx_input attributes
217// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
218// xxx_min_alarm exist, then store the existing paths of the alarm attributes
219// to eventPathList.
220void checkEventLimits(
221 const std::string& sensorPathStr,
222 const boost::container::flat_map<std::string, std::vector<std::string>>&
223 limitEventMatch,
224 boost::container::flat_map<std::string, std::vector<std::string>>&
225 eventPathList)
226{
Lei YUa2c7cea2020-12-23 14:07:28 +0800227 auto attributePartPos = sensorPathStr.find_last_of('_');
228 if (attributePartPos == std::string::npos)
229 {
230 // There is no '_' in the string, skip it
231 return;
232 }
233 auto attributePart =
234 std::string_view(sensorPathStr).substr(attributePartPos + 1);
235 if (attributePart != "input")
236 {
237 // If the sensor is not xxx_input, skip it
238 return;
239 }
240
241 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800242 for (const auto& limitMatch : limitEventMatch)
243 {
244 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
245 const std::string& eventName = limitMatch.first;
246 for (const auto& limitEventAttr : limitEventAttrs)
247 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800248 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800249 std::ifstream eventFile(limitEventPath);
250 if (!eventFile.good())
251 {
252 continue;
253 }
254 eventPathList[eventName].push_back(limitEventPath);
255 }
256 }
257}
Cheng C Yang916360b2019-05-07 18:47:16 +0800258
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530259static void
260 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
261 const std::string& interfacePath,
262 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
263 sdbusplus::asio::object_server& objectServer,
264 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800265{
Zev Weiss741f26e2022-08-12 18:21:02 -0700266 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800267 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700268 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800269 {
270 continue;
271 }
272
273 const std::string& sensorPathStr = sensorPath.string();
274 const std::string& pwmPathStr =
275 boost::replace_all_copy(sensorPathStr, "input", "target");
276 std::ifstream pwmFile(pwmPathStr);
277 if (!pwmFile.good())
278 {
279 continue;
280 }
281
282 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
283 if (findPWMSensor != pwmSensors.end())
284 {
285 continue;
286 }
287
Zev Weissd8c293a2022-08-15 18:58:41 -0700288 std::string name = "Pwm_";
289 name += psuName;
290 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700291 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700292
293 std::string objPath = interfacePath;
294 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700295 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700296
Cheng C Yang916360b2019-05-07 18:47:16 +0800297 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700298 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800299 }
300}
301
Zhikui Ren23c96e72020-11-05 22:32:28 -0800302static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800303 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800304 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
305 const ManagedObjectType& sensorConfigs,
306 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800307 sensorsChanged,
308 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800309{
Josh Lehan49cfba92019-10-08 16:50:42 -0700310 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800311 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800312
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800313 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
314
Cheng C Yang209ec562019-03-12 16:37:44 +0800315 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800316 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
317 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
318 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800319 {
320 std::cerr << "No PSU sensors in system\n";
321 return;
322 }
323
324 boost::container::flat_set<std::string> directories;
325 for (const auto& pmbusPath : pmbusPaths)
326 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800327 boost::container::flat_map<std::string, std::vector<std::string>>
328 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800329 boost::container::flat_map<
330 std::string,
331 boost::container::flat_map<std::string, std::vector<std::string>>>
332 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800333
334 std::ifstream nameFile(pmbusPath);
335 if (!nameFile.good())
336 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700337 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800338 continue;
339 }
340
341 std::string pmbusName;
342 std::getline(nameFile, pmbusName);
343 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700344
Matt Simmering786efb82023-01-18 14:09:21 -0800345 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800346 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700347 // To avoid this error message, add your driver name to
348 // the pmbusNames vector at the top of this file.
349 std::cerr << "Driver name " << pmbusName
350 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800351 continue;
352 }
353
Cheng C Yang209ec562019-03-12 16:37:44 +0800354 auto directory = pmbusPath.parent_path();
355
356 auto ret = directories.insert(directory.string());
357 if (!ret.second)
358 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700359 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800360 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 }
362
Joseph Fub844e972023-03-25 10:24:30 +0800363 DevTypes devType = DevTypes::HWMON;
364 std::string deviceName;
365 if (directory.parent_path() == "/sys/class/hwmon")
366 {
367 deviceName = fs::canonical(directory / "device").stem();
368 }
369 else
370 {
371 deviceName = fs::canonical(directory).parent_path().stem();
372 devType = DevTypes::IIO;
373 }
374
Cheng C Yang209ec562019-03-12 16:37:44 +0800375 size_t bus = 0;
376 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000377 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800378 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 continue;
380 }
381
Zev Weisse8b97ee2022-08-12 15:23:51 -0700382 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800383 const SensorData* sensorData = nullptr;
384 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800385 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800386 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800387
Zev Weiss741f26e2022-08-12 18:21:02 -0700388 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700390 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800391 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800392 {
Zev Weiss054aad82022-08-18 01:37:34 -0700393 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 if (sensorBase != sensorData->end())
395 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700396 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800397 sensorType = type;
398 break;
399 }
400 }
401 if (baseConfig == nullptr)
402 {
403 std::cerr << "error finding base configuration for "
404 << deviceName << "\n";
405 continue;
406 }
407
Zev Weisse8b97ee2022-08-12 15:23:51 -0700408 auto configBus = baseConfig->find("Bus");
409 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800410
Zev Weisse8b97ee2022-08-12 15:23:51 -0700411 if (configBus == baseConfig->end() ||
412 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800413 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800414 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 continue;
416 }
417
Matt Simmering6747eba2023-01-18 16:03:55 -0800418 const uint64_t* confBus =
419 std::get_if<uint64_t>(&(configBus->second));
420 const uint64_t* confAddr =
421 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800422 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800423 {
424 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700425 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800426 continue;
427 }
428
429 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800430 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200431 if constexpr (debug)
432 {
433 std::cerr << "Configuration skipping " << *confBus << "-"
434 << *confAddr << " because not " << bus << "-"
435 << addr << "\n";
436 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800437 continue;
438 }
439
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800440 std::vector<thresholds::Threshold> confThresholds;
441 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
442 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800443 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800444 }
445 thresholdConfSize = confThresholds.size();
446
Zev Weiss741f26e2022-08-12 18:21:02 -0700447 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800448 break;
449 }
450 if (interfacePath == nullptr)
451 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700452 // To avoid this error message, add your export map entry,
453 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800454 std::cerr << "failed to find match for " << deviceName << "\n";
455 continue;
456 }
457
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800458 auto findI2CDev = devices.find(*interfacePath);
459
460 std::shared_ptr<I2CDevice> i2cDev;
461 if (findI2CDev != devices.end())
462 {
463 if (activateOnly && !findI2CDev->second.second)
464 {
465 continue;
466 }
467 i2cDev = findI2CDev->second.first;
468 }
469
Zev Weisse8b97ee2022-08-12 15:23:51 -0700470 auto findPSUName = baseConfig->find("Name");
471 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800472 {
473 std::cerr << "could not determine configuration name for "
474 << deviceName << "\n";
475 continue;
476 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800477 const std::string* psuName =
478 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800479 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800480 {
481 std::cerr << "Cannot find psu name, invalid configuration\n";
482 continue;
483 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800484
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800485 auto findCPU = baseConfig->find("CPURequired");
486 if (findCPU != baseConfig->end())
487 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500488 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800489 auto presenceFind = cpuPresence.find(index);
490 if (presenceFind == cpuPresence.end() || !presenceFind->second)
491 {
492 continue;
493 }
494 }
495
Zhikui Ren23c96e72020-11-05 22:32:28 -0800496 // on rescans, only update sensors we were signaled by
497 if (!firstScan)
498 {
Zhikui Renda98f092021-11-01 09:41:08 -0700499 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500500 auto it = std::find_if(sensorsChanged->begin(),
501 sensorsChanged->end(),
502 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800503 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500504 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800505
506 if (it == sensorsChanged->end())
507 {
508 continue;
509 }
510 sensorsChanged->erase(it);
511 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800512 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800513 checkGroupEvent(directory.string(), groupEventMatch,
514 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800515
Zev Weisse8b97ee2022-08-12 15:23:51 -0700516 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000517
Vijay Khemka996bad12019-05-28 15:15:16 -0700518 /* Check if there are more sensors in the same interface */
519 int i = 1;
520 std::vector<std::string> psuNames;
521 do
522 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700523 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700524 psuNames.push_back(
525 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700526 findPSUName = baseConfig->find("Name" + std::to_string(i++));
527 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700528
Cheng C Yange50345b2019-04-02 17:26:15 +0800529 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800530 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
531 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800532 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800533 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800534 continue;
535 }
536
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530537 /* read max value in sysfs for in, curr, power, temp, ... */
538 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
539 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700540 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530541 {
542 std::cerr << "No max name in PSU \n";
543 }
544 }
545
Zev Weiss8569bf22022-10-11 15:37:44 -0700546 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800547
Vijay Khemka996bad12019-05-28 15:15:16 -0700548 /* Find array of labels to be exposed if it is defined in config */
549 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700550 auto findLabelObj = baseConfig->find("Labels");
551 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700552 {
553 findLabels =
554 std::get<std::vector<std::string>>(findLabelObj->second);
555 }
556
Joseph Fub844e972023-03-25 10:24:30 +0800557 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700558 std::smatch matches;
559
Cheng C Yange50345b2019-04-02 17:26:15 +0800560 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800561 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530562 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800563 std::string labelHead;
564 std::string sensorPathStr = sensorPath.string();
565 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700566 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700567 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
568 {
Josh Lehan06494452019-10-31 09:49:16 -0700569 // hwmon *_input filename without number:
570 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800571 // iio in_*_raw filename without number:
572 // voltage, temp, pressure, ...
573 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700574 }
575 else
576 {
Josh Lehan06494452019-10-31 09:49:16 -0700577 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700578 << sensorNameStr;
579 continue;
580 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800581
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530582 std::string labelPath;
583
Joseph Fub844e972023-03-25 10:24:30 +0800584 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530585 {
Joseph Fub844e972023-03-25 10:24:30 +0800586 /* find and differentiate _max and _input to replace "label" */
587 size_t pos = sensorPathStr.find('_');
588 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530589 {
Joseph Fub844e972023-03-25 10:24:30 +0800590 std::string sensorPathStrMax = sensorPathStr.substr(pos);
591 if (sensorPathStrMax == "_max")
592 {
593 labelPath = boost::replace_all_copy(sensorPathStr,
594 "max", "label");
595 maxLabel = true;
596 }
597 else
598 {
599 labelPath = boost::replace_all_copy(sensorPathStr,
600 "input", "label");
601 maxLabel = false;
602 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530603 }
604 else
605 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800606 continue;
607 }
608
Joseph Fub844e972023-03-25 10:24:30 +0800609 std::ifstream labelFile(labelPath);
610 if (!labelFile.good())
611 {
612 if constexpr (debug)
613 {
614 std::cerr << "Input file " << sensorPath
615 << " has no corresponding label file\n";
616 }
617 // hwmon *_input filename with number:
618 // temp1, temp2, temp3, ...
619 labelHead = sensorNameStr.substr(0,
620 sensorNameStr.find('_'));
621 }
622 else
623 {
624 std::string label;
625 std::getline(labelFile, label);
626 labelFile.close();
627 auto findSensor = sensors.find(label);
628 if (findSensor != sensors.end())
629 {
630 continue;
631 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800632
Joseph Fub844e972023-03-25 10:24:30 +0800633 // hwmon corresponding *_label file contents:
634 // vin1, vout1, ...
635 labelHead = label.substr(0, label.find(' '));
636 }
637
638 /* append "max" for labelMatch */
639 if (maxLabel)
640 {
641 labelHead.insert(0, "max");
642 }
643
644 checkPWMSensor(sensorPath, labelHead, *interfacePath,
645 dbusConnection, objectServer, psuNames[0]);
646 }
647 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530648 {
Joseph Fub844e972023-03-25 10:24:30 +0800649 auto findIIOHyphen = sensorNameStr.find_last_of('_');
650 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530651 }
652
Ed Tanous8a57ec02020-10-09 12:46:52 -0700653 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700654 {
655 std::cerr << "Sensor type=\"" << sensorNameSubStr
656 << "\" label=\"" << labelHead << "\"\n";
657 }
658
Vijay Khemka996bad12019-05-28 15:15:16 -0700659 if (!findLabels.empty())
660 {
661 /* Check if this labelHead is enabled in config file */
662 if (std::find(findLabels.begin(), findLabels.end(),
663 labelHead) == findLabels.end())
664 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700665 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800666 {
667 std::cerr << "could not find " << labelHead
668 << " in the Labels list\n";
669 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700670 continue;
671 }
672 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800673
Cheng C Yange50345b2019-04-02 17:26:15 +0800674 auto findProperty = labelMatch.find(labelHead);
675 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800676 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700677 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700678 {
679 std::cerr << "Could not find matching default property for "
680 << labelHead << "\n";
681 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800682 continue;
683 }
684
Josh Lehan74d9bd92019-10-31 08:51:58 -0700685 // Protect the hardcoded labelMatch list from changes,
686 // by making a copy and modifying that instead.
687 // Avoid bleedthrough of one device's customizations to
688 // the next device, as each should be independently customizable.
689 psuProperties.push_back(findProperty->second);
690 auto psuProperty = psuProperties.rbegin();
691
692 // Use label head as prefix for reading from config file,
693 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
694 std::string keyName = labelHead + "_Name";
695 std::string keyScale = labelHead + "_Scale";
696 std::string keyMin = labelHead + "_Min";
697 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800698 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800699 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700
701 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700702 auto findCustomName = baseConfig->find(keyName);
703 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700704 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700705 try
706 {
707 psuProperty->labelTypeName = std::visit(
708 VariantToStringVisitor(), findCustomName->second);
709 }
Patrick Williams26601e82021-10-06 12:43:25 -0500710 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700711 {
712 std::cerr << "Unable to parse " << keyName << "\n";
713 continue;
714 }
715
716 // All strings are valid, including empty string
717 customizedName = true;
718 }
719
720 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700721 auto findCustomScale = baseConfig->find(keyScale);
722 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700723 {
724 try
725 {
726 psuProperty->sensorScaleFactor = std::visit(
727 VariantToUnsignedIntVisitor(), findCustomScale->second);
728 }
Patrick Williams26601e82021-10-06 12:43:25 -0500729 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700730 {
731 std::cerr << "Unable to parse " << keyScale << "\n";
732 continue;
733 }
734
735 // Avoid later division by zero
736 if (psuProperty->sensorScaleFactor > 0)
737 {
738 customizedScale = true;
739 }
740 else
741 {
742 std::cerr << "Unable to accept " << keyScale << "\n";
743 continue;
744 }
745 }
746
Zev Weisse8b97ee2022-08-12 15:23:51 -0700747 auto findCustomMin = baseConfig->find(keyMin);
748 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700749 {
750 try
751 {
752 psuProperty->minReading = std::visit(
753 VariantToDoubleVisitor(), findCustomMin->second);
754 }
Patrick Williams26601e82021-10-06 12:43:25 -0500755 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700756 {
757 std::cerr << "Unable to parse " << keyMin << "\n";
758 continue;
759 }
760 }
761
Zev Weisse8b97ee2022-08-12 15:23:51 -0700762 auto findCustomMax = baseConfig->find(keyMax);
763 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700764 {
765 try
766 {
767 psuProperty->maxReading = std::visit(
768 VariantToDoubleVisitor(), findCustomMax->second);
769 }
Patrick Williams26601e82021-10-06 12:43:25 -0500770 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700771 {
772 std::cerr << "Unable to parse " << keyMax << "\n";
773 continue;
774 }
775 }
776
Zev Weisse8b97ee2022-08-12 15:23:51 -0700777 auto findCustomOffset = baseConfig->find(keyOffset);
778 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800779 {
780 try
781 {
782 psuProperty->sensorOffset = std::visit(
783 VariantToDoubleVisitor(), findCustomOffset->second);
784 }
Patrick Williams26601e82021-10-06 12:43:25 -0500785 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800786 {
787 std::cerr << "Unable to parse " << keyOffset << "\n";
788 continue;
789 }
790 }
791
Lotus Xucb5af732021-09-10 15:18:50 +0800792 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700793 auto findPowerState = baseConfig->find(keyPowerState);
794 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800795 {
796 std::string powerState = std::visit(VariantToStringVisitor(),
797 findPowerState->second);
798 setReadState(powerState, readState);
799 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700800 if (!(psuProperty->minReading < psuProperty->maxReading))
801 {
802 std::cerr << "Min must be less than Max\n";
803 continue;
804 }
805
806 // If the sensor name is being customized by config file,
807 // then prefix/suffix composition becomes not necessary,
808 // and in fact not wanted, because it gets in the way.
809 std::string psuNameFromIndex;
810 if (!customizedName)
811 {
812 /* Find out sensor name index for this label */
813 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500814 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700815 if (std::regex_search(labelHead, matches, rgx))
816 {
817 nameIndex = std::stoi(matches[1]);
818
819 // Decrement to preserve alignment, because hwmon
820 // human-readable filenames and labels use 1-based
821 // numbering, but the "Name", "Name1", "Name2", etc. naming
822 // convention (the psuNames vector) uses 0-based numbering.
823 if (nameIndex > 0)
824 {
825 --nameIndex;
826 }
827 }
828 else
829 {
830 nameIndex = 0;
831 }
832
833 if (psuNames.size() <= nameIndex)
834 {
835 std::cerr << "Could not pair " << labelHead
836 << " with a Name field\n";
837 continue;
838 }
839
840 psuNameFromIndex = psuNames[nameIndex];
841
Ed Tanous8a57ec02020-10-09 12:46:52 -0700842 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 {
844 std::cerr << "Sensor label head " << labelHead
845 << " paired with " << psuNameFromIndex
846 << " at index " << nameIndex << "\n";
847 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700848 }
849
Joseph Fub844e972023-03-25 10:24:30 +0800850 if (devType == DevTypes::HWMON)
851 {
852 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
853 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800854
Josh Lehan74d9bd92019-10-31 08:51:58 -0700855 // Similarly, if sensor scaling factor is being customized,
856 // then the below power-of-10 constraint becomes unnecessary,
857 // as config should be able to specify an arbitrary divisor.
858 unsigned int factor = psuProperty->sensorScaleFactor;
859 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700860 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700861 // Preserve existing usage of hardcoded labelMatch table below
862 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700863
Josh Lehan74d9bd92019-10-31 08:51:58 -0700864 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700865 char firstChar =
866 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700867 std::string strScaleFactor =
868 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
869
870 // Preserve existing configs by accepting earlier syntax,
871 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700872 auto findScaleFactor = baseConfig->find(strScaleFactor);
873 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700874 {
875 factor = std::visit(VariantToIntVisitor(),
876 findScaleFactor->second);
877 }
878
Ed Tanous8a57ec02020-10-09 12:46:52 -0700879 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700880 {
881 std::cerr << "Sensor scaling factor " << factor
882 << " string " << strScaleFactor << "\n";
883 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700884 }
885
Vijay Khemka996bad12019-05-28 15:15:16 -0700886 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530887 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
888 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800889 {
James Feist17ab6e02019-06-25 12:28:13 -0700890 std::cerr << "error populating thresholds for "
891 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800892 }
893
Zev Weiss6b6891c2021-04-22 02:46:21 -0500894 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
895 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800896 {
Jason Ling5747fab2019-10-02 16:46:23 -0700897 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700898 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800899 continue;
900 }
901
Ed Tanous8a57ec02020-10-09 12:46:52 -0700902 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700903 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700904 std::cerr << "Sensor properties: Name \""
905 << psuProperty->labelTypeName << "\" Scale "
906 << psuProperty->sensorScaleFactor << " Min "
907 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800908 << psuProperty->maxReading << " Offset "
909 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700910 }
911
912 std::string sensorName = psuProperty->labelTypeName;
913 if (customizedName)
914 {
915 if (sensorName.empty())
916 {
917 // Allow selective disabling of an individual sensor,
918 // by customizing its name to an empty string.
919 std::cerr << "Sensor disabled, empty string\n";
920 continue;
921 }
922 }
923 else
924 {
925 // Sensor name not customized, do prefix/suffix composition,
926 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500927 sensorName = psuNameFromIndex + " " +
928 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700929 }
930
Ed Tanous8a57ec02020-10-09 12:46:52 -0700931 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700932 {
933 std::cerr << "Sensor name \"" << sensorName << "\" path \""
934 << sensorPathStr << "\" type \"" << sensorType
935 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700936 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800937 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800938
939 auto& sensor = sensors[sensorName];
940 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700941 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800942 sensor = nullptr;
943 }
944
945 if (sensor != nullptr)
946 {
947 sensor->activate(sensorPathStr, i2cDev);
948 }
949 else
950 {
951 sensors[sensorName] = std::make_shared<PSUSensor>(
952 sensorPathStr, sensorType, objectServer, dbusConnection, io,
953 sensorName, std::move(sensorThresholds), *interfacePath,
954 readState, findSensorUnit->second, factor,
955 psuProperty->maxReading, psuProperty->minReading,
956 psuProperty->sensorOffset, labelHead, thresholdConfSize,
957 pollRate, i2cDev);
958 sensors[sensorName]->setupRead();
959 ++numCreated;
960 if constexpr (debug)
961 {
962 std::cerr << "Created " << numCreated
963 << " sensors so far\n";
964 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700965 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800966 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800967
Joseph Fub844e972023-03-25 10:24:30 +0800968 if (devType == DevTypes::HWMON)
969 {
970 // OperationalStatus event
971 combineEvents[*psuName + "OperationalStatus"] = nullptr;
972 combineEvents[*psuName + "OperationalStatus"] =
973 std::make_unique<PSUCombineEvent>(
974 objectServer, dbusConnection, io, *psuName, readState,
975 eventPathList, groupEventPathList, "OperationalStatus",
976 pollRate);
977 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800978 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700979
Ed Tanous8a57ec02020-10-09 12:46:52 -0700980 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700981 {
982 std::cerr << "Created total of " << numCreated << " sensors\n";
983 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800984}
Cheng C Yang209ec562019-03-12 16:37:44 +0800985
Matt Simmering39fadb92023-05-04 21:47:29 -0700986static void
987 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
988{
989 static const int depth = 2;
990 static const int numKeys = 1;
991 GetSubTreeType cpuSubTree;
992
993 try
994 {
995 auto getItems = dbusConnection->new_method_call(
996 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
997 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
998 std::array<const char*, numKeys>{
999 "xyz.openbmc_project.Inventory.Item"});
1000 auto getItemsResp = dbusConnection->call(getItems);
1001 getItemsResp.read(cpuSubTree);
1002 }
1003 catch (sdbusplus::exception_t& e)
1004 {
1005 std::cerr << "error getting inventory item subtree: " << e.what()
1006 << "\n";
1007 return;
1008 }
1009
1010 for (const auto& [path, objDict] : cpuSubTree)
1011 {
1012 auto obj = sdbusplus::message::object_path(path).filename();
1013 if (!obj.starts_with("cpu") || objDict.empty())
1014 {
1015 continue;
1016 }
1017 const std::string& owner = objDict.begin()->first;
1018
1019 std::variant<bool> respValue;
1020 try
1021 {
1022 auto getPresence = dbusConnection->new_method_call(
1023 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1024 "Get");
1025 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1026 auto resp = dbusConnection->call(getPresence);
1027 resp.read(respValue);
1028 }
1029 catch (sdbusplus::exception_t& e)
1030 {
1031 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1032 continue;
1033 }
1034
1035 auto* present = std::get_if<bool>(&respValue);
1036 if (present != nullptr && *present)
1037 {
1038 int cpuIndex = 0;
1039 try
1040 {
1041 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1042 }
1043 catch (const std::exception& e)
1044 {
1045 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1046 continue;
1047 }
1048 cpuPresence[cpuIndex + 1] = *present;
1049 }
1050 }
1051}
1052
Zhikui Ren23c96e72020-11-05 22:32:28 -08001053void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001054 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001055 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1056 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001057 sensorsChanged,
1058 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001059{
1060 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001061 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1062 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001063 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1064 sensorsChanged, activateOnly);
1065 });
Matt Simmering786efb82023-01-18 14:09:21 -08001066 std::vector<std::string> types(sensorTypes.size());
1067 for (const auto& [type, dt] : sensorTypes)
1068 {
1069 types.push_back(type);
1070 }
1071 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001072}
1073
Cheng C Yang916360b2019-05-07 18:47:16 +08001074void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001075{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001076 sensorTable = {{"power", sensor_paths::unitWatts},
1077 {"curr", sensor_paths::unitAmperes},
1078 {"temp", sensor_paths::unitDegreesC},
1079 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001080 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001081 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001082
Jeff Line41d52f2021-04-07 19:38:51 +08001083 labelMatch = {
1084 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001085 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1086 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001087 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1088 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1089 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1090 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001091 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1092 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1093 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001094 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1095 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001096 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1097 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001098 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001099 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1100 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1101 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1102 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001103 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1117 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1118 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1119 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1125 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1126 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1127 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1128 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1129 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1130 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1131 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1132 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1133 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1134 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1135 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001136 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001137 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001138 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1139 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1140 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1141 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1142 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1143 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001144 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001145 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1146 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001147 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1148 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1149 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1150 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1151 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1152 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1153 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1154 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1155 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1156 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1157 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1158 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1159 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1160 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1161 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001162 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1163 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1164 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001165 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1166 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1167 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1168 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1169 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1170 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1171 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1172 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1173 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001174 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1175 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1176 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001177
Matt Simmering6747eba2023-01-18 16:03:55 -08001178 pwmTable = {{"fan1", "Fan_1"},
1179 {"fan2", "Fan_2"},
1180 {"fan3", "Fan_3"},
1181 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001182
1183 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1184 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1185
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001186 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1187 {"Failure", {"in2_alarm"}},
1188 {"ACLost", {"in1_beep"}},
1189 {"ConfigureError", {"in1_fault"}}};
1190
1191 groupEventMatch = {{"FanFault",
1192 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001193 {"fan2", {"fan2_alarm", "fan2_fault"}},
1194 {"fan3", {"fan3_alarm", "fan3_fault"}},
1195 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001196
1197 devParamMap = {
1198 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1199 {DevTypes::IIO,
1200 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001201}
1202
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001203static void powerStateChanged(
1204 PowerState type, bool newState,
1205 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1206 sensors,
1207 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1208 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1209{
1210 if (newState)
1211 {
1212 createSensors(io, objectServer, dbusConnection, nullptr, true);
1213 }
1214 else
1215 {
1216 for (auto& [path, sensor] : sensors)
1217 {
1218 if (sensor != nullptr && sensor->readState == type)
1219 {
1220 sensor->deactivate();
1221 }
1222 }
1223 }
1224}
1225
James Feistb6c0b912019-07-09 12:21:44 -07001226int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001227{
Ed Tanous1f978632023-02-28 18:16:39 -08001228 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001229 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1230
Ed Tanous14ed5e92022-07-12 15:50:23 -07001231 sdbusplus::asio::object_server objectServer(systemBus, true);
1232 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001233 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001234 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001235 auto sensorsChanged =
1236 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001237
Cheng C Yang916360b2019-05-07 18:47:16 +08001238 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001239
Patrick Williams597e8422023-10-20 11:19:01 -05001240 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1241 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001242 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1243 };
1244
1245 setupPowerMatchCallback(systemBus, powerCallBack);
1246
1247 boost::asio::post(io, [&]() {
1248 createSensors(io, objectServer, systemBus, nullptr, false);
1249 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001250 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001251 std::function<void(sdbusplus::message_t&)> eventHandler =
1252 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001253 if (message.is_method_error())
1254 {
1255 std::cerr << "callback method error\n";
1256 return;
1257 }
1258 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001259 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001260 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1261 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001262 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001263 return;
1264 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001265 if (ec)
1266 {
1267 std::cerr << "timer error\n";
1268 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001269 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001270 });
1271 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001272
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001273 boost::asio::steady_timer cpuFilterTimer(io);
1274 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1275 [&](sdbusplus::message_t& message) {
1276 std::string path = message.get_path();
1277 boost::to_lower(path);
1278
1279 sdbusplus::message::object_path cpuPath(path);
1280 std::string cpuName = cpuPath.filename();
1281 if (!cpuName.starts_with("cpu"))
1282 {
1283 return;
1284 }
1285 size_t index = 0;
1286 try
1287 {
1288 index = std::stoi(path.substr(path.size() - 1));
1289 }
1290 catch (const std::invalid_argument&)
1291 {
1292 std::cerr << "Found invalid path " << path << "\n";
1293 return;
1294 }
1295
1296 std::string objectName;
1297 boost::container::flat_map<std::string, std::variant<bool>> values;
1298 message.read(objectName, values);
1299 auto findPresence = values.find("Present");
1300 try
1301 {
1302 cpuPresence[index] = std::get<bool>(findPresence->second);
1303 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001304 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001305 {
1306 return;
1307 }
1308
Chris Sides3fce1bf2023-04-03 16:57:05 -05001309 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001310 {
1311 return;
1312 }
1313 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1314 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1315 if (ec == boost::asio::error::operation_aborted)
1316 {
1317 return;
1318 }
1319 if (ec)
1320 {
1321 std::cerr << "timer error\n";
1322 return;
1323 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001324 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001325 });
1326 };
1327
Zev Weiss214d9712022-08-12 12:54:31 -07001328 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1329 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001330
1331 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1332 static_cast<sdbusplus::bus_t&>(*systemBus),
1333 "type='signal',member='PropertiesChanged',path_namespace='" +
1334 std::string(cpuInventoryPath) +
1335 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1336 cpuPresenceHandler));
1337
Matt Simmering39fadb92023-05-04 21:47:29 -07001338 getPresentCpus(systemBus);
1339
Bruce Lee1263c3d2021-06-04 15:16:33 +08001340 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001341 io.run();
1342}