blob: ca8fd05c57e62aaf3fe3ff40002108f6e4482cd3 [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 Wyman63ea78b2020-09-24 16:49:09 -050021namespace phosphor::power::manager
Brandon Wyman2bac8602019-09-12 18:12:21 -050022{
23
24/**
25 * @class PSUManager
26 *
27 * This class will create an object used to manage and monitor a list of power
28 * supply devices.
29 */
30class PSUManager
31{
32 public:
33 PSUManager() = delete;
34 ~PSUManager() = default;
35 PSUManager(const PSUManager&) = delete;
36 PSUManager& operator=(const PSUManager&) = delete;
37 PSUManager(PSUManager&&) = delete;
38 PSUManager& operator=(PSUManager&&) = delete;
39
40 /**
41 * Constructor
42 *
43 * @param[in] bus - D-Bus bus object
44 * @param[in] e - event object
Brandon Wyman2fe51862019-11-25 16:43:21 -060045 * @param[in] configfile - string path to the configuration file
Brandon Wyman2bac8602019-09-12 18:12:21 -050046 */
47 PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
Brandon Wymanaed1f752019-11-25 18:10:52 -060048 const std::string& configfile);
Brandon Wyman2fe51862019-11-25 16:43:21 -060049
Brandon Wymanaed1f752019-11-25 18:10:52 -060050 void getJSONProperties(const std::string& path, sdbusplus::bus::bus& bus,
51 sys_properties& p,
52 std::vector<std::unique_ptr<PowerSupply>>& psus);
Brandon Wyman2fe51862019-11-25 16:43:21 -060053
Brandon Wyman2bac8602019-09-12 18:12:21 -050054 /**
55 * Initializes the manager.
56 *
57 * Get current BMC state, ...
58 */
59 void initialize()
60 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050061 // When state = 1, system is powered on
62 int32_t state = 0;
63
64 try
65 {
66 // Use getProperty utility function to get power state.
67 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
68 powerService, bus, state);
69
70 if (state)
71 {
72 powerOn = true;
73 }
74 else
75 {
76 powerOn = false;
77 }
78 }
79 catch (std::exception& e)
80 {
81 log<level::INFO>("Failed to get power state. Assuming it is off.");
82 powerOn = false;
83 }
84
Brandon Wyman59a35792020-06-04 12:37:40 -050085 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -050086 clearFaults();
87 updateInventory();
Brandon Wyman2bac8602019-09-12 18:12:21 -050088 }
89
90 /**
91 * Starts the timer to start monitoring the list of devices.
92 */
93 int run()
94 {
Brandon Wyman2fe51862019-11-25 16:43:21 -060095 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -050096 }
97
98 /**
Brandon Wyman59a35792020-06-04 12:37:40 -050099 * Write PMBus ON_OFF_CONFIG
100 *
101 * This function will be called to cause the PMBus device driver to send the
102 * ON_OFF_CONFIG command. Takes one byte of data.
103 */
104 void onOffConfig(const uint8_t data)
105 {
106 for (auto& psu : psus)
107 {
108 psu->onOffConfig(data);
109 }
110 }
111
112 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500113 * This function will be called in various situations in order to clear
114 * any fault status bits that may have been set, in order to start over
115 * with a clean state. Presence changes and power state changes will want
116 * to clear any faults logged.
117 */
118 void clearFaults()
119 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500120 for (auto& psu : psus)
121 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600122 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500123 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600124
125 faultLogged = false;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500126 }
127
128 private:
129 /**
130 * The D-Bus object
131 */
132 sdbusplus::bus::bus& bus;
133
134 /**
135 * The timer that runs to periodically check the power supplies.
136 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600137 std::unique_ptr<
138 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
139 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500140
141 /**
142 * Analyze the status of each of the power supplies.
143 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500144 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500145
146 /** @brief True if the power is on. */
147 bool powerOn = false;
148
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600149 /** @brief True if fault logged. Clear in clearFaults(). */
150 bool faultLogged = false;
151
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500152 /** @brief Used as part of subscribing to power on state changes*/
153 std::string powerService;
154
Brandon Wyman2bac8602019-09-12 18:12:21 -0500155 /** @brief Used to subscribe to D-Bus power on state changes */
156 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
157
158 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500159 * @brief Callback for power state property changes
160 *
161 * Process changes to the powered on state property for the system.
162 *
163 * @param[in] msg - Data associated with the power state signal
164 */
165 void powerStateChanged(sdbusplus::message::message& msg);
166
167 /**
168 * @brief Adds properties to the inventory.
169 *
170 * Reads the values from the devices and writes them to the associated
171 * power supply D-Bus inventory objects.
172 *
173 * This needs to be done on startup, and each time the presence state
174 * changes.
175 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500176 void updateInventory()
177 {
178 for (auto& psu : psus)
179 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600180 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500181 }
182 }
183
184 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600185 * @brief Minimum number of power supplies to operate.
186 */
187 int minPSUs = 1;
188
189 /**
190 * @brief Maximum number of power supplies possible.
191 */
192 int maxPSUs = 1;
193
194 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500195 * @brief The vector for power supplies.
196 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600197 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500198};
199
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500200} // namespace phosphor::power::manager