blob: 06d7c82d630568759a8d83b9f02aad169990da71 [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 Wyman2bac8602019-09-12 18:12:21 -0500124 }
125
126 private:
127 /**
128 * The D-Bus object
129 */
130 sdbusplus::bus::bus& bus;
131
132 /**
133 * The timer that runs to periodically check the power supplies.
134 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600135 std::unique_ptr<
136 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
137 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500138
139 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500140 * Create an error
141 *
142 * @param[in] faultName - 'name' message for the BMC error log entry
143 * @param[in] additionalData - The AdditionalData property for the error
144 */
145 void createError(const std::string& faultName,
146 const std::map<std::string, std::string>& additionalData);
147
148 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500149 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500150 *
151 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500152 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500153 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500154
155 /** @brief True if the power is on. */
156 bool powerOn = false;
157
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500158 /** @brief Used as part of subscribing to power on state changes*/
159 std::string powerService;
160
Brandon Wyman2bac8602019-09-12 18:12:21 -0500161 /** @brief Used to subscribe to D-Bus power on state changes */
162 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
163
164 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500165 * @brief Callback for power state property changes
166 *
167 * Process changes to the powered on state property for the system.
168 *
169 * @param[in] msg - Data associated with the power state signal
170 */
171 void powerStateChanged(sdbusplus::message::message& msg);
172
173 /**
174 * @brief Adds properties to the inventory.
175 *
176 * Reads the values from the devices and writes them to the associated
177 * power supply D-Bus inventory objects.
178 *
179 * This needs to be done on startup, and each time the presence state
180 * changes.
181 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500182 void updateInventory()
183 {
184 for (auto& psu : psus)
185 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600186 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500187 }
188 }
189
190 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600191 * @brief Minimum number of power supplies to operate.
192 */
193 int minPSUs = 1;
194
195 /**
196 * @brief Maximum number of power supplies possible.
197 */
198 int maxPSUs = 1;
199
200 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500201 * @brief The vector for power supplies.
202 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600203 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500204};
205
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500206} // namespace phosphor::power::manager