blob: 527b0ff4d93a34bc0ac02643df171d006d8f0cfb [file] [log] [blame]
Faisal Awada9864f832025-05-30 12:21:00 -05001#pragma once
2
3#include "power_supply.hpp"
4#include "types.hpp"
5#include "utility.hpp"
6
7#include <phosphor-logging/lg2.hpp>
8#include <sdbusplus/bus.hpp>
9#include <sdbusplus/bus/match.hpp>
10#include <sdbusplus/server/manager.hpp>
11#include <sdbusplus/server/object.hpp>
12#include <sdeventplus/event.hpp>
13#include <sdeventplus/utility/timer.hpp>
14#include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp>
15
16#include <map>
17#include <string>
18#include <vector>
19
20struct SupportedPsuConfiguration
21{
22 int powerSupplyCount;
23 std::vector<uint64_t> inputVoltage;
24 bool powerConfigFullLoad;
25};
26
27using namespace phosphor::power::psu;
28
29namespace phosphor::power::chassis
30{
31
32constexpr uint32_t invalidObjectPathUniqueId = 9999;
33using PowerSystemInputsInterface = sdbusplus::xyz::openbmc_project::State::
34 Decorator::server::PowerSystemInputs;
35using PowerSystemInputsObject =
36 sdbusplus::server::object_t<PowerSystemInputsInterface>;
37
38// Validation timeout. Allow 30s to detect if new EM interfaces show up in D-Bus
39// before performing the validation.
40// Previously the timer was set to 10 seconds was too short, it results in
41// incorrect errors being logged, but no real consequence of longer timeout.
42constexpr auto validationTimeout = std::chrono::seconds(30);
43
44/**
45 * @class Chassis
46 *
47 * @brief This class will create an object used to manage and monitor a list of
48 * power supply devices attached to the chassis.
49 */
50class Chassis
51{
52 public:
53 Chassis() = delete;
54 ~Chassis() = default;
55 Chassis(const Chassis&) = delete;
56 Chassis& operator=(const Chassis&) = delete;
57 Chassis(Chassis&&) = delete;
58 Chassis& operator=(Chassis&&) = delete;
59
60 /**
61 * @brief Constructor to read configuration from D-Bus.
62 *
63 * @param[in] bus - D-Bus bus object
64 * @param[in] chassisPath - Chassis path
65 */
66 Chassis(sdbusplus::bus_t& bus, const std::string& chassisPath);
67
68 /**
69 * @brief Get the status of Power on.
70 */
71 bool isPowerOn()
72 {
73 return powerOn;
74 }
75
76 private:
77 /**
78 * @brief The D-Bus object
79 */
80 sdbusplus::bus_t& bus;
81
82 /**
83 * @brief The timer that performs power supply validation as the entity
84 * manager interfaces show up in d-bus.
85 */
86 std::unique_ptr<
87 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
88 validationTimer;
89
90 /** @brief True if the power is on. */
91 bool powerOn = false;
92
93 /** @brief Used to subscribe to D-Bus power supply presence changes */
94 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> presenceMatches;
95
96 /**
97 * @brief Flag to indicate if the validateConfig() function should be run.
98 * Set to false once the configuration has been validated to avoid
99 * running multiple times due to interfaces added signal. Set to
100 * true during power off to trigger the validation on power on.
101 */
102 bool runValidateConfig = true;
103
104 /**
105 * @brief Map of supported PSU configurations that include the model name
106 * and their properties.
107 */
108 std::map<std::string, SupportedPsuConfiguration> supportedConfigs;
109
110 /**
111 * @brief The vector for power supplies.
112 */
113 std::vector<std::unique_ptr<PowerSupply>> psus;
114
115 /**
116 * @brief The device driver name for all power supplies.
117 */
118 std::string driverName;
119
120 /**
121 * @brief Chassis D-Bus object path
122 */
123 std::string chassisPath;
124
125 /**
126 * @brief The Chassis path unique ID
127 */
128 uint32_t chassisPathUniqueId = invalidObjectPathUniqueId;
129
130 /**
131 * @brief Get PSU properties from D-Bus, use that to build a power supply
132 * object.
133 *
134 * @param[in] properties - A map of property names and values
135 */
136 void getPSUProperties(util::DbusPropertyMap& properties);
137
138 /**
139 * @brief Get PSU configuration from D-Bus
140 */
141 void getPSUConfiguration();
142
143 /**
144 * @brief Initialize the chassis's supported configuration from the
145 * Supported Configuration D-Bus object provided by the Entity
146 * Manager.
147 */
148 void getSupportedConfiguration();
149
150 /**
151 * @brief Callback for inventory property changes
152 *
153 * Process change of the Power Supply presence.
154 *
155 * @param[in] msg - Data associated with the Present change signal
156 **/
157 void psuPresenceChanged(sdbusplus::message_t& msg);
158
159 /**
160 * @brief Helper function to populate the PSU supported configuration
161 *
162 * @param[in] properties - A map of property names and values
163 */
164 void populateSupportedConfiguration(
165 const util::DbusPropertyMap& properties);
166
167 /**
168 * @brief Build the device driver name for the power supply.
169 *
170 * @param[in] i2cbus - i2c bus
171 * @param[in] i2caddr - i2c bus address
172 */
173 void buildDriverName(uint64_t i2cbus, uint64_t i2caddr);
174
175 /**
176 * @brief Find PSU with device driver name, then populate the device
177 * driver name to all PSUs (including missing PSUs).
178 */
179 void populateDriverName();
180
181 /**
182 * @brief Get chassis path unique ID.
183 *
184 * @return uint32_t - Chassis path unique ID.
185 */
186 uint32_t getChassisPathUniqueId();
187};
188
189} // namespace phosphor::power::chassis