blob: 1ab3f7e20c10ed20ebfcc0e2406f90a2416614a9 [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
Anthony Wilson32c532e2018-10-25 21:56:07 -050078 auto currentStateStr =
79 sdbusplus::message::variant_ns::get<std::string>(currentState);
80 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -060081 {
82 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -080083 entry("CURRENT_BMC_STATE=%s", "BMC_READY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -060084 this->currentBMCState(BMCState::Ready);
85
Andrew Geissler58a18012018-01-19 19:36:05 -080086 // Unsubscribe so we stop processing all other signals
87 method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
88 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -050089 try
90 {
91 this->bus.call(method);
92 this->stateSignal.release();
93 }
94 catch (const SdBusError& e)
95 {
96 log<level::INFO>("Error in Unsubscribe",
97 entry("ERROR=%s", e.what()));
98 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060099 }
100 else
101 {
102 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -0800103 entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -0600104 this->currentBMCState(BMCState::NotReady);
105 }
106
107 return;
108}
109
Josh D. King6db38222016-12-19 14:52:40 -0600110void BMC::subscribeToSystemdSignals()
111{
Andrew Geissler58a18012018-01-19 19:36:05 -0800112 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
113 SYSTEMD_INTERFACE, "Subscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500114
115 try
116 {
117 this->bus.call(method);
118 }
119 catch (const SdBusError& e)
120 {
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500121 log<level::ERR>("Failed to subscribe to systemd signals",
122 entry("ERR=%s", e.what()));
123 elog<InternalFailure>();
Anthony Wilson32c532e2018-10-25 21:56:07 -0500124 }
Josh D. King6db38222016-12-19 14:52:40 -0600125
126 return;
127}
128
Josh D. King5162a7b2016-12-19 16:15:00 -0600129void BMC::executeTransition(const Transition tranReq)
130{
Andrew Geissler58a18012018-01-19 19:36:05 -0800131 // Check to make sure it can be found
Josh D. King5162a7b2016-12-19 16:15:00 -0600132 auto iter = SYSTEMD_TABLE.find(tranReq);
Andrew Geissler58a18012018-01-19 19:36:05 -0800133 if (iter == SYSTEMD_TABLE.end())
134 return;
Josh D. King5162a7b2016-12-19 16:15:00 -0600135
136 const auto& sysdUnit = iter->second;
137
Andrew Geissler58a18012018-01-19 19:36:05 -0800138 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
139 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler181f8362017-06-30 16:43:05 -0500140 // The only valid transition is reboot and that
141 // needs to be irreversible once started
142 method.append(sysdUnit, "replace-irreversibly");
Josh D. King5162a7b2016-12-19 16:15:00 -0600143
Anthony Wilson32c532e2018-10-25 21:56:07 -0500144 try
145 {
146 this->bus.call(method);
147 }
148 catch (const SdBusError& e)
149 {
150 log<level::INFO>("Error in StartUnit - replace-irreversibly",
151 entry("ERROR=%s", e.what()));
152 }
153
Josh D. King5162a7b2016-12-19 16:15:00 -0600154 return;
155}
156
Patrick Williamsd32f8182017-05-05 15:55:24 -0500157int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600158{
Andrew Geissler58a18012018-01-19 19:36:05 -0800159 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600160 sdbusplus::message::object_path newStateObjPath;
161 std::string newStateUnit{};
162 std::string newStateResult{};
163
Andrew Geissler58a18012018-01-19 19:36:05 -0800164 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500165 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600166
Andrew Geissler58a18012018-01-19 19:36:05 -0800167 // Caught the signal that indicates the BMC is now BMC_READY
168 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600169 {
170 log<level::INFO>("BMC_READY");
171 this->currentBMCState(BMCState::Ready);
172
Andrew Geissler58a18012018-01-19 19:36:05 -0800173 // Unsubscribe so we stop processing all other signals
174 auto method =
175 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
176 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500177
178 try
179 {
180 this->bus.call(method);
181 this->stateSignal.release();
182 }
183 catch (const SdBusError& e)
184 {
185 log<level::INFO>("Error in Unsubscribe",
186 entry("ERROR=%s", e.what()));
187 }
Josh D. Kingd613b812016-12-19 16:47:45 -0600188 }
189
190 return 0;
191}
192
Josh D. King6db38222016-12-19 14:52:40 -0600193BMC::Transition BMC::requestedBMCTransition(Transition value)
194{
Andrew Geissler58a18012018-01-19 19:36:05 -0800195 log<level::INFO>("Setting the RequestedBMCTransition field",
196 entry("REQUESTED_BMC_TRANSITION=0x%s",
197 convertForMessage(value).c_str()));
Josh D. King6db38222016-12-19 14:52:40 -0600198
Josh D. King5162a7b2016-12-19 16:15:00 -0600199 executeTransition(value);
200 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600201}
202
Josh D. Kingd613b812016-12-19 16:47:45 -0600203BMC::BMCState BMC::currentBMCState(BMCState value)
204{
205 log<level::INFO>(
Andrew Geissler58a18012018-01-19 19:36:05 -0800206 "Setting the BMCState field",
207 entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str()));
Josh D. Kingd613b812016-12-19 16:47:45 -0600208
209 return server::BMC::currentBMCState(value);
210}
211
Matt Spinlere6710b72018-07-12 16:05:55 -0500212uint64_t BMC::lastRebootTime() const
213{
214 using namespace std::chrono;
215 struct sysinfo info;
216
217 auto rc = sysinfo(&info);
218 assert(rc == 0);
219
220 // Since uptime is in seconds, also get the current time in seconds.
221 auto now = time_point_cast<seconds>(system_clock::now());
222 auto rebootTime = now - seconds(info.uptime);
223
224 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
225}
226
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600227} // namespace manager
228} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700229} // namespace phosphor