xref: /openbmc/phosphor-state-manager/discover_system_state.cpp (revision 3d5062804ce3eb4f198ffd2bed2f4f4876af8aee)
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     auto bmcRebootCause =
89         sdbusplus::message::convert_from_string<BMC::RebootCause>(
90             phosphor::state::manager::utils::getProperty(
91                 bus, bmcPath.str, BMCState::interface, "LastRebootCause"));
92 
93     if (bmcRebootCause == BMC::RebootCause::PinholeReset)
94     {
95         info(
96             "BMC was reset due to pinhole reset, no power restore policy will be run");
97         return 0;
98     }
99     else if (bmcRebootCause == BMC::RebootCause::Watchdog)
100     {
101         info(
102             "BMC was reset due to cold reset, no power restore policy will be run");
103         return 0;
104     }
105 
106     /* The logic here is to first check the one-time PowerRestorePolicy setting.
107      * If this property is not the default then look at the persistent
108      * user setting in the non one-time object, otherwise honor the one-time
109      * setting.
110      */
111     auto methodOneTime = bus.new_method_call(
112         settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
113         settings.powerRestorePolicyOneTime.c_str(),
114         "org.freedesktop.DBus.Properties", "Get");
115     methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy");
116 
117     auto methodUserSetting = bus.new_method_call(
118         settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
119         settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
120         "Get");
121     methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
122 
123     std::variant<std::string> result;
124     try
125     {
126         auto reply = bus.call(methodOneTime);
127         reply.read(result);
128         auto powerPolicy = std::get<std::string>(result);
129 
130         if (RestorePolicy::Policy::None ==
131             RestorePolicy::convertPolicyFromString(powerPolicy))
132         {
133             // one_time is set to None so use the customer setting
134             info("One time not set, check user setting of power policy");
135 
136             auto reply = bus.call(methodUserSetting);
137             reply.read(result);
138             powerPolicy = std::get<std::string>(result);
139         }
140         else
141         {
142             // one_time setting was set so we're going to use it. Reset it
143             // to default for next time.
144             info("One time set, use it and reset to default");
145             phosphor::state::manager::utils::setProperty(
146                 bus, settings.powerRestorePolicyOneTime, powerRestoreIntf,
147                 "PowerRestorePolicy",
148                 convertForMessage(RestorePolicy::Policy::None));
149         }
150 
151         auto methodUserSettingDelay = bus.new_method_call(
152             settings.service(settings.powerRestorePolicy, powerRestoreIntf)
153                 .c_str(),
154             settings.powerRestorePolicy.c_str(),
155             "org.freedesktop.DBus.Properties", "Get");
156 
157         methodUserSettingDelay.append(powerRestoreIntf, "PowerRestoreDelay");
158 
159         std::variant<uint64_t> restoreDelay;
160 
161         auto delayResult = bus.call(methodUserSettingDelay);
162         delayResult.read(restoreDelay);
163         auto powerRestoreDelayUsec =
164             std::chrono::microseconds(std::get<uint64_t>(restoreDelay));
165         auto powerRestoreDelaySec =
166             std::chrono::duration_cast<std::chrono::seconds>(
167                 powerRestoreDelayUsec);
168 
169         info("Host power is off, processing power policy {POWER_POLICY}",
170              "POWER_POLICY", powerPolicy);
171 
172         if (RestorePolicy::Policy::AlwaysOn ==
173             RestorePolicy::convertPolicyFromString(powerPolicy))
174         {
175             info(
176                 "power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)",
177                 "DELAY", powerRestoreDelaySec.count());
178 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
179             utils::waitBmcReady(bus, powerRestoreDelaySec);
180 #else
181             std::this_thread::sleep_for(powerRestoreDelayUsec);
182 #endif
183             phosphor::state::manager::utils::setProperty(
184                 bus, hostPath, HostState::interface, "RestartCause",
185                 convertForMessage(
186                     server::Host::RestartCause::PowerPolicyAlwaysOn));
187             phosphor::state::manager::utils::setProperty(
188                 bus, hostPath, HostState::interface, "RequestedHostTransition",
189                 convertForMessage(server::Host::Transition::On));
190         }
191         // Always execute power on if AlwaysOn is set, otherwise check config
192         // option (and AC loss status) on whether to execute other policy
193         // settings
194 #if ONLY_RUN_APR_ON_POWER_LOSS
195         else if (!phosphor::state::manager::utils::checkACLoss(hostId))
196         {
197             info(
198                 "Chassis power was not on prior to BMC reboot so do not run any further power policy");
199             return 0;
200         }
201 #endif
202         else if (RestorePolicy::Policy::AlwaysOff ==
203                  RestorePolicy::convertPolicyFromString(powerPolicy))
204         {
205             info(
206                 "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
207                 "DELAY", powerRestoreDelaySec.count());
208 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
209             utils::waitBmcReady(bus, powerRestoreDelaySec);
210 #else
211             std::this_thread::sleep_for(powerRestoreDelayUsec);
212 #endif
213             // Read last requested state and re-request it to execute it
214             auto hostReqState = phosphor::state::manager::utils::getProperty(
215                 bus, hostPath, HostState::interface, "RequestedHostTransition");
216             if (hostReqState !=
217                 convertForMessage(server::Host::Transition::Off))
218             {
219                 phosphor::state::manager::utils::setProperty(
220                     bus, hostPath, HostState::interface,
221                     "RequestedHostTransition",
222                     convertForMessage(server::Host::Transition::Off));
223             }
224         }
225         else if (RestorePolicy::Policy::Restore ==
226                  RestorePolicy::convertPolicyFromString(powerPolicy))
227         {
228             info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
229                  "DELAY", powerRestoreDelaySec.count());
230 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
231             utils::waitBmcReady(bus, powerRestoreDelaySec);
232 #else
233             std::this_thread::sleep_for(powerRestoreDelayUsec);
234 #endif
235             // Read last requested state and re-request it to execute it
236             auto hostReqState = phosphor::state::manager::utils::getProperty(
237                 bus, hostPath, HostState::interface, "RequestedHostTransition");
238 
239             // As long as the host transition is not 'Off' power on host state.
240             if (hostReqState !=
241                 convertForMessage(server::Host::Transition::Off))
242             {
243                 phosphor::state::manager::utils::setProperty(
244                     bus, hostPath, HostState::interface, "RestartCause",
245                     convertForMessage(
246                         server::Host::RestartCause::PowerPolicyPreviousState));
247                 phosphor::state::manager::utils::setProperty(
248                     bus, hostPath, HostState::interface,
249                     "RequestedHostTransition",
250                     convertForMessage(server::Host::Transition::On));
251             }
252         }
253     }
254     catch (const sdbusplus::exception_t& e)
255     {
256         error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
257         elog<InternalFailure>();
258     }
259 
260     return 0;
261 }
262