blob: 1dc7716f8dc49ffd7be11f4b108aec46ba97fa14 [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>
Patrick Williams6ed41ea2022-04-05 15:50:21 -050015#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050016#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;
Patrick Williams6ed41ea2022-04-05 15:50:21 -050029namespace fs = std::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
Adriana Kobylak396ed8a2022-03-22 15:45:41 +000095 /** @brief Determine status of power into system by examining all the
96 * power-related interfaces of interest
97 */
Andrew Geissler8b1f8622022-01-28 16:37:07 -060098 void determineStatusOfPower();
99
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000100 /** @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 Geissler58a18012018-01-19 19:36:05 -0800110 /**
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 Geissler0029a5d2017-01-24 14:48:35 -0600118
Matthew Barthbe6efab2022-03-01 13:21:45 -0600119 /** @brief Start the systemd unit requested
Andrew Geissler58a18012018-01-19 19:36:05 -0800120 *
Matthew Barthbe6efab2022-03-01 13:21:45 -0600121 * This function calls `StartUnit` on the systemd unit given.
Andrew Geissler58a18012018-01-19 19:36:05 -0800122 *
Matthew Barthbe6efab2022-03-01 13:21:45 -0600123 * @param[in] sysdUnit - Systemd unit
Andrew Geissler58a18012018-01-19 19:36:05 -0800124 */
Matthew Barthbe6efab2022-03-01 13:21:45 -0600125 void startUnit(const std::string& sysdUnit);
Andrew Geisslerce80f242017-01-24 13:25:33 -0600126
Andrew Geissler58a18012018-01-19 19:36:05 -0800127 /**
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. King697474c2017-03-02 11:15:55 -0600138
Andrew Geissler58a18012018-01-19 19:36:05 -0800139 /** @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 Geissler2ec3a7e2016-12-13 22:01:28 -0600148
Andrew Geissler58a18012018-01-19 19:36:05 -0800149 /** @brief Persistent sdbusplus DBus connection. */
150 sdbusplus::bus::bus& bus;
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -0600151
Andrew Geissler58a18012018-01-19 19:36:05 -0800152 /** @brief Used to subscribe to dbus systemd signals **/
153 sdbusplus::bus::match_t systemdSignals;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500154
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000155 /** @brief Watch for any changes to UPS properties **/
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600156 std::unique_ptr<sdbusplus::bus::match_t> uPowerPropChangeSignal;
157
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000158 /** @brief Watch for any changes to PowerSystemInputs properties **/
159 std::unique_ptr<sdbusplus::bus::match_t> powerSysInputsPropChangeSignal;
160
Potin Lai70f36d82022-03-15 10:25:39 +0800161 /** @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 Goruganticb781fe2018-04-06 13:41:01 -0500167 /** @brief Used to Set value of POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500168 uint32_t pohCounter(uint32_t value) override;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500169
William A. Kennington IIId998f822018-10-17 23:17:57 -0700170 /** @brief Used by the timer to update the POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500171 void pohCallback();
William A. Kennington IIId998f822018-10-17 23:17:57 -0700172
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500173 /** @brief Used to restore POHCounter value from persisted file */
174 void restorePOHCounter();
175
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500176 /** @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 Spinler81957842018-07-11 10:37:12 -0500184 serializePOH(const fs::path& dir = fs::path(POH_COUNTER_PERSIST_PATH));
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500185
Matt Spinler81957842018-07-11 10:37:12 -0500186 /** @brief Deserialize a persisted requested POH counter.
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500187 *
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 Spinler81957842018-07-11 10:37:12 -0500194 bool deserializePOH(const fs::path& path, uint32_t& retCounter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500195
Matt Spinler9eab9862018-07-11 14:13:52 -0500196 /** @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 IIId998f822018-10-17 23:17:57 -0700225 /** @brief Timer used for tracking power on hours */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500226 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pohTimer;
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500227
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 Geissler2cf2a262022-02-02 14:38:41 -0600236
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 Kobylak396ed8a2022-03-22 15:45:41 +0000246
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 Geisslera90a31a2016-12-13 16:16:28 -0600256};
257
258} // namespace manager
259} // namespace state
260} // namespace phosphor