blob: cd49ee439873c6627082372d5b90e6985b88c3d6 [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>
James Feist38fb5982020-05-28 10:09:54 -070034#include <sdbusplus/asio/connection.hpp>
35#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070037#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <sdbusplus/exception.hpp>
39#include <sdbusplus/message.hpp>
40#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070041
Ed Tanouseacbfdd2024-04-04 12:00:24 -070042#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070043#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070044#include <cctype>
45#include <chrono>
Josh Lehan74d9bd92019-10-31 08:51:58 -070046#include <cmath>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070047#include <cstddef>
48#include <cstdint>
49#include <exception>
James Feist24f02f22019-04-15 11:05:39 -070050#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080051#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070052#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080053#include <iostream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070054#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080055#include <regex>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070056#include <stdexcept>
Patrick Venture96e97db2019-10-31 13:44:38 -070057#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080058#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070059#include <utility>
60#include <variant>
61#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080062
Ed Tanous8a57ec02020-10-09 12:46:52 -070063static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070064
Matt Simmering786efb82023-01-18 14:09:21 -080065static const I2CDeviceTypeMap sensorTypes{
Delphine CC Chiu436c5bd2023-09-22 09:47:43 +080066 {"ADC128D818", I2CDeviceType{"adc128d818", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080067 {"ADM1266", I2CDeviceType{"adm1266", true}},
68 {"ADM1272", I2CDeviceType{"adm1272", true}},
69 {"ADM1275", I2CDeviceType{"adm1275", true}},
70 {"ADM1278", I2CDeviceType{"adm1278", true}},
71 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080072 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080073 {"ADS7830", I2CDeviceType{"ads7830", true}},
74 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
75 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020076 {"cffps", I2CDeviceType{"cffps", true}},
77 {"cffps1", I2CDeviceType{"cffps", true}},
78 {"cffps2", I2CDeviceType{"cffps", true}},
79 {"cffps3", I2CDeviceType{"cffps", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080080 {"DPS800", I2CDeviceType{"dps800", true}},
81 {"INA219", I2CDeviceType{"ina219", true}},
82 {"INA230", I2CDeviceType{"ina230", true}},
Potin Lai5674ac92023-11-20 14:33:56 +080083 {"INA238", I2CDeviceType{"ina238", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080084 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080085 {"IR38060", I2CDeviceType{"ir38060", true}},
86 {"IR38164", I2CDeviceType{"ir38164", true}},
87 {"IR38263", I2CDeviceType{"ir38263", true}},
88 {"ISL68137", I2CDeviceType{"isl68137", true}},
89 {"ISL68220", I2CDeviceType{"isl68220", true}},
90 {"ISL68223", I2CDeviceType{"isl68223", true}},
91 {"ISL69225", I2CDeviceType{"isl69225", true}},
92 {"ISL69243", I2CDeviceType{"isl69243", true}},
93 {"ISL69260", I2CDeviceType{"isl69260", true}},
94 {"LM25066", I2CDeviceType{"lm25066", true}},
Yi-Shum77e87692023-11-30 10:27:45 +080095 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Potin Lai43847a82024-02-27 16:29:45 +080096 {"LTC4286", I2CDeviceType{"ltc4286", true}},
97 {"LTC4287", I2CDeviceType{"ltc4287", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +020098 {"MAX5970", I2CDeviceType{"max5970", true}},
Joseph Fu15a39df2023-12-27 09:45:49 +080099 {"MAX11607", I2CDeviceType{"max11607", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800100 {"MAX16601", I2CDeviceType{"max16601", true}},
101 {"MAX20710", I2CDeviceType{"max20710", true}},
102 {"MAX20730", I2CDeviceType{"max20730", true}},
103 {"MAX20734", I2CDeviceType{"max20734", true}},
104 {"MAX20796", I2CDeviceType{"max20796", true}},
105 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800106 {"MP2856", I2CDeviceType{"mp2856", true}},
107 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800108 {"MP2971", I2CDeviceType{"mp2971", true}},
109 {"MP2973", I2CDeviceType{"mp2973", true}},
110 {"MP2975", I2CDeviceType{"mp2975", true}},
111 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800112 {"MP5990", I2CDeviceType{"mp5990", true}},
Jeff Lind8e48df2024-06-07 09:06:56 +0800113 {"MPQ8785", I2CDeviceType{"mpq8785", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800114 {"NCP4200", I2CDeviceType{"ncp4200", true}},
115 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
116 {"pmbus", I2CDeviceType{"pmbus", true}},
117 {"PXE1610", I2CDeviceType{"pxe1610", true}},
118 {"RAA228000", I2CDeviceType{"raa228000", true}},
119 {"RAA228228", I2CDeviceType{"raa228228", true}},
120 {"RAA228620", I2CDeviceType{"raa228620", true}},
121 {"RAA229001", I2CDeviceType{"raa229001", true}},
122 {"RAA229004", I2CDeviceType{"raa229004", true}},
123 {"RAA229126", I2CDeviceType{"raa229126", true}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800124 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200125 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800126 {"TPS53679", I2CDeviceType{"tps53679", true}},
127 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800128 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800129 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
130 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800131 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800132};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700133
Joseph Fub844e972023-03-25 10:24:30 +0800134enum class DevTypes
135{
136 Unknown = 0,
137 HWMON,
138 IIO
139};
140
141struct DevParams
142{
143 unsigned int matchIndex = 0;
144 std::string matchRegEx;
145 std::string nameRegEx;
146};
147
Cheng C Yang209ec562019-03-12 16:37:44 +0800148namespace fs = std::filesystem;
149
Yong Libf8b1da2020-04-15 16:32:50 +0800150static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800151 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800152static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
153 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800154static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
155 pwmSensors;
156static boost::container::flat_map<std::string, std::string> sensorTable;
157static boost::container::flat_map<std::string, PSUProperty> labelMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800158static EventPathList eventMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800159static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800160
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800161static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800162static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700163
Cheng C Yang58b2b532019-05-31 00:19:45 +0800164// Function CheckEvent will check each attribute from eventMatch table in the
165// sysfs. If the attributes exists in sysfs, then store the complete path
166// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800167void checkEvent(const std::string& directory, const EventPathList& eventMatch,
168 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800169{
170 for (const auto& match : eventMatch)
171 {
172 const std::vector<std::string>& eventAttrs = match.second;
173 const std::string& eventName = match.first;
174 for (const auto& eventAttr : eventAttrs)
175 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700176 std::string eventPath = directory;
177 eventPath += "/";
178 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800179
180 std::ifstream eventFile(eventPath);
181 if (!eventFile.good())
182 {
183 continue;
184 }
185
186 eventPathList[eventName].push_back(eventPath);
187 }
188 }
189}
190
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800191// Check Group Events which contains more than one targets in each combine
192// events.
George Liu5b3542e2023-10-31 17:27:12 +0800193void checkGroupEvent(const std::string& directory,
George Liu5b3542e2023-10-31 17:27:12 +0800194 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800195{
cchouxe8a14e92023-10-25 23:26:27 +0800196 EventPathList pathList;
197 std::vector<fs::path> eventPaths;
198 if (!findFiles(fs::path(directory), R"(fan\d+_(alarm|fault))", eventPaths))
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800199 {
cchouxe8a14e92023-10-25 23:26:27 +0800200 return;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800201 }
cchouxe8a14e92023-10-25 23:26:27 +0800202
203 for (const auto& eventPath : eventPaths)
204 {
205 std::string attrName = eventPath.filename();
206 pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
207 }
208 groupEventPathList["FanFault"] = pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800209}
210
Cheng C Yang58b2b532019-05-31 00:19:45 +0800211// Function checkEventLimits will check all the psu related xxx_input attributes
212// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
213// xxx_min_alarm exist, then store the existing paths of the alarm attributes
214// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800215void checkEventLimits(const std::string& sensorPathStr,
216 const EventPathList& limitEventMatch,
217 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800218{
Lei YUa2c7cea2020-12-23 14:07:28 +0800219 auto attributePartPos = sensorPathStr.find_last_of('_');
220 if (attributePartPos == std::string::npos)
221 {
222 // There is no '_' in the string, skip it
223 return;
224 }
225 auto attributePart =
226 std::string_view(sensorPathStr).substr(attributePartPos + 1);
227 if (attributePart != "input")
228 {
229 // If the sensor is not xxx_input, skip it
230 return;
231 }
232
233 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800234 for (const auto& limitMatch : limitEventMatch)
235 {
236 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
237 const std::string& eventName = limitMatch.first;
238 for (const auto& limitEventAttr : limitEventAttrs)
239 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800240 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800241 std::ifstream eventFile(limitEventPath);
242 if (!eventFile.good())
243 {
244 continue;
245 }
246 eventPathList[eventName].push_back(limitEventPath);
247 }
248 }
249}
Cheng C Yang916360b2019-05-07 18:47:16 +0800250
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530251static void
252 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
253 const std::string& interfacePath,
254 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
255 sdbusplus::asio::object_server& objectServer,
256 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800257{
cchouxe8a14e92023-10-25 23:26:27 +0800258 if (!labelHead.starts_with("fan"))
Cheng C Yang916360b2019-05-07 18:47:16 +0800259 {
cchouxe8a14e92023-10-25 23:26:27 +0800260 return;
Cheng C Yang916360b2019-05-07 18:47:16 +0800261 }
cchouxe8a14e92023-10-25 23:26:27 +0800262 std::string labelHeadIndex = labelHead.substr(3);
263
264 const std::string& sensorPathStr = sensorPath.string();
265 const std::string& pwmPathStr = boost::replace_all_copy(sensorPathStr,
266 "input", "target");
267 std::ifstream pwmFile(pwmPathStr);
268 if (!pwmFile.good())
269 {
270 return;
271 }
272
273 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
274 if (findPWMSensor != pwmSensors.end())
275 {
276 return;
277 }
278
279 std::string name = "Pwm_";
280 name += psuName;
281 name += "_Fan_";
282 name += labelHeadIndex;
283
284 std::string objPath = interfacePath;
285 objPath += "_Fan_";
286 objPath += labelHeadIndex;
287
288 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
289 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800290}
291
Zhikui Ren23c96e72020-11-05 22:32:28 -0800292static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800293 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800294 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
295 const ManagedObjectType& sensorConfigs,
296 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800297 sensorsChanged,
298 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800299{
Josh Lehan49cfba92019-10-08 16:50:42 -0700300 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800301 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800302
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800303 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
304
Cheng C Yang209ec562019-03-12 16:37:44 +0800305 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800306 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
307 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
308 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800309 {
310 std::cerr << "No PSU sensors in system\n";
311 return;
312 }
313
314 boost::container::flat_set<std::string> directories;
315 for (const auto& pmbusPath : pmbusPaths)
316 {
George Liu5b3542e2023-10-31 17:27:12 +0800317 EventPathList eventPathList;
318 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800319
320 std::ifstream nameFile(pmbusPath);
321 if (!nameFile.good())
322 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700323 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800324 continue;
325 }
326
327 std::string pmbusName;
328 std::getline(nameFile, pmbusName);
329 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700330
Matt Simmering786efb82023-01-18 14:09:21 -0800331 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800332 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700333 // To avoid this error message, add your driver name to
334 // the pmbusNames vector at the top of this file.
335 std::cerr << "Driver name " << pmbusName
336 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800337 continue;
338 }
339
Cheng C Yang209ec562019-03-12 16:37:44 +0800340 auto directory = pmbusPath.parent_path();
341
342 auto ret = directories.insert(directory.string());
343 if (!ret.second)
344 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700345 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800346 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800347 }
348
Joseph Fub844e972023-03-25 10:24:30 +0800349 DevTypes devType = DevTypes::HWMON;
350 std::string deviceName;
351 if (directory.parent_path() == "/sys/class/hwmon")
352 {
353 deviceName = fs::canonical(directory / "device").stem();
354 }
355 else
356 {
357 deviceName = fs::canonical(directory).parent_path().stem();
358 devType = DevTypes::IIO;
359 }
360
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 size_t bus = 0;
362 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000363 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 continue;
366 }
367
Zev Weisse8b97ee2022-08-12 15:23:51 -0700368 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800369 const SensorData* sensorData = nullptr;
370 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800371 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800372 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800373
Zev Weiss741f26e2022-08-12 18:21:02 -0700374 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800375 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700376 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800377 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800378 {
Zev Weiss054aad82022-08-18 01:37:34 -0700379 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 if (sensorBase != sensorData->end())
381 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700382 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800383 sensorType = type;
384 break;
385 }
386 }
387 if (baseConfig == nullptr)
388 {
389 std::cerr << "error finding base configuration for "
390 << deviceName << "\n";
391 continue;
392 }
393
Zev Weisse8b97ee2022-08-12 15:23:51 -0700394 auto configBus = baseConfig->find("Bus");
395 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800396
Zev Weisse8b97ee2022-08-12 15:23:51 -0700397 if (configBus == baseConfig->end() ||
398 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800399 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800400 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800401 continue;
402 }
403
Matt Simmering6747eba2023-01-18 16:03:55 -0800404 const uint64_t* confBus =
405 std::get_if<uint64_t>(&(configBus->second));
406 const uint64_t* confAddr =
407 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800408 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800409 {
410 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700411 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800412 continue;
413 }
414
415 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200417 if constexpr (debug)
418 {
419 std::cerr << "Configuration skipping " << *confBus << "-"
420 << *confAddr << " because not " << bus << "-"
421 << addr << "\n";
422 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800423 continue;
424 }
425
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800426 std::vector<thresholds::Threshold> confThresholds;
427 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
428 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800429 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800430 }
431 thresholdConfSize = confThresholds.size();
432
Zev Weiss741f26e2022-08-12 18:21:02 -0700433 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800434 break;
435 }
436 if (interfacePath == nullptr)
437 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700438 // To avoid this error message, add your export map entry,
439 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800440 std::cerr << "failed to find match for " << deviceName << "\n";
441 continue;
442 }
443
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800444 auto findI2CDev = devices.find(*interfacePath);
445
446 std::shared_ptr<I2CDevice> i2cDev;
447 if (findI2CDev != devices.end())
448 {
449 if (activateOnly && !findI2CDev->second.second)
450 {
451 continue;
452 }
453 i2cDev = findI2CDev->second.first;
454 }
455
Zev Weisse8b97ee2022-08-12 15:23:51 -0700456 auto findPSUName = baseConfig->find("Name");
457 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800458 {
459 std::cerr << "could not determine configuration name for "
460 << deviceName << "\n";
461 continue;
462 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800463 const std::string* psuName =
464 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800465 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800466 {
467 std::cerr << "Cannot find psu name, invalid configuration\n";
468 continue;
469 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800470
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800471 auto findCPU = baseConfig->find("CPURequired");
472 if (findCPU != baseConfig->end())
473 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500474 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800475 auto presenceFind = cpuPresence.find(index);
476 if (presenceFind == cpuPresence.end() || !presenceFind->second)
477 {
478 continue;
479 }
480 }
481
Zhikui Ren23c96e72020-11-05 22:32:28 -0800482 // on rescans, only update sensors we were signaled by
483 if (!firstScan)
484 {
Zhikui Renda98f092021-11-01 09:41:08 -0700485 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500486 auto it = std::find_if(sensorsChanged->begin(),
487 sensorsChanged->end(),
488 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800489 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500490 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800491
492 if (it == sensorsChanged->end())
493 {
494 continue;
495 }
496 sensorsChanged->erase(it);
497 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800498 checkEvent(directory.string(), eventMatch, eventPathList);
cchouxe8a14e92023-10-25 23:26:27 +0800499 checkGroupEvent(directory.string(), groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800500
Zev Weisse8b97ee2022-08-12 15:23:51 -0700501 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000502
Vijay Khemka996bad12019-05-28 15:15:16 -0700503 /* Check if there are more sensors in the same interface */
504 int i = 1;
505 std::vector<std::string> psuNames;
506 do
507 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700508 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700509 psuNames.push_back(
510 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700511 findPSUName = baseConfig->find("Name" + std::to_string(i++));
512 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700513
Cheng C Yange50345b2019-04-02 17:26:15 +0800514 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800515 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
516 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800517 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800518 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800519 continue;
520 }
521
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530522 /* read max value in sysfs for in, curr, power, temp, ... */
523 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
524 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700525 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530526 {
527 std::cerr << "No max name in PSU \n";
528 }
529 }
530
Zev Weiss8569bf22022-10-11 15:37:44 -0700531 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800532
Vijay Khemka996bad12019-05-28 15:15:16 -0700533 /* Find array of labels to be exposed if it is defined in config */
534 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700535 auto findLabelObj = baseConfig->find("Labels");
536 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700537 {
538 findLabels =
539 std::get<std::vector<std::string>>(findLabelObj->second);
540 }
541
Joseph Fub844e972023-03-25 10:24:30 +0800542 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700543 std::smatch matches;
544
Cheng C Yange50345b2019-04-02 17:26:15 +0800545 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800546 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530547 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800548 std::string labelHead;
549 std::string sensorPathStr = sensorPath.string();
550 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700551 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700552 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
553 {
Josh Lehan06494452019-10-31 09:49:16 -0700554 // hwmon *_input filename without number:
555 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800556 // iio in_*_raw filename without number:
557 // voltage, temp, pressure, ...
558 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700559 }
560 else
561 {
Josh Lehan06494452019-10-31 09:49:16 -0700562 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700563 << sensorNameStr;
564 continue;
565 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800566
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530567 std::string labelPath;
568
Joseph Fub844e972023-03-25 10:24:30 +0800569 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530570 {
Joseph Fub844e972023-03-25 10:24:30 +0800571 /* find and differentiate _max and _input to replace "label" */
572 size_t pos = sensorPathStr.find('_');
573 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530574 {
Joseph Fub844e972023-03-25 10:24:30 +0800575 std::string sensorPathStrMax = sensorPathStr.substr(pos);
576 if (sensorPathStrMax == "_max")
577 {
578 labelPath = boost::replace_all_copy(sensorPathStr,
579 "max", "label");
580 maxLabel = true;
581 }
582 else
583 {
584 labelPath = boost::replace_all_copy(sensorPathStr,
585 "input", "label");
586 maxLabel = false;
587 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530588 }
589 else
590 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800591 continue;
592 }
593
Joseph Fub844e972023-03-25 10:24:30 +0800594 std::ifstream labelFile(labelPath);
595 if (!labelFile.good())
596 {
597 if constexpr (debug)
598 {
599 std::cerr << "Input file " << sensorPath
600 << " has no corresponding label file\n";
601 }
602 // hwmon *_input filename with number:
603 // temp1, temp2, temp3, ...
604 labelHead = sensorNameStr.substr(0,
605 sensorNameStr.find('_'));
606 }
607 else
608 {
609 std::string label;
610 std::getline(labelFile, label);
611 labelFile.close();
612 auto findSensor = sensors.find(label);
613 if (findSensor != sensors.end())
614 {
615 continue;
616 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800617
Joseph Fub844e972023-03-25 10:24:30 +0800618 // hwmon corresponding *_label file contents:
619 // vin1, vout1, ...
620 labelHead = label.substr(0, label.find(' '));
621 }
622
623 /* append "max" for labelMatch */
624 if (maxLabel)
625 {
626 labelHead.insert(0, "max");
627 }
628
629 checkPWMSensor(sensorPath, labelHead, *interfacePath,
630 dbusConnection, objectServer, psuNames[0]);
631 }
632 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530633 {
Joseph Fub844e972023-03-25 10:24:30 +0800634 auto findIIOHyphen = sensorNameStr.find_last_of('_');
635 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530636 }
637
Ed Tanous8a57ec02020-10-09 12:46:52 -0700638 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700639 {
640 std::cerr << "Sensor type=\"" << sensorNameSubStr
641 << "\" label=\"" << labelHead << "\"\n";
642 }
643
Vijay Khemka996bad12019-05-28 15:15:16 -0700644 if (!findLabels.empty())
645 {
646 /* Check if this labelHead is enabled in config file */
647 if (std::find(findLabels.begin(), findLabels.end(),
648 labelHead) == findLabels.end())
649 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700650 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800651 {
652 std::cerr << "could not find " << labelHead
653 << " in the Labels list\n";
654 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700655 continue;
656 }
657 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800658
cchouxe8a14e92023-10-25 23:26:27 +0800659 auto findProperty = labelMatch.find(sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800660 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800661 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700662 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700663 {
664 std::cerr << "Could not find matching default property for "
cchouxe8a14e92023-10-25 23:26:27 +0800665 << sensorNameSubStr << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700666 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800667 continue;
668 }
669
Josh Lehan74d9bd92019-10-31 08:51:58 -0700670 // Protect the hardcoded labelMatch list from changes,
671 // by making a copy and modifying that instead.
672 // Avoid bleedthrough of one device's customizations to
673 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800674 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700675
676 // Use label head as prefix for reading from config file,
677 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
678 std::string keyName = labelHead + "_Name";
679 std::string keyScale = labelHead + "_Scale";
680 std::string keyMin = labelHead + "_Min";
681 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800682 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800683 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700684
685 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700686 auto findCustomName = baseConfig->find(keyName);
687 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700688 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700689 try
690 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800691 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 VariantToStringVisitor(), findCustomName->second);
693 }
Patrick Williams26601e82021-10-06 12:43:25 -0500694 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700695 {
696 std::cerr << "Unable to parse " << keyName << "\n";
697 continue;
698 }
699
700 // All strings are valid, including empty string
701 customizedName = true;
702 }
703
704 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700705 auto findCustomScale = baseConfig->find(keyScale);
706 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700707 {
708 try
709 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800710 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700711 VariantToUnsignedIntVisitor(), findCustomScale->second);
712 }
Patrick Williams26601e82021-10-06 12:43:25 -0500713 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700714 {
715 std::cerr << "Unable to parse " << keyScale << "\n";
716 continue;
717 }
718
719 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800720 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700721 {
722 customizedScale = true;
723 }
724 else
725 {
726 std::cerr << "Unable to accept " << keyScale << "\n";
727 continue;
728 }
729 }
730
Zev Weisse8b97ee2022-08-12 15:23:51 -0700731 auto findCustomMin = baseConfig->find(keyMin);
732 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700733 {
734 try
735 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800736 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 VariantToDoubleVisitor(), findCustomMin->second);
738 }
Patrick Williams26601e82021-10-06 12:43:25 -0500739 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700740 {
741 std::cerr << "Unable to parse " << keyMin << "\n";
742 continue;
743 }
744 }
745
Zev Weisse8b97ee2022-08-12 15:23:51 -0700746 auto findCustomMax = baseConfig->find(keyMax);
747 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700748 {
749 try
750 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800751 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700752 VariantToDoubleVisitor(), findCustomMax->second);
753 }
Patrick Williams26601e82021-10-06 12:43:25 -0500754 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700755 {
756 std::cerr << "Unable to parse " << keyMax << "\n";
757 continue;
758 }
759 }
760
Zev Weisse8b97ee2022-08-12 15:23:51 -0700761 auto findCustomOffset = baseConfig->find(keyOffset);
762 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800763 {
764 try
765 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800766 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800767 VariantToDoubleVisitor(), findCustomOffset->second);
768 }
Patrick Williams26601e82021-10-06 12:43:25 -0500769 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800770 {
771 std::cerr << "Unable to parse " << keyOffset << "\n";
772 continue;
773 }
774 }
775
Lotus Xucb5af732021-09-10 15:18:50 +0800776 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700777 auto findPowerState = baseConfig->find(keyPowerState);
778 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800779 {
780 std::string powerState = std::visit(VariantToStringVisitor(),
781 findPowerState->second);
782 setReadState(powerState, readState);
783 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800784 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700785 {
786 std::cerr << "Min must be less than Max\n";
787 continue;
788 }
789
790 // If the sensor name is being customized by config file,
791 // then prefix/suffix composition becomes not necessary,
792 // and in fact not wanted, because it gets in the way.
793 std::string psuNameFromIndex;
cchouxe8a14e92023-10-25 23:26:27 +0800794 std::string nameIndexStr = "1";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700795 if (!customizedName)
796 {
797 /* Find out sensor name index for this label */
798 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500799 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700800 if (std::regex_search(labelHead, matches, rgx))
801 {
cchouxe8a14e92023-10-25 23:26:27 +0800802 nameIndexStr = matches[1];
803 nameIndex = std::stoi(nameIndexStr);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700804
805 // Decrement to preserve alignment, because hwmon
806 // human-readable filenames and labels use 1-based
807 // numbering, but the "Name", "Name1", "Name2", etc. naming
808 // convention (the psuNames vector) uses 0-based numbering.
809 if (nameIndex > 0)
810 {
811 --nameIndex;
812 }
813 }
814 else
815 {
816 nameIndex = 0;
817 }
818
819 if (psuNames.size() <= nameIndex)
820 {
821 std::cerr << "Could not pair " << labelHead
822 << " with a Name field\n";
823 continue;
824 }
825
826 psuNameFromIndex = psuNames[nameIndex];
827
Ed Tanous8a57ec02020-10-09 12:46:52 -0700828 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700829 {
830 std::cerr << "Sensor label head " << labelHead
831 << " paired with " << psuNameFromIndex
832 << " at index " << nameIndex << "\n";
833 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700834 }
835
Joseph Fub844e972023-03-25 10:24:30 +0800836 if (devType == DevTypes::HWMON)
837 {
838 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
839 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800840
Josh Lehan74d9bd92019-10-31 08:51:58 -0700841 // Similarly, if sensor scaling factor is being customized,
842 // then the below power-of-10 constraint becomes unnecessary,
843 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800844 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700845 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700846 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700847 // Preserve existing usage of hardcoded labelMatch table below
848 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700849
Josh Lehan74d9bd92019-10-31 08:51:58 -0700850 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700851 char firstChar =
852 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700853 std::string strScaleFactor =
854 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
855
856 // Preserve existing configs by accepting earlier syntax,
857 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700858 auto findScaleFactor = baseConfig->find(strScaleFactor);
859 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700860 {
861 factor = std::visit(VariantToIntVisitor(),
862 findScaleFactor->second);
863 }
864
Ed Tanous8a57ec02020-10-09 12:46:52 -0700865 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700866 {
867 std::cerr << "Sensor scaling factor " << factor
868 << " string " << strScaleFactor << "\n";
869 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700870 }
871
Vijay Khemka996bad12019-05-28 15:15:16 -0700872 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530873 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
874 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800875 {
James Feist17ab6e02019-06-25 12:28:13 -0700876 std::cerr << "error populating thresholds for "
877 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800878 }
879
Zev Weiss6b6891c2021-04-22 02:46:21 -0500880 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
881 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800882 {
Jason Ling5747fab2019-10-02 16:46:23 -0700883 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700884 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800885 continue;
886 }
887
Ed Tanous8a57ec02020-10-09 12:46:52 -0700888 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700889 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700890 std::cerr << "Sensor properties: Name \""
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800891 << psuProperty.labelTypeName << "\" Scale "
892 << psuProperty.sensorScaleFactor << " Min "
893 << psuProperty.minReading << " Max "
894 << psuProperty.maxReading << " Offset "
895 << psuProperty.sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700896 }
897
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800898 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700899 if (customizedName)
900 {
901 if (sensorName.empty())
902 {
903 // Allow selective disabling of an individual sensor,
904 // by customizing its name to an empty string.
905 std::cerr << "Sensor disabled, empty string\n";
906 continue;
907 }
908 }
909 else
910 {
911 // Sensor name not customized, do prefix/suffix composition,
912 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800913 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
cchouxe8a14e92023-10-25 23:26:27 +0800914
915 // The labelTypeName of a fan can be:
916 // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
917 if (labelHead == "fan" + nameIndexStr)
918 {
919 sensorName += nameIndexStr;
920 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700921 }
922
Ed Tanous8a57ec02020-10-09 12:46:52 -0700923 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700924 {
925 std::cerr << "Sensor name \"" << sensorName << "\" path \""
926 << sensorPathStr << "\" type \"" << sensorType
927 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700928 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800929 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800930
931 auto& sensor = sensors[sensorName];
932 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700933 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800934 sensor = nullptr;
935 }
936
937 if (sensor != nullptr)
938 {
939 sensor->activate(sensorPathStr, i2cDev);
940 }
941 else
942 {
943 sensors[sensorName] = std::make_shared<PSUSensor>(
944 sensorPathStr, sensorType, objectServer, dbusConnection, io,
945 sensorName, std::move(sensorThresholds), *interfacePath,
946 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800947 psuProperty.maxReading, psuProperty.minReading,
948 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800949 pollRate, i2cDev);
950 sensors[sensorName]->setupRead();
951 ++numCreated;
952 if constexpr (debug)
953 {
954 std::cerr << "Created " << numCreated
955 << " sensors so far\n";
956 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700957 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800958 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800959
Joseph Fub844e972023-03-25 10:24:30 +0800960 if (devType == DevTypes::HWMON)
961 {
962 // OperationalStatus event
963 combineEvents[*psuName + "OperationalStatus"] = nullptr;
964 combineEvents[*psuName + "OperationalStatus"] =
965 std::make_unique<PSUCombineEvent>(
966 objectServer, dbusConnection, io, *psuName, readState,
967 eventPathList, groupEventPathList, "OperationalStatus",
968 pollRate);
969 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800970 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700971
Ed Tanous8a57ec02020-10-09 12:46:52 -0700972 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700973 {
974 std::cerr << "Created total of " << numCreated << " sensors\n";
975 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800976}
Cheng C Yang209ec562019-03-12 16:37:44 +0800977
Matt Simmering39fadb92023-05-04 21:47:29 -0700978static void
979 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
980{
981 static const int depth = 2;
982 static const int numKeys = 1;
983 GetSubTreeType cpuSubTree;
984
985 try
986 {
987 auto getItems = dbusConnection->new_method_call(
988 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
989 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
990 std::array<const char*, numKeys>{
991 "xyz.openbmc_project.Inventory.Item"});
992 auto getItemsResp = dbusConnection->call(getItems);
993 getItemsResp.read(cpuSubTree);
994 }
995 catch (sdbusplus::exception_t& e)
996 {
997 std::cerr << "error getting inventory item subtree: " << e.what()
998 << "\n";
999 return;
1000 }
1001
1002 for (const auto& [path, objDict] : cpuSubTree)
1003 {
1004 auto obj = sdbusplus::message::object_path(path).filename();
1005 if (!obj.starts_with("cpu") || objDict.empty())
1006 {
1007 continue;
1008 }
1009 const std::string& owner = objDict.begin()->first;
1010
1011 std::variant<bool> respValue;
1012 try
1013 {
1014 auto getPresence = dbusConnection->new_method_call(
1015 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1016 "Get");
1017 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1018 auto resp = dbusConnection->call(getPresence);
1019 resp.read(respValue);
1020 }
1021 catch (sdbusplus::exception_t& e)
1022 {
1023 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1024 continue;
1025 }
1026
1027 auto* present = std::get_if<bool>(&respValue);
1028 if (present != nullptr && *present)
1029 {
1030 int cpuIndex = 0;
1031 try
1032 {
1033 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1034 }
1035 catch (const std::exception& e)
1036 {
1037 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1038 continue;
1039 }
1040 cpuPresence[cpuIndex + 1] = *present;
1041 }
1042 }
1043}
1044
Zhikui Ren23c96e72020-11-05 22:32:28 -08001045void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001046 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001047 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1048 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001049 sensorsChanged,
1050 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001051{
1052 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001053 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1054 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001055 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1056 sensorsChanged, activateOnly);
1057 });
Matt Simmering786efb82023-01-18 14:09:21 -08001058 std::vector<std::string> types(sensorTypes.size());
1059 for (const auto& [type, dt] : sensorTypes)
1060 {
1061 types.push_back(type);
1062 }
1063 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001064}
1065
Ed Tanous201a1012024-04-03 18:07:28 -07001066void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001067{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001068 sensorTable = {{"power", sensor_paths::unitWatts},
1069 {"curr", sensor_paths::unitAmperes},
1070 {"temp", sensor_paths::unitDegreesC},
1071 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001072 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001073 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001074
Jeff Line41d52f2021-04-07 19:38:51 +08001075 labelMatch = {
1076 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001077 {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
1078 {"power", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001079 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1080 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1081 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001082 {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1083 {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001084 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001085 {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001086 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001087 {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
1088 {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
1089 {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1090 {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
1091 {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1092 {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001093
1094 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1095 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1096
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001097 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1098 {"Failure", {"in2_alarm"}},
1099 {"ACLost", {"in1_beep"}},
1100 {"ConfigureError", {"in1_fault"}}};
1101
Joseph Fub844e972023-03-25 10:24:30 +08001102 devParamMap = {
1103 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1104 {DevTypes::IIO,
1105 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001106}
1107
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001108static void powerStateChanged(
1109 PowerState type, bool newState,
1110 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1111 sensors,
1112 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1113 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1114{
1115 if (newState)
1116 {
1117 createSensors(io, objectServer, dbusConnection, nullptr, true);
1118 }
1119 else
1120 {
1121 for (auto& [path, sensor] : sensors)
1122 {
1123 if (sensor != nullptr && sensor->readState == type)
1124 {
1125 sensor->deactivate();
1126 }
1127 }
1128 }
1129}
1130
James Feistb6c0b912019-07-09 12:21:44 -07001131int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001132{
Ed Tanous1f978632023-02-28 18:16:39 -08001133 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001134 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1135
Ed Tanous14ed5e92022-07-12 15:50:23 -07001136 sdbusplus::asio::object_server objectServer(systemBus, true);
1137 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001138 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001139 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001140 auto sensorsChanged =
1141 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001142
Cheng C Yang916360b2019-05-07 18:47:16 +08001143 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001144
Patrick Williams597e8422023-10-20 11:19:01 -05001145 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1146 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001147 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1148 };
1149
1150 setupPowerMatchCallback(systemBus, powerCallBack);
1151
1152 boost::asio::post(io, [&]() {
1153 createSensors(io, objectServer, systemBus, nullptr, false);
1154 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001155 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001156 std::function<void(sdbusplus::message_t&)> eventHandler =
1157 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001158 if (message.is_method_error())
1159 {
1160 std::cerr << "callback method error\n";
1161 return;
1162 }
1163 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001164 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001165 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1166 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001167 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001168 return;
1169 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001170 if (ec)
1171 {
1172 std::cerr << "timer error\n";
1173 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001174 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001175 });
1176 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001177
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001178 boost::asio::steady_timer cpuFilterTimer(io);
1179 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1180 [&](sdbusplus::message_t& message) {
1181 std::string path = message.get_path();
1182 boost::to_lower(path);
1183
1184 sdbusplus::message::object_path cpuPath(path);
1185 std::string cpuName = cpuPath.filename();
1186 if (!cpuName.starts_with("cpu"))
1187 {
1188 return;
1189 }
1190 size_t index = 0;
1191 try
1192 {
1193 index = std::stoi(path.substr(path.size() - 1));
1194 }
1195 catch (const std::invalid_argument&)
1196 {
1197 std::cerr << "Found invalid path " << path << "\n";
1198 return;
1199 }
1200
1201 std::string objectName;
1202 boost::container::flat_map<std::string, std::variant<bool>> values;
1203 message.read(objectName, values);
1204 auto findPresence = values.find("Present");
1205 try
1206 {
1207 cpuPresence[index] = std::get<bool>(findPresence->second);
1208 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001209 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001210 {
1211 return;
1212 }
1213
Chris Sides3fce1bf2023-04-03 16:57:05 -05001214 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001215 {
1216 return;
1217 }
1218 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1219 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1220 if (ec == boost::asio::error::operation_aborted)
1221 {
1222 return;
1223 }
1224 if (ec)
1225 {
1226 std::cerr << "timer error\n";
1227 return;
1228 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001229 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001230 });
1231 };
1232
Zev Weiss214d9712022-08-12 12:54:31 -07001233 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1234 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001235
1236 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1237 static_cast<sdbusplus::bus_t&>(*systemBus),
1238 "type='signal',member='PropertiesChanged',path_namespace='" +
1239 std::string(cpuInventoryPath) +
1240 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1241 cpuPresenceHandler));
1242
Matt Simmering39fadb92023-05-04 21:47:29 -07001243 getPresentCpus(systemBus);
1244
Bruce Lee1263c3d2021-06-04 15:16:33 +08001245 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001246 io.run();
1247}