blob: 510dde441396809dced556db28d13647363a4c46 [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>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800287 sensorsChanged,
288 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800289{
Josh Lehan49cfba92019-10-08 16:50:42 -0700290 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800291 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800292
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800293 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
294
Cheng C Yang209ec562019-03-12 16:37:44 +0800295 std::vector<fs::path> pmbusPaths;
296 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
297 {
298 std::cerr << "No PSU sensors in system\n";
299 return;
300 }
301
302 boost::container::flat_set<std::string> directories;
303 for (const auto& pmbusPath : pmbusPaths)
304 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800305 boost::container::flat_map<std::string, std::vector<std::string>>
306 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800307 boost::container::flat_map<
308 std::string,
309 boost::container::flat_map<std::string, std::vector<std::string>>>
310 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800311
312 std::ifstream nameFile(pmbusPath);
313 if (!nameFile.good())
314 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700315 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800316 continue;
317 }
318
319 std::string pmbusName;
320 std::getline(nameFile, pmbusName);
321 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700322
Matt Simmering786efb82023-01-18 14:09:21 -0800323 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800324 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700325 // To avoid this error message, add your driver name to
326 // the pmbusNames vector at the top of this file.
327 std::cerr << "Driver name " << pmbusName
328 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800329 continue;
330 }
331
Cheng C Yang209ec562019-03-12 16:37:44 +0800332 auto directory = pmbusPath.parent_path();
333
334 auto ret = directories.insert(directory.string());
335 if (!ret.second)
336 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700337 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800338 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800339 }
340
James Feistb6c0b912019-07-09 12:21:44 -0700341 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800342 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700343 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800344 if (findHyphen == std::string::npos)
345 {
346 std::cerr << "found bad device" << deviceName << "\n";
347 continue;
348 }
349 std::string busStr = deviceName.substr(0, findHyphen);
350 std::string addrStr = deviceName.substr(findHyphen + 1);
351
352 size_t bus = 0;
353 size_t addr = 0;
354
355 try
356 {
357 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700358 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 }
Patrick Williams26601e82021-10-06 12:43:25 -0500360 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700362 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
363 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 continue;
365 }
366
Zev Weisse8b97ee2022-08-12 15:23:51 -0700367 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 const SensorData* sensorData = nullptr;
369 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800370 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800371 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800372
Zev Weiss741f26e2022-08-12 18:21:02 -0700373 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800374 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700375 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800376 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800377 {
Zev Weiss054aad82022-08-18 01:37:34 -0700378 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 if (sensorBase != sensorData->end())
380 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700381 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800382 sensorType = type;
383 break;
384 }
385 }
386 if (baseConfig == nullptr)
387 {
388 std::cerr << "error finding base configuration for "
389 << deviceName << "\n";
390 continue;
391 }
392
Zev Weisse8b97ee2022-08-12 15:23:51 -0700393 auto configBus = baseConfig->find("Bus");
394 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800395
Zev Weisse8b97ee2022-08-12 15:23:51 -0700396 if (configBus == baseConfig->end() ||
397 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800399 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800400 continue;
401 }
402
Matt Simmering6747eba2023-01-18 16:03:55 -0800403 const uint64_t* confBus =
404 std::get_if<uint64_t>(&(configBus->second));
405 const uint64_t* confAddr =
406 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800407 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800408 {
409 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700410 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800411 continue;
412 }
413
414 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700416 std::cerr << "Configuration skipping " << *confBus << "-"
417 << *confAddr << " because not " << bus << "-" << addr
418 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 continue;
420 }
421
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800422 std::vector<thresholds::Threshold> confThresholds;
423 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
424 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800425 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800426 }
427 thresholdConfSize = confThresholds.size();
428
Zev Weiss741f26e2022-08-12 18:21:02 -0700429 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800430 break;
431 }
432 if (interfacePath == nullptr)
433 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700434 // To avoid this error message, add your export map entry,
435 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800436 std::cerr << "failed to find match for " << deviceName << "\n";
437 continue;
438 }
439
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800440 auto findI2CDev = devices.find(*interfacePath);
441
442 std::shared_ptr<I2CDevice> i2cDev;
443 if (findI2CDev != devices.end())
444 {
445 if (activateOnly && !findI2CDev->second.second)
446 {
447 continue;
448 }
449 i2cDev = findI2CDev->second.first;
450 }
451
Zev Weisse8b97ee2022-08-12 15:23:51 -0700452 auto findPSUName = baseConfig->find("Name");
453 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800454 {
455 std::cerr << "could not determine configuration name for "
456 << deviceName << "\n";
457 continue;
458 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800459 const std::string* psuName =
460 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800461 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800462 {
463 std::cerr << "Cannot find psu name, invalid configuration\n";
464 continue;
465 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800466
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800467 auto findCPU = baseConfig->find("CPURequired");
468 if (findCPU != baseConfig->end())
469 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500470 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800471 auto presenceFind = cpuPresence.find(index);
472 if (presenceFind == cpuPresence.end() || !presenceFind->second)
473 {
474 continue;
475 }
476 }
477
Zhikui Ren23c96e72020-11-05 22:32:28 -0800478 // on rescans, only update sensors we were signaled by
479 if (!firstScan)
480 {
Zhikui Renda98f092021-11-01 09:41:08 -0700481 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500482 auto it = std::find_if(sensorsChanged->begin(),
483 sensorsChanged->end(),
484 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800485 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500486 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800487
488 if (it == sensorsChanged->end())
489 {
490 continue;
491 }
492 sensorsChanged->erase(it);
493 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800494 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800495 checkGroupEvent(directory.string(), groupEventMatch,
496 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800497
Zev Weisse8b97ee2022-08-12 15:23:51 -0700498 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000499
Vijay Khemka996bad12019-05-28 15:15:16 -0700500 /* Check if there are more sensors in the same interface */
501 int i = 1;
502 std::vector<std::string> psuNames;
503 do
504 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700505 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700506 psuNames.push_back(
507 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700508 findPSUName = baseConfig->find("Name" + std::to_string(i++));
509 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700510
Cheng C Yange50345b2019-04-02 17:26:15 +0800511 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700512 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800513 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800514 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800515 continue;
516 }
517
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530518 /* read max value in sysfs for in, curr, power, temp, ... */
519 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
520 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700521 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530522 {
523 std::cerr << "No max name in PSU \n";
524 }
525 }
526
Zev Weiss8569bf22022-10-11 15:37:44 -0700527 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800528
Vijay Khemka996bad12019-05-28 15:15:16 -0700529 /* Find array of labels to be exposed if it is defined in config */
530 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700531 auto findLabelObj = baseConfig->find("Labels");
532 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700533 {
534 findLabels =
535 std::get<std::vector<std::string>>(findLabelObj->second);
536 }
537
Jason Ling5747fab2019-10-02 16:46:23 -0700538 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
539 std::smatch matches;
540
Cheng C Yange50345b2019-04-02 17:26:15 +0800541 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800542 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530543 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800544 std::string labelHead;
545 std::string sensorPathStr = sensorPath.string();
546 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700547 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700548 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
549 {
Josh Lehan06494452019-10-31 09:49:16 -0700550 // hwmon *_input filename without number:
551 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700552 sensorNameSubStr = matches[1];
553 }
554 else
555 {
Josh Lehan06494452019-10-31 09:49:16 -0700556 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700557 << sensorNameStr;
558 continue;
559 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800560
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530561 std::string labelPath;
562
563 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700564 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530565 if (pos != std::string::npos)
566 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530567 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700568 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530569 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500570 labelPath = boost::replace_all_copy(sensorPathStr, "max",
571 "label");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530572 maxLabel = true;
573 }
574 else
575 {
576 labelPath = boost::replace_all_copy(sensorPathStr, "input",
577 "label");
578 maxLabel = false;
579 }
580 }
581 else
582 {
583 continue;
584 }
585
Cheng C Yangecba9de2019-09-12 23:41:50 +0800586 std::ifstream labelFile(labelPath);
587 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800588 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700589 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800590 {
591 std::cerr << "Input file " << sensorPath
592 << " has no corresponding label file\n";
593 }
Josh Lehan06494452019-10-31 09:49:16 -0700594 // hwmon *_input filename with number:
595 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700596 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800597 }
598 else
599 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800600 std::string label;
601 std::getline(labelFile, label);
602 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800603 auto findSensor = sensors.find(label);
604 if (findSensor != sensors.end())
605 {
606 continue;
607 }
608
Josh Lehan06494452019-10-31 09:49:16 -0700609 // hwmon corresponding *_label file contents:
610 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700611 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800612 }
613
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530614 /* append "max" for labelMatch */
615 if (maxLabel)
616 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700617 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530618 }
619
Ed Tanous8a57ec02020-10-09 12:46:52 -0700620 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700621 {
622 std::cerr << "Sensor type=\"" << sensorNameSubStr
623 << "\" label=\"" << labelHead << "\"\n";
624 }
625
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530626 checkPWMSensor(sensorPath, labelHead, *interfacePath,
627 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800628
Vijay Khemka996bad12019-05-28 15:15:16 -0700629 if (!findLabels.empty())
630 {
631 /* Check if this labelHead is enabled in config file */
632 if (std::find(findLabels.begin(), findLabels.end(),
633 labelHead) == findLabels.end())
634 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700635 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800636 {
637 std::cerr << "could not find " << labelHead
638 << " in the Labels list\n";
639 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700640 continue;
641 }
642 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800643
Cheng C Yange50345b2019-04-02 17:26:15 +0800644 auto findProperty = labelMatch.find(labelHead);
645 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800646 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700647 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700648 {
649 std::cerr << "Could not find matching default property for "
650 << labelHead << "\n";
651 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800652 continue;
653 }
654
Josh Lehan74d9bd92019-10-31 08:51:58 -0700655 // Protect the hardcoded labelMatch list from changes,
656 // by making a copy and modifying that instead.
657 // Avoid bleedthrough of one device's customizations to
658 // the next device, as each should be independently customizable.
659 psuProperties.push_back(findProperty->second);
660 auto psuProperty = psuProperties.rbegin();
661
662 // Use label head as prefix for reading from config file,
663 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
664 std::string keyName = labelHead + "_Name";
665 std::string keyScale = labelHead + "_Scale";
666 std::string keyMin = labelHead + "_Min";
667 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800668 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800669 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700670
671 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700672 auto findCustomName = baseConfig->find(keyName);
673 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700674 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700675 try
676 {
677 psuProperty->labelTypeName = std::visit(
678 VariantToStringVisitor(), findCustomName->second);
679 }
Patrick Williams26601e82021-10-06 12:43:25 -0500680 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700681 {
682 std::cerr << "Unable to parse " << keyName << "\n";
683 continue;
684 }
685
686 // All strings are valid, including empty string
687 customizedName = true;
688 }
689
690 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700691 auto findCustomScale = baseConfig->find(keyScale);
692 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700693 {
694 try
695 {
696 psuProperty->sensorScaleFactor = std::visit(
697 VariantToUnsignedIntVisitor(), findCustomScale->second);
698 }
Patrick Williams26601e82021-10-06 12:43:25 -0500699 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700 {
701 std::cerr << "Unable to parse " << keyScale << "\n";
702 continue;
703 }
704
705 // Avoid later division by zero
706 if (psuProperty->sensorScaleFactor > 0)
707 {
708 customizedScale = true;
709 }
710 else
711 {
712 std::cerr << "Unable to accept " << keyScale << "\n";
713 continue;
714 }
715 }
716
Zev Weisse8b97ee2022-08-12 15:23:51 -0700717 auto findCustomMin = baseConfig->find(keyMin);
718 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700719 {
720 try
721 {
722 psuProperty->minReading = std::visit(
723 VariantToDoubleVisitor(), findCustomMin->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 " << keyMin << "\n";
728 continue;
729 }
730 }
731
Zev Weisse8b97ee2022-08-12 15:23:51 -0700732 auto findCustomMax = baseConfig->find(keyMax);
733 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700734 {
735 try
736 {
737 psuProperty->maxReading = std::visit(
738 VariantToDoubleVisitor(), findCustomMax->second);
739 }
Patrick Williams26601e82021-10-06 12:43:25 -0500740 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700741 {
742 std::cerr << "Unable to parse " << keyMax << "\n";
743 continue;
744 }
745 }
746
Zev Weisse8b97ee2022-08-12 15:23:51 -0700747 auto findCustomOffset = baseConfig->find(keyOffset);
748 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800749 {
750 try
751 {
752 psuProperty->sensorOffset = std::visit(
753 VariantToDoubleVisitor(), findCustomOffset->second);
754 }
Patrick Williams26601e82021-10-06 12:43:25 -0500755 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800756 {
757 std::cerr << "Unable to parse " << keyOffset << "\n";
758 continue;
759 }
760 }
761
Lotus Xucb5af732021-09-10 15:18:50 +0800762 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700763 auto findPowerState = baseConfig->find(keyPowerState);
764 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800765 {
766 std::string powerState = std::visit(VariantToStringVisitor(),
767 findPowerState->second);
768 setReadState(powerState, readState);
769 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700770 if (!(psuProperty->minReading < psuProperty->maxReading))
771 {
772 std::cerr << "Min must be less than Max\n";
773 continue;
774 }
775
776 // If the sensor name is being customized by config file,
777 // then prefix/suffix composition becomes not necessary,
778 // and in fact not wanted, because it gets in the way.
779 std::string psuNameFromIndex;
780 if (!customizedName)
781 {
782 /* Find out sensor name index for this label */
783 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500784 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700785 if (std::regex_search(labelHead, matches, rgx))
786 {
787 nameIndex = std::stoi(matches[1]);
788
789 // Decrement to preserve alignment, because hwmon
790 // human-readable filenames and labels use 1-based
791 // numbering, but the "Name", "Name1", "Name2", etc. naming
792 // convention (the psuNames vector) uses 0-based numbering.
793 if (nameIndex > 0)
794 {
795 --nameIndex;
796 }
797 }
798 else
799 {
800 nameIndex = 0;
801 }
802
803 if (psuNames.size() <= nameIndex)
804 {
805 std::cerr << "Could not pair " << labelHead
806 << " with a Name field\n";
807 continue;
808 }
809
810 psuNameFromIndex = psuNames[nameIndex];
811
Ed Tanous8a57ec02020-10-09 12:46:52 -0700812 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700813 {
814 std::cerr << "Sensor label head " << labelHead
815 << " paired with " << psuNameFromIndex
816 << " at index " << nameIndex << "\n";
817 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700818 }
819
Cheng C Yang58b2b532019-05-31 00:19:45 +0800820 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
821
Josh Lehan74d9bd92019-10-31 08:51:58 -0700822 // Similarly, if sensor scaling factor is being customized,
823 // then the below power-of-10 constraint becomes unnecessary,
824 // as config should be able to specify an arbitrary divisor.
825 unsigned int factor = psuProperty->sensorScaleFactor;
826 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700827 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700828 // Preserve existing usage of hardcoded labelMatch table below
829 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700830
Josh Lehan74d9bd92019-10-31 08:51:58 -0700831 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700832 char firstChar =
833 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700834 std::string strScaleFactor =
835 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
836
837 // Preserve existing configs by accepting earlier syntax,
838 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700839 auto findScaleFactor = baseConfig->find(strScaleFactor);
840 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700841 {
842 factor = std::visit(VariantToIntVisitor(),
843 findScaleFactor->second);
844 }
845
Ed Tanous8a57ec02020-10-09 12:46:52 -0700846 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700847 {
848 std::cerr << "Sensor scaling factor " << factor
849 << " string " << strScaleFactor << "\n";
850 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700851 }
852
Vijay Khemka996bad12019-05-28 15:15:16 -0700853 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530854 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
855 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800856 {
James Feist17ab6e02019-06-25 12:28:13 -0700857 std::cerr << "error populating thresholds for "
858 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800859 }
860
Zev Weiss6b6891c2021-04-22 02:46:21 -0500861 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
862 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800863 {
Jason Ling5747fab2019-10-02 16:46:23 -0700864 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700865 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800866 continue;
867 }
868
Ed Tanous8a57ec02020-10-09 12:46:52 -0700869 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700870 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700871 std::cerr << "Sensor properties: Name \""
872 << psuProperty->labelTypeName << "\" Scale "
873 << psuProperty->sensorScaleFactor << " Min "
874 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800875 << psuProperty->maxReading << " Offset "
876 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700877 }
878
879 std::string sensorName = psuProperty->labelTypeName;
880 if (customizedName)
881 {
882 if (sensorName.empty())
883 {
884 // Allow selective disabling of an individual sensor,
885 // by customizing its name to an empty string.
886 std::cerr << "Sensor disabled, empty string\n";
887 continue;
888 }
889 }
890 else
891 {
892 // Sensor name not customized, do prefix/suffix composition,
893 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500894 sensorName = psuNameFromIndex + " " +
895 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700896 }
897
Ed Tanous8a57ec02020-10-09 12:46:52 -0700898 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700899 {
900 std::cerr << "Sensor name \"" << sensorName << "\" path \""
901 << sensorPathStr << "\" type \"" << sensorType
902 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700903 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800904 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800905
906 auto& sensor = sensors[sensorName];
907 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700908 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800909 sensor = nullptr;
910 }
911
912 if (sensor != nullptr)
913 {
914 sensor->activate(sensorPathStr, i2cDev);
915 }
916 else
917 {
918 sensors[sensorName] = std::make_shared<PSUSensor>(
919 sensorPathStr, sensorType, objectServer, dbusConnection, io,
920 sensorName, std::move(sensorThresholds), *interfacePath,
921 readState, findSensorUnit->second, factor,
922 psuProperty->maxReading, psuProperty->minReading,
923 psuProperty->sensorOffset, labelHead, thresholdConfSize,
924 pollRate, i2cDev);
925 sensors[sensorName]->setupRead();
926 ++numCreated;
927 if constexpr (debug)
928 {
929 std::cerr << "Created " << numCreated
930 << " sensors so far\n";
931 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700932 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800933 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800934
935 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800936 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800937 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000938 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
939 *psuName, readState,
940 eventPathList, groupEventPathList,
941 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800942 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700943
Ed Tanous8a57ec02020-10-09 12:46:52 -0700944 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700945 {
946 std::cerr << "Created total of " << numCreated << " sensors\n";
947 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800948}
Cheng C Yang209ec562019-03-12 16:37:44 +0800949
Zhikui Ren23c96e72020-11-05 22:32:28 -0800950void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800951 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800952 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
953 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800954 sensorsChanged,
955 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -0800956{
957 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800958 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
959 activateOnly](const ManagedObjectType& sensorConfigs) {
Zhikui Ren23c96e72020-11-05 22:32:28 -0800960 createSensorsCallback(io, objectServer, dbusConnection,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800961 sensorConfigs, sensorsChanged, activateOnly);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800962 });
Matt Simmering786efb82023-01-18 14:09:21 -0800963 std::vector<std::string> types(sensorTypes.size());
964 for (const auto& [type, dt] : sensorTypes)
965 {
966 types.push_back(type);
967 }
968 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800969}
970
Cheng C Yang916360b2019-05-07 18:47:16 +0800971void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800972{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500973 sensorTable = {{"power", sensor_paths::unitWatts},
974 {"curr", sensor_paths::unitAmperes},
975 {"temp", sensor_paths::unitDegreesC},
976 {"in", sensor_paths::unitVolts},
977 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800978
Jeff Line41d52f2021-04-07 19:38:51 +0800979 labelMatch = {
980 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800981 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
982 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800983 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
984 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
985 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
986 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800987 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
988 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
989 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800990 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
991 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +0200992 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
993 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800994 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
995 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1009 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1010 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1011 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1012 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1014 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1015 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1016 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1017 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1018 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1019 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1020 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1021 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1022 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1023 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1024 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1025 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1026 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1027 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001028 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001029 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001030 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1031 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1032 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1033 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1034 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1035 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001036 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001037 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1038 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001039 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1040 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1041 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1042 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1043 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1044 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1045 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1046 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1047 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1048 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1049 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1050 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1051 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1052 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1053 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001054 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1055 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1056 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001057 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1058 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1059 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1060 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1061 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1062 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1063 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1064 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1065 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001066 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1067 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1068 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001069
Matt Simmering6747eba2023-01-18 16:03:55 -08001070 pwmTable = {{"fan1", "Fan_1"},
1071 {"fan2", "Fan_2"},
1072 {"fan3", "Fan_3"},
1073 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001074
1075 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1076 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1077
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001078 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1079 {"Failure", {"in2_alarm"}},
1080 {"ACLost", {"in1_beep"}},
1081 {"ConfigureError", {"in1_fault"}}};
1082
1083 groupEventMatch = {{"FanFault",
1084 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001085 {"fan2", {"fan2_alarm", "fan2_fault"}},
1086 {"fan3", {"fan3_alarm", "fan3_fault"}},
1087 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001088}
1089
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001090static void powerStateChanged(
1091 PowerState type, bool newState,
1092 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1093 sensors,
1094 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1095 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1096{
1097 if (newState)
1098 {
1099 createSensors(io, objectServer, dbusConnection, nullptr, true);
1100 }
1101 else
1102 {
1103 for (auto& [path, sensor] : sensors)
1104 {
1105 if (sensor != nullptr && sensor->readState == type)
1106 {
1107 sensor->deactivate();
1108 }
1109 }
1110 }
1111}
1112
James Feistb6c0b912019-07-09 12:21:44 -07001113int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001114{
Ed Tanous1f978632023-02-28 18:16:39 -08001115 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001116 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1117
Ed Tanous14ed5e92022-07-12 15:50:23 -07001118 sdbusplus::asio::object_server objectServer(systemBus, true);
1119 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001120 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001121 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001122 auto sensorsChanged =
1123 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001124
Cheng C Yang916360b2019-05-07 18:47:16 +08001125 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001126
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001127 auto powerCallBack =
1128 [&io, &objectServer, &systemBus](PowerState type, bool state) {
1129 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1130 };
1131
1132 setupPowerMatchCallback(systemBus, powerCallBack);
1133
1134 boost::asio::post(io, [&]() {
1135 createSensors(io, objectServer, systemBus, nullptr, false);
1136 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001137 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001138 std::function<void(sdbusplus::message_t&)> eventHandler =
1139 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001140 if (message.is_method_error())
1141 {
1142 std::cerr << "callback method error\n";
1143 return;
1144 }
1145 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001146 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001147 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1148 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001149 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001150 return;
1151 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001152 if (ec)
1153 {
1154 std::cerr << "timer error\n";
1155 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001156 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001157 });
1158 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001159
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001160 boost::asio::steady_timer cpuFilterTimer(io);
1161 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1162 [&](sdbusplus::message_t& message) {
1163 std::string path = message.get_path();
1164 boost::to_lower(path);
1165
1166 sdbusplus::message::object_path cpuPath(path);
1167 std::string cpuName = cpuPath.filename();
1168 if (!cpuName.starts_with("cpu"))
1169 {
1170 return;
1171 }
1172 size_t index = 0;
1173 try
1174 {
1175 index = std::stoi(path.substr(path.size() - 1));
1176 }
1177 catch (const std::invalid_argument&)
1178 {
1179 std::cerr << "Found invalid path " << path << "\n";
1180 return;
1181 }
1182
1183 std::string objectName;
1184 boost::container::flat_map<std::string, std::variant<bool>> values;
1185 message.read(objectName, values);
1186 auto findPresence = values.find("Present");
1187 try
1188 {
1189 cpuPresence[index] = std::get<bool>(findPresence->second);
1190 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001191 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001192 {
1193 return;
1194 }
1195
Chris Sides3fce1bf2023-04-03 16:57:05 -05001196 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001197 {
1198 return;
1199 }
1200 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1201 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1202 if (ec == boost::asio::error::operation_aborted)
1203 {
1204 return;
1205 }
1206 if (ec)
1207 {
1208 std::cerr << "timer error\n";
1209 return;
1210 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001211 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001212 });
1213 };
1214
Zev Weiss214d9712022-08-12 12:54:31 -07001215 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1216 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001217
1218 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1219 static_cast<sdbusplus::bus_t&>(*systemBus),
1220 "type='signal',member='PropertiesChanged',path_namespace='" +
1221 std::string(cpuInventoryPath) +
1222 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1223 cpuPresenceHandler));
1224
Bruce Lee1263c3d2021-06-04 15:16:33 +08001225 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001226 io.run();
1227}