Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
Saqib Khan | a8006a2 | 2017-02-14 11:37:08 -0600 | [diff] [blame] | 2 | #include <phosphor-logging/log.hpp> |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 3 | #include "chassis_state_manager.hpp" |
| 4 | |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace state |
| 8 | { |
| 9 | namespace manager |
| 10 | { |
| 11 | |
| 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 | 6838ea9 | 2017-04-11 13:39:18 -0500 | [diff] [blame] | 17 | constexpr auto CHASSIS_STATE_POWEROFF_TGT = "obmc-chassis-poweroff@0.target"; |
| 18 | constexpr auto CHASSIS_STATE_POWERON_TGT = "obmc-chassis-poweron@0.target"; |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 19 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame^] | 20 | constexpr auto ACTIVE_STATE = "active"; |
| 21 | constexpr auto ACTIVATING_STATE = "activating"; |
| 22 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 23 | /* Map a transition to it's systemd target */ |
| 24 | const std::map<server::Chassis::Transition,std::string> SYSTEMD_TARGET_TABLE = |
| 25 | { |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 26 | {server::Chassis::Transition::Off, CHASSIS_STATE_POWEROFF_TGT}, |
| 27 | {server::Chassis::Transition::On, CHASSIS_STATE_POWERON_TGT} |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -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 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame^] | 34 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 35 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 36 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 37 | void Chassis::subscribeToSystemdSignals() |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 38 | { |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 39 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 40 | SYSTEMD_OBJ_PATH, |
| 41 | SYSTEMD_INTERFACE, |
| 42 | "Subscribe"); |
| 43 | this->bus.call_noreply(method); |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 44 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 45 | return; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 46 | } |
| 47 | |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 48 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 49 | // and persistent storage design is in place and sdbusplus |
| 50 | // has read property function |
| 51 | void Chassis::determineInitialState() |
| 52 | { |
| 53 | sdbusplus::message::variant<int> pgood = -1; |
| 54 | auto method = this->bus.new_method_call("org.openbmc.control.Power", |
| 55 | "/org/openbmc/control/power0", |
| 56 | "org.freedesktop.DBus.Properties", |
| 57 | "Get"); |
| 58 | |
| 59 | method.append("org.openbmc.control.Power", "pgood"); |
| 60 | auto reply = this->bus.call(method); |
| 61 | reply.read(pgood); |
| 62 | |
| 63 | if(pgood == 1) |
| 64 | { |
| 65 | log<level::INFO>("Initial Chassis State will be On", |
| 66 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 67 | convertForMessage(PowerState::On).c_str())); |
| 68 | server::Chassis::currentPowerState(PowerState::On); |
| 69 | server::Chassis::requestedPowerTransition(Transition::On); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | log<level::INFO>("Initial Chassis State will be Off", |
| 74 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 75 | convertForMessage(PowerState::Off).c_str())); |
| 76 | server::Chassis::currentPowerState(PowerState::Off); |
| 77 | server::Chassis::requestedPowerTransition(Transition::Off); |
| 78 | } |
| 79 | |
| 80 | return; |
| 81 | } |
| 82 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 83 | void Chassis::executeTransition(Transition tranReq) |
| 84 | { |
| 85 | auto sysdTarget = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 86 | |
| 87 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 88 | SYSTEMD_OBJ_PATH, |
| 89 | SYSTEMD_INTERFACE, |
| 90 | "StartUnit"); |
| 91 | |
| 92 | method.append(sysdTarget); |
| 93 | method.append("replace"); |
| 94 | |
| 95 | this->bus.call_noreply(method); |
| 96 | |
| 97 | return; |
| 98 | } |
| 99 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame^] | 100 | bool Chassis::stateActive(const std::string& target) |
| 101 | { |
| 102 | sdbusplus::message::variant<std::string> currentState; |
| 103 | sdbusplus::message::object_path unitTargetPath; |
| 104 | |
| 105 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 106 | SYSTEMD_OBJ_PATH, |
| 107 | SYSTEMD_INTERFACE, |
| 108 | "GetUnit"); |
| 109 | |
| 110 | method.append(target); |
| 111 | auto result = this->bus.call(method); |
| 112 | |
| 113 | //Check that the bus call didn't result in an error |
| 114 | if(result.is_method_error()) |
| 115 | { |
| 116 | log<level::ERR>("Error in bus call - could not resolve GetUnit for:", |
| 117 | entry(" %s", SYSTEMD_INTERFACE)); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | result.read(unitTargetPath); |
| 122 | |
| 123 | method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 124 | static_cast<const std::string&> |
| 125 | (unitTargetPath).c_str(), |
| 126 | SYSTEMD_PROPERTY_IFACE, |
| 127 | "Get"); |
| 128 | |
| 129 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
| 130 | result = this->bus.call(method); |
| 131 | |
| 132 | //Check that the bus call didn't result in an error |
| 133 | if(result.is_method_error()) |
| 134 | { |
| 135 | log<level::ERR>("Error in bus call - could not resolve Get for:", |
| 136 | entry(" %s", SYSTEMD_PROPERTY_IFACE)); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | result.read(currentState); |
| 141 | |
| 142 | if(currentState != ACTIVE_STATE && currentState != ACTIVATING_STATE) |
| 143 | { |
| 144 | //False - not active |
| 145 | return false; |
| 146 | } |
| 147 | //True - active |
| 148 | return true; |
| 149 | |
| 150 | } |
| 151 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 152 | int Chassis::sysStateChangeSignal(sd_bus_message *msg, void *userData, |
| 153 | sd_bus_error *retError) |
| 154 | { |
| 155 | return static_cast<Chassis*>(userData)->sysStateChange(msg, retError); |
| 156 | } |
| 157 | |
| 158 | int Chassis::sysStateChange(sd_bus_message* msg, |
| 159 | sd_bus_error* retError) |
| 160 | { |
| 161 | uint32_t newStateID {}; |
| 162 | sdbusplus::message::object_path newStateObjPath; |
| 163 | std::string newStateUnit{}; |
| 164 | std::string newStateResult{}; |
| 165 | |
| 166 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 167 | //Read the msg and populate each variable |
| 168 | sdPlusMsg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 169 | |
| 170 | if((newStateUnit == CHASSIS_STATE_POWEROFF_TGT) && |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame^] | 171 | (newStateResult == "done") && |
| 172 | (!stateActive(CHASSIS_STATE_POWERON_TGT))) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 173 | { |
| 174 | log<level::INFO>("Recieved signal that power OFF is complete"); |
| 175 | this->currentPowerState(server::Chassis::PowerState::Off); |
| 176 | } |
| 177 | else if((newStateUnit == CHASSIS_STATE_POWERON_TGT) && |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame^] | 178 | (newStateResult == "done") && |
| 179 | (stateActive(CHASSIS_STATE_POWERON_TGT))) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 180 | { |
| 181 | log<level::INFO>("Recieved signal that power ON is complete"); |
| 182 | this->currentPowerState(server::Chassis::PowerState::On); |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 188 | Chassis::Transition Chassis::requestedPowerTransition(Transition value) |
| 189 | { |
| 190 | |
| 191 | log<level::INFO>("Change to Chassis Requested Power State", |
| 192 | entry("CHASSIS_REQUESTED_POWER_STATE=%s", |
| 193 | convertForMessage(value).c_str())); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 194 | executeTransition(value); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 195 | return server::Chassis::requestedPowerTransition(value); |
| 196 | } |
| 197 | |
| 198 | Chassis::PowerState Chassis::currentPowerState(PowerState value) |
| 199 | { |
| 200 | log<level::INFO>("Change to Chassis Power State", |
| 201 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 202 | convertForMessage(value).c_str())); |
| 203 | return server::Chassis::currentPowerState(value); |
| 204 | } |
| 205 | |
| 206 | } // namespace manager |
| 207 | } // namespace state |
| 208 | } // namepsace phosphor |