blob: 1a1cb3ce847551c9d38a5dfc2e209df502312ce1 [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
Jason Lingdfad1ff2020-07-29 15:43:11 -070043static constexpr std::array<const char*, 14> sensorTypes = {
Josh Lehan62084c82020-06-22 15:27:12 -070044 "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",
Jason Lingdfad1ff2020-07-29 15:43:11 -070049 "xyz.openbmc_project.Configuration.ISL68220",
Alex Qiuf5709b42020-01-29 13:29:50 -080050 "xyz.openbmc_project.Configuration.MAX16601",
Alex Qiu6690d8f2020-01-22 17:56:33 -080051 "xyz.openbmc_project.Configuration.MAX20730",
52 "xyz.openbmc_project.Configuration.MAX20734",
53 "xyz.openbmc_project.Configuration.MAX20796",
54 "xyz.openbmc_project.Configuration.MAX34451",
Josh Lehan62084c82020-06-22 15:27:12 -070055 "xyz.openbmc_project.Configuration.pmbus",
56 "xyz.openbmc_project.Configuration.PXE1610",
57 "xyz.openbmc_project.Configuration.RAA228228"};
Cheng C Yang209ec562019-03-12 16:37:44 +080058
Alex Qiu6690d8f2020-01-22 17:56:33 -080059static std::vector<std::string> pmbusNames = {
Josh Lehan62084c82020-06-22 15:27:12 -070060 "adm1272", "adm1278", "ina219", "ina230", "isl68137",
Jason Lingdfad1ff2020-07-29 15:43:11 -070061 "isl68220", "max16601", "max20730", "max20734", "max20796",
62 "max34451", "pmbus", "pxe1610", "raa228228"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070063
Cheng C Yang209ec562019-03-12 16:37:44 +080064namespace fs = std::filesystem;
65
Yong Libf8b1da2020-04-15 16:32:50 +080066static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080067 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080068static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
69 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080070static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
71 pwmSensors;
72static boost::container::flat_map<std::string, std::string> sensorTable;
73static boost::container::flat_map<std::string, PSUProperty> labelMatch;
74static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080075static boost::container::flat_map<std::string, std::vector<std::string>>
76 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080077static boost::container::flat_map<
78 std::string,
79 boost::container::flat_map<std::string, std::vector<std::string>>>
80 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080081static boost::container::flat_map<std::string, std::vector<std::string>>
82 limitEventMatch;
83
Josh Lehan74d9bd92019-10-31 08:51:58 -070084static std::vector<PSUProperty> psuProperties;
85
Cheng C Yang58b2b532019-05-31 00:19:45 +080086// Function CheckEvent will check each attribute from eventMatch table in the
87// sysfs. If the attributes exists in sysfs, then store the complete path
88// of the attribute into eventPathList.
89void checkEvent(
90 const std::string& directory,
91 const boost::container::flat_map<std::string, std::vector<std::string>>&
92 eventMatch,
93 boost::container::flat_map<std::string, std::vector<std::string>>&
94 eventPathList)
95{
96 for (const auto& match : eventMatch)
97 {
98 const std::vector<std::string>& eventAttrs = match.second;
99 const std::string& eventName = match.first;
100 for (const auto& eventAttr : eventAttrs)
101 {
102 auto eventPath = directory + "/" + eventAttr;
103
104 std::ifstream eventFile(eventPath);
105 if (!eventFile.good())
106 {
107 continue;
108 }
109
110 eventPathList[eventName].push_back(eventPath);
111 }
112 }
113}
114
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800115// Check Group Events which contains more than one targets in each combine
116// events.
117void checkGroupEvent(
118 const std::string& directory,
119 const boost::container::flat_map<
120 std::string,
121 boost::container::flat_map<std::string, std::vector<std::string>>>&
122 groupEventMatch,
123 boost::container::flat_map<
124 std::string,
125 boost::container::flat_map<std::string, std::vector<std::string>>>&
126 groupEventPathList)
127{
128 for (const auto& match : groupEventMatch)
129 {
130 const std::string& groupEventName = match.first;
131 const boost::container::flat_map<std::string, std::vector<std::string>>
132 events = match.second;
133 boost::container::flat_map<std::string, std::vector<std::string>>
134 pathList;
135 for (const auto& match : events)
136 {
137 const std::string& eventName = match.first;
138 const std::vector<std::string>& eventAttrs = match.second;
139 for (const auto& eventAttr : eventAttrs)
140 {
141 auto eventPath = directory + "/" + eventAttr;
142 std::ifstream eventFile(eventPath);
143 if (!eventFile.good())
144 {
145 continue;
146 }
147
148 pathList[eventName].push_back(eventPath);
149 }
150 }
151 groupEventPathList[groupEventName] = pathList;
152 }
153}
154
Cheng C Yang58b2b532019-05-31 00:19:45 +0800155// Function checkEventLimits will check all the psu related xxx_input attributes
156// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
157// xxx_min_alarm exist, then store the existing paths of the alarm attributes
158// to eventPathList.
159void checkEventLimits(
160 const std::string& sensorPathStr,
161 const boost::container::flat_map<std::string, std::vector<std::string>>&
162 limitEventMatch,
163 boost::container::flat_map<std::string, std::vector<std::string>>&
164 eventPathList)
165{
166 for (const auto& limitMatch : limitEventMatch)
167 {
168 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
169 const std::string& eventName = limitMatch.first;
170 for (const auto& limitEventAttr : limitEventAttrs)
171 {
172 auto limitEventPath =
173 boost::replace_all_copy(sensorPathStr, "input", limitEventAttr);
174 std::ifstream eventFile(limitEventPath);
175 if (!eventFile.good())
176 {
177 continue;
178 }
179 eventPathList[eventName].push_back(limitEventPath);
180 }
181 }
182}
Cheng C Yang916360b2019-05-07 18:47:16 +0800183
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530184static void
185 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
186 const std::string& interfacePath,
187 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
188 sdbusplus::asio::object_server& objectServer,
189 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800190{
191 for (const auto& pwmName : pwmTable)
192 {
193 if (pwmName.first != labelHead)
194 {
195 continue;
196 }
197
198 const std::string& sensorPathStr = sensorPath.string();
199 const std::string& pwmPathStr =
200 boost::replace_all_copy(sensorPathStr, "input", "target");
201 std::ifstream pwmFile(pwmPathStr);
202 if (!pwmFile.good())
203 {
204 continue;
205 }
206
207 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
208 if (findPWMSensor != pwmSensors.end())
209 {
210 continue;
211 }
212
213 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530214 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
215 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800216 }
217}
218
219void createSensors(boost::asio::io_service& io,
220 sdbusplus::asio::object_server& objectServer,
221 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Cheng C Yang209ec562019-03-12 16:37:44 +0800222{
223
224 ManagedObjectType sensorConfigs;
Josh Lehan49cfba92019-10-08 16:50:42 -0700225 int numCreated = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800226 bool useCache = false;
227
Cheng C Yang58b2b532019-05-31 00:19:45 +0800228 // TODO may need only modify the ones that need to be changed.
229 sensors.clear();
Cheng C Yang209ec562019-03-12 16:37:44 +0800230 for (const char* type : sensorTypes)
231 {
232 if (!getSensorConfiguration(type, dbusConnection, sensorConfigs,
233 useCache))
234 {
235 std::cerr << "error get sensor config from entity manager\n";
236 return;
237 }
238 useCache = true;
239 }
240
241 std::vector<fs::path> pmbusPaths;
242 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
243 {
244 std::cerr << "No PSU sensors in system\n";
245 return;
246 }
247
248 boost::container::flat_set<std::string> directories;
249 for (const auto& pmbusPath : pmbusPaths)
250 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800251 boost::container::flat_map<std::string, std::vector<std::string>>
252 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800253 boost::container::flat_map<
254 std::string,
255 boost::container::flat_map<std::string, std::vector<std::string>>>
256 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800257
258 std::ifstream nameFile(pmbusPath);
259 if (!nameFile.good())
260 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700261 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800262 continue;
263 }
264
265 std::string pmbusName;
266 std::getline(nameFile, pmbusName);
267 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700268
269 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
270 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800271 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700272 // To avoid this error message, add your driver name to
273 // the pmbusNames vector at the top of this file.
274 std::cerr << "Driver name " << pmbusName
275 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800276 continue;
277 }
278
279 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800280 auto directory = pmbusPath.parent_path();
281
282 auto ret = directories.insert(directory.string());
283 if (!ret.second)
284 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700285 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800286 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800287 }
288
James Feistb6c0b912019-07-09 12:21:44 -0700289 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800290 std::string deviceName = fs::canonical(device).stem();
291 auto findHyphen = deviceName.find("-");
292 if (findHyphen == std::string::npos)
293 {
294 std::cerr << "found bad device" << deviceName << "\n";
295 continue;
296 }
297 std::string busStr = deviceName.substr(0, findHyphen);
298 std::string addrStr = deviceName.substr(findHyphen + 1);
299
300 size_t bus = 0;
301 size_t addr = 0;
302
303 try
304 {
305 bus = std::stoi(busStr);
306 addr = std::stoi(addrStr, 0, 16);
307 }
James Feistb6c0b912019-07-09 12:21:44 -0700308 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800309 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700310 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
311 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800312 continue;
313 }
314
Cheng C Yang209ec562019-03-12 16:37:44 +0800315 const std::pair<std::string, boost::container::flat_map<
316 std::string, BasicVariantType>>*
317 baseConfig = nullptr;
318 const SensorData* sensorData = nullptr;
319 const std::string* interfacePath = nullptr;
320 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800321 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800322
323 for (const std::pair<sdbusplus::message::object_path, SensorData>&
324 sensor : sensorConfigs)
325 {
326 sensorData = &(sensor.second);
327 for (const char* type : sensorTypes)
328 {
329 auto sensorBase = sensorData->find(type);
330 if (sensorBase != sensorData->end())
331 {
332 baseConfig = &(*sensorBase);
333 sensorType = type;
334 break;
335 }
336 }
337 if (baseConfig == nullptr)
338 {
339 std::cerr << "error finding base configuration for "
340 << deviceName << "\n";
341 continue;
342 }
343
344 auto configBus = baseConfig->second.find("Bus");
345 auto configAddress = baseConfig->second.find("Address");
346
347 if (configBus == baseConfig->second.end() ||
348 configAddress == baseConfig->second.end())
349 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800350 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800351 continue;
352 }
353
Cheng C Yang58b2b532019-05-31 00:19:45 +0800354 const uint64_t* confBus;
355 const uint64_t* confAddr;
356 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
357 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
358 {
359 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700360 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800361 continue;
362 }
363
364 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700366 std::cerr << "Configuration skipping " << *confBus << "-"
367 << *confAddr << " because not " << bus << "-" << addr
368 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800369 continue;
370 }
371
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800372 std::vector<thresholds::Threshold> confThresholds;
373 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
374 {
375 std::cerr << "error populating totoal thresholds\n";
376 }
377 thresholdConfSize = confThresholds.size();
378
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 interfacePath = &(sensor.first.str);
380 break;
381 }
382 if (interfacePath == nullptr)
383 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700384 // To avoid this error message, add your export map entry,
385 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800386 std::cerr << "failed to find match for " << deviceName << "\n";
387 continue;
388 }
389
Cheng C Yange50345b2019-04-02 17:26:15 +0800390 auto findPSUName = baseConfig->second.find("Name");
391 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800392 {
393 std::cerr << "could not determine configuration name for "
394 << deviceName << "\n";
395 continue;
396 }
397
Cheng C Yang58b2b532019-05-31 00:19:45 +0800398 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
399 {
400 std::cerr << "Cannot find psu name, invalid configuration\n";
401 continue;
402 }
403 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800404 checkGroupEvent(directory.string(), groupEventMatch,
405 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800406
Vijay Khemka996bad12019-05-28 15:15:16 -0700407 /* Check if there are more sensors in the same interface */
408 int i = 1;
409 std::vector<std::string> psuNames;
410 do
411 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700412 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700413 psuNames.push_back(std::get<std::string>(findPSUName->second));
414 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
415 } while (findPSUName != baseConfig->second.end());
416
Cheng C Yange50345b2019-04-02 17:26:15 +0800417 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700418 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800420 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 continue;
422 }
423
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530424 /* read max value in sysfs for in, curr, power, temp, ... */
425 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
426 {
427 if constexpr (DEBUG)
428 {
429 std::cerr << "No max name in PSU \n";
430 }
431 }
432
Vijay Khemka996bad12019-05-28 15:15:16 -0700433 /* Find array of labels to be exposed if it is defined in config */
434 std::vector<std::string> findLabels;
435 auto findLabelObj = baseConfig->second.find("Labels");
436 if (findLabelObj != baseConfig->second.end())
437 {
438 findLabels =
439 std::get<std::vector<std::string>>(findLabelObj->second);
440 }
441
Jason Ling5747fab2019-10-02 16:46:23 -0700442 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
443 std::smatch matches;
444
Cheng C Yange50345b2019-04-02 17:26:15 +0800445 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800446 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530447 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800448 std::string labelHead;
449 std::string sensorPathStr = sensorPath.string();
450 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700451 std::string sensorNameSubStr{""};
452 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
453 {
Josh Lehan06494452019-10-31 09:49:16 -0700454 // hwmon *_input filename without number:
455 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700456 sensorNameSubStr = matches[1];
457 }
458 else
459 {
Josh Lehan06494452019-10-31 09:49:16 -0700460 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700461 << sensorNameStr;
462 continue;
463 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800464
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530465 std::string labelPath;
466
467 /* find and differentiate _max and _input to replace "label" */
468 int pos = sensorPathStr.find("_");
469 if (pos != std::string::npos)
470 {
471
472 std::string sensorPathStrMax = sensorPathStr.substr(pos);
473 if (sensorPathStrMax.compare("_max") == 0)
474 {
475 labelPath =
476 boost::replace_all_copy(sensorPathStr, "max", "label");
477 maxLabel = true;
478 }
479 else
480 {
481 labelPath = boost::replace_all_copy(sensorPathStr, "input",
482 "label");
483 maxLabel = false;
484 }
485 }
486 else
487 {
488 continue;
489 }
490
Cheng C Yangecba9de2019-09-12 23:41:50 +0800491 std::ifstream labelFile(labelPath);
492 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800493 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800494 if constexpr (DEBUG)
495 {
496 std::cerr << "Input file " << sensorPath
497 << " has no corresponding label file\n";
498 }
Josh Lehan06494452019-10-31 09:49:16 -0700499 // hwmon *_input filename with number:
500 // temp1, temp2, temp3, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800501 labelHead = sensorNameStr.substr(0, sensorNameStr.find("_"));
Cheng C Yang209ec562019-03-12 16:37:44 +0800502 }
503 else
504 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800505 std::string label;
506 std::getline(labelFile, label);
507 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800508 auto findSensor = sensors.find(label);
509 if (findSensor != sensors.end())
510 {
511 continue;
512 }
513
Josh Lehan06494452019-10-31 09:49:16 -0700514 // hwmon corresponding *_label file contents:
515 // vin1, vout1, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800516 labelHead = label.substr(0, label.find(" "));
517 }
518
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530519 /* append "max" for labelMatch */
520 if (maxLabel)
521 {
522 labelHead = "max" + labelHead;
523 }
524
Josh Lehan74d9bd92019-10-31 08:51:58 -0700525 if constexpr (DEBUG)
526 {
527 std::cerr << "Sensor type=\"" << sensorNameSubStr
528 << "\" label=\"" << labelHead << "\"\n";
529 }
530
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530531 checkPWMSensor(sensorPath, labelHead, *interfacePath,
532 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800533
Vijay Khemka996bad12019-05-28 15:15:16 -0700534 if (!findLabels.empty())
535 {
536 /* Check if this labelHead is enabled in config file */
537 if (std::find(findLabels.begin(), findLabels.end(),
538 labelHead) == findLabels.end())
539 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800540 if constexpr (DEBUG)
541 {
542 std::cerr << "could not find " << labelHead
543 << " in the Labels list\n";
544 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700545 continue;
546 }
547 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800548
Cheng C Yange50345b2019-04-02 17:26:15 +0800549 auto findProperty = labelMatch.find(labelHead);
550 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800551 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700552 if constexpr (DEBUG)
553 {
554 std::cerr << "Could not find matching default property for "
555 << labelHead << "\n";
556 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800557 continue;
558 }
559
Josh Lehan74d9bd92019-10-31 08:51:58 -0700560 // Protect the hardcoded labelMatch list from changes,
561 // by making a copy and modifying that instead.
562 // Avoid bleedthrough of one device's customizations to
563 // the next device, as each should be independently customizable.
564 psuProperties.push_back(findProperty->second);
565 auto psuProperty = psuProperties.rbegin();
566
567 // Use label head as prefix for reading from config file,
568 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
569 std::string keyName = labelHead + "_Name";
570 std::string keyScale = labelHead + "_Scale";
571 std::string keyMin = labelHead + "_Min";
572 std::string keyMax = labelHead + "_Max";
573
574 bool customizedName = false;
575 auto findCustomName = baseConfig->second.find(keyName);
576 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700577 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700578 try
579 {
580 psuProperty->labelTypeName = std::visit(
581 VariantToStringVisitor(), findCustomName->second);
582 }
583 catch (std::invalid_argument&)
584 {
585 std::cerr << "Unable to parse " << keyName << "\n";
586 continue;
587 }
588
589 // All strings are valid, including empty string
590 customizedName = true;
591 }
592
593 bool customizedScale = false;
594 auto findCustomScale = baseConfig->second.find(keyScale);
595 if (findCustomScale != baseConfig->second.end())
596 {
597 try
598 {
599 psuProperty->sensorScaleFactor = std::visit(
600 VariantToUnsignedIntVisitor(), findCustomScale->second);
601 }
602 catch (std::invalid_argument&)
603 {
604 std::cerr << "Unable to parse " << keyScale << "\n";
605 continue;
606 }
607
608 // Avoid later division by zero
609 if (psuProperty->sensorScaleFactor > 0)
610 {
611 customizedScale = true;
612 }
613 else
614 {
615 std::cerr << "Unable to accept " << keyScale << "\n";
616 continue;
617 }
618 }
619
620 auto findCustomMin = baseConfig->second.find(keyMin);
621 if (findCustomMin != baseConfig->second.end())
622 {
623 try
624 {
625 psuProperty->minReading = std::visit(
626 VariantToDoubleVisitor(), findCustomMin->second);
627 }
628 catch (std::invalid_argument&)
629 {
630 std::cerr << "Unable to parse " << keyMin << "\n";
631 continue;
632 }
633 }
634
635 auto findCustomMax = baseConfig->second.find(keyMax);
636 if (findCustomMax != baseConfig->second.end())
637 {
638 try
639 {
640 psuProperty->maxReading = std::visit(
641 VariantToDoubleVisitor(), findCustomMax->second);
642 }
643 catch (std::invalid_argument&)
644 {
645 std::cerr << "Unable to parse " << keyMax << "\n";
646 continue;
647 }
648 }
649
650 if (!(psuProperty->minReading < psuProperty->maxReading))
651 {
652 std::cerr << "Min must be less than Max\n";
653 continue;
654 }
655
656 // If the sensor name is being customized by config file,
657 // then prefix/suffix composition becomes not necessary,
658 // and in fact not wanted, because it gets in the way.
659 std::string psuNameFromIndex;
660 if (!customizedName)
661 {
662 /* Find out sensor name index for this label */
663 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500664 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700665 if (std::regex_search(labelHead, matches, rgx))
666 {
667 nameIndex = std::stoi(matches[1]);
668
669 // Decrement to preserve alignment, because hwmon
670 // human-readable filenames and labels use 1-based
671 // numbering, but the "Name", "Name1", "Name2", etc. naming
672 // convention (the psuNames vector) uses 0-based numbering.
673 if (nameIndex > 0)
674 {
675 --nameIndex;
676 }
677 }
678 else
679 {
680 nameIndex = 0;
681 }
682
683 if (psuNames.size() <= nameIndex)
684 {
685 std::cerr << "Could not pair " << labelHead
686 << " with a Name field\n";
687 continue;
688 }
689
690 psuNameFromIndex = psuNames[nameIndex];
691
692 if constexpr (DEBUG)
693 {
694 std::cerr << "Sensor label head " << labelHead
695 << " paired with " << psuNameFromIndex
696 << " at index " << nameIndex << "\n";
697 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700698 }
699
Cheng C Yang58b2b532019-05-31 00:19:45 +0800700 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
701
Josh Lehan74d9bd92019-10-31 08:51:58 -0700702 // Similarly, if sensor scaling factor is being customized,
703 // then the below power-of-10 constraint becomes unnecessary,
704 // as config should be able to specify an arbitrary divisor.
705 unsigned int factor = psuProperty->sensorScaleFactor;
706 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700707 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700708 // Preserve existing usage of hardcoded labelMatch table below
709 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700710
Josh Lehan74d9bd92019-10-31 08:51:58 -0700711 /* Change first char of substring to uppercase */
712 char firstChar = sensorNameSubStr[0] - 0x20;
713 std::string strScaleFactor =
714 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
715
716 // Preserve existing configs by accepting earlier syntax,
717 // example CurrScaleFactor, PowerScaleFactor, ...
718 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
719 if (findScaleFactor != baseConfig->second.end())
720 {
721 factor = std::visit(VariantToIntVisitor(),
722 findScaleFactor->second);
723 }
724
725 if constexpr (DEBUG)
726 {
727 std::cerr << "Sensor scaling factor " << factor
728 << " string " << strScaleFactor << "\n";
729 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700730 }
731
Vijay Khemka996bad12019-05-28 15:15:16 -0700732 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530733 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
734 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800735 {
James Feist17ab6e02019-06-25 12:28:13 -0700736 std::cerr << "error populating thresholds for "
737 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800738 }
739
740 auto findSensorType = sensorTable.find(sensorNameSubStr);
741 if (findSensorType == sensorTable.end())
742 {
Jason Ling5747fab2019-10-02 16:46:23 -0700743 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700744 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800745 continue;
746 }
747
Josh Lehan49cfba92019-10-08 16:50:42 -0700748 if constexpr (DEBUG)
749 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700750 std::cerr << "Sensor properties: Name \""
751 << psuProperty->labelTypeName << "\" Scale "
752 << psuProperty->sensorScaleFactor << " Min "
753 << psuProperty->minReading << " Max "
754 << psuProperty->maxReading << "\n";
755 }
756
757 std::string sensorName = psuProperty->labelTypeName;
758 if (customizedName)
759 {
760 if (sensorName.empty())
761 {
762 // Allow selective disabling of an individual sensor,
763 // by customizing its name to an empty string.
764 std::cerr << "Sensor disabled, empty string\n";
765 continue;
766 }
767 }
768 else
769 {
770 // Sensor name not customized, do prefix/suffix composition,
771 // preserving default behavior by using psuNameFromIndex.
772 sensorName =
773 psuNameFromIndex + " " + psuProperty->labelTypeName;
774 }
775
776 if constexpr (DEBUG)
777 {
778 std::cerr << "Sensor name \"" << sensorName << "\" path \""
779 << sensorPathStr << "\" type \"" << sensorType
780 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700781 }
782
Yong Libf8b1da2020-04-15 16:32:50 +0800783 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800784 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800785 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700786 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800787 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800788 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700789 ++numCreated;
790 if constexpr (DEBUG)
791 {
792 std::cerr << "Created " << numCreated << " sensors so far\n";
793 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800794 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800795
796 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800797 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800798 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800799 std::make_unique<PSUCombineEvent>(
800 objectServer, dbusConnection, io, *psuName, eventPathList,
801 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800802 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700803
804 if constexpr (DEBUG)
805 {
806 std::cerr << "Created total of " << numCreated << " sensors\n";
807 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800808 return;
809}
810
Cheng C Yang916360b2019-05-07 18:47:16 +0800811void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800812{
Cheng C Yange50345b2019-04-02 17:26:15 +0800813 sensorTable = {{"power", "power/"},
814 {"curr", "current/"},
815 {"temp", "temperature/"},
816 {"in", "voltage/"},
817 {"fan", "fan_tach/"}};
818
819 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
820 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700821 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
822 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700823 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530824 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800825 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530826 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700827 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
828 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
829 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700830 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
831 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
832 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
833 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
834 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
835 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
836 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
837 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
838 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
839 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
840 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
841 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
842 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling32047ef2020-09-30 15:43:32 -0700843 {"vout17", PSUProperty("Output Voltage", 255, 0, 3)},
844 {"vout18", PSUProperty("Output Voltage", 255, 0, 3)},
845 {"vout19", PSUProperty("Output Voltage", 255, 0, 3)},
846 {"vout20", PSUProperty("Output Voltage", 255, 0, 3)},
847 {"vout21", PSUProperty("Output Voltage", 255, 0, 3)},
848 {"vout22", PSUProperty("Output Voltage", 255, 0, 3)},
849 {"vout23", PSUProperty("Output Voltage", 255, 0, 3)},
850 {"vout24", PSUProperty("Output Voltage", 255, 0, 3)},
851 {"vout25", PSUProperty("Output Voltage", 255, 0, 3)},
852 {"vout26", PSUProperty("Output Voltage", 255, 0, 3)},
853 {"vout27", PSUProperty("Output Voltage", 255, 0, 3)},
854 {"vout28", PSUProperty("Output Voltage", 255, 0, 3)},
855 {"vout29", PSUProperty("Output Voltage", 255, 0, 3)},
856 {"vout30", PSUProperty("Output Voltage", 255, 0, 3)},
857 {"vout31", PSUProperty("Output Voltage", 255, 0, 3)},
858 {"vout32", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700859 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800860 {"iin", PSUProperty("Input Current", 20, 0, 3)},
861 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700862 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
863 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800864 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
865 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
866 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
867 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
868 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
869 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
870 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
871 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
872 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
873 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
874 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700875 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530876 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800877 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700878 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
879 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700880 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
881 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800882 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530883 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800884 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
885 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800886
887 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800888
889 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
890 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
891
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800892 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
893 {"Failure", {"in2_alarm"}},
894 {"ACLost", {"in1_beep"}},
895 {"ConfigureError", {"in1_fault"}}};
896
897 groupEventMatch = {{"FanFault",
898 {{"fan1", {"fan1_alarm", "fan1_fault"}},
899 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800900}
901
James Feistb6c0b912019-07-09 12:21:44 -0700902int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800903{
904 boost::asio::io_service io;
905 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
906
907 systemBus->request_name("xyz.openbmc_project.PSUSensor");
908 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800909 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Cheng C Yang209ec562019-03-12 16:37:44 +0800910
Cheng C Yang916360b2019-05-07 18:47:16 +0800911 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800912
Cheng C Yang916360b2019-05-07 18:47:16 +0800913 io.post([&]() { createSensors(io, objectServer, systemBus); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800914 boost::asio::deadline_timer filterTimer(io);
915 std::function<void(sdbusplus::message::message&)> eventHandler =
916 [&](sdbusplus::message::message& message) {
917 if (message.is_method_error())
918 {
919 std::cerr << "callback method error\n";
920 return;
921 }
Cheng C Yanga97f1342020-02-11 15:10:41 +0800922 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800923 filterTimer.async_wait([&](const boost::system::error_code& ec) {
924 if (ec == boost::asio::error::operation_aborted)
925 {
926 return;
927 }
928 else if (ec)
929 {
930 std::cerr << "timer error\n";
931 }
Cheng C Yang916360b2019-05-07 18:47:16 +0800932 createSensors(io, objectServer, systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800933 });
934 };
935
936 for (const char* type : sensorTypes)
937 {
938 auto match = std::make_unique<sdbusplus::bus::match::match>(
939 static_cast<sdbusplus::bus::bus&>(*systemBus),
940 "type='signal',member='PropertiesChanged',path_namespace='" +
941 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
942 eventHandler);
943 matches.emplace_back(std::move(match));
944 }
945 io.run();
946}