blob: 5389fc5b978935cd08388f69b465c0c3d5892f51 [file] [log] [blame]
Saqib Khana8006a22017-02-14 11:37:08 -06001#include <phosphor-logging/log.hpp>
Matt Spinlere6710b72018-07-12 16:05:55 -05002#include <sys/sysinfo.h>
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06003#include "bmc_state_manager.hpp"
4
5namespace phosphor
6{
7namespace state
8{
9namespace manager
10{
11
Josh D. King6db38222016-12-19 14:52:40 -060012// When you see server:: you know we're referencing our base class
13namespace server = sdbusplus::xyz::openbmc_project::State::server;
14
15using namespace phosphor::logging;
16
Josh D. Kingd613b812016-12-19 16:47:45 -060017constexpr auto obmcStandbyTarget = "obmc-standby.target";
18constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060019constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060020
Josh D. King5162a7b2016-12-19 16:15:00 -060021/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080022const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
23 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060024
Andrew Geissler58a18012018-01-19 19:36:05 -080025constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
26constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
27constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060028constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060029
30void BMC::discoverInitialState()
31{
32 sdbusplus::message::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060033 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060034
Andrew Geissler58a18012018-01-19 19:36:05 -080035 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
36 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060037
38 method.append(obmcStandbyTarget);
39
40 auto result = this->bus.call(method);
41
Andrew Geissler58a18012018-01-19 19:36:05 -080042 // Check that the bus call didn't result in an error
43 if (result.is_method_error())
Josh D. King2b5d8872017-02-21 13:37:17 -060044 {
45 log<level::ERR>("Error in bus call.");
46 return;
47 }
48
49 result.read(unitTargetPath);
50
Andrew Geissler58a18012018-01-19 19:36:05 -080051 method = this->bus.new_method_call(
52 SYSTEMD_SERVICE,
53 static_cast<const std::string&>(unitTargetPath).c_str(),
54 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060055
56 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
57
Josh D. King2b5d8872017-02-21 13:37:17 -060058 result = this->bus.call(method);
59
Andrew Geissler58a18012018-01-19 19:36:05 -080060 // Check that the bus call didn't result in an error
61 if (result.is_method_error())
Josh D. King2b5d8872017-02-21 13:37:17 -060062 {
63 log<level::INFO>("Error in bus call.");
64 return;
65 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060066
Andrew Geissler58a18012018-01-19 19:36:05 -080067 // Is obmc-standby.target active or inactive?
Josh D. Kingd3e58472017-02-02 11:09:11 -060068 result.read(currentState);
69
Andrew Geissler58a18012018-01-19 19:36:05 -080070 if (currentState == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -060071 {
72 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -080073 entry("CURRENT_BMC_STATE=%s", "BMC_READY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -060074 this->currentBMCState(BMCState::Ready);
75
Andrew Geissler58a18012018-01-19 19:36:05 -080076 // Unsubscribe so we stop processing all other signals
77 method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
78 SYSTEMD_INTERFACE, "Unsubscribe");
Josh D. Kingd3e58472017-02-02 11:09:11 -060079 this->bus.call(method);
80 this->stateSignal.release();
81 }
82 else
83 {
84 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -080085 entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -060086 this->currentBMCState(BMCState::NotReady);
87 }
88
89 return;
90}
91
Josh D. King6db38222016-12-19 14:52:40 -060092void BMC::subscribeToSystemdSignals()
93{
Andrew Geissler58a18012018-01-19 19:36:05 -080094 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
95 SYSTEMD_INTERFACE, "Subscribe");
Josh D. King6db38222016-12-19 14:52:40 -060096 this->bus.call(method);
97
98 return;
99}
100
Josh D. King5162a7b2016-12-19 16:15:00 -0600101void BMC::executeTransition(const Transition tranReq)
102{
Andrew Geissler58a18012018-01-19 19:36:05 -0800103 // Check to make sure it can be found
Josh D. King5162a7b2016-12-19 16:15:00 -0600104 auto iter = SYSTEMD_TABLE.find(tranReq);
Andrew Geissler58a18012018-01-19 19:36:05 -0800105 if (iter == SYSTEMD_TABLE.end())
106 return;
Josh D. King5162a7b2016-12-19 16:15:00 -0600107
108 const auto& sysdUnit = iter->second;
109
Andrew Geissler58a18012018-01-19 19:36:05 -0800110 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
111 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler181f8362017-06-30 16:43:05 -0500112 // The only valid transition is reboot and that
113 // needs to be irreversible once started
114 method.append(sysdUnit, "replace-irreversibly");
Josh D. King5162a7b2016-12-19 16:15:00 -0600115
116 this->bus.call(method);
Josh D. King5162a7b2016-12-19 16:15:00 -0600117 return;
118}
119
Patrick Williamsd32f8182017-05-05 15:55:24 -0500120int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600121{
Andrew Geissler58a18012018-01-19 19:36:05 -0800122 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600123 sdbusplus::message::object_path newStateObjPath;
124 std::string newStateUnit{};
125 std::string newStateResult{};
126
Andrew Geissler58a18012018-01-19 19:36:05 -0800127 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500128 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600129
Andrew Geissler58a18012018-01-19 19:36:05 -0800130 // Caught the signal that indicates the BMC is now BMC_READY
131 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600132 {
133 log<level::INFO>("BMC_READY");
134 this->currentBMCState(BMCState::Ready);
135
Andrew Geissler58a18012018-01-19 19:36:05 -0800136 // Unsubscribe so we stop processing all other signals
137 auto method =
138 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
139 SYSTEMD_INTERFACE, "Unsubscribe");
Josh D. Kingd613b812016-12-19 16:47:45 -0600140 this->bus.call(method);
141 this->stateSignal.release();
142 }
143
144 return 0;
145}
146
Josh D. King6db38222016-12-19 14:52:40 -0600147BMC::Transition BMC::requestedBMCTransition(Transition value)
148{
Andrew Geissler58a18012018-01-19 19:36:05 -0800149 log<level::INFO>("Setting the RequestedBMCTransition field",
150 entry("REQUESTED_BMC_TRANSITION=0x%s",
151 convertForMessage(value).c_str()));
Josh D. King6db38222016-12-19 14:52:40 -0600152
Josh D. King5162a7b2016-12-19 16:15:00 -0600153 executeTransition(value);
154 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600155}
156
Josh D. Kingd613b812016-12-19 16:47:45 -0600157BMC::BMCState BMC::currentBMCState(BMCState value)
158{
159 log<level::INFO>(
Andrew Geissler58a18012018-01-19 19:36:05 -0800160 "Setting the BMCState field",
161 entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str()));
Josh D. Kingd613b812016-12-19 16:47:45 -0600162
163 return server::BMC::currentBMCState(value);
164}
165
Matt Spinlere6710b72018-07-12 16:05:55 -0500166uint64_t BMC::lastRebootTime() const
167{
168 using namespace std::chrono;
169 struct sysinfo info;
170
171 auto rc = sysinfo(&info);
172 assert(rc == 0);
173
174 // Since uptime is in seconds, also get the current time in seconds.
175 auto now = time_point_cast<seconds>(system_clock::now());
176 auto rebootTime = now - seconds(info.uptime);
177
178 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
179}
180
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600181} // namespace manager
182} // namespace state
183} // namepsace phosphor