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 | 2b5d887 | 2017-02-21 13:37:17 -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"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 31 | constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 32 | |
| 33 | void BMC::discoverInitialState() |
| 34 | { |
| 35 | sdbusplus::message::variant<std::string> currentState; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 36 | sdbusplus::message::object_path unitTargetPath; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 37 | |
| 38 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 39 | SYSTEMD_OBJ_PATH, |
| 40 | SYSTEMD_INTERFACE, |
| 41 | "GetUnit"); |
| 42 | |
| 43 | method.append(obmcStandbyTarget); |
| 44 | |
| 45 | auto result = this->bus.call(method); |
| 46 | |
| 47 | //Check that the bus call didn't result in an error |
| 48 | if(result.is_method_error()) |
| 49 | { |
| 50 | log<level::ERR>("Error in bus call."); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | result.read(unitTargetPath); |
| 55 | |
| 56 | method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 57 | static_cast<const std::string&> |
| 58 | (unitTargetPath).c_str(), |
| 59 | SYSTEMD_PRP_INTERFACE, |
| 60 | "Get"); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 61 | |
| 62 | method.append("org.freedesktop.systemd1.Unit", "ActiveState"); |
| 63 | |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 64 | result = this->bus.call(method); |
| 65 | |
| 66 | //Check that the bus call didn't result in an error |
| 67 | if(result.is_method_error()) |
| 68 | { |
| 69 | log<level::INFO>("Error in bus call."); |
| 70 | return; |
| 71 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 72 | |
| 73 | //Is obmc-standby.target active or inactive? |
| 74 | result.read(currentState); |
| 75 | |
| 76 | if(currentState == activeState) |
| 77 | { |
| 78 | log<level::INFO>("Setting the BMCState field", |
| 79 | entry("CURRENT_BMC_STATE=%s", |
| 80 | "BMC_READY")); |
| 81 | this->currentBMCState(BMCState::Ready); |
| 82 | |
| 83 | //Unsubscribe so we stop processing all other signals |
| 84 | method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 85 | SYSTEMD_OBJ_PATH, |
| 86 | SYSTEMD_INTERFACE, |
| 87 | "Unsubscribe"); |
| 88 | this->bus.call(method); |
| 89 | this->stateSignal.release(); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | log<level::INFO>("Setting the BMCState field", |
| 94 | entry("CURRENT_BMC_STATE=%s", |
| 95 | "BMC_NOTREADY")); |
| 96 | this->currentBMCState(BMCState::NotReady); |
| 97 | } |
| 98 | |
| 99 | return; |
| 100 | } |
| 101 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 102 | void BMC::subscribeToSystemdSignals() |
| 103 | { |
| 104 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 105 | SYSTEMD_OBJ_PATH, |
| 106 | SYSTEMD_INTERFACE, |
| 107 | "Subscribe"); |
| 108 | this->bus.call(method); |
| 109 | |
| 110 | return; |
| 111 | } |
| 112 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 113 | void BMC::executeTransition(const Transition tranReq) |
| 114 | { |
| 115 | //Check to make sure it can be found |
| 116 | auto iter = SYSTEMD_TABLE.find(tranReq); |
| 117 | if (iter == SYSTEMD_TABLE.end()) return; |
| 118 | |
| 119 | const auto& sysdUnit = iter->second; |
| 120 | |
| 121 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 122 | SYSTEMD_OBJ_PATH, |
| 123 | SYSTEMD_INTERFACE, |
| 124 | "StartUnit"); |
| 125 | |
| 126 | method.append(sysdUnit, "replace"); |
| 127 | |
| 128 | this->bus.call(method); |
| 129 | |
| 130 | return; |
| 131 | } |
| 132 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 133 | int BMC::bmcStateChangeSignal(sd_bus_message *msg, void *userData, |
| 134 | sd_bus_error *retError) |
| 135 | { |
| 136 | return static_cast<BMC*>(userData)->bmcStateChange(msg, retError); |
| 137 | } |
| 138 | |
| 139 | int BMC::bmcStateChange(sd_bus_message *msg, |
| 140 | sd_bus_error *retError) |
| 141 | { |
| 142 | uint32_t newStateID {}; |
| 143 | sdbusplus::message::object_path newStateObjPath; |
| 144 | std::string newStateUnit{}; |
| 145 | std::string newStateResult{}; |
| 146 | |
| 147 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 148 | //Read the msg and populate each variable |
| 149 | sdPlusMsg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 150 | |
| 151 | //Caught the signal that indicates the BMC is now BMC_READY |
| 152 | if((newStateUnit == obmcStandbyTarget) && |
| 153 | (newStateResult == signalDone)) |
| 154 | { |
| 155 | log<level::INFO>("BMC_READY"); |
| 156 | this->currentBMCState(BMCState::Ready); |
| 157 | |
| 158 | //Unsubscribe so we stop processing all other signals |
| 159 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 160 | SYSTEMD_OBJ_PATH, |
| 161 | SYSTEMD_INTERFACE, |
| 162 | "Unsubscribe"); |
| 163 | this->bus.call(method); |
| 164 | this->stateSignal.release(); |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 170 | BMC::Transition BMC::requestedBMCTransition(Transition value) |
| 171 | { |
| 172 | log<level::INFO>( |
| 173 | "Setting the RequestedBMCTransition field", |
| 174 | entry("REQUESTED_BMC_TRANSITION=0x%s", |
| 175 | convertForMessage(value).c_str())); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 176 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 177 | executeTransition(value); |
| 178 | return server::BMC::requestedBMCTransition(value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 179 | } |
| 180 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 181 | BMC::BMCState BMC::currentBMCState(BMCState value) |
| 182 | { |
| 183 | log<level::INFO>( |
| 184 | "Setting the BMCState field", |
| 185 | entry("CURRENT_BMC_STATE=0x%s", |
| 186 | convertForMessage(value).c_str())); |
| 187 | |
| 188 | return server::BMC::currentBMCState(value); |
| 189 | } |
| 190 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 191 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 192 | } // namespace manager |
| 193 | } // namespace state |
| 194 | } // namepsace phosphor |
| 195 | |