blob: b48409b4465a045d044c24b3e7050cb9ad18e031 [file] [log] [blame]
Andrew Geisslera90a31a2016-12-13 16:16:28 -06001#pragma once
2
Andrew Geisslere426b582020-05-28 12:40:55 -05003#include "config.h"
4
5#include "xyz/openbmc_project/State/Chassis/server.hpp"
6#include "xyz/openbmc_project/State/PowerOnHours/server.hpp"
7
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -05008#include <cereal/cereal.hpp>
Andrew Geisslera90a31a2016-12-13 16:16:28 -06009#include <sdbusplus/bus.hpp>
William A. Kennington IIId998f822018-10-17 23:17:57 -070010#include <sdeventplus/clock.hpp>
11#include <sdeventplus/event.hpp>
12#include <sdeventplus/utility/timer.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050013
14#include <chrono>
15#include <experimental/filesystem>
16#include <functional>
Andrew Geisslera90a31a2016-12-13 16:16:28 -060017
18namespace phosphor
19{
20namespace state
21{
22namespace manager
23{
24
Patrick Williams8f8ba392017-05-05 15:47:39 -050025using ChassisInherit = sdbusplus::server::object::object<
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050026 sdbusplus::xyz::openbmc_project::State::server::Chassis,
27 sdbusplus::xyz::openbmc_project::State::server::PowerOnHours>;
Patrick Williams8f8ba392017-05-05 15:47:39 -050028namespace sdbusRule = sdbusplus::bus::match::rules;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050029namespace fs = std::experimental::filesystem;
Patrick Williams8f8ba392017-05-05 15:47:39 -050030
Andrew Geisslera90a31a2016-12-13 16:16:28 -060031/** @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 Williams8f8ba392017-05-05 15:47:39 -050036class Chassis : public ChassisInherit
Andrew Geisslera90a31a2016-12-13 16:16:28 -060037{
Andrew Geissler58a18012018-01-19 19:36:05 -080038 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 Geissler58a18012018-01-19 19:36:05 -080046 * @param[in] objPath - The Dbus object path
Potin Lai70f36d82022-03-15 10:25:39 +080047 * @param[in] id - Chassis id
Andrew Geissler58a18012018-01-19 19:36:05 -080048 */
Potin Lai70f36d82022-03-15 10:25:39 +080049 Chassis(sdbusplus::bus::bus& bus, const char* objPath, size_t id) :
Andrew Geissler769a62f2019-12-06 13:36:08 -060050 ChassisInherit(bus, objPath, true), bus(bus),
Andrew Geissler58a18012018-01-19 19:36:05 -080051 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 IIId998f822018-10-17 23:17:57 -070057 std::placeholders::_1)),
Potin Lai70f36d82022-03-15 10:25:39 +080058 id(id), pohTimer(sdeventplus::Event::get_default(),
59 std::bind(&Chassis::pohCallback, this),
60 std::chrono::hours{1}, std::chrono::minutes{1})
Andrew Geissler58a18012018-01-19 19:36:05 -080061 {
62 subscribeToSystemdSignals();
Andrew Geissler0029a5d2017-01-24 14:48:35 -060063
Potin Lai70f36d82022-03-15 10:25:39 +080064 createSystemdTargetTable();
65
Matt Spinler9eab9862018-07-11 14:13:52 -050066 restoreChassisStateChangeTime();
67
Andrew Geissler58a18012018-01-19 19:36:05 -080068 determineInitialState();
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060069
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050070 restorePOHCounter(); // restore POHCounter from persisted file
71
Andrew Geissler58a18012018-01-19 19:36:05 -080072 // We deferred this until we could get our property correct
73 this->emit_object_added();
74 }
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060075
Andrew Geissler58a18012018-01-19 19:36:05 -080076 /** @brief Set value of RequestedPowerTransition */
77 Transition requestedPowerTransition(Transition value) override;
Andrew Geisslera90a31a2016-12-13 16:16:28 -060078
Andrew Geissler58a18012018-01-19 19:36:05 -080079 /** @brief Set value of CurrentPowerState */
80 PowerState currentPowerState(PowerState value) override;
Andrew Geisslera90a31a2016-12-13 16:16:28 -060081
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050082 /** @brief Get value of POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -050083 using ChassisInherit::pohCounter;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050084
85 /** @brief Increment POHCounter if Chassis Power state is ON */
86 void startPOHCounter();
87
Andrew Geissler58a18012018-01-19 19:36:05 -080088 private:
Potin Lai70f36d82022-03-15 10:25:39 +080089 /** @brief Create systemd target instance names and mapping table */
90 void createSystemdTargetTable();
91
Andrew Geissler58a18012018-01-19 19:36:05 -080092 /** @brief Determine initial chassis state and set internally */
93 void determineInitialState();
Andrew Geissler0029a5d2017-01-24 14:48:35 -060094
Andrew Geissler8b1f8622022-01-28 16:37:07 -060095 /** @brief Determine status of power into system */
96 void determineStatusOfPower();
97
Andrew Geissler58a18012018-01-19 19:36:05 -080098 /**
99 * @brief subscribe to the systemd signals
100 *
101 * This object needs to capture when it's systemd targets complete
102 * so it can keep it's state updated
103 *
104 **/
105 void subscribeToSystemdSignals();
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600106
Matthew Barthbe6efab2022-03-01 13:21:45 -0600107 /** @brief Start the systemd unit requested
Andrew Geissler58a18012018-01-19 19:36:05 -0800108 *
Matthew Barthbe6efab2022-03-01 13:21:45 -0600109 * This function calls `StartUnit` on the systemd unit given.
Andrew Geissler58a18012018-01-19 19:36:05 -0800110 *
Matthew Barthbe6efab2022-03-01 13:21:45 -0600111 * @param[in] sysdUnit - Systemd unit
Andrew Geissler58a18012018-01-19 19:36:05 -0800112 */
Matthew Barthbe6efab2022-03-01 13:21:45 -0600113 void startUnit(const std::string& sysdUnit);
Andrew Geisslerce80f242017-01-24 13:25:33 -0600114
Andrew Geissler58a18012018-01-19 19:36:05 -0800115 /**
116 * @brief Determine if target is active
117 *
118 * This function determines if the target is active and
119 * helps prevent misleading log recorded states.
120 *
121 * @param[in] target - Target string to check on
122 *
123 * @return boolean corresponding to state active
124 **/
125 bool stateActive(const std::string& target);
Josh D. King697474c2017-03-02 11:15:55 -0600126
Andrew Geissler58a18012018-01-19 19:36:05 -0800127 /** @brief Check if systemd state change is relevant to this object
128 *
129 * Instance specific interface to handle the detected systemd state
130 * change
131 *
132 * @param[in] msg - Data associated with subscribed signal
133 *
134 */
135 int sysStateChange(sdbusplus::message::message& msg);
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -0600136
Andrew Geissler58a18012018-01-19 19:36:05 -0800137 /** @brief Persistent sdbusplus DBus connection. */
138 sdbusplus::bus::bus& bus;
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -0600139
Andrew Geissler58a18012018-01-19 19:36:05 -0800140 /** @brief Used to subscribe to dbus systemd signals **/
141 sdbusplus::bus::match_t systemdSignals;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500142
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600143 /** @brief Watch for any changes to UPS properties **/
144 std::unique_ptr<sdbusplus::bus::match_t> uPowerPropChangeSignal;
145
Potin Lai70f36d82022-03-15 10:25:39 +0800146 /** @brief Chassis id. **/
147 const size_t id = 0;
148
149 /** @brief Transition state to systemd target mapping table. **/
150 std::map<Transition, std::string> systemdTargetTable;
151
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500152 /** @brief Used to Set value of POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500153 uint32_t pohCounter(uint32_t value) override;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500154
William A. Kennington IIId998f822018-10-17 23:17:57 -0700155 /** @brief Used by the timer to update the POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500156 void pohCallback();
William A. Kennington IIId998f822018-10-17 23:17:57 -0700157
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500158 /** @brief Used to restore POHCounter value from persisted file */
159 void restorePOHCounter();
160
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500161 /** @brief Serialize and persist requested POH counter.
162 *
163 * @param[in] dir - pathname of file where the serialized POH counter will
164 * be placed.
165 *
166 * @return fs::path - pathname of persisted requested POH counter.
167 */
168 fs::path
Matt Spinler81957842018-07-11 10:37:12 -0500169 serializePOH(const fs::path& dir = fs::path(POH_COUNTER_PERSIST_PATH));
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500170
Matt Spinler81957842018-07-11 10:37:12 -0500171 /** @brief Deserialize a persisted requested POH counter.
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500172 *
173 * @param[in] path - pathname of persisted POH counter file
174 * @param[in] retCounter - deserialized POH counter value
175 *
176 * @return bool - true if the deserialization was successful, false
177 * otherwise.
178 */
Matt Spinler81957842018-07-11 10:37:12 -0500179 bool deserializePOH(const fs::path& path, uint32_t& retCounter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500180
Matt Spinler9eab9862018-07-11 14:13:52 -0500181 /** @brief Sets the LastStateChangeTime property and persists it. */
182 void setStateChangeTime();
183
184 /** @brief Serialize the last power state change time.
185 *
186 * Save the time the state changed and the state itself.
187 * The state needs to be saved as well so that during rediscovery
188 * on reboots there's a way to know not to update the time again.
189 */
190 void serializeStateChangeTime();
191
192 /** @brief Deserialize the last power state change time.
193 *
194 * @param[out] time - Deserialized time
195 * @param[out] state - Deserialized power state
196 *
197 * @return bool - true if successful, false otherwise.
198 */
199 bool deserializeStateChangeTime(uint64_t& time, PowerState& state);
200
201 /** @brief Restores the power state change time.
202 *
203 * The time is loaded into the LastStateChangeTime D-Bus property.
204 * On the very first start after this code has been applied but
205 * before the state has changed, the LastStateChangeTime value
206 * will be zero.
207 */
208 void restoreChassisStateChangeTime();
209
William A. Kennington IIId998f822018-10-17 23:17:57 -0700210 /** @brief Timer used for tracking power on hours */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500211 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pohTimer;
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500212
213 /** @brief Function to check for a standby voltage regulator fault
214 *
215 * Determine if a standby voltage regulator fault was detected and
216 * return true or false accordingly.
217 *
218 * @return true if fault detected, else false
219 */
220 bool standbyVoltageRegulatorFault();
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600221
222 /** @brief Process UPS property changes
223 *
224 * Instance specific interface to monitor for changes to the UPS
225 * properties which may impact CurrentPowerStatus
226 *
227 * @param[in] msg - Data associated with subscribed signal
228 *
229 */
230 void uPowerChangeEvent(sdbusplus::message::message& msg);
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600231};
232
233} // namespace manager
234} // namespace state
235} // namespace phosphor