blob: f3bba275704e0f447e03d370c418515320ab4b8c [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 /**
50 * @brief Initialize the system properties and PowerSupply objects from
51 * the JSON config file.
52 * @param[in] path - Path to the JSON config file
53 */
54 void getJSONProperties(const std::string& path);
Brandon Wyman2fe51862019-11-25 16:43:21 -060055
Brandon Wyman2bac8602019-09-12 18:12:21 -050056 /**
57 * Initializes the manager.
58 *
59 * Get current BMC state, ...
60 */
61 void initialize()
62 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050063 // When state = 1, system is powered on
64 int32_t state = 0;
65
66 try
67 {
68 // Use getProperty utility function to get power state.
69 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
70 powerService, bus, state);
71
72 if (state)
73 {
74 powerOn = true;
75 }
76 else
77 {
78 powerOn = false;
79 }
80 }
81 catch (std::exception& e)
82 {
83 log<level::INFO>("Failed to get power state. Assuming it is off.");
84 powerOn = false;
85 }
86
Brandon Wyman59a35792020-06-04 12:37:40 -050087 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -050088 clearFaults();
89 updateInventory();
Brandon Wyman2bac8602019-09-12 18:12:21 -050090 }
91
92 /**
93 * Starts the timer to start monitoring the list of devices.
94 */
95 int run()
96 {
Brandon Wyman2fe51862019-11-25 16:43:21 -060097 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -050098 }
99
100 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500101 * Write PMBus ON_OFF_CONFIG
102 *
103 * This function will be called to cause the PMBus device driver to send the
104 * ON_OFF_CONFIG command. Takes one byte of data.
105 */
106 void onOffConfig(const uint8_t data)
107 {
108 for (auto& psu : psus)
109 {
110 psu->onOffConfig(data);
111 }
112 }
113
114 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500115 * This function will be called in various situations in order to clear
116 * any fault status bits that may have been set, in order to start over
117 * with a clean state. Presence changes and power state changes will want
118 * to clear any faults logged.
119 */
120 void clearFaults()
121 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500122 for (auto& psu : psus)
123 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600124 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500125 }
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 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500142 * Create an error
143 *
144 * @param[in] faultName - 'name' message for the BMC error log entry
145 * @param[in] additionalData - The AdditionalData property for the error
146 */
147 void createError(const std::string& faultName,
148 const std::map<std::string, std::string>& additionalData);
149
150 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500151 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500152 *
153 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500154 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500155 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500156
157 /** @brief True if the power is on. */
158 bool powerOn = false;
159
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500160 /** @brief Used as part of subscribing to power on state changes*/
161 std::string powerService;
162
Brandon Wyman2bac8602019-09-12 18:12:21 -0500163 /** @brief Used to subscribe to D-Bus power on state changes */
164 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
165
166 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500167 * @brief Callback for power state property changes
168 *
169 * Process changes to the powered on state property for the system.
170 *
171 * @param[in] msg - Data associated with the power state signal
172 */
173 void powerStateChanged(sdbusplus::message::message& msg);
174
175 /**
176 * @brief Adds properties to the inventory.
177 *
178 * Reads the values from the devices and writes them to the associated
179 * power supply D-Bus inventory objects.
180 *
181 * This needs to be done on startup, and each time the presence state
182 * changes.
183 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500184 void updateInventory()
185 {
186 for (auto& psu : psus)
187 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600188 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500189 }
190 }
191
192 /**
Adriana Kobylak169975c2021-03-06 15:19:55 +0000193 * @brief The system properties.
Brandon Wymanaed1f752019-11-25 18:10:52 -0600194 */
Adriana Kobylak169975c2021-03-06 15:19:55 +0000195 sys_properties sysProperties;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600196
197 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500198 * @brief The vector for power supplies.
199 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600200 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500201};
202
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500203} // namespace phosphor::power::manager