Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 1 | #include <iostream> |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 2 | #include <string> |
Saqib Khan | a8006a2 | 2017-02-14 11:37:08 -0600 | [diff] [blame] | 3 | #include <phosphor-logging/log.hpp> |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 4 | #include "bmc_state_manager.hpp" |
| 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace state |
| 9 | { |
| 10 | namespace manager |
| 11 | { |
| 12 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 13 | // When you see server:: you know we're referencing our base class |
| 14 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 18 | constexpr auto obmcStandbyTarget = "obmc-standby.target"; |
| 19 | constexpr auto signalDone = "done"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 20 | constexpr auto activeState = "active"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 21 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 22 | /* Map a transition to it's systemd target */ |
| 23 | const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = |
| 24 | { |
| 25 | {server::BMC::Transition::Reboot, "reboot.target"} |
| 26 | }; |
| 27 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 28 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 29 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 30 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 31 | |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 32 | constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 33 | constexpr auto SYSTEMD_TGT_PATH = "/org/freedesktop/systemd1/unit/" |
| 34 | "obmc_2dstandby_2etarget"; |
| 35 | |
| 36 | void BMC::discoverInitialState() |
| 37 | { |
| 38 | sdbusplus::message::variant<std::string> currentState; |
| 39 | |
| 40 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 41 | SYSTEMD_TGT_PATH, |
| 42 | SYSTEMD_PRP_INTERFACE, |
| 43 | "Get"); |
| 44 | |
| 45 | method.append("org.freedesktop.systemd1.Unit", "ActiveState"); |
| 46 | |
| 47 | auto result = this->bus.call(method); |
| 48 | |
| 49 | //Is obmc-standby.target active or inactive? |
| 50 | result.read(currentState); |
| 51 | |
| 52 | if(currentState == activeState) |
| 53 | { |
| 54 | log<level::INFO>("Setting the BMCState field", |
| 55 | entry("CURRENT_BMC_STATE=%s", |
| 56 | "BMC_READY")); |
| 57 | this->currentBMCState(BMCState::Ready); |
| 58 | |
| 59 | //Unsubscribe so we stop processing all other signals |
| 60 | method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 61 | SYSTEMD_OBJ_PATH, |
| 62 | SYSTEMD_INTERFACE, |
| 63 | "Unsubscribe"); |
| 64 | this->bus.call(method); |
| 65 | this->stateSignal.release(); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | log<level::INFO>("Setting the BMCState field", |
| 70 | entry("CURRENT_BMC_STATE=%s", |
| 71 | "BMC_NOTREADY")); |
| 72 | this->currentBMCState(BMCState::NotReady); |
| 73 | } |
| 74 | |
| 75 | return; |
| 76 | } |
| 77 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 78 | void BMC::subscribeToSystemdSignals() |
| 79 | { |
| 80 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 81 | SYSTEMD_OBJ_PATH, |
| 82 | SYSTEMD_INTERFACE, |
| 83 | "Subscribe"); |
| 84 | this->bus.call(method); |
| 85 | |
| 86 | return; |
| 87 | } |
| 88 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 89 | void BMC::executeTransition(const Transition tranReq) |
| 90 | { |
| 91 | //Check to make sure it can be found |
| 92 | auto iter = SYSTEMD_TABLE.find(tranReq); |
| 93 | if (iter == SYSTEMD_TABLE.end()) return; |
| 94 | |
| 95 | const auto& sysdUnit = iter->second; |
| 96 | |
| 97 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 98 | SYSTEMD_OBJ_PATH, |
| 99 | SYSTEMD_INTERFACE, |
| 100 | "StartUnit"); |
| 101 | |
| 102 | method.append(sysdUnit, "replace"); |
| 103 | |
| 104 | this->bus.call(method); |
| 105 | |
| 106 | return; |
| 107 | } |
| 108 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 109 | int BMC::bmcStateChangeSignal(sd_bus_message *msg, void *userData, |
| 110 | sd_bus_error *retError) |
| 111 | { |
| 112 | return static_cast<BMC*>(userData)->bmcStateChange(msg, retError); |
| 113 | } |
| 114 | |
| 115 | int BMC::bmcStateChange(sd_bus_message *msg, |
| 116 | sd_bus_error *retError) |
| 117 | { |
| 118 | uint32_t newStateID {}; |
| 119 | sdbusplus::message::object_path newStateObjPath; |
| 120 | std::string newStateUnit{}; |
| 121 | std::string newStateResult{}; |
| 122 | |
| 123 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 124 | //Read the msg and populate each variable |
| 125 | sdPlusMsg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 126 | |
| 127 | //Caught the signal that indicates the BMC is now BMC_READY |
| 128 | if((newStateUnit == obmcStandbyTarget) && |
| 129 | (newStateResult == signalDone)) |
| 130 | { |
| 131 | log<level::INFO>("BMC_READY"); |
| 132 | this->currentBMCState(BMCState::Ready); |
| 133 | |
| 134 | //Unsubscribe so we stop processing all other signals |
| 135 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 136 | SYSTEMD_OBJ_PATH, |
| 137 | SYSTEMD_INTERFACE, |
| 138 | "Unsubscribe"); |
| 139 | this->bus.call(method); |
| 140 | this->stateSignal.release(); |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 146 | BMC::Transition BMC::requestedBMCTransition(Transition value) |
| 147 | { |
| 148 | log<level::INFO>( |
| 149 | "Setting the RequestedBMCTransition field", |
| 150 | entry("REQUESTED_BMC_TRANSITION=0x%s", |
| 151 | convertForMessage(value).c_str())); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 152 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 153 | executeTransition(value); |
| 154 | return server::BMC::requestedBMCTransition(value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 157 | BMC::BMCState BMC::currentBMCState(BMCState value) |
| 158 | { |
| 159 | log<level::INFO>( |
| 160 | "Setting the BMCState field", |
| 161 | entry("CURRENT_BMC_STATE=0x%s", |
| 162 | convertForMessage(value).c_str())); |
| 163 | |
| 164 | return server::BMC::currentBMCState(value); |
| 165 | } |
| 166 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 167 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 168 | } // namespace manager |
| 169 | } // namespace state |
| 170 | } // namepsace phosphor |
| 171 | |