blob: 616aec71c48bb32b6add2d2177c7936e3aad23d0 [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
Vijay Khemka996bad12019-05-28 15:15:16 -0700423 /* Find array of labels to be exposed if it is defined in config */
424 std::vector<std::string> findLabels;
425 auto findLabelObj = baseConfig->second.find("Labels");
426 if (findLabelObj != baseConfig->second.end())
427 {
428 findLabels =
429 std::get<std::vector<std::string>>(findLabelObj->second);
430 }
431
Jason Ling5747fab2019-10-02 16:46:23 -0700432 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
433 std::smatch matches;
434
Cheng C Yange50345b2019-04-02 17:26:15 +0800435 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800436 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800437 std::string labelHead;
438 std::string sensorPathStr = sensorPath.string();
439 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700440 std::string sensorNameSubStr{""};
441 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
442 {
Josh Lehan06494452019-10-31 09:49:16 -0700443 // hwmon *_input filename without number:
444 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700445 sensorNameSubStr = matches[1];
446 }
447 else
448 {
Josh Lehan06494452019-10-31 09:49:16 -0700449 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700450 << sensorNameStr;
451 continue;
452 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800453
Cheng C Yangecba9de2019-09-12 23:41:50 +0800454 auto labelPath =
455 boost::replace_all_copy(sensorPathStr, "input", "label");
456 std::ifstream labelFile(labelPath);
457 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800458 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800459 if constexpr (DEBUG)
460 {
461 std::cerr << "Input file " << sensorPath
462 << " has no corresponding label file\n";
463 }
Josh Lehan06494452019-10-31 09:49:16 -0700464 // hwmon *_input filename with number:
465 // temp1, temp2, temp3, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800466 labelHead = sensorNameStr.substr(0, sensorNameStr.find("_"));
Cheng C Yang209ec562019-03-12 16:37:44 +0800467 }
468 else
469 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800470 std::string label;
471 std::getline(labelFile, label);
472 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800473 auto findSensor = sensors.find(label);
474 if (findSensor != sensors.end())
475 {
476 continue;
477 }
478
Josh Lehan06494452019-10-31 09:49:16 -0700479 // hwmon corresponding *_label file contents:
480 // vin1, vout1, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800481 labelHead = label.substr(0, label.find(" "));
482 }
483
Josh Lehan74d9bd92019-10-31 08:51:58 -0700484 if constexpr (DEBUG)
485 {
486 std::cerr << "Sensor type=\"" << sensorNameSubStr
487 << "\" label=\"" << labelHead << "\"\n";
488 }
489
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530490 checkPWMSensor(sensorPath, labelHead, *interfacePath,
491 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800492
Vijay Khemka996bad12019-05-28 15:15:16 -0700493 if (!findLabels.empty())
494 {
495 /* Check if this labelHead is enabled in config file */
496 if (std::find(findLabels.begin(), findLabels.end(),
497 labelHead) == findLabels.end())
498 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800499 if constexpr (DEBUG)
500 {
501 std::cerr << "could not find " << labelHead
502 << " in the Labels list\n";
503 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700504 continue;
505 }
506 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800507
Cheng C Yange50345b2019-04-02 17:26:15 +0800508 auto findProperty = labelMatch.find(labelHead);
509 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800510 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700511 if constexpr (DEBUG)
512 {
513 std::cerr << "Could not find matching default property for "
514 << labelHead << "\n";
515 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800516 continue;
517 }
518
Josh Lehan74d9bd92019-10-31 08:51:58 -0700519 // Protect the hardcoded labelMatch list from changes,
520 // by making a copy and modifying that instead.
521 // Avoid bleedthrough of one device's customizations to
522 // the next device, as each should be independently customizable.
523 psuProperties.push_back(findProperty->second);
524 auto psuProperty = psuProperties.rbegin();
525
526 // Use label head as prefix for reading from config file,
527 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
528 std::string keyName = labelHead + "_Name";
529 std::string keyScale = labelHead + "_Scale";
530 std::string keyMin = labelHead + "_Min";
531 std::string keyMax = labelHead + "_Max";
532
533 bool customizedName = false;
534 auto findCustomName = baseConfig->second.find(keyName);
535 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700536 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700537 try
538 {
539 psuProperty->labelTypeName = std::visit(
540 VariantToStringVisitor(), findCustomName->second);
541 }
542 catch (std::invalid_argument&)
543 {
544 std::cerr << "Unable to parse " << keyName << "\n";
545 continue;
546 }
547
548 // All strings are valid, including empty string
549 customizedName = true;
550 }
551
552 bool customizedScale = false;
553 auto findCustomScale = baseConfig->second.find(keyScale);
554 if (findCustomScale != baseConfig->second.end())
555 {
556 try
557 {
558 psuProperty->sensorScaleFactor = std::visit(
559 VariantToUnsignedIntVisitor(), findCustomScale->second);
560 }
561 catch (std::invalid_argument&)
562 {
563 std::cerr << "Unable to parse " << keyScale << "\n";
564 continue;
565 }
566
567 // Avoid later division by zero
568 if (psuProperty->sensorScaleFactor > 0)
569 {
570 customizedScale = true;
571 }
572 else
573 {
574 std::cerr << "Unable to accept " << keyScale << "\n";
575 continue;
576 }
577 }
578
579 auto findCustomMin = baseConfig->second.find(keyMin);
580 if (findCustomMin != baseConfig->second.end())
581 {
582 try
583 {
584 psuProperty->minReading = std::visit(
585 VariantToDoubleVisitor(), findCustomMin->second);
586 }
587 catch (std::invalid_argument&)
588 {
589 std::cerr << "Unable to parse " << keyMin << "\n";
590 continue;
591 }
592 }
593
594 auto findCustomMax = baseConfig->second.find(keyMax);
595 if (findCustomMax != baseConfig->second.end())
596 {
597 try
598 {
599 psuProperty->maxReading = std::visit(
600 VariantToDoubleVisitor(), findCustomMax->second);
601 }
602 catch (std::invalid_argument&)
603 {
604 std::cerr << "Unable to parse " << keyMax << "\n";
605 continue;
606 }
607 }
608
609 if (!(psuProperty->minReading < psuProperty->maxReading))
610 {
611 std::cerr << "Min must be less than Max\n";
612 continue;
613 }
614
615 // If the sensor name is being customized by config file,
616 // then prefix/suffix composition becomes not necessary,
617 // and in fact not wanted, because it gets in the way.
618 std::string psuNameFromIndex;
619 if (!customizedName)
620 {
621 /* Find out sensor name index for this label */
622 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500623 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700624 if (std::regex_search(labelHead, matches, rgx))
625 {
626 nameIndex = std::stoi(matches[1]);
627
628 // Decrement to preserve alignment, because hwmon
629 // human-readable filenames and labels use 1-based
630 // numbering, but the "Name", "Name1", "Name2", etc. naming
631 // convention (the psuNames vector) uses 0-based numbering.
632 if (nameIndex > 0)
633 {
634 --nameIndex;
635 }
636 }
637 else
638 {
639 nameIndex = 0;
640 }
641
642 if (psuNames.size() <= nameIndex)
643 {
644 std::cerr << "Could not pair " << labelHead
645 << " with a Name field\n";
646 continue;
647 }
648
649 psuNameFromIndex = psuNames[nameIndex];
650
651 if constexpr (DEBUG)
652 {
653 std::cerr << "Sensor label head " << labelHead
654 << " paired with " << psuNameFromIndex
655 << " at index " << nameIndex << "\n";
656 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700657 }
658
Cheng C Yang58b2b532019-05-31 00:19:45 +0800659 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
660
Josh Lehan74d9bd92019-10-31 08:51:58 -0700661 // Similarly, if sensor scaling factor is being customized,
662 // then the below power-of-10 constraint becomes unnecessary,
663 // as config should be able to specify an arbitrary divisor.
664 unsigned int factor = psuProperty->sensorScaleFactor;
665 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700666 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700667 // Preserve existing usage of hardcoded labelMatch table below
668 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700669
Josh Lehan74d9bd92019-10-31 08:51:58 -0700670 /* Change first char of substring to uppercase */
671 char firstChar = sensorNameSubStr[0] - 0x20;
672 std::string strScaleFactor =
673 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
674
675 // Preserve existing configs by accepting earlier syntax,
676 // example CurrScaleFactor, PowerScaleFactor, ...
677 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
678 if (findScaleFactor != baseConfig->second.end())
679 {
680 factor = std::visit(VariantToIntVisitor(),
681 findScaleFactor->second);
682 }
683
684 if constexpr (DEBUG)
685 {
686 std::cerr << "Sensor scaling factor " << factor
687 << " string " << strScaleFactor << "\n";
688 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700689 }
690
Vijay Khemka996bad12019-05-28 15:15:16 -0700691 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530692 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
693 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800694 {
James Feist17ab6e02019-06-25 12:28:13 -0700695 std::cerr << "error populating thresholds for "
696 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800697 }
698
699 auto findSensorType = sensorTable.find(sensorNameSubStr);
700 if (findSensorType == sensorTable.end())
701 {
Jason Ling5747fab2019-10-02 16:46:23 -0700702 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700703 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800704 continue;
705 }
706
Josh Lehan49cfba92019-10-08 16:50:42 -0700707 if constexpr (DEBUG)
708 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700709 std::cerr << "Sensor properties: Name \""
710 << psuProperty->labelTypeName << "\" Scale "
711 << psuProperty->sensorScaleFactor << " Min "
712 << psuProperty->minReading << " Max "
713 << psuProperty->maxReading << "\n";
714 }
715
716 std::string sensorName = psuProperty->labelTypeName;
717 if (customizedName)
718 {
719 if (sensorName.empty())
720 {
721 // Allow selective disabling of an individual sensor,
722 // by customizing its name to an empty string.
723 std::cerr << "Sensor disabled, empty string\n";
724 continue;
725 }
726 }
727 else
728 {
729 // Sensor name not customized, do prefix/suffix composition,
730 // preserving default behavior by using psuNameFromIndex.
731 sensorName =
732 psuNameFromIndex + " " + psuProperty->labelTypeName;
733 }
734
735 if constexpr (DEBUG)
736 {
737 std::cerr << "Sensor name \"" << sensorName << "\" path \""
738 << sensorPathStr << "\" type \"" << sensorType
739 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700740 }
741
Yong Libf8b1da2020-04-15 16:32:50 +0800742 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800743 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800744 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700745 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800746 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800747 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700748 ++numCreated;
749 if constexpr (DEBUG)
750 {
751 std::cerr << "Created " << numCreated << " sensors so far\n";
752 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800753 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800754
755 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800756 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800757 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800758 std::make_unique<PSUCombineEvent>(
759 objectServer, dbusConnection, io, *psuName, eventPathList,
760 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800761 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700762
763 if constexpr (DEBUG)
764 {
765 std::cerr << "Created total of " << numCreated << " sensors\n";
766 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800767 return;
768}
769
Cheng C Yang916360b2019-05-07 18:47:16 +0800770void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800771{
Cheng C Yange50345b2019-04-02 17:26:15 +0800772 sensorTable = {{"power", "power/"},
773 {"curr", "current/"},
774 {"temp", "temperature/"},
775 {"in", "voltage/"},
776 {"fan", "fan_tach/"}};
777
778 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
779 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700780 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
781 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700782 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800783 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700784 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
785 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
786 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700787 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
788 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
789 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
790 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
791 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
792 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
793 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
794 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
795 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
796 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
797 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
798 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
799 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700800 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800801 {"iin", PSUProperty("Input Current", 20, 0, 3)},
802 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700803 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
804 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800805 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
806 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
807 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
808 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
809 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
810 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
811 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
812 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
813 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
814 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
815 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700816 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800817 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700818 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
819 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700820 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
821 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800822 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800823 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
824 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800825
826 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800827
828 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
829 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
830
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800831 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
832 {"Failure", {"in2_alarm"}},
833 {"ACLost", {"in1_beep"}},
834 {"ConfigureError", {"in1_fault"}}};
835
836 groupEventMatch = {{"FanFault",
837 {{"fan1", {"fan1_alarm", "fan1_fault"}},
838 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800839}
840
James Feistb6c0b912019-07-09 12:21:44 -0700841int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800842{
843 boost::asio::io_service io;
844 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
845
846 systemBus->request_name("xyz.openbmc_project.PSUSensor");
847 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800848 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Cheng C Yang209ec562019-03-12 16:37:44 +0800849
Cheng C Yang916360b2019-05-07 18:47:16 +0800850 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800851
Cheng C Yang916360b2019-05-07 18:47:16 +0800852 io.post([&]() { createSensors(io, objectServer, systemBus); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800853 boost::asio::deadline_timer filterTimer(io);
854 std::function<void(sdbusplus::message::message&)> eventHandler =
855 [&](sdbusplus::message::message& message) {
856 if (message.is_method_error())
857 {
858 std::cerr << "callback method error\n";
859 return;
860 }
Cheng C Yanga97f1342020-02-11 15:10:41 +0800861 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800862 filterTimer.async_wait([&](const boost::system::error_code& ec) {
863 if (ec == boost::asio::error::operation_aborted)
864 {
865 return;
866 }
867 else if (ec)
868 {
869 std::cerr << "timer error\n";
870 }
Cheng C Yang916360b2019-05-07 18:47:16 +0800871 createSensors(io, objectServer, systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800872 });
873 };
874
875 for (const char* type : sensorTypes)
876 {
877 auto match = std::make_unique<sdbusplus::bus::match::match>(
878 static_cast<sdbusplus::bus::bus&>(*systemBus),
879 "type='signal',member='PropertiesChanged',path_namespace='" +
880 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
881 eventHandler);
882 matches.emplace_back(std::move(match));
883 }
884 io.run();
885}