Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 3 | #include <functional> |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 4 | #include <experimental/filesystem> |
| 5 | #include <cereal/cereal.hpp> |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include "xyz/openbmc_project/State/Chassis/server.hpp" |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 8 | #include "xyz/openbmc_project/State/PowerOnHours/server.hpp" |
| 9 | #include "config.h" |
| 10 | #include "timer.hpp" |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace state |
| 15 | { |
| 16 | namespace manager |
| 17 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 18 | namespace POH |
| 19 | { |
| 20 | |
| 21 | using namespace std::chrono_literals; |
| 22 | constexpr auto hour = 3600s; // seconds Per Hour |
| 23 | |
| 24 | } // namespace POH |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 25 | |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 26 | using ChassisInherit = sdbusplus::server::object::object< |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 27 | sdbusplus::xyz::openbmc_project::State::server::Chassis, |
| 28 | sdbusplus::xyz::openbmc_project::State::server::PowerOnHours>; |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 29 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 30 | namespace fs = std::experimental::filesystem; |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 31 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 32 | /** @class Chassis |
| 33 | * @brief OpenBMC chassis state management implementation. |
| 34 | * @details A concrete implementation for xyz.openbmc_project.State.Chassis |
| 35 | * DBus API. |
| 36 | */ |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 37 | class Chassis : public ChassisInherit |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 38 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 39 | public: |
| 40 | /** @brief Constructs Chassis State Manager |
| 41 | * |
| 42 | * @note This constructor passes 'true' to the base class in order to |
| 43 | * defer dbus object registration until we can run |
| 44 | * determineInitialState() and set our properties |
| 45 | * |
| 46 | * @param[in] bus - The Dbus bus object |
| 47 | * @param[in] instance - The instance of this object |
| 48 | * @param[in] objPath - The Dbus object path |
| 49 | */ |
| 50 | Chassis(sdbusplus::bus::bus& bus, const char* busName, |
| 51 | const char* objPath) : |
| 52 | ChassisInherit(bus, objPath, true), |
| 53 | bus(bus), |
| 54 | systemdSignals( |
| 55 | bus, |
| 56 | sdbusRule::type::signal() + sdbusRule::member("JobRemoved") + |
| 57 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 58 | sdbusRule::interface("org.freedesktop.systemd1.Manager"), |
| 59 | std::bind(std::mem_fn(&Chassis::sysStateChange), this, |
| 60 | std::placeholders::_1)) |
| 61 | { |
| 62 | subscribeToSystemdSignals(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 63 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 64 | determineInitialState(); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 65 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 66 | restorePOHCounter(); // restore POHCounter from persisted file |
| 67 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 68 | // We deferred this until we could get our property correct |
| 69 | this->emit_object_added(); |
| 70 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 71 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 72 | /** @brief Set value of RequestedPowerTransition */ |
| 73 | Transition requestedPowerTransition(Transition value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 74 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 75 | /** @brief Set value of CurrentPowerState */ |
| 76 | PowerState currentPowerState(PowerState value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 77 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 78 | /** @brief Get value of POHCounter */ |
| 79 | using ChassisInherit::pOHCounter; |
| 80 | |
| 81 | /** @brief Increment POHCounter if Chassis Power state is ON */ |
| 82 | void startPOHCounter(); |
| 83 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 84 | private: |
| 85 | /** @brief Determine initial chassis state and set internally */ |
| 86 | void determineInitialState(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 87 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 88 | /** |
| 89 | * @brief subscribe to the systemd signals |
| 90 | * |
| 91 | * This object needs to capture when it's systemd targets complete |
| 92 | * so it can keep it's state updated |
| 93 | * |
| 94 | **/ |
| 95 | void subscribeToSystemdSignals(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 96 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 97 | /** @brief Execute the transition request |
| 98 | * |
| 99 | * This function calls the appropriate systemd target for the input |
| 100 | * transition. |
| 101 | * |
| 102 | * @param[in] tranReq - Transition requested |
| 103 | */ |
| 104 | void executeTransition(Transition tranReq); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 105 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 106 | /** |
| 107 | * @brief Determine if target is active |
| 108 | * |
| 109 | * This function determines if the target is active and |
| 110 | * helps prevent misleading log recorded states. |
| 111 | * |
| 112 | * @param[in] target - Target string to check on |
| 113 | * |
| 114 | * @return boolean corresponding to state active |
| 115 | **/ |
| 116 | bool stateActive(const std::string& target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 117 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 118 | /** @brief Check if systemd state change is relevant to this object |
| 119 | * |
| 120 | * Instance specific interface to handle the detected systemd state |
| 121 | * change |
| 122 | * |
| 123 | * @param[in] msg - Data associated with subscribed signal |
| 124 | * |
| 125 | */ |
| 126 | int sysStateChange(sdbusplus::message::message& msg); |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 127 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 128 | /** @brief Persistent sdbusplus DBus connection. */ |
| 129 | sdbusplus::bus::bus& bus; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 130 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 131 | /** @brief Used to subscribe to dbus systemd signals **/ |
| 132 | sdbusplus::bus::match_t systemdSignals; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame^] | 133 | |
| 134 | /** @brief Used to Set value of POHCounter */ |
| 135 | uint32_t pOHCounter(uint32_t value) override; |
| 136 | |
| 137 | /** @brief Used to restore POHCounter value from persisted file */ |
| 138 | void restorePOHCounter(); |
| 139 | |
| 140 | /** @brief Function required by Cereal to perform serialization. |
| 141 | * |
| 142 | * @tparam Archive - Cereal archive type (json in our case). |
| 143 | * @param[in] archive - reference to Cereal archive. |
| 144 | * @param[in] version - Class version that enables handling |
| 145 | * a serialized data across code levels |
| 146 | */ |
| 147 | template <class Archive> |
| 148 | inline void save(Archive& archive, const std::uint32_t version) const |
| 149 | { |
| 150 | archive(pOHCounter()); |
| 151 | } |
| 152 | |
| 153 | /** @brief Function required by Cereal to perform deserialization. |
| 154 | * |
| 155 | * @tparam Archive - Cereal archive type (json in our case). |
| 156 | * @param[in] archive - reference to Cereal archive. |
| 157 | * @param[in] version - Class version that enables handling |
| 158 | * a serialized data across code levels |
| 159 | */ |
| 160 | template <class Archive> |
| 161 | inline void load(Archive& archive, const std::uint32_t version) |
| 162 | { |
| 163 | uint32_t value; |
| 164 | archive(value); |
| 165 | pOHCounter(value); |
| 166 | } |
| 167 | |
| 168 | /** @brief Serialize and persist requested POH counter. |
| 169 | * |
| 170 | * @param[in] dir - pathname of file where the serialized POH counter will |
| 171 | * be placed. |
| 172 | * |
| 173 | * @return fs::path - pathname of persisted requested POH counter. |
| 174 | */ |
| 175 | fs::path |
| 176 | serialize(const fs::path& dir = fs::path(POH_COUNTER_PERSIST_PATH)); |
| 177 | |
| 178 | /** @brief Deserialze a persisted requested POH counter. |
| 179 | * |
| 180 | * @param[in] path - pathname of persisted POH counter file |
| 181 | * @param[in] retCounter - deserialized POH counter value |
| 182 | * |
| 183 | * @return bool - true if the deserialization was successful, false |
| 184 | * otherwise. |
| 185 | */ |
| 186 | bool deserialize(const fs::path& path, uint32_t& retCounter); |
| 187 | |
| 188 | /** @brief Timer */ |
| 189 | std::unique_ptr<phosphor::state::manager::Timer> timer; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | } // namespace manager |
| 193 | } // namespace state |
| 194 | } // namespace phosphor |