blob: 41072bf088cc385afda033c1c56d30a67dced538 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070064static constexpr bool debug = false;
Chau Lyc4ddf642024-07-09 05:01:06 +000065static std::regex i2cDevRegex(R"((\/i2c\-\d+\/\d+-[a-fA-F0-9]{4,4})(\/|$))");
Josh Lehan49cfba92019-10-08 16:50:42 -070066
Matt Simmering786efb82023-01-18 14:09:21 -080067static const I2CDeviceTypeMap sensorTypes{
Delphine CC Chiu436c5bd2023-09-22 09:47:43 +080068 {"ADC128D818", I2CDeviceType{"adc128d818", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080069 {"ADM1266", I2CDeviceType{"adm1266", true}},
70 {"ADM1272", I2CDeviceType{"adm1272", true}},
71 {"ADM1275", I2CDeviceType{"adm1275", true}},
72 {"ADM1278", I2CDeviceType{"adm1278", true}},
73 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080074 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080075 {"ADS7830", I2CDeviceType{"ads7830", true}},
76 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
77 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020078 {"cffps", I2CDeviceType{"cffps", true}},
79 {"cffps1", I2CDeviceType{"cffps", true}},
80 {"cffps2", I2CDeviceType{"cffps", true}},
81 {"cffps3", I2CDeviceType{"cffps", true}},
Andrew Geisslerb2392ef2024-12-10 09:39:19 -060082 {"CRPS185", I2CDeviceType{"crps185", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080083 {"DPS800", I2CDeviceType{"dps800", true}},
84 {"INA219", I2CDeviceType{"ina219", true}},
85 {"INA230", I2CDeviceType{"ina230", true}},
Potin Lai5674ac92023-11-20 14:33:56 +080086 {"INA238", I2CDeviceType{"ina238", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080087 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
George Liu43b074d2025-04-09 08:52:19 +080088 {"IR35221", I2CDeviceType{"ir35221", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080089 {"IR38060", I2CDeviceType{"ir38060", true}},
90 {"IR38164", I2CDeviceType{"ir38164", true}},
91 {"IR38263", I2CDeviceType{"ir38263", true}},
Yikai Tsai5b74aba2024-09-06 13:58:28 +080092 {"ISL28022", I2CDeviceType{"isl28022", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080093 {"ISL68137", I2CDeviceType{"isl68137", true}},
94 {"ISL68220", I2CDeviceType{"isl68220", true}},
95 {"ISL68223", I2CDeviceType{"isl68223", true}},
96 {"ISL69225", I2CDeviceType{"isl69225", true}},
97 {"ISL69243", I2CDeviceType{"isl69243", true}},
98 {"ISL69260", I2CDeviceType{"isl69260", true}},
99 {"LM25066", I2CDeviceType{"lm25066", true}},
Yi-Shum77e87692023-11-30 10:27:45 +0800100 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Potin Lai43847a82024-02-27 16:29:45 +0800101 {"LTC4286", I2CDeviceType{"ltc4286", true}},
102 {"LTC4287", I2CDeviceType{"ltc4287", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +0200103 {"MAX5970", I2CDeviceType{"max5970", true}},
Joseph Fu15a39df2023-12-27 09:45:49 +0800104 {"MAX11607", I2CDeviceType{"max11607", false}},
Ian Chienc82a4092024-07-24 10:52:59 +0800105 {"MAX11615", I2CDeviceType{"max11615", false}},
Allen.Wang8c13c282024-07-03 09:42:54 +0800106 {"MAX11617", I2CDeviceType{"max11617", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800107 {"MAX16601", I2CDeviceType{"max16601", true}},
108 {"MAX20710", I2CDeviceType{"max20710", true}},
109 {"MAX20730", I2CDeviceType{"max20730", true}},
110 {"MAX20734", I2CDeviceType{"max20734", true}},
111 {"MAX20796", I2CDeviceType{"max20796", true}},
112 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800113 {"MP2856", I2CDeviceType{"mp2856", true}},
114 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800115 {"MP2971", I2CDeviceType{"mp2971", true}},
116 {"MP2973", I2CDeviceType{"mp2973", true}},
117 {"MP2975", I2CDeviceType{"mp2975", true}},
118 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800119 {"MP5990", I2CDeviceType{"mp5990", true}},
Jeff Lind8e48df2024-06-07 09:06:56 +0800120 {"MPQ8785", I2CDeviceType{"mpq8785", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800121 {"NCP4200", I2CDeviceType{"ncp4200", true}},
122 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
123 {"pmbus", I2CDeviceType{"pmbus", true}},
124 {"PXE1610", I2CDeviceType{"pxe1610", true}},
125 {"RAA228000", I2CDeviceType{"raa228000", true}},
Potin Lai6e6618f2024-05-16 15:02:56 +0800126 {"RAA228004", I2CDeviceType{"raa228004", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800127 {"RAA228228", I2CDeviceType{"raa228228", true}},
128 {"RAA228620", I2CDeviceType{"raa228620", true}},
129 {"RAA229001", I2CDeviceType{"raa229001", true}},
130 {"RAA229004", I2CDeviceType{"raa229004", true}},
131 {"RAA229126", I2CDeviceType{"raa229126", true}},
Ian Chien982d99f2024-08-20 17:16:56 +0800132 {"RTQ6056", I2CDeviceType{"rtq6056", false}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800133 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Chau Lyc4ddf642024-07-09 05:01:06 +0000134 {"smpro_hwmon", I2CDeviceType{"smpro", false}},
Rush Chena9ea8e52025-01-02 11:28:57 +0800135 {"SY24655", I2CDeviceType{"sy24655", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200136 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800137 {"TPS53679", I2CDeviceType{"tps53679", true}},
138 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800139 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800140 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
141 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800142 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800143};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700144
Joseph Fub844e972023-03-25 10:24:30 +0800145enum class DevTypes
146{
147 Unknown = 0,
148 HWMON,
149 IIO
150};
151
152struct DevParams
153{
154 unsigned int matchIndex = 0;
155 std::string matchRegEx;
156 std::string nameRegEx;
157};
158
Yong Libf8b1da2020-04-15 16:32:50 +0800159static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800160 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800161static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
162 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800163static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
164 pwmSensors;
165static boost::container::flat_map<std::string, std::string> sensorTable;
166static boost::container::flat_map<std::string, PSUProperty> labelMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800167static EventPathList eventMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800168static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800169
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800170static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800171static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700172
Cheng C Yang58b2b532019-05-31 00:19:45 +0800173// Function CheckEvent will check each attribute from eventMatch table in the
174// sysfs. If the attributes exists in sysfs, then store the complete path
175// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800176void checkEvent(const std::string& directory, const EventPathList& eventMatch,
177 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800178{
179 for (const auto& match : eventMatch)
180 {
181 const std::vector<std::string>& eventAttrs = match.second;
182 const std::string& eventName = match.first;
183 for (const auto& eventAttr : eventAttrs)
184 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700185 std::string eventPath = directory;
186 eventPath += "/";
187 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800188
189 std::ifstream eventFile(eventPath);
190 if (!eventFile.good())
191 {
192 continue;
193 }
194
195 eventPathList[eventName].push_back(eventPath);
196 }
197 }
198}
199
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800200// Check Group Events which contains more than one targets in each combine
201// events.
George Liu5b3542e2023-10-31 17:27:12 +0800202void checkGroupEvent(const std::string& directory,
George Liu5b3542e2023-10-31 17:27:12 +0800203 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800204{
cchouxe8a14e92023-10-25 23:26:27 +0800205 EventPathList pathList;
Ed Tanous2e466962025-01-30 10:59:49 -0800206 std::vector<std::filesystem::path> eventPaths;
207 if (!findFiles(std::filesystem::path(directory), R"(fan\d+_(alarm|fault))",
208 eventPaths))
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800209 {
cchouxe8a14e92023-10-25 23:26:27 +0800210 return;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800211 }
cchouxe8a14e92023-10-25 23:26:27 +0800212
213 for (const auto& eventPath : eventPaths)
214 {
215 std::string attrName = eventPath.filename();
216 pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
217 }
218 groupEventPathList["FanFault"] = pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800219}
220
Cheng C Yang58b2b532019-05-31 00:19:45 +0800221// Function checkEventLimits will check all the psu related xxx_input attributes
222// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
223// xxx_min_alarm exist, then store the existing paths of the alarm attributes
224// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800225void checkEventLimits(const std::string& sensorPathStr,
226 const EventPathList& limitEventMatch,
227 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800228{
Lei YUa2c7cea2020-12-23 14:07:28 +0800229 auto attributePartPos = sensorPathStr.find_last_of('_');
230 if (attributePartPos == std::string::npos)
231 {
232 // There is no '_' in the string, skip it
233 return;
234 }
235 auto attributePart =
236 std::string_view(sensorPathStr).substr(attributePartPos + 1);
237 if (attributePart != "input")
238 {
239 // If the sensor is not xxx_input, skip it
240 return;
241 }
242
243 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800244 for (const auto& limitMatch : limitEventMatch)
245 {
246 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
247 const std::string& eventName = limitMatch.first;
248 for (const auto& limitEventAttr : limitEventAttrs)
249 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800250 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800251 std::ifstream eventFile(limitEventPath);
252 if (!eventFile.good())
253 {
254 continue;
255 }
256 eventPathList[eventName].push_back(limitEventPath);
257 }
258 }
259}
Cheng C Yang916360b2019-05-07 18:47:16 +0800260
Patrick Williams2aaf7172024-08-16 15:20:40 -0400261static void checkPWMSensor(
Ed Tanous2e466962025-01-30 10:59:49 -0800262 const std::filesystem::path& sensorPath, std::string& labelHead,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400263 const std::string& interfacePath,
264 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
265 sdbusplus::asio::object_server& objectServer, const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800266{
cchouxe8a14e92023-10-25 23:26:27 +0800267 if (!labelHead.starts_with("fan"))
Cheng C Yang916360b2019-05-07 18:47:16 +0800268 {
cchouxe8a14e92023-10-25 23:26:27 +0800269 return;
Cheng C Yang916360b2019-05-07 18:47:16 +0800270 }
cchouxe8a14e92023-10-25 23:26:27 +0800271 std::string labelHeadIndex = labelHead.substr(3);
272
273 const std::string& sensorPathStr = sensorPath.string();
Patrick Williams2aaf7172024-08-16 15:20:40 -0400274 const std::string& pwmPathStr =
275 boost::replace_all_copy(sensorPathStr, "input", "target");
cchouxe8a14e92023-10-25 23:26:27 +0800276 std::ifstream pwmFile(pwmPathStr);
277 if (!pwmFile.good())
278 {
279 return;
280 }
281
282 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
283 if (findPWMSensor != pwmSensors.end())
284 {
285 return;
286 }
287
288 std::string name = "Pwm_";
289 name += psuName;
290 name += "_Fan_";
291 name += labelHeadIndex;
292
293 std::string objPath = interfacePath;
294 objPath += "_Fan_";
295 objPath += labelHeadIndex;
296
297 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
298 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800299}
300
Zhikui Ren23c96e72020-11-05 22:32:28 -0800301static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800302 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800303 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
304 const ManagedObjectType& sensorConfigs,
305 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800306 sensorsChanged,
307 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800308{
Josh Lehan49cfba92019-10-08 16:50:42 -0700309 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800310 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800311
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800312 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
313
Ed Tanous2e466962025-01-30 10:59:49 -0800314 std::vector<std::filesystem::path> pmbusPaths;
315 findFiles(std::filesystem::path("/sys/bus/iio/devices"), "name",
316 pmbusPaths);
317 findFiles(std::filesystem::path("/sys/class/hwmon"), "name", pmbusPaths);
Joseph Fub844e972023-03-25 10:24:30 +0800318 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800319 {
George Liu4155a5a2025-01-24 15:26:27 +0800320 lg2::error("No PSU sensors in system");
Cheng C Yang209ec562019-03-12 16:37:44 +0800321 return;
322 }
323
324 boost::container::flat_set<std::string> directories;
325 for (const auto& pmbusPath : pmbusPaths)
326 {
George Liu5b3542e2023-10-31 17:27:12 +0800327 EventPathList eventPathList;
328 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800329
330 std::ifstream nameFile(pmbusPath);
331 if (!nameFile.good())
332 {
George Liu4155a5a2025-01-24 15:26:27 +0800333 lg2::error("Failure finding '{PATH}'", "PATH", pmbusPath);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800334 continue;
335 }
336
337 std::string pmbusName;
338 std::getline(nameFile, pmbusName);
339 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700340
Patrick Williams80c68052025-05-13 23:31:49 -0400341 if (!sensorTypes.contains(pmbusName))
Cheng C Yang58b2b532019-05-31 00:19:45 +0800342 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700343 // To avoid this error message, add your driver name to
344 // the pmbusNames vector at the top of this file.
George Liu4155a5a2025-01-24 15:26:27 +0800345 lg2::error("'{NAME}' not found in sensor whitelist", "NAME",
346 pmbusName);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800347 continue;
348 }
349
Cheng C Yang209ec562019-03-12 16:37:44 +0800350 auto directory = pmbusPath.parent_path();
351
352 auto ret = directories.insert(directory.string());
353 if (!ret.second)
354 {
George Liu4155a5a2025-01-24 15:26:27 +0800355 lg2::error("Duplicate path: '{PATH}'", "PATH", directory);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800356 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800357 }
358
Joseph Fub844e972023-03-25 10:24:30 +0800359 DevTypes devType = DevTypes::HWMON;
360 std::string deviceName;
361 if (directory.parent_path() == "/sys/class/hwmon")
362 {
Ed Tanous2e466962025-01-30 10:59:49 -0800363 std::string devicePath =
364 std::filesystem::canonical(directory / "device");
Chau Lyc4ddf642024-07-09 05:01:06 +0000365 std::smatch match;
366 // Find /i2c-<bus>/<bus>-<address> match in device path
367 std::regex_search(devicePath, match, i2cDevRegex);
368 if (match.empty())
369 {
George Liu4155a5a2025-01-24 15:26:27 +0800370 lg2::error("Found bad device path: '{PATH}'", "PATH",
371 devicePath);
Chau Lyc4ddf642024-07-09 05:01:06 +0000372 continue;
373 }
374 // Extract <bus>-<address>
375 std::string matchStr = match[1];
376 deviceName = matchStr.substr(matchStr.find_last_of('/') + 1);
Joseph Fub844e972023-03-25 10:24:30 +0800377 }
378 else
379 {
Ed Tanous2e466962025-01-30 10:59:49 -0800380 deviceName =
381 std::filesystem::canonical(directory).parent_path().stem();
Joseph Fub844e972023-03-25 10:24:30 +0800382 devType = DevTypes::IIO;
383 }
384
Cheng C Yang209ec562019-03-12 16:37:44 +0800385 size_t bus = 0;
386 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000387 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800388 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 continue;
390 }
391
Zev Weisse8b97ee2022-08-12 15:23:51 -0700392 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800393 const SensorData* sensorData = nullptr;
394 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800395 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800396 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800397
Zev Weiss741f26e2022-08-12 18:21:02 -0700398 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800399 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700400 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800401 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800402 {
Zev Weiss054aad82022-08-18 01:37:34 -0700403 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800404 if (sensorBase != sensorData->end())
405 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700406 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800407 sensorType = type;
408 break;
409 }
410 }
411 if (baseConfig == nullptr)
412 {
George Liu4155a5a2025-01-24 15:26:27 +0800413 lg2::error("error finding base configuration for '{NAME}'",
414 "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 continue;
416 }
417
Zev Weisse8b97ee2022-08-12 15:23:51 -0700418 auto configBus = baseConfig->find("Bus");
419 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800420
Zev Weisse8b97ee2022-08-12 15:23:51 -0700421 if (configBus == baseConfig->end() ||
422 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800423 {
George Liu4155a5a2025-01-24 15:26:27 +0800424 lg2::error("error finding necessary entry in configuration");
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 continue;
426 }
427
Matt Simmering6747eba2023-01-18 16:03:55 -0800428 const uint64_t* confBus =
429 std::get_if<uint64_t>(&(configBus->second));
430 const uint64_t* confAddr =
431 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800432 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800433 {
George Liu4155a5a2025-01-24 15:26:27 +0800434 lg2::error("Cannot get bus or address, invalid configuration");
Cheng C Yang58b2b532019-05-31 00:19:45 +0800435 continue;
436 }
437
438 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800439 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200440 if constexpr (debug)
441 {
George Liu4155a5a2025-01-24 15:26:27 +0800442 lg2::error(
443 "Configuration skipping '{CONFBUS}'-'{CONFADDR}' because not {BUS}-{ADDR}",
444 "CONFBUS", *confBus, "CONFADDR", *confAddr, "BUS", bus,
445 "ADDR", addr);
Patrick Rudolph9f270872023-10-25 09:13:09 +0200446 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800447 continue;
448 }
449
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800450 std::vector<thresholds::Threshold> confThresholds;
451 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
452 {
George Liu4155a5a2025-01-24 15:26:27 +0800453 lg2::error("error populating total thresholds");
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800454 }
455 thresholdConfSize = confThresholds.size();
456
Zev Weiss741f26e2022-08-12 18:21:02 -0700457 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800458 break;
459 }
460 if (interfacePath == nullptr)
461 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700462 // To avoid this error message, add your export map entry,
463 // from Entity Manager, to sensorTypes at the top of this file.
George Liu4155a5a2025-01-24 15:26:27 +0800464 lg2::error("failed to find match for '{NAME}'", "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800465 continue;
466 }
467
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800468 auto findI2CDev = devices.find(*interfacePath);
469
470 std::shared_ptr<I2CDevice> i2cDev;
471 if (findI2CDev != devices.end())
472 {
473 if (activateOnly && !findI2CDev->second.second)
474 {
475 continue;
476 }
477 i2cDev = findI2CDev->second.first;
478 }
479
Zev Weisse8b97ee2022-08-12 15:23:51 -0700480 auto findPSUName = baseConfig->find("Name");
481 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800482 {
George Liu4155a5a2025-01-24 15:26:27 +0800483 lg2::error("could not determine configuration name for '{NAME}'",
484 "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800485 continue;
486 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800487 const std::string* psuName =
488 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800489 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800490 {
George Liu4155a5a2025-01-24 15:26:27 +0800491 lg2::error("Cannot find psu name, invalid configuration");
Cheng C Yang58b2b532019-05-31 00:19:45 +0800492 continue;
493 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800494
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800495 auto findCPU = baseConfig->find("CPURequired");
496 if (findCPU != baseConfig->end())
497 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500498 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800499 auto presenceFind = cpuPresence.find(index);
500 if (presenceFind == cpuPresence.end() || !presenceFind->second)
501 {
502 continue;
503 }
504 }
505
Zhikui Ren23c96e72020-11-05 22:32:28 -0800506 // on rescans, only update sensors we were signaled by
507 if (!firstScan)
508 {
Zhikui Renda98f092021-11-01 09:41:08 -0700509 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400510 auto it =
511 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
512 [psuNameStr](std::string& s) {
513 return s.ends_with(psuNameStr);
514 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800515
516 if (it == sensorsChanged->end())
517 {
518 continue;
519 }
520 sensorsChanged->erase(it);
521 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800522 checkEvent(directory.string(), eventMatch, eventPathList);
cchouxe8a14e92023-10-25 23:26:27 +0800523 checkGroupEvent(directory.string(), groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800524
Zev Weisse8b97ee2022-08-12 15:23:51 -0700525 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000526
Vijay Khemka996bad12019-05-28 15:15:16 -0700527 /* Check if there are more sensors in the same interface */
528 int i = 1;
529 std::vector<std::string> psuNames;
530 do
531 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700532 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700533 psuNames.push_back(
534 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700535 findPSUName = baseConfig->find("Name" + std::to_string(i++));
536 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700537
Ed Tanous2e466962025-01-30 10:59:49 -0800538 std::vector<std::filesystem::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800539 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
540 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800541 {
George Liu4155a5a2025-01-24 15:26:27 +0800542 lg2::error("No PSU non-label sensor in PSU");
Cheng C Yang209ec562019-03-12 16:37:44 +0800543 continue;
544 }
545
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530546 /* read max value in sysfs for in, curr, power, temp, ... */
547 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
548 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700549 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530550 {
George Liu4155a5a2025-01-24 15:26:27 +0800551 lg2::error("No max name in PSU");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530552 }
553 }
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 {
621 if constexpr (debug)
622 {
George Liu4155a5a2025-01-24 15:26:27 +0800623 lg2::error(
624 "Input file '{PATH}' has no corresponding label file",
625 "PATH", sensorPath.string());
Joseph Fub844e972023-03-25 10:24:30 +0800626 }
627 // hwmon *_input filename with number:
628 // temp1, temp2, temp3, ...
Patrick Williams2aaf7172024-08-16 15:20:40 -0400629 labelHead =
630 sensorNameStr.substr(0, sensorNameStr.find('_'));
Joseph Fub844e972023-03-25 10:24:30 +0800631 }
632 else
633 {
634 std::string label;
635 std::getline(labelFile, label);
636 labelFile.close();
637 auto findSensor = sensors.find(label);
638 if (findSensor != sensors.end())
639 {
640 continue;
641 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800642
Joseph Fub844e972023-03-25 10:24:30 +0800643 // hwmon corresponding *_label file contents:
644 // vin1, vout1, ...
645 labelHead = label.substr(0, label.find(' '));
646 }
647
648 /* append "max" for labelMatch */
649 if (maxLabel)
650 {
651 labelHead.insert(0, "max");
652 }
653
Andrew Geissler66783882025-04-02 11:50:33 -0500654 // Don't add PWM sensors if it's not in label list
655 if (!findLabels.empty())
656 {
657 /* Check if this labelHead is enabled in config file */
658 if (std::find(findLabels.begin(), findLabels.end(),
659 labelHead) == findLabels.end())
660 {
661 if constexpr (debug)
662 {
663 lg2::error(
664 "could not find {LABEL} in the Labels list",
665 "LABEL", labelHead);
666 }
667 continue;
668 }
669 }
Joseph Fub844e972023-03-25 10:24:30 +0800670 checkPWMSensor(sensorPath, labelHead, *interfacePath,
671 dbusConnection, objectServer, psuNames[0]);
672 }
673 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530674 {
Joseph Fub844e972023-03-25 10:24:30 +0800675 auto findIIOHyphen = sensorNameStr.find_last_of('_');
676 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530677 }
678
Ed Tanous8a57ec02020-10-09 12:46:52 -0700679 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700680 {
George Liu4155a5a2025-01-24 15:26:27 +0800681 lg2::error("Sensor type: {NAME}, label: {LABEL}", "NAME",
682 sensorNameSubStr, "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700683 }
684
Vijay Khemka996bad12019-05-28 15:15:16 -0700685 if (!findLabels.empty())
686 {
687 /* Check if this labelHead is enabled in config file */
688 if (std::find(findLabels.begin(), findLabels.end(),
689 labelHead) == findLabels.end())
690 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700691 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800692 {
George Liu4155a5a2025-01-24 15:26:27 +0800693 lg2::error(
694 "could not find '{LABEL}' in the Labels list",
695 "LABEL", labelHead);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800696 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700697 continue;
698 }
699 }
Jayaprakash Mutyalaa205c6f2025-02-13 06:09:04 +0000700 auto it = std::find_if(labelHead.begin(), labelHead.end(),
701 static_cast<int (*)(int)>(std::isdigit));
702 std::string_view labelHeadView(
703 labelHead.data(), std::distance(labelHead.begin(), it));
704 auto findProperty =
705 labelMatch.find(static_cast<std::string>(labelHeadView));
Cheng C Yange50345b2019-04-02 17:26:15 +0800706 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800707 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700708 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700709 {
George Liu4155a5a2025-01-24 15:26:27 +0800710 lg2::error(
711 "Could not find matching default property for '{LABEL}'",
712 "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700713 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800714 continue;
715 }
716
Josh Lehan74d9bd92019-10-31 08:51:58 -0700717 // Protect the hardcoded labelMatch list from changes,
718 // by making a copy and modifying that instead.
719 // Avoid bleedthrough of one device's customizations to
720 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800721 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700722
723 // Use label head as prefix for reading from config file,
724 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
725 std::string keyName = labelHead + "_Name";
726 std::string keyScale = labelHead + "_Scale";
727 std::string keyMin = labelHead + "_Min";
728 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800729 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800730 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700731
732 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700733 auto findCustomName = baseConfig->find(keyName);
734 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700735 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700736 try
737 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800738 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700739 VariantToStringVisitor(), findCustomName->second);
740 }
Patrick Williams26601e82021-10-06 12:43:25 -0500741 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700742 {
George Liu4155a5a2025-01-24 15:26:27 +0800743 lg2::error("Unable to parse '{NAME}'", "NAME", keyName);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700744 continue;
745 }
746
747 // All strings are valid, including empty string
748 customizedName = true;
749 }
750
751 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700752 auto findCustomScale = baseConfig->find(keyScale);
753 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700754 {
755 try
756 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800757 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700758 VariantToUnsignedIntVisitor(), findCustomScale->second);
759 }
Patrick Williams26601e82021-10-06 12:43:25 -0500760 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700761 {
George Liu4155a5a2025-01-24 15:26:27 +0800762 lg2::error("Unable to parse '{SCALE}'", "SCALE", keyScale);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700763 continue;
764 }
765
766 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800767 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700768 {
769 customizedScale = true;
770 }
771 else
772 {
George Liu4155a5a2025-01-24 15:26:27 +0800773 lg2::error("Unable to accept '{SCALE}'", "SCALE", keyScale);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700774 continue;
775 }
776 }
777
Zev Weisse8b97ee2022-08-12 15:23:51 -0700778 auto findCustomMin = baseConfig->find(keyMin);
779 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700780 {
781 try
782 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800783 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700784 VariantToDoubleVisitor(), findCustomMin->second);
785 }
Patrick Williams26601e82021-10-06 12:43:25 -0500786 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700787 {
George Liu4155a5a2025-01-24 15:26:27 +0800788 lg2::error("Unable to parse '{MIN}'", "MIN", keyMin);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700789 continue;
790 }
791 }
792
Zev Weisse8b97ee2022-08-12 15:23:51 -0700793 auto findCustomMax = baseConfig->find(keyMax);
794 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700795 {
796 try
797 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800798 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700799 VariantToDoubleVisitor(), findCustomMax->second);
800 }
Patrick Williams26601e82021-10-06 12:43:25 -0500801 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700802 {
George Liu4155a5a2025-01-24 15:26:27 +0800803 lg2::error("Unable to parse '{MAX}'", "MAX", keyMax);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700804 continue;
805 }
806 }
807
Zev Weisse8b97ee2022-08-12 15:23:51 -0700808 auto findCustomOffset = baseConfig->find(keyOffset);
809 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800810 {
811 try
812 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800813 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800814 VariantToDoubleVisitor(), findCustomOffset->second);
815 }
Patrick Williams26601e82021-10-06 12:43:25 -0500816 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800817 {
George Liu4155a5a2025-01-24 15:26:27 +0800818 lg2::error("Unable to parse '{OFFSET}'", "OFFSET",
819 keyOffset);
Jeff Line41d52f2021-04-07 19:38:51 +0800820 continue;
821 }
822 }
823
Lotus Xucb5af732021-09-10 15:18:50 +0800824 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700825 auto findPowerState = baseConfig->find(keyPowerState);
826 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800827 {
828 std::string powerState = std::visit(VariantToStringVisitor(),
829 findPowerState->second);
830 setReadState(powerState, readState);
831 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800832 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700833 {
George Liu4155a5a2025-01-24 15:26:27 +0800834 lg2::error("Min must be less than Max");
Josh Lehan74d9bd92019-10-31 08:51:58 -0700835 continue;
836 }
837
838 // If the sensor name is being customized by config file,
839 // then prefix/suffix composition becomes not necessary,
840 // and in fact not wanted, because it gets in the way.
841 std::string psuNameFromIndex;
cchouxe8a14e92023-10-25 23:26:27 +0800842 std::string nameIndexStr = "1";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 if (!customizedName)
844 {
845 /* Find out sensor name index for this label */
846 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500847 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700848 if (std::regex_search(labelHead, matches, rgx))
849 {
cchouxe8a14e92023-10-25 23:26:27 +0800850 nameIndexStr = matches[1];
851 nameIndex = std::stoi(nameIndexStr);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700852
853 // Decrement to preserve alignment, because hwmon
854 // human-readable filenames and labels use 1-based
855 // numbering, but the "Name", "Name1", "Name2", etc. naming
856 // convention (the psuNames vector) uses 0-based numbering.
857 if (nameIndex > 0)
858 {
859 --nameIndex;
860 }
861 }
862 else
863 {
864 nameIndex = 0;
865 }
866
867 if (psuNames.size() <= nameIndex)
868 {
George Liu4155a5a2025-01-24 15:26:27 +0800869 lg2::error("Could not pair '{LABEL}' with a Name field",
870 "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700871 continue;
872 }
873
874 psuNameFromIndex = psuNames[nameIndex];
875
Ed Tanous8a57ec02020-10-09 12:46:52 -0700876 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700877 {
George Liu4155a5a2025-01-24 15:26:27 +0800878 lg2::error(
879 "'{LABEL}' paired with '{NAME}' at index '{INDEX}'",
880 "LABEL", labelHead, "NAME", psuNameFromIndex, "INDEX",
881 nameIndex);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700882 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700883 }
884
Joseph Fub844e972023-03-25 10:24:30 +0800885 if (devType == DevTypes::HWMON)
886 {
887 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
888 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800889
Josh Lehan74d9bd92019-10-31 08:51:58 -0700890 // Similarly, if sensor scaling factor is being customized,
891 // then the below power-of-10 constraint becomes unnecessary,
892 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800893 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700894 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700895 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700896 // Preserve existing usage of hardcoded labelMatch table below
897 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700898
Josh Lehan74d9bd92019-10-31 08:51:58 -0700899 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700900 char firstChar =
901 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700902 std::string strScaleFactor =
903 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
904
905 // Preserve existing configs by accepting earlier syntax,
906 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700907 auto findScaleFactor = baseConfig->find(strScaleFactor);
908 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700909 {
910 factor = std::visit(VariantToIntVisitor(),
911 findScaleFactor->second);
912 }
913
Ed Tanous8a57ec02020-10-09 12:46:52 -0700914 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700915 {
George Liu4155a5a2025-01-24 15:26:27 +0800916 lg2::error(
917 "Sensor scaling factor '{FACTOR}' string '{SCALE_FACTOR}'",
918 "FACTOR", factor, "SCALE_FACTOR", strScaleFactor);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700919 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700920 }
921
Vijay Khemka996bad12019-05-28 15:15:16 -0700922 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530923 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
924 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800925 {
George Liu4155a5a2025-01-24 15:26:27 +0800926 lg2::error("error populating thresholds for '{NAME}'", "NAME",
927 sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800928 }
929
Zev Weiss6b6891c2021-04-22 02:46:21 -0500930 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
931 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800932 {
George Liu4155a5a2025-01-24 15:26:27 +0800933 lg2::error("'{NAME}' is not a recognized sensor type", "NAME",
934 sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800935 continue;
936 }
937
Ed Tanous8a57ec02020-10-09 12:46:52 -0700938 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700939 {
George Liu4155a5a2025-01-24 15:26:27 +0800940 lg2::error("Sensor properties - Name: {NAME}, Scale: {SCALE}, "
941 "Min: {MIN}, Max: {MAX}, Offset: {OFFSET}",
942 "NAME", psuProperty.labelTypeName, "SCALE",
943 psuProperty.sensorScaleFactor, "MIN",
944 psuProperty.minReading, "MAX",
945 psuProperty.maxReading, "OFFSET",
946 psuProperty.sensorOffset);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700947 }
948
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800949 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700950 if (customizedName)
951 {
952 if (sensorName.empty())
953 {
954 // Allow selective disabling of an individual sensor,
955 // by customizing its name to an empty string.
George Liu4155a5a2025-01-24 15:26:27 +0800956 lg2::error("Sensor disabled, empty string");
Josh Lehan74d9bd92019-10-31 08:51:58 -0700957 continue;
958 }
959 }
960 else
961 {
962 // Sensor name not customized, do prefix/suffix composition,
963 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800964 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
cchouxe8a14e92023-10-25 23:26:27 +0800965
966 // The labelTypeName of a fan can be:
967 // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
968 if (labelHead == "fan" + nameIndexStr)
969 {
970 sensorName += nameIndexStr;
971 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700972 }
973
Ed Tanous8a57ec02020-10-09 12:46:52 -0700974 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700975 {
George Liu4155a5a2025-01-24 15:26:27 +0800976 lg2::error("Sensor name: {NAME}, path: {PATH}, type: {TYPE}",
977 "NAME", sensorName, "PATH", sensorPathStr, "TYPE",
978 sensorType);
Josh Lehan49cfba92019-10-08 16:50:42 -0700979 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800980 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800981
982 auto& sensor = sensors[sensorName];
983 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700984 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800985 sensor = nullptr;
986 }
987
988 if (sensor != nullptr)
989 {
990 sensor->activate(sensorPathStr, i2cDev);
991 }
992 else
993 {
994 sensors[sensorName] = std::make_shared<PSUSensor>(
995 sensorPathStr, sensorType, objectServer, dbusConnection, io,
996 sensorName, std::move(sensorThresholds), *interfacePath,
997 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800998 psuProperty.maxReading, psuProperty.minReading,
999 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001000 pollRate, i2cDev);
1001 sensors[sensorName]->setupRead();
1002 ++numCreated;
1003 if constexpr (debug)
1004 {
George Liu4155a5a2025-01-24 15:26:27 +08001005 lg2::error("Created '{NUM}' sensors so far", "NUM",
1006 numCreated);
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001007 }
Josh Lehan74d9bd92019-10-31 08:51:58 -07001008 }
Cheng C Yang209ec562019-03-12 16:37:44 +08001009 }
Cheng C Yang58b2b532019-05-31 00:19:45 +08001010
Joseph Fub844e972023-03-25 10:24:30 +08001011 if (devType == DevTypes::HWMON)
1012 {
1013 // OperationalStatus event
1014 combineEvents[*psuName + "OperationalStatus"] = nullptr;
1015 combineEvents[*psuName + "OperationalStatus"] =
1016 std::make_unique<PSUCombineEvent>(
1017 objectServer, dbusConnection, io, *psuName, readState,
1018 eventPathList, groupEventPathList, "OperationalStatus",
1019 pollRate);
1020 }
Cheng C Yang209ec562019-03-12 16:37:44 +08001021 }
Josh Lehan49cfba92019-10-08 16:50:42 -07001022
Ed Tanous8a57ec02020-10-09 12:46:52 -07001023 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -07001024 {
George Liu4155a5a2025-01-24 15:26:27 +08001025 lg2::error("Created total of '{NUM}' sensors", "NUM", numCreated);
Josh Lehan49cfba92019-10-08 16:50:42 -07001026 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001027}
Cheng C Yang209ec562019-03-12 16:37:44 +08001028
Patrick Williams556e04b2025-02-01 08:22:22 -05001029static void getPresentCpus(
1030 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Matt Simmering39fadb92023-05-04 21:47:29 -07001031{
1032 static const int depth = 2;
1033 static const int numKeys = 1;
1034 GetSubTreeType cpuSubTree;
1035
1036 try
1037 {
1038 auto getItems = dbusConnection->new_method_call(
1039 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
1040 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
1041 std::array<const char*, numKeys>{
1042 "xyz.openbmc_project.Inventory.Item"});
1043 auto getItemsResp = dbusConnection->call(getItems);
1044 getItemsResp.read(cpuSubTree);
1045 }
1046 catch (sdbusplus::exception_t& e)
1047 {
George Liu4155a5a2025-01-24 15:26:27 +08001048 lg2::error("error getting inventory item subtree: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001049 return;
1050 }
1051
1052 for (const auto& [path, objDict] : cpuSubTree)
1053 {
1054 auto obj = sdbusplus::message::object_path(path).filename();
Patrick Rudolph457715b2024-04-10 14:51:35 +02001055 boost::to_lower(obj);
1056
Matt Simmering39fadb92023-05-04 21:47:29 -07001057 if (!obj.starts_with("cpu") || objDict.empty())
1058 {
1059 continue;
1060 }
1061 const std::string& owner = objDict.begin()->first;
1062
1063 std::variant<bool> respValue;
1064 try
1065 {
1066 auto getPresence = dbusConnection->new_method_call(
1067 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1068 "Get");
1069 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1070 auto resp = dbusConnection->call(getPresence);
1071 resp.read(respValue);
1072 }
1073 catch (sdbusplus::exception_t& e)
1074 {
George Liu4155a5a2025-01-24 15:26:27 +08001075 lg2::error("Error in getting CPU presence: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001076 continue;
1077 }
1078
1079 auto* present = std::get_if<bool>(&respValue);
1080 if (present != nullptr && *present)
1081 {
1082 int cpuIndex = 0;
1083 try
1084 {
Patrick Rudolph457715b2024-04-10 14:51:35 +02001085 cpuIndex = std::stoi(obj.substr(obj.size() - 1));
Matt Simmering39fadb92023-05-04 21:47:29 -07001086 }
1087 catch (const std::exception& e)
1088 {
George Liu4155a5a2025-01-24 15:26:27 +08001089 lg2::error("Error converting CPU index: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001090 continue;
1091 }
Patrick Rudolph457715b2024-04-10 14:51:35 +02001092 cpuPresence[cpuIndex] = *present;
Matt Simmering39fadb92023-05-04 21:47:29 -07001093 }
1094 }
1095}
1096
Zhikui Ren23c96e72020-11-05 22:32:28 -08001097void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001098 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001099 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1100 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001101 sensorsChanged,
1102 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001103{
1104 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001105 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1106 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001107 createSensorsCallback(io, objectServer, dbusConnection,
1108 sensorConfigs, sensorsChanged, activateOnly);
1109 });
Matt Simmering786efb82023-01-18 14:09:21 -08001110 std::vector<std::string> types(sensorTypes.size());
1111 for (const auto& [type, dt] : sensorTypes)
1112 {
1113 types.push_back(type);
1114 }
1115 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001116}
1117
Ed Tanous201a1012024-04-03 18:07:28 -07001118void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001119{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001120 sensorTable = {{"power", sensor_paths::unitWatts},
1121 {"curr", sensor_paths::unitAmperes},
1122 {"temp", sensor_paths::unitDegreesC},
1123 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001124 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001125 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001126
Jeff Line41d52f2021-04-07 19:38:51 +08001127 labelMatch = {
1128 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001129 {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
1130 {"power", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001131 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1132 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1133 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001134 {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Ian Chienc82a4092024-07-24 10:52:59 +08001135 {"voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001136 {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001137 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001138 {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001139 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001140 {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
1141 {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
1142 {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1143 {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
1144 {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1145 {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001146
1147 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1148 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1149
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001150 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1151 {"Failure", {"in2_alarm"}},
1152 {"ACLost", {"in1_beep"}},
1153 {"ConfigureError", {"in1_fault"}}};
1154
Joseph Fub844e972023-03-25 10:24:30 +08001155 devParamMap = {
1156 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1157 {DevTypes::IIO,
1158 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001159}
1160
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001161static void powerStateChanged(
1162 PowerState type, bool newState,
1163 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1164 sensors,
1165 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1166 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1167{
1168 if (newState)
1169 {
1170 createSensors(io, objectServer, dbusConnection, nullptr, true);
1171 }
1172 else
1173 {
1174 for (auto& [path, sensor] : sensors)
1175 {
1176 if (sensor != nullptr && sensor->readState == type)
1177 {
1178 sensor->deactivate();
1179 }
1180 }
1181 }
1182}
1183
James Feistb6c0b912019-07-09 12:21:44 -07001184int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001185{
Ed Tanous1f978632023-02-28 18:16:39 -08001186 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001187 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1188
Ed Tanous14ed5e92022-07-12 15:50:23 -07001189 sdbusplus::asio::object_server objectServer(systemBus, true);
1190 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001191 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001192 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001193 auto sensorsChanged =
1194 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001195
Cheng C Yang916360b2019-05-07 18:47:16 +08001196 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001197
Patrick Williams2aaf7172024-08-16 15:20:40 -04001198 auto powerCallBack = [&io, &objectServer,
1199 &systemBus](PowerState type, bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001200 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1201 };
1202
1203 setupPowerMatchCallback(systemBus, powerCallBack);
1204
1205 boost::asio::post(io, [&]() {
1206 createSensors(io, objectServer, systemBus, nullptr, false);
1207 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001208 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001209 std::function<void(sdbusplus::message_t&)> eventHandler =
1210 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001211 if (message.is_method_error())
Cheng C Yang209ec562019-03-12 16:37:44 +08001212 {
George Liu4155a5a2025-01-24 15:26:27 +08001213 lg2::error("callback method error");
Cheng C Yang209ec562019-03-12 16:37:44 +08001214 return;
1215 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001216 sensorsChanged->insert(message.get_path());
1217 filterTimer.expires_after(std::chrono::seconds(3));
1218 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1219 if (ec == boost::asio::error::operation_aborted)
1220 {
1221 return;
1222 }
1223 if (ec)
1224 {
George Liu4155a5a2025-01-24 15:26:27 +08001225 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -04001226 }
1227 createSensors(io, objectServer, systemBus, sensorsChanged,
1228 false);
1229 });
1230 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001231
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001232 boost::asio::steady_timer cpuFilterTimer(io);
1233 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1234 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001235 std::string path = message.get_path();
1236 boost::to_lower(path);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001237
Patrick Williams2aaf7172024-08-16 15:20:40 -04001238 sdbusplus::message::object_path cpuPath(path);
1239 std::string cpuName = cpuPath.filename();
1240 if (!cpuName.starts_with("cpu"))
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001241 {
1242 return;
1243 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001244 size_t index = 0;
1245 try
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001246 {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001247 index = std::stoi(path.substr(path.size() - 1));
1248 }
1249 catch (const std::invalid_argument&)
1250 {
George Liu4155a5a2025-01-24 15:26:27 +08001251 lg2::error("Found invalid path: '{PATH}'", "PATH", path);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001252 return;
1253 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001254
1255 std::string objectName;
1256 boost::container::flat_map<std::string, std::variant<bool>> values;
1257 message.read(objectName, values);
1258 auto findPresence = values.find("Present");
Vikash Chandolaf2fc1b32024-08-29 15:59:27 +05301259 if (findPresence == values.end())
1260 {
1261 return;
1262 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001263 try
1264 {
1265 cpuPresence[index] = std::get<bool>(findPresence->second);
1266 }
1267 catch (const std::bad_variant_access& err)
1268 {
1269 return;
1270 }
1271
1272 if (!cpuPresence[index])
1273 {
1274 return;
1275 }
1276 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1277 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1278 if (ec == boost::asio::error::operation_aborted)
1279 {
1280 return;
1281 }
1282 if (ec)
1283 {
George Liu4155a5a2025-01-24 15:26:27 +08001284 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -04001285 return;
1286 }
1287 createSensors(io, objectServer, systemBus, nullptr, false);
1288 });
1289 };
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001290
Zev Weiss214d9712022-08-12 12:54:31 -07001291 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1292 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001293
1294 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1295 static_cast<sdbusplus::bus_t&>(*systemBus),
1296 "type='signal',member='PropertiesChanged',path_namespace='" +
1297 std::string(cpuInventoryPath) +
1298 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1299 cpuPresenceHandler));
1300
Matt Simmering39fadb92023-05-04 21:47:29 -07001301 getPresentCpus(systemBus);
1302
Bruce Lee1263c3d2021-06-04 15:16:33 +08001303 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001304 io.run();
1305}