blob: 6ea73d19d3575dd6333fae2de4bdece6b0d4eef0 [file] [log] [blame]
Andrew Geissler769a62f2019-12-06 13:36:08 -06001#include "config.h"
Andrew Geisslere426b582020-05-28 12:40:55 -05002
Michael Tritz3ed10082017-03-20 23:28:09 -05003#include "host_state_manager.hpp"
Deepak Kodihalli55f132b2017-07-25 07:36:06 -05004#include "settings.hpp"
Andrew Geissler49e67132022-01-26 14:27:52 -06005#include "utils.hpp"
Deepak Kodihalli55f132b2017-07-25 07:36:06 -05006#include "xyz/openbmc_project/Common/error.hpp"
7#include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
Michael Tritz3ed10082017-03-20 23:28:09 -05008
Andrew Geisslere426b582020-05-28 12:40:55 -05009#include <getopt.h>
10#include <systemd/sd-bus.h>
11
12#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050013#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050014#include <sdbusplus/exception.hpp>
15#include <sdbusplus/server.hpp>
16
17#include <iostream>
18#include <map>
19#include <string>
20
Michael Tritz3ed10082017-03-20 23:28:09 -050021namespace phosphor
22{
23namespace state
24{
25namespace manager
26{
27
Andrew Geissler8ffdb262021-09-20 15:25:19 -050028PHOSPHOR_LOG2_USING;
29
Michael Tritz3ed10082017-03-20 23:28:09 -050030using namespace phosphor::logging;
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050031using namespace sdbusplus::xyz::openbmc_project::Common::Error;
32using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
Michael Tritz3ed10082017-03-20 23:28:09 -050033
Michael Tritz3ed10082017-03-20 23:28:09 -050034} // namespace manager
35} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -070036} // namespace phosphor
Michael Tritz3ed10082017-03-20 23:28:09 -050037
Michael Tritzd50f6572017-09-05 16:38:11 -050038int main(int argc, char** argv)
Michael Tritz3ed10082017-03-20 23:28:09 -050039{
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050040 using namespace phosphor::logging;
41
Michael Tritzd50f6572017-09-05 16:38:11 -050042 std::string hostPath = "/xyz/openbmc_project/state/host0";
43 int arg;
44 int optIndex = 0;
45
Andrew Geissler58a18012018-01-19 19:36:05 -080046 static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
47 {0, 0, 0, 0}};
Michael Tritzd50f6572017-09-05 16:38:11 -050048
Andrew Geissler58a18012018-01-19 19:36:05 -080049 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
Michael Tritzd50f6572017-09-05 16:38:11 -050050 {
51 switch (arg)
52 {
53 case 'h':
Andrew Geissler58a18012018-01-19 19:36:05 -080054 hostPath =
55 std::string("/xyz/openbmc_project/state/host") + optarg;
Michael Tritzd50f6572017-09-05 16:38:11 -050056 break;
57 default:
58 break;
59 }
60 }
61
Michael Tritz3ed10082017-03-20 23:28:09 -050062 auto bus = sdbusplus::bus::new_default();
63
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050064 using namespace settings;
65 Objects settings(bus);
66
Michael Tritz3ed10082017-03-20 23:28:09 -050067 using namespace phosphor::state::manager;
68 namespace server = sdbusplus::xyz::openbmc_project::State::server;
69
Andrew Geissler033fc3b2017-08-30 15:11:44 -050070 // This application is only run if chassis power is off
Michael Tritz3ed10082017-03-20 23:28:09 -050071
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060072 // If the BMC was rebooted due to a user initiated pinhole reset, do not
73 // implement any power restore policies
74 auto bmcRebootCause = phosphor::state::manager::utils::getProperty(
75 bus, "/xyz/openbmc_project/state/bmc0", BMC_BUSNAME, "LastRebootCause");
76 if (bmcRebootCause ==
77 "xyz.openbmc_project.State.BMC.RebootCause.PinholeReset")
78 {
79 info(
80 "BMC was reset due to pinhole reset, no power restore policy will be run");
81 return 0;
82 }
Thang Q. Nguyenb669ea32022-02-22 07:15:15 +000083 else if (bmcRebootCause ==
84 "xyz.openbmc_project.State.BMC.RebootCause.Watchdog")
85 {
86 info(
87 "BMC was reset due to cold reset, no power restore policy will be run");
88 return 0;
89 }
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060090
Andrew Geissler35ca2e32021-02-09 13:54:26 -060091 /* The logic here is to first check the one-time PowerRestorePolicy setting.
92 * If this property is not the default then look at the persistent
93 * user setting in the non one-time object, otherwise honor the one-time
94 * setting.
95 */
96 auto methodOneTime = bus.new_method_call(
97 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
98 settings.powerRestorePolicyOneTime.c_str(),
99 "org.freedesktop.DBus.Properties", "Get");
100 methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy");
101
102 auto methodUserSetting = bus.new_method_call(
Andrew Geissler58a18012018-01-19 19:36:05 -0800103 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
104 settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
105 "Get");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600106 methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500107
Patrick Williams2975e262020-05-13 18:01:09 -0500108 std::variant<std::string> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500109 try
Michael Tritz3ed10082017-03-20 23:28:09 -0500110 {
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600111 auto reply = bus.call(methodOneTime);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500112 reply.read(result);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600113 auto powerPolicy = std::get<std::string>(result);
114
115 if (RestorePolicy::Policy::None ==
116 RestorePolicy::convertPolicyFromString(powerPolicy))
117 {
118 // one_time is set to None so use the customer setting
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500119 info("One time not set, check user setting of power policy");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600120 auto reply = bus.call(methodUserSetting);
121 reply.read(result);
122 powerPolicy = std::get<std::string>(result);
123 }
124 else
125 {
126 // one_time setting was set so we're going to use it. Reset it
127 // to default for next time.
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500128 info("One time set, use it and reset to default");
Andrew Geissler49e67132022-01-26 14:27:52 -0600129 phosphor::state::manager::utils::setProperty(
130 bus, settings.powerRestorePolicyOneTime.c_str(),
131 powerRestoreIntf, "PowerRestorePolicy",
132 convertForMessage(RestorePolicy::Policy::None));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600133 }
134
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500135 info("Host power is off, processing power policy {POWER_POLICY}",
136 "POWER_POLICY", powerPolicy);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600137
138 if (RestorePolicy::Policy::AlwaysOn ==
139 RestorePolicy::convertPolicyFromString(powerPolicy))
140 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500141 info("power_policy=ALWAYS_POWER_ON, powering host on");
Andrew Geissler49e67132022-01-26 14:27:52 -0600142 phosphor::state::manager::utils::setProperty(
143 bus, hostPath, HOST_BUSNAME, "RestartCause",
144 convertForMessage(
145 server::Host::RestartCause::PowerPolicyAlwaysOn));
146 phosphor::state::manager::utils::setProperty(
147 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
148 convertForMessage(server::Host::Transition::On));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600149 }
Thang Tran1fc48452022-02-08 16:04:11 +0700150 else if (RestorePolicy::Policy::AlwaysOff ==
151 RestorePolicy::convertPolicyFromString(powerPolicy))
152 {
153 info("power_policy=ALWAYS_POWER_OFF, set requested state to off");
Thang Tran6810ad52022-02-24 09:36:57 +0700154 // Read last requested state and re-request it to execute it
155 auto hostReqState = phosphor::state::manager::utils::getProperty(
156 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
157 if (hostReqState == convertForMessage(server::Host::Transition::On))
158 {
159 phosphor::state::manager::utils::setProperty(
160 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
161 convertForMessage(server::Host::Transition::Off));
162 }
Thang Tran1fc48452022-02-08 16:04:11 +0700163 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600164 else if (RestorePolicy::Policy::Restore ==
165 RestorePolicy::convertPolicyFromString(powerPolicy))
166 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500167 info("power_policy=RESTORE, restoring last state");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600168 // Read last requested state and re-request it to execute it
Andrew Geissler49e67132022-01-26 14:27:52 -0600169 auto hostReqState = phosphor::state::manager::utils::getProperty(
170 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
Thang Tran6810ad52022-02-24 09:36:57 +0700171 if (hostReqState == convertForMessage(server::Host::Transition::On))
172 {
173 phosphor::state::manager::utils::setProperty(
174 bus, hostPath, HOST_BUSNAME, "RestartCause",
175 convertForMessage(
176 server::Host::RestartCause::PowerPolicyPreviousState));
177 phosphor::state::manager::utils::setProperty(
178 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
179 hostReqState);
180 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600181 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500182 }
Patrick Williams0a675212021-09-02 09:49:43 -0500183 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500184 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500185 error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500186 elog<InternalFailure>();
187 }
Deepak Kodihalli55f132b2017-07-25 07:36:06 -0500188
Michael Tritz3ed10082017-03-20 23:28:09 -0500189 return 0;
190}