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