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