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