blob: be45dce03e6eb8f28e1431b41d45891e3bfaa2ce [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}},
54 {"DPS800", I2CDeviceType{"dps800", true}},
55 {"INA219", I2CDeviceType{"ina219", true}},
56 {"INA230", I2CDeviceType{"ina230", true}},
57 {"IPSPS", I2CDeviceType{"ipsps", true}},
58 {"IR38060", I2CDeviceType{"ir38060", true}},
59 {"IR38164", I2CDeviceType{"ir38164", true}},
60 {"IR38263", I2CDeviceType{"ir38263", true}},
61 {"ISL68137", I2CDeviceType{"isl68137", true}},
62 {"ISL68220", I2CDeviceType{"isl68220", true}},
63 {"ISL68223", I2CDeviceType{"isl68223", true}},
64 {"ISL69225", I2CDeviceType{"isl69225", true}},
65 {"ISL69243", I2CDeviceType{"isl69243", true}},
66 {"ISL69260", I2CDeviceType{"isl69260", true}},
67 {"LM25066", I2CDeviceType{"lm25066", true}},
68 {"MAX16601", I2CDeviceType{"max16601", true}},
69 {"MAX20710", I2CDeviceType{"max20710", true}},
70 {"MAX20730", I2CDeviceType{"max20730", true}},
71 {"MAX20734", I2CDeviceType{"max20734", true}},
72 {"MAX20796", I2CDeviceType{"max20796", true}},
73 {"MAX34451", I2CDeviceType{"max34451", true}},
74 {"MP2971", I2CDeviceType{"mp2971", true}},
75 {"MP2973", I2CDeviceType{"mp2973", true}},
76 {"MP2975", I2CDeviceType{"mp2975", true}},
77 {"MP5023", I2CDeviceType{"mp5023", true}},
78 {"NCP4200", I2CDeviceType{"ncp4200", true}},
79 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
80 {"pmbus", I2CDeviceType{"pmbus", true}},
81 {"PXE1610", I2CDeviceType{"pxe1610", true}},
82 {"RAA228000", I2CDeviceType{"raa228000", true}},
83 {"RAA228228", I2CDeviceType{"raa228228", true}},
84 {"RAA228620", I2CDeviceType{"raa228620", true}},
85 {"RAA229001", I2CDeviceType{"raa229001", true}},
86 {"RAA229004", I2CDeviceType{"raa229004", true}},
87 {"RAA229126", I2CDeviceType{"raa229126", true}},
88 {"TPS53679", I2CDeviceType{"tps53679", true}},
89 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
90 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
91 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
92};
Josh Lehan0830c7b2019-10-08 16:35:09 -070093
Cheng C Yang209ec562019-03-12 16:37:44 +080094namespace fs = std::filesystem;
95
Yong Libf8b1da2020-04-15 16:32:50 +080096static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080097 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080098static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
99 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800100static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
101 pwmSensors;
102static boost::container::flat_map<std::string, std::string> sensorTable;
103static boost::container::flat_map<std::string, PSUProperty> labelMatch;
104static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800105static boost::container::flat_map<std::string, std::vector<std::string>>
106 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800107static boost::container::flat_map<
108 std::string,
109 boost::container::flat_map<std::string, std::vector<std::string>>>
110 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800111static boost::container::flat_map<std::string, std::vector<std::string>>
112 limitEventMatch;
113
Josh Lehan74d9bd92019-10-31 08:51:58 -0700114static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800115static boost::container::flat_map<size_t, bool> cpuPresence;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700116
Cheng C Yang58b2b532019-05-31 00:19:45 +0800117// Function CheckEvent will check each attribute from eventMatch table in the
118// sysfs. If the attributes exists in sysfs, then store the complete path
119// of the attribute into eventPathList.
120void checkEvent(
121 const std::string& directory,
122 const boost::container::flat_map<std::string, std::vector<std::string>>&
123 eventMatch,
124 boost::container::flat_map<std::string, std::vector<std::string>>&
125 eventPathList)
126{
127 for (const auto& match : eventMatch)
128 {
129 const std::vector<std::string>& eventAttrs = match.second;
130 const std::string& eventName = match.first;
131 for (const auto& eventAttr : eventAttrs)
132 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700133 std::string eventPath = directory;
134 eventPath += "/";
135 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800136
137 std::ifstream eventFile(eventPath);
138 if (!eventFile.good())
139 {
140 continue;
141 }
142
143 eventPathList[eventName].push_back(eventPath);
144 }
145 }
146}
147
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800148// Check Group Events which contains more than one targets in each combine
149// events.
150void checkGroupEvent(
151 const std::string& directory,
152 const boost::container::flat_map<
153 std::string,
154 boost::container::flat_map<std::string, std::vector<std::string>>>&
155 groupEventMatch,
156 boost::container::flat_map<
157 std::string,
158 boost::container::flat_map<std::string, std::vector<std::string>>>&
159 groupEventPathList)
160{
161 for (const auto& match : groupEventMatch)
162 {
163 const std::string& groupEventName = match.first;
164 const boost::container::flat_map<std::string, std::vector<std::string>>
165 events = match.second;
166 boost::container::flat_map<std::string, std::vector<std::string>>
167 pathList;
168 for (const auto& match : events)
169 {
170 const std::string& eventName = match.first;
171 const std::vector<std::string>& eventAttrs = match.second;
172 for (const auto& eventAttr : eventAttrs)
173 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700174 std::string eventPath = directory;
175 eventPath += "/";
176 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800177 std::ifstream eventFile(eventPath);
178 if (!eventFile.good())
179 {
180 continue;
181 }
182
183 pathList[eventName].push_back(eventPath);
184 }
185 }
186 groupEventPathList[groupEventName] = pathList;
187 }
188}
189
Cheng C Yang58b2b532019-05-31 00:19:45 +0800190// Function checkEventLimits will check all the psu related xxx_input attributes
191// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
192// xxx_min_alarm exist, then store the existing paths of the alarm attributes
193// to eventPathList.
194void checkEventLimits(
195 const std::string& sensorPathStr,
196 const boost::container::flat_map<std::string, std::vector<std::string>>&
197 limitEventMatch,
198 boost::container::flat_map<std::string, std::vector<std::string>>&
199 eventPathList)
200{
Lei YUa2c7cea2020-12-23 14:07:28 +0800201 auto attributePartPos = sensorPathStr.find_last_of('_');
202 if (attributePartPos == std::string::npos)
203 {
204 // There is no '_' in the string, skip it
205 return;
206 }
207 auto attributePart =
208 std::string_view(sensorPathStr).substr(attributePartPos + 1);
209 if (attributePart != "input")
210 {
211 // If the sensor is not xxx_input, skip it
212 return;
213 }
214
215 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800216 for (const auto& limitMatch : limitEventMatch)
217 {
218 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
219 const std::string& eventName = limitMatch.first;
220 for (const auto& limitEventAttr : limitEventAttrs)
221 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800222 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800223 std::ifstream eventFile(limitEventPath);
224 if (!eventFile.good())
225 {
226 continue;
227 }
228 eventPathList[eventName].push_back(limitEventPath);
229 }
230 }
231}
Cheng C Yang916360b2019-05-07 18:47:16 +0800232
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530233static void
234 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
235 const std::string& interfacePath,
236 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
237 sdbusplus::asio::object_server& objectServer,
238 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800239{
Zev Weiss741f26e2022-08-12 18:21:02 -0700240 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800241 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700242 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800243 {
244 continue;
245 }
246
247 const std::string& sensorPathStr = sensorPath.string();
248 const std::string& pwmPathStr =
249 boost::replace_all_copy(sensorPathStr, "input", "target");
250 std::ifstream pwmFile(pwmPathStr);
251 if (!pwmFile.good())
252 {
253 continue;
254 }
255
256 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
257 if (findPWMSensor != pwmSensors.end())
258 {
259 continue;
260 }
261
Zev Weissd8c293a2022-08-15 18:58:41 -0700262 std::string name = "Pwm_";
263 name += psuName;
264 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700265 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700266
267 std::string objPath = interfacePath;
268 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700269 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700270
Cheng C Yang916360b2019-05-07 18:47:16 +0800271 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700272 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800273 }
274}
275
Zhikui Ren23c96e72020-11-05 22:32:28 -0800276static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800277 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800278 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
279 const ManagedObjectType& sensorConfigs,
280 const std::shared_ptr<boost::container::flat_set<std::string>>&
281 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800282{
Josh Lehan49cfba92019-10-08 16:50:42 -0700283 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800284 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800285
286 std::vector<fs::path> pmbusPaths;
287 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
288 {
289 std::cerr << "No PSU sensors in system\n";
290 return;
291 }
292
293 boost::container::flat_set<std::string> directories;
294 for (const auto& pmbusPath : pmbusPaths)
295 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800296 boost::container::flat_map<std::string, std::vector<std::string>>
297 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800298 boost::container::flat_map<
299 std::string,
300 boost::container::flat_map<std::string, std::vector<std::string>>>
301 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800302
303 std::ifstream nameFile(pmbusPath);
304 if (!nameFile.good())
305 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700306 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800307 continue;
308 }
309
310 std::string pmbusName;
311 std::getline(nameFile, pmbusName);
312 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700313
Matt Simmering786efb82023-01-18 14:09:21 -0800314 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800315 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700316 // To avoid this error message, add your driver name to
317 // the pmbusNames vector at the top of this file.
318 std::cerr << "Driver name " << pmbusName
319 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800320 continue;
321 }
322
Cheng C Yang209ec562019-03-12 16:37:44 +0800323 auto directory = pmbusPath.parent_path();
324
325 auto ret = directories.insert(directory.string());
326 if (!ret.second)
327 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700328 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800329 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800330 }
331
James Feistb6c0b912019-07-09 12:21:44 -0700332 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800333 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700334 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800335 if (findHyphen == std::string::npos)
336 {
337 std::cerr << "found bad device" << deviceName << "\n";
338 continue;
339 }
340 std::string busStr = deviceName.substr(0, findHyphen);
341 std::string addrStr = deviceName.substr(findHyphen + 1);
342
343 size_t bus = 0;
344 size_t addr = 0;
345
346 try
347 {
348 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700349 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800350 }
Patrick Williams26601e82021-10-06 12:43:25 -0500351 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800352 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700353 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
354 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800355 continue;
356 }
357
Zev Weisse8b97ee2022-08-12 15:23:51 -0700358 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 const SensorData* sensorData = nullptr;
360 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800361 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800362 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800363
Zev Weiss741f26e2022-08-12 18:21:02 -0700364 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700366 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800367 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 {
Zev Weiss054aad82022-08-18 01:37:34 -0700369 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800370 if (sensorBase != sensorData->end())
371 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700372 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800373 sensorType = type;
374 break;
375 }
376 }
377 if (baseConfig == nullptr)
378 {
379 std::cerr << "error finding base configuration for "
380 << deviceName << "\n";
381 continue;
382 }
383
Zev Weisse8b97ee2022-08-12 15:23:51 -0700384 auto configBus = baseConfig->find("Bus");
385 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800386
Zev Weisse8b97ee2022-08-12 15:23:51 -0700387 if (configBus == baseConfig->end() ||
388 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800390 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800391 continue;
392 }
393
Matt Simmering6747eba2023-01-18 16:03:55 -0800394 const uint64_t* confBus =
395 std::get_if<uint64_t>(&(configBus->second));
396 const uint64_t* confAddr =
397 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800398 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800399 {
400 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700401 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800402 continue;
403 }
404
405 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800406 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700407 std::cerr << "Configuration skipping " << *confBus << "-"
408 << *confAddr << " because not " << bus << "-" << addr
409 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800410 continue;
411 }
412
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800413 std::vector<thresholds::Threshold> confThresholds;
414 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
415 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800416 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800417 }
418 thresholdConfSize = confThresholds.size();
419
Zev Weiss741f26e2022-08-12 18:21:02 -0700420 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 break;
422 }
423 if (interfacePath == nullptr)
424 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700425 // To avoid this error message, add your export map entry,
426 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800427 std::cerr << "failed to find match for " << deviceName << "\n";
428 continue;
429 }
430
Zev Weisse8b97ee2022-08-12 15:23:51 -0700431 auto findPSUName = baseConfig->find("Name");
432 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800433 {
434 std::cerr << "could not determine configuration name for "
435 << deviceName << "\n";
436 continue;
437 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800438 const std::string* psuName =
439 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800440 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800441 {
442 std::cerr << "Cannot find psu name, invalid configuration\n";
443 continue;
444 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800445
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800446 auto findCPU = baseConfig->find("CPURequired");
447 if (findCPU != baseConfig->end())
448 {
449 size_t index =
450 std::visit(VariantToIntVisitor(), findCPU->second);
451 auto presenceFind = cpuPresence.find(index);
452 if (presenceFind == cpuPresence.end() || !presenceFind->second)
453 {
454 continue;
455 }
456 }
457
Zhikui Ren23c96e72020-11-05 22:32:28 -0800458 // on rescans, only update sensors we were signaled by
459 if (!firstScan)
460 {
Zhikui Renda98f092021-11-01 09:41:08 -0700461 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800462 auto it =
463 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
464 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800465 return s.ends_with(psuNameStr);
466 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800467
468 if (it == sensorsChanged->end())
469 {
470 continue;
471 }
472 sensorsChanged->erase(it);
473 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800474 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800475 checkGroupEvent(directory.string(), groupEventMatch,
476 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800477
Zev Weisse8b97ee2022-08-12 15:23:51 -0700478 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000479
Vijay Khemka996bad12019-05-28 15:15:16 -0700480 /* Check if there are more sensors in the same interface */
481 int i = 1;
482 std::vector<std::string> psuNames;
483 do
484 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700485 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700486 psuNames.push_back(
487 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700488 findPSUName = baseConfig->find("Name" + std::to_string(i++));
489 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700490
Cheng C Yange50345b2019-04-02 17:26:15 +0800491 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700492 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800493 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800494 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800495 continue;
496 }
497
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530498 /* read max value in sysfs for in, curr, power, temp, ... */
499 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
500 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700501 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530502 {
503 std::cerr << "No max name in PSU \n";
504 }
505 }
506
Zev Weiss8569bf22022-10-11 15:37:44 -0700507 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800508
Vijay Khemka996bad12019-05-28 15:15:16 -0700509 /* Find array of labels to be exposed if it is defined in config */
510 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700511 auto findLabelObj = baseConfig->find("Labels");
512 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700513 {
514 findLabels =
515 std::get<std::vector<std::string>>(findLabelObj->second);
516 }
517
Jason Ling5747fab2019-10-02 16:46:23 -0700518 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
519 std::smatch matches;
520
Cheng C Yange50345b2019-04-02 17:26:15 +0800521 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800522 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530523 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800524 std::string labelHead;
525 std::string sensorPathStr = sensorPath.string();
526 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700527 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700528 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
529 {
Josh Lehan06494452019-10-31 09:49:16 -0700530 // hwmon *_input filename without number:
531 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700532 sensorNameSubStr = matches[1];
533 }
534 else
535 {
Josh Lehan06494452019-10-31 09:49:16 -0700536 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700537 << sensorNameStr;
538 continue;
539 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800540
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530541 std::string labelPath;
542
543 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700544 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530545 if (pos != std::string::npos)
546 {
547
548 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700549 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530550 {
551 labelPath =
552 boost::replace_all_copy(sensorPathStr, "max", "label");
553 maxLabel = true;
554 }
555 else
556 {
557 labelPath = boost::replace_all_copy(sensorPathStr, "input",
558 "label");
559 maxLabel = false;
560 }
561 }
562 else
563 {
564 continue;
565 }
566
Cheng C Yangecba9de2019-09-12 23:41:50 +0800567 std::ifstream labelFile(labelPath);
568 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800569 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700570 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800571 {
572 std::cerr << "Input file " << sensorPath
573 << " has no corresponding label file\n";
574 }
Josh Lehan06494452019-10-31 09:49:16 -0700575 // hwmon *_input filename with number:
576 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700577 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800578 }
579 else
580 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800581 std::string label;
582 std::getline(labelFile, label);
583 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800584 auto findSensor = sensors.find(label);
585 if (findSensor != sensors.end())
586 {
587 continue;
588 }
589
Josh Lehan06494452019-10-31 09:49:16 -0700590 // hwmon corresponding *_label file contents:
591 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700592 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800593 }
594
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530595 /* append "max" for labelMatch */
596 if (maxLabel)
597 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700598 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530599 }
600
Ed Tanous8a57ec02020-10-09 12:46:52 -0700601 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700602 {
603 std::cerr << "Sensor type=\"" << sensorNameSubStr
604 << "\" label=\"" << labelHead << "\"\n";
605 }
606
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530607 checkPWMSensor(sensorPath, labelHead, *interfacePath,
608 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800609
Vijay Khemka996bad12019-05-28 15:15:16 -0700610 if (!findLabels.empty())
611 {
612 /* Check if this labelHead is enabled in config file */
613 if (std::find(findLabels.begin(), findLabels.end(),
614 labelHead) == findLabels.end())
615 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700616 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800617 {
618 std::cerr << "could not find " << labelHead
619 << " in the Labels list\n";
620 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700621 continue;
622 }
623 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800624
Cheng C Yange50345b2019-04-02 17:26:15 +0800625 auto findProperty = labelMatch.find(labelHead);
626 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800627 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700628 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700629 {
630 std::cerr << "Could not find matching default property for "
631 << labelHead << "\n";
632 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800633 continue;
634 }
635
Josh Lehan74d9bd92019-10-31 08:51:58 -0700636 // Protect the hardcoded labelMatch list from changes,
637 // by making a copy and modifying that instead.
638 // Avoid bleedthrough of one device's customizations to
639 // the next device, as each should be independently customizable.
640 psuProperties.push_back(findProperty->second);
641 auto psuProperty = psuProperties.rbegin();
642
643 // Use label head as prefix for reading from config file,
644 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
645 std::string keyName = labelHead + "_Name";
646 std::string keyScale = labelHead + "_Scale";
647 std::string keyMin = labelHead + "_Min";
648 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800649 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800650 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700651
652 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700653 auto findCustomName = baseConfig->find(keyName);
654 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700655 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700656 try
657 {
658 psuProperty->labelTypeName = std::visit(
659 VariantToStringVisitor(), findCustomName->second);
660 }
Patrick Williams26601e82021-10-06 12:43:25 -0500661 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700662 {
663 std::cerr << "Unable to parse " << keyName << "\n";
664 continue;
665 }
666
667 // All strings are valid, including empty string
668 customizedName = true;
669 }
670
671 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700672 auto findCustomScale = baseConfig->find(keyScale);
673 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700674 {
675 try
676 {
677 psuProperty->sensorScaleFactor = std::visit(
678 VariantToUnsignedIntVisitor(), findCustomScale->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 " << keyScale << "\n";
683 continue;
684 }
685
686 // Avoid later division by zero
687 if (psuProperty->sensorScaleFactor > 0)
688 {
689 customizedScale = true;
690 }
691 else
692 {
693 std::cerr << "Unable to accept " << keyScale << "\n";
694 continue;
695 }
696 }
697
Zev Weisse8b97ee2022-08-12 15:23:51 -0700698 auto findCustomMin = baseConfig->find(keyMin);
699 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700 {
701 try
702 {
703 psuProperty->minReading = std::visit(
704 VariantToDoubleVisitor(), findCustomMin->second);
705 }
Patrick Williams26601e82021-10-06 12:43:25 -0500706 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700707 {
708 std::cerr << "Unable to parse " << keyMin << "\n";
709 continue;
710 }
711 }
712
Zev Weisse8b97ee2022-08-12 15:23:51 -0700713 auto findCustomMax = baseConfig->find(keyMax);
714 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700715 {
716 try
717 {
718 psuProperty->maxReading = std::visit(
719 VariantToDoubleVisitor(), findCustomMax->second);
720 }
Patrick Williams26601e82021-10-06 12:43:25 -0500721 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700722 {
723 std::cerr << "Unable to parse " << keyMax << "\n";
724 continue;
725 }
726 }
727
Zev Weisse8b97ee2022-08-12 15:23:51 -0700728 auto findCustomOffset = baseConfig->find(keyOffset);
729 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800730 {
731 try
732 {
733 psuProperty->sensorOffset = std::visit(
734 VariantToDoubleVisitor(), findCustomOffset->second);
735 }
Patrick Williams26601e82021-10-06 12:43:25 -0500736 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800737 {
738 std::cerr << "Unable to parse " << keyOffset << "\n";
739 continue;
740 }
741 }
742
Lotus Xucb5af732021-09-10 15:18:50 +0800743 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700744 auto findPowerState = baseConfig->find(keyPowerState);
745 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800746 {
747 std::string powerState = std::visit(VariantToStringVisitor(),
748 findPowerState->second);
749 setReadState(powerState, readState);
750 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700751 if (!(psuProperty->minReading < psuProperty->maxReading))
752 {
753 std::cerr << "Min must be less than Max\n";
754 continue;
755 }
756
757 // If the sensor name is being customized by config file,
758 // then prefix/suffix composition becomes not necessary,
759 // and in fact not wanted, because it gets in the way.
760 std::string psuNameFromIndex;
761 if (!customizedName)
762 {
763 /* Find out sensor name index for this label */
764 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500765 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700766 if (std::regex_search(labelHead, matches, rgx))
767 {
768 nameIndex = std::stoi(matches[1]);
769
770 // Decrement to preserve alignment, because hwmon
771 // human-readable filenames and labels use 1-based
772 // numbering, but the "Name", "Name1", "Name2", etc. naming
773 // convention (the psuNames vector) uses 0-based numbering.
774 if (nameIndex > 0)
775 {
776 --nameIndex;
777 }
778 }
779 else
780 {
781 nameIndex = 0;
782 }
783
784 if (psuNames.size() <= nameIndex)
785 {
786 std::cerr << "Could not pair " << labelHead
787 << " with a Name field\n";
788 continue;
789 }
790
791 psuNameFromIndex = psuNames[nameIndex];
792
Ed Tanous8a57ec02020-10-09 12:46:52 -0700793 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700794 {
795 std::cerr << "Sensor label head " << labelHead
796 << " paired with " << psuNameFromIndex
797 << " at index " << nameIndex << "\n";
798 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700799 }
800
Cheng C Yang58b2b532019-05-31 00:19:45 +0800801 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
802
Josh Lehan74d9bd92019-10-31 08:51:58 -0700803 // Similarly, if sensor scaling factor is being customized,
804 // then the below power-of-10 constraint becomes unnecessary,
805 // as config should be able to specify an arbitrary divisor.
806 unsigned int factor = psuProperty->sensorScaleFactor;
807 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700808 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700809 // Preserve existing usage of hardcoded labelMatch table below
810 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700811
Josh Lehan74d9bd92019-10-31 08:51:58 -0700812 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700813 char firstChar =
814 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700815 std::string strScaleFactor =
816 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
817
818 // Preserve existing configs by accepting earlier syntax,
819 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700820 auto findScaleFactor = baseConfig->find(strScaleFactor);
821 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700822 {
823 factor = std::visit(VariantToIntVisitor(),
824 findScaleFactor->second);
825 }
826
Ed Tanous8a57ec02020-10-09 12:46:52 -0700827 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700828 {
829 std::cerr << "Sensor scaling factor " << factor
830 << " string " << strScaleFactor << "\n";
831 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700832 }
833
Vijay Khemka996bad12019-05-28 15:15:16 -0700834 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530835 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
836 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800837 {
James Feist17ab6e02019-06-25 12:28:13 -0700838 std::cerr << "error populating thresholds for "
839 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800840 }
841
Zev Weiss6b6891c2021-04-22 02:46:21 -0500842 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
843 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800844 {
Jason Ling5747fab2019-10-02 16:46:23 -0700845 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700846 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800847 continue;
848 }
849
Ed Tanous8a57ec02020-10-09 12:46:52 -0700850 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700851 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700852 std::cerr << "Sensor properties: Name \""
853 << psuProperty->labelTypeName << "\" Scale "
854 << psuProperty->sensorScaleFactor << " Min "
855 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800856 << psuProperty->maxReading << " Offset "
857 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700858 }
859
860 std::string sensorName = psuProperty->labelTypeName;
861 if (customizedName)
862 {
863 if (sensorName.empty())
864 {
865 // Allow selective disabling of an individual sensor,
866 // by customizing its name to an empty string.
867 std::cerr << "Sensor disabled, empty string\n";
868 continue;
869 }
870 }
871 else
872 {
873 // Sensor name not customized, do prefix/suffix composition,
874 // preserving default behavior by using psuNameFromIndex.
875 sensorName =
876 psuNameFromIndex + " " + psuProperty->labelTypeName;
877 }
878
Ed Tanous8a57ec02020-10-09 12:46:52 -0700879 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700880 {
881 std::cerr << "Sensor name \"" << sensorName << "\" path \""
882 << sensorPathStr << "\" type \"" << sensorType
883 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700884 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800885 // destruct existing one first if already created
886 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800887 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800888 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800889 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000890 readState, findSensorUnit->second, factor,
891 psuProperty->maxReading, psuProperty->minReading,
892 psuProperty->sensorOffset, labelHead, thresholdConfSize,
893 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800894 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700895 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700896 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700897 {
898 std::cerr << "Created " << numCreated << " sensors so far\n";
899 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800900 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800901
902 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800903 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800904 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000905 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
906 *psuName, readState,
907 eventPathList, groupEventPathList,
908 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800909 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700910
Ed Tanous8a57ec02020-10-09 12:46:52 -0700911 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700912 {
913 std::cerr << "Created total of " << numCreated << " sensors\n";
914 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800915}
Cheng C Yang209ec562019-03-12 16:37:44 +0800916
Zhikui Ren23c96e72020-11-05 22:32:28 -0800917void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800918 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800919 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
920 const std::shared_ptr<boost::container::flat_set<std::string>>&
921 sensorsChanged)
922{
923 auto getter = std::make_shared<GetSensorConfiguration>(
924 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
925 const ManagedObjectType& sensorConfigs) {
926 createSensorsCallback(io, objectServer, dbusConnection,
927 sensorConfigs, sensorsChanged);
928 });
Matt Simmering786efb82023-01-18 14:09:21 -0800929 std::vector<std::string> types(sensorTypes.size());
930 for (const auto& [type, dt] : sensorTypes)
931 {
932 types.push_back(type);
933 }
934 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800935}
936
Cheng C Yang916360b2019-05-07 18:47:16 +0800937void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800938{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500939 sensorTable = {{"power", sensor_paths::unitWatts},
940 {"curr", sensor_paths::unitAmperes},
941 {"temp", sensor_paths::unitDegreesC},
942 {"in", sensor_paths::unitVolts},
943 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800944
Jeff Line41d52f2021-04-07 19:38:51 +0800945 labelMatch = {
946 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800947 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
948 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800949 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
950 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
951 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
952 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800953 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
954 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
955 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800956 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
957 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
958 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
959 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
960 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
961 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
962 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800992 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800993 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800994 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001000 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001001 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1002 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001003 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1004 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1007 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1008 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1009 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1013 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1014 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1015 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1016 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1017 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001018 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1019 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1020 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001021 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1022 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1023 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1024 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1025 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1026 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1027 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1028 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1029 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001030 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1031 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1032 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001033
Matt Simmering6747eba2023-01-18 16:03:55 -08001034 pwmTable = {{"fan1", "Fan_1"},
1035 {"fan2", "Fan_2"},
1036 {"fan3", "Fan_3"},
1037 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001038
1039 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1040 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1041
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001042 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1043 {"Failure", {"in2_alarm"}},
1044 {"ACLost", {"in1_beep"}},
1045 {"ConfigureError", {"in1_fault"}}};
1046
1047 groupEventMatch = {{"FanFault",
1048 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001049 {"fan2", {"fan2_alarm", "fan2_fault"}},
1050 {"fan3", {"fan3_alarm", "fan3_fault"}},
1051 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001052}
1053
James Feistb6c0b912019-07-09 12:21:44 -07001054int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001055{
Ed Tanous1f978632023-02-28 18:16:39 -08001056 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001057 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1058
Ed Tanous14ed5e92022-07-12 15:50:23 -07001059 sdbusplus::asio::object_server objectServer(systemBus, true);
1060 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001061 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001062 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001063 auto sensorsChanged =
1064 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001065
Cheng C Yang916360b2019-05-07 18:47:16 +08001066 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001067
Matt Simmering786efb82023-01-18 14:09:21 -08001068 boost::asio::post(
1069 io, [&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001070 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001071 std::function<void(sdbusplus::message_t&)> eventHandler =
1072 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001073 if (message.is_method_error())
1074 {
1075 std::cerr << "callback method error\n";
1076 return;
1077 }
1078 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001079 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001080 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1081 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001082 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001083 return;
1084 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001085 if (ec)
1086 {
1087 std::cerr << "timer error\n";
1088 }
1089 createSensors(io, objectServer, systemBus, sensorsChanged);
1090 });
1091 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001092
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001093 boost::asio::steady_timer cpuFilterTimer(io);
1094 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1095 [&](sdbusplus::message_t& message) {
1096 std::string path = message.get_path();
1097 boost::to_lower(path);
1098
1099 sdbusplus::message::object_path cpuPath(path);
1100 std::string cpuName = cpuPath.filename();
1101 if (!cpuName.starts_with("cpu"))
1102 {
1103 return;
1104 }
1105 size_t index = 0;
1106 try
1107 {
1108 index = std::stoi(path.substr(path.size() - 1));
1109 }
1110 catch (const std::invalid_argument&)
1111 {
1112 std::cerr << "Found invalid path " << path << "\n";
1113 return;
1114 }
1115
1116 std::string objectName;
1117 boost::container::flat_map<std::string, std::variant<bool>> values;
1118 message.read(objectName, values);
1119 auto findPresence = values.find("Present");
1120 try
1121 {
1122 cpuPresence[index] = std::get<bool>(findPresence->second);
1123 }
1124 catch(const std::bad_variant_access& err)
1125 {
1126 return;
1127 }
1128
1129 if(!cpuPresence[index])
1130 {
1131 return;
1132 }
1133 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1134 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1135 if (ec == boost::asio::error::operation_aborted)
1136 {
1137 return;
1138 }
1139 if (ec)
1140 {
1141 std::cerr << "timer error\n";
1142 return;
1143 }
1144 createSensors(io, objectServer, systemBus, nullptr);
1145 });
1146 };
1147
Zev Weiss214d9712022-08-12 12:54:31 -07001148 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1149 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001150
1151 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1152 static_cast<sdbusplus::bus_t&>(*systemBus),
1153 "type='signal',member='PropertiesChanged',path_namespace='" +
1154 std::string(cpuInventoryPath) +
1155 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1156 cpuPresenceHandler));
1157
Bruce Lee1263c3d2021-06-04 15:16:33 +08001158 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001159 io.run();
1160}