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> |
| 15 | #include <experimental/filesystem> |
| 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; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 29 | namespace fs = std::experimental::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 |
| 47 | */ |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 48 | Chassis(sdbusplus::bus::bus& bus, const char* objPath) : |
| 49 | ChassisInherit(bus, objPath, true), bus(bus), |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 50 | systemdSignals( |
| 51 | bus, |
| 52 | sdbusRule::type::signal() + sdbusRule::member("JobRemoved") + |
| 53 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 54 | sdbusRule::interface("org.freedesktop.systemd1.Manager"), |
| 55 | std::bind(std::mem_fn(&Chassis::sysStateChange), this, |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 56 | std::placeholders::_1)), |
| 57 | pOHTimer(sdeventplus::Event::get_default(), |
| 58 | std::bind(&Chassis::pOHCallback, this), std::chrono::hours{1}, |
| 59 | std::chrono::minutes{1}) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 60 | { |
| 61 | subscribeToSystemdSignals(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 62 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 63 | restoreChassisStateChangeTime(); |
| 64 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 65 | determineInitialState(); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 66 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 67 | restorePOHCounter(); // restore POHCounter from persisted file |
| 68 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 69 | // We deferred this until we could get our property correct |
| 70 | this->emit_object_added(); |
| 71 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 72 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 73 | /** @brief Set value of RequestedPowerTransition */ |
| 74 | Transition requestedPowerTransition(Transition value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 75 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 76 | /** @brief Set value of CurrentPowerState */ |
| 77 | PowerState currentPowerState(PowerState value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 78 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 79 | /** @brief Get value of POHCounter */ |
| 80 | using ChassisInherit::pOHCounter; |
| 81 | |
| 82 | /** @brief Increment POHCounter if Chassis Power state is ON */ |
| 83 | void startPOHCounter(); |
| 84 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 85 | private: |
| 86 | /** @brief Determine initial chassis state and set internally */ |
| 87 | void determineInitialState(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 88 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 89 | /** |
| 90 | * @brief subscribe to the systemd signals |
| 91 | * |
| 92 | * This object needs to capture when it's systemd targets complete |
| 93 | * so it can keep it's state updated |
| 94 | * |
| 95 | **/ |
| 96 | void subscribeToSystemdSignals(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 97 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 98 | /** @brief Execute the transition request |
| 99 | * |
| 100 | * This function calls the appropriate systemd target for the input |
| 101 | * transition. |
| 102 | * |
| 103 | * @param[in] tranReq - Transition requested |
| 104 | */ |
| 105 | void executeTransition(Transition tranReq); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 106 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 107 | /** |
| 108 | * @brief Determine if target is active |
| 109 | * |
| 110 | * This function determines if the target is active and |
| 111 | * helps prevent misleading log recorded states. |
| 112 | * |
| 113 | * @param[in] target - Target string to check on |
| 114 | * |
| 115 | * @return boolean corresponding to state active |
| 116 | **/ |
| 117 | bool stateActive(const std::string& target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 118 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 119 | /** @brief Check if systemd state change is relevant to this object |
| 120 | * |
| 121 | * Instance specific interface to handle the detected systemd state |
| 122 | * change |
| 123 | * |
| 124 | * @param[in] msg - Data associated with subscribed signal |
| 125 | * |
| 126 | */ |
| 127 | int sysStateChange(sdbusplus::message::message& msg); |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 128 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 129 | /** @brief Persistent sdbusplus DBus connection. */ |
| 130 | sdbusplus::bus::bus& bus; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 131 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 132 | /** @brief Used to subscribe to dbus systemd signals **/ |
| 133 | sdbusplus::bus::match_t systemdSignals; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 134 | |
| 135 | /** @brief Used to Set value of POHCounter */ |
| 136 | uint32_t pOHCounter(uint32_t value) override; |
| 137 | |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 138 | /** @brief Used by the timer to update the POHCounter */ |
| 139 | void pOHCallback(); |
| 140 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 141 | /** @brief Used to restore POHCounter value from persisted file */ |
| 142 | void restorePOHCounter(); |
| 143 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 144 | /** @brief Serialize and persist requested POH counter. |
| 145 | * |
| 146 | * @param[in] dir - pathname of file where the serialized POH counter will |
| 147 | * be placed. |
| 148 | * |
| 149 | * @return fs::path - pathname of persisted requested POH counter. |
| 150 | */ |
| 151 | fs::path |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 152 | serializePOH(const fs::path& dir = fs::path(POH_COUNTER_PERSIST_PATH)); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 153 | |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 154 | /** @brief Deserialize a persisted requested POH counter. |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 155 | * |
| 156 | * @param[in] path - pathname of persisted POH counter file |
| 157 | * @param[in] retCounter - deserialized POH counter value |
| 158 | * |
| 159 | * @return bool - true if the deserialization was successful, false |
| 160 | * otherwise. |
| 161 | */ |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 162 | bool deserializePOH(const fs::path& path, uint32_t& retCounter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 163 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 164 | /** @brief Sets the LastStateChangeTime property and persists it. */ |
| 165 | void setStateChangeTime(); |
| 166 | |
| 167 | /** @brief Serialize the last power state change time. |
| 168 | * |
| 169 | * Save the time the state changed and the state itself. |
| 170 | * The state needs to be saved as well so that during rediscovery |
| 171 | * on reboots there's a way to know not to update the time again. |
| 172 | */ |
| 173 | void serializeStateChangeTime(); |
| 174 | |
| 175 | /** @brief Deserialize the last power state change time. |
| 176 | * |
| 177 | * @param[out] time - Deserialized time |
| 178 | * @param[out] state - Deserialized power state |
| 179 | * |
| 180 | * @return bool - true if successful, false otherwise. |
| 181 | */ |
| 182 | bool deserializeStateChangeTime(uint64_t& time, PowerState& state); |
| 183 | |
| 184 | /** @brief Restores the power state change time. |
| 185 | * |
| 186 | * The time is loaded into the LastStateChangeTime D-Bus property. |
| 187 | * On the very first start after this code has been applied but |
| 188 | * before the state has changed, the LastStateChangeTime value |
| 189 | * will be zero. |
| 190 | */ |
| 191 | void restoreChassisStateChangeTime(); |
| 192 | |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 193 | /** @brief Timer used for tracking power on hours */ |
| 194 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pOHTimer; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | } // namespace manager |
| 198 | } // namespace state |
| 199 | } // namespace phosphor |