blob: 2b159c3222309de65ded3e40931ceb46e813c2da [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}},
Ian Chienc82a4092024-07-24 10:52:59 +0800100 {"MAX11615", I2CDeviceType{"max11615", false}},
Allen.Wang8c13c282024-07-03 09:42:54 +0800101 {"MAX11617", I2CDeviceType{"max11617", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800102 {"MAX16601", I2CDeviceType{"max16601", true}},
103 {"MAX20710", I2CDeviceType{"max20710", true}},
104 {"MAX20730", I2CDeviceType{"max20730", true}},
105 {"MAX20734", I2CDeviceType{"max20734", true}},
106 {"MAX20796", I2CDeviceType{"max20796", true}},
107 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800108 {"MP2856", I2CDeviceType{"mp2856", true}},
109 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800110 {"MP2971", I2CDeviceType{"mp2971", true}},
111 {"MP2973", I2CDeviceType{"mp2973", true}},
112 {"MP2975", I2CDeviceType{"mp2975", true}},
113 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800114 {"MP5990", I2CDeviceType{"mp5990", true}},
Jeff Lind8e48df2024-06-07 09:06:56 +0800115 {"MPQ8785", I2CDeviceType{"mpq8785", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800116 {"NCP4200", I2CDeviceType{"ncp4200", true}},
117 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
118 {"pmbus", I2CDeviceType{"pmbus", true}},
119 {"PXE1610", I2CDeviceType{"pxe1610", true}},
120 {"RAA228000", I2CDeviceType{"raa228000", true}},
Potin Lai6e6618f2024-05-16 15:02:56 +0800121 {"RAA228004", I2CDeviceType{"raa228004", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800122 {"RAA228228", I2CDeviceType{"raa228228", true}},
123 {"RAA228620", I2CDeviceType{"raa228620", true}},
124 {"RAA229001", I2CDeviceType{"raa229001", true}},
125 {"RAA229004", I2CDeviceType{"raa229004", true}},
126 {"RAA229126", I2CDeviceType{"raa229126", true}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800127 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200128 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800129 {"TPS53679", I2CDeviceType{"tps53679", true}},
130 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800131 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800132 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
133 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800134 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800135};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700136
Joseph Fub844e972023-03-25 10:24:30 +0800137enum class DevTypes
138{
139 Unknown = 0,
140 HWMON,
141 IIO
142};
143
144struct DevParams
145{
146 unsigned int matchIndex = 0;
147 std::string matchRegEx;
148 std::string nameRegEx;
149};
150
Cheng C Yang209ec562019-03-12 16:37:44 +0800151namespace fs = std::filesystem;
152
Yong Libf8b1da2020-04-15 16:32:50 +0800153static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800154 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800155static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
156 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800157static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
158 pwmSensors;
159static boost::container::flat_map<std::string, std::string> sensorTable;
160static boost::container::flat_map<std::string, PSUProperty> labelMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800161static EventPathList eventMatch;
George Liu5b3542e2023-10-31 17:27:12 +0800162static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800163
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800164static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800165static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700166
Cheng C Yang58b2b532019-05-31 00:19:45 +0800167// Function CheckEvent will check each attribute from eventMatch table in the
168// sysfs. If the attributes exists in sysfs, then store the complete path
169// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800170void checkEvent(const std::string& directory, const EventPathList& eventMatch,
171 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800172{
173 for (const auto& match : eventMatch)
174 {
175 const std::vector<std::string>& eventAttrs = match.second;
176 const std::string& eventName = match.first;
177 for (const auto& eventAttr : eventAttrs)
178 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700179 std::string eventPath = directory;
180 eventPath += "/";
181 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800182
183 std::ifstream eventFile(eventPath);
184 if (!eventFile.good())
185 {
186 continue;
187 }
188
189 eventPathList[eventName].push_back(eventPath);
190 }
191 }
192}
193
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800194// Check Group Events which contains more than one targets in each combine
195// events.
George Liu5b3542e2023-10-31 17:27:12 +0800196void checkGroupEvent(const std::string& directory,
George Liu5b3542e2023-10-31 17:27:12 +0800197 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800198{
cchouxe8a14e92023-10-25 23:26:27 +0800199 EventPathList pathList;
200 std::vector<fs::path> eventPaths;
201 if (!findFiles(fs::path(directory), R"(fan\d+_(alarm|fault))", eventPaths))
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800202 {
cchouxe8a14e92023-10-25 23:26:27 +0800203 return;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800204 }
cchouxe8a14e92023-10-25 23:26:27 +0800205
206 for (const auto& eventPath : eventPaths)
207 {
208 std::string attrName = eventPath.filename();
209 pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
210 }
211 groupEventPathList["FanFault"] = pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800212}
213
Cheng C Yang58b2b532019-05-31 00:19:45 +0800214// Function checkEventLimits will check all the psu related xxx_input attributes
215// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
216// xxx_min_alarm exist, then store the existing paths of the alarm attributes
217// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800218void checkEventLimits(const std::string& sensorPathStr,
219 const EventPathList& limitEventMatch,
220 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800221{
Lei YUa2c7cea2020-12-23 14:07:28 +0800222 auto attributePartPos = sensorPathStr.find_last_of('_');
223 if (attributePartPos == std::string::npos)
224 {
225 // There is no '_' in the string, skip it
226 return;
227 }
228 auto attributePart =
229 std::string_view(sensorPathStr).substr(attributePartPos + 1);
230 if (attributePart != "input")
231 {
232 // If the sensor is not xxx_input, skip it
233 return;
234 }
235
236 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800237 for (const auto& limitMatch : limitEventMatch)
238 {
239 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
240 const std::string& eventName = limitMatch.first;
241 for (const auto& limitEventAttr : limitEventAttrs)
242 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800243 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800244 std::ifstream eventFile(limitEventPath);
245 if (!eventFile.good())
246 {
247 continue;
248 }
249 eventPathList[eventName].push_back(limitEventPath);
250 }
251 }
252}
Cheng C Yang916360b2019-05-07 18:47:16 +0800253
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530254static void
255 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
256 const std::string& interfacePath,
257 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
258 sdbusplus::asio::object_server& objectServer,
259 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800260{
cchouxe8a14e92023-10-25 23:26:27 +0800261 if (!labelHead.starts_with("fan"))
Cheng C Yang916360b2019-05-07 18:47:16 +0800262 {
cchouxe8a14e92023-10-25 23:26:27 +0800263 return;
Cheng C Yang916360b2019-05-07 18:47:16 +0800264 }
cchouxe8a14e92023-10-25 23:26:27 +0800265 std::string labelHeadIndex = labelHead.substr(3);
266
267 const std::string& sensorPathStr = sensorPath.string();
268 const std::string& pwmPathStr = boost::replace_all_copy(sensorPathStr,
269 "input", "target");
270 std::ifstream pwmFile(pwmPathStr);
271 if (!pwmFile.good())
272 {
273 return;
274 }
275
276 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
277 if (findPWMSensor != pwmSensors.end())
278 {
279 return;
280 }
281
282 std::string name = "Pwm_";
283 name += psuName;
284 name += "_Fan_";
285 name += labelHeadIndex;
286
287 std::string objPath = interfacePath;
288 objPath += "_Fan_";
289 objPath += labelHeadIndex;
290
291 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
292 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800293}
294
Zhikui Ren23c96e72020-11-05 22:32:28 -0800295static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800296 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800297 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
298 const ManagedObjectType& sensorConfigs,
299 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800300 sensorsChanged,
301 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800302{
Josh Lehan49cfba92019-10-08 16:50:42 -0700303 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800304 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800305
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800306 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
307
Cheng C Yang209ec562019-03-12 16:37:44 +0800308 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800309 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
310 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
311 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800312 {
313 std::cerr << "No PSU sensors in system\n";
314 return;
315 }
316
317 boost::container::flat_set<std::string> directories;
318 for (const auto& pmbusPath : pmbusPaths)
319 {
George Liu5b3542e2023-10-31 17:27:12 +0800320 EventPathList eventPathList;
321 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800322
323 std::ifstream nameFile(pmbusPath);
324 if (!nameFile.good())
325 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700326 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800327 continue;
328 }
329
330 std::string pmbusName;
331 std::getline(nameFile, pmbusName);
332 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700333
Matt Simmering786efb82023-01-18 14:09:21 -0800334 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800335 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700336 // To avoid this error message, add your driver name to
337 // the pmbusNames vector at the top of this file.
338 std::cerr << "Driver name " << pmbusName
339 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800340 continue;
341 }
342
Cheng C Yang209ec562019-03-12 16:37:44 +0800343 auto directory = pmbusPath.parent_path();
344
345 auto ret = directories.insert(directory.string());
346 if (!ret.second)
347 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700348 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800349 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800350 }
351
Joseph Fub844e972023-03-25 10:24:30 +0800352 DevTypes devType = DevTypes::HWMON;
353 std::string deviceName;
354 if (directory.parent_path() == "/sys/class/hwmon")
355 {
356 deviceName = fs::canonical(directory / "device").stem();
357 }
358 else
359 {
360 deviceName = fs::canonical(directory).parent_path().stem();
361 devType = DevTypes::IIO;
362 }
363
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 size_t bus = 0;
365 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000366 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800367 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 continue;
369 }
370
Zev Weisse8b97ee2022-08-12 15:23:51 -0700371 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800372 const SensorData* sensorData = nullptr;
373 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800374 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800375 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800376
Zev Weiss741f26e2022-08-12 18:21:02 -0700377 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800378 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700379 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800380 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800381 {
Zev Weiss054aad82022-08-18 01:37:34 -0700382 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800383 if (sensorBase != sensorData->end())
384 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700385 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800386 sensorType = type;
387 break;
388 }
389 }
390 if (baseConfig == nullptr)
391 {
392 std::cerr << "error finding base configuration for "
393 << deviceName << "\n";
394 continue;
395 }
396
Zev Weisse8b97ee2022-08-12 15:23:51 -0700397 auto configBus = baseConfig->find("Bus");
398 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800399
Zev Weisse8b97ee2022-08-12 15:23:51 -0700400 if (configBus == baseConfig->end() ||
401 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800402 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800403 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800404 continue;
405 }
406
Matt Simmering6747eba2023-01-18 16:03:55 -0800407 const uint64_t* confBus =
408 std::get_if<uint64_t>(&(configBus->second));
409 const uint64_t* confAddr =
410 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800411 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800412 {
413 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700414 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800415 continue;
416 }
417
418 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200420 if constexpr (debug)
421 {
422 std::cerr << "Configuration skipping " << *confBus << "-"
423 << *confAddr << " because not " << bus << "-"
424 << addr << "\n";
425 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800426 continue;
427 }
428
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800429 std::vector<thresholds::Threshold> confThresholds;
430 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
431 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800432 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800433 }
434 thresholdConfSize = confThresholds.size();
435
Zev Weiss741f26e2022-08-12 18:21:02 -0700436 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800437 break;
438 }
439 if (interfacePath == nullptr)
440 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700441 // To avoid this error message, add your export map entry,
442 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800443 std::cerr << "failed to find match for " << deviceName << "\n";
444 continue;
445 }
446
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800447 auto findI2CDev = devices.find(*interfacePath);
448
449 std::shared_ptr<I2CDevice> i2cDev;
450 if (findI2CDev != devices.end())
451 {
452 if (activateOnly && !findI2CDev->second.second)
453 {
454 continue;
455 }
456 i2cDev = findI2CDev->second.first;
457 }
458
Zev Weisse8b97ee2022-08-12 15:23:51 -0700459 auto findPSUName = baseConfig->find("Name");
460 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800461 {
462 std::cerr << "could not determine configuration name for "
463 << deviceName << "\n";
464 continue;
465 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800466 const std::string* psuName =
467 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800468 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800469 {
470 std::cerr << "Cannot find psu name, invalid configuration\n";
471 continue;
472 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800473
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800474 auto findCPU = baseConfig->find("CPURequired");
475 if (findCPU != baseConfig->end())
476 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500477 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800478 auto presenceFind = cpuPresence.find(index);
479 if (presenceFind == cpuPresence.end() || !presenceFind->second)
480 {
481 continue;
482 }
483 }
484
Zhikui Ren23c96e72020-11-05 22:32:28 -0800485 // on rescans, only update sensors we were signaled by
486 if (!firstScan)
487 {
Zhikui Renda98f092021-11-01 09:41:08 -0700488 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500489 auto it = std::find_if(sensorsChanged->begin(),
490 sensorsChanged->end(),
491 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800492 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500493 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800494
495 if (it == sensorsChanged->end())
496 {
497 continue;
498 }
499 sensorsChanged->erase(it);
500 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800501 checkEvent(directory.string(), eventMatch, eventPathList);
cchouxe8a14e92023-10-25 23:26:27 +0800502 checkGroupEvent(directory.string(), groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800503
Zev Weisse8b97ee2022-08-12 15:23:51 -0700504 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000505
Vijay Khemka996bad12019-05-28 15:15:16 -0700506 /* Check if there are more sensors in the same interface */
507 int i = 1;
508 std::vector<std::string> psuNames;
509 do
510 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700511 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700512 psuNames.push_back(
513 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700514 findPSUName = baseConfig->find("Name" + std::to_string(i++));
515 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700516
Cheng C Yange50345b2019-04-02 17:26:15 +0800517 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800518 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
519 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800520 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800521 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800522 continue;
523 }
524
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530525 /* read max value in sysfs for in, curr, power, temp, ... */
526 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
527 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700528 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530529 {
530 std::cerr << "No max name in PSU \n";
531 }
532 }
533
Zev Weiss8569bf22022-10-11 15:37:44 -0700534 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800535
Vijay Khemka996bad12019-05-28 15:15:16 -0700536 /* Find array of labels to be exposed if it is defined in config */
537 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700538 auto findLabelObj = baseConfig->find("Labels");
539 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700540 {
541 findLabels =
542 std::get<std::vector<std::string>>(findLabelObj->second);
543 }
544
Joseph Fub844e972023-03-25 10:24:30 +0800545 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700546 std::smatch matches;
547
Cheng C Yange50345b2019-04-02 17:26:15 +0800548 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800549 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530550 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800551 std::string labelHead;
552 std::string sensorPathStr = sensorPath.string();
553 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700554 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700555 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
556 {
Josh Lehan06494452019-10-31 09:49:16 -0700557 // hwmon *_input filename without number:
558 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800559 // iio in_*_raw filename without number:
560 // voltage, temp, pressure, ...
561 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700562 }
563 else
564 {
Josh Lehan06494452019-10-31 09:49:16 -0700565 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700566 << sensorNameStr;
567 continue;
568 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800569
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530570 std::string labelPath;
571
Joseph Fub844e972023-03-25 10:24:30 +0800572 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530573 {
Joseph Fub844e972023-03-25 10:24:30 +0800574 /* find and differentiate _max and _input to replace "label" */
575 size_t pos = sensorPathStr.find('_');
576 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530577 {
Joseph Fub844e972023-03-25 10:24:30 +0800578 std::string sensorPathStrMax = sensorPathStr.substr(pos);
579 if (sensorPathStrMax == "_max")
580 {
581 labelPath = boost::replace_all_copy(sensorPathStr,
582 "max", "label");
583 maxLabel = true;
584 }
585 else
586 {
587 labelPath = boost::replace_all_copy(sensorPathStr,
588 "input", "label");
589 maxLabel = false;
590 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530591 }
592 else
593 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800594 continue;
595 }
596
Joseph Fub844e972023-03-25 10:24:30 +0800597 std::ifstream labelFile(labelPath);
598 if (!labelFile.good())
599 {
600 if constexpr (debug)
601 {
602 std::cerr << "Input file " << sensorPath
603 << " has no corresponding label file\n";
604 }
605 // hwmon *_input filename with number:
606 // temp1, temp2, temp3, ...
607 labelHead = sensorNameStr.substr(0,
608 sensorNameStr.find('_'));
609 }
610 else
611 {
612 std::string label;
613 std::getline(labelFile, label);
614 labelFile.close();
615 auto findSensor = sensors.find(label);
616 if (findSensor != sensors.end())
617 {
618 continue;
619 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800620
Joseph Fub844e972023-03-25 10:24:30 +0800621 // hwmon corresponding *_label file contents:
622 // vin1, vout1, ...
623 labelHead = label.substr(0, label.find(' '));
624 }
625
626 /* append "max" for labelMatch */
627 if (maxLabel)
628 {
629 labelHead.insert(0, "max");
630 }
631
632 checkPWMSensor(sensorPath, labelHead, *interfacePath,
633 dbusConnection, objectServer, psuNames[0]);
634 }
635 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530636 {
Joseph Fub844e972023-03-25 10:24:30 +0800637 auto findIIOHyphen = sensorNameStr.find_last_of('_');
638 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530639 }
640
Ed Tanous8a57ec02020-10-09 12:46:52 -0700641 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700642 {
643 std::cerr << "Sensor type=\"" << sensorNameSubStr
644 << "\" label=\"" << labelHead << "\"\n";
645 }
646
Vijay Khemka996bad12019-05-28 15:15:16 -0700647 if (!findLabels.empty())
648 {
649 /* Check if this labelHead is enabled in config file */
650 if (std::find(findLabels.begin(), findLabels.end(),
651 labelHead) == findLabels.end())
652 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700653 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800654 {
655 std::cerr << "could not find " << labelHead
656 << " in the Labels list\n";
657 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700658 continue;
659 }
660 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800661
cchouxe8a14e92023-10-25 23:26:27 +0800662 auto findProperty = labelMatch.find(sensorNameSubStr);
Cheng C Yange50345b2019-04-02 17:26:15 +0800663 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800664 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700665 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700666 {
667 std::cerr << "Could not find matching default property for "
cchouxe8a14e92023-10-25 23:26:27 +0800668 << sensorNameSubStr << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700669 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800670 continue;
671 }
672
Josh Lehan74d9bd92019-10-31 08:51:58 -0700673 // Protect the hardcoded labelMatch list from changes,
674 // by making a copy and modifying that instead.
675 // Avoid bleedthrough of one device's customizations to
676 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800677 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700678
679 // Use label head as prefix for reading from config file,
680 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
681 std::string keyName = labelHead + "_Name";
682 std::string keyScale = labelHead + "_Scale";
683 std::string keyMin = labelHead + "_Min";
684 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800685 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800686 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700687
688 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700689 auto findCustomName = baseConfig->find(keyName);
690 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700691 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 try
693 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800694 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700695 VariantToStringVisitor(), findCustomName->second);
696 }
Patrick Williams26601e82021-10-06 12:43:25 -0500697 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700698 {
699 std::cerr << "Unable to parse " << keyName << "\n";
700 continue;
701 }
702
703 // All strings are valid, including empty string
704 customizedName = true;
705 }
706
707 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700708 auto findCustomScale = baseConfig->find(keyScale);
709 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700710 {
711 try
712 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800713 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700714 VariantToUnsignedIntVisitor(), findCustomScale->second);
715 }
Patrick Williams26601e82021-10-06 12:43:25 -0500716 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700717 {
718 std::cerr << "Unable to parse " << keyScale << "\n";
719 continue;
720 }
721
722 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800723 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700724 {
725 customizedScale = true;
726 }
727 else
728 {
729 std::cerr << "Unable to accept " << keyScale << "\n";
730 continue;
731 }
732 }
733
Zev Weisse8b97ee2022-08-12 15:23:51 -0700734 auto findCustomMin = baseConfig->find(keyMin);
735 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700736 {
737 try
738 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800739 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700740 VariantToDoubleVisitor(), findCustomMin->second);
741 }
Patrick Williams26601e82021-10-06 12:43:25 -0500742 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700743 {
744 std::cerr << "Unable to parse " << keyMin << "\n";
745 continue;
746 }
747 }
748
Zev Weisse8b97ee2022-08-12 15:23:51 -0700749 auto findCustomMax = baseConfig->find(keyMax);
750 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700751 {
752 try
753 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800754 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700755 VariantToDoubleVisitor(), findCustomMax->second);
756 }
Patrick Williams26601e82021-10-06 12:43:25 -0500757 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700758 {
759 std::cerr << "Unable to parse " << keyMax << "\n";
760 continue;
761 }
762 }
763
Zev Weisse8b97ee2022-08-12 15:23:51 -0700764 auto findCustomOffset = baseConfig->find(keyOffset);
765 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800766 {
767 try
768 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800769 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800770 VariantToDoubleVisitor(), findCustomOffset->second);
771 }
Patrick Williams26601e82021-10-06 12:43:25 -0500772 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800773 {
774 std::cerr << "Unable to parse " << keyOffset << "\n";
775 continue;
776 }
777 }
778
Lotus Xucb5af732021-09-10 15:18:50 +0800779 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700780 auto findPowerState = baseConfig->find(keyPowerState);
781 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800782 {
783 std::string powerState = std::visit(VariantToStringVisitor(),
784 findPowerState->second);
785 setReadState(powerState, readState);
786 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800787 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700788 {
789 std::cerr << "Min must be less than Max\n";
790 continue;
791 }
792
793 // If the sensor name is being customized by config file,
794 // then prefix/suffix composition becomes not necessary,
795 // and in fact not wanted, because it gets in the way.
796 std::string psuNameFromIndex;
cchouxe8a14e92023-10-25 23:26:27 +0800797 std::string nameIndexStr = "1";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700798 if (!customizedName)
799 {
800 /* Find out sensor name index for this label */
801 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500802 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700803 if (std::regex_search(labelHead, matches, rgx))
804 {
cchouxe8a14e92023-10-25 23:26:27 +0800805 nameIndexStr = matches[1];
806 nameIndex = std::stoi(nameIndexStr);
Josh Lehan74d9bd92019-10-31 08:51:58 -0700807
808 // Decrement to preserve alignment, because hwmon
809 // human-readable filenames and labels use 1-based
810 // numbering, but the "Name", "Name1", "Name2", etc. naming
811 // convention (the psuNames vector) uses 0-based numbering.
812 if (nameIndex > 0)
813 {
814 --nameIndex;
815 }
816 }
817 else
818 {
819 nameIndex = 0;
820 }
821
822 if (psuNames.size() <= nameIndex)
823 {
824 std::cerr << "Could not pair " << labelHead
825 << " with a Name field\n";
826 continue;
827 }
828
829 psuNameFromIndex = psuNames[nameIndex];
830
Ed Tanous8a57ec02020-10-09 12:46:52 -0700831 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700832 {
833 std::cerr << "Sensor label head " << labelHead
834 << " paired with " << psuNameFromIndex
835 << " at index " << nameIndex << "\n";
836 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700837 }
838
Joseph Fub844e972023-03-25 10:24:30 +0800839 if (devType == DevTypes::HWMON)
840 {
841 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
842 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800843
Josh Lehan74d9bd92019-10-31 08:51:58 -0700844 // Similarly, if sensor scaling factor is being customized,
845 // then the below power-of-10 constraint becomes unnecessary,
846 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800847 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700848 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700849 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700850 // Preserve existing usage of hardcoded labelMatch table below
851 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700852
Josh Lehan74d9bd92019-10-31 08:51:58 -0700853 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700854 char firstChar =
855 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700856 std::string strScaleFactor =
857 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
858
859 // Preserve existing configs by accepting earlier syntax,
860 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700861 auto findScaleFactor = baseConfig->find(strScaleFactor);
862 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700863 {
864 factor = std::visit(VariantToIntVisitor(),
865 findScaleFactor->second);
866 }
867
Ed Tanous8a57ec02020-10-09 12:46:52 -0700868 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700869 {
870 std::cerr << "Sensor scaling factor " << factor
871 << " string " << strScaleFactor << "\n";
872 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700873 }
874
Vijay Khemka996bad12019-05-28 15:15:16 -0700875 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530876 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
877 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800878 {
James Feist17ab6e02019-06-25 12:28:13 -0700879 std::cerr << "error populating thresholds for "
880 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800881 }
882
Zev Weiss6b6891c2021-04-22 02:46:21 -0500883 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
884 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800885 {
Jason Ling5747fab2019-10-02 16:46:23 -0700886 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700887 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800888 continue;
889 }
890
Ed Tanous8a57ec02020-10-09 12:46:52 -0700891 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700892 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700893 std::cerr << "Sensor properties: Name \""
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800894 << psuProperty.labelTypeName << "\" Scale "
895 << psuProperty.sensorScaleFactor << " Min "
896 << psuProperty.minReading << " Max "
897 << psuProperty.maxReading << " Offset "
898 << psuProperty.sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700899 }
900
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800901 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700902 if (customizedName)
903 {
904 if (sensorName.empty())
905 {
906 // Allow selective disabling of an individual sensor,
907 // by customizing its name to an empty string.
908 std::cerr << "Sensor disabled, empty string\n";
909 continue;
910 }
911 }
912 else
913 {
914 // Sensor name not customized, do prefix/suffix composition,
915 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800916 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
cchouxe8a14e92023-10-25 23:26:27 +0800917
918 // The labelTypeName of a fan can be:
919 // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
920 if (labelHead == "fan" + nameIndexStr)
921 {
922 sensorName += nameIndexStr;
923 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700924 }
925
Ed Tanous8a57ec02020-10-09 12:46:52 -0700926 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700927 {
928 std::cerr << "Sensor name \"" << sensorName << "\" path \""
929 << sensorPathStr << "\" type \"" << sensorType
930 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700931 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800932 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800933
934 auto& sensor = sensors[sensorName];
935 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700936 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800937 sensor = nullptr;
938 }
939
940 if (sensor != nullptr)
941 {
942 sensor->activate(sensorPathStr, i2cDev);
943 }
944 else
945 {
946 sensors[sensorName] = std::make_shared<PSUSensor>(
947 sensorPathStr, sensorType, objectServer, dbusConnection, io,
948 sensorName, std::move(sensorThresholds), *interfacePath,
949 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800950 psuProperty.maxReading, psuProperty.minReading,
951 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800952 pollRate, i2cDev);
953 sensors[sensorName]->setupRead();
954 ++numCreated;
955 if constexpr (debug)
956 {
957 std::cerr << "Created " << numCreated
958 << " sensors so far\n";
959 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700960 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800961 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800962
Joseph Fub844e972023-03-25 10:24:30 +0800963 if (devType == DevTypes::HWMON)
964 {
965 // OperationalStatus event
966 combineEvents[*psuName + "OperationalStatus"] = nullptr;
967 combineEvents[*psuName + "OperationalStatus"] =
968 std::make_unique<PSUCombineEvent>(
969 objectServer, dbusConnection, io, *psuName, readState,
970 eventPathList, groupEventPathList, "OperationalStatus",
971 pollRate);
972 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800973 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700974
Ed Tanous8a57ec02020-10-09 12:46:52 -0700975 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700976 {
977 std::cerr << "Created total of " << numCreated << " sensors\n";
978 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800979}
Cheng C Yang209ec562019-03-12 16:37:44 +0800980
Matt Simmering39fadb92023-05-04 21:47:29 -0700981static void
982 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
983{
984 static const int depth = 2;
985 static const int numKeys = 1;
986 GetSubTreeType cpuSubTree;
987
988 try
989 {
990 auto getItems = dbusConnection->new_method_call(
991 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
992 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
993 std::array<const char*, numKeys>{
994 "xyz.openbmc_project.Inventory.Item"});
995 auto getItemsResp = dbusConnection->call(getItems);
996 getItemsResp.read(cpuSubTree);
997 }
998 catch (sdbusplus::exception_t& e)
999 {
1000 std::cerr << "error getting inventory item subtree: " << e.what()
1001 << "\n";
1002 return;
1003 }
1004
1005 for (const auto& [path, objDict] : cpuSubTree)
1006 {
1007 auto obj = sdbusplus::message::object_path(path).filename();
Patrick Rudolph457715b2024-04-10 14:51:35 +02001008 boost::to_lower(obj);
1009
Matt Simmering39fadb92023-05-04 21:47:29 -07001010 if (!obj.starts_with("cpu") || objDict.empty())
1011 {
1012 continue;
1013 }
1014 const std::string& owner = objDict.begin()->first;
1015
1016 std::variant<bool> respValue;
1017 try
1018 {
1019 auto getPresence = dbusConnection->new_method_call(
1020 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1021 "Get");
1022 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1023 auto resp = dbusConnection->call(getPresence);
1024 resp.read(respValue);
1025 }
1026 catch (sdbusplus::exception_t& e)
1027 {
1028 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1029 continue;
1030 }
1031
1032 auto* present = std::get_if<bool>(&respValue);
1033 if (present != nullptr && *present)
1034 {
1035 int cpuIndex = 0;
1036 try
1037 {
Patrick Rudolph457715b2024-04-10 14:51:35 +02001038 cpuIndex = std::stoi(obj.substr(obj.size() - 1));
Matt Simmering39fadb92023-05-04 21:47:29 -07001039 }
1040 catch (const std::exception& e)
1041 {
1042 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1043 continue;
1044 }
Patrick Rudolph457715b2024-04-10 14:51:35 +02001045 cpuPresence[cpuIndex] = *present;
Matt Simmering39fadb92023-05-04 21:47:29 -07001046 }
1047 }
1048}
1049
Zhikui Ren23c96e72020-11-05 22:32:28 -08001050void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001051 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001052 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1053 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001054 sensorsChanged,
1055 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001056{
1057 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001058 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1059 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001060 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1061 sensorsChanged, activateOnly);
1062 });
Matt Simmering786efb82023-01-18 14:09:21 -08001063 std::vector<std::string> types(sensorTypes.size());
1064 for (const auto& [type, dt] : sensorTypes)
1065 {
1066 types.push_back(type);
1067 }
1068 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001069}
1070
Ed Tanous201a1012024-04-03 18:07:28 -07001071void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001072{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001073 sensorTable = {{"power", sensor_paths::unitWatts},
1074 {"curr", sensor_paths::unitAmperes},
1075 {"temp", sensor_paths::unitDegreesC},
1076 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001077 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001078 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001079
Jeff Line41d52f2021-04-07 19:38:51 +08001080 labelMatch = {
1081 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001082 {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
1083 {"power", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001084 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1085 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1086 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001087 {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Ian Chienc82a4092024-07-24 10:52:59 +08001088 {"voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001089 {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001090 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001091 {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001092 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
cchouxe8a14e92023-10-25 23:26:27 +08001093 {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
1094 {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
1095 {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1096 {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
1097 {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1098 {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001099
1100 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1101 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1102
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001103 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1104 {"Failure", {"in2_alarm"}},
1105 {"ACLost", {"in1_beep"}},
1106 {"ConfigureError", {"in1_fault"}}};
1107
Joseph Fub844e972023-03-25 10:24:30 +08001108 devParamMap = {
1109 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1110 {DevTypes::IIO,
1111 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001112}
1113
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001114static void powerStateChanged(
1115 PowerState type, bool newState,
1116 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1117 sensors,
1118 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1119 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1120{
1121 if (newState)
1122 {
1123 createSensors(io, objectServer, dbusConnection, nullptr, true);
1124 }
1125 else
1126 {
1127 for (auto& [path, sensor] : sensors)
1128 {
1129 if (sensor != nullptr && sensor->readState == type)
1130 {
1131 sensor->deactivate();
1132 }
1133 }
1134 }
1135}
1136
James Feistb6c0b912019-07-09 12:21:44 -07001137int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001138{
Ed Tanous1f978632023-02-28 18:16:39 -08001139 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001140 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1141
Ed Tanous14ed5e92022-07-12 15:50:23 -07001142 sdbusplus::asio::object_server objectServer(systemBus, true);
1143 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001144 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001145 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001146 auto sensorsChanged =
1147 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001148
Cheng C Yang916360b2019-05-07 18:47:16 +08001149 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001150
Patrick Williams597e8422023-10-20 11:19:01 -05001151 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1152 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001153 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1154 };
1155
1156 setupPowerMatchCallback(systemBus, powerCallBack);
1157
1158 boost::asio::post(io, [&]() {
1159 createSensors(io, objectServer, systemBus, nullptr, false);
1160 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001161 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001162 std::function<void(sdbusplus::message_t&)> eventHandler =
1163 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001164 if (message.is_method_error())
1165 {
1166 std::cerr << "callback method error\n";
1167 return;
1168 }
1169 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001170 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001171 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1172 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001173 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001174 return;
1175 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001176 if (ec)
1177 {
1178 std::cerr << "timer error\n";
1179 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001180 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001181 });
1182 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001183
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001184 boost::asio::steady_timer cpuFilterTimer(io);
1185 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1186 [&](sdbusplus::message_t& message) {
1187 std::string path = message.get_path();
1188 boost::to_lower(path);
1189
1190 sdbusplus::message::object_path cpuPath(path);
1191 std::string cpuName = cpuPath.filename();
1192 if (!cpuName.starts_with("cpu"))
1193 {
1194 return;
1195 }
1196 size_t index = 0;
1197 try
1198 {
1199 index = std::stoi(path.substr(path.size() - 1));
1200 }
1201 catch (const std::invalid_argument&)
1202 {
1203 std::cerr << "Found invalid path " << path << "\n";
1204 return;
1205 }
1206
1207 std::string objectName;
1208 boost::container::flat_map<std::string, std::variant<bool>> values;
1209 message.read(objectName, values);
1210 auto findPresence = values.find("Present");
1211 try
1212 {
1213 cpuPresence[index] = std::get<bool>(findPresence->second);
1214 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001215 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001216 {
1217 return;
1218 }
1219
Chris Sides3fce1bf2023-04-03 16:57:05 -05001220 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001221 {
1222 return;
1223 }
1224 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1225 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1226 if (ec == boost::asio::error::operation_aborted)
1227 {
1228 return;
1229 }
1230 if (ec)
1231 {
1232 std::cerr << "timer error\n";
1233 return;
1234 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001235 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001236 });
1237 };
1238
Zev Weiss214d9712022-08-12 12:54:31 -07001239 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1240 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001241
1242 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1243 static_cast<sdbusplus::bus_t&>(*systemBus),
1244 "type='signal',member='PropertiesChanged',path_namespace='" +
1245 std::string(cpuInventoryPath) +
1246 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1247 cpuPresenceHandler));
1248
Matt Simmering39fadb92023-05-04 21:47:29 -07001249 getPresentCpus(systemBus);
1250
Bruce Lee1263c3d2021-06-04 15:16:33 +08001251 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001252 io.run();
1253}