xref: /openbmc/phosphor-state-manager/discover_system_state.cpp (revision 840ac402dbe726469ad922d68f123d20b7e58e66)
1 #include "config.h"
2 
3 #include "host_state_manager.hpp"
4 #include "settings.hpp"
5 #include "utils.hpp"
6 #include "xyz/openbmc_project/Common/error.hpp"
7 #include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
8 
9 #include <getopt.h>
10 #include <systemd/sd-bus.h>
11 
12 #include <phosphor-logging/elog-errors.hpp>
13 #include <phosphor-logging/lg2.hpp>
14 #include <sdbusplus/exception.hpp>
15 #include <sdbusplus/server.hpp>
16 #include <xyz/openbmc_project/State/BMC/client.hpp>
17 #include <xyz/openbmc_project/State/Host/client.hpp>
18 
19 #include <filesystem>
20 #include <iostream>
21 #include <map>
22 #include <string>
23 #include <thread>
24 
25 namespace phosphor
26 {
27 namespace state
28 {
29 namespace manager
30 {
31 
32 PHOSPHOR_LOG2_USING;
33 
34 using namespace phosphor::logging;
35 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
36 using namespace sdbusplus::server::xyz::openbmc_project::control::power;
37 using HostState = sdbusplus::client::xyz::openbmc_project::state::Host<>;
38 using BMCState = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
39 
40 } // namespace manager
41 } // namespace state
42 } // namespace phosphor
43 
main(int argc,char ** argv)44 int main(int argc, char** argv)
45 {
46     using namespace phosphor::logging;
47 
48     size_t hostId = 0;
49     int arg;
50     int optIndex = 0;
51 
52     static struct option longOpts[] = {
53         {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
54 
55     while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
56     {
57         switch (arg)
58         {
59             case 'h':
60                 hostId = std::stoul(optarg);
61                 break;
62             default:
63                 break;
64         }
65     }
66 
67     using Host = sdbusplus::client::xyz::openbmc_project::state::Host<>;
68     std::string hostPath =
69         std::string(Host::namespace_path::value) + "/" +
70         std::string(Host::namespace_path::host) + std::to_string(hostId);
71 
72     auto bus = sdbusplus::bus::new_default();
73 
74     using namespace settings;
75     HostObjects settings(bus, hostId);
76 
77     using namespace phosphor::state::manager;
78     namespace server = sdbusplus::server::xyz::openbmc_project::state;
79 
80     // This application is only run if chassis power is off
81 
82     // If the BMC was rebooted due to a user initiated pinhole reset, do not
83     // implement any power restore policies
84     using BMC = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
85     auto bmcPath = sdbusplus::message::object_path(BMC::namespace_path::value) /
86                    BMC::namespace_path::bmc;
87 
88 #if !(RUN_APR_ON_PINHOLE_RESET && RUN_APR_ON_WATCHDOG_RESET &&                 \
89       RUN_APR_ON_SOFTWARE_RESET)
90     auto bmcRebootCause =
91         sdbusplus::message::convert_from_string<BMC::RebootCause>(
92             phosphor::state::manager::utils::getProperty(
93                 bus, bmcPath.str, BMCState::interface, "LastRebootCause"));
94 
95 #if !RUN_APR_ON_PINHOLE_RESET
96     if (bmcRebootCause == BMC::RebootCause::PinholeReset)
97     {
98         info(
99             "BMC was reset due to pinhole reset, no power restore policy will be run");
100         return 0;
101     }
102 #endif // RUN_APR_ON_PINHOLE_RESET
103 
104 #if !RUN_APR_ON_WATCHDOG_RESET
105     if (bmcRebootCause == BMC::RebootCause::Watchdog)
106     {
107         info(
108             "BMC was reset due to watchdog, no power restore policy will be run");
109         return 0;
110     }
111 #endif // RUN_APR_ON_WATCHDOG_RESET
112 
113 #if !RUN_APR_ON_SOFTWARE_RESET
114     if (bmcRebootCause == BMC::RebootCause::Software)
115     {
116         info(
117             "BMC was reset due to cold reset, no power restore policy will be run");
118         return 0;
119     }
120 #endif // RUN_APR_ON_SOFTWARE_RESET
121 #endif
122 
123     /* The logic here is to first check the one-time PowerRestorePolicy setting.
124      * If this property is not the default then look at the persistent
125      * user setting in the non one-time object, otherwise honor the one-time
126      * setting.
127      */
128     auto methodOneTime = bus.new_method_call(
129         settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
130         settings.powerRestorePolicyOneTime.c_str(),
131         "org.freedesktop.DBus.Properties", "Get");
132     methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy");
133 
134     auto methodUserSetting = bus.new_method_call(
135         settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
136         settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
137         "Get");
138     methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
139 
140     std::variant<std::string> result;
141     try
142     {
143         auto reply = bus.call(methodOneTime);
144         reply.read(result);
145         auto powerPolicy = std::get<std::string>(result);
146 
147         if (RestorePolicy::Policy::None ==
148             RestorePolicy::convertPolicyFromString(powerPolicy))
149         {
150             // one_time is set to None so use the customer setting
151             info("One time not set, check user setting of power policy");
152 
153             auto reply = bus.call(methodUserSetting);
154             reply.read(result);
155             powerPolicy = std::get<std::string>(result);
156         }
157         else
158         {
159             // one_time setting was set so we're going to use it. Reset it
160             // to default for next time.
161             info("One time set, use it and reset to default");
162             phosphor::state::manager::utils::setProperty(
163                 bus, settings.powerRestorePolicyOneTime, powerRestoreIntf,
164                 "PowerRestorePolicy",
165                 convertForMessage(RestorePolicy::Policy::None));
166         }
167 
168         auto methodUserSettingDelay = bus.new_method_call(
169             settings.service(settings.powerRestorePolicy, powerRestoreIntf)
170                 .c_str(),
171             settings.powerRestorePolicy.c_str(),
172             "org.freedesktop.DBus.Properties", "Get");
173 
174         methodUserSettingDelay.append(powerRestoreIntf, "PowerRestoreDelay");
175 
176         std::variant<uint64_t> restoreDelay;
177 
178         auto delayResult = bus.call(methodUserSettingDelay);
179         delayResult.read(restoreDelay);
180         auto powerRestoreDelayUsec =
181             std::chrono::microseconds(std::get<uint64_t>(restoreDelay));
182         auto powerRestoreDelaySec =
183             std::chrono::duration_cast<std::chrono::seconds>(
184                 powerRestoreDelayUsec);
185 
186         info(
187             "Host{HOST_ID} power is off, processing power policy {POWER_POLICY}",
188             "HOST_ID", hostId, "POWER_POLICY", powerPolicy);
189 
190         if (RestorePolicy::Policy::AlwaysOn ==
191             RestorePolicy::convertPolicyFromString(powerPolicy))
192         {
193             info(
194                 "power_policy=ALWAYS_POWER_ON, powering host{HOST_ID} on ({DELAY}s delay)",
195                 "HOST_ID", hostId, "DELAY", powerRestoreDelaySec.count());
196 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
197             utils::waitBmcReady(bus, powerRestoreDelaySec);
198 #else
199             std::this_thread::sleep_for(powerRestoreDelayUsec);
200 #endif
201             phosphor::state::manager::utils::setProperty(
202                 bus, hostPath, HostState::interface, "RestartCause",
203                 convertForMessage(
204                     server::Host::RestartCause::PowerPolicyAlwaysOn));
205             phosphor::state::manager::utils::setProperty(
206                 bus, hostPath, HostState::interface, "RequestedHostTransition",
207                 convertForMessage(server::Host::Transition::On));
208         }
209         // Always execute power on if AlwaysOn is set, otherwise check config
210         // option (and AC loss status) on whether to execute other policy
211         // settings
212 #if ONLY_RUN_APR_ON_POWER_LOSS
213         else if (!phosphor::state::manager::utils::checkACLoss(hostId))
214         {
215             info(
216                 "Chassis power was not on prior to BMC reboot so do not run any further power policy");
217             return 0;
218         }
219 #endif
220         else if (RestorePolicy::Policy::AlwaysOff ==
221                  RestorePolicy::convertPolicyFromString(powerPolicy))
222         {
223             info(
224                 "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
225                 "DELAY", powerRestoreDelaySec.count());
226 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
227             utils::waitBmcReady(bus, powerRestoreDelaySec);
228 #else
229             std::this_thread::sleep_for(powerRestoreDelayUsec);
230 #endif
231             // Read last requested state and re-request it to execute it
232             auto hostReqState = phosphor::state::manager::utils::getProperty(
233                 bus, hostPath, HostState::interface, "RequestedHostTransition");
234             if (hostReqState !=
235                 convertForMessage(server::Host::Transition::Off))
236             {
237                 phosphor::state::manager::utils::setProperty(
238                     bus, hostPath, HostState::interface,
239                     "RequestedHostTransition",
240                     convertForMessage(server::Host::Transition::Off));
241             }
242         }
243         else if (RestorePolicy::Policy::Restore ==
244                  RestorePolicy::convertPolicyFromString(powerPolicy))
245         {
246             info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
247                  "DELAY", powerRestoreDelaySec.count());
248 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
249             utils::waitBmcReady(bus, powerRestoreDelaySec);
250 #else
251             std::this_thread::sleep_for(powerRestoreDelayUsec);
252 #endif
253             // Read last requested state and re-request it to execute it
254             auto hostReqState = phosphor::state::manager::utils::getProperty(
255                 bus, hostPath, HostState::interface, "RequestedHostTransition");
256 
257             // As long as the host transition is not 'Off' power on host state.
258             if (hostReqState !=
259                 convertForMessage(server::Host::Transition::Off))
260             {
261                 phosphor::state::manager::utils::setProperty(
262                     bus, hostPath, HostState::interface, "RestartCause",
263                     convertForMessage(
264                         server::Host::RestartCause::PowerPolicyPreviousState));
265                 phosphor::state::manager::utils::setProperty(
266                     bus, hostPath, HostState::interface,
267                     "RequestedHostTransition",
268                     convertForMessage(server::Host::Transition::On));
269             }
270         }
271     }
272     catch (const sdbusplus::exception_t& e)
273     {
274         error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
275         elog<InternalFailure>();
276     }
277 
278     return 0;
279 }
280