blob: a9a0bd5103b042b5f15951d46cee9bc8dde70a34 [file] [log] [blame]
Brandon Wyman2bac8602019-09-12 18:12:21 -05001#pragma once
2
Brandon Wymana0f33ce2019-10-17 18:32:29 -05003#include "power_supply.hpp"
4#include "types.hpp"
5#include "utility.hpp"
6
7#include <phosphor-logging/log.hpp>
Brandon Wyman2bac8602019-09-12 18:12:21 -05008#include <sdbusplus/bus/match.hpp>
9#include <sdeventplus/event.hpp>
10#include <sdeventplus/utility/timer.hpp>
11
Brandon Wymanaed1f752019-11-25 18:10:52 -060012struct sys_properties
13{
14 int pollInterval;
15 int minPowerSupplies;
16 int maxPowerSupplies;
17};
18
Brandon Wymana0f33ce2019-10-17 18:32:29 -050019using namespace phosphor::power::psu;
20using namespace phosphor::logging;
21
Brandon Wyman2bac8602019-09-12 18:12:21 -050022namespace phosphor
23{
24namespace power
25{
26namespace manager
27{
28
29/**
30 * @class PSUManager
31 *
32 * This class will create an object used to manage and monitor a list of power
33 * supply devices.
34 */
35class PSUManager
36{
37 public:
38 PSUManager() = delete;
39 ~PSUManager() = default;
40 PSUManager(const PSUManager&) = delete;
41 PSUManager& operator=(const PSUManager&) = delete;
42 PSUManager(PSUManager&&) = delete;
43 PSUManager& operator=(PSUManager&&) = delete;
44
45 /**
46 * Constructor
47 *
48 * @param[in] bus - D-Bus bus object
49 * @param[in] e - event object
Brandon Wyman2fe51862019-11-25 16:43:21 -060050 * @param[in] configfile - string path to the configuration file
Brandon Wyman2bac8602019-09-12 18:12:21 -050051 */
52 PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
Brandon Wymanaed1f752019-11-25 18:10:52 -060053 const std::string& configfile);
Brandon Wyman2fe51862019-11-25 16:43:21 -060054
Brandon Wymanaed1f752019-11-25 18:10:52 -060055 void getJSONProperties(const std::string& path, sdbusplus::bus::bus& bus,
56 sys_properties& p,
57 std::vector<std::unique_ptr<PowerSupply>>& psus);
Brandon Wyman2fe51862019-11-25 16:43:21 -060058
Brandon Wyman2bac8602019-09-12 18:12:21 -050059 /**
60 * Initializes the manager.
61 *
62 * Get current BMC state, ...
63 */
64 void initialize()
65 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050066 // When state = 1, system is powered on
67 int32_t state = 0;
68
69 try
70 {
71 // Use getProperty utility function to get power state.
72 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
73 powerService, bus, state);
74
75 if (state)
76 {
77 powerOn = true;
78 }
79 else
80 {
81 powerOn = false;
82 }
83 }
84 catch (std::exception& e)
85 {
86 log<level::INFO>("Failed to get power state. Assuming it is off.");
87 powerOn = false;
88 }
89
Brandon Wyman59a35792020-06-04 12:37:40 -050090 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -050091 clearFaults();
92 updateInventory();
Brandon Wyman2bac8602019-09-12 18:12:21 -050093 }
94
95 /**
96 * Starts the timer to start monitoring the list of devices.
97 */
98 int run()
99 {
Brandon Wyman2fe51862019-11-25 16:43:21 -0600100 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500101 }
102
103 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500104 * Write PMBus ON_OFF_CONFIG
105 *
106 * This function will be called to cause the PMBus device driver to send the
107 * ON_OFF_CONFIG command. Takes one byte of data.
108 */
109 void onOffConfig(const uint8_t data)
110 {
111 for (auto& psu : psus)
112 {
113 psu->onOffConfig(data);
114 }
115 }
116
117 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500118 * This function will be called in various situations in order to clear
119 * any fault status bits that may have been set, in order to start over
120 * with a clean state. Presence changes and power state changes will want
121 * to clear any faults logged.
122 */
123 void clearFaults()
124 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500125 for (auto& psu : psus)
126 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600127 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500128 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600129
130 faultLogged = false;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500131 }
132
133 private:
134 /**
135 * The D-Bus object
136 */
137 sdbusplus::bus::bus& bus;
138
139 /**
140 * The timer that runs to periodically check the power supplies.
141 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600142 std::unique_ptr<
143 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
144 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500145
146 /**
147 * Analyze the status of each of the power supplies.
148 */
149 void analyze()
150 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500151 for (auto& psu : psus)
152 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600153 psu->analyze();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500154 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600155
156 for (auto& psu : psus)
157 {
158 // TODO: Fault priorities #918
159 if (!faultLogged && psu->isFaulted())
160 {
161 if (psu->hasInputFault())
162 {
163 // TODO: Create error log
164 }
165
166 if (psu->hasMFRFault())
167 {
168 // TODO: Create error log
169 }
170
171 if (psu->hasVINUVFault())
172 {
173 // TODO: Create error log
174 }
175 }
176 }
Brandon Wyman2bac8602019-09-12 18:12:21 -0500177 }
178
179 /** @brief True if the power is on. */
180 bool powerOn = false;
181
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600182 /** @brief True if fault logged. Clear in clearFaults(). */
183 bool faultLogged = false;
184
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500185 /** @brief Used as part of subscribing to power on state changes*/
186 std::string powerService;
187
Brandon Wyman2bac8602019-09-12 18:12:21 -0500188 /** @brief Used to subscribe to D-Bus power on state changes */
189 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
190
191 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500192 * @brief Callback for power state property changes
193 *
194 * Process changes to the powered on state property for the system.
195 *
196 * @param[in] msg - Data associated with the power state signal
197 */
198 void powerStateChanged(sdbusplus::message::message& msg);
199
200 /**
201 * @brief Adds properties to the inventory.
202 *
203 * Reads the values from the devices and writes them to the associated
204 * power supply D-Bus inventory objects.
205 *
206 * This needs to be done on startup, and each time the presence state
207 * changes.
208 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500209 void updateInventory()
210 {
211 for (auto& psu : psus)
212 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600213 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500214 }
215 }
216
217 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600218 * @brief Minimum number of power supplies to operate.
219 */
220 int minPSUs = 1;
221
222 /**
223 * @brief Maximum number of power supplies possible.
224 */
225 int maxPSUs = 1;
226
227 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500228 * @brief The vector for power supplies.
229 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600230 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500231};
232
233} // namespace manager
234} // namespace power
235} // namespace phosphor