Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
| 5 | #include "xyz/openbmc_project/State/Chassis/server.hpp" |
| 6 | #include "xyz/openbmc_project/State/PowerOnHours/server.hpp" |
| 7 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 8 | #include <cereal/cereal.hpp> |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 9 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 10 | #include <sdeventplus/clock.hpp> |
| 11 | #include <sdeventplus/event.hpp> |
| 12 | #include <sdeventplus/utility/timer.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 13 | |
| 14 | #include <chrono> |
Patrick Williams | 6ed41ea | 2022-04-05 15:50:21 -0500 | [diff] [blame] | 15 | #include <filesystem> |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace state |
| 20 | { |
| 21 | namespace manager |
| 22 | { |
| 23 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 24 | using ChassisInherit = sdbusplus::server::object_t< |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 25 | sdbusplus::xyz::openbmc_project::State::server::Chassis, |
| 26 | sdbusplus::xyz::openbmc_project::State::server::PowerOnHours>; |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 27 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Patrick Williams | 6ed41ea | 2022-04-05 15:50:21 -0500 | [diff] [blame] | 28 | namespace fs = std::filesystem; |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 29 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 30 | /** @class Chassis |
| 31 | * @brief OpenBMC chassis state management implementation. |
| 32 | * @details A concrete implementation for xyz.openbmc_project.State.Chassis |
| 33 | * DBus API. |
| 34 | */ |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 35 | class Chassis : public ChassisInherit |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 36 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 37 | public: |
| 38 | /** @brief Constructs Chassis State Manager |
| 39 | * |
| 40 | * @note This constructor passes 'true' to the base class in order to |
| 41 | * defer dbus object registration until we can run |
| 42 | * determineInitialState() and set our properties |
| 43 | * |
| 44 | * @param[in] bus - The Dbus bus object |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 45 | * @param[in] objPath - The Dbus object path |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 46 | * @param[in] id - Chassis id |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 47 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 48 | Chassis(sdbusplus::bus_t& bus, const char* objPath, size_t id) : |
Patrick Williams | 7607074 | 2022-04-05 15:20:12 -0500 | [diff] [blame] | 49 | ChassisInherit(bus, objPath, ChassisInherit::action::defer_emit), |
| 50 | bus(bus), |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 51 | systemdSignals( |
| 52 | bus, |
| 53 | sdbusRule::type::signal() + sdbusRule::member("JobRemoved") + |
| 54 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 55 | sdbusRule::interface("org.freedesktop.systemd1.Manager"), |
William A. Kennington III | bd28f02 | 2022-11-22 17:11:49 -0800 | [diff] [blame] | 56 | [this](sdbusplus::message_t& m) { sysStateChange(m); }), |
| 57 | id(id), |
| 58 | pohTimer( |
| 59 | sdeventplus::Event::get_default(), [this](auto&) { pohCallback(); }, |
| 60 | std::chrono::hours{1}, std::chrono::minutes{1}) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 61 | { |
| 62 | subscribeToSystemdSignals(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 63 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 64 | createSystemdTargetTable(); |
| 65 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 66 | restoreChassisStateChangeTime(); |
| 67 | |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 68 | // No default in PDI so start at Good, skip D-Bus signal for now |
| 69 | currentPowerStatus(PowerStatus::Good, true); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 70 | determineInitialState(); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 71 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 72 | restorePOHCounter(); // restore POHCounter from persisted file |
| 73 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 74 | // We deferred this until we could get our property correct |
| 75 | this->emit_object_added(); |
| 76 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 77 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 78 | /** @brief Set value of RequestedPowerTransition */ |
| 79 | Transition requestedPowerTransition(Transition value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 80 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 81 | /** @brief Set value of CurrentPowerState */ |
| 82 | PowerState currentPowerState(PowerState value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 83 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 84 | /** @brief Get value of POHCounter */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 85 | using ChassisInherit::pohCounter; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 86 | |
| 87 | /** @brief Increment POHCounter if Chassis Power state is ON */ |
| 88 | void startPOHCounter(); |
| 89 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 90 | private: |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 91 | /** @brief Create systemd target instance names and mapping table */ |
| 92 | void createSystemdTargetTable(); |
| 93 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 94 | /** @brief Determine initial chassis state and set internally */ |
| 95 | void determineInitialState(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 96 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 97 | /** @brief Determine status of power into system by examining all the |
| 98 | * power-related interfaces of interest |
| 99 | */ |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 100 | void determineStatusOfPower(); |
| 101 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 102 | /** @brief Determine status of power provided by an Uninterruptible Power |
| 103 | * Supply into the system |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 104 | * |
| 105 | * @return True if UPS power is good, false otherwise |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 106 | */ |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 107 | bool determineStatusOfUPSPower(); |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 108 | |
| 109 | /** @brief Determine status of power provided by the power supply units into |
| 110 | * the system |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 111 | * |
| 112 | * @return True if PSU power is good, false otherwise |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 113 | */ |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 114 | bool determineStatusOfPSUPower(); |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 115 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 116 | /** |
| 117 | * @brief subscribe to the systemd signals |
| 118 | * |
| 119 | * This object needs to capture when it's systemd targets complete |
| 120 | * so it can keep it's state updated |
| 121 | * |
| 122 | **/ |
| 123 | void subscribeToSystemdSignals(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 124 | |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 125 | /** @brief Start the systemd unit requested |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 126 | * |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 127 | * This function calls `StartUnit` on the systemd unit given. |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 128 | * |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 129 | * @param[in] sysdUnit - Systemd unit |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 130 | */ |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 131 | void startUnit(const std::string& sysdUnit); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 132 | |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 133 | /** @brief Restart the systemd unit requested |
| 134 | * |
| 135 | * This function calls `RestartUnit` on the systemd unit given. |
| 136 | * This is useful when needing to restart a service that is already running |
| 137 | * |
| 138 | * @param[in] sysdUnit - Systemd unit to restart |
| 139 | */ |
| 140 | void restartUnit(const std::string& sysdUnit); |
| 141 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 142 | /** |
| 143 | * @brief Determine if target is active |
| 144 | * |
| 145 | * This function determines if the target is active and |
| 146 | * helps prevent misleading log recorded states. |
| 147 | * |
| 148 | * @param[in] target - Target string to check on |
| 149 | * |
| 150 | * @return boolean corresponding to state active |
| 151 | **/ |
| 152 | bool stateActive(const std::string& target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 153 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 154 | /** @brief Check if systemd state change is relevant to this object |
| 155 | * |
| 156 | * Instance specific interface to handle the detected systemd state |
| 157 | * change |
| 158 | * |
| 159 | * @param[in] msg - Data associated with subscribed signal |
| 160 | * |
| 161 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 162 | int sysStateChange(sdbusplus::message_t& msg); |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 163 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 164 | /** @brief Persistent sdbusplus DBus connection. */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 165 | sdbusplus::bus_t& bus; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 166 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 167 | /** @brief Used to subscribe to dbus systemd signals **/ |
| 168 | sdbusplus::bus::match_t systemdSignals; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 169 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 170 | /** @brief Watch for any changes to UPS properties **/ |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 171 | std::unique_ptr<sdbusplus::bus::match_t> uPowerPropChangeSignal; |
| 172 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 173 | /** @brief Watch for any changes to PowerSystemInputs properties **/ |
| 174 | std::unique_ptr<sdbusplus::bus::match_t> powerSysInputsPropChangeSignal; |
| 175 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 176 | /** @brief Chassis id. **/ |
| 177 | const size_t id = 0; |
| 178 | |
| 179 | /** @brief Transition state to systemd target mapping table. **/ |
| 180 | std::map<Transition, std::string> systemdTargetTable; |
| 181 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 182 | /** @brief Used to Set value of POHCounter */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 183 | uint32_t pohCounter(uint32_t value) override; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 184 | |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 185 | /** @brief Used by the timer to update the POHCounter */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 186 | void pohCallback(); |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 187 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 188 | /** @brief Used to restore POHCounter value from persisted file */ |
| 189 | void restorePOHCounter(); |
| 190 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 191 | /** @brief Serialize and persist requested POH counter. |
| 192 | * |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 193 | * @return fs::path - pathname of persisted requested POH counter. |
| 194 | */ |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 195 | fs::path serializePOH(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 196 | |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 197 | /** @brief Deserialize a persisted requested POH counter. |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 198 | * |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 199 | * @param[in] retCounter - deserialized POH counter value |
| 200 | * |
| 201 | * @return bool - true if the deserialization was successful, false |
| 202 | * otherwise. |
| 203 | */ |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 204 | bool deserializePOH(uint32_t& retCounter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 205 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 206 | /** @brief Sets the LastStateChangeTime property and persists it. */ |
| 207 | void setStateChangeTime(); |
| 208 | |
| 209 | /** @brief Serialize the last power state change time. |
| 210 | * |
| 211 | * Save the time the state changed and the state itself. |
| 212 | * The state needs to be saved as well so that during rediscovery |
| 213 | * on reboots there's a way to know not to update the time again. |
| 214 | */ |
| 215 | void serializeStateChangeTime(); |
| 216 | |
| 217 | /** @brief Deserialize the last power state change time. |
| 218 | * |
| 219 | * @param[out] time - Deserialized time |
| 220 | * @param[out] state - Deserialized power state |
| 221 | * |
| 222 | * @return bool - true if successful, false otherwise. |
| 223 | */ |
| 224 | bool deserializeStateChangeTime(uint64_t& time, PowerState& state); |
| 225 | |
| 226 | /** @brief Restores the power state change time. |
| 227 | * |
| 228 | * The time is loaded into the LastStateChangeTime D-Bus property. |
| 229 | * On the very first start after this code has been applied but |
| 230 | * before the state has changed, the LastStateChangeTime value |
| 231 | * will be zero. |
| 232 | */ |
| 233 | void restoreChassisStateChangeTime(); |
| 234 | |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 235 | /** @brief Timer used for tracking power on hours */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 236 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pohTimer; |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 237 | |
| 238 | /** @brief Function to check for a standby voltage regulator fault |
| 239 | * |
| 240 | * Determine if a standby voltage regulator fault was detected and |
| 241 | * return true or false accordingly. |
| 242 | * |
| 243 | * @return true if fault detected, else false |
| 244 | */ |
| 245 | bool standbyVoltageRegulatorFault(); |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 246 | |
| 247 | /** @brief Process UPS property changes |
| 248 | * |
| 249 | * Instance specific interface to monitor for changes to the UPS |
| 250 | * properties which may impact CurrentPowerStatus |
| 251 | * |
| 252 | * @param[in] msg - Data associated with subscribed signal |
| 253 | * |
| 254 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 255 | void uPowerChangeEvent(sdbusplus::message_t& msg); |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 256 | |
| 257 | /** @brief Process PowerSystemInputs property changes |
| 258 | * |
| 259 | * Instance specific interface to monitor for changes to the |
| 260 | * PowerSystemInputs properties which may impact CurrentPowerStatus |
| 261 | * |
| 262 | * @param[in] msg - Data associated with subscribed signal |
| 263 | * |
| 264 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 265 | void powerSysInputsChangeEvent(sdbusplus::message_t& msg); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | } // namespace manager |
| 269 | } // namespace state |
| 270 | } // namespace phosphor |