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 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 17 | constexpr auto CHASSIS_STATE_POWEROFF_TGT = "obmc-power-chassis-off@0.target"; |
| 18 | constexpr auto CHASSIS_STATE_POWERON_TGT = "obmc-power-chassis-on@0.target"; |
| 19 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 20 | /* Map a transition to it's systemd target */ |
| 21 | const std::map<server::Chassis::Transition,std::string> SYSTEMD_TARGET_TABLE = |
| 22 | { |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 23 | {server::Chassis::Transition::Off, CHASSIS_STATE_POWEROFF_TGT}, |
| 24 | {server::Chassis::Transition::On, CHASSIS_STATE_POWERON_TGT} |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -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 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 31 | void Chassis::subscribeToSystemdSignals() |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 32 | { |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 33 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 34 | SYSTEMD_OBJ_PATH, |
| 35 | SYSTEMD_INTERFACE, |
| 36 | "Subscribe"); |
| 37 | this->bus.call_noreply(method); |
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 | return; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 40 | } |
| 41 | |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 42 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 43 | // and persistent storage design is in place and sdbusplus |
| 44 | // has read property function |
| 45 | void Chassis::determineInitialState() |
| 46 | { |
| 47 | sdbusplus::message::variant<int> pgood = -1; |
| 48 | auto method = this->bus.new_method_call("org.openbmc.control.Power", |
| 49 | "/org/openbmc/control/power0", |
| 50 | "org.freedesktop.DBus.Properties", |
| 51 | "Get"); |
| 52 | |
| 53 | method.append("org.openbmc.control.Power", "pgood"); |
| 54 | auto reply = this->bus.call(method); |
| 55 | reply.read(pgood); |
| 56 | |
| 57 | if(pgood == 1) |
| 58 | { |
| 59 | log<level::INFO>("Initial Chassis State will be On", |
| 60 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 61 | convertForMessage(PowerState::On).c_str())); |
| 62 | server::Chassis::currentPowerState(PowerState::On); |
| 63 | server::Chassis::requestedPowerTransition(Transition::On); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | log<level::INFO>("Initial Chassis State will be Off", |
| 68 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 69 | convertForMessage(PowerState::Off).c_str())); |
| 70 | server::Chassis::currentPowerState(PowerState::Off); |
| 71 | server::Chassis::requestedPowerTransition(Transition::Off); |
| 72 | } |
| 73 | |
| 74 | return; |
| 75 | } |
| 76 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 77 | void Chassis::executeTransition(Transition tranReq) |
| 78 | { |
| 79 | auto sysdTarget = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 80 | |
| 81 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 82 | SYSTEMD_OBJ_PATH, |
| 83 | SYSTEMD_INTERFACE, |
| 84 | "StartUnit"); |
| 85 | |
| 86 | method.append(sysdTarget); |
| 87 | method.append("replace"); |
| 88 | |
| 89 | this->bus.call_noreply(method); |
| 90 | |
| 91 | return; |
| 92 | } |
| 93 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 94 | int Chassis::sysStateChangeSignal(sd_bus_message *msg, void *userData, |
| 95 | sd_bus_error *retError) |
| 96 | { |
| 97 | return static_cast<Chassis*>(userData)->sysStateChange(msg, retError); |
| 98 | } |
| 99 | |
| 100 | int Chassis::sysStateChange(sd_bus_message* msg, |
| 101 | sd_bus_error* retError) |
| 102 | { |
| 103 | uint32_t newStateID {}; |
| 104 | sdbusplus::message::object_path newStateObjPath; |
| 105 | std::string newStateUnit{}; |
| 106 | std::string newStateResult{}; |
| 107 | |
| 108 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 109 | //Read the msg and populate each variable |
| 110 | sdPlusMsg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 111 | |
| 112 | if((newStateUnit == CHASSIS_STATE_POWEROFF_TGT) && |
| 113 | (newStateResult == "done")) |
| 114 | { |
| 115 | log<level::INFO>("Recieved signal that power OFF is complete"); |
| 116 | this->currentPowerState(server::Chassis::PowerState::Off); |
| 117 | } |
| 118 | else if((newStateUnit == CHASSIS_STATE_POWERON_TGT) && |
| 119 | (newStateResult == "done")) |
| 120 | { |
| 121 | log<level::INFO>("Recieved signal that power ON is complete"); |
| 122 | this->currentPowerState(server::Chassis::PowerState::On); |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 128 | Chassis::Transition Chassis::requestedPowerTransition(Transition value) |
| 129 | { |
| 130 | |
| 131 | log<level::INFO>("Change to Chassis Requested Power State", |
| 132 | entry("CHASSIS_REQUESTED_POWER_STATE=%s", |
| 133 | convertForMessage(value).c_str())); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 134 | executeTransition(value); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 135 | return server::Chassis::requestedPowerTransition(value); |
| 136 | } |
| 137 | |
| 138 | Chassis::PowerState Chassis::currentPowerState(PowerState value) |
| 139 | { |
| 140 | log<level::INFO>("Change to Chassis Power State", |
| 141 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 142 | convertForMessage(value).c_str())); |
| 143 | return server::Chassis::currentPowerState(value); |
| 144 | } |
| 145 | |
| 146 | } // namespace manager |
| 147 | } // namespace state |
| 148 | } // namepsace phosphor |