Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 1 | #include <log.hpp> |
| 2 | #include "chassis_state_manager.hpp" |
| 3 | |
| 4 | namespace phosphor |
| 5 | { |
| 6 | namespace state |
| 7 | { |
| 8 | namespace manager |
| 9 | { |
| 10 | |
| 11 | // When you see server:: you know we're referencing our base class |
| 12 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 13 | |
| 14 | using namespace phosphor::logging; |
| 15 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame^] | 16 | /* Map a transition to it's systemd target */ |
| 17 | const std::map<server::Chassis::Transition,std::string> SYSTEMD_TARGET_TABLE = |
| 18 | { |
| 19 | {server::Chassis::Transition::Off, "obmc-power-chassis-off@0.target"}, |
| 20 | {server::Chassis::Transition::On, "obmc-power-chassis-on@0.target"} |
| 21 | }; |
| 22 | |
| 23 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 24 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 25 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 26 | |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 27 | /* TODO:Issue 774 - Use systemd target signals to control chassis state */ |
| 28 | int Chassis::handlePgoodOn(sd_bus_message* /*msg*/, void* usrData, |
| 29 | sd_bus_error* retError) |
| 30 | { |
| 31 | log<level::INFO>("Pgood has turned on", |
| 32 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 33 | convertForMessage(PowerState::On).c_str())); |
| 34 | auto chassisInst = static_cast<Chassis*>(usrData); |
| 35 | chassisInst->currentPowerState(PowerState::On); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | int Chassis::handlePgoodOff(sd_bus_message* /*msg*/, void* usrData, |
| 41 | sd_bus_error* retError) |
| 42 | { |
| 43 | log<level::INFO>("Pgood has turned off", |
| 44 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 45 | convertForMessage(PowerState::Off).c_str())); |
| 46 | auto chassisInst = static_cast<Chassis*>(usrData); |
| 47 | chassisInst->currentPowerState(PowerState::Off); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 52 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 53 | // and persistent storage design is in place and sdbusplus |
| 54 | // has read property function |
| 55 | void Chassis::determineInitialState() |
| 56 | { |
| 57 | sdbusplus::message::variant<int> pgood = -1; |
| 58 | auto method = this->bus.new_method_call("org.openbmc.control.Power", |
| 59 | "/org/openbmc/control/power0", |
| 60 | "org.freedesktop.DBus.Properties", |
| 61 | "Get"); |
| 62 | |
| 63 | method.append("org.openbmc.control.Power", "pgood"); |
| 64 | auto reply = this->bus.call(method); |
| 65 | reply.read(pgood); |
| 66 | |
| 67 | if(pgood == 1) |
| 68 | { |
| 69 | log<level::INFO>("Initial Chassis State will be On", |
| 70 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 71 | convertForMessage(PowerState::On).c_str())); |
| 72 | server::Chassis::currentPowerState(PowerState::On); |
| 73 | server::Chassis::requestedPowerTransition(Transition::On); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | log<level::INFO>("Initial Chassis State will be Off", |
| 78 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 79 | convertForMessage(PowerState::Off).c_str())); |
| 80 | server::Chassis::currentPowerState(PowerState::Off); |
| 81 | server::Chassis::requestedPowerTransition(Transition::Off); |
| 82 | } |
| 83 | |
| 84 | return; |
| 85 | } |
| 86 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame^] | 87 | void Chassis::executeTransition(Transition tranReq) |
| 88 | { |
| 89 | auto sysdTarget = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 90 | |
| 91 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 92 | SYSTEMD_OBJ_PATH, |
| 93 | SYSTEMD_INTERFACE, |
| 94 | "StartUnit"); |
| 95 | |
| 96 | method.append(sysdTarget); |
| 97 | method.append("replace"); |
| 98 | |
| 99 | this->bus.call_noreply(method); |
| 100 | |
| 101 | return; |
| 102 | } |
| 103 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 104 | Chassis::Transition Chassis::requestedPowerTransition(Transition value) |
| 105 | { |
| 106 | |
| 107 | log<level::INFO>("Change to Chassis Requested Power State", |
| 108 | entry("CHASSIS_REQUESTED_POWER_STATE=%s", |
| 109 | convertForMessage(value).c_str())); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame^] | 110 | executeTransition(value); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 111 | return server::Chassis::requestedPowerTransition(value); |
| 112 | } |
| 113 | |
| 114 | Chassis::PowerState Chassis::currentPowerState(PowerState value) |
| 115 | { |
| 116 | log<level::INFO>("Change to Chassis Power State", |
| 117 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 118 | convertForMessage(value).c_str())); |
| 119 | return server::Chassis::currentPowerState(value); |
| 120 | } |
| 121 | |
| 122 | } // namespace manager |
| 123 | } // namespace state |
| 124 | } // namepsace phosphor |