blob: 13b581e2b1543c5e51ad59c54dd3c3a3c00e6678 [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{
Brandon Wymanaed1f752019-11-25 18:10:52 -060014 int minPowerSupplies;
15 int maxPowerSupplies;
16};
17
Brandon Wymana0f33ce2019-10-17 18:32:29 -050018using namespace phosphor::power::psu;
19using namespace phosphor::logging;
20
Brandon Wyman2bac8602019-09-12 18:12:21 -050021namespace phosphor
22{
23namespace power
24{
25namespace manager
26{
27
28/**
29 * @class PSUManager
30 *
31 * This class will create an object used to manage and monitor a list of power
32 * supply devices.
33 */
34class PSUManager
35{
36 public:
37 PSUManager() = delete;
38 ~PSUManager() = default;
39 PSUManager(const PSUManager&) = delete;
40 PSUManager& operator=(const PSUManager&) = delete;
41 PSUManager(PSUManager&&) = delete;
42 PSUManager& operator=(PSUManager&&) = delete;
43
44 /**
45 * Constructor
46 *
47 * @param[in] bus - D-Bus bus object
48 * @param[in] e - event object
Brandon Wyman2fe51862019-11-25 16:43:21 -060049 * @param[in] configfile - string path to the configuration file
Brandon Wyman2bac8602019-09-12 18:12:21 -050050 */
51 PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
Brandon Wymanaed1f752019-11-25 18:10:52 -060052 const std::string& configfile);
Brandon Wyman2fe51862019-11-25 16:43:21 -060053
Brandon Wymanaed1f752019-11-25 18:10:52 -060054 void getJSONProperties(const std::string& path, sdbusplus::bus::bus& bus,
55 sys_properties& p,
56 std::vector<std::unique_ptr<PowerSupply>>& psus);
Brandon Wyman2fe51862019-11-25 16:43:21 -060057
Brandon Wyman2bac8602019-09-12 18:12:21 -050058 /**
59 * Initializes the manager.
60 *
61 * Get current BMC state, ...
62 */
63 void initialize()
64 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050065 // When state = 1, system is powered on
66 int32_t state = 0;
67
68 try
69 {
70 // Use getProperty utility function to get power state.
71 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
72 powerService, bus, state);
73
74 if (state)
75 {
76 powerOn = true;
77 }
78 else
79 {
80 powerOn = false;
81 }
82 }
83 catch (std::exception& e)
84 {
85 log<level::INFO>("Failed to get power state. Assuming it is off.");
86 powerOn = false;
87 }
88
Brandon Wyman59a35792020-06-04 12:37:40 -050089 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -050090 clearFaults();
91 updateInventory();
Brandon Wyman2bac8602019-09-12 18:12:21 -050092 }
93
94 /**
95 * Starts the timer to start monitoring the list of devices.
96 */
97 int run()
98 {
Brandon Wyman2fe51862019-11-25 16:43:21 -060099 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500100 }
101
102 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500103 * Write PMBus ON_OFF_CONFIG
104 *
105 * This function will be called to cause the PMBus device driver to send the
106 * ON_OFF_CONFIG command. Takes one byte of data.
107 */
108 void onOffConfig(const uint8_t data)
109 {
110 for (auto& psu : psus)
111 {
112 psu->onOffConfig(data);
113 }
114 }
115
116 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500117 * This function will be called in various situations in order to clear
118 * any fault status bits that may have been set, in order to start over
119 * with a clean state. Presence changes and power state changes will want
120 * to clear any faults logged.
121 */
122 void clearFaults()
123 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500124 for (auto& psu : psus)
125 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600126 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500127 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600128
129 faultLogged = false;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500130 }
131
132 private:
133 /**
134 * The D-Bus object
135 */
136 sdbusplus::bus::bus& bus;
137
138 /**
139 * The timer that runs to periodically check the power supplies.
140 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600141 std::unique_ptr<
142 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
143 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500144
145 /**
146 * Analyze the status of each of the power supplies.
147 */
148 void analyze()
149 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500150 for (auto& psu : psus)
151 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600152 psu->analyze();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500153 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600154
155 for (auto& psu : psus)
156 {
157 // TODO: Fault priorities #918
158 if (!faultLogged && psu->isFaulted())
159 {
160 if (psu->hasInputFault())
161 {
162 // TODO: Create error log
163 }
164
165 if (psu->hasMFRFault())
166 {
167 // TODO: Create error log
168 }
169
170 if (psu->hasVINUVFault())
171 {
172 // TODO: Create error log
173 }
174 }
175 }
Brandon Wyman2bac8602019-09-12 18:12:21 -0500176 }
177
178 /** @brief True if the power is on. */
179 bool powerOn = false;
180
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600181 /** @brief True if fault logged. Clear in clearFaults(). */
182 bool faultLogged = false;
183
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500184 /** @brief Used as part of subscribing to power on state changes*/
185 std::string powerService;
186
Brandon Wyman2bac8602019-09-12 18:12:21 -0500187 /** @brief Used to subscribe to D-Bus power on state changes */
188 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
189
190 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500191 * @brief Callback for power state property changes
192 *
193 * Process changes to the powered on state property for the system.
194 *
195 * @param[in] msg - Data associated with the power state signal
196 */
197 void powerStateChanged(sdbusplus::message::message& msg);
198
199 /**
200 * @brief Adds properties to the inventory.
201 *
202 * Reads the values from the devices and writes them to the associated
203 * power supply D-Bus inventory objects.
204 *
205 * This needs to be done on startup, and each time the presence state
206 * changes.
207 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500208 void updateInventory()
209 {
210 for (auto& psu : psus)
211 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600212 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500213 }
214 }
215
216 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600217 * @brief Minimum number of power supplies to operate.
218 */
219 int minPSUs = 1;
220
221 /**
222 * @brief Maximum number of power supplies possible.
223 */
224 int maxPSUs = 1;
225
226 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500227 * @brief The vector for power supplies.
228 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600229 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500230};
231
232} // namespace manager
233} // namespace power
234} // namespace phosphor