blob: d4a78951b32a4531657ee4bf56e9fd751f6bf798 [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"
20#include "Utils.hpp"
21
Matt Simmeringe4e6a282023-03-03 14:41:04 -080022#include <boost/algorithm/string/case_conv.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080025#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070031#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070032#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080035#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080036#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080038#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <utility>
40#include <variant>
41#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080042
Ed Tanous8a57ec02020-10-09 12:46:52 -070043static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070044
Matt Simmering786efb82023-01-18 14:09:21 -080045static const I2CDeviceTypeMap sensorTypes{
46 {"ADM1266", I2CDeviceType{"adm1266", true}},
47 {"ADM1272", I2CDeviceType{"adm1272", true}},
48 {"ADM1275", I2CDeviceType{"adm1275", true}},
49 {"ADM1278", I2CDeviceType{"adm1278", true}},
50 {"ADM1293", I2CDeviceType{"adm1293", true}},
51 {"ADS7830", I2CDeviceType{"ads7830", true}},
52 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
53 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020054 {"cffps", I2CDeviceType{"cffps", true}},
55 {"cffps1", I2CDeviceType{"cffps", true}},
56 {"cffps2", I2CDeviceType{"cffps", true}},
57 {"cffps3", I2CDeviceType{"cffps", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080058 {"DPS800", I2CDeviceType{"dps800", true}},
59 {"INA219", I2CDeviceType{"ina219", true}},
60 {"INA230", I2CDeviceType{"ina230", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080061 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080062 {"IR38060", I2CDeviceType{"ir38060", true}},
63 {"IR38164", I2CDeviceType{"ir38164", true}},
64 {"IR38263", I2CDeviceType{"ir38263", true}},
65 {"ISL68137", I2CDeviceType{"isl68137", true}},
66 {"ISL68220", I2CDeviceType{"isl68220", true}},
67 {"ISL68223", I2CDeviceType{"isl68223", true}},
68 {"ISL69225", I2CDeviceType{"isl69225", true}},
69 {"ISL69243", I2CDeviceType{"isl69243", true}},
70 {"ISL69260", I2CDeviceType{"isl69260", true}},
71 {"LM25066", I2CDeviceType{"lm25066", true}},
72 {"MAX16601", I2CDeviceType{"max16601", true}},
73 {"MAX20710", I2CDeviceType{"max20710", true}},
74 {"MAX20730", I2CDeviceType{"max20730", true}},
75 {"MAX20734", I2CDeviceType{"max20734", true}},
76 {"MAX20796", I2CDeviceType{"max20796", true}},
77 {"MAX34451", I2CDeviceType{"max34451", true}},
78 {"MP2971", I2CDeviceType{"mp2971", true}},
79 {"MP2973", I2CDeviceType{"mp2973", true}},
80 {"MP2975", I2CDeviceType{"mp2975", true}},
81 {"MP5023", I2CDeviceType{"mp5023", true}},
82 {"NCP4200", I2CDeviceType{"ncp4200", true}},
83 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
84 {"pmbus", I2CDeviceType{"pmbus", true}},
85 {"PXE1610", I2CDeviceType{"pxe1610", true}},
86 {"RAA228000", I2CDeviceType{"raa228000", true}},
87 {"RAA228228", I2CDeviceType{"raa228228", true}},
88 {"RAA228620", I2CDeviceType{"raa228620", true}},
89 {"RAA229001", I2CDeviceType{"raa229001", true}},
90 {"RAA229004", I2CDeviceType{"raa229004", true}},
91 {"RAA229126", I2CDeviceType{"raa229126", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +020092 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080093 {"TPS53679", I2CDeviceType{"tps53679", true}},
94 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
95 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
96 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +080097 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080098};
Josh Lehan0830c7b2019-10-08 16:35:09 -070099
Cheng C Yang209ec562019-03-12 16:37:44 +0800100namespace fs = std::filesystem;
101
Yong Libf8b1da2020-04-15 16:32:50 +0800102static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800103 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800104static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
105 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800106static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
107 pwmSensors;
108static boost::container::flat_map<std::string, std::string> sensorTable;
109static boost::container::flat_map<std::string, PSUProperty> labelMatch;
110static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800111static boost::container::flat_map<std::string, std::vector<std::string>>
112 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800113static boost::container::flat_map<
114 std::string,
115 boost::container::flat_map<std::string, std::vector<std::string>>>
116 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800117static boost::container::flat_map<std::string, std::vector<std::string>>
118 limitEventMatch;
119
Josh Lehan74d9bd92019-10-31 08:51:58 -0700120static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800121static boost::container::flat_map<size_t, bool> cpuPresence;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700122
Cheng C Yang58b2b532019-05-31 00:19:45 +0800123// Function CheckEvent will check each attribute from eventMatch table in the
124// sysfs. If the attributes exists in sysfs, then store the complete path
125// of the attribute into eventPathList.
126void checkEvent(
127 const std::string& directory,
128 const boost::container::flat_map<std::string, std::vector<std::string>>&
129 eventMatch,
130 boost::container::flat_map<std::string, std::vector<std::string>>&
131 eventPathList)
132{
133 for (const auto& match : eventMatch)
134 {
135 const std::vector<std::string>& eventAttrs = match.second;
136 const std::string& eventName = match.first;
137 for (const auto& eventAttr : eventAttrs)
138 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700139 std::string eventPath = directory;
140 eventPath += "/";
141 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800142
143 std::ifstream eventFile(eventPath);
144 if (!eventFile.good())
145 {
146 continue;
147 }
148
149 eventPathList[eventName].push_back(eventPath);
150 }
151 }
152}
153
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800154// Check Group Events which contains more than one targets in each combine
155// events.
156void checkGroupEvent(
157 const std::string& directory,
158 const boost::container::flat_map<
159 std::string,
160 boost::container::flat_map<std::string, std::vector<std::string>>>&
161 groupEventMatch,
162 boost::container::flat_map<
163 std::string,
164 boost::container::flat_map<std::string, std::vector<std::string>>>&
165 groupEventPathList)
166{
167 for (const auto& match : groupEventMatch)
168 {
169 const std::string& groupEventName = match.first;
170 const boost::container::flat_map<std::string, std::vector<std::string>>
171 events = match.second;
172 boost::container::flat_map<std::string, std::vector<std::string>>
173 pathList;
174 for (const auto& match : events)
175 {
176 const std::string& eventName = match.first;
177 const std::vector<std::string>& eventAttrs = match.second;
178 for (const auto& eventAttr : eventAttrs)
179 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700180 std::string eventPath = directory;
181 eventPath += "/";
182 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800183 std::ifstream eventFile(eventPath);
184 if (!eventFile.good())
185 {
186 continue;
187 }
188
189 pathList[eventName].push_back(eventPath);
190 }
191 }
192 groupEventPathList[groupEventName] = pathList;
193 }
194}
195
Cheng C Yang58b2b532019-05-31 00:19:45 +0800196// Function checkEventLimits will check all the psu related xxx_input attributes
197// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
198// xxx_min_alarm exist, then store the existing paths of the alarm attributes
199// to eventPathList.
200void checkEventLimits(
201 const std::string& sensorPathStr,
202 const boost::container::flat_map<std::string, std::vector<std::string>>&
203 limitEventMatch,
204 boost::container::flat_map<std::string, std::vector<std::string>>&
205 eventPathList)
206{
Lei YUa2c7cea2020-12-23 14:07:28 +0800207 auto attributePartPos = sensorPathStr.find_last_of('_');
208 if (attributePartPos == std::string::npos)
209 {
210 // There is no '_' in the string, skip it
211 return;
212 }
213 auto attributePart =
214 std::string_view(sensorPathStr).substr(attributePartPos + 1);
215 if (attributePart != "input")
216 {
217 // If the sensor is not xxx_input, skip it
218 return;
219 }
220
221 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800222 for (const auto& limitMatch : limitEventMatch)
223 {
224 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
225 const std::string& eventName = limitMatch.first;
226 for (const auto& limitEventAttr : limitEventAttrs)
227 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800228 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800229 std::ifstream eventFile(limitEventPath);
230 if (!eventFile.good())
231 {
232 continue;
233 }
234 eventPathList[eventName].push_back(limitEventPath);
235 }
236 }
237}
Cheng C Yang916360b2019-05-07 18:47:16 +0800238
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530239static void
240 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
241 const std::string& interfacePath,
242 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
243 sdbusplus::asio::object_server& objectServer,
244 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800245{
Zev Weiss741f26e2022-08-12 18:21:02 -0700246 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800247 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700248 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800249 {
250 continue;
251 }
252
253 const std::string& sensorPathStr = sensorPath.string();
254 const std::string& pwmPathStr =
255 boost::replace_all_copy(sensorPathStr, "input", "target");
256 std::ifstream pwmFile(pwmPathStr);
257 if (!pwmFile.good())
258 {
259 continue;
260 }
261
262 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
263 if (findPWMSensor != pwmSensors.end())
264 {
265 continue;
266 }
267
Zev Weissd8c293a2022-08-15 18:58:41 -0700268 std::string name = "Pwm_";
269 name += psuName;
270 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700271 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700272
273 std::string objPath = interfacePath;
274 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700275 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700276
Cheng C Yang916360b2019-05-07 18:47:16 +0800277 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700278 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800279 }
280}
281
Zhikui Ren23c96e72020-11-05 22:32:28 -0800282static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800283 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800284 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
285 const ManagedObjectType& sensorConfigs,
286 const std::shared_ptr<boost::container::flat_set<std::string>>&
287 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800288{
Josh Lehan49cfba92019-10-08 16:50:42 -0700289 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800290 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800291
292 std::vector<fs::path> pmbusPaths;
293 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
294 {
295 std::cerr << "No PSU sensors in system\n";
296 return;
297 }
298
299 boost::container::flat_set<std::string> directories;
300 for (const auto& pmbusPath : pmbusPaths)
301 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800302 boost::container::flat_map<std::string, std::vector<std::string>>
303 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800304 boost::container::flat_map<
305 std::string,
306 boost::container::flat_map<std::string, std::vector<std::string>>>
307 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800308
309 std::ifstream nameFile(pmbusPath);
310 if (!nameFile.good())
311 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700312 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800313 continue;
314 }
315
316 std::string pmbusName;
317 std::getline(nameFile, pmbusName);
318 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700319
Matt Simmering786efb82023-01-18 14:09:21 -0800320 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800321 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700322 // To avoid this error message, add your driver name to
323 // the pmbusNames vector at the top of this file.
324 std::cerr << "Driver name " << pmbusName
325 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800326 continue;
327 }
328
Cheng C Yang209ec562019-03-12 16:37:44 +0800329 auto directory = pmbusPath.parent_path();
330
331 auto ret = directories.insert(directory.string());
332 if (!ret.second)
333 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700334 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800335 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800336 }
337
James Feistb6c0b912019-07-09 12:21:44 -0700338 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800339 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700340 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800341 if (findHyphen == std::string::npos)
342 {
343 std::cerr << "found bad device" << deviceName << "\n";
344 continue;
345 }
346 std::string busStr = deviceName.substr(0, findHyphen);
347 std::string addrStr = deviceName.substr(findHyphen + 1);
348
349 size_t bus = 0;
350 size_t addr = 0;
351
352 try
353 {
354 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700355 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 }
Patrick Williams26601e82021-10-06 12:43:25 -0500357 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700359 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
360 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 continue;
362 }
363
Zev Weisse8b97ee2022-08-12 15:23:51 -0700364 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 const SensorData* sensorData = nullptr;
366 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800367 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800368 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800369
Zev Weiss741f26e2022-08-12 18:21:02 -0700370 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800371 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700372 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800373 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800374 {
Zev Weiss054aad82022-08-18 01:37:34 -0700375 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 if (sensorBase != sensorData->end())
377 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700378 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 sensorType = type;
380 break;
381 }
382 }
383 if (baseConfig == nullptr)
384 {
385 std::cerr << "error finding base configuration for "
386 << deviceName << "\n";
387 continue;
388 }
389
Zev Weisse8b97ee2022-08-12 15:23:51 -0700390 auto configBus = baseConfig->find("Bus");
391 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800392
Zev Weisse8b97ee2022-08-12 15:23:51 -0700393 if (configBus == baseConfig->end() ||
394 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800396 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800397 continue;
398 }
399
Matt Simmering6747eba2023-01-18 16:03:55 -0800400 const uint64_t* confBus =
401 std::get_if<uint64_t>(&(configBus->second));
402 const uint64_t* confAddr =
403 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800404 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800405 {
406 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700407 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800408 continue;
409 }
410
411 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800412 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700413 std::cerr << "Configuration skipping " << *confBus << "-"
414 << *confAddr << " because not " << bus << "-" << addr
415 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 continue;
417 }
418
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800419 std::vector<thresholds::Threshold> confThresholds;
420 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
421 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800422 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800423 }
424 thresholdConfSize = confThresholds.size();
425
Zev Weiss741f26e2022-08-12 18:21:02 -0700426 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800427 break;
428 }
429 if (interfacePath == nullptr)
430 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700431 // To avoid this error message, add your export map entry,
432 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800433 std::cerr << "failed to find match for " << deviceName << "\n";
434 continue;
435 }
436
Zev Weisse8b97ee2022-08-12 15:23:51 -0700437 auto findPSUName = baseConfig->find("Name");
438 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800439 {
440 std::cerr << "could not determine configuration name for "
441 << deviceName << "\n";
442 continue;
443 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800444 const std::string* psuName =
445 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800446 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800447 {
448 std::cerr << "Cannot find psu name, invalid configuration\n";
449 continue;
450 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800451
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800452 auto findCPU = baseConfig->find("CPURequired");
453 if (findCPU != baseConfig->end())
454 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500455 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800456 auto presenceFind = cpuPresence.find(index);
457 if (presenceFind == cpuPresence.end() || !presenceFind->second)
458 {
459 continue;
460 }
461 }
462
Zhikui Ren23c96e72020-11-05 22:32:28 -0800463 // on rescans, only update sensors we were signaled by
464 if (!firstScan)
465 {
Zhikui Renda98f092021-11-01 09:41:08 -0700466 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500467 auto it = std::find_if(sensorsChanged->begin(),
468 sensorsChanged->end(),
469 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800470 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500471 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800472
473 if (it == sensorsChanged->end())
474 {
475 continue;
476 }
477 sensorsChanged->erase(it);
478 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800479 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800480 checkGroupEvent(directory.string(), groupEventMatch,
481 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800482
Zev Weisse8b97ee2022-08-12 15:23:51 -0700483 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000484
Vijay Khemka996bad12019-05-28 15:15:16 -0700485 /* Check if there are more sensors in the same interface */
486 int i = 1;
487 std::vector<std::string> psuNames;
488 do
489 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700490 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700491 psuNames.push_back(
492 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700493 findPSUName = baseConfig->find("Name" + std::to_string(i++));
494 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700495
Cheng C Yange50345b2019-04-02 17:26:15 +0800496 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700497 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800498 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800499 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800500 continue;
501 }
502
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530503 /* read max value in sysfs for in, curr, power, temp, ... */
504 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
505 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700506 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530507 {
508 std::cerr << "No max name in PSU \n";
509 }
510 }
511
Zev Weiss8569bf22022-10-11 15:37:44 -0700512 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800513
Vijay Khemka996bad12019-05-28 15:15:16 -0700514 /* Find array of labels to be exposed if it is defined in config */
515 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700516 auto findLabelObj = baseConfig->find("Labels");
517 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700518 {
519 findLabels =
520 std::get<std::vector<std::string>>(findLabelObj->second);
521 }
522
Jason Ling5747fab2019-10-02 16:46:23 -0700523 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
524 std::smatch matches;
525
Cheng C Yange50345b2019-04-02 17:26:15 +0800526 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800527 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530528 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800529 std::string labelHead;
530 std::string sensorPathStr = sensorPath.string();
531 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700532 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700533 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
534 {
Josh Lehan06494452019-10-31 09:49:16 -0700535 // hwmon *_input filename without number:
536 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700537 sensorNameSubStr = matches[1];
538 }
539 else
540 {
Josh Lehan06494452019-10-31 09:49:16 -0700541 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700542 << sensorNameStr;
543 continue;
544 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800545
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530546 std::string labelPath;
547
548 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700549 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530550 if (pos != std::string::npos)
551 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530552 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700553 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530554 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500555 labelPath = boost::replace_all_copy(sensorPathStr, "max",
556 "label");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530557 maxLabel = true;
558 }
559 else
560 {
561 labelPath = boost::replace_all_copy(sensorPathStr, "input",
562 "label");
563 maxLabel = false;
564 }
565 }
566 else
567 {
568 continue;
569 }
570
Cheng C Yangecba9de2019-09-12 23:41:50 +0800571 std::ifstream labelFile(labelPath);
572 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800573 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700574 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800575 {
576 std::cerr << "Input file " << sensorPath
577 << " has no corresponding label file\n";
578 }
Josh Lehan06494452019-10-31 09:49:16 -0700579 // hwmon *_input filename with number:
580 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700581 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800582 }
583 else
584 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800585 std::string label;
586 std::getline(labelFile, label);
587 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800588 auto findSensor = sensors.find(label);
589 if (findSensor != sensors.end())
590 {
591 continue;
592 }
593
Josh Lehan06494452019-10-31 09:49:16 -0700594 // hwmon corresponding *_label file contents:
595 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700596 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800597 }
598
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530599 /* append "max" for labelMatch */
600 if (maxLabel)
601 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700602 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530603 }
604
Ed Tanous8a57ec02020-10-09 12:46:52 -0700605 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700606 {
607 std::cerr << "Sensor type=\"" << sensorNameSubStr
608 << "\" label=\"" << labelHead << "\"\n";
609 }
610
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530611 checkPWMSensor(sensorPath, labelHead, *interfacePath,
612 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800613
Vijay Khemka996bad12019-05-28 15:15:16 -0700614 if (!findLabels.empty())
615 {
616 /* Check if this labelHead is enabled in config file */
617 if (std::find(findLabels.begin(), findLabels.end(),
618 labelHead) == findLabels.end())
619 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700620 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800621 {
622 std::cerr << "could not find " << labelHead
623 << " in the Labels list\n";
624 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700625 continue;
626 }
627 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800628
Cheng C Yange50345b2019-04-02 17:26:15 +0800629 auto findProperty = labelMatch.find(labelHead);
630 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800631 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700632 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700633 {
634 std::cerr << "Could not find matching default property for "
635 << labelHead << "\n";
636 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800637 continue;
638 }
639
Josh Lehan74d9bd92019-10-31 08:51:58 -0700640 // Protect the hardcoded labelMatch list from changes,
641 // by making a copy and modifying that instead.
642 // Avoid bleedthrough of one device's customizations to
643 // the next device, as each should be independently customizable.
644 psuProperties.push_back(findProperty->second);
645 auto psuProperty = psuProperties.rbegin();
646
647 // Use label head as prefix for reading from config file,
648 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
649 std::string keyName = labelHead + "_Name";
650 std::string keyScale = labelHead + "_Scale";
651 std::string keyMin = labelHead + "_Min";
652 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800653 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800654 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700655
656 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700657 auto findCustomName = baseConfig->find(keyName);
658 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700659 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700660 try
661 {
662 psuProperty->labelTypeName = std::visit(
663 VariantToStringVisitor(), findCustomName->second);
664 }
Patrick Williams26601e82021-10-06 12:43:25 -0500665 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700666 {
667 std::cerr << "Unable to parse " << keyName << "\n";
668 continue;
669 }
670
671 // All strings are valid, including empty string
672 customizedName = true;
673 }
674
675 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700676 auto findCustomScale = baseConfig->find(keyScale);
677 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700678 {
679 try
680 {
681 psuProperty->sensorScaleFactor = std::visit(
682 VariantToUnsignedIntVisitor(), findCustomScale->second);
683 }
Patrick Williams26601e82021-10-06 12:43:25 -0500684 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700685 {
686 std::cerr << "Unable to parse " << keyScale << "\n";
687 continue;
688 }
689
690 // Avoid later division by zero
691 if (psuProperty->sensorScaleFactor > 0)
692 {
693 customizedScale = true;
694 }
695 else
696 {
697 std::cerr << "Unable to accept " << keyScale << "\n";
698 continue;
699 }
700 }
701
Zev Weisse8b97ee2022-08-12 15:23:51 -0700702 auto findCustomMin = baseConfig->find(keyMin);
703 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700704 {
705 try
706 {
707 psuProperty->minReading = std::visit(
708 VariantToDoubleVisitor(), findCustomMin->second);
709 }
Patrick Williams26601e82021-10-06 12:43:25 -0500710 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700711 {
712 std::cerr << "Unable to parse " << keyMin << "\n";
713 continue;
714 }
715 }
716
Zev Weisse8b97ee2022-08-12 15:23:51 -0700717 auto findCustomMax = baseConfig->find(keyMax);
718 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700719 {
720 try
721 {
722 psuProperty->maxReading = std::visit(
723 VariantToDoubleVisitor(), findCustomMax->second);
724 }
Patrick Williams26601e82021-10-06 12:43:25 -0500725 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700726 {
727 std::cerr << "Unable to parse " << keyMax << "\n";
728 continue;
729 }
730 }
731
Zev Weisse8b97ee2022-08-12 15:23:51 -0700732 auto findCustomOffset = baseConfig->find(keyOffset);
733 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800734 {
735 try
736 {
737 psuProperty->sensorOffset = std::visit(
738 VariantToDoubleVisitor(), findCustomOffset->second);
739 }
Patrick Williams26601e82021-10-06 12:43:25 -0500740 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800741 {
742 std::cerr << "Unable to parse " << keyOffset << "\n";
743 continue;
744 }
745 }
746
Lotus Xucb5af732021-09-10 15:18:50 +0800747 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700748 auto findPowerState = baseConfig->find(keyPowerState);
749 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800750 {
751 std::string powerState = std::visit(VariantToStringVisitor(),
752 findPowerState->second);
753 setReadState(powerState, readState);
754 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700755 if (!(psuProperty->minReading < psuProperty->maxReading))
756 {
757 std::cerr << "Min must be less than Max\n";
758 continue;
759 }
760
761 // If the sensor name is being customized by config file,
762 // then prefix/suffix composition becomes not necessary,
763 // and in fact not wanted, because it gets in the way.
764 std::string psuNameFromIndex;
765 if (!customizedName)
766 {
767 /* Find out sensor name index for this label */
768 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500769 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700770 if (std::regex_search(labelHead, matches, rgx))
771 {
772 nameIndex = std::stoi(matches[1]);
773
774 // Decrement to preserve alignment, because hwmon
775 // human-readable filenames and labels use 1-based
776 // numbering, but the "Name", "Name1", "Name2", etc. naming
777 // convention (the psuNames vector) uses 0-based numbering.
778 if (nameIndex > 0)
779 {
780 --nameIndex;
781 }
782 }
783 else
784 {
785 nameIndex = 0;
786 }
787
788 if (psuNames.size() <= nameIndex)
789 {
790 std::cerr << "Could not pair " << labelHead
791 << " with a Name field\n";
792 continue;
793 }
794
795 psuNameFromIndex = psuNames[nameIndex];
796
Ed Tanous8a57ec02020-10-09 12:46:52 -0700797 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700798 {
799 std::cerr << "Sensor label head " << labelHead
800 << " paired with " << psuNameFromIndex
801 << " at index " << nameIndex << "\n";
802 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700803 }
804
Cheng C Yang58b2b532019-05-31 00:19:45 +0800805 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
806
Josh Lehan74d9bd92019-10-31 08:51:58 -0700807 // Similarly, if sensor scaling factor is being customized,
808 // then the below power-of-10 constraint becomes unnecessary,
809 // as config should be able to specify an arbitrary divisor.
810 unsigned int factor = psuProperty->sensorScaleFactor;
811 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700812 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700813 // Preserve existing usage of hardcoded labelMatch table below
814 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700815
Josh Lehan74d9bd92019-10-31 08:51:58 -0700816 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700817 char firstChar =
818 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700819 std::string strScaleFactor =
820 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
821
822 // Preserve existing configs by accepting earlier syntax,
823 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700824 auto findScaleFactor = baseConfig->find(strScaleFactor);
825 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 {
827 factor = std::visit(VariantToIntVisitor(),
828 findScaleFactor->second);
829 }
830
Ed Tanous8a57ec02020-10-09 12:46:52 -0700831 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700832 {
833 std::cerr << "Sensor scaling factor " << factor
834 << " string " << strScaleFactor << "\n";
835 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700836 }
837
Vijay Khemka996bad12019-05-28 15:15:16 -0700838 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530839 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
840 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800841 {
James Feist17ab6e02019-06-25 12:28:13 -0700842 std::cerr << "error populating thresholds for "
843 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800844 }
845
Zev Weiss6b6891c2021-04-22 02:46:21 -0500846 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
847 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800848 {
Jason Ling5747fab2019-10-02 16:46:23 -0700849 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700850 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800851 continue;
852 }
853
Ed Tanous8a57ec02020-10-09 12:46:52 -0700854 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700855 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700856 std::cerr << "Sensor properties: Name \""
857 << psuProperty->labelTypeName << "\" Scale "
858 << psuProperty->sensorScaleFactor << " Min "
859 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800860 << psuProperty->maxReading << " Offset "
861 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700862 }
863
864 std::string sensorName = psuProperty->labelTypeName;
865 if (customizedName)
866 {
867 if (sensorName.empty())
868 {
869 // Allow selective disabling of an individual sensor,
870 // by customizing its name to an empty string.
871 std::cerr << "Sensor disabled, empty string\n";
872 continue;
873 }
874 }
875 else
876 {
877 // Sensor name not customized, do prefix/suffix composition,
878 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500879 sensorName = psuNameFromIndex + " " +
880 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700881 }
882
Ed Tanous8a57ec02020-10-09 12:46:52 -0700883 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700884 {
885 std::cerr << "Sensor name \"" << sensorName << "\" path \""
886 << sensorPathStr << "\" type \"" << sensorType
887 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700888 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800889 // destruct existing one first if already created
890 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800891 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800892 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800893 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000894 readState, findSensorUnit->second, factor,
895 psuProperty->maxReading, psuProperty->minReading,
896 psuProperty->sensorOffset, labelHead, thresholdConfSize,
897 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800898 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700899 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700900 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700901 {
902 std::cerr << "Created " << numCreated << " sensors so far\n";
903 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800904 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800905
906 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800907 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800908 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000909 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
910 *psuName, readState,
911 eventPathList, groupEventPathList,
912 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800913 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700914
Ed Tanous8a57ec02020-10-09 12:46:52 -0700915 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700916 {
917 std::cerr << "Created total of " << numCreated << " sensors\n";
918 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800919}
Cheng C Yang209ec562019-03-12 16:37:44 +0800920
Zhikui Ren23c96e72020-11-05 22:32:28 -0800921void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800922 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800923 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
924 const std::shared_ptr<boost::container::flat_set<std::string>>&
925 sensorsChanged)
926{
927 auto getter = std::make_shared<GetSensorConfiguration>(
928 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
929 const ManagedObjectType& sensorConfigs) {
930 createSensorsCallback(io, objectServer, dbusConnection,
931 sensorConfigs, sensorsChanged);
932 });
Matt Simmering786efb82023-01-18 14:09:21 -0800933 std::vector<std::string> types(sensorTypes.size());
934 for (const auto& [type, dt] : sensorTypes)
935 {
936 types.push_back(type);
937 }
938 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800939}
940
Cheng C Yang916360b2019-05-07 18:47:16 +0800941void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800942{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500943 sensorTable = {{"power", sensor_paths::unitWatts},
944 {"curr", sensor_paths::unitAmperes},
945 {"temp", sensor_paths::unitDegreesC},
946 {"in", sensor_paths::unitVolts},
947 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800948
Jeff Line41d52f2021-04-07 19:38:51 +0800949 labelMatch = {
950 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800951 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
952 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800953 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
954 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
955 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
956 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800957 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
958 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
959 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800960 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
961 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +0200962 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
963 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800964 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
965 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800998 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800999 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001000 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001006 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001007 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1008 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001009 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1013 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1014 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1015 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1016 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1017 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1018 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1019 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1020 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1021 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1022 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1023 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001024 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1025 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1026 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001027 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1028 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1029 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1030 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1031 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1032 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1033 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1034 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1035 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001036 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1037 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1038 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001039
Matt Simmering6747eba2023-01-18 16:03:55 -08001040 pwmTable = {{"fan1", "Fan_1"},
1041 {"fan2", "Fan_2"},
1042 {"fan3", "Fan_3"},
1043 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001044
1045 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1046 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1047
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001048 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1049 {"Failure", {"in2_alarm"}},
1050 {"ACLost", {"in1_beep"}},
1051 {"ConfigureError", {"in1_fault"}}};
1052
1053 groupEventMatch = {{"FanFault",
1054 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001055 {"fan2", {"fan2_alarm", "fan2_fault"}},
1056 {"fan3", {"fan3_alarm", "fan3_fault"}},
1057 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001058}
1059
James Feistb6c0b912019-07-09 12:21:44 -07001060int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001061{
Ed Tanous1f978632023-02-28 18:16:39 -08001062 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001063 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1064
Ed Tanous14ed5e92022-07-12 15:50:23 -07001065 sdbusplus::asio::object_server objectServer(systemBus, true);
1066 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001067 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001068 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001069 auto sensorsChanged =
1070 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001071
Cheng C Yang916360b2019-05-07 18:47:16 +08001072 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001073
Matt Simmering786efb82023-01-18 14:09:21 -08001074 boost::asio::post(
1075 io, [&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001076 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001077 std::function<void(sdbusplus::message_t&)> eventHandler =
1078 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001079 if (message.is_method_error())
1080 {
1081 std::cerr << "callback method error\n";
1082 return;
1083 }
1084 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001085 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001086 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1087 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001088 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001089 return;
1090 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001091 if (ec)
1092 {
1093 std::cerr << "timer error\n";
1094 }
1095 createSensors(io, objectServer, systemBus, sensorsChanged);
1096 });
1097 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001098
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001099 boost::asio::steady_timer cpuFilterTimer(io);
1100 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1101 [&](sdbusplus::message_t& message) {
1102 std::string path = message.get_path();
1103 boost::to_lower(path);
1104
1105 sdbusplus::message::object_path cpuPath(path);
1106 std::string cpuName = cpuPath.filename();
1107 if (!cpuName.starts_with("cpu"))
1108 {
1109 return;
1110 }
1111 size_t index = 0;
1112 try
1113 {
1114 index = std::stoi(path.substr(path.size() - 1));
1115 }
1116 catch (const std::invalid_argument&)
1117 {
1118 std::cerr << "Found invalid path " << path << "\n";
1119 return;
1120 }
1121
1122 std::string objectName;
1123 boost::container::flat_map<std::string, std::variant<bool>> values;
1124 message.read(objectName, values);
1125 auto findPresence = values.find("Present");
1126 try
1127 {
1128 cpuPresence[index] = std::get<bool>(findPresence->second);
1129 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001130 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001131 {
1132 return;
1133 }
1134
Chris Sides3fce1bf2023-04-03 16:57:05 -05001135 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001136 {
1137 return;
1138 }
1139 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1140 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1141 if (ec == boost::asio::error::operation_aborted)
1142 {
1143 return;
1144 }
1145 if (ec)
1146 {
1147 std::cerr << "timer error\n";
1148 return;
1149 }
1150 createSensors(io, objectServer, systemBus, nullptr);
1151 });
1152 };
1153
Zev Weiss214d9712022-08-12 12:54:31 -07001154 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1155 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001156
1157 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1158 static_cast<sdbusplus::bus_t&>(*systemBus),
1159 "type='signal',member='PropertiesChanged',path_namespace='" +
1160 std::string(cpuInventoryPath) +
1161 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1162 cpuPresenceHandler));
1163
Bruce Lee1263c3d2021-06-04 15:16:33 +08001164 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001165 io.run();
1166}