blob: 51d9d786489bd857a53e5fabc50bcc05b638ddea [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}},
Potin Lai8339feb2025-05-28 15:51:56 +0800100 {"LM5066I", I2CDeviceType{"lm5066i", true}},
Yi-Shum77e87692023-11-30 10:27:45 +0800101 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Potin Lai43847a82024-02-27 16:29:45 +0800102 {"LTC4286", I2CDeviceType{"ltc4286", true}},
103 {"LTC4287", I2CDeviceType{"ltc4287", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +0200104 {"MAX5970", I2CDeviceType{"max5970", true}},
Joseph Fu15a39df2023-12-27 09:45:49 +0800105 {"MAX11607", I2CDeviceType{"max11607", false}},
Ian Chienc82a4092024-07-24 10:52:59 +0800106 {"MAX11615", I2CDeviceType{"max11615", false}},
Allen.Wang8c13c282024-07-03 09:42:54 +0800107 {"MAX11617", I2CDeviceType{"max11617", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800108 {"MAX16601", I2CDeviceType{"max16601", true}},
109 {"MAX20710", I2CDeviceType{"max20710", true}},
110 {"MAX20730", I2CDeviceType{"max20730", true}},
111 {"MAX20734", I2CDeviceType{"max20734", true}},
112 {"MAX20796", I2CDeviceType{"max20796", true}},
113 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800114 {"MP2856", I2CDeviceType{"mp2856", true}},
115 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800116 {"MP2971", I2CDeviceType{"mp2971", true}},
117 {"MP2973", I2CDeviceType{"mp2973", true}},
118 {"MP2975", I2CDeviceType{"mp2975", true}},
119 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800120 {"MP5990", I2CDeviceType{"mp5990", true}},
Jeff Lind8e48df2024-06-07 09:06:56 +0800121 {"MPQ8785", I2CDeviceType{"mpq8785", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800122 {"NCP4200", I2CDeviceType{"ncp4200", true}},
123 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
124 {"pmbus", I2CDeviceType{"pmbus", true}},
125 {"PXE1610", I2CDeviceType{"pxe1610", true}},
126 {"RAA228000", I2CDeviceType{"raa228000", true}},
Potin Lai6e6618f2024-05-16 15:02:56 +0800127 {"RAA228004", I2CDeviceType{"raa228004", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800128 {"RAA228228", I2CDeviceType{"raa228228", true}},
129 {"RAA228620", I2CDeviceType{"raa228620", true}},
130 {"RAA229001", I2CDeviceType{"raa229001", true}},
131 {"RAA229004", I2CDeviceType{"raa229004", true}},
132 {"RAA229126", I2CDeviceType{"raa229126", true}},
Ian Chien982d99f2024-08-20 17:16:56 +0800133 {"RTQ6056", I2CDeviceType{"rtq6056", false}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800134 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Chau Lyc4ddf642024-07-09 05:01:06 +0000135 {"smpro_hwmon", I2CDeviceType{"smpro", false}},
Rush Chena9ea8e52025-01-02 11:28:57 +0800136 {"SY24655", I2CDeviceType{"sy24655", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200137 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800138 {"TPS53679", I2CDeviceType{"tps53679", true}},
139 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800140 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800141 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
142 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800143 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800144};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700145
Joseph Fub844e972023-03-25 10:24:30 +0800146enum class DevTypes
147{
148 Unknown = 0,
149 HWMON,
150 IIO
151};
152
153struct DevParams
154{
155 unsigned int matchIndex = 0;
156 std::string matchRegEx;
157 std::string nameRegEx;
158};
159
Yong Libf8b1da2020-04-15 16:32:50 +0800160static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800161 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800162static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
163 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800164static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
165 pwmSensors;
166static boost::container::flat_map<std::string, std::string> sensorTable;
167static boost::container::flat_map<std::string, PSUProperty> labelMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800168static EventPathList eventMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800169static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800170
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800171static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800172static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700173
Cheng C Yang58b2b532019-05-31 00:19:45 +0800174// Function CheckEvent will check each attribute from eventMatch table in the
175// sysfs. If the attributes exists in sysfs, then store the complete path
176// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800177void checkEvent(const std::string& directory, const EventPathList& eventMatch,
178 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800179{
180 for (const auto& match : eventMatch)
181 {
182 const std::vector<std::string>& eventAttrs = match.second;
183 const std::string& eventName = match.first;
184 for (const auto& eventAttr : eventAttrs)
185 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700186 std::string eventPath = directory;
187 eventPath += "/";
188 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800189
190 std::ifstream eventFile(eventPath);
191 if (!eventFile.good())
192 {
193 continue;
194 }
195
196 eventPathList[eventName].push_back(eventPath);
197 }
198 }
199}
200
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800201// Check Group Events which contains more than one targets in each combine
202// events.
George Liu5b3542e2023-10-31 17:27:12 +0800203void checkGroupEvent(const std::string& directory,
George Liu5b3542e2023-10-31 17:27:12 +0800204 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800205{
cchouxe8a14e92023-10-25 23:26:27 +0800206 EventPathList pathList;
Ed Tanous2e466962025-01-30 10:59:49 -0800207 std::vector<std::filesystem::path> eventPaths;
208 if (!findFiles(std::filesystem::path(directory), R"(fan\d+_(alarm|fault))",
209 eventPaths))
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800210 {
cchouxe8a14e92023-10-25 23:26:27 +0800211 return;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800212 }
cchouxe8a14e92023-10-25 23:26:27 +0800213
214 for (const auto& eventPath : eventPaths)
215 {
216 std::string attrName = eventPath.filename();
217 pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
218 }
219 groupEventPathList["FanFault"] = pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800220}
221
Cheng C Yang58b2b532019-05-31 00:19:45 +0800222// Function checkEventLimits will check all the psu related xxx_input attributes
223// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
224// xxx_min_alarm exist, then store the existing paths of the alarm attributes
225// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800226void checkEventLimits(const std::string& sensorPathStr,
227 const EventPathList& limitEventMatch,
228 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800229{
Lei YUa2c7cea2020-12-23 14:07:28 +0800230 auto attributePartPos = sensorPathStr.find_last_of('_');
231 if (attributePartPos == std::string::npos)
232 {
233 // There is no '_' in the string, skip it
234 return;
235 }
236 auto attributePart =
237 std::string_view(sensorPathStr).substr(attributePartPos + 1);
238 if (attributePart != "input")
239 {
240 // If the sensor is not xxx_input, skip it
241 return;
242 }
243
244 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800245 for (const auto& limitMatch : limitEventMatch)
246 {
247 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
248 const std::string& eventName = limitMatch.first;
249 for (const auto& limitEventAttr : limitEventAttrs)
250 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800251 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800252 std::ifstream eventFile(limitEventPath);
253 if (!eventFile.good())
254 {
255 continue;
256 }
257 eventPathList[eventName].push_back(limitEventPath);
258 }
259 }
260}
Cheng C Yang916360b2019-05-07 18:47:16 +0800261
Patrick Williams2aaf7172024-08-16 15:20:40 -0400262static void checkPWMSensor(
Ed Tanous2e466962025-01-30 10:59:49 -0800263 const std::filesystem::path& sensorPath, std::string& labelHead,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400264 const std::string& interfacePath,
265 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
266 sdbusplus::asio::object_server& objectServer, const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800267{
cchouxe8a14e92023-10-25 23:26:27 +0800268 if (!labelHead.starts_with("fan"))
Cheng C Yang916360b2019-05-07 18:47:16 +0800269 {
cchouxe8a14e92023-10-25 23:26:27 +0800270 return;
Cheng C Yang916360b2019-05-07 18:47:16 +0800271 }
cchouxe8a14e92023-10-25 23:26:27 +0800272 std::string labelHeadIndex = labelHead.substr(3);
273
274 const std::string& sensorPathStr = sensorPath.string();
Patrick Williams2aaf7172024-08-16 15:20:40 -0400275 const std::string& pwmPathStr =
276 boost::replace_all_copy(sensorPathStr, "input", "target");
cchouxe8a14e92023-10-25 23:26:27 +0800277 std::ifstream pwmFile(pwmPathStr);
278 if (!pwmFile.good())
279 {
280 return;
281 }
282
283 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
284 if (findPWMSensor != pwmSensors.end())
285 {
286 return;
287 }
288
289 std::string name = "Pwm_";
290 name += psuName;
291 name += "_Fan_";
292 name += labelHeadIndex;
293
294 std::string objPath = interfacePath;
295 objPath += "_Fan_";
296 objPath += labelHeadIndex;
297
298 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
299 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800300}
301
Zhikui Ren23c96e72020-11-05 22:32:28 -0800302static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800303 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800304 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
305 const ManagedObjectType& sensorConfigs,
306 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800307 sensorsChanged,
308 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800309{
Josh Lehan49cfba92019-10-08 16:50:42 -0700310 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800311 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800312
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800313 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
314
Ed Tanous2e466962025-01-30 10:59:49 -0800315 std::vector<std::filesystem::path> pmbusPaths;
316 findFiles(std::filesystem::path("/sys/bus/iio/devices"), "name",
317 pmbusPaths);
318 findFiles(std::filesystem::path("/sys/class/hwmon"), "name", pmbusPaths);
Joseph Fub844e972023-03-25 10:24:30 +0800319 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800320 {
George Liu4155a5a2025-01-24 15:26:27 +0800321 lg2::error("No PSU sensors in system");
Cheng C Yang209ec562019-03-12 16:37:44 +0800322 return;
323 }
324
325 boost::container::flat_set<std::string> directories;
326 for (const auto& pmbusPath : pmbusPaths)
327 {
George Liu5b3542e2023-10-31 17:27:12 +0800328 EventPathList eventPathList;
329 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800330
331 std::ifstream nameFile(pmbusPath);
332 if (!nameFile.good())
333 {
George Liu4155a5a2025-01-24 15:26:27 +0800334 lg2::error("Failure finding '{PATH}'", "PATH", pmbusPath);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800335 continue;
336 }
337
338 std::string pmbusName;
339 std::getline(nameFile, pmbusName);
340 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700341
Patrick Williams80c68052025-05-13 23:31:49 -0400342 if (!sensorTypes.contains(pmbusName))
Cheng C Yang58b2b532019-05-31 00:19:45 +0800343 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700344 // To avoid this error message, add your driver name to
345 // the pmbusNames vector at the top of this file.
George Liu4155a5a2025-01-24 15:26:27 +0800346 lg2::error("'{NAME}' not found in sensor whitelist", "NAME",
347 pmbusName);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800348 continue;
349 }
350
Cheng C Yang209ec562019-03-12 16:37:44 +0800351 auto directory = pmbusPath.parent_path();
352
353 auto ret = directories.insert(directory.string());
354 if (!ret.second)
355 {
George Liu4155a5a2025-01-24 15:26:27 +0800356 lg2::error("Duplicate path: '{PATH}'", "PATH", directory);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800357 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 }
359
Joseph Fub844e972023-03-25 10:24:30 +0800360 DevTypes devType = DevTypes::HWMON;
361 std::string deviceName;
362 if (directory.parent_path() == "/sys/class/hwmon")
363 {
Ed Tanous2e466962025-01-30 10:59:49 -0800364 std::string devicePath =
365 std::filesystem::canonical(directory / "device");
Chau Lyc4ddf642024-07-09 05:01:06 +0000366 std::smatch match;
367 // Find /i2c-<bus>/<bus>-<address> match in device path
368 std::regex_search(devicePath, match, i2cDevRegex);
369 if (match.empty())
370 {
George Liu4155a5a2025-01-24 15:26:27 +0800371 lg2::error("Found bad device path: '{PATH}'", "PATH",
372 devicePath);
Chau Lyc4ddf642024-07-09 05:01:06 +0000373 continue;
374 }
375 // Extract <bus>-<address>
376 std::string matchStr = match[1];
377 deviceName = matchStr.substr(matchStr.find_last_of('/') + 1);
Joseph Fub844e972023-03-25 10:24:30 +0800378 }
379 else
380 {
Ed Tanous2e466962025-01-30 10:59:49 -0800381 deviceName =
382 std::filesystem::canonical(directory).parent_path().stem();
Joseph Fub844e972023-03-25 10:24:30 +0800383 devType = DevTypes::IIO;
384 }
385
Cheng C Yang209ec562019-03-12 16:37:44 +0800386 size_t bus = 0;
387 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000388 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800390 continue;
391 }
392
Zev Weisse8b97ee2022-08-12 15:23:51 -0700393 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 const SensorData* sensorData = nullptr;
395 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800396 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800397 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800398
Zev Weiss741f26e2022-08-12 18:21:02 -0700399 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800400 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700401 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800402 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800403 {
Zev Weiss054aad82022-08-18 01:37:34 -0700404 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800405 if (sensorBase != sensorData->end())
406 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700407 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800408 sensorType = type;
409 break;
410 }
411 }
412 if (baseConfig == nullptr)
413 {
George Liu4155a5a2025-01-24 15:26:27 +0800414 lg2::error("error finding base configuration for '{NAME}'",
415 "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 continue;
417 }
418
Zev Weisse8b97ee2022-08-12 15:23:51 -0700419 auto configBus = baseConfig->find("Bus");
420 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800421
Zev Weisse8b97ee2022-08-12 15:23:51 -0700422 if (configBus == baseConfig->end() ||
423 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800424 {
George Liu4155a5a2025-01-24 15:26:27 +0800425 lg2::error("error finding necessary entry in configuration");
Cheng C Yang209ec562019-03-12 16:37:44 +0800426 continue;
427 }
428
Matt Simmering6747eba2023-01-18 16:03:55 -0800429 const uint64_t* confBus =
430 std::get_if<uint64_t>(&(configBus->second));
431 const uint64_t* confAddr =
432 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800433 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800434 {
George Liu4155a5a2025-01-24 15:26:27 +0800435 lg2::error("Cannot get bus or address, invalid configuration");
Cheng C Yang58b2b532019-05-31 00:19:45 +0800436 continue;
437 }
438
439 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800440 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200441 if constexpr (debug)
442 {
George Liu4155a5a2025-01-24 15:26:27 +0800443 lg2::error(
444 "Configuration skipping '{CONFBUS}'-'{CONFADDR}' because not {BUS}-{ADDR}",
445 "CONFBUS", *confBus, "CONFADDR", *confAddr, "BUS", bus,
446 "ADDR", addr);
Patrick Rudolph9f270872023-10-25 09:13:09 +0200447 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800448 continue;
449 }
450
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800451 std::vector<thresholds::Threshold> confThresholds;
452 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
453 {
George Liu4155a5a2025-01-24 15:26:27 +0800454 lg2::error("error populating total thresholds");
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800455 }
456 thresholdConfSize = confThresholds.size();
457
Zev Weiss741f26e2022-08-12 18:21:02 -0700458 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800459 break;
460 }
461 if (interfacePath == nullptr)
462 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700463 // To avoid this error message, add your export map entry,
464 // from Entity Manager, to sensorTypes at the top of this file.
George Liu4155a5a2025-01-24 15:26:27 +0800465 lg2::error("failed to find match for '{NAME}'", "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800466 continue;
467 }
468
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800469 auto findI2CDev = devices.find(*interfacePath);
470
471 std::shared_ptr<I2CDevice> i2cDev;
472 if (findI2CDev != devices.end())
473 {
474 if (activateOnly && !findI2CDev->second.second)
475 {
476 continue;
477 }
478 i2cDev = findI2CDev->second.first;
479 }
480
Zev Weisse8b97ee2022-08-12 15:23:51 -0700481 auto findPSUName = baseConfig->find("Name");
482 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800483 {
George Liu4155a5a2025-01-24 15:26:27 +0800484 lg2::error("could not determine configuration name for '{NAME}'",
485 "NAME", deviceName);
Cheng C Yang209ec562019-03-12 16:37:44 +0800486 continue;
487 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800488 const std::string* psuName =
489 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800490 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800491 {
George Liu4155a5a2025-01-24 15:26:27 +0800492 lg2::error("Cannot find psu name, invalid configuration");
Cheng C Yang58b2b532019-05-31 00:19:45 +0800493 continue;
494 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800495
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800496 auto findCPU = baseConfig->find("CPURequired");
497 if (findCPU != baseConfig->end())
498 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500499 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800500 auto presenceFind = cpuPresence.find(index);
501 if (presenceFind == cpuPresence.end() || !presenceFind->second)
502 {
503 continue;
504 }
505 }
506
Zhikui Ren23c96e72020-11-05 22:32:28 -0800507 // on rescans, only update sensors we were signaled by
508 if (!firstScan)
509 {
Zhikui Renda98f092021-11-01 09:41:08 -0700510 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400511 auto it =
512 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
513 [psuNameStr](std::string& s) {
514 return s.ends_with(psuNameStr);
515 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800516
517 if (it == sensorsChanged->end())
518 {
519 continue;
520 }
521 sensorsChanged->erase(it);
522 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800523 checkEvent(directory.string(), eventMatch, eventPathList);
cchouxe8a14e92023-10-25 23:26:27 +0800524 checkGroupEvent(directory.string(), groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800525
Zev Weisse8b97ee2022-08-12 15:23:51 -0700526 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000527
Vijay Khemka996bad12019-05-28 15:15:16 -0700528 /* Check if there are more sensors in the same interface */
529 int i = 1;
530 std::vector<std::string> psuNames;
531 do
532 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700533 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700534 psuNames.push_back(
535 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700536 findPSUName = baseConfig->find("Name" + std::to_string(i++));
537 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700538
Ed Tanous2e466962025-01-30 10:59:49 -0800539 std::vector<std::filesystem::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800540 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
541 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800542 {
George Liu4155a5a2025-01-24 15:26:27 +0800543 lg2::error("No PSU non-label sensor in PSU");
Cheng C Yang209ec562019-03-12 16:37:44 +0800544 continue;
545 }
546
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530547 /* read max value in sysfs for in, curr, power, temp, ... */
548 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
549 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700550 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530551 {
George Liu4155a5a2025-01-24 15:26:27 +0800552 lg2::error("No max name in PSU");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530553 }
554 }
555
Zev Weiss8569bf22022-10-11 15:37:44 -0700556 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800557
Vijay Khemka996bad12019-05-28 15:15:16 -0700558 /* Find array of labels to be exposed if it is defined in config */
559 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700560 auto findLabelObj = baseConfig->find("Labels");
561 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700562 {
563 findLabels =
564 std::get<std::vector<std::string>>(findLabelObj->second);
565 }
566
Joseph Fub844e972023-03-25 10:24:30 +0800567 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700568 std::smatch matches;
569
Cheng C Yange50345b2019-04-02 17:26:15 +0800570 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800571 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530572 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800573 std::string labelHead;
574 std::string sensorPathStr = sensorPath.string();
575 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700576 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700577 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
578 {
Josh Lehan06494452019-10-31 09:49:16 -0700579 // hwmon *_input filename without number:
580 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800581 // iio in_*_raw filename without number:
582 // voltage, temp, pressure, ...
583 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700584 }
585 else
586 {
George Liu4155a5a2025-01-24 15:26:27 +0800587 lg2::error("Could not extract the alpha prefix from '{NAME}'",
588 "NAME", sensorNameStr);
Jason Ling5747fab2019-10-02 16:46:23 -0700589 continue;
590 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800591
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530592 std::string labelPath;
593
Joseph Fub844e972023-03-25 10:24:30 +0800594 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530595 {
Joseph Fub844e972023-03-25 10:24:30 +0800596 /* find and differentiate _max and _input to replace "label" */
597 size_t pos = sensorPathStr.find('_');
598 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530599 {
Joseph Fub844e972023-03-25 10:24:30 +0800600 std::string sensorPathStrMax = sensorPathStr.substr(pos);
601 if (sensorPathStrMax == "_max")
602 {
603 labelPath = boost::replace_all_copy(sensorPathStr,
604 "max", "label");
605 maxLabel = true;
606 }
607 else
608 {
609 labelPath = boost::replace_all_copy(sensorPathStr,
610 "input", "label");
611 maxLabel = false;
612 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530613 }
614 else
615 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800616 continue;
617 }
618
Joseph Fub844e972023-03-25 10:24:30 +0800619 std::ifstream labelFile(labelPath);
620 if (!labelFile.good())
621 {
622 if constexpr (debug)
623 {
George Liu4155a5a2025-01-24 15:26:27 +0800624 lg2::error(
625 "Input file '{PATH}' has no corresponding label file",
626 "PATH", sensorPath.string());
Joseph Fub844e972023-03-25 10:24:30 +0800627 }
628 // hwmon *_input filename with number:
629 // temp1, temp2, temp3, ...
Patrick Williams2aaf7172024-08-16 15:20:40 -0400630 labelHead =
631 sensorNameStr.substr(0, sensorNameStr.find('_'));
Joseph Fub844e972023-03-25 10:24:30 +0800632 }
633 else
634 {
635 std::string label;
636 std::getline(labelFile, label);
637 labelFile.close();
638 auto findSensor = sensors.find(label);
639 if (findSensor != sensors.end())
640 {
641 continue;
642 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800643
Joseph Fub844e972023-03-25 10:24:30 +0800644 // hwmon corresponding *_label file contents:
645 // vin1, vout1, ...
646 labelHead = label.substr(0, label.find(' '));
647 }
648
649 /* append "max" for labelMatch */
650 if (maxLabel)
651 {
652 labelHead.insert(0, "max");
653 }
654
Andrew Geissler66783882025-04-02 11:50:33 -0500655 // Don't add PWM sensors if it's not in label list
656 if (!findLabels.empty())
657 {
658 /* Check if this labelHead is enabled in config file */
659 if (std::find(findLabels.begin(), findLabels.end(),
660 labelHead) == findLabels.end())
661 {
662 if constexpr (debug)
663 {
664 lg2::error(
665 "could not find {LABEL} in the Labels list",
666 "LABEL", labelHead);
667 }
668 continue;
669 }
670 }
Joseph Fub844e972023-03-25 10:24:30 +0800671 checkPWMSensor(sensorPath, labelHead, *interfacePath,
672 dbusConnection, objectServer, psuNames[0]);
673 }
674 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530675 {
Joseph Fub844e972023-03-25 10:24:30 +0800676 auto findIIOHyphen = sensorNameStr.find_last_of('_');
677 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530678 }
679
Ed Tanous8a57ec02020-10-09 12:46:52 -0700680 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700681 {
George Liu4155a5a2025-01-24 15:26:27 +0800682 lg2::error("Sensor type: {NAME}, label: {LABEL}", "NAME",
683 sensorNameSubStr, "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700684 }
685
Vijay Khemka996bad12019-05-28 15:15:16 -0700686 if (!findLabels.empty())
687 {
688 /* Check if this labelHead is enabled in config file */
689 if (std::find(findLabels.begin(), findLabels.end(),
690 labelHead) == findLabels.end())
691 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700692 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800693 {
George Liu4155a5a2025-01-24 15:26:27 +0800694 lg2::error(
695 "could not find '{LABEL}' in the Labels list",
696 "LABEL", labelHead);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800697 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700698 continue;
699 }
700 }
Jayaprakash Mutyalaa205c6f2025-02-13 06:09:04 +0000701 auto it = std::find_if(labelHead.begin(), labelHead.end(),
702 static_cast<int (*)(int)>(std::isdigit));
703 std::string_view labelHeadView(
704 labelHead.data(), std::distance(labelHead.begin(), it));
705 auto findProperty =
706 labelMatch.find(static_cast<std::string>(labelHeadView));
Cheng C Yange50345b2019-04-02 17:26:15 +0800707 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800708 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700709 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700710 {
George Liu4155a5a2025-01-24 15:26:27 +0800711 lg2::error(
712 "Could not find matching default property for '{LABEL}'",
713 "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700714 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800715 continue;
716 }
717
Josh Lehan74d9bd92019-10-31 08:51:58 -0700718 // Protect the hardcoded labelMatch list from changes,
719 // by making a copy and modifying that instead.
720 // Avoid bleedthrough of one device's customizations to
721 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800722 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700723
724 // Use label head as prefix for reading from config file,
725 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
726 std::string keyName = labelHead + "_Name";
727 std::string keyScale = labelHead + "_Scale";
728 std::string keyMin = labelHead + "_Min";
729 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800730 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800731 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700732
733 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700734 auto findCustomName = baseConfig->find(keyName);
735 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700736 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 try
738 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800739 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700740 VariantToStringVisitor(), findCustomName->second);
741 }
Patrick Williams26601e82021-10-06 12:43:25 -0500742 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700743 {
George Liu4155a5a2025-01-24 15:26:27 +0800744 lg2::error("Unable to parse '{NAME}'", "NAME", keyName);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700745 continue;
746 }
747
748 // All strings are valid, including empty string
749 customizedName = true;
750 }
751
752 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700753 auto findCustomScale = baseConfig->find(keyScale);
754 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700755 {
756 try
757 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800758 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700759 VariantToUnsignedIntVisitor(), findCustomScale->second);
760 }
Patrick Williams26601e82021-10-06 12:43:25 -0500761 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700762 {
George Liu4155a5a2025-01-24 15:26:27 +0800763 lg2::error("Unable to parse '{SCALE}'", "SCALE", keyScale);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700764 continue;
765 }
766
767 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800768 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700769 {
770 customizedScale = true;
771 }
772 else
773 {
George Liu4155a5a2025-01-24 15:26:27 +0800774 lg2::error("Unable to accept '{SCALE}'", "SCALE", keyScale);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700775 continue;
776 }
777 }
778
Zev Weisse8b97ee2022-08-12 15:23:51 -0700779 auto findCustomMin = baseConfig->find(keyMin);
780 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700781 {
782 try
783 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800784 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700785 VariantToDoubleVisitor(), findCustomMin->second);
786 }
Patrick Williams26601e82021-10-06 12:43:25 -0500787 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700788 {
George Liu4155a5a2025-01-24 15:26:27 +0800789 lg2::error("Unable to parse '{MIN}'", "MIN", keyMin);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700790 continue;
791 }
792 }
793
Zev Weisse8b97ee2022-08-12 15:23:51 -0700794 auto findCustomMax = baseConfig->find(keyMax);
795 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700796 {
797 try
798 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800799 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700800 VariantToDoubleVisitor(), findCustomMax->second);
801 }
Patrick Williams26601e82021-10-06 12:43:25 -0500802 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700803 {
George Liu4155a5a2025-01-24 15:26:27 +0800804 lg2::error("Unable to parse '{MAX}'", "MAX", keyMax);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700805 continue;
806 }
807 }
808
Zev Weisse8b97ee2022-08-12 15:23:51 -0700809 auto findCustomOffset = baseConfig->find(keyOffset);
810 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800811 {
812 try
813 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800814 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800815 VariantToDoubleVisitor(), findCustomOffset->second);
816 }
Patrick Williams26601e82021-10-06 12:43:25 -0500817 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800818 {
George Liu4155a5a2025-01-24 15:26:27 +0800819 lg2::error("Unable to parse '{OFFSET}'", "OFFSET",
820 keyOffset);
Jeff Line41d52f2021-04-07 19:38:51 +0800821 continue;
822 }
823 }
824
Lotus Xucb5af732021-09-10 15:18:50 +0800825 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700826 auto findPowerState = baseConfig->find(keyPowerState);
827 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800828 {
829 std::string powerState = std::visit(VariantToStringVisitor(),
830 findPowerState->second);
831 setReadState(powerState, readState);
832 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800833 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700834 {
George Liu4155a5a2025-01-24 15:26:27 +0800835 lg2::error("Min must be less than Max");
Josh Lehan74d9bd92019-10-31 08:51:58 -0700836 continue;
837 }
838
839 // If the sensor name is being customized by config file,
840 // then prefix/suffix composition becomes not necessary,
841 // and in fact not wanted, because it gets in the way.
842 std::string psuNameFromIndex;
cchouxe8a14e92023-10-25 23:26:27 +0800843 std::string nameIndexStr = "1";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700844 if (!customizedName)
845 {
846 /* Find out sensor name index for this label */
847 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500848 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700849 if (std::regex_search(labelHead, matches, rgx))
850 {
cchouxe8a14e92023-10-25 23:26:27 +0800851 nameIndexStr = matches[1];
852 nameIndex = std::stoi(nameIndexStr);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700853
854 // Decrement to preserve alignment, because hwmon
855 // human-readable filenames and labels use 1-based
856 // numbering, but the "Name", "Name1", "Name2", etc. naming
857 // convention (the psuNames vector) uses 0-based numbering.
858 if (nameIndex > 0)
859 {
860 --nameIndex;
861 }
862 }
863 else
864 {
865 nameIndex = 0;
866 }
867
868 if (psuNames.size() <= nameIndex)
869 {
George Liu4155a5a2025-01-24 15:26:27 +0800870 lg2::error("Could not pair '{LABEL}' with a Name field",
871 "LABEL", labelHead);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700872 continue;
873 }
874
875 psuNameFromIndex = psuNames[nameIndex];
876
Ed Tanous8a57ec02020-10-09 12:46:52 -0700877 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700878 {
George Liu4155a5a2025-01-24 15:26:27 +0800879 lg2::error(
880 "'{LABEL}' paired with '{NAME}' at index '{INDEX}'",
881 "LABEL", labelHead, "NAME", psuNameFromIndex, "INDEX",
882 nameIndex);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700883 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700884 }
885
Joseph Fub844e972023-03-25 10:24:30 +0800886 if (devType == DevTypes::HWMON)
887 {
888 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
889 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800890
Josh Lehan74d9bd92019-10-31 08:51:58 -0700891 // Similarly, if sensor scaling factor is being customized,
892 // then the below power-of-10 constraint becomes unnecessary,
893 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800894 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700895 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700896 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700897 // Preserve existing usage of hardcoded labelMatch table below
898 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700899
Josh Lehan74d9bd92019-10-31 08:51:58 -0700900 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700901 char firstChar =
902 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700903 std::string strScaleFactor =
904 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
905
906 // Preserve existing configs by accepting earlier syntax,
907 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700908 auto findScaleFactor = baseConfig->find(strScaleFactor);
909 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700910 {
911 factor = std::visit(VariantToIntVisitor(),
912 findScaleFactor->second);
913 }
914
Ed Tanous8a57ec02020-10-09 12:46:52 -0700915 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700916 {
George Liu4155a5a2025-01-24 15:26:27 +0800917 lg2::error(
918 "Sensor scaling factor '{FACTOR}' string '{SCALE_FACTOR}'",
919 "FACTOR", factor, "SCALE_FACTOR", strScaleFactor);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700920 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700921 }
922
Vijay Khemka996bad12019-05-28 15:15:16 -0700923 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530924 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
925 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800926 {
George Liu4155a5a2025-01-24 15:26:27 +0800927 lg2::error("error populating thresholds for '{NAME}'", "NAME",
928 sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800929 }
930
Zev Weiss6b6891c2021-04-22 02:46:21 -0500931 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
932 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800933 {
George Liu4155a5a2025-01-24 15:26:27 +0800934 lg2::error("'{NAME}' is not a recognized sensor type", "NAME",
935 sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800936 continue;
937 }
938
Ed Tanous8a57ec02020-10-09 12:46:52 -0700939 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700940 {
George Liu4155a5a2025-01-24 15:26:27 +0800941 lg2::error("Sensor properties - Name: {NAME}, Scale: {SCALE}, "
942 "Min: {MIN}, Max: {MAX}, Offset: {OFFSET}",
943 "NAME", psuProperty.labelTypeName, "SCALE",
944 psuProperty.sensorScaleFactor, "MIN",
945 psuProperty.minReading, "MAX",
946 psuProperty.maxReading, "OFFSET",
947 psuProperty.sensorOffset);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700948 }
949
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800950 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700951 if (customizedName)
952 {
953 if (sensorName.empty())
954 {
955 // Allow selective disabling of an individual sensor,
956 // by customizing its name to an empty string.
George Liu4155a5a2025-01-24 15:26:27 +0800957 lg2::error("Sensor disabled, empty string");
Josh Lehan74d9bd92019-10-31 08:51:58 -0700958 continue;
959 }
960 }
961 else
962 {
963 // Sensor name not customized, do prefix/suffix composition,
964 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800965 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
cchouxe8a14e92023-10-25 23:26:27 +0800966
967 // The labelTypeName of a fan can be:
968 // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
969 if (labelHead == "fan" + nameIndexStr)
970 {
971 sensorName += nameIndexStr;
972 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700973 }
974
Ed Tanous8a57ec02020-10-09 12:46:52 -0700975 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700976 {
George Liu4155a5a2025-01-24 15:26:27 +0800977 lg2::error("Sensor name: {NAME}, path: {PATH}, type: {TYPE}",
978 "NAME", sensorName, "PATH", sensorPathStr, "TYPE",
979 sensorType);
Josh Lehan49cfba92019-10-08 16:50:42 -0700980 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800981 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800982
983 auto& sensor = sensors[sensorName];
984 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700985 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800986 sensor = nullptr;
987 }
988
989 if (sensor != nullptr)
990 {
991 sensor->activate(sensorPathStr, i2cDev);
992 }
993 else
994 {
995 sensors[sensorName] = std::make_shared<PSUSensor>(
996 sensorPathStr, sensorType, objectServer, dbusConnection, io,
997 sensorName, std::move(sensorThresholds), *interfacePath,
998 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800999 psuProperty.maxReading, psuProperty.minReading,
1000 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001001 pollRate, i2cDev);
1002 sensors[sensorName]->setupRead();
1003 ++numCreated;
1004 if constexpr (debug)
1005 {
George Liu4155a5a2025-01-24 15:26:27 +08001006 lg2::error("Created '{NUM}' sensors so far", "NUM",
1007 numCreated);
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001008 }
Josh Lehan74d9bd92019-10-31 08:51:58 -07001009 }
Cheng C Yang209ec562019-03-12 16:37:44 +08001010 }
Cheng C Yang58b2b532019-05-31 00:19:45 +08001011
Joseph Fub844e972023-03-25 10:24:30 +08001012 if (devType == DevTypes::HWMON)
1013 {
1014 // OperationalStatus event
1015 combineEvents[*psuName + "OperationalStatus"] = nullptr;
1016 combineEvents[*psuName + "OperationalStatus"] =
1017 std::make_unique<PSUCombineEvent>(
1018 objectServer, dbusConnection, io, *psuName, readState,
1019 eventPathList, groupEventPathList, "OperationalStatus",
1020 pollRate);
1021 }
Cheng C Yang209ec562019-03-12 16:37:44 +08001022 }
Josh Lehan49cfba92019-10-08 16:50:42 -07001023
Ed Tanous8a57ec02020-10-09 12:46:52 -07001024 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -07001025 {
George Liu4155a5a2025-01-24 15:26:27 +08001026 lg2::error("Created total of '{NUM}' sensors", "NUM", numCreated);
Josh Lehan49cfba92019-10-08 16:50:42 -07001027 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001028}
Cheng C Yang209ec562019-03-12 16:37:44 +08001029
Patrick Williams556e04b2025-02-01 08:22:22 -05001030static void getPresentCpus(
1031 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Matt Simmering39fadb92023-05-04 21:47:29 -07001032{
1033 static const int depth = 2;
1034 static const int numKeys = 1;
1035 GetSubTreeType cpuSubTree;
1036
1037 try
1038 {
1039 auto getItems = dbusConnection->new_method_call(
1040 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
1041 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
1042 std::array<const char*, numKeys>{
1043 "xyz.openbmc_project.Inventory.Item"});
1044 auto getItemsResp = dbusConnection->call(getItems);
1045 getItemsResp.read(cpuSubTree);
1046 }
1047 catch (sdbusplus::exception_t& e)
1048 {
George Liu4155a5a2025-01-24 15:26:27 +08001049 lg2::error("error getting inventory item subtree: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001050 return;
1051 }
1052
1053 for (const auto& [path, objDict] : cpuSubTree)
1054 {
1055 auto obj = sdbusplus::message::object_path(path).filename();
Patrick Rudolph457715b2024-04-10 14:51:35 +02001056 boost::to_lower(obj);
1057
Matt Simmering39fadb92023-05-04 21:47:29 -07001058 if (!obj.starts_with("cpu") || objDict.empty())
1059 {
1060 continue;
1061 }
1062 const std::string& owner = objDict.begin()->first;
1063
1064 std::variant<bool> respValue;
1065 try
1066 {
1067 auto getPresence = dbusConnection->new_method_call(
1068 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1069 "Get");
1070 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1071 auto resp = dbusConnection->call(getPresence);
1072 resp.read(respValue);
1073 }
1074 catch (sdbusplus::exception_t& e)
1075 {
George Liu4155a5a2025-01-24 15:26:27 +08001076 lg2::error("Error in getting CPU presence: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001077 continue;
1078 }
1079
1080 auto* present = std::get_if<bool>(&respValue);
1081 if (present != nullptr && *present)
1082 {
1083 int cpuIndex = 0;
1084 try
1085 {
Patrick Rudolph457715b2024-04-10 14:51:35 +02001086 cpuIndex = std::stoi(obj.substr(obj.size() - 1));
Matt Simmering39fadb92023-05-04 21:47:29 -07001087 }
1088 catch (const std::exception& e)
1089 {
George Liu4155a5a2025-01-24 15:26:27 +08001090 lg2::error("Error converting CPU index: '{ERR}'", "ERR", e);
Matt Simmering39fadb92023-05-04 21:47:29 -07001091 continue;
1092 }
Patrick Rudolph457715b2024-04-10 14:51:35 +02001093 cpuPresence[cpuIndex] = *present;
Matt Simmering39fadb92023-05-04 21:47:29 -07001094 }
1095 }
1096}
1097
Zhikui Ren23c96e72020-11-05 22:32:28 -08001098void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001099 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001100 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1101 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001102 sensorsChanged,
1103 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001104{
1105 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001106 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1107 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001108 createSensorsCallback(io, objectServer, dbusConnection,
1109 sensorConfigs, sensorsChanged, activateOnly);
1110 });
Matt Simmering786efb82023-01-18 14:09:21 -08001111 std::vector<std::string> types(sensorTypes.size());
1112 for (const auto& [type, dt] : sensorTypes)
1113 {
1114 types.push_back(type);
1115 }
1116 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001117}
1118
Ed Tanous201a1012024-04-03 18:07:28 -07001119void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001120{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001121 sensorTable = {{"power", sensor_paths::unitWatts},
1122 {"curr", sensor_paths::unitAmperes},
1123 {"temp", sensor_paths::unitDegreesC},
1124 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001125 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001126 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001127
Jeff Line41d52f2021-04-07 19:38:51 +08001128 labelMatch = {
1129 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001130 {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
1131 {"power", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001132 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1133 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1134 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001135 {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Ian Chienc82a4092024-07-24 10:52:59 +08001136 {"voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001137 {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001138 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001139 {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001140 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001141 {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
1142 {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
1143 {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1144 {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
1145 {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1146 {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001147
1148 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1149 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1150
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001151 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1152 {"Failure", {"in2_alarm"}},
1153 {"ACLost", {"in1_beep"}},
1154 {"ConfigureError", {"in1_fault"}}};
1155
Joseph Fub844e972023-03-25 10:24:30 +08001156 devParamMap = {
1157 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1158 {DevTypes::IIO,
1159 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001160}
1161
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001162static void powerStateChanged(
1163 PowerState type, bool newState,
1164 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1165 sensors,
1166 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1167 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1168{
1169 if (newState)
1170 {
1171 createSensors(io, objectServer, dbusConnection, nullptr, true);
1172 }
1173 else
1174 {
1175 for (auto& [path, sensor] : sensors)
1176 {
1177 if (sensor != nullptr && sensor->readState == type)
1178 {
1179 sensor->deactivate();
1180 }
1181 }
1182 }
1183}
1184
James Feistb6c0b912019-07-09 12:21:44 -07001185int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001186{
Ed Tanous1f978632023-02-28 18:16:39 -08001187 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001188 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1189
Ed Tanous14ed5e92022-07-12 15:50:23 -07001190 sdbusplus::asio::object_server objectServer(systemBus, true);
1191 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001192 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001193 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001194 auto sensorsChanged =
1195 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001196
Cheng C Yang916360b2019-05-07 18:47:16 +08001197 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001198
Patrick Williams2aaf7172024-08-16 15:20:40 -04001199 auto powerCallBack = [&io, &objectServer,
1200 &systemBus](PowerState type, bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001201 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1202 };
1203
1204 setupPowerMatchCallback(systemBus, powerCallBack);
1205
1206 boost::asio::post(io, [&]() {
1207 createSensors(io, objectServer, systemBus, nullptr, false);
1208 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001209 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001210 std::function<void(sdbusplus::message_t&)> eventHandler =
1211 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001212 if (message.is_method_error())
Cheng C Yang209ec562019-03-12 16:37:44 +08001213 {
George Liu4155a5a2025-01-24 15:26:27 +08001214 lg2::error("callback method error");
Cheng C Yang209ec562019-03-12 16:37:44 +08001215 return;
1216 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001217 sensorsChanged->insert(message.get_path());
1218 filterTimer.expires_after(std::chrono::seconds(3));
1219 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1220 if (ec == boost::asio::error::operation_aborted)
1221 {
1222 return;
1223 }
1224 if (ec)
1225 {
George Liu4155a5a2025-01-24 15:26:27 +08001226 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -04001227 }
1228 createSensors(io, objectServer, systemBus, sensorsChanged,
1229 false);
1230 });
1231 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001232
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001233 boost::asio::steady_timer cpuFilterTimer(io);
1234 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1235 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001236 std::string path = message.get_path();
1237 boost::to_lower(path);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001238
Patrick Williams2aaf7172024-08-16 15:20:40 -04001239 sdbusplus::message::object_path cpuPath(path);
1240 std::string cpuName = cpuPath.filename();
1241 if (!cpuName.starts_with("cpu"))
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001242 {
1243 return;
1244 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001245 size_t index = 0;
1246 try
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001247 {
Patrick Williams2aaf7172024-08-16 15:20:40 -04001248 index = std::stoi(path.substr(path.size() - 1));
1249 }
1250 catch (const std::invalid_argument&)
1251 {
George Liu4155a5a2025-01-24 15:26:27 +08001252 lg2::error("Found invalid path: '{PATH}'", "PATH", path);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001253 return;
1254 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001255
1256 std::string objectName;
1257 boost::container::flat_map<std::string, std::variant<bool>> values;
1258 message.read(objectName, values);
1259 auto findPresence = values.find("Present");
Vikash Chandolaf2fc1b32024-08-29 15:59:27 +05301260 if (findPresence == values.end())
1261 {
1262 return;
1263 }
Patrick Williams2aaf7172024-08-16 15:20:40 -04001264 try
1265 {
1266 cpuPresence[index] = std::get<bool>(findPresence->second);
1267 }
1268 catch (const std::bad_variant_access& err)
1269 {
1270 return;
1271 }
1272
1273 if (!cpuPresence[index])
1274 {
1275 return;
1276 }
1277 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1278 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1279 if (ec == boost::asio::error::operation_aborted)
1280 {
1281 return;
1282 }
1283 if (ec)
1284 {
George Liu4155a5a2025-01-24 15:26:27 +08001285 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -04001286 return;
1287 }
1288 createSensors(io, objectServer, systemBus, nullptr, false);
1289 });
1290 };
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001291
Zev Weiss214d9712022-08-12 12:54:31 -07001292 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1293 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001294
1295 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1296 static_cast<sdbusplus::bus_t&>(*systemBus),
1297 "type='signal',member='PropertiesChanged',path_namespace='" +
1298 std::string(cpuInventoryPath) +
1299 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1300 cpuPresenceHandler));
1301
Matt Simmering39fadb92023-05-04 21:47:29 -07001302 getPresentCpus(systemBus);
1303
Bruce Lee1263c3d2021-06-04 15:16:33 +08001304 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001305 io.run();
1306}