blob: 6338ffeefd89a62495462b41a96338bd3fb00ad2 [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
Alex Qiuf5709b42020-01-29 13:29:50 -080043static constexpr std::array<const char*, 8> sensorTypes = {
Devjit Gopalpurde65ef72019-10-30 04:47:35 -070044 "xyz.openbmc_project.Configuration.INA230",
Alex Qiu6690d8f2020-01-22 17:56:33 -080045 "xyz.openbmc_project.Configuration.ISL68137",
Alex Qiuf5709b42020-01-29 13:29:50 -080046 "xyz.openbmc_project.Configuration.MAX16601",
Alex Qiu6690d8f2020-01-22 17:56:33 -080047 "xyz.openbmc_project.Configuration.MAX20730",
48 "xyz.openbmc_project.Configuration.MAX20734",
49 "xyz.openbmc_project.Configuration.MAX20796",
50 "xyz.openbmc_project.Configuration.MAX34451",
51 "xyz.openbmc_project.Configuration.pmbus"};
Cheng C Yang209ec562019-03-12 16:37:44 +080052
Alex Qiu6690d8f2020-01-22 17:56:33 -080053static std::vector<std::string> pmbusNames = {
Jason Ling38786d02020-06-05 17:45:13 -070054 "adm1272", "isl68137", "ina219", "ina230", "max16601", "max20730",
55 "max20734", "max20796", "max34451", "pmbus", "pxe1610", "raa228228"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070056
Cheng C Yang209ec562019-03-12 16:37:44 +080057namespace fs = std::filesystem;
58
Yong Libf8b1da2020-04-15 16:32:50 +080059static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080060 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080061static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
62 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080063static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
64 pwmSensors;
65static boost::container::flat_map<std::string, std::string> sensorTable;
66static boost::container::flat_map<std::string, PSUProperty> labelMatch;
67static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080068static boost::container::flat_map<std::string, std::vector<std::string>>
69 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080070static boost::container::flat_map<
71 std::string,
72 boost::container::flat_map<std::string, std::vector<std::string>>>
73 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080074static boost::container::flat_map<std::string, std::vector<std::string>>
75 limitEventMatch;
76
Josh Lehan74d9bd92019-10-31 08:51:58 -070077static std::vector<PSUProperty> psuProperties;
78
Cheng C Yang58b2b532019-05-31 00:19:45 +080079// Function CheckEvent will check each attribute from eventMatch table in the
80// sysfs. If the attributes exists in sysfs, then store the complete path
81// of the attribute into eventPathList.
82void checkEvent(
83 const std::string& directory,
84 const boost::container::flat_map<std::string, std::vector<std::string>>&
85 eventMatch,
86 boost::container::flat_map<std::string, std::vector<std::string>>&
87 eventPathList)
88{
89 for (const auto& match : eventMatch)
90 {
91 const std::vector<std::string>& eventAttrs = match.second;
92 const std::string& eventName = match.first;
93 for (const auto& eventAttr : eventAttrs)
94 {
95 auto eventPath = directory + "/" + eventAttr;
96
97 std::ifstream eventFile(eventPath);
98 if (!eventFile.good())
99 {
100 continue;
101 }
102
103 eventPathList[eventName].push_back(eventPath);
104 }
105 }
106}
107
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800108// Check Group Events which contains more than one targets in each combine
109// events.
110void checkGroupEvent(
111 const std::string& directory,
112 const boost::container::flat_map<
113 std::string,
114 boost::container::flat_map<std::string, std::vector<std::string>>>&
115 groupEventMatch,
116 boost::container::flat_map<
117 std::string,
118 boost::container::flat_map<std::string, std::vector<std::string>>>&
119 groupEventPathList)
120{
121 for (const auto& match : groupEventMatch)
122 {
123 const std::string& groupEventName = match.first;
124 const boost::container::flat_map<std::string, std::vector<std::string>>
125 events = match.second;
126 boost::container::flat_map<std::string, std::vector<std::string>>
127 pathList;
128 for (const auto& match : events)
129 {
130 const std::string& eventName = match.first;
131 const std::vector<std::string>& eventAttrs = match.second;
132 for (const auto& eventAttr : eventAttrs)
133 {
134 auto eventPath = directory + "/" + eventAttr;
135 std::ifstream eventFile(eventPath);
136 if (!eventFile.good())
137 {
138 continue;
139 }
140
141 pathList[eventName].push_back(eventPath);
142 }
143 }
144 groupEventPathList[groupEventName] = pathList;
145 }
146}
147
Cheng C Yang58b2b532019-05-31 00:19:45 +0800148// Function checkEventLimits will check all the psu related xxx_input attributes
149// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
150// xxx_min_alarm exist, then store the existing paths of the alarm attributes
151// to eventPathList.
152void checkEventLimits(
153 const std::string& sensorPathStr,
154 const boost::container::flat_map<std::string, std::vector<std::string>>&
155 limitEventMatch,
156 boost::container::flat_map<std::string, std::vector<std::string>>&
157 eventPathList)
158{
159 for (const auto& limitMatch : limitEventMatch)
160 {
161 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
162 const std::string& eventName = limitMatch.first;
163 for (const auto& limitEventAttr : limitEventAttrs)
164 {
165 auto limitEventPath =
166 boost::replace_all_copy(sensorPathStr, "input", limitEventAttr);
167 std::ifstream eventFile(limitEventPath);
168 if (!eventFile.good())
169 {
170 continue;
171 }
172 eventPathList[eventName].push_back(limitEventPath);
173 }
174 }
175}
Cheng C Yang916360b2019-05-07 18:47:16 +0800176
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530177static void
178 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
179 const std::string& interfacePath,
180 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
181 sdbusplus::asio::object_server& objectServer,
182 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800183{
184 for (const auto& pwmName : pwmTable)
185 {
186 if (pwmName.first != labelHead)
187 {
188 continue;
189 }
190
191 const std::string& sensorPathStr = sensorPath.string();
192 const std::string& pwmPathStr =
193 boost::replace_all_copy(sensorPathStr, "input", "target");
194 std::ifstream pwmFile(pwmPathStr);
195 if (!pwmFile.good())
196 {
197 continue;
198 }
199
200 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
201 if (findPWMSensor != pwmSensors.end())
202 {
203 continue;
204 }
205
206 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530207 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
208 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800209 }
210}
211
212void createSensors(boost::asio::io_service& io,
213 sdbusplus::asio::object_server& objectServer,
214 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Cheng C Yang209ec562019-03-12 16:37:44 +0800215{
216
217 ManagedObjectType sensorConfigs;
Josh Lehan49cfba92019-10-08 16:50:42 -0700218 int numCreated = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800219 bool useCache = false;
220
Cheng C Yang58b2b532019-05-31 00:19:45 +0800221 // TODO may need only modify the ones that need to be changed.
222 sensors.clear();
Cheng C Yang209ec562019-03-12 16:37:44 +0800223 for (const char* type : sensorTypes)
224 {
225 if (!getSensorConfiguration(type, dbusConnection, sensorConfigs,
226 useCache))
227 {
228 std::cerr << "error get sensor config from entity manager\n";
229 return;
230 }
231 useCache = true;
232 }
233
234 std::vector<fs::path> pmbusPaths;
235 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
236 {
237 std::cerr << "No PSU sensors in system\n";
238 return;
239 }
240
241 boost::container::flat_set<std::string> directories;
242 for (const auto& pmbusPath : pmbusPaths)
243 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800244 boost::container::flat_map<std::string, std::vector<std::string>>
245 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800246 boost::container::flat_map<
247 std::string,
248 boost::container::flat_map<std::string, std::vector<std::string>>>
249 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800250
251 std::ifstream nameFile(pmbusPath);
252 if (!nameFile.good())
253 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700254 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800255 continue;
256 }
257
258 std::string pmbusName;
259 std::getline(nameFile, pmbusName);
260 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700261
262 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
263 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800264 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700265 // To avoid this error message, add your driver name to
266 // the pmbusNames vector at the top of this file.
267 std::cerr << "Driver name " << pmbusName
268 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800269 continue;
270 }
271
272 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800273 auto directory = pmbusPath.parent_path();
274
275 auto ret = directories.insert(directory.string());
276 if (!ret.second)
277 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700278 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800279 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800280 }
281
James Feistb6c0b912019-07-09 12:21:44 -0700282 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800283 std::string deviceName = fs::canonical(device).stem();
284 auto findHyphen = deviceName.find("-");
285 if (findHyphen == std::string::npos)
286 {
287 std::cerr << "found bad device" << deviceName << "\n";
288 continue;
289 }
290 std::string busStr = deviceName.substr(0, findHyphen);
291 std::string addrStr = deviceName.substr(findHyphen + 1);
292
293 size_t bus = 0;
294 size_t addr = 0;
295
296 try
297 {
298 bus = std::stoi(busStr);
299 addr = std::stoi(addrStr, 0, 16);
300 }
James Feistb6c0b912019-07-09 12:21:44 -0700301 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800302 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700303 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
304 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800305 continue;
306 }
307
Cheng C Yang209ec562019-03-12 16:37:44 +0800308 const std::pair<std::string, boost::container::flat_map<
309 std::string, BasicVariantType>>*
310 baseConfig = nullptr;
311 const SensorData* sensorData = nullptr;
312 const std::string* interfacePath = nullptr;
313 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800314 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800315
316 for (const std::pair<sdbusplus::message::object_path, SensorData>&
317 sensor : sensorConfigs)
318 {
319 sensorData = &(sensor.second);
320 for (const char* type : sensorTypes)
321 {
322 auto sensorBase = sensorData->find(type);
323 if (sensorBase != sensorData->end())
324 {
325 baseConfig = &(*sensorBase);
326 sensorType = type;
327 break;
328 }
329 }
330 if (baseConfig == nullptr)
331 {
332 std::cerr << "error finding base configuration for "
333 << deviceName << "\n";
334 continue;
335 }
336
337 auto configBus = baseConfig->second.find("Bus");
338 auto configAddress = baseConfig->second.find("Address");
339
340 if (configBus == baseConfig->second.end() ||
341 configAddress == baseConfig->second.end())
342 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800343 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800344 continue;
345 }
346
Cheng C Yang58b2b532019-05-31 00:19:45 +0800347 const uint64_t* confBus;
348 const uint64_t* confAddr;
349 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
350 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
351 {
352 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700353 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800354 continue;
355 }
356
357 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700359 std::cerr << "Configuration skipping " << *confBus << "-"
360 << *confAddr << " because not " << bus << "-" << addr
361 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 continue;
363 }
364
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800365 std::vector<thresholds::Threshold> confThresholds;
366 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
367 {
368 std::cerr << "error populating totoal thresholds\n";
369 }
370 thresholdConfSize = confThresholds.size();
371
Cheng C Yang209ec562019-03-12 16:37:44 +0800372 interfacePath = &(sensor.first.str);
373 break;
374 }
375 if (interfacePath == nullptr)
376 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700377 // To avoid this error message, add your export map entry,
378 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 std::cerr << "failed to find match for " << deviceName << "\n";
380 continue;
381 }
382
Cheng C Yange50345b2019-04-02 17:26:15 +0800383 auto findPSUName = baseConfig->second.find("Name");
384 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800385 {
386 std::cerr << "could not determine configuration name for "
387 << deviceName << "\n";
388 continue;
389 }
390
Cheng C Yang58b2b532019-05-31 00:19:45 +0800391 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
392 {
393 std::cerr << "Cannot find psu name, invalid configuration\n";
394 continue;
395 }
396 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800397 checkGroupEvent(directory.string(), groupEventMatch,
398 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800399
Vijay Khemka996bad12019-05-28 15:15:16 -0700400 /* Check if there are more sensors in the same interface */
401 int i = 1;
402 std::vector<std::string> psuNames;
403 do
404 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700405 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700406 psuNames.push_back(std::get<std::string>(findPSUName->second));
407 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
408 } while (findPSUName != baseConfig->second.end());
409
Cheng C Yange50345b2019-04-02 17:26:15 +0800410 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700411 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800412 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800413 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800414 continue;
415 }
416
Vijay Khemka996bad12019-05-28 15:15:16 -0700417 /* Find array of labels to be exposed if it is defined in config */
418 std::vector<std::string> findLabels;
419 auto findLabelObj = baseConfig->second.find("Labels");
420 if (findLabelObj != baseConfig->second.end())
421 {
422 findLabels =
423 std::get<std::vector<std::string>>(findLabelObj->second);
424 }
425
Jason Ling5747fab2019-10-02 16:46:23 -0700426 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
427 std::smatch matches;
428
Cheng C Yange50345b2019-04-02 17:26:15 +0800429 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800430 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800431 std::string labelHead;
432 std::string sensorPathStr = sensorPath.string();
433 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700434 std::string sensorNameSubStr{""};
435 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
436 {
Josh Lehan06494452019-10-31 09:49:16 -0700437 // hwmon *_input filename without number:
438 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700439 sensorNameSubStr = matches[1];
440 }
441 else
442 {
Josh Lehan06494452019-10-31 09:49:16 -0700443 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700444 << sensorNameStr;
445 continue;
446 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800447
Cheng C Yangecba9de2019-09-12 23:41:50 +0800448 auto labelPath =
449 boost::replace_all_copy(sensorPathStr, "input", "label");
450 std::ifstream labelFile(labelPath);
451 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800452 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800453 if constexpr (DEBUG)
454 {
455 std::cerr << "Input file " << sensorPath
456 << " has no corresponding label file\n";
457 }
Josh Lehan06494452019-10-31 09:49:16 -0700458 // hwmon *_input filename with number:
459 // temp1, temp2, temp3, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800460 labelHead = sensorNameStr.substr(0, sensorNameStr.find("_"));
Cheng C Yang209ec562019-03-12 16:37:44 +0800461 }
462 else
463 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800464 std::string label;
465 std::getline(labelFile, label);
466 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800467 auto findSensor = sensors.find(label);
468 if (findSensor != sensors.end())
469 {
470 continue;
471 }
472
Josh Lehan06494452019-10-31 09:49:16 -0700473 // hwmon corresponding *_label file contents:
474 // vin1, vout1, ...
Cheng C Yange50345b2019-04-02 17:26:15 +0800475 labelHead = label.substr(0, label.find(" "));
476 }
477
Josh Lehan74d9bd92019-10-31 08:51:58 -0700478 if constexpr (DEBUG)
479 {
480 std::cerr << "Sensor type=\"" << sensorNameSubStr
481 << "\" label=\"" << labelHead << "\"\n";
482 }
483
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530484 checkPWMSensor(sensorPath, labelHead, *interfacePath,
485 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800486
Vijay Khemka996bad12019-05-28 15:15:16 -0700487 if (!findLabels.empty())
488 {
489 /* Check if this labelHead is enabled in config file */
490 if (std::find(findLabels.begin(), findLabels.end(),
491 labelHead) == findLabels.end())
492 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800493 if constexpr (DEBUG)
494 {
495 std::cerr << "could not find " << labelHead
496 << " in the Labels list\n";
497 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700498 continue;
499 }
500 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800501
Cheng C Yange50345b2019-04-02 17:26:15 +0800502 auto findProperty = labelMatch.find(labelHead);
503 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800504 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700505 if constexpr (DEBUG)
506 {
507 std::cerr << "Could not find matching default property for "
508 << labelHead << "\n";
509 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800510 continue;
511 }
512
Josh Lehan74d9bd92019-10-31 08:51:58 -0700513 // Protect the hardcoded labelMatch list from changes,
514 // by making a copy and modifying that instead.
515 // Avoid bleedthrough of one device's customizations to
516 // the next device, as each should be independently customizable.
517 psuProperties.push_back(findProperty->second);
518 auto psuProperty = psuProperties.rbegin();
519
520 // Use label head as prefix for reading from config file,
521 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
522 std::string keyName = labelHead + "_Name";
523 std::string keyScale = labelHead + "_Scale";
524 std::string keyMin = labelHead + "_Min";
525 std::string keyMax = labelHead + "_Max";
526
527 bool customizedName = false;
528 auto findCustomName = baseConfig->second.find(keyName);
529 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700530 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700531 try
532 {
533 psuProperty->labelTypeName = std::visit(
534 VariantToStringVisitor(), findCustomName->second);
535 }
536 catch (std::invalid_argument&)
537 {
538 std::cerr << "Unable to parse " << keyName << "\n";
539 continue;
540 }
541
542 // All strings are valid, including empty string
543 customizedName = true;
544 }
545
546 bool customizedScale = false;
547 auto findCustomScale = baseConfig->second.find(keyScale);
548 if (findCustomScale != baseConfig->second.end())
549 {
550 try
551 {
552 psuProperty->sensorScaleFactor = std::visit(
553 VariantToUnsignedIntVisitor(), findCustomScale->second);
554 }
555 catch (std::invalid_argument&)
556 {
557 std::cerr << "Unable to parse " << keyScale << "\n";
558 continue;
559 }
560
561 // Avoid later division by zero
562 if (psuProperty->sensorScaleFactor > 0)
563 {
564 customizedScale = true;
565 }
566 else
567 {
568 std::cerr << "Unable to accept " << keyScale << "\n";
569 continue;
570 }
571 }
572
573 auto findCustomMin = baseConfig->second.find(keyMin);
574 if (findCustomMin != baseConfig->second.end())
575 {
576 try
577 {
578 psuProperty->minReading = std::visit(
579 VariantToDoubleVisitor(), findCustomMin->second);
580 }
581 catch (std::invalid_argument&)
582 {
583 std::cerr << "Unable to parse " << keyMin << "\n";
584 continue;
585 }
586 }
587
588 auto findCustomMax = baseConfig->second.find(keyMax);
589 if (findCustomMax != baseConfig->second.end())
590 {
591 try
592 {
593 psuProperty->maxReading = std::visit(
594 VariantToDoubleVisitor(), findCustomMax->second);
595 }
596 catch (std::invalid_argument&)
597 {
598 std::cerr << "Unable to parse " << keyMax << "\n";
599 continue;
600 }
601 }
602
603 if (!(psuProperty->minReading < psuProperty->maxReading))
604 {
605 std::cerr << "Min must be less than Max\n";
606 continue;
607 }
608
609 // If the sensor name is being customized by config file,
610 // then prefix/suffix composition becomes not necessary,
611 // and in fact not wanted, because it gets in the way.
612 std::string psuNameFromIndex;
613 if (!customizedName)
614 {
615 /* Find out sensor name index for this label */
616 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500617 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700618 if (std::regex_search(labelHead, matches, rgx))
619 {
620 nameIndex = std::stoi(matches[1]);
621
622 // Decrement to preserve alignment, because hwmon
623 // human-readable filenames and labels use 1-based
624 // numbering, but the "Name", "Name1", "Name2", etc. naming
625 // convention (the psuNames vector) uses 0-based numbering.
626 if (nameIndex > 0)
627 {
628 --nameIndex;
629 }
630 }
631 else
632 {
633 nameIndex = 0;
634 }
635
636 if (psuNames.size() <= nameIndex)
637 {
638 std::cerr << "Could not pair " << labelHead
639 << " with a Name field\n";
640 continue;
641 }
642
643 psuNameFromIndex = psuNames[nameIndex];
644
645 if constexpr (DEBUG)
646 {
647 std::cerr << "Sensor label head " << labelHead
648 << " paired with " << psuNameFromIndex
649 << " at index " << nameIndex << "\n";
650 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700651 }
652
Cheng C Yang58b2b532019-05-31 00:19:45 +0800653 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
654
Josh Lehan74d9bd92019-10-31 08:51:58 -0700655 // Similarly, if sensor scaling factor is being customized,
656 // then the below power-of-10 constraint becomes unnecessary,
657 // as config should be able to specify an arbitrary divisor.
658 unsigned int factor = psuProperty->sensorScaleFactor;
659 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700660 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700661 // Preserve existing usage of hardcoded labelMatch table below
662 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700663
Josh Lehan74d9bd92019-10-31 08:51:58 -0700664 /* Change first char of substring to uppercase */
665 char firstChar = sensorNameSubStr[0] - 0x20;
666 std::string strScaleFactor =
667 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
668
669 // Preserve existing configs by accepting earlier syntax,
670 // example CurrScaleFactor, PowerScaleFactor, ...
671 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
672 if (findScaleFactor != baseConfig->second.end())
673 {
674 factor = std::visit(VariantToIntVisitor(),
675 findScaleFactor->second);
676 }
677
678 if constexpr (DEBUG)
679 {
680 std::cerr << "Sensor scaling factor " << factor
681 << " string " << strScaleFactor << "\n";
682 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700683 }
684
Vijay Khemka996bad12019-05-28 15:15:16 -0700685 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530686 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
687 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800688 {
James Feist17ab6e02019-06-25 12:28:13 -0700689 std::cerr << "error populating thresholds for "
690 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800691 }
692
693 auto findSensorType = sensorTable.find(sensorNameSubStr);
694 if (findSensorType == sensorTable.end())
695 {
Jason Ling5747fab2019-10-02 16:46:23 -0700696 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700697 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800698 continue;
699 }
700
Josh Lehan49cfba92019-10-08 16:50:42 -0700701 if constexpr (DEBUG)
702 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700703 std::cerr << "Sensor properties: Name \""
704 << psuProperty->labelTypeName << "\" Scale "
705 << psuProperty->sensorScaleFactor << " Min "
706 << psuProperty->minReading << " Max "
707 << psuProperty->maxReading << "\n";
708 }
709
710 std::string sensorName = psuProperty->labelTypeName;
711 if (customizedName)
712 {
713 if (sensorName.empty())
714 {
715 // Allow selective disabling of an individual sensor,
716 // by customizing its name to an empty string.
717 std::cerr << "Sensor disabled, empty string\n";
718 continue;
719 }
720 }
721 else
722 {
723 // Sensor name not customized, do prefix/suffix composition,
724 // preserving default behavior by using psuNameFromIndex.
725 sensorName =
726 psuNameFromIndex + " " + psuProperty->labelTypeName;
727 }
728
729 if constexpr (DEBUG)
730 {
731 std::cerr << "Sensor name \"" << sensorName << "\" path \""
732 << sensorPathStr << "\" type \"" << sensorType
733 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700734 }
735
Yong Libf8b1da2020-04-15 16:32:50 +0800736 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800737 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800738 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700739 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800740 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800741 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700742 ++numCreated;
743 if constexpr (DEBUG)
744 {
745 std::cerr << "Created " << numCreated << " sensors so far\n";
746 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800747 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800748
749 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800750 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800751 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800752 std::make_unique<PSUCombineEvent>(
753 objectServer, dbusConnection, io, *psuName, eventPathList,
754 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800755 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700756
757 if constexpr (DEBUG)
758 {
759 std::cerr << "Created total of " << numCreated << " sensors\n";
760 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800761 return;
762}
763
Cheng C Yang916360b2019-05-07 18:47:16 +0800764void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800765{
Cheng C Yange50345b2019-04-02 17:26:15 +0800766 sensorTable = {{"power", "power/"},
767 {"curr", "current/"},
768 {"temp", "temperature/"},
769 {"in", "voltage/"},
770 {"fan", "fan_tach/"}};
771
772 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
773 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700774 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
775 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700776 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800777 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700778 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
779 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
780 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700781 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
782 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
783 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
784 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
785 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
786 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
787 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
788 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
789 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
790 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
791 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
792 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
793 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700794 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800795 {"iin", PSUProperty("Input Current", 20, 0, 3)},
796 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700797 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
798 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800799 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
800 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
801 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
802 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
803 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
804 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
805 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
806 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
807 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
808 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
809 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700810 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800811 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700812 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
813 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700814 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
815 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800816 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800817 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
818 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800819
820 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800821
822 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
823 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
824
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800825 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
826 {"Failure", {"in2_alarm"}},
827 {"ACLost", {"in1_beep"}},
828 {"ConfigureError", {"in1_fault"}}};
829
830 groupEventMatch = {{"FanFault",
831 {{"fan1", {"fan1_alarm", "fan1_fault"}},
832 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800833}
834
James Feistb6c0b912019-07-09 12:21:44 -0700835int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800836{
837 boost::asio::io_service io;
838 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
839
840 systemBus->request_name("xyz.openbmc_project.PSUSensor");
841 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800842 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Cheng C Yang209ec562019-03-12 16:37:44 +0800843
Cheng C Yang916360b2019-05-07 18:47:16 +0800844 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800845
Cheng C Yang916360b2019-05-07 18:47:16 +0800846 io.post([&]() { createSensors(io, objectServer, systemBus); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800847 boost::asio::deadline_timer filterTimer(io);
848 std::function<void(sdbusplus::message::message&)> eventHandler =
849 [&](sdbusplus::message::message& message) {
850 if (message.is_method_error())
851 {
852 std::cerr << "callback method error\n";
853 return;
854 }
Cheng C Yanga97f1342020-02-11 15:10:41 +0800855 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800856 filterTimer.async_wait([&](const boost::system::error_code& ec) {
857 if (ec == boost::asio::error::operation_aborted)
858 {
859 return;
860 }
861 else if (ec)
862 {
863 std::cerr << "timer error\n";
864 }
Cheng C Yang916360b2019-05-07 18:47:16 +0800865 createSensors(io, objectServer, systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800866 });
867 };
868
869 for (const char* type : sensorTypes)
870 {
871 auto match = std::make_unique<sdbusplus::bus::match::match>(
872 static_cast<sdbusplus::bus::bus&>(*systemBus),
873 "type='signal',member='PropertiesChanged',path_namespace='" +
874 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
875 eventHandler);
876 matches.emplace_back(std::move(match));
877 }
878 io.run();
879}