blob: 3f5648ef58ed7c05d95a4cc3071b41255d590fa7 [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "PSUEvent.hpp"
18#include "PSUSensor.hpp"
19#include "Utils.hpp"
20
Cheng C Yang209ec562019-03-12 16:37:44 +080021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080024#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <sdbusplus/bus/match.hpp>
28
29#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070030#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070031#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080032#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080034#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080035#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <string>
37#include <utility>
38#include <variant>
39#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080040
Josh Lehan49cfba92019-10-08 16:50:42 -070041static constexpr bool DEBUG = false;
42
Josh Lehan62084c82020-06-22 15:27:12 -070043static constexpr std::array<const char*, 13> sensorTypes = {
44 "xyz.openbmc_project.Configuration.ADM1272",
45 "xyz.openbmc_project.Configuration.ADM1278",
46 "xyz.openbmc_project.Configuration.INA219",
Devjit Gopalpurde65ef72019-10-30 04:47:35 -070047 "xyz.openbmc_project.Configuration.INA230",
Alex Qiu6690d8f2020-01-22 17:56:33 -080048 "xyz.openbmc_project.Configuration.ISL68137",
Alex Qiuf5709b42020-01-29 13:29:50 -080049 "xyz.openbmc_project.Configuration.MAX16601",
Alex Qiu6690d8f2020-01-22 17:56:33 -080050 "xyz.openbmc_project.Configuration.MAX20730",
51 "xyz.openbmc_project.Configuration.MAX20734",
52 "xyz.openbmc_project.Configuration.MAX20796",
53 "xyz.openbmc_project.Configuration.MAX34451",
Josh Lehan62084c82020-06-22 15:27:12 -070054 "xyz.openbmc_project.Configuration.pmbus",
55 "xyz.openbmc_project.Configuration.PXE1610",
56 "xyz.openbmc_project.Configuration.RAA228228"};
Cheng C Yang209ec562019-03-12 16:37:44 +080057
Alex Qiu6690d8f2020-01-22 17:56:33 -080058static std::vector<std::string> pmbusNames = {
Josh Lehan62084c82020-06-22 15:27:12 -070059 "adm1272", "adm1278", "ina219", "ina230", "isl68137",
Manikandan Elumalai68b14e62020-06-03 17:09:52 +053060 "max16601", "max20730", "max20734", "max20796", "max34451",
61 "pmbus", "pxe1610", "raa228228"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070062
Cheng C Yang209ec562019-03-12 16:37:44 +080063namespace fs = std::filesystem;
64
Yong Libf8b1da2020-04-15 16:32:50 +080065static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080066 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080067static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
68 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080069static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
70 pwmSensors;
71static boost::container::flat_map<std::string, std::string> sensorTable;
72static boost::container::flat_map<std::string, PSUProperty> labelMatch;
73static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080074static boost::container::flat_map<std::string, std::vector<std::string>>
75 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080076static boost::container::flat_map<
77 std::string,
78 boost::container::flat_map<std::string, std::vector<std::string>>>
79 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080080static boost::container::flat_map<std::string, std::vector<std::string>>
81 limitEventMatch;
82
Josh Lehan74d9bd92019-10-31 08:51:58 -070083static std::vector<PSUProperty> psuProperties;
84
Cheng C Yang58b2b532019-05-31 00:19:45 +080085// Function CheckEvent will check each attribute from eventMatch table in the
86// sysfs. If the attributes exists in sysfs, then store the complete path
87// of the attribute into eventPathList.
88void checkEvent(
89 const std::string& directory,
90 const boost::container::flat_map<std::string, std::vector<std::string>>&
91 eventMatch,
92 boost::container::flat_map<std::string, std::vector<std::string>>&
93 eventPathList)
94{
95 for (const auto& match : eventMatch)
96 {
97 const std::vector<std::string>& eventAttrs = match.second;
98 const std::string& eventName = match.first;
99 for (const auto& eventAttr : eventAttrs)
100 {
101 auto eventPath = directory + "/" + eventAttr;
102
103 std::ifstream eventFile(eventPath);
104 if (!eventFile.good())
105 {
106 continue;
107 }
108
109 eventPathList[eventName].push_back(eventPath);
110 }
111 }
112}
113
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800114// Check Group Events which contains more than one targets in each combine
115// events.
116void checkGroupEvent(
117 const std::string& directory,
118 const boost::container::flat_map<
119 std::string,
120 boost::container::flat_map<std::string, std::vector<std::string>>>&
121 groupEventMatch,
122 boost::container::flat_map<
123 std::string,
124 boost::container::flat_map<std::string, std::vector<std::string>>>&
125 groupEventPathList)
126{
127 for (const auto& match : groupEventMatch)
128 {
129 const std::string& groupEventName = match.first;
130 const boost::container::flat_map<std::string, std::vector<std::string>>
131 events = match.second;
132 boost::container::flat_map<std::string, std::vector<std::string>>
133 pathList;
134 for (const auto& match : events)
135 {
136 const std::string& eventName = match.first;
137 const std::vector<std::string>& eventAttrs = match.second;
138 for (const auto& eventAttr : eventAttrs)
139 {
140 auto eventPath = directory + "/" + eventAttr;
141 std::ifstream eventFile(eventPath);
142 if (!eventFile.good())
143 {
144 continue;
145 }
146
147 pathList[eventName].push_back(eventPath);
148 }
149 }
150 groupEventPathList[groupEventName] = pathList;
151 }
152}
153
Cheng C Yang58b2b532019-05-31 00:19:45 +0800154// Function checkEventLimits will check all the psu related xxx_input attributes
155// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
156// xxx_min_alarm exist, then store the existing paths of the alarm attributes
157// to eventPathList.
158void checkEventLimits(
159 const std::string& sensorPathStr,
160 const boost::container::flat_map<std::string, std::vector<std::string>>&
161 limitEventMatch,
162 boost::container::flat_map<std::string, std::vector<std::string>>&
163 eventPathList)
164{
165 for (const auto& limitMatch : limitEventMatch)
166 {
167 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
168 const std::string& eventName = limitMatch.first;
169 for (const auto& limitEventAttr : limitEventAttrs)
170 {
171 auto limitEventPath =
172 boost::replace_all_copy(sensorPathStr, "input", limitEventAttr);
173 std::ifstream eventFile(limitEventPath);
174 if (!eventFile.good())
175 {
176 continue;
177 }
178 eventPathList[eventName].push_back(limitEventPath);
179 }
180 }
181}
Cheng C Yang916360b2019-05-07 18:47:16 +0800182
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530183static void
184 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
185 const std::string& interfacePath,
186 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
187 sdbusplus::asio::object_server& objectServer,
188 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800189{
190 for (const auto& pwmName : pwmTable)
191 {
192 if (pwmName.first != labelHead)
193 {
194 continue;
195 }
196
197 const std::string& sensorPathStr = sensorPath.string();
198 const std::string& pwmPathStr =
199 boost::replace_all_copy(sensorPathStr, "input", "target");
200 std::ifstream pwmFile(pwmPathStr);
201 if (!pwmFile.good())
202 {
203 continue;
204 }
205
206 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
207 if (findPWMSensor != pwmSensors.end())
208 {
209 continue;
210 }
211
212 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530213 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
214 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800215 }
216}
217
218void createSensors(boost::asio::io_service& io,
219 sdbusplus::asio::object_server& objectServer,
220 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Cheng C Yang209ec562019-03-12 16:37:44 +0800221{
222
223 ManagedObjectType sensorConfigs;
Josh Lehan49cfba92019-10-08 16:50:42 -0700224 int numCreated = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800225 bool useCache = false;
226
Cheng C Yang58b2b532019-05-31 00:19:45 +0800227 // TODO may need only modify the ones that need to be changed.
228 sensors.clear();
Cheng C Yang209ec562019-03-12 16:37:44 +0800229 for (const char* type : sensorTypes)
230 {
231 if (!getSensorConfiguration(type, dbusConnection, sensorConfigs,
232 useCache))
233 {
234 std::cerr << "error get sensor config from entity manager\n";
235 return;
236 }
237 useCache = true;
238 }
239
240 std::vector<fs::path> pmbusPaths;
241 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
242 {
243 std::cerr << "No PSU sensors in system\n";
244 return;
245 }
246
247 boost::container::flat_set<std::string> directories;
248 for (const auto& pmbusPath : pmbusPaths)
249 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800250 boost::container::flat_map<std::string, std::vector<std::string>>
251 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800252 boost::container::flat_map<
253 std::string,
254 boost::container::flat_map<std::string, std::vector<std::string>>>
255 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800256
257 std::ifstream nameFile(pmbusPath);
258 if (!nameFile.good())
259 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700260 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800261 continue;
262 }
263
264 std::string pmbusName;
265 std::getline(nameFile, pmbusName);
266 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700267
268 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
269 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800270 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700271 // To avoid this error message, add your driver name to
272 // the pmbusNames vector at the top of this file.
273 std::cerr << "Driver name " << pmbusName
274 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800275 continue;
276 }
277
278 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800279 auto directory = pmbusPath.parent_path();
280
281 auto ret = directories.insert(directory.string());
282 if (!ret.second)
283 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700284 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800285 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800286 }
287
James Feistb6c0b912019-07-09 12:21:44 -0700288 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800289 std::string deviceName = fs::canonical(device).stem();
290 auto findHyphen = deviceName.find("-");
291 if (findHyphen == std::string::npos)
292 {
293 std::cerr << "found bad device" << deviceName << "\n";
294 continue;
295 }
296 std::string busStr = deviceName.substr(0, findHyphen);
297 std::string addrStr = deviceName.substr(findHyphen + 1);
298
299 size_t bus = 0;
300 size_t addr = 0;
301
302 try
303 {
304 bus = std::stoi(busStr);
305 addr = std::stoi(addrStr, 0, 16);
306 }
James Feistb6c0b912019-07-09 12:21:44 -0700307 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800308 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700309 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
310 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800311 continue;
312 }
313
Cheng C Yang209ec562019-03-12 16:37:44 +0800314 const std::pair<std::string, boost::container::flat_map<
315 std::string, BasicVariantType>>*
316 baseConfig = nullptr;
317 const SensorData* sensorData = nullptr;
318 const std::string* interfacePath = nullptr;
319 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800320 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800321
322 for (const std::pair<sdbusplus::message::object_path, SensorData>&
323 sensor : sensorConfigs)
324 {
325 sensorData = &(sensor.second);
326 for (const char* type : sensorTypes)
327 {
328 auto sensorBase = sensorData->find(type);
329 if (sensorBase != sensorData->end())
330 {
331 baseConfig = &(*sensorBase);
332 sensorType = type;
333 break;
334 }
335 }
336 if (baseConfig == nullptr)
337 {
338 std::cerr << "error finding base configuration for "
339 << deviceName << "\n";
340 continue;
341 }
342
343 auto configBus = baseConfig->second.find("Bus");
344 auto configAddress = baseConfig->second.find("Address");
345
346 if (configBus == baseConfig->second.end() ||
347 configAddress == baseConfig->second.end())
348 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800349 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800350 continue;
351 }
352
Cheng C Yang58b2b532019-05-31 00:19:45 +0800353 const uint64_t* confBus;
354 const uint64_t* confAddr;
355 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
356 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
357 {
358 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700359 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800360 continue;
361 }
362
363 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700365 std::cerr << "Configuration skipping " << *confBus << "-"
366 << *confAddr << " because not " << bus << "-" << addr
367 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 continue;
369 }
370
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800371 std::vector<thresholds::Threshold> confThresholds;
372 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
373 {
374 std::cerr << "error populating totoal thresholds\n";
375 }
376 thresholdConfSize = confThresholds.size();
377
Cheng C Yang209ec562019-03-12 16:37:44 +0800378 interfacePath = &(sensor.first.str);
379 break;
380 }
381 if (interfacePath == nullptr)
382 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700383 // To avoid this error message, add your export map entry,
384 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800385 std::cerr << "failed to find match for " << deviceName << "\n";
386 continue;
387 }
388
Cheng C Yange50345b2019-04-02 17:26:15 +0800389 auto findPSUName = baseConfig->second.find("Name");
390 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800391 {
392 std::cerr << "could not determine configuration name for "
393 << deviceName << "\n";
394 continue;
395 }
396
Cheng C Yang58b2b532019-05-31 00:19:45 +0800397 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
398 {
399 std::cerr << "Cannot find psu name, invalid configuration\n";
400 continue;
401 }
402 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800403 checkGroupEvent(directory.string(), groupEventMatch,
404 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800405
Vijay Khemka996bad12019-05-28 15:15:16 -0700406 /* Check if there are more sensors in the same interface */
407 int i = 1;
408 std::vector<std::string> psuNames;
409 do
410 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700411 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700412 psuNames.push_back(std::get<std::string>(findPSUName->second));
413 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
414 } while (findPSUName != baseConfig->second.end());
415
Cheng C Yange50345b2019-04-02 17:26:15 +0800416 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700417 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800418 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800419 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800420 continue;
421 }
422
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530423 /* read max value in sysfs for in, curr, power, temp, ... */
424 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
425 {
426 if constexpr (DEBUG)
427 {
428 std::cerr << "No max name in PSU \n";
429 }
430 }
431
Vijay Khemka996bad12019-05-28 15:15:16 -0700432 /* Find array of labels to be exposed if it is defined in config */
433 std::vector<std::string> findLabels;
434 auto findLabelObj = baseConfig->second.find("Labels");
435 if (findLabelObj != baseConfig->second.end())
436 {
437 findLabels =
438 std::get<std::vector<std::string>>(findLabelObj->second);
439 }
440
Jason Ling5747fab2019-10-02 16:46:23 -0700441 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
442 std::smatch matches;
443
Cheng C Yange50345b2019-04-02 17:26:15 +0800444 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800445 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530446 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800447 std::string labelHead;
448 std::string sensorPathStr = sensorPath.string();
449 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700450 std::string sensorNameSubStr{""};
451 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
452 {
Josh Lehan06494452019-10-31 09:49:16 -0700453 // hwmon *_input filename without number:
454 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700455 sensorNameSubStr = matches[1];
456 }
457 else
458 {
Josh Lehan06494452019-10-31 09:49:16 -0700459 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700460 << sensorNameStr;
461 continue;
462 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800463
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530464 std::string labelPath;
465
466 /* find and differentiate _max and _input to replace "label" */
467 int pos = sensorPathStr.find("_");
468 if (pos != std::string::npos)
469 {
470
471 std::string sensorPathStrMax = sensorPathStr.substr(pos);
472 if (sensorPathStrMax.compare("_max") == 0)
473 {
474 labelPath =
475 boost::replace_all_copy(sensorPathStr, "max", "label");
476 maxLabel = true;
477 }
478 else
479 {
480 labelPath = boost::replace_all_copy(sensorPathStr, "input",
481 "label");
482 maxLabel = false;
483 }
484 }
485 else
486 {
487 continue;
488 }
489
Cheng C Yangecba9de2019-09-12 23:41:50 +0800490 std::ifstream labelFile(labelPath);
491 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800492 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800493 if constexpr (DEBUG)
494 {
495 std::cerr << "Input file " << sensorPath
496 << " has no corresponding label file\n";
497 }
Josh Lehan06494452019-10-31 09:49:16 -0700498 // hwmon *_input filename with number:
499 // temp1, temp2, temp3, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800500 labelHead = sensorNameStr.substr(0, sensorNameStr.find("_"));
Cheng C Yang209ec562019-03-12 16:37:44 +0800501 }
502 else
503 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800504 std::string label;
505 std::getline(labelFile, label);
506 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800507 auto findSensor = sensors.find(label);
508 if (findSensor != sensors.end())
509 {
510 continue;
511 }
512
Josh Lehan06494452019-10-31 09:49:16 -0700513 // hwmon corresponding *_label file contents:
514 // vin1, vout1, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800515 labelHead = label.substr(0, label.find(" "));
516 }
517
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530518 /* append "max" for labelMatch */
519 if (maxLabel)
520 {
521 labelHead = "max" + labelHead;
522 }
523
Josh Lehan74d9bd92019-10-31 08:51:58 -0700524 if constexpr (DEBUG)
525 {
526 std::cerr << "Sensor type=\"" << sensorNameSubStr
527 << "\" label=\"" << labelHead << "\"\n";
528 }
529
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530530 checkPWMSensor(sensorPath, labelHead, *interfacePath,
531 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800532
Vijay Khemka996bad12019-05-28 15:15:16 -0700533 if (!findLabels.empty())
534 {
535 /* Check if this labelHead is enabled in config file */
536 if (std::find(findLabels.begin(), findLabels.end(),
537 labelHead) == findLabels.end())
538 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800539 if constexpr (DEBUG)
540 {
541 std::cerr << "could not find " << labelHead
542 << " in the Labels list\n";
543 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700544 continue;
545 }
546 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800547
Cheng C Yange50345b2019-04-02 17:26:15 +0800548 auto findProperty = labelMatch.find(labelHead);
549 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800550 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700551 if constexpr (DEBUG)
552 {
553 std::cerr << "Could not find matching default property for "
554 << labelHead << "\n";
555 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800556 continue;
557 }
558
Josh Lehan74d9bd92019-10-31 08:51:58 -0700559 // Protect the hardcoded labelMatch list from changes,
560 // by making a copy and modifying that instead.
561 // Avoid bleedthrough of one device's customizations to
562 // the next device, as each should be independently customizable.
563 psuProperties.push_back(findProperty->second);
564 auto psuProperty = psuProperties.rbegin();
565
566 // Use label head as prefix for reading from config file,
567 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
568 std::string keyName = labelHead + "_Name";
569 std::string keyScale = labelHead + "_Scale";
570 std::string keyMin = labelHead + "_Min";
571 std::string keyMax = labelHead + "_Max";
572
573 bool customizedName = false;
574 auto findCustomName = baseConfig->second.find(keyName);
575 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700576 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700577 try
578 {
579 psuProperty->labelTypeName = std::visit(
580 VariantToStringVisitor(), findCustomName->second);
581 }
582 catch (std::invalid_argument&)
583 {
584 std::cerr << "Unable to parse " << keyName << "\n";
585 continue;
586 }
587
588 // All strings are valid, including empty string
589 customizedName = true;
590 }
591
592 bool customizedScale = false;
593 auto findCustomScale = baseConfig->second.find(keyScale);
594 if (findCustomScale != baseConfig->second.end())
595 {
596 try
597 {
598 psuProperty->sensorScaleFactor = std::visit(
599 VariantToUnsignedIntVisitor(), findCustomScale->second);
600 }
601 catch (std::invalid_argument&)
602 {
603 std::cerr << "Unable to parse " << keyScale << "\n";
604 continue;
605 }
606
607 // Avoid later division by zero
608 if (psuProperty->sensorScaleFactor > 0)
609 {
610 customizedScale = true;
611 }
612 else
613 {
614 std::cerr << "Unable to accept " << keyScale << "\n";
615 continue;
616 }
617 }
618
619 auto findCustomMin = baseConfig->second.find(keyMin);
620 if (findCustomMin != baseConfig->second.end())
621 {
622 try
623 {
624 psuProperty->minReading = std::visit(
625 VariantToDoubleVisitor(), findCustomMin->second);
626 }
627 catch (std::invalid_argument&)
628 {
629 std::cerr << "Unable to parse " << keyMin << "\n";
630 continue;
631 }
632 }
633
634 auto findCustomMax = baseConfig->second.find(keyMax);
635 if (findCustomMax != baseConfig->second.end())
636 {
637 try
638 {
639 psuProperty->maxReading = std::visit(
640 VariantToDoubleVisitor(), findCustomMax->second);
641 }
642 catch (std::invalid_argument&)
643 {
644 std::cerr << "Unable to parse " << keyMax << "\n";
645 continue;
646 }
647 }
648
649 if (!(psuProperty->minReading < psuProperty->maxReading))
650 {
651 std::cerr << "Min must be less than Max\n";
652 continue;
653 }
654
655 // If the sensor name is being customized by config file,
656 // then prefix/suffix composition becomes not necessary,
657 // and in fact not wanted, because it gets in the way.
658 std::string psuNameFromIndex;
659 if (!customizedName)
660 {
661 /* Find out sensor name index for this label */
662 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500663 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700664 if (std::regex_search(labelHead, matches, rgx))
665 {
666 nameIndex = std::stoi(matches[1]);
667
668 // Decrement to preserve alignment, because hwmon
669 // human-readable filenames and labels use 1-based
670 // numbering, but the "Name", "Name1", "Name2", etc. naming
671 // convention (the psuNames vector) uses 0-based numbering.
672 if (nameIndex > 0)
673 {
674 --nameIndex;
675 }
676 }
677 else
678 {
679 nameIndex = 0;
680 }
681
682 if (psuNames.size() <= nameIndex)
683 {
684 std::cerr << "Could not pair " << labelHead
685 << " with a Name field\n";
686 continue;
687 }
688
689 psuNameFromIndex = psuNames[nameIndex];
690
691 if constexpr (DEBUG)
692 {
693 std::cerr << "Sensor label head " << labelHead
694 << " paired with " << psuNameFromIndex
695 << " at index " << nameIndex << "\n";
696 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700697 }
698
Cheng C Yang58b2b532019-05-31 00:19:45 +0800699 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
700
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701 // Similarly, if sensor scaling factor is being customized,
702 // then the below power-of-10 constraint becomes unnecessary,
703 // as config should be able to specify an arbitrary divisor.
704 unsigned int factor = psuProperty->sensorScaleFactor;
705 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700706 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700707 // Preserve existing usage of hardcoded labelMatch table below
708 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700709
Josh Lehan74d9bd92019-10-31 08:51:58 -0700710 /* Change first char of substring to uppercase */
711 char firstChar = sensorNameSubStr[0] - 0x20;
712 std::string strScaleFactor =
713 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
714
715 // Preserve existing configs by accepting earlier syntax,
716 // example CurrScaleFactor, PowerScaleFactor, ...
717 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
718 if (findScaleFactor != baseConfig->second.end())
719 {
720 factor = std::visit(VariantToIntVisitor(),
721 findScaleFactor->second);
722 }
723
724 if constexpr (DEBUG)
725 {
726 std::cerr << "Sensor scaling factor " << factor
727 << " string " << strScaleFactor << "\n";
728 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700729 }
730
Vijay Khemka996bad12019-05-28 15:15:16 -0700731 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530732 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
733 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800734 {
James Feist17ab6e02019-06-25 12:28:13 -0700735 std::cerr << "error populating thresholds for "
736 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800737 }
738
739 auto findSensorType = sensorTable.find(sensorNameSubStr);
740 if (findSensorType == sensorTable.end())
741 {
Jason Ling5747fab2019-10-02 16:46:23 -0700742 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700743 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800744 continue;
745 }
746
Josh Lehan49cfba92019-10-08 16:50:42 -0700747 if constexpr (DEBUG)
748 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700749 std::cerr << "Sensor properties: Name \""
750 << psuProperty->labelTypeName << "\" Scale "
751 << psuProperty->sensorScaleFactor << " Min "
752 << psuProperty->minReading << " Max "
753 << psuProperty->maxReading << "\n";
754 }
755
756 std::string sensorName = psuProperty->labelTypeName;
757 if (customizedName)
758 {
759 if (sensorName.empty())
760 {
761 // Allow selective disabling of an individual sensor,
762 // by customizing its name to an empty string.
763 std::cerr << "Sensor disabled, empty string\n";
764 continue;
765 }
766 }
767 else
768 {
769 // Sensor name not customized, do prefix/suffix composition,
770 // preserving default behavior by using psuNameFromIndex.
771 sensorName =
772 psuNameFromIndex + " " + psuProperty->labelTypeName;
773 }
774
775 if constexpr (DEBUG)
776 {
777 std::cerr << "Sensor name \"" << sensorName << "\" path \""
778 << sensorPathStr << "\" type \"" << sensorType
779 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700780 }
781
Yong Libf8b1da2020-04-15 16:32:50 +0800782 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800783 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800784 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700785 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800786 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800787 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700788 ++numCreated;
789 if constexpr (DEBUG)
790 {
791 std::cerr << "Created " << numCreated << " sensors so far\n";
792 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800793 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800794
795 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800796 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800797 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800798 std::make_unique<PSUCombineEvent>(
799 objectServer, dbusConnection, io, *psuName, eventPathList,
800 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800801 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700802
803 if constexpr (DEBUG)
804 {
805 std::cerr << "Created total of " << numCreated << " sensors\n";
806 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800807 return;
808}
809
Cheng C Yang916360b2019-05-07 18:47:16 +0800810void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800811{
Cheng C Yange50345b2019-04-02 17:26:15 +0800812 sensorTable = {{"power", "power/"},
813 {"curr", "current/"},
814 {"temp", "temperature/"},
815 {"in", "voltage/"},
816 {"fan", "fan_tach/"}};
817
818 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
819 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700820 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
821 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700822 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530823 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800824 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530825 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700826 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
827 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
828 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700829 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
830 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
831 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
832 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
833 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
834 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
835 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
836 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
837 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
838 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
839 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
840 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
841 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700842 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800843 {"iin", PSUProperty("Input Current", 20, 0, 3)},
844 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700845 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
846 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800847 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
848 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
849 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
850 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
851 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
852 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
853 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
854 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
855 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
856 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
857 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700858 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530859 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800860 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700861 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
862 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700863 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
864 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800865 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530866 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800867 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
868 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800869
870 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800871
872 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
873 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
874
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800875 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
876 {"Failure", {"in2_alarm"}},
877 {"ACLost", {"in1_beep"}},
878 {"ConfigureError", {"in1_fault"}}};
879
880 groupEventMatch = {{"FanFault",
881 {{"fan1", {"fan1_alarm", "fan1_fault"}},
882 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800883}
884
James Feistb6c0b912019-07-09 12:21:44 -0700885int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800886{
887 boost::asio::io_service io;
888 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
889
890 systemBus->request_name("xyz.openbmc_project.PSUSensor");
891 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800892 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Cheng C Yang209ec562019-03-12 16:37:44 +0800893
Cheng C Yang916360b2019-05-07 18:47:16 +0800894 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800895
Cheng C Yang916360b2019-05-07 18:47:16 +0800896 io.post([&]() { createSensors(io, objectServer, systemBus); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800897 boost::asio::deadline_timer filterTimer(io);
898 std::function<void(sdbusplus::message::message&)> eventHandler =
899 [&](sdbusplus::message::message& message) {
900 if (message.is_method_error())
901 {
902 std::cerr << "callback method error\n";
903 return;
904 }
Cheng C Yanga97f1342020-02-11 15:10:41 +0800905 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800906 filterTimer.async_wait([&](const boost::system::error_code& ec) {
907 if (ec == boost::asio::error::operation_aborted)
908 {
909 return;
910 }
911 else if (ec)
912 {
913 std::cerr << "timer error\n";
914 }
Cheng C Yang916360b2019-05-07 18:47:16 +0800915 createSensors(io, objectServer, systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800916 });
917 };
918
919 for (const char* type : sensorTypes)
920 {
921 auto match = std::make_unique<sdbusplus::bus::match::match>(
922 static_cast<sdbusplus::bus::bus&>(*systemBus),
923 "type='signal',member='PropertiesChanged',path_namespace='" +
924 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
925 eventHandler);
926 matches.emplace_back(std::move(match));
927 }
928 io.run();
929}