blob: 26deec96a8903f80fe7fb42f58c6ded35ca03281 [file] [log] [blame]
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06001#include "bmc_state_manager.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05002
Andrew Geissler2f60aae2019-09-12 13:25:21 -05003#include "xyz/openbmc_project/Common/error.hpp"
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06004
Andrew Geisslere426b582020-05-28 12:40:55 -05005#include <sys/sysinfo.h>
6
7#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/log.hpp>
9#include <sdbusplus/exception.hpp>
10
11#include <cassert>
12
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060013namespace phosphor
14{
15namespace state
16{
17namespace manager
18{
19
Josh D. King6db38222016-12-19 14:52:40 -060020// When you see server:: you know we're referencing our base class
21namespace server = sdbusplus::xyz::openbmc_project::State::server;
22
23using namespace phosphor::logging;
Anthony Wilson32c532e2018-10-25 21:56:07 -050024using sdbusplus::exception::SdBusError;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050025using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Josh D. King6db38222016-12-19 14:52:40 -060026
Anthony Wilsoneef31f82019-04-23 17:04:09 -050027constexpr auto obmcStandbyTarget = "multi-user.target";
Josh D. Kingd613b812016-12-19 16:47:45 -060028constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060029constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060030
Josh D. King5162a7b2016-12-19 16:15:00 -060031/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080032const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
33 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060034
Andrew Geissler58a18012018-01-19 19:36:05 -080035constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
36constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
37constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060038constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060039
40void BMC::discoverInitialState()
41{
Patrick Williams2975e262020-05-13 18:01:09 -050042 std::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060043 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060044
Andrew Geissler58a18012018-01-19 19:36:05 -080045 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
46 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060047
48 method.append(obmcStandbyTarget);
49
Anthony Wilson32c532e2018-10-25 21:56:07 -050050 try
Josh D. King2b5d8872017-02-21 13:37:17 -060051 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050052 auto result = this->bus.call(method);
53 result.read(unitTargetPath);
54 }
55 catch (const SdBusError& e)
56 {
57 log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060058 return;
59 }
60
Andrew Geissler58a18012018-01-19 19:36:05 -080061 method = this->bus.new_method_call(
62 SYSTEMD_SERVICE,
63 static_cast<const std::string&>(unitTargetPath).c_str(),
64 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060065
66 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
67
Anthony Wilson32c532e2018-10-25 21:56:07 -050068 try
Josh D. King2b5d8872017-02-21 13:37:17 -060069 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050070 auto result = this->bus.call(method);
71
72 // Is obmc-standby.target active or inactive?
73 result.read(currentState);
74 }
75 catch (const SdBusError& e)
76 {
77 log<level::INFO>("Error in ActiveState Get",
78 entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060079 return;
80 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060081
Patrick Williams37413dc2020-05-13 11:29:54 -050082 auto currentStateStr = std::get<std::string>(currentState);
Anthony Wilson32c532e2018-10-25 21:56:07 -050083 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -060084 {
85 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -080086 entry("CURRENT_BMC_STATE=%s", "BMC_READY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -060087 this->currentBMCState(BMCState::Ready);
88
Andrew Geissler58a18012018-01-19 19:36:05 -080089 // Unsubscribe so we stop processing all other signals
90 method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
91 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -050092 try
93 {
94 this->bus.call(method);
95 this->stateSignal.release();
96 }
97 catch (const SdBusError& e)
98 {
99 log<level::INFO>("Error in Unsubscribe",
100 entry("ERROR=%s", e.what()));
101 }
Josh D. Kingd3e58472017-02-02 11:09:11 -0600102 }
103 else
104 {
105 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -0800106 entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -0600107 this->currentBMCState(BMCState::NotReady);
108 }
109
110 return;
111}
112
Josh D. King6db38222016-12-19 14:52:40 -0600113void BMC::subscribeToSystemdSignals()
114{
Andrew Geissler58a18012018-01-19 19:36:05 -0800115 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
116 SYSTEMD_INTERFACE, "Subscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500117
118 try
119 {
120 this->bus.call(method);
121 }
122 catch (const SdBusError& e)
123 {
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500124 log<level::ERR>("Failed to subscribe to systemd signals",
125 entry("ERR=%s", e.what()));
126 elog<InternalFailure>();
Anthony Wilson32c532e2018-10-25 21:56:07 -0500127 }
Josh D. King6db38222016-12-19 14:52:40 -0600128
129 return;
130}
131
Josh D. King5162a7b2016-12-19 16:15:00 -0600132void BMC::executeTransition(const Transition tranReq)
133{
Andrew Geissler58a18012018-01-19 19:36:05 -0800134 // Check to make sure it can be found
Josh D. King5162a7b2016-12-19 16:15:00 -0600135 auto iter = SYSTEMD_TABLE.find(tranReq);
Andrew Geissler58a18012018-01-19 19:36:05 -0800136 if (iter == SYSTEMD_TABLE.end())
137 return;
Josh D. King5162a7b2016-12-19 16:15:00 -0600138
139 const auto& sysdUnit = iter->second;
140
Andrew Geissler58a18012018-01-19 19:36:05 -0800141 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
142 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler181f8362017-06-30 16:43:05 -0500143 // The only valid transition is reboot and that
144 // needs to be irreversible once started
145 method.append(sysdUnit, "replace-irreversibly");
Josh D. King5162a7b2016-12-19 16:15:00 -0600146
Anthony Wilson32c532e2018-10-25 21:56:07 -0500147 try
148 {
149 this->bus.call(method);
150 }
151 catch (const SdBusError& e)
152 {
153 log<level::INFO>("Error in StartUnit - replace-irreversibly",
154 entry("ERROR=%s", e.what()));
155 }
156
Josh D. King5162a7b2016-12-19 16:15:00 -0600157 return;
158}
159
Patrick Williamsd32f8182017-05-05 15:55:24 -0500160int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600161{
Andrew Geissler58a18012018-01-19 19:36:05 -0800162 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600163 sdbusplus::message::object_path newStateObjPath;
164 std::string newStateUnit{};
165 std::string newStateResult{};
166
Andrew Geissler58a18012018-01-19 19:36:05 -0800167 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500168 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600169
Andrew Geissler58a18012018-01-19 19:36:05 -0800170 // Caught the signal that indicates the BMC is now BMC_READY
171 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600172 {
173 log<level::INFO>("BMC_READY");
174 this->currentBMCState(BMCState::Ready);
175
Andrew Geissler58a18012018-01-19 19:36:05 -0800176 // Unsubscribe so we stop processing all other signals
177 auto method =
178 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
179 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500180
181 try
182 {
183 this->bus.call(method);
184 this->stateSignal.release();
185 }
186 catch (const SdBusError& e)
187 {
188 log<level::INFO>("Error in Unsubscribe",
189 entry("ERROR=%s", e.what()));
190 }
Josh D. Kingd613b812016-12-19 16:47:45 -0600191 }
192
193 return 0;
194}
195
Josh D. King6db38222016-12-19 14:52:40 -0600196BMC::Transition BMC::requestedBMCTransition(Transition value)
197{
Andrew Geissler58a18012018-01-19 19:36:05 -0800198 log<level::INFO>("Setting the RequestedBMCTransition field",
199 entry("REQUESTED_BMC_TRANSITION=0x%s",
200 convertForMessage(value).c_str()));
Josh D. King6db38222016-12-19 14:52:40 -0600201
Josh D. King5162a7b2016-12-19 16:15:00 -0600202 executeTransition(value);
203 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600204}
205
Josh D. Kingd613b812016-12-19 16:47:45 -0600206BMC::BMCState BMC::currentBMCState(BMCState value)
207{
208 log<level::INFO>(
Andrew Geissler58a18012018-01-19 19:36:05 -0800209 "Setting the BMCState field",
210 entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str()));
Josh D. Kingd613b812016-12-19 16:47:45 -0600211
212 return server::BMC::currentBMCState(value);
213}
214
Matt Spinlere6710b72018-07-12 16:05:55 -0500215uint64_t BMC::lastRebootTime() const
216{
217 using namespace std::chrono;
218 struct sysinfo info;
219
220 auto rc = sysinfo(&info);
221 assert(rc == 0);
222
223 // Since uptime is in seconds, also get the current time in seconds.
224 auto now = time_point_cast<seconds>(system_clock::now());
225 auto rebootTime = now - seconds(info.uptime);
226
227 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
228}
229
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600230} // namespace manager
231} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700232} // namespace phosphor