blob: b9508691f2553743e63dce59023ab3eaf666a7ab [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 maxPowerSupplies;
15};
16
Brandon Wymana0f33ce2019-10-17 18:32:29 -050017using namespace phosphor::power::psu;
18using namespace phosphor::logging;
19
Brandon Wyman63ea78b2020-09-24 16:49:09 -050020namespace phosphor::power::manager
Brandon Wyman2bac8602019-09-12 18:12:21 -050021{
22
23/**
24 * @class PSUManager
25 *
26 * This class will create an object used to manage and monitor a list of power
27 * supply devices.
28 */
29class PSUManager
30{
31 public:
32 PSUManager() = delete;
33 ~PSUManager() = default;
34 PSUManager(const PSUManager&) = delete;
35 PSUManager& operator=(const PSUManager&) = delete;
36 PSUManager(PSUManager&&) = delete;
37 PSUManager& operator=(PSUManager&&) = delete;
38
39 /**
Brandon Wyman510acaa2020-11-05 18:32:04 -060040 * Constructor to read configuration from JSON file.
Brandon Wyman2bac8602019-09-12 18:12:21 -050041 *
42 * @param[in] bus - D-Bus bus object
43 * @param[in] e - event object
Brandon Wyman2fe51862019-11-25 16:43:21 -060044 * @param[in] configfile - string path to the configuration file
Brandon Wyman2bac8602019-09-12 18:12:21 -050045 */
46 PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
Brandon Wymanaed1f752019-11-25 18:10:52 -060047 const std::string& configfile);
Brandon Wyman2fe51862019-11-25 16:43:21 -060048
Adriana Kobylak169975c2021-03-06 15:19:55 +000049 /**
Brandon Wyman510acaa2020-11-05 18:32:04 -060050 * Constructor to read configuration from D-Bus.
51 *
52 * @param[in] bus - D-Bus bus object
53 * @param[in] e - event object
54 */
55 PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e);
56
57 /**
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060058 * @brief Initialize the PowerSupply objects from the JSON config file.
Adriana Kobylak169975c2021-03-06 15:19:55 +000059 * @param[in] path - Path to the JSON config file
60 */
61 void getJSONProperties(const std::string& path);
Brandon Wyman2fe51862019-11-25 16:43:21 -060062
Brandon Wyman2bac8602019-09-12 18:12:21 -050063 /**
Brandon Wyman510acaa2020-11-05 18:32:04 -060064 * Get PSU properties from D-Bus, use that to build a power supply
65 * object.
66 *
67 * @param[in] properties - A map of property names and values
68 *
69 */
70 void getPSUProperties(util::DbusPropertyMap& properties);
71
72 /**
73 * Get PSU configuration from D-Bus
74 */
75 void getPSUConfiguration();
76
77 /**
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060078 * @brief Initialize the system properties from the Supported Configuration
79 * D-Bus object provided by Entity Manager.
80 */
81 void getSystemProperties();
82
83 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -050084 * Initializes the manager.
85 *
86 * Get current BMC state, ...
87 */
88 void initialize()
89 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050090 // When state = 1, system is powered on
91 int32_t state = 0;
92
93 try
94 {
95 // Use getProperty utility function to get power state.
96 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
97 powerService, bus, state);
98
99 if (state)
100 {
101 powerOn = true;
102 }
103 else
104 {
105 powerOn = false;
106 }
107 }
108 catch (std::exception& e)
109 {
110 log<level::INFO>("Failed to get power state. Assuming it is off.");
111 powerOn = false;
112 }
113
Brandon Wyman59a35792020-06-04 12:37:40 -0500114 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500115 clearFaults();
116 updateInventory();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500117 }
118
119 /**
120 * Starts the timer to start monitoring the list of devices.
121 */
122 int run()
123 {
Brandon Wyman2fe51862019-11-25 16:43:21 -0600124 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500125 }
126
127 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500128 * Write PMBus ON_OFF_CONFIG
129 *
130 * This function will be called to cause the PMBus device driver to send the
131 * ON_OFF_CONFIG command. Takes one byte of data.
132 */
133 void onOffConfig(const uint8_t data)
134 {
135 for (auto& psu : psus)
136 {
137 psu->onOffConfig(data);
138 }
139 }
140
141 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500142 * This function will be called in various situations in order to clear
143 * any fault status bits that may have been set, in order to start over
144 * with a clean state. Presence changes and power state changes will want
145 * to clear any faults logged.
146 */
147 void clearFaults()
148 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500149 for (auto& psu : psus)
150 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600151 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500152 }
Brandon Wyman2bac8602019-09-12 18:12:21 -0500153 }
154
155 private:
156 /**
157 * The D-Bus object
158 */
159 sdbusplus::bus::bus& bus;
160
161 /**
162 * The timer that runs to periodically check the power supplies.
163 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600164 std::unique_ptr<
165 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
166 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500167
168 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500169 * Create an error
170 *
171 * @param[in] faultName - 'name' message for the BMC error log entry
172 * @param[in] additionalData - The AdditionalData property for the error
173 */
174 void createError(const std::string& faultName,
175 const std::map<std::string, std::string>& additionalData);
176
177 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500178 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500179 *
180 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500181 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500182 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500183
184 /** @brief True if the power is on. */
185 bool powerOn = false;
186
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500187 /** @brief Used as part of subscribing to power on state changes*/
188 std::string powerService;
189
Brandon Wyman2bac8602019-09-12 18:12:21 -0500190 /** @brief Used to subscribe to D-Bus power on state changes */
191 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
192
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600193 /** @brief Used to subscribe to Entity Manager interfaces added */
194 std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch;
195
Brandon Wyman2bac8602019-09-12 18:12:21 -0500196 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500197 * @brief Callback for power state property changes
198 *
199 * Process changes to the powered on state property for the system.
200 *
201 * @param[in] msg - Data associated with the power state signal
202 */
203 void powerStateChanged(sdbusplus::message::message& msg);
204
205 /**
Brandon Wyman3e429132021-03-18 18:03:14 -0500206 * @brief Callback for entity-manager interface added
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600207 *
Brandon Wyman3e429132021-03-18 18:03:14 -0500208 * Process the information from the supported configuration and or IBM CFFPS
209 * Connector interface being added.
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600210 *
211 * @param[in] msg - Data associated with the interfaces added signal
212 */
Brandon Wyman3e429132021-03-18 18:03:14 -0500213 void entityManagerIfaceAdded(sdbusplus::message::message& msg);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600214
215 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500216 * @brief Adds properties to the inventory.
217 *
218 * Reads the values from the devices and writes them to the associated
219 * power supply D-Bus inventory objects.
220 *
221 * This needs to be done on startup, and each time the presence state
222 * changes.
223 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500224 void updateInventory()
225 {
226 for (auto& psu : psus)
227 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600228 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500229 }
230 }
231
232 /**
Adriana Kobylake1074d82021-03-16 20:46:44 +0000233 * @brief Helper function to populate the system properties
234 *
235 * @param[in] properties - A map of property names and values
236 */
237 void populateSysProperties(const util::DbusPropertyMap& properties);
238
239 /**
Adriana Kobylak169975c2021-03-06 15:19:55 +0000240 * @brief The system properties.
Brandon Wymanaed1f752019-11-25 18:10:52 -0600241 */
Adriana Kobylak169975c2021-03-06 15:19:55 +0000242 sys_properties sysProperties;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600243
244 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500245 * @brief The vector for power supplies.
246 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600247 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500248};
249
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500250} // namespace phosphor::power::manager