blob: 82ad2d15d5971fb66540d092efb5fe8cfb69f920 [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) :
Patrick Williams76070742022-04-05 15:20:12 -050050 ChassisInherit(bus, objPath, ChassisInherit::action::defer_emit),
51 bus(bus),
Andrew Geissler58a18012018-01-19 19:36:05 -080052 systemdSignals(
53 bus,
54 sdbusRule::type::signal() + sdbusRule::member("JobRemoved") +
55 sdbusRule::path("/org/freedesktop/systemd1") +
56 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
57 std::bind(std::mem_fn(&Chassis::sysStateChange), this,
William A. Kennington IIId998f822018-10-17 23:17:57 -070058 std::placeholders::_1)),
Potin Lai70f36d82022-03-15 10:25:39 +080059 id(id), pohTimer(sdeventplus::Event::get_default(),
60 std::bind(&Chassis::pohCallback, this),
61 std::chrono::hours{1}, std::chrono::minutes{1})
Andrew Geissler58a18012018-01-19 19:36:05 -080062 {
63 subscribeToSystemdSignals();
Andrew Geissler0029a5d2017-01-24 14:48:35 -060064
Potin Lai70f36d82022-03-15 10:25:39 +080065 createSystemdTargetTable();
66
Matt Spinler9eab9862018-07-11 14:13:52 -050067 restoreChassisStateChangeTime();
68
Andrew Geissler58a18012018-01-19 19:36:05 -080069 determineInitialState();
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060070
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050071 restorePOHCounter(); // restore POHCounter from persisted file
72
Andrew Geissler58a18012018-01-19 19:36:05 -080073 // We deferred this until we could get our property correct
74 this->emit_object_added();
75 }
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060076
Andrew Geissler58a18012018-01-19 19:36:05 -080077 /** @brief Set value of RequestedPowerTransition */
78 Transition requestedPowerTransition(Transition value) override;
Andrew Geisslera90a31a2016-12-13 16:16:28 -060079
Andrew Geissler58a18012018-01-19 19:36:05 -080080 /** @brief Set value of CurrentPowerState */
81 PowerState currentPowerState(PowerState value) override;
Andrew Geisslera90a31a2016-12-13 16:16:28 -060082
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050083 /** @brief Get value of POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -050084 using ChassisInherit::pohCounter;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050085
86 /** @brief Increment POHCounter if Chassis Power state is ON */
87 void startPOHCounter();
88
Andrew Geissler58a18012018-01-19 19:36:05 -080089 private:
Potin Lai70f36d82022-03-15 10:25:39 +080090 /** @brief Create systemd target instance names and mapping table */
91 void createSystemdTargetTable();
92
Andrew Geissler58a18012018-01-19 19:36:05 -080093 /** @brief Determine initial chassis state and set internally */
94 void determineInitialState();
Andrew Geissler0029a5d2017-01-24 14:48:35 -060095
Adriana Kobylak396ed8a2022-03-22 15:45:41 +000096 /** @brief Determine status of power into system by examining all the
97 * power-related interfaces of interest
98 */
Andrew Geissler8b1f8622022-01-28 16:37:07 -060099 void determineStatusOfPower();
100
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000101 /** @brief Determine status of power provided by an Uninterruptible Power
102 * Supply into the system
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400103 *
104 * @return True if UPS power is good, false otherwise
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000105 */
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400106 bool determineStatusOfUPSPower();
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000107
108 /** @brief Determine status of power provided by the power supply units into
109 * the system
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400110 *
111 * @return True if PSU power is good, false otherwise
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000112 */
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400113 bool determineStatusOfPSUPower();
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000114
Andrew Geissler58a18012018-01-19 19:36:05 -0800115 /**
116 * @brief subscribe to the systemd signals
117 *
118 * This object needs to capture when it's systemd targets complete
119 * so it can keep it's state updated
120 *
121 **/
122 void subscribeToSystemdSignals();
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600123
Matthew Barthbe6efab2022-03-01 13:21:45 -0600124 /** @brief Start the systemd unit requested
Andrew Geissler58a18012018-01-19 19:36:05 -0800125 *
Matthew Barthbe6efab2022-03-01 13:21:45 -0600126 * This function calls `StartUnit` on the systemd unit given.
Andrew Geissler58a18012018-01-19 19:36:05 -0800127 *
Matthew Barthbe6efab2022-03-01 13:21:45 -0600128 * @param[in] sysdUnit - Systemd unit
Andrew Geissler58a18012018-01-19 19:36:05 -0800129 */
Matthew Barthbe6efab2022-03-01 13:21:45 -0600130 void startUnit(const std::string& sysdUnit);
Andrew Geisslerce80f242017-01-24 13:25:33 -0600131
Andrew Geissler58a18012018-01-19 19:36:05 -0800132 /**
133 * @brief Determine if target is active
134 *
135 * This function determines if the target is active and
136 * helps prevent misleading log recorded states.
137 *
138 * @param[in] target - Target string to check on
139 *
140 * @return boolean corresponding to state active
141 **/
142 bool stateActive(const std::string& target);
Josh D. King697474c2017-03-02 11:15:55 -0600143
Andrew Geissler58a18012018-01-19 19:36:05 -0800144 /** @brief Check if systemd state change is relevant to this object
145 *
146 * Instance specific interface to handle the detected systemd state
147 * change
148 *
149 * @param[in] msg - Data associated with subscribed signal
150 *
151 */
152 int sysStateChange(sdbusplus::message::message& msg);
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -0600153
Andrew Geissler58a18012018-01-19 19:36:05 -0800154 /** @brief Persistent sdbusplus DBus connection. */
155 sdbusplus::bus::bus& bus;
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -0600156
Andrew Geissler58a18012018-01-19 19:36:05 -0800157 /** @brief Used to subscribe to dbus systemd signals **/
158 sdbusplus::bus::match_t systemdSignals;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500159
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000160 /** @brief Watch for any changes to UPS properties **/
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600161 std::unique_ptr<sdbusplus::bus::match_t> uPowerPropChangeSignal;
162
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000163 /** @brief Watch for any changes to PowerSystemInputs properties **/
164 std::unique_ptr<sdbusplus::bus::match_t> powerSysInputsPropChangeSignal;
165
Potin Lai70f36d82022-03-15 10:25:39 +0800166 /** @brief Chassis id. **/
167 const size_t id = 0;
168
169 /** @brief Transition state to systemd target mapping table. **/
170 std::map<Transition, std::string> systemdTargetTable;
171
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500172 /** @brief Used to Set value of POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500173 uint32_t pohCounter(uint32_t value) override;
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500174
William A. Kennington IIId998f822018-10-17 23:17:57 -0700175 /** @brief Used by the timer to update the POHCounter */
Patrick Williams45a1ed72021-04-30 21:02:43 -0500176 void pohCallback();
William A. Kennington IIId998f822018-10-17 23:17:57 -0700177
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500178 /** @brief Used to restore POHCounter value from persisted file */
179 void restorePOHCounter();
180
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500181 /** @brief Serialize and persist requested POH counter.
182 *
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500183 * @return fs::path - pathname of persisted requested POH counter.
184 */
Allen.Wangba182f02022-03-23 19:01:53 +0800185 fs::path serializePOH();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500186
Matt Spinler81957842018-07-11 10:37:12 -0500187 /** @brief Deserialize a persisted requested POH counter.
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500188 *
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500189 * @param[in] retCounter - deserialized POH counter value
190 *
191 * @return bool - true if the deserialization was successful, false
192 * otherwise.
193 */
Allen.Wangba182f02022-03-23 19:01:53 +0800194 bool deserializePOH(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