blob: 0f93279717f153b7bf41f370cfe3a70009b85ad7 [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 /**
40 * Constructor
41 *
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 /**
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060050 * @brief Initialize the PowerSupply objects from the JSON config file.
Adriana Kobylak169975c2021-03-06 15:19:55 +000051 * @param[in] path - Path to the JSON config file
52 */
53 void getJSONProperties(const std::string& path);
Brandon Wyman2fe51862019-11-25 16:43:21 -060054
Brandon Wyman2bac8602019-09-12 18:12:21 -050055 /**
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060056 * @brief Initialize the system properties from the Supported Configuration
57 * D-Bus object provided by Entity Manager.
58 */
59 void getSystemProperties();
60
61 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -050062 * Initializes the manager.
63 *
64 * Get current BMC state, ...
65 */
66 void initialize()
67 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050068 // When state = 1, system is powered on
69 int32_t state = 0;
70
71 try
72 {
73 // Use getProperty utility function to get power state.
74 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
75 powerService, bus, state);
76
77 if (state)
78 {
79 powerOn = true;
80 }
81 else
82 {
83 powerOn = false;
84 }
85 }
86 catch (std::exception& e)
87 {
88 log<level::INFO>("Failed to get power state. Assuming it is off.");
89 powerOn = false;
90 }
91
Brandon Wyman59a35792020-06-04 12:37:40 -050092 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -050093 clearFaults();
94 updateInventory();
Brandon Wyman2bac8602019-09-12 18:12:21 -050095 }
96
97 /**
98 * Starts the timer to start monitoring the list of devices.
99 */
100 int run()
101 {
Brandon Wyman2fe51862019-11-25 16:43:21 -0600102 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500103 }
104
105 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500106 * Write PMBus ON_OFF_CONFIG
107 *
108 * This function will be called to cause the PMBus device driver to send the
109 * ON_OFF_CONFIG command. Takes one byte of data.
110 */
111 void onOffConfig(const uint8_t data)
112 {
113 for (auto& psu : psus)
114 {
115 psu->onOffConfig(data);
116 }
117 }
118
119 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500120 * This function will be called in various situations in order to clear
121 * any fault status bits that may have been set, in order to start over
122 * with a clean state. Presence changes and power state changes will want
123 * to clear any faults logged.
124 */
125 void clearFaults()
126 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500127 for (auto& psu : psus)
128 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600129 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500130 }
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 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500147 * Create an error
148 *
149 * @param[in] faultName - 'name' message for the BMC error log entry
150 * @param[in] additionalData - The AdditionalData property for the error
151 */
152 void createError(const std::string& faultName,
153 const std::map<std::string, std::string>& additionalData);
154
155 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500156 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500157 *
158 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500159 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500160 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500161
162 /** @brief True if the power is on. */
163 bool powerOn = false;
164
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500165 /** @brief Used as part of subscribing to power on state changes*/
166 std::string powerService;
167
Brandon Wyman2bac8602019-09-12 18:12:21 -0500168 /** @brief Used to subscribe to D-Bus power on state changes */
169 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
170
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600171 /** @brief Used to subscribe to Entity Manager interfaces added */
172 std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch;
173
Brandon Wyman2bac8602019-09-12 18:12:21 -0500174 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500175 * @brief Callback for power state property changes
176 *
177 * Process changes to the powered on state property for the system.
178 *
179 * @param[in] msg - Data associated with the power state signal
180 */
181 void powerStateChanged(sdbusplus::message::message& msg);
182
183 /**
Brandon Wyman3e429132021-03-18 18:03:14 -0500184 * @brief Callback for entity-manager interface added
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600185 *
Brandon Wyman3e429132021-03-18 18:03:14 -0500186 * Process the information from the supported configuration and or IBM CFFPS
187 * Connector interface being added.
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600188 *
189 * @param[in] msg - Data associated with the interfaces added signal
190 */
Brandon Wyman3e429132021-03-18 18:03:14 -0500191 void entityManagerIfaceAdded(sdbusplus::message::message& msg);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600192
193 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500194 * @brief Adds properties to the inventory.
195 *
196 * Reads the values from the devices and writes them to the associated
197 * power supply D-Bus inventory objects.
198 *
199 * This needs to be done on startup, and each time the presence state
200 * changes.
201 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500202 void updateInventory()
203 {
204 for (auto& psu : psus)
205 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600206 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500207 }
208 }
209
210 /**
Adriana Kobylake1074d82021-03-16 20:46:44 +0000211 * @brief Helper function to populate the system properties
212 *
213 * @param[in] properties - A map of property names and values
214 */
215 void populateSysProperties(const util::DbusPropertyMap& properties);
216
217 /**
Adriana Kobylak169975c2021-03-06 15:19:55 +0000218 * @brief The system properties.
Brandon Wymanaed1f752019-11-25 18:10:52 -0600219 */
Adriana Kobylak169975c2021-03-06 15:19:55 +0000220 sys_properties sysProperties;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600221
222 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500223 * @brief The vector for power supplies.
224 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600225 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500226};
227
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500228} // namespace phosphor::power::manager