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