blob: 94ef530f8a4b175a62cf5af6aa05960fce8b115c [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}},
Allen.Wang8c13c282024-07-03 09:42:54 +0800100 {"MAX11617", I2CDeviceType{"max11617", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800101 {"MAX16601", I2CDeviceType{"max16601", true}},
102 {"MAX20710", I2CDeviceType{"max20710", true}},
103 {"MAX20730", I2CDeviceType{"max20730", true}},
104 {"MAX20734", I2CDeviceType{"max20734", true}},
105 {"MAX20796", I2CDeviceType{"max20796", true}},
106 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800107 {"MP2856", I2CDeviceType{"mp2856", true}},
108 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800109 {"MP2971", I2CDeviceType{"mp2971", true}},
110 {"MP2973", I2CDeviceType{"mp2973", true}},
111 {"MP2975", I2CDeviceType{"mp2975", true}},
112 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800113 {"MP5990", I2CDeviceType{"mp5990", true}},
Jeff Lind8e48df2024-06-07 09:06:56 +0800114 {"MPQ8785", I2CDeviceType{"mpq8785", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800115 {"NCP4200", I2CDeviceType{"ncp4200", true}},
116 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
117 {"pmbus", I2CDeviceType{"pmbus", true}},
118 {"PXE1610", I2CDeviceType{"pxe1610", true}},
119 {"RAA228000", I2CDeviceType{"raa228000", true}},
Potin Lai6e6618f2024-05-16 15:02:56 +0800120 {"RAA228004", I2CDeviceType{"raa228004", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800121 {"RAA228228", I2CDeviceType{"raa228228", true}},
122 {"RAA228620", I2CDeviceType{"raa228620", true}},
123 {"RAA229001", I2CDeviceType{"raa229001", true}},
124 {"RAA229004", I2CDeviceType{"raa229004", true}},
125 {"RAA229126", I2CDeviceType{"raa229126", true}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800126 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200127 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800128 {"TPS53679", I2CDeviceType{"tps53679", true}},
129 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800130 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800131 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
132 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800133 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800134};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700135
Joseph Fub844e972023-03-25 10:24:30 +0800136enum class DevTypes
137{
138 Unknown = 0,
139 HWMON,
140 IIO
141};
142
143struct DevParams
144{
145 unsigned int matchIndex = 0;
146 std::string matchRegEx;
147 std::string nameRegEx;
148};
149
Cheng C Yang209ec562019-03-12 16:37:44 +0800150namespace fs = std::filesystem;
151
Yong Libf8b1da2020-04-15 16:32:50 +0800152static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800153 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800154static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
155 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800156static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
157 pwmSensors;
158static boost::container::flat_map<std::string, std::string> sensorTable;
159static boost::container::flat_map<std::string, PSUProperty> labelMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800160static EventPathList eventMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800161static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800162
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800163static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800164static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700165
Cheng C Yang58b2b532019-05-31 00:19:45 +0800166// Function CheckEvent will check each attribute from eventMatch table in the
167// sysfs. If the attributes exists in sysfs, then store the complete path
168// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800169void checkEvent(const std::string& directory, const EventPathList& eventMatch,
170 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800171{
172 for (const auto& match : eventMatch)
173 {
174 const std::vector<std::string>& eventAttrs = match.second;
175 const std::string& eventName = match.first;
176 for (const auto& eventAttr : eventAttrs)
177 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700178 std::string eventPath = directory;
179 eventPath += "/";
180 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800181
182 std::ifstream eventFile(eventPath);
183 if (!eventFile.good())
184 {
185 continue;
186 }
187
188 eventPathList[eventName].push_back(eventPath);
189 }
190 }
191}
192
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800193// Check Group Events which contains more than one targets in each combine
194// events.
George Liu5b3542e2023-10-31 17:27:12 +0800195void checkGroupEvent(const std::string& directory,
George Liu5b3542e2023-10-31 17:27:12 +0800196 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800197{
cchouxe8a14e92023-10-25 23:26:27 +0800198 EventPathList pathList;
199 std::vector<fs::path> eventPaths;
200 if (!findFiles(fs::path(directory), R"(fan\d+_(alarm|fault))", eventPaths))
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800201 {
cchouxe8a14e92023-10-25 23:26:27 +0800202 return;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800203 }
cchouxe8a14e92023-10-25 23:26:27 +0800204
205 for (const auto& eventPath : eventPaths)
206 {
207 std::string attrName = eventPath.filename();
208 pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
209 }
210 groupEventPathList["FanFault"] = pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800211}
212
Cheng C Yang58b2b532019-05-31 00:19:45 +0800213// Function checkEventLimits will check all the psu related xxx_input attributes
214// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
215// xxx_min_alarm exist, then store the existing paths of the alarm attributes
216// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800217void checkEventLimits(const std::string& sensorPathStr,
218 const EventPathList& limitEventMatch,
219 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800220{
Lei YUa2c7cea2020-12-23 14:07:28 +0800221 auto attributePartPos = sensorPathStr.find_last_of('_');
222 if (attributePartPos == std::string::npos)
223 {
224 // There is no '_' in the string, skip it
225 return;
226 }
227 auto attributePart =
228 std::string_view(sensorPathStr).substr(attributePartPos + 1);
229 if (attributePart != "input")
230 {
231 // If the sensor is not xxx_input, skip it
232 return;
233 }
234
235 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800236 for (const auto& limitMatch : limitEventMatch)
237 {
238 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
239 const std::string& eventName = limitMatch.first;
240 for (const auto& limitEventAttr : limitEventAttrs)
241 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800242 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800243 std::ifstream eventFile(limitEventPath);
244 if (!eventFile.good())
245 {
246 continue;
247 }
248 eventPathList[eventName].push_back(limitEventPath);
249 }
250 }
251}
Cheng C Yang916360b2019-05-07 18:47:16 +0800252
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530253static void
254 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
255 const std::string& interfacePath,
256 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
257 sdbusplus::asio::object_server& objectServer,
258 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800259{
cchouxe8a14e92023-10-25 23:26:27 +0800260 if (!labelHead.starts_with("fan"))
Cheng C Yang916360b2019-05-07 18:47:16 +0800261 {
cchouxe8a14e92023-10-25 23:26:27 +0800262 return;
Cheng C Yang916360b2019-05-07 18:47:16 +0800263 }
cchouxe8a14e92023-10-25 23:26:27 +0800264 std::string labelHeadIndex = labelHead.substr(3);
265
266 const std::string& sensorPathStr = sensorPath.string();
267 const std::string& pwmPathStr = boost::replace_all_copy(sensorPathStr,
268 "input", "target");
269 std::ifstream pwmFile(pwmPathStr);
270 if (!pwmFile.good())
271 {
272 return;
273 }
274
275 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
276 if (findPWMSensor != pwmSensors.end())
277 {
278 return;
279 }
280
281 std::string name = "Pwm_";
282 name += psuName;
283 name += "_Fan_";
284 name += labelHeadIndex;
285
286 std::string objPath = interfacePath;
287 objPath += "_Fan_";
288 objPath += labelHeadIndex;
289
290 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
291 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800292}
293
Zhikui Ren23c96e72020-11-05 22:32:28 -0800294static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800295 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800296 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
297 const ManagedObjectType& sensorConfigs,
298 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800299 sensorsChanged,
300 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800301{
Josh Lehan49cfba92019-10-08 16:50:42 -0700302 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800303 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800304
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800305 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
306
Cheng C Yang209ec562019-03-12 16:37:44 +0800307 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800308 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
309 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
310 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800311 {
312 std::cerr << "No PSU sensors in system\n";
313 return;
314 }
315
316 boost::container::flat_set<std::string> directories;
317 for (const auto& pmbusPath : pmbusPaths)
318 {
George Liu5b3542e2023-10-31 17:27:12 +0800319 EventPathList eventPathList;
320 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800321
322 std::ifstream nameFile(pmbusPath);
323 if (!nameFile.good())
324 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700325 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800326 continue;
327 }
328
329 std::string pmbusName;
330 std::getline(nameFile, pmbusName);
331 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700332
Matt Simmering786efb82023-01-18 14:09:21 -0800333 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800334 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700335 // To avoid this error message, add your driver name to
336 // the pmbusNames vector at the top of this file.
337 std::cerr << "Driver name " << pmbusName
338 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800339 continue;
340 }
341
Cheng C Yang209ec562019-03-12 16:37:44 +0800342 auto directory = pmbusPath.parent_path();
343
344 auto ret = directories.insert(directory.string());
345 if (!ret.second)
346 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700347 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800348 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800349 }
350
Joseph Fub844e972023-03-25 10:24:30 +0800351 DevTypes devType = DevTypes::HWMON;
352 std::string deviceName;
353 if (directory.parent_path() == "/sys/class/hwmon")
354 {
355 deviceName = fs::canonical(directory / "device").stem();
356 }
357 else
358 {
359 deviceName = fs::canonical(directory).parent_path().stem();
360 devType = DevTypes::IIO;
361 }
362
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 size_t bus = 0;
364 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000365 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800366 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800367 continue;
368 }
369
Zev Weisse8b97ee2022-08-12 15:23:51 -0700370 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800371 const SensorData* sensorData = nullptr;
372 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800373 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800374 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800375
Zev Weiss741f26e2022-08-12 18:21:02 -0700376 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800377 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700378 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800379 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 {
Zev Weiss054aad82022-08-18 01:37:34 -0700381 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800382 if (sensorBase != sensorData->end())
383 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700384 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800385 sensorType = type;
386 break;
387 }
388 }
389 if (baseConfig == nullptr)
390 {
391 std::cerr << "error finding base configuration for "
392 << deviceName << "\n";
393 continue;
394 }
395
Zev Weisse8b97ee2022-08-12 15:23:51 -0700396 auto configBus = baseConfig->find("Bus");
397 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800398
Zev Weisse8b97ee2022-08-12 15:23:51 -0700399 if (configBus == baseConfig->end() ||
400 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800401 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800402 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800403 continue;
404 }
405
Matt Simmering6747eba2023-01-18 16:03:55 -0800406 const uint64_t* confBus =
407 std::get_if<uint64_t>(&(configBus->second));
408 const uint64_t* confAddr =
409 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800410 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800411 {
412 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700413 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800414 continue;
415 }
416
417 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800418 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200419 if constexpr (debug)
420 {
421 std::cerr << "Configuration skipping " << *confBus << "-"
422 << *confAddr << " because not " << bus << "-"
423 << addr << "\n";
424 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 continue;
426 }
427
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800428 std::vector<thresholds::Threshold> confThresholds;
429 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
430 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800431 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800432 }
433 thresholdConfSize = confThresholds.size();
434
Zev Weiss741f26e2022-08-12 18:21:02 -0700435 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800436 break;
437 }
438 if (interfacePath == nullptr)
439 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700440 // To avoid this error message, add your export map entry,
441 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800442 std::cerr << "failed to find match for " << deviceName << "\n";
443 continue;
444 }
445
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800446 auto findI2CDev = devices.find(*interfacePath);
447
448 std::shared_ptr<I2CDevice> i2cDev;
449 if (findI2CDev != devices.end())
450 {
451 if (activateOnly && !findI2CDev->second.second)
452 {
453 continue;
454 }
455 i2cDev = findI2CDev->second.first;
456 }
457
Zev Weisse8b97ee2022-08-12 15:23:51 -0700458 auto findPSUName = baseConfig->find("Name");
459 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800460 {
461 std::cerr << "could not determine configuration name for "
462 << deviceName << "\n";
463 continue;
464 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800465 const std::string* psuName =
466 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800467 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800468 {
469 std::cerr << "Cannot find psu name, invalid configuration\n";
470 continue;
471 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800472
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800473 auto findCPU = baseConfig->find("CPURequired");
474 if (findCPU != baseConfig->end())
475 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500476 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800477 auto presenceFind = cpuPresence.find(index);
478 if (presenceFind == cpuPresence.end() || !presenceFind->second)
479 {
480 continue;
481 }
482 }
483
Zhikui Ren23c96e72020-11-05 22:32:28 -0800484 // on rescans, only update sensors we were signaled by
485 if (!firstScan)
486 {
Zhikui Renda98f092021-11-01 09:41:08 -0700487 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500488 auto it = std::find_if(sensorsChanged->begin(),
489 sensorsChanged->end(),
490 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800491 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500492 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800493
494 if (it == sensorsChanged->end())
495 {
496 continue;
497 }
498 sensorsChanged->erase(it);
499 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800500 checkEvent(directory.string(), eventMatch, eventPathList);
cchouxe8a14e92023-10-25 23:26:27 +0800501 checkGroupEvent(directory.string(), groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800502
Zev Weisse8b97ee2022-08-12 15:23:51 -0700503 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000504
Vijay Khemka996bad12019-05-28 15:15:16 -0700505 /* Check if there are more sensors in the same interface */
506 int i = 1;
507 std::vector<std::string> psuNames;
508 do
509 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700510 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700511 psuNames.push_back(
512 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700513 findPSUName = baseConfig->find("Name" + std::to_string(i++));
514 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700515
Cheng C Yange50345b2019-04-02 17:26:15 +0800516 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800517 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
518 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800519 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800520 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800521 continue;
522 }
523
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530524 /* read max value in sysfs for in, curr, power, temp, ... */
525 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
526 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700527 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530528 {
529 std::cerr << "No max name in PSU \n";
530 }
531 }
532
Zev Weiss8569bf22022-10-11 15:37:44 -0700533 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800534
Vijay Khemka996bad12019-05-28 15:15:16 -0700535 /* Find array of labels to be exposed if it is defined in config */
536 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700537 auto findLabelObj = baseConfig->find("Labels");
538 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700539 {
540 findLabels =
541 std::get<std::vector<std::string>>(findLabelObj->second);
542 }
543
Joseph Fub844e972023-03-25 10:24:30 +0800544 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700545 std::smatch matches;
546
Cheng C Yange50345b2019-04-02 17:26:15 +0800547 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800548 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530549 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800550 std::string labelHead;
551 std::string sensorPathStr = sensorPath.string();
552 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700553 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700554 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
555 {
Josh Lehan06494452019-10-31 09:49:16 -0700556 // hwmon *_input filename without number:
557 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800558 // iio in_*_raw filename without number:
559 // voltage, temp, pressure, ...
560 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700561 }
562 else
563 {
Josh Lehan06494452019-10-31 09:49:16 -0700564 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700565 << sensorNameStr;
566 continue;
567 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800568
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530569 std::string labelPath;
570
Joseph Fub844e972023-03-25 10:24:30 +0800571 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530572 {
Joseph Fub844e972023-03-25 10:24:30 +0800573 /* find and differentiate _max and _input to replace "label" */
574 size_t pos = sensorPathStr.find('_');
575 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530576 {
Joseph Fub844e972023-03-25 10:24:30 +0800577 std::string sensorPathStrMax = sensorPathStr.substr(pos);
578 if (sensorPathStrMax == "_max")
579 {
580 labelPath = boost::replace_all_copy(sensorPathStr,
581 "max", "label");
582 maxLabel = true;
583 }
584 else
585 {
586 labelPath = boost::replace_all_copy(sensorPathStr,
587 "input", "label");
588 maxLabel = false;
589 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530590 }
591 else
592 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800593 continue;
594 }
595
Joseph Fub844e972023-03-25 10:24:30 +0800596 std::ifstream labelFile(labelPath);
597 if (!labelFile.good())
598 {
599 if constexpr (debug)
600 {
601 std::cerr << "Input file " << sensorPath
602 << " has no corresponding label file\n";
603 }
604 // hwmon *_input filename with number:
605 // temp1, temp2, temp3, ...
606 labelHead = sensorNameStr.substr(0,
607 sensorNameStr.find('_'));
608 }
609 else
610 {
611 std::string label;
612 std::getline(labelFile, label);
613 labelFile.close();
614 auto findSensor = sensors.find(label);
615 if (findSensor != sensors.end())
616 {
617 continue;
618 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800619
Joseph Fub844e972023-03-25 10:24:30 +0800620 // hwmon corresponding *_label file contents:
621 // vin1, vout1, ...
622 labelHead = label.substr(0, label.find(' '));
623 }
624
625 /* append "max" for labelMatch */
626 if (maxLabel)
627 {
628 labelHead.insert(0, "max");
629 }
630
631 checkPWMSensor(sensorPath, labelHead, *interfacePath,
632 dbusConnection, objectServer, psuNames[0]);
633 }
634 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530635 {
Joseph Fub844e972023-03-25 10:24:30 +0800636 auto findIIOHyphen = sensorNameStr.find_last_of('_');
637 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530638 }
639
Ed Tanous8a57ec02020-10-09 12:46:52 -0700640 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700641 {
642 std::cerr << "Sensor type=\"" << sensorNameSubStr
643 << "\" label=\"" << labelHead << "\"\n";
644 }
645
Vijay Khemka996bad12019-05-28 15:15:16 -0700646 if (!findLabels.empty())
647 {
648 /* Check if this labelHead is enabled in config file */
649 if (std::find(findLabels.begin(), findLabels.end(),
650 labelHead) == findLabels.end())
651 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700652 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800653 {
654 std::cerr << "could not find " << labelHead
655 << " in the Labels list\n";
656 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700657 continue;
658 }
659 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800660
cchouxe8a14e92023-10-25 23:26:27 +0800661 auto findProperty = labelMatch.find(sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800662 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800663 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700664 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700665 {
666 std::cerr << "Could not find matching default property for "
cchouxe8a14e92023-10-25 23:26:27 +0800667 << sensorNameSubStr << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700668 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800669 continue;
670 }
671
Josh Lehan74d9bd92019-10-31 08:51:58 -0700672 // Protect the hardcoded labelMatch list from changes,
673 // by making a copy and modifying that instead.
674 // Avoid bleedthrough of one device's customizations to
675 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800676 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700677
678 // Use label head as prefix for reading from config file,
679 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
680 std::string keyName = labelHead + "_Name";
681 std::string keyScale = labelHead + "_Scale";
682 std::string keyMin = labelHead + "_Min";
683 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800684 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800685 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700686
687 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700688 auto findCustomName = baseConfig->find(keyName);
689 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700690 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700691 try
692 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800693 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700694 VariantToStringVisitor(), findCustomName->second);
695 }
Patrick Williams26601e82021-10-06 12:43:25 -0500696 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700697 {
698 std::cerr << "Unable to parse " << keyName << "\n";
699 continue;
700 }
701
702 // All strings are valid, including empty string
703 customizedName = true;
704 }
705
706 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700707 auto findCustomScale = baseConfig->find(keyScale);
708 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700709 {
710 try
711 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800712 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700713 VariantToUnsignedIntVisitor(), findCustomScale->second);
714 }
Patrick Williams26601e82021-10-06 12:43:25 -0500715 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700716 {
717 std::cerr << "Unable to parse " << keyScale << "\n";
718 continue;
719 }
720
721 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800722 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700723 {
724 customizedScale = true;
725 }
726 else
727 {
728 std::cerr << "Unable to accept " << keyScale << "\n";
729 continue;
730 }
731 }
732
Zev Weisse8b97ee2022-08-12 15:23:51 -0700733 auto findCustomMin = baseConfig->find(keyMin);
734 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700735 {
736 try
737 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800738 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700739 VariantToDoubleVisitor(), findCustomMin->second);
740 }
Patrick Williams26601e82021-10-06 12:43:25 -0500741 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700742 {
743 std::cerr << "Unable to parse " << keyMin << "\n";
744 continue;
745 }
746 }
747
Zev Weisse8b97ee2022-08-12 15:23:51 -0700748 auto findCustomMax = baseConfig->find(keyMax);
749 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700750 {
751 try
752 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800753 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700754 VariantToDoubleVisitor(), findCustomMax->second);
755 }
Patrick Williams26601e82021-10-06 12:43:25 -0500756 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700757 {
758 std::cerr << "Unable to parse " << keyMax << "\n";
759 continue;
760 }
761 }
762
Zev Weisse8b97ee2022-08-12 15:23:51 -0700763 auto findCustomOffset = baseConfig->find(keyOffset);
764 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800765 {
766 try
767 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800768 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800769 VariantToDoubleVisitor(), findCustomOffset->second);
770 }
Patrick Williams26601e82021-10-06 12:43:25 -0500771 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800772 {
773 std::cerr << "Unable to parse " << keyOffset << "\n";
774 continue;
775 }
776 }
777
Lotus Xucb5af732021-09-10 15:18:50 +0800778 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700779 auto findPowerState = baseConfig->find(keyPowerState);
780 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800781 {
782 std::string powerState = std::visit(VariantToStringVisitor(),
783 findPowerState->second);
784 setReadState(powerState, readState);
785 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800786 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700787 {
788 std::cerr << "Min must be less than Max\n";
789 continue;
790 }
791
792 // If the sensor name is being customized by config file,
793 // then prefix/suffix composition becomes not necessary,
794 // and in fact not wanted, because it gets in the way.
795 std::string psuNameFromIndex;
cchouxe8a14e92023-10-25 23:26:27 +0800796 std::string nameIndexStr = "1";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700797 if (!customizedName)
798 {
799 /* Find out sensor name index for this label */
800 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500801 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700802 if (std::regex_search(labelHead, matches, rgx))
803 {
cchouxe8a14e92023-10-25 23:26:27 +0800804 nameIndexStr = matches[1];
805 nameIndex = std::stoi(nameIndexStr);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700806
807 // Decrement to preserve alignment, because hwmon
808 // human-readable filenames and labels use 1-based
809 // numbering, but the "Name", "Name1", "Name2", etc. naming
810 // convention (the psuNames vector) uses 0-based numbering.
811 if (nameIndex > 0)
812 {
813 --nameIndex;
814 }
815 }
816 else
817 {
818 nameIndex = 0;
819 }
820
821 if (psuNames.size() <= nameIndex)
822 {
823 std::cerr << "Could not pair " << labelHead
824 << " with a Name field\n";
825 continue;
826 }
827
828 psuNameFromIndex = psuNames[nameIndex];
829
Ed Tanous8a57ec02020-10-09 12:46:52 -0700830 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700831 {
832 std::cerr << "Sensor label head " << labelHead
833 << " paired with " << psuNameFromIndex
834 << " at index " << nameIndex << "\n";
835 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700836 }
837
Joseph Fub844e972023-03-25 10:24:30 +0800838 if (devType == DevTypes::HWMON)
839 {
840 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
841 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800842
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 // Similarly, if sensor scaling factor is being customized,
844 // then the below power-of-10 constraint becomes unnecessary,
845 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800846 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700847 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700848 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700849 // Preserve existing usage of hardcoded labelMatch table below
850 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700851
Josh Lehan74d9bd92019-10-31 08:51:58 -0700852 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700853 char firstChar =
854 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700855 std::string strScaleFactor =
856 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
857
858 // Preserve existing configs by accepting earlier syntax,
859 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700860 auto findScaleFactor = baseConfig->find(strScaleFactor);
861 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700862 {
863 factor = std::visit(VariantToIntVisitor(),
864 findScaleFactor->second);
865 }
866
Ed Tanous8a57ec02020-10-09 12:46:52 -0700867 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700868 {
869 std::cerr << "Sensor scaling factor " << factor
870 << " string " << strScaleFactor << "\n";
871 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700872 }
873
Vijay Khemka996bad12019-05-28 15:15:16 -0700874 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530875 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
876 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800877 {
James Feist17ab6e02019-06-25 12:28:13 -0700878 std::cerr << "error populating thresholds for "
879 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800880 }
881
Zev Weiss6b6891c2021-04-22 02:46:21 -0500882 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
883 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800884 {
Jason Ling5747fab2019-10-02 16:46:23 -0700885 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700886 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800887 continue;
888 }
889
Ed Tanous8a57ec02020-10-09 12:46:52 -0700890 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700891 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700892 std::cerr << "Sensor properties: Name \""
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800893 << psuProperty.labelTypeName << "\" Scale "
894 << psuProperty.sensorScaleFactor << " Min "
895 << psuProperty.minReading << " Max "
896 << psuProperty.maxReading << " Offset "
897 << psuProperty.sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700898 }
899
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800900 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700901 if (customizedName)
902 {
903 if (sensorName.empty())
904 {
905 // Allow selective disabling of an individual sensor,
906 // by customizing its name to an empty string.
907 std::cerr << "Sensor disabled, empty string\n";
908 continue;
909 }
910 }
911 else
912 {
913 // Sensor name not customized, do prefix/suffix composition,
914 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800915 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
cchouxe8a14e92023-10-25 23:26:27 +0800916
917 // The labelTypeName of a fan can be:
918 // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
919 if (labelHead == "fan" + nameIndexStr)
920 {
921 sensorName += nameIndexStr;
922 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700923 }
924
Ed Tanous8a57ec02020-10-09 12:46:52 -0700925 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700926 {
927 std::cerr << "Sensor name \"" << sensorName << "\" path \""
928 << sensorPathStr << "\" type \"" << sensorType
929 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700930 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800931 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800932
933 auto& sensor = sensors[sensorName];
934 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700935 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800936 sensor = nullptr;
937 }
938
939 if (sensor != nullptr)
940 {
941 sensor->activate(sensorPathStr, i2cDev);
942 }
943 else
944 {
945 sensors[sensorName] = std::make_shared<PSUSensor>(
946 sensorPathStr, sensorType, objectServer, dbusConnection, io,
947 sensorName, std::move(sensorThresholds), *interfacePath,
948 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800949 psuProperty.maxReading, psuProperty.minReading,
950 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800951 pollRate, i2cDev);
952 sensors[sensorName]->setupRead();
953 ++numCreated;
954 if constexpr (debug)
955 {
956 std::cerr << "Created " << numCreated
957 << " sensors so far\n";
958 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700959 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800960 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800961
Joseph Fub844e972023-03-25 10:24:30 +0800962 if (devType == DevTypes::HWMON)
963 {
964 // OperationalStatus event
965 combineEvents[*psuName + "OperationalStatus"] = nullptr;
966 combineEvents[*psuName + "OperationalStatus"] =
967 std::make_unique<PSUCombineEvent>(
968 objectServer, dbusConnection, io, *psuName, readState,
969 eventPathList, groupEventPathList, "OperationalStatus",
970 pollRate);
971 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800972 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700973
Ed Tanous8a57ec02020-10-09 12:46:52 -0700974 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700975 {
976 std::cerr << "Created total of " << numCreated << " sensors\n";
977 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800978}
Cheng C Yang209ec562019-03-12 16:37:44 +0800979
Matt Simmering39fadb92023-05-04 21:47:29 -0700980static void
981 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
982{
983 static const int depth = 2;
984 static const int numKeys = 1;
985 GetSubTreeType cpuSubTree;
986
987 try
988 {
989 auto getItems = dbusConnection->new_method_call(
990 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
991 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
992 std::array<const char*, numKeys>{
993 "xyz.openbmc_project.Inventory.Item"});
994 auto getItemsResp = dbusConnection->call(getItems);
995 getItemsResp.read(cpuSubTree);
996 }
997 catch (sdbusplus::exception_t& e)
998 {
999 std::cerr << "error getting inventory item subtree: " << e.what()
1000 << "\n";
1001 return;
1002 }
1003
1004 for (const auto& [path, objDict] : cpuSubTree)
1005 {
1006 auto obj = sdbusplus::message::object_path(path).filename();
Patrick Rudolph457715b2024-04-10 14:51:35 +02001007 boost::to_lower(obj);
1008
Matt Simmering39fadb92023-05-04 21:47:29 -07001009 if (!obj.starts_with("cpu") || objDict.empty())
1010 {
1011 continue;
1012 }
1013 const std::string& owner = objDict.begin()->first;
1014
1015 std::variant<bool> respValue;
1016 try
1017 {
1018 auto getPresence = dbusConnection->new_method_call(
1019 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1020 "Get");
1021 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1022 auto resp = dbusConnection->call(getPresence);
1023 resp.read(respValue);
1024 }
1025 catch (sdbusplus::exception_t& e)
1026 {
1027 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1028 continue;
1029 }
1030
1031 auto* present = std::get_if<bool>(&respValue);
1032 if (present != nullptr && *present)
1033 {
1034 int cpuIndex = 0;
1035 try
1036 {
Patrick Rudolph457715b2024-04-10 14:51:35 +02001037 cpuIndex = std::stoi(obj.substr(obj.size() - 1));
Matt Simmering39fadb92023-05-04 21:47:29 -07001038 }
1039 catch (const std::exception& e)
1040 {
1041 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1042 continue;
1043 }
Patrick Rudolph457715b2024-04-10 14:51:35 +02001044 cpuPresence[cpuIndex] = *present;
Matt Simmering39fadb92023-05-04 21:47:29 -07001045 }
1046 }
1047}
1048
Zhikui Ren23c96e72020-11-05 22:32:28 -08001049void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001050 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001051 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1052 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001053 sensorsChanged,
1054 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001055{
1056 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001057 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1058 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001059 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1060 sensorsChanged, activateOnly);
1061 });
Matt Simmering786efb82023-01-18 14:09:21 -08001062 std::vector<std::string> types(sensorTypes.size());
1063 for (const auto& [type, dt] : sensorTypes)
1064 {
1065 types.push_back(type);
1066 }
1067 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001068}
1069
Ed Tanous201a1012024-04-03 18:07:28 -07001070void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001071{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001072 sensorTable = {{"power", sensor_paths::unitWatts},
1073 {"curr", sensor_paths::unitAmperes},
1074 {"temp", sensor_paths::unitDegreesC},
1075 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001076 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001077 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001078
Jeff Line41d52f2021-04-07 19:38:51 +08001079 labelMatch = {
1080 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001081 {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
1082 {"power", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001083 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1084 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1085 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001086 {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1087 {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001088 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001089 {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001090 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001091 {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
1092 {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
1093 {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1094 {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
1095 {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1096 {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001097
1098 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1099 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1100
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001101 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1102 {"Failure", {"in2_alarm"}},
1103 {"ACLost", {"in1_beep"}},
1104 {"ConfigureError", {"in1_fault"}}};
1105
Joseph Fub844e972023-03-25 10:24:30 +08001106 devParamMap = {
1107 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1108 {DevTypes::IIO,
1109 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001110}
1111
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001112static void powerStateChanged(
1113 PowerState type, bool newState,
1114 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1115 sensors,
1116 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1117 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1118{
1119 if (newState)
1120 {
1121 createSensors(io, objectServer, dbusConnection, nullptr, true);
1122 }
1123 else
1124 {
1125 for (auto& [path, sensor] : sensors)
1126 {
1127 if (sensor != nullptr && sensor->readState == type)
1128 {
1129 sensor->deactivate();
1130 }
1131 }
1132 }
1133}
1134
James Feistb6c0b912019-07-09 12:21:44 -07001135int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001136{
Ed Tanous1f978632023-02-28 18:16:39 -08001137 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001138 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1139
Ed Tanous14ed5e92022-07-12 15:50:23 -07001140 sdbusplus::asio::object_server objectServer(systemBus, true);
1141 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001142 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001143 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001144 auto sensorsChanged =
1145 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001146
Cheng C Yang916360b2019-05-07 18:47:16 +08001147 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001148
Patrick Williams597e8422023-10-20 11:19:01 -05001149 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1150 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001151 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1152 };
1153
1154 setupPowerMatchCallback(systemBus, powerCallBack);
1155
1156 boost::asio::post(io, [&]() {
1157 createSensors(io, objectServer, systemBus, nullptr, false);
1158 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001159 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001160 std::function<void(sdbusplus::message_t&)> eventHandler =
1161 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001162 if (message.is_method_error())
1163 {
1164 std::cerr << "callback method error\n";
1165 return;
1166 }
1167 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001168 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001169 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1170 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001171 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001172 return;
1173 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001174 if (ec)
1175 {
1176 std::cerr << "timer error\n";
1177 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001178 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001179 });
1180 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001181
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001182 boost::asio::steady_timer cpuFilterTimer(io);
1183 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1184 [&](sdbusplus::message_t& message) {
1185 std::string path = message.get_path();
1186 boost::to_lower(path);
1187
1188 sdbusplus::message::object_path cpuPath(path);
1189 std::string cpuName = cpuPath.filename();
1190 if (!cpuName.starts_with("cpu"))
1191 {
1192 return;
1193 }
1194 size_t index = 0;
1195 try
1196 {
1197 index = std::stoi(path.substr(path.size() - 1));
1198 }
1199 catch (const std::invalid_argument&)
1200 {
1201 std::cerr << "Found invalid path " << path << "\n";
1202 return;
1203 }
1204
1205 std::string objectName;
1206 boost::container::flat_map<std::string, std::variant<bool>> values;
1207 message.read(objectName, values);
1208 auto findPresence = values.find("Present");
1209 try
1210 {
1211 cpuPresence[index] = std::get<bool>(findPresence->second);
1212 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001213 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001214 {
1215 return;
1216 }
1217
Chris Sides3fce1bf2023-04-03 16:57:05 -05001218 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001219 {
1220 return;
1221 }
1222 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1223 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1224 if (ec == boost::asio::error::operation_aborted)
1225 {
1226 return;
1227 }
1228 if (ec)
1229 {
1230 std::cerr << "timer error\n";
1231 return;
1232 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001233 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001234 });
1235 };
1236
Zev Weiss214d9712022-08-12 12:54:31 -07001237 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1238 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001239
1240 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1241 static_cast<sdbusplus::bus_t&>(*systemBus),
1242 "type='signal',member='PropertiesChanged',path_namespace='" +
1243 std::string(cpuInventoryPath) +
1244 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1245 cpuPresenceHandler));
1246
Matt Simmering39fadb92023-05-04 21:47:29 -07001247 getPresentCpus(systemBus);
1248
Bruce Lee1263c3d2021-06-04 15:16:33 +08001249 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001250 io.run();
1251}