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