blob: 2933c70064c952bfc270700563fb284d84f4991e [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <PSUEvent.hpp>
18#include <PSUSensor.hpp>
19#include <Utils.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080020#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
28#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070029#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080033#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080034#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080036#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <utility>
38#include <variant>
39#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080040
Ed Tanous8a57ec02020-10-09 12:46:52 -070041static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070042
Brandon Kim66558232021-11-09 16:53:08 -080043static constexpr auto sensorTypes{std::to_array<const char*>(
44 {"xyz.openbmc_project.Configuration.ADM1266",
45 "xyz.openbmc_project.Configuration.ADM1272",
46 "xyz.openbmc_project.Configuration.ADM1275",
47 "xyz.openbmc_project.Configuration.ADM1278",
linchuyuan5cf66df2021-12-21 17:38:55 -080048 "xyz.openbmc_project.Configuration.BMR490",
Brandon Kim66558232021-11-09 16:53:08 -080049 "xyz.openbmc_project.Configuration.DPS800",
50 "xyz.openbmc_project.Configuration.INA219",
51 "xyz.openbmc_project.Configuration.INA230",
52 "xyz.openbmc_project.Configuration.IPSPS",
53 "xyz.openbmc_project.Configuration.ISL68137",
54 "xyz.openbmc_project.Configuration.ISL68220",
55 "xyz.openbmc_project.Configuration.ISL68223",
Khang Kieu7d6b77d2022-02-10 10:43:36 +000056 "xyz.openbmc_project.Configuration.ISL69225",
Brandon Kim66558232021-11-09 16:53:08 -080057 "xyz.openbmc_project.Configuration.ISL69243",
58 "xyz.openbmc_project.Configuration.ISL69260",
59 "xyz.openbmc_project.Configuration.LM25066",
60 "xyz.openbmc_project.Configuration.MAX16601",
61 "xyz.openbmc_project.Configuration.MAX20710",
62 "xyz.openbmc_project.Configuration.MAX20730",
63 "xyz.openbmc_project.Configuration.MAX20734",
64 "xyz.openbmc_project.Configuration.MAX20796",
65 "xyz.openbmc_project.Configuration.MAX34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080066 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080067 "xyz.openbmc_project.Configuration.pmbus",
68 "xyz.openbmc_project.Configuration.PXE1610",
69 "xyz.openbmc_project.Configuration.RAA228000",
70 "xyz.openbmc_project.Configuration.RAA228228",
71 "xyz.openbmc_project.Configuration.RAA229004",
72 "xyz.openbmc_project.Configuration.TPS546D24",
73 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080074
Ed Tanousa2df7862021-12-07 16:30:27 -080075// clang-format off
76static constexpr auto pmbusNames{std::to_array<const char*>({
77 "adm1266",
78 "adm1272",
79 "adm1275",
80 "adm1278",
linchuyuan5cf66df2021-12-21 17:38:55 -080081 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080082 "dps800",
83 "ina219",
84 "ina230",
85 "ipsps1",
86 "isl68137",
87 "isl68220",
88 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000089 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080090 "isl69243",
91 "isl69260",
92 "lm25066",
93 "max16601",
94 "max20710",
95 "max20730",
96 "max20734",
97 "max20796",
98 "max34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080099 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -0800100 "pmbus",
101 "pxe1610",
102 "raa228000",
103 "raa228228",
104 "raa229004",
105 "tps546d24",
106 "xdpe12284"
107})};
108//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700109
Cheng C Yang209ec562019-03-12 16:37:44 +0800110namespace fs = std::filesystem;
111
Yong Libf8b1da2020-04-15 16:32:50 +0800112static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800113 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800114static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
115 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800116static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
117 pwmSensors;
118static boost::container::flat_map<std::string, std::string> sensorTable;
119static boost::container::flat_map<std::string, PSUProperty> labelMatch;
120static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800121static boost::container::flat_map<std::string, std::vector<std::string>>
122 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800123static boost::container::flat_map<
124 std::string,
125 boost::container::flat_map<std::string, std::vector<std::string>>>
126 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800127static boost::container::flat_map<std::string, std::vector<std::string>>
128 limitEventMatch;
129
Josh Lehan74d9bd92019-10-31 08:51:58 -0700130static std::vector<PSUProperty> psuProperties;
131
Cheng C Yang58b2b532019-05-31 00:19:45 +0800132// Function CheckEvent will check each attribute from eventMatch table in the
133// sysfs. If the attributes exists in sysfs, then store the complete path
134// of the attribute into eventPathList.
135void checkEvent(
136 const std::string& directory,
137 const boost::container::flat_map<std::string, std::vector<std::string>>&
138 eventMatch,
139 boost::container::flat_map<std::string, std::vector<std::string>>&
140 eventPathList)
141{
142 for (const auto& match : eventMatch)
143 {
144 const std::vector<std::string>& eventAttrs = match.second;
145 const std::string& eventName = match.first;
146 for (const auto& eventAttr : eventAttrs)
147 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700148 std::string eventPath = directory;
149 eventPath += "/";
150 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800151
152 std::ifstream eventFile(eventPath);
153 if (!eventFile.good())
154 {
155 continue;
156 }
157
158 eventPathList[eventName].push_back(eventPath);
159 }
160 }
161}
162
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800163// Check Group Events which contains more than one targets in each combine
164// events.
165void checkGroupEvent(
166 const std::string& directory,
167 const boost::container::flat_map<
168 std::string,
169 boost::container::flat_map<std::string, std::vector<std::string>>>&
170 groupEventMatch,
171 boost::container::flat_map<
172 std::string,
173 boost::container::flat_map<std::string, std::vector<std::string>>>&
174 groupEventPathList)
175{
176 for (const auto& match : groupEventMatch)
177 {
178 const std::string& groupEventName = match.first;
179 const boost::container::flat_map<std::string, std::vector<std::string>>
180 events = match.second;
181 boost::container::flat_map<std::string, std::vector<std::string>>
182 pathList;
183 for (const auto& match : events)
184 {
185 const std::string& eventName = match.first;
186 const std::vector<std::string>& eventAttrs = match.second;
187 for (const auto& eventAttr : eventAttrs)
188 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700189 std::string eventPath = directory;
190 eventPath += "/";
191 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800192 std::ifstream eventFile(eventPath);
193 if (!eventFile.good())
194 {
195 continue;
196 }
197
198 pathList[eventName].push_back(eventPath);
199 }
200 }
201 groupEventPathList[groupEventName] = pathList;
202 }
203}
204
Cheng C Yang58b2b532019-05-31 00:19:45 +0800205// Function checkEventLimits will check all the psu related xxx_input attributes
206// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
207// xxx_min_alarm exist, then store the existing paths of the alarm attributes
208// to eventPathList.
209void checkEventLimits(
210 const std::string& sensorPathStr,
211 const boost::container::flat_map<std::string, std::vector<std::string>>&
212 limitEventMatch,
213 boost::container::flat_map<std::string, std::vector<std::string>>&
214 eventPathList)
215{
Lei YUa2c7cea2020-12-23 14:07:28 +0800216 auto attributePartPos = sensorPathStr.find_last_of('_');
217 if (attributePartPos == std::string::npos)
218 {
219 // There is no '_' in the string, skip it
220 return;
221 }
222 auto attributePart =
223 std::string_view(sensorPathStr).substr(attributePartPos + 1);
224 if (attributePart != "input")
225 {
226 // If the sensor is not xxx_input, skip it
227 return;
228 }
229
230 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800231 for (const auto& limitMatch : limitEventMatch)
232 {
233 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
234 const std::string& eventName = limitMatch.first;
235 for (const auto& limitEventAttr : limitEventAttrs)
236 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800237 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800238 std::ifstream eventFile(limitEventPath);
239 if (!eventFile.good())
240 {
241 continue;
242 }
243 eventPathList[eventName].push_back(limitEventPath);
244 }
245 }
246}
Cheng C Yang916360b2019-05-07 18:47:16 +0800247
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530248static void
249 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
250 const std::string& interfacePath,
251 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
252 sdbusplus::asio::object_server& objectServer,
253 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800254{
255 for (const auto& pwmName : pwmTable)
256 {
257 if (pwmName.first != labelHead)
258 {
259 continue;
260 }
261
262 const std::string& sensorPathStr = sensorPath.string();
263 const std::string& pwmPathStr =
264 boost::replace_all_copy(sensorPathStr, "input", "target");
265 std::ifstream pwmFile(pwmPathStr);
266 if (!pwmFile.good())
267 {
268 continue;
269 }
270
271 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
272 if (findPWMSensor != pwmSensors.end())
273 {
274 continue;
275 }
276
277 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530278 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
279 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800280 }
281}
282
Zhikui Ren23c96e72020-11-05 22:32:28 -0800283static void createSensorsCallback(
284 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
285 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
286 const ManagedObjectType& sensorConfigs,
287 const std::shared_ptr<boost::container::flat_set<std::string>>&
288 sensorsChanged)
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
293 std::vector<fs::path> pmbusPaths;
294 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
295 {
296 std::cerr << "No PSU sensors in system\n";
297 return;
298 }
299
300 boost::container::flat_set<std::string> directories;
301 for (const auto& pmbusPath : pmbusPaths)
302 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800303 boost::container::flat_map<std::string, std::vector<std::string>>
304 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800305 boost::container::flat_map<
306 std::string,
307 boost::container::flat_map<std::string, std::vector<std::string>>>
308 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800309
310 std::ifstream nameFile(pmbusPath);
311 if (!nameFile.good())
312 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700313 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800314 continue;
315 }
316
317 std::string pmbusName;
318 std::getline(nameFile, pmbusName);
319 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700320
321 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
322 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800323 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700324 // To avoid this error message, add your driver name to
325 // the pmbusNames vector at the top of this file.
326 std::cerr << "Driver name " << pmbusName
327 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800328 continue;
329 }
330
Cheng C Yang209ec562019-03-12 16:37:44 +0800331 auto directory = pmbusPath.parent_path();
332
333 auto ret = directories.insert(directory.string());
334 if (!ret.second)
335 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700336 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800337 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800338 }
339
James Feistb6c0b912019-07-09 12:21:44 -0700340 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800341 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700342 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800343 if (findHyphen == std::string::npos)
344 {
345 std::cerr << "found bad device" << deviceName << "\n";
346 continue;
347 }
348 std::string busStr = deviceName.substr(0, findHyphen);
349 std::string addrStr = deviceName.substr(findHyphen + 1);
350
351 size_t bus = 0;
352 size_t addr = 0;
353
354 try
355 {
356 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700357 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 }
Patrick Williams26601e82021-10-06 12:43:25 -0500359 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700361 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
362 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 continue;
364 }
365
Cheng C Yang209ec562019-03-12 16:37:44 +0800366 const std::pair<std::string, boost::container::flat_map<
367 std::string, BasicVariantType>>*
368 baseConfig = nullptr;
369 const SensorData* sensorData = nullptr;
370 const std::string* interfacePath = nullptr;
371 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800372 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800373
374 for (const std::pair<sdbusplus::message::object_path, SensorData>&
375 sensor : sensorConfigs)
376 {
377 sensorData = &(sensor.second);
378 for (const char* type : sensorTypes)
379 {
380 auto sensorBase = sensorData->find(type);
381 if (sensorBase != sensorData->end())
382 {
383 baseConfig = &(*sensorBase);
384 sensorType = type;
385 break;
386 }
387 }
388 if (baseConfig == nullptr)
389 {
390 std::cerr << "error finding base configuration for "
391 << deviceName << "\n";
392 continue;
393 }
394
395 auto configBus = baseConfig->second.find("Bus");
396 auto configAddress = baseConfig->second.find("Address");
397
398 if (configBus == baseConfig->second.end() ||
399 configAddress == baseConfig->second.end())
400 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800401 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800402 continue;
403 }
404
Ed Tanousa771f6a2022-01-14 09:36:51 -0800405 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
406 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
407 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 {
425 std::cerr << "error populating totoal thresholds\n";
426 }
427 thresholdConfSize = confThresholds.size();
428
Cheng C Yang209ec562019-03-12 16:37:44 +0800429 interfacePath = &(sensor.first.str);
430 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
Cheng C Yange50345b2019-04-02 17:26:15 +0800440 auto findPSUName = baseConfig->second.find("Name");
441 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800442 {
443 std::cerr << "could not determine configuration name for "
444 << deviceName << "\n";
445 continue;
446 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800447 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
448 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800449 {
450 std::cerr << "Cannot find psu name, invalid configuration\n";
451 continue;
452 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800453
454 // on rescans, only update sensors we were signaled by
455 if (!firstScan)
456 {
Zhikui Renda98f092021-11-01 09:41:08 -0700457 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800458 auto it =
459 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
460 [psuNameStr](std::string& s) {
461 return boost::ends_with(s, psuNameStr);
462 });
463
464 if (it == sensorsChanged->end())
465 {
466 continue;
467 }
468 sensorsChanged->erase(it);
469 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800470 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800471 checkGroupEvent(directory.string(), groupEventMatch,
472 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800473
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000474 PowerState readState = PowerState::always;
475 auto findPowerOn = baseConfig->second.find("PowerState");
476 if (findPowerOn != baseConfig->second.end())
477 {
478 std::string powerState =
479 std::visit(VariantToStringVisitor(), findPowerOn->second);
480 setReadState(powerState, readState);
481 }
482
Vijay Khemka996bad12019-05-28 15:15:16 -0700483 /* Check if there are more sensors in the same interface */
484 int i = 1;
485 std::vector<std::string> psuNames;
486 do
487 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700488 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700489 psuNames.push_back(
490 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700491 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
492 } while (findPSUName != baseConfig->second.end());
493
Cheng C Yange50345b2019-04-02 17:26:15 +0800494 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700495 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800496 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800497 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800498 continue;
499 }
500
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530501 /* read max value in sysfs for in, curr, power, temp, ... */
502 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
503 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700504 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530505 {
506 std::cerr << "No max name in PSU \n";
507 }
508 }
509
Lei YU7170a232021-02-04 16:19:27 +0800510 /* The poll rate for the sensors */
511 double pollRate = 0.0;
512 auto pollRateObj = baseConfig->second.find("PollRate");
513
514 if (pollRateObj != baseConfig->second.end())
515 {
516 pollRate =
517 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
518 if (pollRate <= 0.0)
519 {
520 pollRate = PSUSensor::defaultSensorPoll;
521 }
522 }
523
Vijay Khemka996bad12019-05-28 15:15:16 -0700524 /* Find array of labels to be exposed if it is defined in config */
525 std::vector<std::string> findLabels;
526 auto findLabelObj = baseConfig->second.find("Labels");
527 if (findLabelObj != baseConfig->second.end())
528 {
529 findLabels =
530 std::get<std::vector<std::string>>(findLabelObj->second);
531 }
532
Jason Ling5747fab2019-10-02 16:46:23 -0700533 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
534 std::smatch matches;
535
Cheng C Yange50345b2019-04-02 17:26:15 +0800536 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800537 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530538 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800539 std::string labelHead;
540 std::string sensorPathStr = sensorPath.string();
541 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700542 std::string sensorNameSubStr{""};
543 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
544 {
Josh Lehan06494452019-10-31 09:49:16 -0700545 // hwmon *_input filename without number:
546 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700547 sensorNameSubStr = matches[1];
548 }
549 else
550 {
Josh Lehan06494452019-10-31 09:49:16 -0700551 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700552 << sensorNameStr;
553 continue;
554 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800555
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530556 std::string labelPath;
557
558 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700559 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530560 if (pos != std::string::npos)
561 {
562
563 std::string sensorPathStrMax = sensorPathStr.substr(pos);
564 if (sensorPathStrMax.compare("_max") == 0)
565 {
566 labelPath =
567 boost::replace_all_copy(sensorPathStr, "max", "label");
568 maxLabel = true;
569 }
570 else
571 {
572 labelPath = boost::replace_all_copy(sensorPathStr, "input",
573 "label");
574 maxLabel = false;
575 }
576 }
577 else
578 {
579 continue;
580 }
581
Cheng C Yangecba9de2019-09-12 23:41:50 +0800582 std::ifstream labelFile(labelPath);
583 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800584 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700585 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800586 {
587 std::cerr << "Input file " << sensorPath
588 << " has no corresponding label file\n";
589 }
Josh Lehan06494452019-10-31 09:49:16 -0700590 // hwmon *_input filename with number:
591 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700592 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800593 }
594 else
595 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800596 std::string label;
597 std::getline(labelFile, label);
598 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800599 auto findSensor = sensors.find(label);
600 if (findSensor != sensors.end())
601 {
602 continue;
603 }
604
Josh Lehan06494452019-10-31 09:49:16 -0700605 // hwmon corresponding *_label file contents:
606 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700607 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800608 }
609
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530610 /* append "max" for labelMatch */
611 if (maxLabel)
612 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700613 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530614 }
615
Ed Tanous8a57ec02020-10-09 12:46:52 -0700616 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700617 {
618 std::cerr << "Sensor type=\"" << sensorNameSubStr
619 << "\" label=\"" << labelHead << "\"\n";
620 }
621
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530622 checkPWMSensor(sensorPath, labelHead, *interfacePath,
623 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800624
Vijay Khemka996bad12019-05-28 15:15:16 -0700625 if (!findLabels.empty())
626 {
627 /* Check if this labelHead is enabled in config file */
628 if (std::find(findLabels.begin(), findLabels.end(),
629 labelHead) == findLabels.end())
630 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700631 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800632 {
633 std::cerr << "could not find " << labelHead
634 << " in the Labels list\n";
635 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700636 continue;
637 }
638 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800639
Cheng C Yange50345b2019-04-02 17:26:15 +0800640 auto findProperty = labelMatch.find(labelHead);
641 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800642 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700643 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700644 {
645 std::cerr << "Could not find matching default property for "
646 << labelHead << "\n";
647 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800648 continue;
649 }
650
Josh Lehan74d9bd92019-10-31 08:51:58 -0700651 // Protect the hardcoded labelMatch list from changes,
652 // by making a copy and modifying that instead.
653 // Avoid bleedthrough of one device's customizations to
654 // the next device, as each should be independently customizable.
655 psuProperties.push_back(findProperty->second);
656 auto psuProperty = psuProperties.rbegin();
657
658 // Use label head as prefix for reading from config file,
659 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
660 std::string keyName = labelHead + "_Name";
661 std::string keyScale = labelHead + "_Scale";
662 std::string keyMin = labelHead + "_Min";
663 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800664 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800665 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700666
667 bool customizedName = false;
668 auto findCustomName = baseConfig->second.find(keyName);
669 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700670 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700671 try
672 {
673 psuProperty->labelTypeName = std::visit(
674 VariantToStringVisitor(), findCustomName->second);
675 }
Patrick Williams26601e82021-10-06 12:43:25 -0500676 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700677 {
678 std::cerr << "Unable to parse " << keyName << "\n";
679 continue;
680 }
681
682 // All strings are valid, including empty string
683 customizedName = true;
684 }
685
686 bool customizedScale = false;
687 auto findCustomScale = baseConfig->second.find(keyScale);
688 if (findCustomScale != baseConfig->second.end())
689 {
690 try
691 {
692 psuProperty->sensorScaleFactor = std::visit(
693 VariantToUnsignedIntVisitor(), findCustomScale->second);
694 }
Patrick Williams26601e82021-10-06 12:43:25 -0500695 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700696 {
697 std::cerr << "Unable to parse " << keyScale << "\n";
698 continue;
699 }
700
701 // Avoid later division by zero
702 if (psuProperty->sensorScaleFactor > 0)
703 {
704 customizedScale = true;
705 }
706 else
707 {
708 std::cerr << "Unable to accept " << keyScale << "\n";
709 continue;
710 }
711 }
712
713 auto findCustomMin = baseConfig->second.find(keyMin);
714 if (findCustomMin != baseConfig->second.end())
715 {
716 try
717 {
718 psuProperty->minReading = std::visit(
719 VariantToDoubleVisitor(), findCustomMin->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 " << keyMin << "\n";
724 continue;
725 }
726 }
727
728 auto findCustomMax = baseConfig->second.find(keyMax);
729 if (findCustomMax != baseConfig->second.end())
730 {
731 try
732 {
733 psuProperty->maxReading = std::visit(
734 VariantToDoubleVisitor(), findCustomMax->second);
735 }
Patrick Williams26601e82021-10-06 12:43:25 -0500736 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 {
738 std::cerr << "Unable to parse " << keyMax << "\n";
739 continue;
740 }
741 }
742
Jeff Line41d52f2021-04-07 19:38:51 +0800743 auto findCustomOffset = baseConfig->second.find(keyOffset);
744 if (findCustomOffset != baseConfig->second.end())
745 {
746 try
747 {
748 psuProperty->sensorOffset = std::visit(
749 VariantToDoubleVisitor(), findCustomOffset->second);
750 }
Patrick Williams26601e82021-10-06 12:43:25 -0500751 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800752 {
753 std::cerr << "Unable to parse " << keyOffset << "\n";
754 continue;
755 }
756 }
757
Lotus Xucb5af732021-09-10 15:18:50 +0800758 // if we find label head power state set ,override the powerstate.
759 auto findPowerState = baseConfig->second.find(keyPowerState);
760 if (findPowerState != baseConfig->second.end())
761 {
762 std::string powerState = std::visit(VariantToStringVisitor(),
763 findPowerState->second);
764 setReadState(powerState, readState);
765 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700766 if (!(psuProperty->minReading < psuProperty->maxReading))
767 {
768 std::cerr << "Min must be less than Max\n";
769 continue;
770 }
771
772 // If the sensor name is being customized by config file,
773 // then prefix/suffix composition becomes not necessary,
774 // and in fact not wanted, because it gets in the way.
775 std::string psuNameFromIndex;
776 if (!customizedName)
777 {
778 /* Find out sensor name index for this label */
779 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500780 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700781 if (std::regex_search(labelHead, matches, rgx))
782 {
783 nameIndex = std::stoi(matches[1]);
784
785 // Decrement to preserve alignment, because hwmon
786 // human-readable filenames and labels use 1-based
787 // numbering, but the "Name", "Name1", "Name2", etc. naming
788 // convention (the psuNames vector) uses 0-based numbering.
789 if (nameIndex > 0)
790 {
791 --nameIndex;
792 }
793 }
794 else
795 {
796 nameIndex = 0;
797 }
798
799 if (psuNames.size() <= nameIndex)
800 {
801 std::cerr << "Could not pair " << labelHead
802 << " with a Name field\n";
803 continue;
804 }
805
806 psuNameFromIndex = psuNames[nameIndex];
807
Ed Tanous8a57ec02020-10-09 12:46:52 -0700808 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700809 {
810 std::cerr << "Sensor label head " << labelHead
811 << " paired with " << psuNameFromIndex
812 << " at index " << nameIndex << "\n";
813 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700814 }
815
Cheng C Yang58b2b532019-05-31 00:19:45 +0800816 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
817
Josh Lehan74d9bd92019-10-31 08:51:58 -0700818 // Similarly, if sensor scaling factor is being customized,
819 // then the below power-of-10 constraint becomes unnecessary,
820 // as config should be able to specify an arbitrary divisor.
821 unsigned int factor = psuProperty->sensorScaleFactor;
822 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700823 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700824 // Preserve existing usage of hardcoded labelMatch table below
825 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700826
Josh Lehan74d9bd92019-10-31 08:51:58 -0700827 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700828 char firstChar =
829 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700830 std::string strScaleFactor =
831 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
832
833 // Preserve existing configs by accepting earlier syntax,
834 // example CurrScaleFactor, PowerScaleFactor, ...
835 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
836 if (findScaleFactor != baseConfig->second.end())
837 {
838 factor = std::visit(VariantToIntVisitor(),
839 findScaleFactor->second);
840 }
841
Ed Tanous8a57ec02020-10-09 12:46:52 -0700842 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 {
844 std::cerr << "Sensor scaling factor " << factor
845 << " string " << strScaleFactor << "\n";
846 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700847 }
848
Vijay Khemka996bad12019-05-28 15:15:16 -0700849 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530850 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
851 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800852 {
James Feist17ab6e02019-06-25 12:28:13 -0700853 std::cerr << "error populating thresholds for "
854 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800855 }
856
Zev Weiss6b6891c2021-04-22 02:46:21 -0500857 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
858 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800859 {
Jason Ling5747fab2019-10-02 16:46:23 -0700860 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700861 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800862 continue;
863 }
864
Ed Tanous8a57ec02020-10-09 12:46:52 -0700865 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700866 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700867 std::cerr << "Sensor properties: Name \""
868 << psuProperty->labelTypeName << "\" Scale "
869 << psuProperty->sensorScaleFactor << " Min "
870 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800871 << psuProperty->maxReading << " Offset "
872 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700873 }
874
875 std::string sensorName = psuProperty->labelTypeName;
876 if (customizedName)
877 {
878 if (sensorName.empty())
879 {
880 // Allow selective disabling of an individual sensor,
881 // by customizing its name to an empty string.
882 std::cerr << "Sensor disabled, empty string\n";
883 continue;
884 }
885 }
886 else
887 {
888 // Sensor name not customized, do prefix/suffix composition,
889 // preserving default behavior by using psuNameFromIndex.
890 sensorName =
891 psuNameFromIndex + " " + psuProperty->labelTypeName;
892 }
893
Ed Tanous8a57ec02020-10-09 12:46:52 -0700894 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700895 {
896 std::cerr << "Sensor name \"" << sensorName << "\" path \""
897 << sensorPathStr << "\" type \"" << sensorType
898 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700899 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800900 // destruct existing one first if already created
901 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800902 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800903 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800904 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000905 readState, findSensorUnit->second, factor,
906 psuProperty->maxReading, psuProperty->minReading,
907 psuProperty->sensorOffset, labelHead, thresholdConfSize,
908 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800909 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700910 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700911 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700912 {
913 std::cerr << "Created " << numCreated << " sensors so far\n";
914 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800915 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800916
917 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800918 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800919 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000920 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
921 *psuName, readState,
922 eventPathList, groupEventPathList,
923 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800924 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700925
Ed Tanous8a57ec02020-10-09 12:46:52 -0700926 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700927 {
928 std::cerr << "Created total of " << numCreated << " sensors\n";
929 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800930 return;
931}
932
Zhikui Ren23c96e72020-11-05 22:32:28 -0800933void createSensors(
934 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
935 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
936 const std::shared_ptr<boost::container::flat_set<std::string>>&
937 sensorsChanged)
938{
939 auto getter = std::make_shared<GetSensorConfiguration>(
940 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
941 const ManagedObjectType& sensorConfigs) {
942 createSensorsCallback(io, objectServer, dbusConnection,
943 sensorConfigs, sensorsChanged);
944 });
945 getter->getConfiguration(
946 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
947}
948
Cheng C Yang916360b2019-05-07 18:47:16 +0800949void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800950{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500951 sensorTable = {{"power", sensor_paths::unitWatts},
952 {"curr", sensor_paths::unitAmperes},
953 {"temp", sensor_paths::unitDegreesC},
954 {"in", sensor_paths::unitVolts},
955 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800956
Jeff Line41d52f2021-04-07 19:38:51 +0800957 labelMatch = {
958 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
959 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
960 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
961 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
962 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
963 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
964 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
965 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
966 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
999 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
1001 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1003 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1004 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1007 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1008 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1009 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1013 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1014 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1015 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
1016 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1017 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1018 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1019 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1020 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1021 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1022 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1023 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1024 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1025 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001026
1027 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001028
1029 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1030 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1031
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001032 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1033 {"Failure", {"in2_alarm"}},
1034 {"ACLost", {"in1_beep"}},
1035 {"ConfigureError", {"in1_fault"}}};
1036
1037 groupEventMatch = {{"FanFault",
1038 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1039 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001040}
1041
James Feistb6c0b912019-07-09 12:21:44 -07001042int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001043{
1044 boost::asio::io_service io;
1045 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1046
1047 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1048 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001049 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001050 auto sensorsChanged =
1051 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001052
Cheng C Yang916360b2019-05-07 18:47:16 +08001053 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001054
Zhikui Ren23c96e72020-11-05 22:32:28 -08001055 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001056 boost::asio::deadline_timer filterTimer(io);
1057 std::function<void(sdbusplus::message::message&)> eventHandler =
1058 [&](sdbusplus::message::message& message) {
1059 if (message.is_method_error())
1060 {
1061 std::cerr << "callback method error\n";
1062 return;
1063 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001064 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001065 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001066 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1067 if (ec == boost::asio::error::operation_aborted)
1068 {
1069 return;
1070 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001071 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001072 {
1073 std::cerr << "timer error\n";
1074 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001075 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001076 });
1077 };
1078
1079 for (const char* type : sensorTypes)
1080 {
1081 auto match = std::make_unique<sdbusplus::bus::match::match>(
1082 static_cast<sdbusplus::bus::bus&>(*systemBus),
1083 "type='signal',member='PropertiesChanged',path_namespace='" +
1084 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1085 eventHandler);
1086 matches.emplace_back(std::move(match));
1087 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001088
1089 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001090 io.run();
1091}