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