blob: 1c9d1280b8aa51df122220ea862a4543c9ee64b2 [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"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "PwmSensor.hpp"
21#include "SensorPaths.hpp"
22#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103023#include "Utils.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include "VariantVisitors.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103025
Matt Simmeringe4e6a282023-03-03 14:41:04 -080026#include <boost/algorithm/string/case_conv.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080027#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <boost/asio/error.hpp>
29#include <boost/asio/io_context.hpp>
30#include <boost/asio/post.hpp>
31#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <boost/container/flat_set.hpp>
George Liu4155a5a2025-01-24 15:26:27 +080034#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070035#include <sdbusplus/asio/connection.hpp>
36#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070037#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070038#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070039#include <sdbusplus/exception.hpp>
40#include <sdbusplus/message.hpp>
41#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070042
Ed Tanouseacbfdd2024-04-04 12:00:24 -070043#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070044#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070045#include <cctype>
46#include <chrono>
Josh Lehan74d9bd92019-10-31 08:51:58 -070047#include <cmath>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070048#include <cstddef>
49#include <cstdint>
50#include <exception>
James Feist24f02f22019-04-15 11:05:39 -070051#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080052#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070053#include <functional>
Jayaprakash Mutyalaa205c6f2025-02-13 06:09:04 +000054#include <iterator>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070055#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080056#include <regex>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070057#include <stdexcept>
Patrick Venture96e97db2019-10-31 13:44:38 -070058#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080059#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070060#include <utility>
61#include <variant>
62#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080063
Chau Lyc4ddf642024-07-09 05:01:06 +000064static std::regex i2cDevRegex(R"((\/i2c\-\d+\/\d+-[a-fA-F0-9]{4,4})(\/|$))");
Josh Lehan49cfba92019-10-08 16:50:42 -070065
Matt Simmering786efb82023-01-18 14:09:21 -080066static const I2CDeviceTypeMap sensorTypes{
Delphine CC Chiu436c5bd2023-09-22 09:47:43 +080067 {"ADC128D818", I2CDeviceType{"adc128d818", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080068 {"ADM1266", I2CDeviceType{"adm1266", true}},
69 {"ADM1272", I2CDeviceType{"adm1272", true}},
70 {"ADM1275", I2CDeviceType{"adm1275", true}},
71 {"ADM1278", I2CDeviceType{"adm1278", true}},
72 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080073 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080074 {"ADS7830", I2CDeviceType{"ads7830", true}},
75 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
76 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020077 {"cffps", I2CDeviceType{"cffps", true}},
78 {"cffps1", I2CDeviceType{"cffps", true}},
79 {"cffps2", I2CDeviceType{"cffps", true}},
80 {"cffps3", I2CDeviceType{"cffps", true}},
Andrew Geisslerb2392ef2024-12-10 09:39:19 -060081 {"CRPS185", I2CDeviceType{"crps185", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080082 {"DPS800", I2CDeviceType{"dps800", true}},
83 {"INA219", I2CDeviceType{"ina219", true}},
Alejandro Leal20b56822025-04-25 00:46:11 +000084 {"INA226", I2CDeviceType{"ina226", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080085 {"INA230", I2CDeviceType{"ina230", true}},
Delphine CC Chiu082ae0b2023-09-22 09:49:53 +080086 {"INA233", I2CDeviceType{"ina233", true}},
Potin Lai5674ac92023-11-20 14:33:56 +080087 {"INA238", I2CDeviceType{"ina238", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080088 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
George Liu43b074d2025-04-09 08:52:19 +080089 {"IR35221", I2CDeviceType{"ir35221", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080090 {"IR38060", I2CDeviceType{"ir38060", true}},
91 {"IR38164", I2CDeviceType{"ir38164", true}},
92 {"IR38263", I2CDeviceType{"ir38263", true}},
Yikai Tsai5b74aba2024-09-06 13:58:28 +080093 {"ISL28022", I2CDeviceType{"isl28022", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080094 {"ISL68137", I2CDeviceType{"isl68137", true}},
95 {"ISL68220", I2CDeviceType{"isl68220", true}},
96 {"ISL68223", I2CDeviceType{"isl68223", true}},
97 {"ISL69225", I2CDeviceType{"isl69225", true}},
98 {"ISL69243", I2CDeviceType{"isl69243", true}},
99 {"ISL69260", I2CDeviceType{"isl69260", true}},
100 {"LM25066", I2CDeviceType{"lm25066", true}},
Potin Lai8339feb2025-05-28 15:51:56 +0800101 {"LM5066I", I2CDeviceType{"lm5066i", true}},
Yi-Shum77e87692023-11-30 10:27:45 +0800102 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Potin Lai43847a82024-02-27 16:29:45 +0800103 {"LTC4286", I2CDeviceType{"ltc4286", true}},
104 {"LTC4287", I2CDeviceType{"ltc4287", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +0200105 {"MAX5970", I2CDeviceType{"max5970", true}},
Joseph Fu15a39df2023-12-27 09:45:49 +0800106 {"MAX11607", I2CDeviceType{"max11607", false}},
Ian Chienc82a4092024-07-24 10:52:59 +0800107 {"MAX11615", I2CDeviceType{"max11615", false}},
Allen.Wang8c13c282024-07-03 09:42:54 +0800108 {"MAX11617", I2CDeviceType{"max11617", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800109 {"MAX16601", I2CDeviceType{"max16601", true}},
110 {"MAX20710", I2CDeviceType{"max20710", true}},
111 {"MAX20730", I2CDeviceType{"max20730", true}},
112 {"MAX20734", I2CDeviceType{"max20734", true}},
113 {"MAX20796", I2CDeviceType{"max20796", true}},
114 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800115 {"MP2856", I2CDeviceType{"mp2856", true}},
116 {"MP2857", I2CDeviceType{"mp2857", true}},
Leo Yang3a3ebd12025-09-10 14:38:13 +0800117 {"MP2869", I2CDeviceType{"mp2869", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800118 {"MP2971", I2CDeviceType{"mp2971", true}},
119 {"MP2973", I2CDeviceType{"mp2973", true}},
120 {"MP2975", I2CDeviceType{"mp2975", true}},
121 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800122 {"MP5990", I2CDeviceType{"mp5990", true}},
Leo Yang3a3ebd12025-09-10 14:38:13 +0800123 {"MP29612", I2CDeviceType{"mp29612", true}},
Jeff Lind8e48df2024-06-07 09:06:56 +0800124 {"MPQ8785", I2CDeviceType{"mpq8785", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800125 {"NCP4200", I2CDeviceType{"ncp4200", true}},
126 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
127 {"pmbus", I2CDeviceType{"pmbus", true}},
128 {"PXE1610", I2CDeviceType{"pxe1610", true}},
Peter Yin143d00e2025-09-16 16:56:41 +0800129 {"SQ52206", I2CDeviceType{"sq52206", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800130 {"RAA228000", I2CDeviceType{"raa228000", true}},
Potin Lai6e6618f2024-05-16 15:02:56 +0800131 {"RAA228004", I2CDeviceType{"raa228004", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800132 {"RAA228228", I2CDeviceType{"raa228228", true}},
133 {"RAA228620", I2CDeviceType{"raa228620", true}},
134 {"RAA229001", I2CDeviceType{"raa229001", true}},
135 {"RAA229004", I2CDeviceType{"raa229004", true}},
136 {"RAA229126", I2CDeviceType{"raa229126", true}},
Ian Chien982d99f2024-08-20 17:16:56 +0800137 {"RTQ6056", I2CDeviceType{"rtq6056", false}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800138 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Chau Lyc4ddf642024-07-09 05:01:06 +0000139 {"smpro_hwmon", I2CDeviceType{"smpro", false}},
Rush Chena9ea8e52025-01-02 11:28:57 +0800140 {"SY24655", I2CDeviceType{"sy24655", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200141 {"TDA38640", I2CDeviceType{"tda38640", true}},
Kevin Tung8e84dc82025-07-10 18:20:42 +0800142 {"TPS25990", I2CDeviceType{"tps25990", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800143 {"TPS53679", I2CDeviceType{"tps53679", true}},
144 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800145 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800146 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
147 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800148 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800149};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700150
Joseph Fub844e972023-03-25 10:24:30 +0800151enum class DevTypes
152{
153 Unknown = 0,
154 HWMON,
155 IIO
156};
157
158struct DevParams
159{
160 unsigned int matchIndex = 0;
161 std::string matchRegEx;
162 std::string nameRegEx;
163};
164
Yong Libf8b1da2020-04-15 16:32:50 +0800165static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800166 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800167static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
168 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800169static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
170 pwmSensors;
171static boost::container::flat_map<std::string, std::string> sensorTable;
172static boost::container::flat_map<std::string, PSUProperty> labelMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800173static EventPathList eventMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800174static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800175
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800176static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800177static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700178
Cheng C Yang58b2b532019-05-31 00:19:45 +0800179// Function CheckEvent will check each attribute from eventMatch table in the
180// sysfs. If the attributes exists in sysfs, then store the complete path
181// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800182void checkEvent(const std::string& directory, const EventPathList& eventMatch,
183 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800184{
185 for (const auto& match : eventMatch)
186 {
187 const std::vector<std::string>& eventAttrs = match.second;
188 const std::string& eventName = match.first;
189 for (const auto& eventAttr : eventAttrs)
190 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700191 std::string eventPath = directory;
192 eventPath += "/";
193 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800194
195 std::ifstream eventFile(eventPath);
196 if (!eventFile.good())
197 {
198 continue;
199 }
200
201 eventPathList[eventName].push_back(eventPath);
202 }
203 }
204}
205
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800206// Check Group Events which contains more than one targets in each combine
207// events.
George Liu5b3542e2023-10-31 17:27:12 +0800208void checkGroupEvent(const std::string& directory,
George Liu5b3542e2023-10-31 17:27:12 +0800209 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800210{
cchouxe8a14e92023-10-25 23:26:27 +0800211 EventPathList pathList;
Ed Tanous2e466962025-01-30 10:59:49 -0800212 std::vector<std::filesystem::path> eventPaths;
213 if (!findFiles(std::filesystem::path(directory), R"(fan\d+_(alarm|fault))",
214 eventPaths))
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800215 {
cchouxe8a14e92023-10-25 23:26:27 +0800216 return;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800217 }
cchouxe8a14e92023-10-25 23:26:27 +0800218
219 for (const auto& eventPath : eventPaths)
220 {
221 std::string attrName = eventPath.filename();
222 pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
223 }
224 groupEventPathList["FanFault"] = pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800225}
226
Cheng C Yang58b2b532019-05-31 00:19:45 +0800227// Function checkEventLimits will check all the psu related xxx_input attributes
228// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
229// xxx_min_alarm exist, then store the existing paths of the alarm attributes
230// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800231void checkEventLimits(const std::string& sensorPathStr,
232 const EventPathList& limitEventMatch,
233 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800234{
Lei YUa2c7cea2020-12-23 14:07:28 +0800235 auto attributePartPos = sensorPathStr.find_last_of('_');
236 if (attributePartPos == std::string::npos)
237 {
238 // There is no '_' in the string, skip it
239 return;
240 }
241 auto attributePart =
242 std::string_view(sensorPathStr).substr(attributePartPos + 1);
243 if (attributePart != "input")
244 {
245 // If the sensor is not xxx_input, skip it
246 return;
247 }
248
249 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800250 for (const auto& limitMatch : limitEventMatch)
251 {
252 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
253 const std::string& eventName = limitMatch.first;
254 for (const auto& limitEventAttr : limitEventAttrs)
255 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800256 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800257 std::ifstream eventFile(limitEventPath);
258 if (!eventFile.good())
259 {
260 continue;
261 }
262 eventPathList[eventName].push_back(limitEventPath);
263 }
264 }
265}
Cheng C Yang916360b2019-05-07 18:47:16 +0800266
Patrick Williams2aaf7172024-08-16 15:20:40 -0400267static void checkPWMSensor(
Ed Tanous2e466962025-01-30 10:59:49 -0800268 const std::filesystem::path& sensorPath, std::string& labelHead,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400269 const std::string& interfacePath,
270 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
271 sdbusplus::asio::object_server& objectServer, const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800272{
cchouxe8a14e92023-10-25 23:26:27 +0800273 if (!labelHead.starts_with("fan"))
Cheng C Yang916360b2019-05-07 18:47:16 +0800274 {
cchouxe8a14e92023-10-25 23:26:27 +0800275 return;
Cheng C Yang916360b2019-05-07 18:47:16 +0800276 }
cchouxe8a14e92023-10-25 23:26:27 +0800277 std::string labelHeadIndex = labelHead.substr(3);
278
279 const std::string& sensorPathStr = sensorPath.string();
Patrick Williams2aaf7172024-08-16 15:20:40 -0400280 const std::string& pwmPathStr =
281 boost::replace_all_copy(sensorPathStr, "input", "target");
cchouxe8a14e92023-10-25 23:26:27 +0800282 std::ifstream pwmFile(pwmPathStr);
283 if (!pwmFile.good())
284 {
285 return;
286 }
287
288 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
289 if (findPWMSensor != pwmSensors.end())
290 {
291 return;
292 }
293
294 std::string name = "Pwm_";
295 name += psuName;
296 name += "_Fan_";
297 name += labelHeadIndex;
298
299 std::string objPath = interfacePath;
300 objPath += "_Fan_";
301 objPath += labelHeadIndex;
302
303 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
304 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800305}
306
Zhikui Ren23c96e72020-11-05 22:32:28 -0800307static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800308 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800309 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
310 const ManagedObjectType& sensorConfigs,
311 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800312 sensorsChanged,
313 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800314{
Josh Lehan49cfba92019-10-08 16:50:42 -0700315 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800316 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800317
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800318 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
319
Ed Tanous2e466962025-01-30 10:59:49 -0800320 std::vector<std::filesystem::path> pmbusPaths;
321 findFiles(std::filesystem::path("/sys/bus/iio/devices"), "name",
322 pmbusPaths);
323 findFiles(std::filesystem::path("/sys/class/hwmon"), "name", pmbusPaths);
Joseph Fub844e972023-03-25 10:24:30 +0800324 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800325 {
George Liu4155a5a2025-01-24 15:26:27 +0800326 lg2::error("No PSU sensors in system");
Cheng C Yang209ec562019-03-12 16:37:44 +0800327 return;
328 }
329
330 boost::container::flat_set<std::string> directories;
331 for (const auto& pmbusPath : pmbusPaths)
332 {
George Liu5b3542e2023-10-31 17:27:12 +0800333 EventPathList eventPathList;
334 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800335
336 std::ifstream nameFile(pmbusPath);
337 if (!nameFile.good())
338 {
George Liu4155a5a2025-01-24 15:26:27 +0800339 lg2::error("Failure finding '{PATH}'", "PATH", pmbusPath);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800340 continue;
341 }
342
343 std::string pmbusName;
344 std::getline(nameFile, pmbusName);
345 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700346
Patrick Williams80c68052025-05-13 23:31:49 -0400347 if (!sensorTypes.contains(pmbusName))
Cheng C Yang58b2b532019-05-31 00:19:45 +0800348 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700349 // To avoid this error message, add your driver name to
350 // the pmbusNames vector at the top of this file.
George Liu4155a5a2025-01-24 15:26:27 +0800351 lg2::error("'{NAME}' not found in sensor whitelist", "NAME",
352 pmbusName);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800353 continue;
354 }
355
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 auto directory = pmbusPath.parent_path();
357
358 auto ret = directories.insert(directory.string());
359 if (!ret.second)
360 {
George Liu4155a5a2025-01-24 15:26:27 +0800361 lg2::error("Duplicate path: '{PATH}'", "PATH", directory);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800362 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 }
364
Joseph Fub844e972023-03-25 10:24:30 +0800365 DevTypes devType = DevTypes::HWMON;
366 std::string deviceName;
367 if (directory.parent_path() == "/sys/class/hwmon")
368 {
Ed Tanous2e466962025-01-30 10:59:49 -0800369 std::string devicePath =
370 std::filesystem::canonical(directory / "device");
Chau Lyc4ddf642024-07-09 05:01:06 +0000371 std::smatch match;
372 // Find /i2c-<bus>/<bus>-<address> match in device path
373 std::regex_search(devicePath, match, i2cDevRegex);
374 if (match.empty())
375 {
George Liu4155a5a2025-01-24 15:26:27 +0800376 lg2::error("Found bad device path: '{PATH}'", "PATH",
377 devicePath);
Chau Lyc4ddf642024-07-09 05:01:06 +0000378 continue;
379 }
380 // Extract <bus>-<address>
381 std::string matchStr = match[1];
382 deviceName = matchStr.substr(matchStr.find_last_of('/') + 1);
Joseph Fub844e972023-03-25 10:24:30 +0800383 }
384 else
385 {
Ed Tanous2e466962025-01-30 10:59:49 -0800386 deviceName =
387 std::filesystem::canonical(directory).parent_path().stem();
Joseph Fub844e972023-03-25 10:24:30 +0800388 devType = DevTypes::IIO;
389 }
390
Cheng C Yang209ec562019-03-12 16:37:44 +0800391 size_t bus = 0;
392 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000393 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 continue;
396 }
397
Zev Weisse8b97ee2022-08-12 15:23:51 -0700398 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800399 const SensorData* sensorData = nullptr;
400 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800401 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800402 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800403
Zev Weiss741f26e2022-08-12 18:21:02 -0700404 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800405 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700406 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800407 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800408 {
Zev Weiss054aad82022-08-18 01:37:34 -0700409 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800410 if (sensorBase != sensorData->end())
411 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700412 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800413 sensorType = type;
414 break;
415 }
416 }
417 if (baseConfig == nullptr)
418 {
George Liu4155a5a2025-01-24 15:26:27 +0800419 lg2::error("error finding base configuration for '{NAME}'",
420 "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 continue;
422 }
423
Zev Weisse8b97ee2022-08-12 15:23:51 -0700424 auto configBus = baseConfig->find("Bus");
425 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800426
Zev Weisse8b97ee2022-08-12 15:23:51 -0700427 if (configBus == baseConfig->end() ||
428 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800429 {
George Liu4155a5a2025-01-24 15:26:27 +0800430 lg2::error("error finding necessary entry in configuration");
Cheng C Yang209ec562019-03-12 16:37:44 +0800431 continue;
432 }
433
Matt Simmering6747eba2023-01-18 16:03:55 -0800434 const uint64_t* confBus =
435 std::get_if<uint64_t>(&(configBus->second));
436 const uint64_t* confAddr =
437 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800438 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800439 {
George Liu4155a5a2025-01-24 15:26:27 +0800440 lg2::error("Cannot get bus or address, invalid configuration");
Cheng C Yang58b2b532019-05-31 00:19:45 +0800441 continue;
442 }
443
444 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800445 {
Alexander Hansen89be6142025-09-18 15:36:16 +0200446 lg2::debug(
447 "Configuration skipping '{CONFBUS}'-'{CONFADDR}' because not {BUS}-{ADDR}",
448 "CONFBUS", *confBus, "CONFADDR", *confAddr, "BUS", bus,
449 "ADDR", addr);
Cheng C Yang209ec562019-03-12 16:37:44 +0800450 continue;
451 }
452
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800453 std::vector<thresholds::Threshold> confThresholds;
454 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
455 {
George Liu4155a5a2025-01-24 15:26:27 +0800456 lg2::error("error populating total thresholds");
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800457 }
458 thresholdConfSize = confThresholds.size();
459
Zev Weiss741f26e2022-08-12 18:21:02 -0700460 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800461 break;
462 }
463 if (interfacePath == nullptr)
464 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700465 // To avoid this error message, add your export map entry,
466 // from Entity Manager, to sensorTypes at the top of this file.
George Liu4155a5a2025-01-24 15:26:27 +0800467 lg2::error("failed to find match for '{NAME}'", "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800468 continue;
469 }
470
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800471 auto findI2CDev = devices.find(*interfacePath);
472
473 std::shared_ptr<I2CDevice> i2cDev;
474 if (findI2CDev != devices.end())
475 {
476 if (activateOnly && !findI2CDev->second.second)
477 {
478 continue;
479 }
480 i2cDev = findI2CDev->second.first;
481 }
482
Zev Weisse8b97ee2022-08-12 15:23:51 -0700483 auto findPSUName = baseConfig->find("Name");
484 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800485 {
George Liu4155a5a2025-01-24 15:26:27 +0800486 lg2::error("could not determine configuration name for '{NAME}'",
487 "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800488 continue;
489 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800490 const std::string* psuName =
491 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800492 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800493 {
George Liu4155a5a2025-01-24 15:26:27 +0800494 lg2::error("Cannot find psu name, invalid configuration");
Cheng C Yang58b2b532019-05-31 00:19:45 +0800495 continue;
496 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800497
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800498 auto findCPU = baseConfig->find("CPURequired");
499 if (findCPU != baseConfig->end())
500 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500501 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800502 auto presenceFind = cpuPresence.find(index);
503 if (presenceFind == cpuPresence.end() || !presenceFind->second)
504 {
505 continue;
506 }
507 }
508
Zhikui Ren23c96e72020-11-05 22:32:28 -0800509 // on rescans, only update sensors we were signaled by
510 if (!firstScan)
511 {
Zhikui Renda98f092021-11-01 09:41:08 -0700512 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400513 auto it =
514 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
515 [psuNameStr](std::string& s) {
516 return s.ends_with(psuNameStr);
517 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800518
519 if (it == sensorsChanged->end())
520 {
521 continue;
522 }
523 sensorsChanged->erase(it);
524 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800525 checkEvent(directory.string(), eventMatch, eventPathList);
cchouxe8a14e92023-10-25 23:26:27 +0800526 checkGroupEvent(directory.string(), groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800527
Zev Weisse8b97ee2022-08-12 15:23:51 -0700528 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000529
Vijay Khemka996bad12019-05-28 15:15:16 -0700530 /* Check if there are more sensors in the same interface */
531 int i = 1;
532 std::vector<std::string> psuNames;
533 do
534 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700535 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700536 psuNames.push_back(
537 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700538 findPSUName = baseConfig->find("Name" + std::to_string(i++));
539 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700540
Ed Tanous2e466962025-01-30 10:59:49 -0800541 std::vector<std::filesystem::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800542 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
543 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800544 {
George Liu4155a5a2025-01-24 15:26:27 +0800545 lg2::error("No PSU non-label sensor in PSU");
Cheng C Yang209ec562019-03-12 16:37:44 +0800546 continue;
547 }
548
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530549 /* read max value in sysfs for in, curr, power, temp, ... */
550 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
551 {
Alexander Hansen89be6142025-09-18 15:36:16 +0200552 lg2::debug("No max name in PSU");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530553 }
554
Zev Weiss8569bf22022-10-11 15:37:44 -0700555 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800556
Vijay Khemka996bad12019-05-28 15:15:16 -0700557 /* Find array of labels to be exposed if it is defined in config */
558 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700559 auto findLabelObj = baseConfig->find("Labels");
560 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700561 {
562 findLabels =
563 std::get<std::vector<std::string>>(findLabelObj->second);
564 }
565
Joseph Fub844e972023-03-25 10:24:30 +0800566 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700567 std::smatch matches;
568
Cheng C Yange50345b2019-04-02 17:26:15 +0800569 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800570 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530571 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800572 std::string labelHead;
573 std::string sensorPathStr = sensorPath.string();
574 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700575 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700576 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
577 {
Josh Lehan06494452019-10-31 09:49:16 -0700578 // hwmon *_input filename without number:
579 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800580 // iio in_*_raw filename without number:
581 // voltage, temp, pressure, ...
582 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700583 }
584 else
585 {
George Liu4155a5a2025-01-24 15:26:27 +0800586 lg2::error("Could not extract the alpha prefix from '{NAME}'",
587 "NAME", sensorNameStr);
Jason Ling5747fab2019-10-02 16:46:23 -0700588 continue;
589 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800590
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530591 std::string labelPath;
592
Joseph Fub844e972023-03-25 10:24:30 +0800593 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530594 {
Joseph Fub844e972023-03-25 10:24:30 +0800595 /* find and differentiate _max and _input to replace "label" */
596 size_t pos = sensorPathStr.find('_');
597 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530598 {
Joseph Fub844e972023-03-25 10:24:30 +0800599 std::string sensorPathStrMax = sensorPathStr.substr(pos);
600 if (sensorPathStrMax == "_max")
601 {
602 labelPath = boost::replace_all_copy(sensorPathStr,
603 "max", "label");
604 maxLabel = true;
605 }
606 else
607 {
608 labelPath = boost::replace_all_copy(sensorPathStr,
609 "input", "label");
610 maxLabel = false;
611 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530612 }
613 else
614 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800615 continue;
616 }
617
Joseph Fub844e972023-03-25 10:24:30 +0800618 std::ifstream labelFile(labelPath);
619 if (!labelFile.good())
620 {
Alexander Hansen89be6142025-09-18 15:36:16 +0200621 lg2::debug(
622 "Input file '{PATH}' has no corresponding label file",
623 "PATH", sensorPath.string());
Joseph Fub844e972023-03-25 10:24:30 +0800624 // hwmon *_input filename with number:
625 // temp1, temp2, temp3, ...
Patrick Williams2aaf7172024-08-16 15:20:40 -0400626 labelHead =
627 sensorNameStr.substr(0, sensorNameStr.find('_'));
Joseph Fub844e972023-03-25 10:24:30 +0800628 }
629 else
630 {
631 std::string label;
632 std::getline(labelFile, label);
633 labelFile.close();
634 auto findSensor = sensors.find(label);
635 if (findSensor != sensors.end())
636 {
637 continue;
638 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800639
Joseph Fub844e972023-03-25 10:24:30 +0800640 // hwmon corresponding *_label file contents:
641 // vin1, vout1, ...
642 labelHead = label.substr(0, label.find(' '));
643 }
644
645 /* append "max" for labelMatch */
646 if (maxLabel)
647 {
648 labelHead.insert(0, "max");
649 }
650
Andrew Geissler66783882025-04-02 11:50:33 -0500651 // Don't add PWM sensors if it's not in label list
652 if (!findLabels.empty())
653 {
654 /* Check if this labelHead is enabled in config file */
655 if (std::find(findLabels.begin(), findLabels.end(),
656 labelHead) == findLabels.end())
657 {
Alexander Hansen89be6142025-09-18 15:36:16 +0200658 lg2::debug("could not find {LABEL} in the Labels list",
659 "LABEL", labelHead);
Andrew Geissler66783882025-04-02 11:50:33 -0500660 continue;
661 }
662 }
Joseph Fub844e972023-03-25 10:24:30 +0800663 checkPWMSensor(sensorPath, labelHead, *interfacePath,
664 dbusConnection, objectServer, psuNames[0]);
665 }
666 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530667 {
Joseph Fub844e972023-03-25 10:24:30 +0800668 auto findIIOHyphen = sensorNameStr.find_last_of('_');
669 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530670 }
671
Alexander Hansen89be6142025-09-18 15:36:16 +0200672 lg2::debug("Sensor type: {NAME}, label: {LABEL}", "NAME",
673 sensorNameSubStr, "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700674
Vijay Khemka996bad12019-05-28 15:15:16 -0700675 if (!findLabels.empty())
676 {
677 /* Check if this labelHead is enabled in config file */
678 if (std::find(findLabels.begin(), findLabels.end(),
679 labelHead) == findLabels.end())
680 {
Alexander Hansen89be6142025-09-18 15:36:16 +0200681 lg2::debug("could not find '{LABEL}' in the Labels list",
682 "LABEL", labelHead);
Vijay Khemka996bad12019-05-28 15:15:16 -0700683 continue;
684 }
685 }
Jayaprakash Mutyalaa205c6f2025-02-13 06:09:04 +0000686 auto it = std::find_if(labelHead.begin(), labelHead.end(),
687 static_cast<int (*)(int)>(std::isdigit));
688 std::string_view labelHeadView(
689 labelHead.data(), std::distance(labelHead.begin(), it));
690 auto findProperty =
691 labelMatch.find(static_cast<std::string>(labelHeadView));
Cheng C Yange50345b2019-04-02 17:26:15 +0800692 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800693 {
Alexander Hansen89be6142025-09-18 15:36:16 +0200694 lg2::debug(
695 "Could not find matching default property for '{LABEL}'",
696 "LABEL", labelHead);
Cheng C Yang209ec562019-03-12 16:37:44 +0800697 continue;
698 }
699
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700 // Protect the hardcoded labelMatch list from changes,
701 // by making a copy and modifying that instead.
702 // Avoid bleedthrough of one device's customizations to
703 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800704 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700705
706 // Use label head as prefix for reading from config file,
707 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
708 std::string keyName = labelHead + "_Name";
709 std::string keyScale = labelHead + "_Scale";
710 std::string keyMin = labelHead + "_Min";
711 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800712 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800713 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700714
715 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700716 auto findCustomName = baseConfig->find(keyName);
717 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700718 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700719 try
720 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800721 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700722 VariantToStringVisitor(), findCustomName->second);
723 }
Patrick Williams26601e82021-10-06 12:43:25 -0500724 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700725 {
George Liu4155a5a2025-01-24 15:26:27 +0800726 lg2::error("Unable to parse '{NAME}'", "NAME", keyName);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700727 continue;
728 }
729
730 // All strings are valid, including empty string
731 customizedName = true;
732 }
733
734 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700735 auto findCustomScale = baseConfig->find(keyScale);
736 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 {
738 try
739 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800740 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700741 VariantToUnsignedIntVisitor(), findCustomScale->second);
742 }
Patrick Williams26601e82021-10-06 12:43:25 -0500743 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700744 {
George Liu4155a5a2025-01-24 15:26:27 +0800745 lg2::error("Unable to parse '{SCALE}'", "SCALE", keyScale);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700746 continue;
747 }
748
749 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800750 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700751 {
752 customizedScale = true;
753 }
754 else
755 {
George Liu4155a5a2025-01-24 15:26:27 +0800756 lg2::error("Unable to accept '{SCALE}'", "SCALE", keyScale);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700757 continue;
758 }
759 }
760
Zev Weisse8b97ee2022-08-12 15:23:51 -0700761 auto findCustomMin = baseConfig->find(keyMin);
762 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700763 {
764 try
765 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800766 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700767 VariantToDoubleVisitor(), findCustomMin->second);
768 }
Patrick Williams26601e82021-10-06 12:43:25 -0500769 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700770 {
George Liu4155a5a2025-01-24 15:26:27 +0800771 lg2::error("Unable to parse '{MIN}'", "MIN", keyMin);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700772 continue;
773 }
774 }
775
Zev Weisse8b97ee2022-08-12 15:23:51 -0700776 auto findCustomMax = baseConfig->find(keyMax);
777 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700778 {
779 try
780 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800781 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700782 VariantToDoubleVisitor(), findCustomMax->second);
783 }
Patrick Williams26601e82021-10-06 12:43:25 -0500784 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700785 {
George Liu4155a5a2025-01-24 15:26:27 +0800786 lg2::error("Unable to parse '{MAX}'", "MAX", keyMax);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700787 continue;
788 }
789 }
790
Zev Weisse8b97ee2022-08-12 15:23:51 -0700791 auto findCustomOffset = baseConfig->find(keyOffset);
792 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800793 {
794 try
795 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800796 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800797 VariantToDoubleVisitor(), findCustomOffset->second);
798 }
Patrick Williams26601e82021-10-06 12:43:25 -0500799 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800800 {
George Liu4155a5a2025-01-24 15:26:27 +0800801 lg2::error("Unable to parse '{OFFSET}'", "OFFSET",
802 keyOffset);
Jeff Line41d52f2021-04-07 19:38:51 +0800803 continue;
804 }
805 }
806
Lotus Xucb5af732021-09-10 15:18:50 +0800807 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700808 auto findPowerState = baseConfig->find(keyPowerState);
809 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800810 {
811 std::string powerState = std::visit(VariantToStringVisitor(),
812 findPowerState->second);
813 setReadState(powerState, readState);
814 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800815 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700816 {
George Liu4155a5a2025-01-24 15:26:27 +0800817 lg2::error("Min must be less than Max");
Josh Lehan74d9bd92019-10-31 08:51:58 -0700818 continue;
819 }
820
821 // If the sensor name is being customized by config file,
822 // then prefix/suffix composition becomes not necessary,
823 // and in fact not wanted, because it gets in the way.
824 std::string psuNameFromIndex;
cchouxe8a14e92023-10-25 23:26:27 +0800825 std::string nameIndexStr = "1";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 if (!customizedName)
827 {
828 /* Find out sensor name index for this label */
829 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500830 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700831 if (std::regex_search(labelHead, matches, rgx))
832 {
cchouxe8a14e92023-10-25 23:26:27 +0800833 nameIndexStr = matches[1];
834 nameIndex = std::stoi(nameIndexStr);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700835
836 // Decrement to preserve alignment, because hwmon
837 // human-readable filenames and labels use 1-based
838 // numbering, but the "Name", "Name1", "Name2", etc. naming
839 // convention (the psuNames vector) uses 0-based numbering.
840 if (nameIndex > 0)
841 {
842 --nameIndex;
843 }
844 }
845 else
846 {
847 nameIndex = 0;
848 }
849
850 if (psuNames.size() <= nameIndex)
851 {
George Liu4155a5a2025-01-24 15:26:27 +0800852 lg2::error("Could not pair '{LABEL}' with a Name field",
853 "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700854 continue;
855 }
856
857 psuNameFromIndex = psuNames[nameIndex];
858
Alexander Hansen89be6142025-09-18 15:36:16 +0200859 lg2::debug("'{LABEL}' paired with '{NAME}' at index '{INDEX}'",
860 "LABEL", labelHead, "NAME", psuNameFromIndex,
861 "INDEX", nameIndex);
Josh Lehan432d1ed2019-10-16 12:23:31 -0700862 }
863
Joseph Fub844e972023-03-25 10:24:30 +0800864 if (devType == DevTypes::HWMON)
865 {
866 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
867 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800868
Josh Lehan74d9bd92019-10-31 08:51:58 -0700869 // Similarly, if sensor scaling factor is being customized,
870 // then the below power-of-10 constraint becomes unnecessary,
871 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800872 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700873 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700874 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700875 // Preserve existing usage of hardcoded labelMatch table below
876 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700877
Josh Lehan74d9bd92019-10-31 08:51:58 -0700878 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700879 char firstChar =
880 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700881 std::string strScaleFactor =
882 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
883
884 // Preserve existing configs by accepting earlier syntax,
885 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700886 auto findScaleFactor = baseConfig->find(strScaleFactor);
887 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700888 {
889 factor = std::visit(VariantToIntVisitor(),
890 findScaleFactor->second);
891 }
892
Alexander Hansen89be6142025-09-18 15:36:16 +0200893 lg2::debug(
894 "Sensor scaling factor '{FACTOR}' string '{SCALE_FACTOR}'",
895 "FACTOR", factor, "SCALE_FACTOR", strScaleFactor);
Josh Lehan49cfba92019-10-08 16:50:42 -0700896 }
897
Vijay Khemka996bad12019-05-28 15:15:16 -0700898 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530899 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
900 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800901 {
George Liu4155a5a2025-01-24 15:26:27 +0800902 lg2::error("error populating thresholds for '{NAME}'", "NAME",
903 sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800904 }
905
Zev Weiss6b6891c2021-04-22 02:46:21 -0500906 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
907 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800908 {
George Liu4155a5a2025-01-24 15:26:27 +0800909 lg2::error("'{NAME}' is not a recognized sensor type", "NAME",
910 sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800911 continue;
912 }
913
Alexander Hansen89be6142025-09-18 15:36:16 +0200914 lg2::debug("Sensor properties - Name: {NAME}, Scale: {SCALE}, "
915 "Min: {MIN}, Max: {MAX}, Offset: {OFFSET}",
916 "NAME", psuProperty.labelTypeName, "SCALE",
917 psuProperty.sensorScaleFactor, "MIN",
918 psuProperty.minReading, "MAX", psuProperty.maxReading,
919 "OFFSET", psuProperty.sensorOffset);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700920
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800921 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700922 if (customizedName)
923 {
924 if (sensorName.empty())
925 {
926 // Allow selective disabling of an individual sensor,
927 // by customizing its name to an empty string.
George Liu4155a5a2025-01-24 15:26:27 +0800928 lg2::error("Sensor disabled, empty string");
Josh Lehan74d9bd92019-10-31 08:51:58 -0700929 continue;
930 }
931 }
932 else
933 {
934 // Sensor name not customized, do prefix/suffix composition,
935 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800936 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
cchouxe8a14e92023-10-25 23:26:27 +0800937
938 // The labelTypeName of a fan can be:
939 // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
940 if (labelHead == "fan" + nameIndexStr)
941 {
942 sensorName += nameIndexStr;
943 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700944 }
945
Alexander Hansen89be6142025-09-18 15:36:16 +0200946 lg2::debug("Sensor name: {NAME}, path: {PATH}, type: {TYPE}",
947 "NAME", sensorName, "PATH", sensorPathStr, "TYPE",
948 sensorType);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800949 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800950
951 auto& sensor = sensors[sensorName];
952 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700953 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800954 sensor = nullptr;
955 }
956
957 if (sensor != nullptr)
958 {
959 sensor->activate(sensorPathStr, i2cDev);
960 }
961 else
962 {
963 sensors[sensorName] = std::make_shared<PSUSensor>(
964 sensorPathStr, sensorType, objectServer, dbusConnection, io,
965 sensorName, std::move(sensorThresholds), *interfacePath,
966 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800967 psuProperty.maxReading, psuProperty.minReading,
968 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800969 pollRate, i2cDev);
970 sensors[sensorName]->setupRead();
971 ++numCreated;
Alexander Hansen89be6142025-09-18 15:36:16 +0200972 lg2::debug("Created '{NUM}' sensors so far", "NUM", numCreated);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700973 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800974 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800975
Joseph Fub844e972023-03-25 10:24:30 +0800976 if (devType == DevTypes::HWMON)
977 {
978 // OperationalStatus event
979 combineEvents[*psuName + "OperationalStatus"] = nullptr;
980 combineEvents[*psuName + "OperationalStatus"] =
981 std::make_unique<PSUCombineEvent>(
982 objectServer, dbusConnection, io, *psuName, readState,
983 eventPathList, groupEventPathList, "OperationalStatus",
984 pollRate);
985 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800986 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700987
Alexander Hansen89be6142025-09-18 15:36:16 +0200988 lg2::debug("Created total of '{NUM}' sensors", "NUM", numCreated);
Matt Simmering6747eba2023-01-18 16:03:55 -0800989}
Cheng C Yang209ec562019-03-12 16:37:44 +0800990
Patrick Williams556e04b2025-02-01 08:22:22 -0500991static void getPresentCpus(
992 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Matt Simmering39fadb92023-05-04 21:47:29 -0700993{
994 static const int depth = 2;
995 static const int numKeys = 1;
996 GetSubTreeType cpuSubTree;
997
998 try
999 {
1000 auto getItems = dbusConnection->new_method_call(
1001 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
1002 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
1003 std::array<const char*, numKeys>{
1004 "xyz.openbmc_project.Inventory.Item"});
1005 auto getItemsResp = dbusConnection->call(getItems);
1006 getItemsResp.read(cpuSubTree);
1007 }
1008 catch (sdbusplus::exception_t& e)
1009 {
George Liu4155a5a2025-01-24 15:26:27 +08001010 lg2::error("error getting inventory item subtree: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001011 return;
1012 }
1013
1014 for (const auto& [path, objDict] : cpuSubTree)
1015 {
1016 auto obj = sdbusplus::message::object_path(path).filename();
Patrick Rudolph457715b2024-04-10 14:51:35 +02001017 boost::to_lower(obj);
1018
Matt Simmering39fadb92023-05-04 21:47:29 -07001019 if (!obj.starts_with("cpu") || objDict.empty())
1020 {
1021 continue;
1022 }
1023 const std::string& owner = objDict.begin()->first;
1024
1025 std::variant<bool> respValue;
1026 try
1027 {
1028 auto getPresence = dbusConnection->new_method_call(
1029 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1030 "Get");
1031 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1032 auto resp = dbusConnection->call(getPresence);
1033 resp.read(respValue);
1034 }
1035 catch (sdbusplus::exception_t& e)
1036 {
George Liu4155a5a2025-01-24 15:26:27 +08001037 lg2::error("Error in getting CPU presence: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001038 continue;
1039 }
1040
1041 auto* present = std::get_if<bool>(&respValue);
1042 if (present != nullptr && *present)
1043 {
1044 int cpuIndex = 0;
1045 try
1046 {
Patrick Rudolph457715b2024-04-10 14:51:35 +02001047 cpuIndex = std::stoi(obj.substr(obj.size() - 1));
Matt Simmering39fadb92023-05-04 21:47:29 -07001048 }
1049 catch (const std::exception& e)
1050 {
George Liu4155a5a2025-01-24 15:26:27 +08001051 lg2::error("Error converting CPU index: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001052 continue;
1053 }
Patrick Rudolph457715b2024-04-10 14:51:35 +02001054 cpuPresence[cpuIndex] = *present;
Matt Simmering39fadb92023-05-04 21:47:29 -07001055 }
1056 }
1057}
1058
Zhikui Ren23c96e72020-11-05 22:32:28 -08001059void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001060 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001061 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1062 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001063 sensorsChanged,
1064 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001065{
1066 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001067 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1068 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001069 createSensorsCallback(io, objectServer, dbusConnection,
1070 sensorConfigs, sensorsChanged, activateOnly);
1071 });
Zane Li02bb6da2025-03-07 14:56:37 +08001072 std::vector<std::string> types;
1073 types.reserve(sensorTypes.size());
Matt Simmering786efb82023-01-18 14:09:21 -08001074 for (const auto& [type, dt] : sensorTypes)
1075 {
1076 types.push_back(type);
1077 }
1078 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001079}
1080
Ed Tanous201a1012024-04-03 18:07:28 -07001081void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001082{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001083 sensorTable = {{"power", sensor_paths::unitWatts},
1084 {"curr", sensor_paths::unitAmperes},
1085 {"temp", sensor_paths::unitDegreesC},
1086 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001087 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001088 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001089
Jeff Line41d52f2021-04-07 19:38:51 +08001090 labelMatch = {
1091 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001092 {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
1093 {"power", 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)},
1096 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001097 {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Ian Chienc82a4092024-07-24 10:52:59 +08001098 {"voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001099 {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001100 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001101 {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001102 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001103 {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
1104 {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
1105 {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1106 {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
1107 {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1108 {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001109
1110 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1111 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1112
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001113 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1114 {"Failure", {"in2_alarm"}},
1115 {"ACLost", {"in1_beep"}},
1116 {"ConfigureError", {"in1_fault"}}};
1117
Joseph Fub844e972023-03-25 10:24:30 +08001118 devParamMap = {
1119 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1120 {DevTypes::IIO,
1121 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001122}
1123
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001124static void powerStateChanged(
1125 PowerState type, bool newState,
1126 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1127 sensors,
1128 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1129 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1130{
1131 if (newState)
1132 {
1133 createSensors(io, objectServer, dbusConnection, nullptr, true);
1134 }
1135 else
1136 {
1137 for (auto& [path, sensor] : sensors)
1138 {
1139 if (sensor != nullptr && sensor->readState == type)
1140 {
1141 sensor->deactivate();
1142 }
1143 }
1144 }
1145}
1146
James Feistb6c0b912019-07-09 12:21:44 -07001147int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001148{
Ed Tanous1f978632023-02-28 18:16:39 -08001149 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001150 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1151
Ed Tanous14ed5e92022-07-12 15:50:23 -07001152 sdbusplus::asio::object_server objectServer(systemBus, true);
1153 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001154 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001155 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001156 auto sensorsChanged =
1157 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001158
Cheng C Yang916360b2019-05-07 18:47:16 +08001159 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001160
Patrick Williams2aaf7172024-08-16 15:20:40 -04001161 auto powerCallBack = [&io, &objectServer,
1162 &systemBus](PowerState type, bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001163 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1164 };
1165
1166 setupPowerMatchCallback(systemBus, powerCallBack);
1167
1168 boost::asio::post(io, [&]() {
1169 createSensors(io, objectServer, systemBus, nullptr, false);
1170 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001171 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001172 std::function<void(sdbusplus::message_t&)> eventHandler =
1173 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001174 if (message.is_method_error())
Cheng C Yang209ec562019-03-12 16:37:44 +08001175 {
George Liu4155a5a2025-01-24 15:26:27 +08001176 lg2::error("callback method error");
Cheng C Yang209ec562019-03-12 16:37:44 +08001177 return;
1178 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001179 sensorsChanged->insert(message.get_path());
1180 filterTimer.expires_after(std::chrono::seconds(3));
1181 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1182 if (ec == boost::asio::error::operation_aborted)
1183 {
1184 return;
1185 }
1186 if (ec)
1187 {
George Liu4155a5a2025-01-24 15:26:27 +08001188 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -04001189 }
1190 createSensors(io, objectServer, systemBus, sensorsChanged,
1191 false);
1192 });
1193 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001194
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001195 boost::asio::steady_timer cpuFilterTimer(io);
1196 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1197 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001198 std::string path = message.get_path();
1199 boost::to_lower(path);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001200
Patrick Williams2aaf7172024-08-16 15:20:40 -04001201 sdbusplus::message::object_path cpuPath(path);
1202 std::string cpuName = cpuPath.filename();
1203 if (!cpuName.starts_with("cpu"))
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001204 {
1205 return;
1206 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001207 size_t index = 0;
1208 try
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001209 {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001210 index = std::stoi(path.substr(path.size() - 1));
1211 }
1212 catch (const std::invalid_argument&)
1213 {
George Liu4155a5a2025-01-24 15:26:27 +08001214 lg2::error("Found invalid path: '{PATH}'", "PATH", path);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001215 return;
1216 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001217
1218 std::string objectName;
1219 boost::container::flat_map<std::string, std::variant<bool>> values;
1220 message.read(objectName, values);
1221 auto findPresence = values.find("Present");
Vikash Chandolaf2fc1b32024-08-29 15:59:27 +05301222 if (findPresence == values.end())
1223 {
1224 return;
1225 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001226 try
1227 {
1228 cpuPresence[index] = std::get<bool>(findPresence->second);
1229 }
1230 catch (const std::bad_variant_access& err)
1231 {
1232 return;
1233 }
1234
1235 if (!cpuPresence[index])
1236 {
1237 return;
1238 }
1239 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1240 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1241 if (ec == boost::asio::error::operation_aborted)
1242 {
1243 return;
1244 }
1245 if (ec)
1246 {
George Liu4155a5a2025-01-24 15:26:27 +08001247 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -04001248 return;
1249 }
1250 createSensors(io, objectServer, systemBus, nullptr, false);
1251 });
1252 };
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001253
Zev Weiss214d9712022-08-12 12:54:31 -07001254 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1255 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001256
1257 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1258 static_cast<sdbusplus::bus_t&>(*systemBus),
1259 "type='signal',member='PropertiesChanged',path_namespace='" +
1260 std::string(cpuInventoryPath) +
1261 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1262 cpuPresenceHandler));
1263
Matt Simmering39fadb92023-05-04 21:47:29 -07001264 getPresentCpus(systemBus);
1265
Bruce Lee1263c3d2021-06-04 15:16:33 +08001266 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001267 io.run();
1268}