forked from openbmc/phosphor-state-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chassis_state_manager.cpp
797 lines (689 loc) · 25.3 KB
/
chassis_state_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#include "config.h"
#include "chassis_state_manager.hpp"
#include "utils.hpp"
#include "xyz/openbmc_project/Common/error.hpp"
#include "xyz/openbmc_project/State/Shutdown/Power/error.hpp"
#include <fmt/format.h>
#include <fmt/printf.h>
#include <cereal/archives/json.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/exception.hpp>
#include <sdeventplus/event.hpp>
#include <sdeventplus/exception.hpp>
#include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp>
#include <filesystem>
#include <fstream>
namespace phosphor
{
namespace state
{
namespace manager
{
PHOSPHOR_LOG2_USING;
// When you see server:: you know we're referencing our base class
namespace server = sdbusplus::xyz::openbmc_project::State::server;
namespace decoratorServer =
sdbusplus::xyz::openbmc_project::State::Decorator::server;
using namespace phosphor::logging;
using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Blackout;
using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Regulator;
constexpr auto CHASSIS_STATE_POWEROFF_TGT_FMT =
"obmc-chassis-poweroff@{}.target";
constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT_FMT =
"obmc-chassis-hard-poweroff@{}.target";
constexpr auto CHASSIS_STATE_POWERON_TGT_FMT = "obmc-chassis-poweron@{}.target";
constexpr auto CHASSIS_BLACKOUT_TGT_FMT = "obmc-chassis-blackout@{}.target";
constexpr auto CHASSIS_STATE_POWERCYCLE_TGT_FMT =
"obmc-chassis-powercycle@{}.target";
constexpr auto AUTO_POWER_RESTORE_SVC_FMT =
"phosphor-discover-system-state@{}.service";
constexpr auto ACTIVE_STATE = "active";
constexpr auto ACTIVATING_STATE = "activating";
// Details at https://upower.freedesktop.org/docs/Device.html
constexpr uint TYPE_UPS = 3;
constexpr uint STATE_FULLY_CHARGED = 4;
constexpr uint BATTERY_LVL_FULL = 8;
constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
constexpr auto UPOWER_INTERFACE = "org.freedesktop.UPower.Device";
constexpr auto POWERSYSINPUTS_INTERFACE =
"xyz.openbmc_project.State.Decorator.PowerSystemInputs";
constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
void Chassis::createSystemdTargetTable()
{
systemdTargetTable = {
// Use the hard off target to ensure we shutdown immediately
{Transition::Off, fmt::format(CHASSIS_STATE_HARD_POWEROFF_TGT_FMT, id)},
{Transition::On, fmt::format(CHASSIS_STATE_POWERON_TGT_FMT, id)},
{Transition::PowerCycle,
fmt::format(CHASSIS_STATE_POWERCYCLE_TGT_FMT, id)}};
}
// TODO - Will be rewritten once sdbusplus client bindings are in place
// and persistent storage design is in place and sdbusplus
// has read property function
void Chassis::determineInitialState()
{
// Monitor for any properties changed signals on UPower device path
uPowerPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
bus,
sdbusplus::bus::match::rules::propertiesChangedNamespace(
"/org/freedesktop/UPower", UPOWER_INTERFACE),
[this](auto& msg) { this->uPowerChangeEvent(msg); });
// Monitor for any properties changed signals on PowerSystemInputs
powerSysInputsPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
bus,
sdbusplus::bus::match::rules::propertiesChangedNamespace(
fmt::format(
"/xyz/openbmc_project/power/power_supplies/chassis{}/psus", id),
POWERSYSINPUTS_INTERFACE),
[this](auto& msg) { this->powerSysInputsChangeEvent(msg); });
determineStatusOfPower();
std::variant<int> pgood = -1;
auto method = this->bus.new_method_call(
"org.openbmc.control.Power", "/org/openbmc/control/power0",
"org.freedesktop.DBus.Properties", "Get");
method.append("org.openbmc.control.Power", "pgood");
try
{
auto reply = this->bus.call(method);
reply.read(pgood);
if (std::get<int>(pgood) == 1)
{
info("Initial Chassis State will be On");
server::Chassis::currentPowerState(PowerState::On);
server::Chassis::requestedPowerTransition(Transition::On);
return;
}
else
{
// The system is off. If we think it should be on then
// we probably lost AC while up, so set a new state
// change time.
uint64_t lastTime;
PowerState lastState;
if (deserializeStateChangeTime(lastTime, lastState))
{
// If power was on before the BMC reboot and the reboot reason
// was not a pinhole reset, log an error
if (lastState == PowerState::On)
{
info(
"Chassis power was on before the BMC reboot and it is off now");
// Reset host sensors since system is off now
// Ensure Power Leds are off.
startUnit(fmt::format(CHASSIS_BLACKOUT_TGT_FMT, id));
setStateChangeTime();
// Generate file indicating AC loss occurred
std::string chassisLostPowerFileFmt =
fmt::sprintf(CHASSIS_LOST_POWER_FILE, id);
fs::create_directories(BASE_FILE_DIR);
fs::path chassisPowerLossFile{chassisLostPowerFileFmt};
std::ofstream outfile(chassisPowerLossFile);
outfile.close();
// 0 indicates pinhole reset. 1 is NOT pinhole reset
if (phosphor::state::manager::utils::getGpioValue(
"reset-cause-pinhole") != 0)
{
if (standbyVoltageRegulatorFault())
{
report<Regulator>();
}
else
{
report<Blackout>(Entry::Level::Critical);
}
}
else
{
info("Pinhole reset");
}
}
}
}
}
catch (const sdbusplus::exception_t& e)
{
// It's acceptable for the pgood state service to not be available
// since it will notify us of the pgood state when it comes up.
if (e.name() != nullptr &&
strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0)
{
goto fail;
}
// Only log for unexpected error types.
error("Error performing call to get pgood: {ERROR}", "ERROR", e);
goto fail;
}
fail:
info("Initial Chassis State will be Off");
server::Chassis::currentPowerState(PowerState::Off);
server::Chassis::requestedPowerTransition(Transition::Off);
return;
}
void Chassis::determineStatusOfPower()
{
auto initialPowerStatus = server::Chassis::currentPowerStatus();
bool powerGood = determineStatusOfUPSPower();
if (!powerGood)
{
return;
}
powerGood = determineStatusOfPSUPower();
if (powerGood)
{
// All checks passed, set power status to good
server::Chassis::currentPowerStatus(PowerStatus::Good);
// If power status transitioned from bad to good and chassis power is
// off then call Auto Power Restart to see if the system should auto
// power on now that power status is good
if ((initialPowerStatus != PowerStatus::Good) &&
(server::Chassis::currentPowerState() == PowerState::Off))
{
info("power status transitioned from {START_PWR_STATE} to Good and "
"chassis power is off, calling APR",
"START_PWR_STATE", initialPowerStatus);
restartUnit(fmt::format(AUTO_POWER_RESTORE_SVC_FMT, this->id));
}
}
}
bool Chassis::determineStatusOfUPSPower()
{
// Find all implementations of the UPower interface
auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
MAPPER_INTERFACE, "GetSubTree");
mapper.append("/", 0, std::vector<std::string>({UPOWER_INTERFACE}));
std::map<std::string, std::map<std::string, std::vector<std::string>>>
mapperResponse;
try
{
auto mapperResponseMsg = bus.call(mapper);
mapperResponseMsg.read(mapperResponse);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in mapper GetSubTree call for UPS: {ERROR}", "ERROR", e);
throw;
}
if (mapperResponse.empty())
{
debug("No UPower devices found in system");
}
// Iterate through all returned Upower interfaces and look for UPS's
for (const auto& [path, services] : mapperResponse)
{
for (const auto& serviceIter : services)
{
const std::string& service = serviceIter.first;
try
{
auto method = bus.new_method_call(service.c_str(), path.c_str(),
PROPERTY_INTERFACE, "GetAll");
method.append(UPOWER_INTERFACE);
auto response = bus.call(method);
using Property = std::string;
using Value = std::variant<bool, uint>;
using PropertyMap = std::map<Property, Value>;
PropertyMap properties;
response.read(properties);
if (std::get<uint>(properties["Type"]) != TYPE_UPS)
{
info("UPower device {OBJ_PATH} is not a UPS device",
"OBJ_PATH", path);
continue;
}
if (std::get<bool>(properties["IsPresent"]) != true)
{
// There is a UPS detected but it is not officially
// "present" yet. Monitor it for state change.
info("UPower device {OBJ_PATH} is not present", "OBJ_PATH",
path);
continue;
}
if (std::get<uint>(properties["State"]) == STATE_FULLY_CHARGED)
{
info("UPS is fully charged");
}
else
{
info("UPS is not fully charged: {UPS_STATE}", "UPS_STATE",
std::get<uint>(properties["State"]));
server::Chassis::currentPowerStatus(
PowerStatus::UninterruptiblePowerSupply);
return false;
}
if (std::get<uint>(properties["BatteryLevel"]) ==
BATTERY_LVL_FULL)
{
info("UPS Battery Level is Full");
// Only one UPS per system, we've found it and it's all
// good so exit function
return true;
}
else
{
info("UPS Battery Level is Low: {UPS_BAT_LEVEL}",
"UPS_BAT_LEVEL",
std::get<uint>(properties["BatteryLevel"]));
server::Chassis::currentPowerStatus(
PowerStatus::UninterruptiblePowerSupply);
return false;
}
}
catch (const sdbusplus::exception_t& e)
{
error("Error reading UPS property, error: {ERROR}, "
"service: {SERVICE} path: {PATH}",
"ERROR", e, "SERVICE", service, "PATH", path);
throw;
}
}
}
return true;
}
bool Chassis::determineStatusOfPSUPower()
{
// Find all implementations of the PowerSystemInputs interface
auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
MAPPER_INTERFACE, "GetSubTree");
mapper.append("/", 0, std::vector<std::string>({POWERSYSINPUTS_INTERFACE}));
std::map<std::string, std::map<std::string, std::vector<std::string>>>
mapperResponse;
try
{
auto mapperResponseMsg = bus.call(mapper);
mapperResponseMsg.read(mapperResponse);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in mapper GetSubTree call for PowerSystemInputs: {ERROR}",
"ERROR", e);
throw;
}
for (const auto& [path, services] : mapperResponse)
{
for (const auto& serviceIter : services)
{
const std::string& service = serviceIter.first;
try
{
auto method = bus.new_method_call(service.c_str(), path.c_str(),
PROPERTY_INTERFACE, "GetAll");
method.append(POWERSYSINPUTS_INTERFACE);
auto response = bus.call(method);
using Property = std::string;
using Value = std::variant<std::string>;
using PropertyMap = std::map<Property, Value>;
PropertyMap properties;
response.read(properties);
auto statusStr = std::get<std::string>(properties["Status"]);
auto status =
decoratorServer::PowerSystemInputs::convertStatusFromString(
statusStr);
if (status == decoratorServer::PowerSystemInputs::Status::Fault)
{
info("Power System Inputs status is in Fault state");
server::Chassis::currentPowerStatus(PowerStatus::BrownOut);
return false;
}
}
catch (const sdbusplus::exception_t& e)
{
error(
"Error reading Power System Inputs property, error: {ERROR}, "
"service: {SERVICE} path: {PATH}",
"ERROR", e, "SERVICE", service, "PATH", path);
throw;
}
}
}
return true;
}
void Chassis::uPowerChangeEvent(sdbusplus::message_t& msg)
{
debug("UPS Property Change Event Triggered");
std::string statusInterface;
std::map<std::string, std::variant<uint, bool>> msgData;
msg.read(statusInterface, msgData);
// If the change is to any of the properties we are interested in, then call
// determineStatusOfPower(), which looks at all the power-related
// interfaces, to see if a power status change is needed
auto propertyMap = msgData.find("IsPresent");
if (propertyMap != msgData.end())
{
info("UPS presence changed to {UPS_PRES_INFO}", "UPS_PRES_INFO",
std::get<bool>(propertyMap->second));
determineStatusOfPower();
return;
}
propertyMap = msgData.find("State");
if (propertyMap != msgData.end())
{
info("UPS State changed to {UPS_STATE}", "UPS_STATE",
std::get<uint>(propertyMap->second));
determineStatusOfPower();
return;
}
propertyMap = msgData.find("BatteryLevel");
if (propertyMap != msgData.end())
{
info("UPS BatteryLevel changed to {UPS_BAT_LEVEL}", "UPS_BAT_LEVEL",
std::get<uint>(propertyMap->second));
determineStatusOfPower();
return;
}
return;
}
void Chassis::powerSysInputsChangeEvent(sdbusplus::message_t& msg)
{
debug("Power System Inputs Property Change Event Triggered");
std::string statusInterface;
std::map<std::string, std::variant<std::string>> msgData;
msg.read(statusInterface, msgData);
// If the change is to any of the properties we are interested in, then call
// determineStatusOfPower(), which looks at all the power-related
// interfaces, to see if a power status change is needed
auto propertyMap = msgData.find("Status");
if (propertyMap != msgData.end())
{
info("Power System Inputs status changed to {POWER_SYS_INPUT_STATUS}",
"POWER_SYS_INPUT_STATUS",
std::get<std::string>(propertyMap->second));
determineStatusOfPower();
return;
}
return;
}
void Chassis::startUnit(const std::string& sysdUnit)
{
auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
SYSTEMD_INTERFACE, "StartUnit");
method.append(sysdUnit);
method.append("replace");
this->bus.call_noreply(method);
return;
}
void Chassis::restartUnit(const std::string& sysdUnit)
{
auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
SYSTEMD_INTERFACE, "RestartUnit");
method.append(sysdUnit);
method.append("replace");
this->bus.call_noreply(method);
return;
}
bool Chassis::stateActive(const std::string& target)
{
std::variant<std::string> currentState;
sdbusplus::message::object_path unitTargetPath;
auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
SYSTEMD_INTERFACE, "GetUnit");
method.append(target);
try
{
auto result = this->bus.call(method);
result.read(unitTargetPath);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in GetUnit call: {ERROR}", "ERROR", e);
return false;
}
method = this->bus.new_method_call(
SYSTEMD_SERVICE,
static_cast<const std::string&>(unitTargetPath).c_str(),
SYSTEMD_PROPERTY_IFACE, "Get");
method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
try
{
auto result = this->bus.call(method);
result.read(currentState);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in ActiveState Get: {ERROR}", "ERROR", e);
return false;
}
const auto& currentStateStr = std::get<std::string>(currentState);
return currentStateStr == ACTIVE_STATE ||
currentStateStr == ACTIVATING_STATE;
}
int Chassis::sysStateChange(sdbusplus::message_t& msg)
{
sdbusplus::message::object_path newStateObjPath;
std::string newStateUnit{};
std::string newStateResult{};
// Read the msg and populate each variable
try
{
// newStateID is a throwaway that is needed in order to read the
// parameters that are useful out of the dbus message
uint32_t newStateID{};
msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in state change - bad encoding: {ERROR} {REPLY_SIG}",
"ERROR", e, "REPLY_SIG", msg.get_signature());
return 0;
}
if ((newStateUnit == fmt::format(CHASSIS_STATE_POWEROFF_TGT_FMT, id)) &&
(newStateResult == "done") &&
(!stateActive(systemdTargetTable[Transition::On])))
{
info("Received signal that power OFF is complete");
this->currentPowerState(server::Chassis::PowerState::Off);
this->setStateChangeTime();
}
else if ((newStateUnit == systemdTargetTable[Transition::On]) &&
(newStateResult == "done") &&
(stateActive(systemdTargetTable[Transition::On])))
{
info("Received signal that power ON is complete");
this->currentPowerState(server::Chassis::PowerState::On);
this->setStateChangeTime();
// Remove temporary file which is utilized for scenarios where the
// BMC is rebooted while the chassis power is still on.
// This file is used to indicate to chassis related systemd services
// that the chassis is already on and they should skip running.
// Once the chassis state is back to on we can clear this file.
auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
size++; // null
std::unique_ptr<char[]> chassisFile(new char[size]);
std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0);
if (std::filesystem::exists(chassisFile.get()))
{
std::filesystem::remove(chassisFile.get());
}
}
return 0;
}
Chassis::Transition Chassis::requestedPowerTransition(Transition value)
{
info("Change to Chassis Requested Power State: {REQ_POWER_TRAN}",
"REQ_POWER_TRAN", value);
startUnit(systemdTargetTable.find(value)->second);
return server::Chassis::requestedPowerTransition(value);
}
Chassis::PowerState Chassis::currentPowerState(PowerState value)
{
PowerState chassisPowerState;
info("Change to Chassis Power State: {CUR_POWER_STATE}", "CUR_POWER_STATE",
value);
chassisPowerState = server::Chassis::currentPowerState(value);
if (chassisPowerState == PowerState::On)
{
pohTimer.resetRemaining();
}
return chassisPowerState;
}
uint32_t Chassis::pohCounter(uint32_t value)
{
if (value != pohCounter())
{
ChassisInherit::pohCounter(value);
serializePOH();
}
return pohCounter();
}
void Chassis::pohCallback()
{
if (ChassisInherit::currentPowerState() == PowerState::On)
{
pohCounter(pohCounter() + 1);
}
}
void Chassis::restorePOHCounter()
{
uint32_t counter;
if (!deserializePOH(counter))
{
// set to default value
pohCounter(0);
}
else
{
pohCounter(counter);
}
}
fs::path Chassis::serializePOH()
{
fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
std::ofstream os(path.c_str(), std::ios::binary);
cereal::JSONOutputArchive oarchive(os);
oarchive(pohCounter());
return path;
}
bool Chassis::deserializePOH(uint32_t& pohCounter)
{
fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
try
{
if (fs::exists(path))
{
std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
cereal::JSONInputArchive iarchive(is);
iarchive(pohCounter);
return true;
}
return false;
}
catch (const cereal::Exception& e)
{
error("deserialize exception: {ERROR}", "ERROR", e);
fs::remove(path);
return false;
}
catch (const fs::filesystem_error& e)
{
return false;
}
return false;
}
void Chassis::startPOHCounter()
{
auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path();
fs::create_directories(dir);
try
{
auto event = sdeventplus::Event::get_default();
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
event.loop();
}
catch (const sdeventplus::SdEventError& e)
{
error("Error occurred during the sdeventplus loop: {ERROR}", "ERROR",
e);
phosphor::logging::commit<InternalFailure>();
}
}
void Chassis::serializeStateChangeTime()
{
fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
std::ofstream os(path.c_str(), std::ios::binary);
cereal::JSONOutputArchive oarchive(os);
oarchive(ChassisInherit::lastStateChangeTime(),
ChassisInherit::currentPowerState());
}
bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state)
{
fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
try
{
if (fs::exists(path))
{
std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
cereal::JSONInputArchive iarchive(is);
iarchive(time, state);
return true;
}
}
catch (const std::exception& e)
{
error("deserialize exception: {ERROR}", "ERROR", e);
fs::remove(path);
}
return false;
}
void Chassis::restoreChassisStateChangeTime()
{
uint64_t time;
PowerState state;
if (!deserializeStateChangeTime(time, state))
{
ChassisInherit::lastStateChangeTime(0);
}
else
{
ChassisInherit::lastStateChangeTime(time);
}
}
void Chassis::setStateChangeTime()
{
using namespace std::chrono;
uint64_t lastTime;
PowerState lastState;
auto now =
duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count();
// If power is on when the BMC is rebooted, this function will get called
// because sysStateChange() runs. Since the power state didn't change
// in this case, neither should the state change time, so check that
// the power state actually did change here.
if (deserializeStateChangeTime(lastTime, lastState))
{
if (lastState == ChassisInherit::currentPowerState())
{
return;
}
}
ChassisInherit::lastStateChangeTime(now);
serializeStateChangeTime();
}
bool Chassis::standbyVoltageRegulatorFault()
{
bool regulatorFault = false;
// find standby voltage regulator fault via gpiog
auto gpioval = utils::getGpioValue("regulator-standby-faulted");
if (-1 == gpioval)
{
error("Failed reading regulator-standby-faulted GPIO");
}
if (1 == gpioval)
{
info("Detected standby voltage regulator fault");
regulatorFault = true;
}
return regulatorFault;
}
} // namespace manager
} // namespace state
} // namespace phosphor