blob: 8e1be6a5172f049add01ad34e3b762bdea236b7d [file] [log] [blame]
Brandon Wymana0f33ce2019-10-17 18:32:29 -05001#include "psu_manager.hpp"
2
3#include "utility.hpp"
4
Brandon Wymanb76ab242020-09-16 18:06:06 -05005#include <fmt/format.h>
6#include <sys/types.h>
7#include <unistd.h>
8
Brandon Wymanecbecbc2021-08-31 22:53:21 +00009#include <regex>
10
Brandon Wymanaed1f752019-11-25 18:10:52 -060011using namespace phosphor::logging;
12
Brandon Wyman63ea78b2020-09-24 16:49:09 -050013namespace phosphor::power::manager
Brandon Wymana0f33ce2019-10-17 18:32:29 -050014{
15
Brandon Wyman510acaa2020-11-05 18:32:04 -060016constexpr auto IBMCFFPSInterface =
17 "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
18constexpr auto i2cBusProp = "I2CBus";
19constexpr auto i2cAddressProp = "I2CAddress";
20constexpr auto psuNameProp = "Name";
B. J. Wyman681b2a32021-04-20 22:31:22 +000021constexpr auto presLineName = "NamedPresenceGpio";
Brandon Wyman510acaa2020-11-05 18:32:04 -060022
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060023constexpr auto supportedConfIntf =
24 "xyz.openbmc_project.Configuration.SupportedConfiguration";
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060025
Brandon Wyman510acaa2020-11-05 18:32:04 -060026PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e) :
27 bus(bus)
28{
Brandon Wyman510acaa2020-11-05 18:32:04 -060029 // Subscribe to InterfacesAdded before doing a property read, otherwise
30 // the interface could be created after the read attempt but before the
31 // match is created.
32 entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
33 bus,
34 sdbusplus::bus::match::rules::interfacesAdded() +
35 sdbusplus::bus::match::rules::sender(
36 "xyz.openbmc_project.EntityManager"),
37 std::bind(&PSUManager::entityManagerIfaceAdded, this,
38 std::placeholders::_1));
39 getPSUConfiguration();
40 getSystemProperties();
41
42 using namespace sdeventplus;
43 auto interval = std::chrono::milliseconds(1000);
44 timer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
45 e, std::bind(&PSUManager::analyze, this), interval);
46
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +000047 validationTimer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
48 e, std::bind(&PSUManager::validateConfig, this));
49
Brandon Wyman510acaa2020-11-05 18:32:04 -060050 // Subscribe to power state changes
51 powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
52 powerOnMatch = std::make_unique<sdbusplus::bus::match_t>(
53 bus,
54 sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH,
55 POWER_IFACE),
56 [this](auto& msg) { this->powerStateChanged(msg); });
57
58 initialize();
59}
60
Brandon Wyman510acaa2020-11-05 18:32:04 -060061void PSUManager::getPSUConfiguration()
62{
63 using namespace phosphor::power::util;
64 auto depth = 0;
65 auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
66
67 psus.clear();
68
69 // I should get a map of objects back.
70 // Each object will have a path, a service, and an interface.
71 // The interface should match the one passed into this function.
72 for (const auto& [path, services] : objects)
73 {
74 auto service = services.begin()->first;
75
76 if (path.empty() || service.empty())
77 {
78 continue;
79 }
80
81 // For each object in the array of objects, I want to get properties
82 // from the service, path, and interface.
83 auto properties =
84 getAllProperties(bus, path, IBMCFFPSInterface, service);
85
86 getPSUProperties(properties);
87 }
88
89 if (psus.empty())
90 {
91 // Interface or properties not found. Let the Interfaces Added callback
92 // process the information once the interfaces are added to D-Bus.
93 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
94 }
95}
96
97void PSUManager::getPSUProperties(util::DbusPropertyMap& properties)
98{
99 // From passed in properties, I want to get: I2CBus, I2CAddress,
100 // and Name. Create a power supply object, using Name to build the inventory
101 // path.
102 const auto basePSUInvPath =
103 "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply";
104 uint64_t* i2cbus = nullptr;
105 uint64_t* i2caddr = nullptr;
106 std::string* psuname = nullptr;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000107 std::string* preslineptr = nullptr;
Brandon Wyman510acaa2020-11-05 18:32:04 -0600108
109 for (const auto& property : properties)
110 {
111 try
112 {
113 if (property.first == i2cBusProp)
114 {
115 i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]);
116 }
117 else if (property.first == i2cAddressProp)
118 {
119 i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]);
120 }
121 else if (property.first == psuNameProp)
122 {
123 psuname = std::get_if<std::string>(&properties[psuNameProp]);
124 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000125 else if (property.first == presLineName)
126 {
127 preslineptr =
128 std::get_if<std::string>(&properties[presLineName]);
129 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600130 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500131 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000132 {}
Brandon Wyman510acaa2020-11-05 18:32:04 -0600133 }
134
135 if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty()))
136 {
137 std::string invpath = basePSUInvPath;
138 invpath.push_back(psuname->back());
B. J. Wyman681b2a32021-04-20 22:31:22 +0000139 std::string presline = "";
Brandon Wyman510acaa2020-11-05 18:32:04 -0600140
141 log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str());
142
B. J. Wyman681b2a32021-04-20 22:31:22 +0000143 if (nullptr != preslineptr)
144 {
145 presline = *preslineptr;
146 }
147
Brandon Wymanecbecbc2021-08-31 22:53:21 +0000148 auto invMatch =
149 std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) {
150 return psu->getInventoryPath() == invpath;
151 });
152 if (invMatch != psus.end())
153 {
154 // This power supply has the same inventory path as the one with
155 // information just added to D-Bus.
156 // Changes to GPIO line name unlikely, so skip checking.
157 // Changes to the I2C bus and address unlikely, as that would
158 // require corresponding device tree updates.
159 // Return out to avoid duplicate object creation.
160 return;
161 }
162
B. J. Wyman681b2a32021-04-20 22:31:22 +0000163 log<level::DEBUG>(
164 fmt::format("make PowerSupply bus: {} addr: {} presline: {}",
165 *i2cbus, *i2caddr, presline)
166 .c_str());
167 auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus,
168 *i2caddr, presline);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600169 psus.emplace_back(std::move(psu));
170 }
171
172 if (psus.empty())
173 {
174 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
175 }
176}
177
Adriana Kobylake1074d82021-03-16 20:46:44 +0000178void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties)
179{
180 try
181 {
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000182 auto propIt = properties.find("SupportedType");
183 if (propIt == properties.end())
184 {
185 return;
186 }
187 const std::string* type = std::get_if<std::string>(&(propIt->second));
188 if ((type == nullptr) || (*type != "PowerSupply"))
189 {
190 return;
191 }
192
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000193 propIt = properties.find("SupportedModel");
194 if (propIt == properties.end())
195 {
196 return;
197 }
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000198 const std::string* model = std::get_if<std::string>(&(propIt->second));
199 if (model == nullptr)
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000200 {
201 return;
202 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000203
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000204 sys_properties sys;
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000205 propIt = properties.find("RedundantCount");
Adriana Kobylake1074d82021-03-16 20:46:44 +0000206 if (propIt != properties.end())
207 {
208 const uint64_t* count = std::get_if<uint64_t>(&(propIt->second));
209 if (count != nullptr)
210 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000211 sys.powerSupplyCount = *count;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000212 }
213 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000214 propIt = properties.find("InputVoltage");
215 if (propIt != properties.end())
216 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000217 const std::vector<uint64_t>* voltage =
218 std::get_if<std::vector<uint64_t>>(&(propIt->second));
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000219 if (voltage != nullptr)
220 {
221 sys.inputVoltage = *voltage;
222 }
223 }
224
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000225 supportedConfigs.emplace(*model, sys);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000226 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500227 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000228 {}
Adriana Kobylake1074d82021-03-16 20:46:44 +0000229}
230
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600231void PSUManager::getSystemProperties()
232{
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600233
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600234 try
235 {
236 util::DbusSubtree subtree =
237 util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000238 if (subtree.empty())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600239 {
240 throw std::runtime_error("Supported Configuration Not Found");
241 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600242
Adriana Kobylake1074d82021-03-16 20:46:44 +0000243 for (const auto& [objPath, services] : subtree)
244 {
245 std::string service = services.begin()->first;
246 if (objPath.empty() || service.empty())
247 {
248 continue;
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600249 }
Adriana Kobylake1074d82021-03-16 20:46:44 +0000250 auto properties = util::getAllProperties(
251 bus, objPath, supportedConfIntf, service);
252 populateSysProperties(properties);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600253 }
254 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500255 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600256 {
257 // Interface or property not found. Let the Interfaces Added callback
258 // process the information once the interfaces are added to D-Bus.
259 }
260}
261
Brandon Wyman3e429132021-03-18 18:03:14 -0500262void PSUManager::entityManagerIfaceAdded(sdbusplus::message::message& msg)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600263{
264 try
265 {
266 sdbusplus::message::object_path objPath;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000267 std::map<std::string, std::map<std::string, util::DbusVariant>>
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600268 interfaces;
269 msg.read(objPath, interfaces);
270
271 auto itIntf = interfaces.find(supportedConfIntf);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600272 if (itIntf != interfaces.cend())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600273 {
Brandon Wyman510acaa2020-11-05 18:32:04 -0600274 populateSysProperties(itIntf->second);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600275 }
276
Brandon Wyman510acaa2020-11-05 18:32:04 -0600277 itIntf = interfaces.find(IBMCFFPSInterface);
278 if (itIntf != interfaces.cend())
279 {
280 log<level::INFO>(
281 fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface)
282 .c_str());
283 getPSUProperties(itIntf->second);
284 }
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000285
286 // Call to validate the psu configuration if the power is on and both
287 // the IBMCFFPSConnector and SupportedConfiguration interfaces have been
288 // processed
289 if (powerOn && !psus.empty() && !supportedConfigs.empty())
290 {
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000291 validationTimer->restartOnce(validationTimeout);
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000292 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600293 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500294 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600295 {
296 // Ignore, the property may be of a different type than expected.
297 }
298}
299
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500300void PSUManager::powerStateChanged(sdbusplus::message::message& msg)
301{
302 int32_t state = 0;
303 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500304 std::map<std::string, std::variant<int32_t>> msgData;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500305 msg.read(msgSensor, msgData);
306
307 // Check if it was the Present property that changed.
308 auto valPropMap = msgData.find("state");
309 if (valPropMap != msgData.end())
310 {
311 state = std::get<int32_t>(valPropMap->second);
312
313 // Power is on when state=1. Clear faults.
314 if (state)
315 {
316 powerOn = true;
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000317 validationTimer->restartOnce(validationTimeout);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500318 clearFaults();
319 }
320 else
321 {
322 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000323 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500324 }
325 }
326}
327
Brandon Wyman8b662882021-10-08 17:31:51 +0000328void PSUManager::createError(const std::string& faultName,
329 std::map<std::string, std::string>& additionalData)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500330{
331 using namespace sdbusplus::xyz::openbmc_project;
332 constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
333 constexpr auto loggingCreateInterface =
334 "xyz.openbmc_project.Logging.Create";
335
336 try
337 {
Brandon Wyman8b662882021-10-08 17:31:51 +0000338 additionalData["_PID"] = std::to_string(getpid());
339
Brandon Wymanb76ab242020-09-16 18:06:06 -0500340 auto service =
341 util::getService(loggingObjectPath, loggingCreateInterface, bus);
342
343 if (service.empty())
344 {
345 log<level::ERR>("Unable to get logging manager service");
346 return;
347 }
348
349 auto method = bus.new_method_call(service.c_str(), loggingObjectPath,
350 loggingCreateInterface, "Create");
351
352 auto level = Logging::server::Entry::Level::Error;
353 method.append(faultName, level, additionalData);
354
355 auto reply = bus.call(method);
356 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500357 catch (const std::exception& e)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500358 {
359 log<level::ERR>(
360 fmt::format(
361 "Failed creating event log for fault {} due to error {}",
362 faultName, e.what())
363 .c_str());
364 }
365}
366
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500367void PSUManager::analyze()
368{
369 for (auto& psu : psus)
370 {
371 psu->analyze();
372 }
373
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600374 if (powerOn)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500375 {
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000376 std::map<std::string, std::string> additionalData;
377 auto requiredPSUsPresent = hasRequiredPSUs(additionalData);
378
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600379 for (auto& psu : psus)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500380 {
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600381 // TODO: Fault priorities #918
382 if (!psu->isFaultLogged() && !psu->isPresent())
383 {
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000384 if (!requiredPSUsPresent)
385 {
386 // Create error for power supply missing.
387 additionalData["CALLOUT_INVENTORY_PATH"] =
388 psu->getInventoryPath();
389 additionalData["CALLOUT_PRIORITY"] = "H";
390 createError(
391 "xyz.openbmc_project.Power.PowerSupply.Error.Missing",
392 additionalData);
393 }
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600394 psu->setFaultLogged();
395 }
396 else if (!psu->isFaultLogged() && psu->isFaulted())
397 {
Brandon Wyman786b6f42021-10-12 20:21:41 +0000398 // Add STATUS_WORD and STATUS_MFR last response, in padded
399 // hexadecimal format.
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600400 additionalData["STATUS_WORD"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000401 fmt::format("{:#04x}", psu->getStatusWord());
Jay Meyer10d94052020-11-30 14:41:21 -0600402 additionalData["STATUS_MFR"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000403 fmt::format("{:#02x}", psu->getMFRFault());
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600404 // If there are faults being reported, they possibly could be
405 // related to a bug in the firmware version running on the power
406 // supply. Capture that data into the error as well.
407 additionalData["FW_VERSION"] = psu->getFWVersion();
408
409 if ((psu->hasInputFault() || psu->hasVINUVFault()))
410 {
411 /* The power supply location might be needed if the input
412 * fault is due to a problem with the power supply itself.
413 * Include the inventory path with a call out priority of
414 * low.
415 */
416 additionalData["CALLOUT_INVENTORY_PATH"] =
417 psu->getInventoryPath();
418 additionalData["CALLOUT_PRIORITY"] = "L";
419 createError("xyz.openbmc_project.Power.PowerSupply.Error."
420 "InputFault",
421 additionalData);
422 psu->setFaultLogged();
423 }
424 else if (psu->hasMFRFault())
425 {
426 /* This can represent a variety of faults that result in
427 * calling out the power supply for replacement: Output
428 * OverCurrent, Output Under Voltage, and potentially other
429 * faults.
430 *
431 * Also plan on putting specific fault in AdditionalData,
432 * along with register names and register values
433 * (STATUS_WORD, STATUS_MFR, etc.).*/
434
435 additionalData["CALLOUT_INVENTORY_PATH"] =
436 psu->getInventoryPath();
437
438 createError(
439 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
Brandon Wyman52e54e82020-10-08 14:44:58 -0500440 additionalData);
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500441
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600442 psu->setFaultLogged();
443 }
444 else if (psu->hasCommFault())
445 {
446 /* Attempts to communicate with the power supply have
447 * reached there limit. Create an error. */
448 additionalData["CALLOUT_DEVICE_PATH"] =
449 psu->getDevicePath();
Brandon Wymanb76ab242020-09-16 18:06:06 -0500450
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600451 createError(
452 "xyz.openbmc_project.Power.PowerSupply.Error.CommFault",
453 additionalData);
Brandon Wymanb76ab242020-09-16 18:06:06 -0500454
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600455 psu->setFaultLogged();
456 }
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500457 }
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500458 }
459 }
460}
461
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000462void PSUManager::validateConfig()
463{
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000464 if (!runValidateConfig || supportedConfigs.empty())
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000465 {
466 return;
467 }
468
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000469 std::map<std::string, std::string> additionalData;
470 auto supported = hasRequiredPSUs(additionalData);
471 if (supported)
472 {
473 runValidateConfig = false;
474 return;
475 }
476
477 // Validation failed, create an error log.
478 // Return without setting the runValidateConfig flag to false because
479 // it may be that an additional supported configuration interface is
480 // added and we need to validate it to see if it matches this system.
481 createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported",
482 additionalData);
483}
484
485bool PSUManager::hasRequiredPSUs(
486 std::map<std::string, std::string>& additionalData)
487{
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000488 // Check that all PSUs have the same model name. Initialize the model
489 // variable with the first PSU name found, then use it as a base to compare
490 // against the rest of the PSUs.
491 std::string model{};
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000492 for (const auto& psu : psus)
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000493 {
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000494 auto psuModel = psu->getModelName();
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000495 if (psuModel.empty())
496 {
497 continue;
498 }
499 if (model.empty())
500 {
501 model = psuModel;
502 continue;
503 }
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000504 if (psuModel != model)
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000505 {
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000506 additionalData["EXPECTED_MODEL"] = model;
507 additionalData["ACTUAL_MODEL"] = psuModel;
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000508 additionalData["CALLOUT_INVENTORY_PATH"] = psu->getInventoryPath();
509 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000510 }
511 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000512
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000513 auto presentCount =
514 std::count_if(psus.begin(), psus.end(),
515 [](const auto& psu) { return psu->isPresent(); });
516
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000517 // Validate the supported configurations. A system may support more than one
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000518 // power supply model configuration. Since all configurations need to be
519 // checked, the additional data would contain only the information of the
520 // last configuration that did not match.
521 std::map<std::string, std::string> tmpAdditionalData;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000522 for (const auto& config : supportedConfigs)
523 {
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000524 if (config.first != model)
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000525 {
526 continue;
527 }
528 if (presentCount != config.second.powerSupplyCount)
529 {
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000530 tmpAdditionalData.clear();
531 tmpAdditionalData["EXPECTED_COUNT"] =
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000532 std::to_string(config.second.powerSupplyCount);
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000533 tmpAdditionalData["ACTUAL_COUNT"] = std::to_string(presentCount);
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000534 continue;
535 }
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000536
537 bool voltageValidated = true;
538 for (const auto& psu : psus)
539 {
540 if (!psu->isPresent())
541 {
542 // Only present PSUs report a valid input voltage
543 continue;
544 }
545
546 double actualInputVoltage;
547 int inputVoltage;
548 psu->getInputVoltage(actualInputVoltage, inputVoltage);
549
550 if (std::find(config.second.inputVoltage.begin(),
551 config.second.inputVoltage.end(),
552 inputVoltage) == config.second.inputVoltage.end())
553 {
554 tmpAdditionalData.clear();
555 tmpAdditionalData["ACTUAL_VOLTAGE"] =
556 std::to_string(actualInputVoltage);
557 for (const auto& voltage : config.second.inputVoltage)
558 {
559 tmpAdditionalData["EXPECTED_VOLTAGE"] +=
560 std::to_string(voltage) + " ";
561 }
562 tmpAdditionalData["CALLOUT_INVENTORY_PATH"] =
563 psu->getInventoryPath();
564
565 voltageValidated = false;
566 break;
567 }
568 }
569 if (!voltageValidated)
570 {
571 continue;
572 }
573
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000574 return true;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000575 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000576
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000577 additionalData.insert(tmpAdditionalData.begin(), tmpAdditionalData.end());
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000578 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000579}
580
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500581} // namespace phosphor::power::manager