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