blob: 1623922da38c26d0ee478bd8a9968e426d79c474 [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>
NodeMan97358de952022-07-20 08:31:36 -050020#include <thread>
Andrew Geisslere426b582020-05-28 12:40:55 -050021
Michael Tritz3ed10082017-03-20 23:28:09 -050022namespace phosphor
23{
24namespace state
25{
26namespace manager
27{
28
Andrew Geissler8ffdb262021-09-20 15:25:19 -050029PHOSPHOR_LOG2_USING;
30
Michael Tritz3ed10082017-03-20 23:28:09 -050031using namespace phosphor::logging;
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050032using namespace sdbusplus::xyz::openbmc_project::Common::Error;
33using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
Michael Tritz3ed10082017-03-20 23:28:09 -050034
Michael Tritz3ed10082017-03-20 23:28:09 -050035} // namespace manager
36} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -070037} // namespace phosphor
Michael Tritz3ed10082017-03-20 23:28:09 -050038
Michael Tritzd50f6572017-09-05 16:38:11 -050039int main(int argc, char** argv)
Michael Tritz3ed10082017-03-20 23:28:09 -050040{
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050041 using namespace phosphor::logging;
42
Potin Laic328a4c2022-03-18 23:49:37 +080043 size_t hostId = 0;
Michael Tritzd50f6572017-09-05 16:38:11 -050044 std::string hostPath = "/xyz/openbmc_project/state/host0";
45 int arg;
46 int optIndex = 0;
47
Andrew Geissler58a18012018-01-19 19:36:05 -080048 static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
49 {0, 0, 0, 0}};
Michael Tritzd50f6572017-09-05 16:38:11 -050050
Andrew Geissler58a18012018-01-19 19:36:05 -080051 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
Michael Tritzd50f6572017-09-05 16:38:11 -050052 {
53 switch (arg)
54 {
55 case 'h':
Potin Laic328a4c2022-03-18 23:49:37 +080056 hostId = std::stoul(optarg);
Andrew Geissler58a18012018-01-19 19:36:05 -080057 hostPath =
58 std::string("/xyz/openbmc_project/state/host") + optarg;
Michael Tritzd50f6572017-09-05 16:38:11 -050059 break;
60 default:
61 break;
62 }
63 }
64
Michael Tritz3ed10082017-03-20 23:28:09 -050065 auto bus = sdbusplus::bus::new_default();
66
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050067 using namespace settings;
Potin Laic328a4c2022-03-18 23:49:37 +080068 HostObjects settings(bus, hostId);
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050069
Michael Tritz3ed10082017-03-20 23:28:09 -050070 using namespace phosphor::state::manager;
71 namespace server = sdbusplus::xyz::openbmc_project::State::server;
72
Andrew Geissler033fc3b2017-08-30 15:11:44 -050073 // This application is only run if chassis power is off
Michael Tritz3ed10082017-03-20 23:28:09 -050074
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060075 // If the BMC was rebooted due to a user initiated pinhole reset, do not
76 // implement any power restore policies
77 auto bmcRebootCause = phosphor::state::manager::utils::getProperty(
78 bus, "/xyz/openbmc_project/state/bmc0", BMC_BUSNAME, "LastRebootCause");
79 if (bmcRebootCause ==
80 "xyz.openbmc_project.State.BMC.RebootCause.PinholeReset")
81 {
82 info(
83 "BMC was reset due to pinhole reset, no power restore policy will be run");
84 return 0;
85 }
Thang Q. Nguyenb669ea32022-02-22 07:15:15 +000086 else if (bmcRebootCause ==
87 "xyz.openbmc_project.State.BMC.RebootCause.Watchdog")
88 {
89 info(
90 "BMC was reset due to cold reset, no power restore policy will be run");
91 return 0;
92 }
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060093
Andrew Geissler35ca2e32021-02-09 13:54:26 -060094 /* The logic here is to first check the one-time PowerRestorePolicy setting.
95 * If this property is not the default then look at the persistent
96 * user setting in the non one-time object, otherwise honor the one-time
97 * setting.
98 */
99 auto methodOneTime = bus.new_method_call(
100 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
101 settings.powerRestorePolicyOneTime.c_str(),
102 "org.freedesktop.DBus.Properties", "Get");
103 methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy");
104
105 auto methodUserSetting = bus.new_method_call(
Andrew Geissler58a18012018-01-19 19:36:05 -0800106 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
107 settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
108 "Get");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600109 methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500110
Patrick Williams2975e262020-05-13 18:01:09 -0500111 std::variant<std::string> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500112 try
Michael Tritz3ed10082017-03-20 23:28:09 -0500113 {
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600114 auto reply = bus.call(methodOneTime);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500115 reply.read(result);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600116 auto powerPolicy = std::get<std::string>(result);
117
118 if (RestorePolicy::Policy::None ==
119 RestorePolicy::convertPolicyFromString(powerPolicy))
120 {
121 // one_time is set to None so use the customer setting
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500122 info("One time not set, check user setting of power policy");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600123 auto reply = bus.call(methodUserSetting);
124 reply.read(result);
125 powerPolicy = std::get<std::string>(result);
126 }
127 else
128 {
129 // one_time setting was set so we're going to use it. Reset it
130 // to default for next time.
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500131 info("One time set, use it and reset to default");
Andrew Geissler49e67132022-01-26 14:27:52 -0600132 phosphor::state::manager::utils::setProperty(
133 bus, settings.powerRestorePolicyOneTime.c_str(),
134 powerRestoreIntf, "PowerRestorePolicy",
135 convertForMessage(RestorePolicy::Policy::None));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600136 }
137
NodeMan97358de952022-07-20 08:31:36 -0500138 auto methodUserSettingDelay = bus.new_method_call(
139 settings.service(settings.powerRestorePolicy, powerRestoreIntf)
140 .c_str(),
141 settings.powerRestorePolicy.c_str(),
142 "org.freedesktop.DBus.Properties", "Get");
143
144 methodUserSettingDelay.append(powerRestoreIntf, "PowerRestoreDelay");
145
146 std::variant<uint64_t> restoreDelay;
147
148 auto delayResult = bus.call(methodUserSettingDelay);
149 delayResult.read(restoreDelay);
150 auto powerRestoreDelayUsec =
151 std::chrono::microseconds(std::get<uint64_t>(restoreDelay));
152 auto powerRestoreDelaySec =
153 std::chrono::duration_cast<std::chrono::seconds>(
154 powerRestoreDelayUsec);
155
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500156 info("Host power is off, processing power policy {POWER_POLICY}",
157 "POWER_POLICY", powerPolicy);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600158
159 if (RestorePolicy::Policy::AlwaysOn ==
160 RestorePolicy::convertPolicyFromString(powerPolicy))
161 {
NodeMan97358de952022-07-20 08:31:36 -0500162 info(
163 "power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)",
164 "DELAY", powerRestoreDelaySec.count());
165 std::this_thread::sleep_for(powerRestoreDelayUsec);
Andrew Geissler49e67132022-01-26 14:27:52 -0600166 phosphor::state::manager::utils::setProperty(
167 bus, hostPath, HOST_BUSNAME, "RestartCause",
168 convertForMessage(
169 server::Host::RestartCause::PowerPolicyAlwaysOn));
170 phosphor::state::manager::utils::setProperty(
171 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
172 convertForMessage(server::Host::Transition::On));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600173 }
Thang Tran1fc48452022-02-08 16:04:11 +0700174 else if (RestorePolicy::Policy::AlwaysOff ==
175 RestorePolicy::convertPolicyFromString(powerPolicy))
176 {
NodeMan97358de952022-07-20 08:31:36 -0500177 info(
178 "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
179 "DELAY", powerRestoreDelaySec.count());
180 std::this_thread::sleep_for(powerRestoreDelayUsec);
Thang Tran6810ad52022-02-24 09:36:57 +0700181 // Read last requested state and re-request it to execute it
182 auto hostReqState = phosphor::state::manager::utils::getProperty(
183 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
NodeMan97744fe622022-07-21 17:14:23 -0500184 if (hostReqState !=
185 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700186 {
187 phosphor::state::manager::utils::setProperty(
188 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
189 convertForMessage(server::Host::Transition::Off));
190 }
Thang Tran1fc48452022-02-08 16:04:11 +0700191 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600192 else if (RestorePolicy::Policy::Restore ==
193 RestorePolicy::convertPolicyFromString(powerPolicy))
194 {
NodeMan97358de952022-07-20 08:31:36 -0500195 info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
196 "DELAY", powerRestoreDelaySec.count());
197 std::this_thread::sleep_for(powerRestoreDelayUsec);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600198 // Read last requested state and re-request it to execute it
Andrew Geissler49e67132022-01-26 14:27:52 -0600199 auto hostReqState = phosphor::state::manager::utils::getProperty(
200 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
NodeMan97744fe622022-07-21 17:14:23 -0500201
202 // As long as the host transition is not 'Off' power on host state.
203 if (hostReqState !=
204 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700205 {
206 phosphor::state::manager::utils::setProperty(
207 bus, hostPath, HOST_BUSNAME, "RestartCause",
208 convertForMessage(
209 server::Host::RestartCause::PowerPolicyPreviousState));
210 phosphor::state::manager::utils::setProperty(
211 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
NodeMan97744fe622022-07-21 17:14:23 -0500212 convertForMessage(server::Host::Transition::On));
Thang Tran6810ad52022-02-24 09:36:57 +0700213 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600214 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500215 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500216 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500217 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500218 error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500219 elog<InternalFailure>();
220 }
Deepak Kodihalli55f132b2017-07-25 07:36:06 -0500221
Michael Tritz3ed10082017-03-20 23:28:09 -0500222 return 0;
223}