William A. Kennington III | 436f3b8 | 2018-11-12 17:25:00 -0800 | [diff] [blame] | 1 | #include <cassert> |
Saqib Khan | a8006a2 | 2017-02-14 11:37:08 -0600 | [diff] [blame] | 2 | #include <phosphor-logging/log.hpp> |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 3 | #include <sdbusplus/exception.hpp> |
Matt Spinler | e6710b7 | 2018-07-12 16:05:55 -0500 | [diff] [blame] | 4 | #include <sys/sysinfo.h> |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 5 | #include "bmc_state_manager.hpp" |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace state |
| 10 | { |
| 11 | namespace manager |
| 12 | { |
| 13 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 14 | // When you see server:: you know we're referencing our base class |
| 15 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 16 | |
| 17 | using namespace phosphor::logging; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 18 | using sdbusplus::exception::SdBusError; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 19 | |
Anthony Wilson | eef31f8 | 2019-04-23 17:04:09 -0500 | [diff] [blame^] | 20 | constexpr auto obmcStandbyTarget = "multi-user.target"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 21 | constexpr auto signalDone = "done"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 22 | constexpr auto activeState = "active"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 23 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 24 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 25 | const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = { |
| 26 | {server::BMC::Transition::Reboot, "reboot.target"}}; |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 27 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 28 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 29 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 30 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 31 | constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 32 | |
| 33 | void BMC::discoverInitialState() |
| 34 | { |
| 35 | sdbusplus::message::variant<std::string> currentState; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 36 | sdbusplus::message::object_path unitTargetPath; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 37 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 38 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 39 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 40 | |
| 41 | method.append(obmcStandbyTarget); |
| 42 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 43 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 44 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 45 | auto result = this->bus.call(method); |
| 46 | result.read(unitTargetPath); |
| 47 | } |
| 48 | catch (const SdBusError& e) |
| 49 | { |
| 50 | log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what())); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 54 | method = this->bus.new_method_call( |
| 55 | SYSTEMD_SERVICE, |
| 56 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 57 | SYSTEMD_PRP_INTERFACE, "Get"); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 58 | |
| 59 | method.append("org.freedesktop.systemd1.Unit", "ActiveState"); |
| 60 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 61 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 62 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 63 | auto result = this->bus.call(method); |
| 64 | |
| 65 | // Is obmc-standby.target active or inactive? |
| 66 | result.read(currentState); |
| 67 | } |
| 68 | catch (const SdBusError& e) |
| 69 | { |
| 70 | log<level::INFO>("Error in ActiveState Get", |
| 71 | entry("ERROR=%s", e.what())); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 72 | return; |
| 73 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 74 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 75 | auto currentStateStr = |
| 76 | sdbusplus::message::variant_ns::get<std::string>(currentState); |
| 77 | if (currentStateStr == activeState) |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 78 | { |
| 79 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 80 | entry("CURRENT_BMC_STATE=%s", "BMC_READY")); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 81 | this->currentBMCState(BMCState::Ready); |
| 82 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 83 | // Unsubscribe so we stop processing all other signals |
| 84 | method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 85 | SYSTEMD_INTERFACE, "Unsubscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 86 | try |
| 87 | { |
| 88 | this->bus.call(method); |
| 89 | this->stateSignal.release(); |
| 90 | } |
| 91 | catch (const SdBusError& e) |
| 92 | { |
| 93 | log<level::INFO>("Error in Unsubscribe", |
| 94 | entry("ERROR=%s", e.what())); |
| 95 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 96 | } |
| 97 | else |
| 98 | { |
| 99 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 100 | entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY")); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 101 | this->currentBMCState(BMCState::NotReady); |
| 102 | } |
| 103 | |
| 104 | return; |
| 105 | } |
| 106 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 107 | void BMC::subscribeToSystemdSignals() |
| 108 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 109 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 110 | SYSTEMD_INTERFACE, "Subscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 111 | |
| 112 | try |
| 113 | { |
| 114 | this->bus.call(method); |
| 115 | } |
| 116 | catch (const SdBusError& e) |
| 117 | { |
| 118 | log<level::INFO>("Error in Subscribe", entry("ERROR=%s", e.what())); |
| 119 | } |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 120 | |
| 121 | return; |
| 122 | } |
| 123 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 124 | void BMC::executeTransition(const Transition tranReq) |
| 125 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 126 | // Check to make sure it can be found |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 127 | auto iter = SYSTEMD_TABLE.find(tranReq); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 128 | if (iter == SYSTEMD_TABLE.end()) |
| 129 | return; |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 130 | |
| 131 | const auto& sysdUnit = iter->second; |
| 132 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 133 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 134 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | 181f836 | 2017-06-30 16:43:05 -0500 | [diff] [blame] | 135 | // The only valid transition is reboot and that |
| 136 | // needs to be irreversible once started |
| 137 | method.append(sysdUnit, "replace-irreversibly"); |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 138 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 139 | try |
| 140 | { |
| 141 | this->bus.call(method); |
| 142 | } |
| 143 | catch (const SdBusError& e) |
| 144 | { |
| 145 | log<level::INFO>("Error in StartUnit - replace-irreversibly", |
| 146 | entry("ERROR=%s", e.what())); |
| 147 | } |
| 148 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 152 | int BMC::bmcStateChange(sdbusplus::message::message& msg) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 153 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 154 | uint32_t newStateID{}; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 155 | sdbusplus::message::object_path newStateObjPath; |
| 156 | std::string newStateUnit{}; |
| 157 | std::string newStateResult{}; |
| 158 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 159 | // Read the msg and populate each variable |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 160 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 161 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 162 | // Caught the signal that indicates the BMC is now BMC_READY |
| 163 | if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone)) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 164 | { |
| 165 | log<level::INFO>("BMC_READY"); |
| 166 | this->currentBMCState(BMCState::Ready); |
| 167 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 168 | // Unsubscribe so we stop processing all other signals |
| 169 | auto method = |
| 170 | this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 171 | SYSTEMD_INTERFACE, "Unsubscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 172 | |
| 173 | try |
| 174 | { |
| 175 | this->bus.call(method); |
| 176 | this->stateSignal.release(); |
| 177 | } |
| 178 | catch (const SdBusError& e) |
| 179 | { |
| 180 | log<level::INFO>("Error in Unsubscribe", |
| 181 | entry("ERROR=%s", e.what())); |
| 182 | } |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 188 | BMC::Transition BMC::requestedBMCTransition(Transition value) |
| 189 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 190 | log<level::INFO>("Setting the RequestedBMCTransition field", |
| 191 | entry("REQUESTED_BMC_TRANSITION=0x%s", |
| 192 | convertForMessage(value).c_str())); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 193 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 194 | executeTransition(value); |
| 195 | return server::BMC::requestedBMCTransition(value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 196 | } |
| 197 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 198 | BMC::BMCState BMC::currentBMCState(BMCState value) |
| 199 | { |
| 200 | log<level::INFO>( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 201 | "Setting the BMCState field", |
| 202 | entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str())); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 203 | |
| 204 | return server::BMC::currentBMCState(value); |
| 205 | } |
| 206 | |
Matt Spinler | e6710b7 | 2018-07-12 16:05:55 -0500 | [diff] [blame] | 207 | uint64_t BMC::lastRebootTime() const |
| 208 | { |
| 209 | using namespace std::chrono; |
| 210 | struct sysinfo info; |
| 211 | |
| 212 | auto rc = sysinfo(&info); |
| 213 | assert(rc == 0); |
| 214 | |
| 215 | // Since uptime is in seconds, also get the current time in seconds. |
| 216 | auto now = time_point_cast<seconds>(system_clock::now()); |
| 217 | auto rebootTime = now - seconds(info.uptime); |
| 218 | |
| 219 | return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count(); |
| 220 | } |
| 221 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 222 | } // namespace manager |
| 223 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 224 | } // namespace phosphor |