Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 2 | #include <sdbusplus/exception.hpp> |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 3 | #include <sdeventplus/event.hpp> |
| 4 | #include <sdeventplus/exception.hpp> |
Saqib Khan | a8006a2 | 2017-02-14 11:37:08 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/elog-errors.hpp> |
| 7 | #include "xyz/openbmc_project/Common/error.hpp" |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 8 | #include "chassis_state_manager.hpp" |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 9 | #include <cereal/archives/json.hpp> |
| 10 | #include <fstream> |
| 11 | #include "config.h" |
| 12 | #include <experimental/filesystem> |
| 13 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace state |
| 17 | { |
| 18 | namespace manager |
| 19 | { |
| 20 | |
| 21 | // When you see server:: you know we're referencing our base class |
| 22 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 23 | |
| 24 | using namespace phosphor::logging; |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 25 | using sdbusplus::exception::SdBusError; |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 26 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 27 | |
Josh D. King | 6838ea9 | 2017-04-11 13:39:18 -0500 | [diff] [blame] | 28 | constexpr auto CHASSIS_STATE_POWEROFF_TGT = "obmc-chassis-poweroff@0.target"; |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 29 | constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT = |
| 30 | "obmc-chassis-hard-poweroff@0.target"; |
Josh D. King | 6838ea9 | 2017-04-11 13:39:18 -0500 | [diff] [blame] | 31 | constexpr auto CHASSIS_STATE_POWERON_TGT = "obmc-chassis-poweron@0.target"; |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 32 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 33 | constexpr auto ACTIVE_STATE = "active"; |
| 34 | constexpr auto ACTIVATING_STATE = "activating"; |
| 35 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 36 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 37 | const std::map<server::Chassis::Transition, std::string> SYSTEMD_TARGET_TABLE = |
| 38 | { |
Andrew Geissler | 8cf2f9a | 2017-07-21 11:58:04 -0500 | [diff] [blame] | 39 | // Use the hard off target to ensure we shutdown immediately |
| 40 | {server::Chassis::Transition::Off, CHASSIS_STATE_HARD_POWEROFF_TGT}, |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 41 | {server::Chassis::Transition::On, CHASSIS_STATE_POWERON_TGT}}; |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 42 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 43 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 44 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 45 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 46 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 47 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 48 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 49 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 50 | void Chassis::subscribeToSystemdSignals() |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 51 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 52 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 53 | SYSTEMD_INTERFACE, "Subscribe"); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 54 | this->bus.call_noreply(method); |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 55 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 56 | return; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 57 | } |
| 58 | |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 59 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 60 | // and persistent storage design is in place and sdbusplus |
| 61 | // has read property function |
| 62 | void Chassis::determineInitialState() |
| 63 | { |
| 64 | sdbusplus::message::variant<int> pgood = -1; |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 65 | auto method = this->bus.new_method_call( |
| 66 | "org.openbmc.control.Power", "/org/openbmc/control/power0", |
| 67 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 68 | |
| 69 | method.append("org.openbmc.control.Power", "pgood"); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 70 | try |
| 71 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 72 | auto reply = this->bus.call(method); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 73 | reply.read(pgood); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 74 | |
William A. Kennington III | 7a0689a | 2018-11-12 17:19:33 -0800 | [diff] [blame] | 75 | if (sdbusplus::message::variant_ns::get<int>(pgood) == 1) |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 76 | { |
| 77 | log<level::INFO>("Initial Chassis State will be On", |
| 78 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 79 | convertForMessage(PowerState::On).c_str())); |
| 80 | server::Chassis::currentPowerState(PowerState::On); |
| 81 | server::Chassis::requestedPowerTransition(Transition::On); |
| 82 | return; |
| 83 | } |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 84 | else |
| 85 | { |
| 86 | // The system is off. If we think it should be on then |
| 87 | // we probably lost AC while up, so set a new state |
| 88 | // change time. |
| 89 | uint64_t lastTime; |
| 90 | PowerState lastState; |
| 91 | |
| 92 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 93 | { |
| 94 | if (lastState == PowerState::On) |
| 95 | { |
| 96 | setStateChangeTime(); |
| 97 | } |
| 98 | } |
| 99 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 100 | } |
| 101 | catch (const SdBusError& e) |
| 102 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 103 | // It's acceptable for the pgood state service to not be available |
| 104 | // since it will notify us of the pgood state when it comes up. |
| 105 | if (e.name() != nullptr && |
| 106 | strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0) |
| 107 | { |
| 108 | goto fail; |
| 109 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 110 | |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 111 | // Only log for unexpected error types. |
| 112 | log<level::ERR>("Error performing call to get pgood", |
| 113 | entry("ERROR=%s", e.what())); |
| 114 | goto fail; |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 115 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 116 | |
| 117 | fail: |
| 118 | log<level::INFO>("Initial Chassis State will be Off", |
| 119 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 120 | convertForMessage(PowerState::Off).c_str())); |
| 121 | server::Chassis::currentPowerState(PowerState::Off); |
| 122 | server::Chassis::requestedPowerTransition(Transition::Off); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 123 | |
| 124 | return; |
| 125 | } |
| 126 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 127 | void Chassis::executeTransition(Transition tranReq) |
| 128 | { |
| 129 | auto sysdTarget = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 130 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 131 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 132 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 133 | |
| 134 | method.append(sysdTarget); |
| 135 | method.append("replace"); |
| 136 | |
| 137 | this->bus.call_noreply(method); |
| 138 | |
| 139 | return; |
| 140 | } |
| 141 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 142 | bool Chassis::stateActive(const std::string& target) |
| 143 | { |
| 144 | sdbusplus::message::variant<std::string> currentState; |
| 145 | sdbusplus::message::object_path unitTargetPath; |
| 146 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 147 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 148 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 149 | |
| 150 | method.append(target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 151 | |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 152 | try |
| 153 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 154 | auto result = this->bus.call(method); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 155 | result.read(unitTargetPath); |
| 156 | } |
| 157 | catch (const SdBusError& e) |
| 158 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 159 | log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what())); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 162 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 163 | method = this->bus.new_method_call( |
| 164 | SYSTEMD_SERVICE, |
| 165 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 166 | SYSTEMD_PROPERTY_IFACE, "Get"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 167 | |
| 168 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 169 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 170 | try |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 171 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 172 | auto result = this->bus.call(method); |
| 173 | result.read(currentState); |
| 174 | } |
| 175 | catch (const SdBusError& e) |
| 176 | { |
| 177 | log<level::ERR>("Error in ActiveState Get", |
| 178 | entry("ERROR=%s", e.what())); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | |
William A. Kennington III | 7a0689a | 2018-11-12 17:19:33 -0800 | [diff] [blame] | 182 | const auto& currentStateStr = |
| 183 | sdbusplus::message::variant_ns::get<std::string>(currentState); |
| 184 | return currentStateStr == ACTIVE_STATE || |
| 185 | currentStateStr == ACTIVATING_STATE; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 186 | } |
| 187 | |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 188 | int Chassis::sysStateChange(sdbusplus::message::message& msg) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 189 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 190 | uint32_t newStateID{}; |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 191 | sdbusplus::message::object_path newStateObjPath; |
| 192 | std::string newStateUnit{}; |
| 193 | std::string newStateResult{}; |
| 194 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 195 | // Read the msg and populate each variable |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 196 | try |
| 197 | { |
| 198 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 199 | } |
| 200 | catch (const SdBusError& e) |
| 201 | { |
| 202 | log<level::ERR>("Error in state change - bad encoding", |
| 203 | entry("ERROR=%s", e.what()), |
| 204 | entry("REPLY_SIG=%s", msg.get_signature())); |
| 205 | return 0; |
| 206 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 207 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 208 | if ((newStateUnit == CHASSIS_STATE_POWEROFF_TGT) && |
| 209 | (newStateResult == "done") && (!stateActive(CHASSIS_STATE_POWERON_TGT))) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 210 | { |
Andrew Jeffery | 55b983e | 2017-04-19 11:11:26 +0930 | [diff] [blame] | 211 | log<level::INFO>("Received signal that power OFF is complete"); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 212 | this->currentPowerState(server::Chassis::PowerState::Off); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 213 | this->setStateChangeTime(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 214 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 215 | else if ((newStateUnit == CHASSIS_STATE_POWERON_TGT) && |
| 216 | (newStateResult == "done") && |
| 217 | (stateActive(CHASSIS_STATE_POWERON_TGT))) |
| 218 | { |
| 219 | log<level::INFO>("Received signal that power ON is complete"); |
| 220 | this->currentPowerState(server::Chassis::PowerState::On); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 221 | this->setStateChangeTime(); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 222 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 227 | Chassis::Transition Chassis::requestedPowerTransition(Transition value) |
| 228 | { |
| 229 | |
| 230 | log<level::INFO>("Change to Chassis Requested Power State", |
| 231 | entry("CHASSIS_REQUESTED_POWER_STATE=%s", |
| 232 | convertForMessage(value).c_str())); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 233 | executeTransition(value); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 234 | return server::Chassis::requestedPowerTransition(value); |
| 235 | } |
| 236 | |
| 237 | Chassis::PowerState Chassis::currentPowerState(PowerState value) |
| 238 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 239 | PowerState chassisPowerState; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 240 | log<level::INFO>("Change to Chassis Power State", |
| 241 | entry("CHASSIS_CURRENT_POWER_STATE=%s", |
| 242 | convertForMessage(value).c_str())); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 243 | |
| 244 | chassisPowerState = server::Chassis::currentPowerState(value); |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 245 | pOHTimer.setEnabled(chassisPowerState == PowerState::On); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 246 | return chassisPowerState; |
| 247 | } |
| 248 | |
| 249 | uint32_t Chassis::pOHCounter(uint32_t value) |
| 250 | { |
| 251 | if (value != pOHCounter()) |
| 252 | { |
| 253 | ChassisInherit::pOHCounter(value); |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 254 | serializePOH(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 255 | } |
| 256 | return pOHCounter(); |
| 257 | } |
| 258 | |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 259 | void Chassis::pOHCallback() |
| 260 | { |
| 261 | if (ChassisInherit::currentPowerState() == PowerState::On) |
| 262 | { |
| 263 | pOHCounter(pOHCounter() + 1); |
| 264 | } |
| 265 | } |
| 266 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 267 | void Chassis::restorePOHCounter() |
| 268 | { |
| 269 | uint32_t counter; |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 270 | if (!deserializePOH(POH_COUNTER_PERSIST_PATH, counter)) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 271 | { |
| 272 | // set to default value |
| 273 | pOHCounter(0); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | pOHCounter(counter); |
| 278 | } |
| 279 | } |
| 280 | |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 281 | fs::path Chassis::serializePOH(const fs::path& path) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 282 | { |
| 283 | std::ofstream os(path.c_str(), std::ios::binary); |
| 284 | cereal::JSONOutputArchive oarchive(os); |
| 285 | oarchive(pOHCounter()); |
| 286 | return path; |
| 287 | } |
| 288 | |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 289 | bool Chassis::deserializePOH(const fs::path& path, uint32_t& pOHCounter) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 290 | { |
| 291 | try |
| 292 | { |
| 293 | if (fs::exists(path)) |
| 294 | { |
| 295 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 296 | cereal::JSONInputArchive iarchive(is); |
| 297 | iarchive(pOHCounter); |
| 298 | return true; |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | catch (cereal::Exception& e) |
| 303 | { |
| 304 | log<level::ERR>(e.what()); |
| 305 | fs::remove(path); |
| 306 | return false; |
| 307 | } |
| 308 | catch (const fs::filesystem_error& e) |
| 309 | { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | void Chassis::startPOHCounter() |
| 317 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 318 | auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path(); |
| 319 | fs::create_directories(dir); |
| 320 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 321 | try |
| 322 | { |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 323 | auto event = sdeventplus::Event::get_default(); |
| 324 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 325 | event.loop(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 326 | } |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 327 | catch (const sdeventplus::SdEventError& e) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 328 | { |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 329 | log<level::ERR>("Error occurred during the sdeventplus loop", |
| 330 | entry("ERROR=%s", e.what())); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 331 | phosphor::logging::commit<InternalFailure>(); |
| 332 | } |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 333 | } |
| 334 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 335 | void Chassis::serializeStateChangeTime() |
| 336 | { |
| 337 | fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH}; |
| 338 | std::ofstream os(path.c_str(), std::ios::binary); |
| 339 | cereal::JSONOutputArchive oarchive(os); |
| 340 | |
| 341 | oarchive(ChassisInherit::lastStateChangeTime(), |
| 342 | ChassisInherit::currentPowerState()); |
| 343 | } |
| 344 | |
| 345 | bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state) |
| 346 | { |
| 347 | fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH}; |
| 348 | |
| 349 | try |
| 350 | { |
| 351 | if (fs::exists(path)) |
| 352 | { |
| 353 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 354 | cereal::JSONInputArchive iarchive(is); |
| 355 | iarchive(time, state); |
| 356 | return true; |
| 357 | } |
| 358 | } |
| 359 | catch (std::exception& e) |
| 360 | { |
| 361 | log<level::ERR>(e.what()); |
| 362 | fs::remove(path); |
| 363 | } |
| 364 | |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | void Chassis::restoreChassisStateChangeTime() |
| 369 | { |
| 370 | uint64_t time; |
| 371 | PowerState state; |
| 372 | |
| 373 | if (!deserializeStateChangeTime(time, state)) |
| 374 | { |
| 375 | ChassisInherit::lastStateChangeTime(0); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | ChassisInherit::lastStateChangeTime(time); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void Chassis::setStateChangeTime() |
| 384 | { |
| 385 | using namespace std::chrono; |
| 386 | uint64_t lastTime; |
| 387 | PowerState lastState; |
| 388 | |
| 389 | auto now = |
| 390 | duration_cast<milliseconds>(system_clock::now().time_since_epoch()) |
| 391 | .count(); |
| 392 | |
| 393 | // If power is on when the BMC is rebooted, this function will get called |
| 394 | // because sysStateChange() runs. Since the power state didn't change |
| 395 | // in this case, neither should the state change time, so check that |
| 396 | // the power state actually did change here. |
| 397 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 398 | { |
| 399 | if (lastState == ChassisInherit::currentPowerState()) |
| 400 | { |
| 401 | return; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | ChassisInherit::lastStateChangeTime(now); |
| 406 | serializeStateChangeTime(); |
| 407 | } |
| 408 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 409 | } // namespace manager |
| 410 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 411 | } // namespace phosphor |