Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 1 | #include <iostream> |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 2 | #include <map> |
| 3 | #include <string> |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 4 | #include <systemd/sd-bus.h> |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 5 | #include <log.hpp> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 6 | #include "host_state_manager.hpp" |
| 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace state |
| 11 | { |
| 12 | namespace manager |
| 13 | { |
| 14 | |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 15 | // When you see server:: you know we're referencing our base class |
| 16 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 17 | |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 18 | using namespace phosphor::logging; |
| 19 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 20 | constexpr auto HOST_STATE_POWEROFF_TGT = "obmc-chassis-stop@0.target"; |
| 21 | constexpr auto HOST_STATE_POWERON_TGT = "obmc-chassis-start@0.target"; |
| 22 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 23 | /* Map a transition to it's systemd target */ |
| 24 | const std::map<server::Host::Transition,std::string> SYSTEMD_TARGET_TABLE = |
| 25 | { |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 26 | {server::Host::Transition::Off, HOST_STATE_POWEROFF_TGT}, |
| 27 | {server::Host::Transition::On, HOST_STATE_POWERON_TGT} |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 31 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 32 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 33 | |
| 34 | constexpr auto SYSTEM_SERVICE = "org.openbmc.managers.System"; |
| 35 | constexpr auto SYSTEM_OBJ_PATH = "/org/openbmc/managers/System"; |
| 36 | constexpr auto SYSTEM_INTERFACE = SYSTEM_SERVICE; |
| 37 | |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 38 | /* Map a system state to the HostState */ |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 39 | const std::map<std::string, server::Host::HostState> SYS_HOST_STATE_TABLE = { |
| 40 | {"HOST_BOOTING", server::Host::HostState::Running}, |
| 41 | {"HOST_POWERED_OFF", server::Host::HostState::Off} |
| 42 | }; |
| 43 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 44 | void Host::subscribeToSystemdSignals() |
| 45 | { |
| 46 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 47 | SYSTEMD_OBJ_PATH, |
| 48 | SYSTEMD_INTERFACE, |
| 49 | "Subscribe"); |
| 50 | this->bus.call_noreply(method); |
| 51 | |
| 52 | return; |
| 53 | } |
| 54 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 55 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 56 | // and persistent storage design is in place |
| 57 | void Host::determineInitialState() |
| 58 | { |
| 59 | std::string sysState; |
| 60 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 61 | auto method = this->bus.new_method_call(SYSTEM_SERVICE, |
| 62 | SYSTEM_OBJ_PATH, |
| 63 | SYSTEM_INTERFACE, |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 64 | "getSystemState"); |
| 65 | |
| 66 | auto reply = this->bus.call(method); |
| 67 | |
| 68 | reply.read(sysState); |
| 69 | |
| 70 | if(sysState == "HOST_BOOTED") |
| 71 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 72 | log<level::INFO>("Initial Host State will be Running", |
| 73 | entry("CURRENT_HOST_STATE=%s", |
| 74 | convertForMessage(HostState::Running).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 75 | server::Host::currentHostState(HostState::Running); |
| 76 | server::Host::requestedHostTransition(Transition::On); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 77 | } |
| 78 | else |
| 79 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 80 | log<level::INFO>("Initial Host State will be Off", |
| 81 | entry("CURRENT_HOST_STATE=%s", |
| 82 | convertForMessage(HostState::Off).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 83 | server::Host::currentHostState(HostState::Off); |
| 84 | server::Host::requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Set transition initially to Off |
| 88 | // TODO - Eventually need to restore this from persistent storage |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 89 | server::Host::requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 90 | |
| 91 | return; |
| 92 | } |
| 93 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 94 | void Host::executeTransition(Transition tranReq) |
| 95 | { |
| 96 | auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 97 | |
| 98 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 99 | SYSTEMD_OBJ_PATH, |
| 100 | SYSTEMD_INTERFACE, |
| 101 | "StartUnit"); |
| 102 | |
| 103 | method.append(sysdUnit); |
| 104 | method.append("replace"); |
| 105 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 106 | this->bus.call_noreply(method); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 107 | |
| 108 | return; |
| 109 | } |
| 110 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 111 | int Host::sysStateChangeSignal(sd_bus_message *msg, void *userData, |
| 112 | sd_bus_error *retError) |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 113 | { |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 114 | return static_cast<Host*>(userData)->sysStateChange(msg, retError); |
| 115 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 116 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 117 | int Host::sysStateChange(sd_bus_message* msg, |
| 118 | sd_bus_error* retError) |
| 119 | { |
| 120 | uint32_t newStateID {}; |
| 121 | sdbusplus::message::object_path newStateObjPath; |
| 122 | std::string newStateUnit{}; |
| 123 | std::string newStateResult{}; |
| 124 | |
| 125 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 126 | //Read the msg and populate each variable |
| 127 | sdPlusMsg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 128 | |
| 129 | if((newStateUnit == HOST_STATE_POWEROFF_TGT) && |
| 130 | (newStateResult == "done")) |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 131 | { |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 132 | log<level::INFO>("Recieved signal that host is off"); |
| 133 | this->currentHostState(server::Host::HostState::Off); |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 134 | |
| 135 | // Check if we need to start a new transition (i.e. a Reboot) |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 136 | if(this->server::Host::requestedHostTransition() == |
| 137 | Transition::Reboot) |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 138 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 139 | log<level::DEBUG>("Reached intermediate state, going to next"); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 140 | this->executeTransition(server::Host::Transition::On); |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 141 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 142 | } |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 143 | else if((newStateUnit == HOST_STATE_POWERON_TGT) && |
| 144 | (newStateResult == "done")) |
| 145 | { |
| 146 | log<level::INFO>("Recieved signal that host is running"); |
| 147 | this->currentHostState(server::Host::HostState::Running); |
| 148 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 153 | Host::Transition Host::requestedHostTransition(Transition value) |
| 154 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 155 | log<level::INFO>( |
| 156 | "Host State transaction request", |
| 157 | entry("REQUESTED_HOST_TRANSITION=%s", |
| 158 | convertForMessage(value).c_str())); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 159 | |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 160 | Transition tranReq = value; |
| 161 | if(value == server::Host::Transition::Reboot) |
| 162 | { |
| 163 | // On reboot requests we just need to do a off if we're on and |
| 164 | // vice versa. The handleSysStateChange() code above handles the |
| 165 | // second part of the reboot |
| 166 | if(this->server::Host::currentHostState() == |
| 167 | server::Host::HostState::Off) |
| 168 | { |
| 169 | tranReq = server::Host::Transition::On; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | tranReq = server::Host::Transition::Off; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | executeTransition(tranReq); |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 178 | return server::Host::requestedHostTransition(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 179 | } |
| 180 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 181 | Host::HostState Host::currentHostState(HostState value) |
| 182 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 183 | log<level::INFO>("Change to Host State", |
| 184 | entry("CURRENT_HOST_STATE=%s", |
| 185 | convertForMessage(value).c_str())); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 186 | return server::Host::currentHostState(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 187 | } |
| 188 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 189 | } // namespace manager |
| 190 | } // namespace state |
| 191 | } // namepsace phosphor |