-
Notifications
You must be signed in to change notification settings - Fork 23
/
discover_system_state.cpp
249 lines (216 loc) · 9.24 KB
/
discover_system_state.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
#include "config.h"
#include "host_state_manager.hpp"
#include "settings.hpp"
#include "utils.hpp"
#include "xyz/openbmc_project/Common/error.hpp"
#include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
#include <getopt.h>
#include <systemd/sd-bus.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/server.hpp>
#include <xyz/openbmc_project/State/BMC/client.hpp>
#include <xyz/openbmc_project/State/Host/client.hpp>
#include <filesystem>
#include <iostream>
#include <map>
#include <string>
#include <thread>
namespace phosphor
{
namespace state
{
namespace manager
{
PHOSPHOR_LOG2_USING;
using namespace phosphor::logging;
using namespace sdbusplus::xyz::openbmc_project::Common::Error;
using namespace sdbusplus::server::xyz::openbmc_project::control::power;
using HostState = sdbusplus::client::xyz::openbmc_project::state::Host<>;
using BMCState = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
} // namespace manager
} // namespace state
} // namespace phosphor
int main(int argc, char** argv)
{
using namespace phosphor::logging;
size_t hostId = 0;
int arg;
int optIndex = 0;
static struct option longOpts[] = {
{"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
{
switch (arg)
{
case 'h':
hostId = std::stoul(optarg);
break;
default:
break;
}
}
using Host = sdbusplus::client::xyz::openbmc_project::state::Host<>;
std::string hostPath =
std::string(Host::namespace_path::value) + "/" +
std::string(Host::namespace_path::host) + std::to_string(hostId);
auto bus = sdbusplus::bus::new_default();
using namespace settings;
HostObjects settings(bus, hostId);
using namespace phosphor::state::manager;
namespace server = sdbusplus::server::xyz::openbmc_project::state;
// This application is only run if chassis power is off
// If the BMC was rebooted due to a user initiated pinhole reset, do not
// implement any power restore policies
using BMC = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
auto bmcPath = sdbusplus::message::object_path(BMC::namespace_path::value) /
BMC::namespace_path::bmc;
auto bmcRebootCause =
sdbusplus::message::convert_from_string<BMC::RebootCause>(
phosphor::state::manager::utils::getProperty(
bus, bmcPath.str, BMCState::interface, "LastRebootCause"));
if (bmcRebootCause == BMC::RebootCause::PinholeReset)
{
info(
"BMC was reset due to pinhole reset, no power restore policy will be run");
return 0;
}
else if (bmcRebootCause == BMC::RebootCause::Watchdog)
{
info(
"BMC was reset due to cold reset, no power restore policy will be run");
return 0;
}
/* The logic here is to first check the one-time PowerRestorePolicy setting.
* If this property is not the default then look at the persistent
* user setting in the non one-time object, otherwise honor the one-time
* setting.
*/
auto methodOneTime = bus.new_method_call(
settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
settings.powerRestorePolicyOneTime.c_str(),
"org.freedesktop.DBus.Properties", "Get");
methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy");
auto methodUserSetting = bus.new_method_call(
settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
"Get");
methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
std::variant<std::string> result;
try
{
auto reply = bus.call(methodOneTime);
reply.read(result);
auto powerPolicy = std::get<std::string>(result);
if (RestorePolicy::Policy::None ==
RestorePolicy::convertPolicyFromString(powerPolicy))
{
// one_time is set to None so use the customer setting
info("One time not set, check user setting of power policy");
auto reply = bus.call(methodUserSetting);
reply.read(result);
powerPolicy = std::get<std::string>(result);
}
else
{
// one_time setting was set so we're going to use it. Reset it
// to default for next time.
info("One time set, use it and reset to default");
phosphor::state::manager::utils::setProperty(
bus, settings.powerRestorePolicyOneTime, powerRestoreIntf,
"PowerRestorePolicy",
convertForMessage(RestorePolicy::Policy::None));
}
auto methodUserSettingDelay = bus.new_method_call(
settings.service(settings.powerRestorePolicy, powerRestoreIntf)
.c_str(),
settings.powerRestorePolicy.c_str(),
"org.freedesktop.DBus.Properties", "Get");
methodUserSettingDelay.append(powerRestoreIntf, "PowerRestoreDelay");
std::variant<uint64_t> restoreDelay;
auto delayResult = bus.call(methodUserSettingDelay);
delayResult.read(restoreDelay);
auto powerRestoreDelayUsec =
std::chrono::microseconds(std::get<uint64_t>(restoreDelay));
auto powerRestoreDelaySec =
std::chrono::duration_cast<std::chrono::seconds>(
powerRestoreDelayUsec);
info("Host power is off, processing power policy {POWER_POLICY}",
"POWER_POLICY", powerPolicy);
if (RestorePolicy::Policy::AlwaysOn ==
RestorePolicy::convertPolicyFromString(powerPolicy))
{
info(
"power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)",
"DELAY", powerRestoreDelaySec.count());
utils::waitBmcReady(bus, powerRestoreDelaySec);
phosphor::state::manager::utils::setProperty(
bus, hostPath, HostState::interface, "RestartCause",
convertForMessage(
server::Host::RestartCause::PowerPolicyAlwaysOn));
phosphor::state::manager::utils::setProperty(
bus, hostPath, HostState::interface, "RequestedHostTransition",
convertForMessage(server::Host::Transition::On));
}
// Always execute power on if AlwaysOn is set, otherwise check config
// option (and AC loss status) on whether to execute other policy
// settings
#if ONLY_RUN_APR_ON_POWER_LOSS
else if (!phosphor::state::manager::utils::checkACLoss(hostId))
{
info(
"Chassis power was not on prior to BMC reboot so do not run any further power policy");
return 0;
}
#endif
else if (RestorePolicy::Policy::AlwaysOff ==
RestorePolicy::convertPolicyFromString(powerPolicy))
{
info(
"power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
"DELAY", powerRestoreDelaySec.count());
utils::waitBmcReady(bus, powerRestoreDelaySec);
// Read last requested state and re-request it to execute it
auto hostReqState = phosphor::state::manager::utils::getProperty(
bus, hostPath, HostState::interface, "RequestedHostTransition");
if (hostReqState !=
convertForMessage(server::Host::Transition::Off))
{
phosphor::state::manager::utils::setProperty(
bus, hostPath, HostState::interface,
"RequestedHostTransition",
convertForMessage(server::Host::Transition::Off));
}
}
else if (RestorePolicy::Policy::Restore ==
RestorePolicy::convertPolicyFromString(powerPolicy))
{
info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
"DELAY", powerRestoreDelaySec.count());
utils::waitBmcReady(bus, powerRestoreDelaySec);
// Read last requested state and re-request it to execute it
auto hostReqState = phosphor::state::manager::utils::getProperty(
bus, hostPath, HostState::interface, "RequestedHostTransition");
// As long as the host transition is not 'Off' power on host state.
if (hostReqState !=
convertForMessage(server::Host::Transition::Off))
{
phosphor::state::manager::utils::setProperty(
bus, hostPath, HostState::interface, "RestartCause",
convertForMessage(
server::Host::RestartCause::PowerPolicyPreviousState));
phosphor::state::manager::utils::setProperty(
bus, hostPath, HostState::interface,
"RequestedHostTransition",
convertForMessage(server::Host::Transition::On));
}
}
}
catch (const sdbusplus::exception_t& e)
{
error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
elog<InternalFailure>();
}
return 0;
}