blob: 1b7d3114a279ee0b9cfa8d08cbb15df91561212f [file] [log] [blame]
William A. Kennington III436f3b82018-11-12 17:25:00 -08001#include <cassert>
Saqib Khana8006a22017-02-14 11:37:08 -06002#include <phosphor-logging/log.hpp>
Andrew Geissler2f60aae2019-09-12 13:25:21 -05003#include <phosphor-logging/elog-errors.hpp>
Anthony Wilson32c532e2018-10-25 21:56:07 -05004#include <sdbusplus/exception.hpp>
Matt Spinlere6710b72018-07-12 16:05:55 -05005#include <sys/sysinfo.h>
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06006#include "bmc_state_manager.hpp"
Andrew Geissler2f60aae2019-09-12 13:25:21 -05007#include "xyz/openbmc_project/Common/error.hpp"
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06008
9namespace phosphor
10{
11namespace state
12{
13namespace manager
14{
15
Josh D. King6db38222016-12-19 14:52:40 -060016// When you see server:: you know we're referencing our base class
17namespace server = sdbusplus::xyz::openbmc_project::State::server;
18
19using namespace phosphor::logging;
Anthony Wilson32c532e2018-10-25 21:56:07 -050020using sdbusplus::exception::SdBusError;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050021using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Josh D. King6db38222016-12-19 14:52:40 -060022
Anthony Wilsoneef31f82019-04-23 17:04:09 -050023constexpr auto obmcStandbyTarget = "multi-user.target";
Josh D. Kingd613b812016-12-19 16:47:45 -060024constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060025constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060026
Josh D. King5162a7b2016-12-19 16:15:00 -060027/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080028const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
29 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060030
Andrew Geissler58a18012018-01-19 19:36:05 -080031constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
32constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
33constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060034constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060035
36void BMC::discoverInitialState()
37{
38 sdbusplus::message::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060039 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060040
Andrew Geissler58a18012018-01-19 19:36:05 -080041 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
42 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060043
44 method.append(obmcStandbyTarget);
45
Anthony Wilson32c532e2018-10-25 21:56:07 -050046 try
Josh D. King2b5d8872017-02-21 13:37:17 -060047 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050048 auto result = this->bus.call(method);
49 result.read(unitTargetPath);
50 }
51 catch (const SdBusError& e)
52 {
53 log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060054 return;
55 }
56
Andrew Geissler58a18012018-01-19 19:36:05 -080057 method = this->bus.new_method_call(
58 SYSTEMD_SERVICE,
59 static_cast<const std::string&>(unitTargetPath).c_str(),
60 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060061
62 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
63
Anthony Wilson32c532e2018-10-25 21:56:07 -050064 try
Josh D. King2b5d8872017-02-21 13:37:17 -060065 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050066 auto result = this->bus.call(method);
67
68 // Is obmc-standby.target active or inactive?
69 result.read(currentState);
70 }
71 catch (const SdBusError& e)
72 {
73 log<level::INFO>("Error in ActiveState Get",
74 entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060075 return;
76 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060077
Patrick Williams37413dc2020-05-13 11:29:54 -050078 auto currentStateStr = std::get<std::string>(currentState);
Anthony Wilson32c532e2018-10-25 21:56:07 -050079 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -060080 {
81 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -080082 entry("CURRENT_BMC_STATE=%s", "BMC_READY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -060083 this->currentBMCState(BMCState::Ready);
84
Andrew Geissler58a18012018-01-19 19:36:05 -080085 // Unsubscribe so we stop processing all other signals
86 method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
87 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -050088 try
89 {
90 this->bus.call(method);
91 this->stateSignal.release();
92 }
93 catch (const SdBusError& e)
94 {
95 log<level::INFO>("Error in Unsubscribe",
96 entry("ERROR=%s", e.what()));
97 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060098 }
99 else
100 {
101 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -0800102 entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -0600103 this->currentBMCState(BMCState::NotReady);
104 }
105
106 return;
107}
108
Josh D. King6db38222016-12-19 14:52:40 -0600109void BMC::subscribeToSystemdSignals()
110{
Andrew Geissler58a18012018-01-19 19:36:05 -0800111 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
112 SYSTEMD_INTERFACE, "Subscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500113
114 try
115 {
116 this->bus.call(method);
117 }
118 catch (const SdBusError& e)
119 {
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500120 log<level::ERR>("Failed to subscribe to systemd signals",
121 entry("ERR=%s", e.what()));
122 elog<InternalFailure>();
Anthony Wilson32c532e2018-10-25 21:56:07 -0500123 }
Josh D. King6db38222016-12-19 14:52:40 -0600124
125 return;
126}
127
Josh D. King5162a7b2016-12-19 16:15:00 -0600128void BMC::executeTransition(const Transition tranReq)
129{
Andrew Geissler58a18012018-01-19 19:36:05 -0800130 // Check to make sure it can be found
Josh D. King5162a7b2016-12-19 16:15:00 -0600131 auto iter = SYSTEMD_TABLE.find(tranReq);
Andrew Geissler58a18012018-01-19 19:36:05 -0800132 if (iter == SYSTEMD_TABLE.end())
133 return;
Josh D. King5162a7b2016-12-19 16:15:00 -0600134
135 const auto& sysdUnit = iter->second;
136
Andrew Geissler58a18012018-01-19 19:36:05 -0800137 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
138 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler181f8362017-06-30 16:43:05 -0500139 // The only valid transition is reboot and that
140 // needs to be irreversible once started
141 method.append(sysdUnit, "replace-irreversibly");
Josh D. King5162a7b2016-12-19 16:15:00 -0600142
Anthony Wilson32c532e2018-10-25 21:56:07 -0500143 try
144 {
145 this->bus.call(method);
146 }
147 catch (const SdBusError& e)
148 {
149 log<level::INFO>("Error in StartUnit - replace-irreversibly",
150 entry("ERROR=%s", e.what()));
151 }
152
Josh D. King5162a7b2016-12-19 16:15:00 -0600153 return;
154}
155
Patrick Williamsd32f8182017-05-05 15:55:24 -0500156int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600157{
Andrew Geissler58a18012018-01-19 19:36:05 -0800158 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600159 sdbusplus::message::object_path newStateObjPath;
160 std::string newStateUnit{};
161 std::string newStateResult{};
162
Andrew Geissler58a18012018-01-19 19:36:05 -0800163 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500164 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600165
Andrew Geissler58a18012018-01-19 19:36:05 -0800166 // Caught the signal that indicates the BMC is now BMC_READY
167 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600168 {
169 log<level::INFO>("BMC_READY");
170 this->currentBMCState(BMCState::Ready);
171
Andrew Geissler58a18012018-01-19 19:36:05 -0800172 // Unsubscribe so we stop processing all other signals
173 auto method =
174 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
175 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500176
177 try
178 {
179 this->bus.call(method);
180 this->stateSignal.release();
181 }
182 catch (const SdBusError& e)
183 {
184 log<level::INFO>("Error in Unsubscribe",
185 entry("ERROR=%s", e.what()));
186 }
Josh D. Kingd613b812016-12-19 16:47:45 -0600187 }
188
189 return 0;
190}
191
Josh D. King6db38222016-12-19 14:52:40 -0600192BMC::Transition BMC::requestedBMCTransition(Transition value)
193{
Andrew Geissler58a18012018-01-19 19:36:05 -0800194 log<level::INFO>("Setting the RequestedBMCTransition field",
195 entry("REQUESTED_BMC_TRANSITION=0x%s",
196 convertForMessage(value).c_str()));
Josh D. King6db38222016-12-19 14:52:40 -0600197
Josh D. King5162a7b2016-12-19 16:15:00 -0600198 executeTransition(value);
199 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600200}
201
Josh D. Kingd613b812016-12-19 16:47:45 -0600202BMC::BMCState BMC::currentBMCState(BMCState value)
203{
204 log<level::INFO>(
Andrew Geissler58a18012018-01-19 19:36:05 -0800205 "Setting the BMCState field",
206 entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str()));
Josh D. Kingd613b812016-12-19 16:47:45 -0600207
208 return server::BMC::currentBMCState(value);
209}
210
Matt Spinlere6710b72018-07-12 16:05:55 -0500211uint64_t BMC::lastRebootTime() const
212{
213 using namespace std::chrono;
214 struct sysinfo info;
215
216 auto rc = sysinfo(&info);
217 assert(rc == 0);
218
219 // Since uptime is in seconds, also get the current time in seconds.
220 auto now = time_point_cast<seconds>(system_clock::now());
221 auto rebootTime = now - seconds(info.uptime);
222
223 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
224}
225
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600226} // namespace manager
227} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700228} // namespace phosphor