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