Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 3 | #include "power_supply.hpp" |
| 4 | #include "types.hpp" |
| 5 | #include "utility.hpp" |
| 6 | |
| 7 | #include <phosphor-logging/log.hpp> |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus/match.hpp> |
| 9 | #include <sdeventplus/event.hpp> |
| 10 | #include <sdeventplus/utility/timer.hpp> |
| 11 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 12 | struct sys_properties |
| 13 | { |
| 14 | int pollInterval; |
| 15 | int minPowerSupplies; |
| 16 | int maxPowerSupplies; |
| 17 | }; |
| 18 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 19 | using namespace phosphor::power::psu; |
| 20 | using namespace phosphor::logging; |
| 21 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace power |
| 25 | { |
| 26 | namespace manager |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * @class PSUManager |
| 31 | * |
| 32 | * This class will create an object used to manage and monitor a list of power |
| 33 | * supply devices. |
| 34 | */ |
| 35 | class PSUManager |
| 36 | { |
| 37 | public: |
| 38 | PSUManager() = delete; |
| 39 | ~PSUManager() = default; |
| 40 | PSUManager(const PSUManager&) = delete; |
| 41 | PSUManager& operator=(const PSUManager&) = delete; |
| 42 | PSUManager(PSUManager&&) = delete; |
| 43 | PSUManager& operator=(PSUManager&&) = delete; |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | * |
| 48 | * @param[in] bus - D-Bus bus object |
| 49 | * @param[in] e - event object |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 50 | * @param[in] configfile - string path to the configuration file |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 51 | */ |
| 52 | PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e, |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 53 | const std::string& configfile); |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 54 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 55 | void getJSONProperties(const std::string& path, sdbusplus::bus::bus& bus, |
| 56 | sys_properties& p, |
| 57 | std::vector<std::unique_ptr<PowerSupply>>& psus); |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 58 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 59 | /** |
| 60 | * Initializes the manager. |
| 61 | * |
| 62 | * Get current BMC state, ... |
| 63 | */ |
| 64 | void initialize() |
| 65 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 66 | // When state = 1, system is powered on |
| 67 | int32_t state = 0; |
| 68 | |
| 69 | try |
| 70 | { |
| 71 | // Use getProperty utility function to get power state. |
| 72 | util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, |
| 73 | powerService, bus, state); |
| 74 | |
| 75 | if (state) |
| 76 | { |
| 77 | powerOn = true; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | powerOn = false; |
| 82 | } |
| 83 | } |
| 84 | catch (std::exception& e) |
| 85 | { |
| 86 | log<level::INFO>("Failed to get power state. Assuming it is off."); |
| 87 | powerOn = false; |
| 88 | } |
| 89 | |
| 90 | clearFaults(); |
| 91 | updateInventory(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Starts the timer to start monitoring the list of devices. |
| 96 | */ |
| 97 | int run() |
| 98 | { |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 99 | return timer->get_event().loop(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * This function will be called in various situations in order to clear |
| 104 | * any fault status bits that may have been set, in order to start over |
| 105 | * with a clean state. Presence changes and power state changes will want |
| 106 | * to clear any faults logged. |
| 107 | */ |
| 108 | void clearFaults() |
| 109 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 110 | for (auto& psu : psus) |
| 111 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 112 | psu->clearFaults(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 113 | } |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 114 | |
| 115 | faultLogged = false; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private: |
| 119 | /** |
| 120 | * The D-Bus object |
| 121 | */ |
| 122 | sdbusplus::bus::bus& bus; |
| 123 | |
| 124 | /** |
| 125 | * The timer that runs to periodically check the power supplies. |
| 126 | */ |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 127 | std::unique_ptr< |
| 128 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 129 | timer; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Analyze the status of each of the power supplies. |
| 133 | */ |
| 134 | void analyze() |
| 135 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 136 | for (auto& psu : psus) |
| 137 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 138 | psu->analyze(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 139 | } |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 140 | |
| 141 | for (auto& psu : psus) |
| 142 | { |
| 143 | // TODO: Fault priorities #918 |
| 144 | if (!faultLogged && psu->isFaulted()) |
| 145 | { |
| 146 | if (psu->hasInputFault()) |
| 147 | { |
| 148 | // TODO: Create error log |
| 149 | } |
| 150 | |
| 151 | if (psu->hasMFRFault()) |
| 152 | { |
| 153 | // TODO: Create error log |
| 154 | } |
| 155 | |
| 156 | if (psu->hasVINUVFault()) |
| 157 | { |
| 158 | // TODO: Create error log |
| 159 | } |
| 160 | } |
| 161 | } |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** @brief True if the power is on. */ |
| 165 | bool powerOn = false; |
| 166 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 167 | /** @brief True if fault logged. Clear in clearFaults(). */ |
| 168 | bool faultLogged = false; |
| 169 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 170 | /** @brief Used as part of subscribing to power on state changes*/ |
| 171 | std::string powerService; |
| 172 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 173 | /** @brief Used to subscribe to D-Bus power on state changes */ |
| 174 | std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch; |
| 175 | |
| 176 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 177 | * @brief Callback for power state property changes |
| 178 | * |
| 179 | * Process changes to the powered on state property for the system. |
| 180 | * |
| 181 | * @param[in] msg - Data associated with the power state signal |
| 182 | */ |
| 183 | void powerStateChanged(sdbusplus::message::message& msg); |
| 184 | |
| 185 | /** |
| 186 | * @brief Adds properties to the inventory. |
| 187 | * |
| 188 | * Reads the values from the devices and writes them to the associated |
| 189 | * power supply D-Bus inventory objects. |
| 190 | * |
| 191 | * This needs to be done on startup, and each time the presence state |
| 192 | * changes. |
| 193 | */ |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 194 | void updateInventory() |
| 195 | { |
| 196 | for (auto& psu : psus) |
| 197 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 198 | psu->updateInventory(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 203 | * @brief Minimum number of power supplies to operate. |
| 204 | */ |
| 205 | int minPSUs = 1; |
| 206 | |
| 207 | /** |
| 208 | * @brief Maximum number of power supplies possible. |
| 209 | */ |
| 210 | int maxPSUs = 1; |
| 211 | |
| 212 | /** |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 213 | * @brief The vector for power supplies. |
| 214 | */ |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 215 | std::vector<std::unique_ptr<PowerSupply>> psus; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | } // namespace manager |
| 219 | } // namespace power |
| 220 | } // namespace phosphor |