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 | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 20 | /* Map a transition to it's systemd target */ |
| 21 | const std::map<server::Host::Transition,std::string> SYSTEMD_TARGET_TABLE = |
| 22 | { |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 23 | {server::Host::Transition::Off, "obmc-chassis-stop@0.target"}, |
| 24 | {server::Host::Transition::On, "obmc-chassis-start@0.target"} |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 28 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 29 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 30 | |
| 31 | constexpr auto SYSTEM_SERVICE = "org.openbmc.managers.System"; |
| 32 | constexpr auto SYSTEM_OBJ_PATH = "/org/openbmc/managers/System"; |
| 33 | constexpr auto SYSTEM_INTERFACE = SYSTEM_SERVICE; |
| 34 | |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 35 | /* Map a system state to the HostState */ |
| 36 | /* TODO:Issue 774 - Use systemd target signals to control host states */ |
| 37 | const std::map<std::string, server::Host::HostState> SYS_HOST_STATE_TABLE = { |
| 38 | {"HOST_BOOTING", server::Host::HostState::Running}, |
| 39 | {"HOST_POWERED_OFF", server::Host::HostState::Off} |
| 40 | }; |
| 41 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 42 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 43 | // and persistent storage design is in place |
| 44 | void Host::determineInitialState() |
| 45 | { |
| 46 | std::string sysState; |
| 47 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 48 | auto method = this->bus.new_method_call(SYSTEM_SERVICE, |
| 49 | SYSTEM_OBJ_PATH, |
| 50 | SYSTEM_INTERFACE, |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 51 | "getSystemState"); |
| 52 | |
| 53 | auto reply = this->bus.call(method); |
| 54 | |
| 55 | reply.read(sysState); |
| 56 | |
| 57 | if(sysState == "HOST_BOOTED") |
| 58 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame^] | 59 | log<level::INFO>("Initial Host State will be Running", |
| 60 | entry("CURRENT_HOST_STATE=%s", |
| 61 | convertForMessage(HostState::Running).c_str())); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 62 | currentHostState(HostState::Running); |
Andrew Geissler | 359aaaf | 2016-12-13 13:23:53 -0600 | [diff] [blame] | 63 | requestedHostTransition(Transition::On); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 64 | } |
| 65 | else |
| 66 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame^] | 67 | log<level::INFO>("Initial Host State will be Off", |
| 68 | entry("CURRENT_HOST_STATE=%s", |
| 69 | convertForMessage(HostState::Off).c_str())); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 70 | currentHostState(HostState::Off); |
Andrew Geissler | 359aaaf | 2016-12-13 13:23:53 -0600 | [diff] [blame] | 71 | requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Set transition initially to Off |
| 75 | // TODO - Eventually need to restore this from persistent storage |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 76 | server::Host::requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 77 | |
| 78 | return; |
| 79 | } |
| 80 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 81 | void Host::executeTransition(Transition tranReq) |
| 82 | { |
| 83 | auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 84 | |
| 85 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 86 | SYSTEMD_OBJ_PATH, |
| 87 | SYSTEMD_INTERFACE, |
| 88 | "StartUnit"); |
| 89 | |
| 90 | method.append(sysdUnit); |
| 91 | method.append("replace"); |
| 92 | |
| 93 | this->bus.call(method); |
| 94 | |
| 95 | return; |
| 96 | } |
| 97 | |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 98 | int Host::handleSysStateChange(sd_bus_message *msg, void *usrData, |
| 99 | sd_bus_error *retError) |
| 100 | { |
| 101 | const char *newState = nullptr; |
| 102 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 103 | sdPlusMsg.read(newState); |
| 104 | |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 105 | auto it = SYS_HOST_STATE_TABLE.find(newState); |
| 106 | if(it != SYS_HOST_STATE_TABLE.end()) |
| 107 | { |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 108 | // This is a state change we're interested in so process it |
| 109 | Host::HostState gotoState = it->second; |
| 110 | // Grab the host object instance from the userData |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 111 | auto hostInst = static_cast<Host*>(usrData); |
| 112 | hostInst->currentHostState(gotoState); |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 113 | |
| 114 | // Check if we need to start a new transition (i.e. a Reboot) |
| 115 | if((gotoState == server::Host::HostState::Off) && |
| 116 | (hostInst->server::Host::requestedHostTransition() == |
| 117 | Transition::Reboot)) |
| 118 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame^] | 119 | log<level::DEBUG>("Reached intermediate state, going to next"); |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 120 | hostInst->executeTransition(server::Host::Transition::On); |
| 121 | } |
| 122 | else |
| 123 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame^] | 124 | log<level::DEBUG>("Reached final state"); |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 125 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 126 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 131 | Host::Transition Host::requestedHostTransition(Transition value) |
| 132 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame^] | 133 | log<level::INFO>( |
| 134 | "Host State transaction request", |
| 135 | entry("REQUESTED_HOST_TRANSITION=%s", |
| 136 | convertForMessage(value).c_str())); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 137 | |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 138 | Transition tranReq = value; |
| 139 | if(value == server::Host::Transition::Reboot) |
| 140 | { |
| 141 | // On reboot requests we just need to do a off if we're on and |
| 142 | // vice versa. The handleSysStateChange() code above handles the |
| 143 | // second part of the reboot |
| 144 | if(this->server::Host::currentHostState() == |
| 145 | server::Host::HostState::Off) |
| 146 | { |
| 147 | tranReq = server::Host::Transition::On; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | tranReq = server::Host::Transition::Off; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | executeTransition(tranReq); |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 156 | return server::Host::requestedHostTransition(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 157 | } |
| 158 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 159 | Host::HostState Host::currentHostState(HostState value) |
| 160 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame^] | 161 | log<level::INFO>("Change to Host State", |
| 162 | entry("CURRENT_HOST_STATE=%s", |
| 163 | convertForMessage(value).c_str())); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 164 | return server::Host::currentHostState(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 165 | } |
| 166 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 167 | } // namespace manager |
| 168 | } // namespace state |
| 169 | } // namepsace phosphor |