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 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 14 | int minPowerSupplies; |
| 15 | int maxPowerSupplies; |
| 16 | }; |
| 17 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 18 | using namespace phosphor::power::psu; |
| 19 | using namespace phosphor::logging; |
| 20 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 21 | namespace phosphor::power::manager |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 22 | { |
| 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 | */ |
| 30 | class 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 Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 45 | * @param[in] configfile - string path to the configuration file |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 46 | */ |
| 47 | PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e, |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 48 | const std::string& configfile); |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 49 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 50 | void getJSONProperties(const std::string& path, sdbusplus::bus::bus& bus, |
| 51 | sys_properties& p, |
| 52 | std::vector<std::unique_ptr<PowerSupply>>& psus); |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 53 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 54 | /** |
| 55 | * Initializes the manager. |
| 56 | * |
| 57 | * Get current BMC state, ... |
| 58 | */ |
| 59 | void initialize() |
| 60 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 61 | // 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 Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 85 | onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 86 | clearFaults(); |
| 87 | updateInventory(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Starts the timer to start monitoring the list of devices. |
| 92 | */ |
| 93 | int run() |
| 94 | { |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 95 | return timer->get_event().loop(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 99 | * 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 Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 113 | * 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 Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 120 | for (auto& psu : psus) |
| 121 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 122 | psu->clearFaults(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 123 | } |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 124 | |
| 125 | faultLogged = false; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 126 | } |
| 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 Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 137 | std::unique_ptr< |
| 138 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 139 | timer; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Analyze the status of each of the power supplies. |
| 143 | */ |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 144 | void analyze(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 145 | |
| 146 | /** @brief True if the power is on. */ |
| 147 | bool powerOn = false; |
| 148 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 149 | /** @brief True if fault logged. Clear in clearFaults(). */ |
| 150 | bool faultLogged = false; |
| 151 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 152 | /** @brief Used as part of subscribing to power on state changes*/ |
| 153 | std::string powerService; |
| 154 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 155 | /** @brief Used to subscribe to D-Bus power on state changes */ |
| 156 | std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch; |
| 157 | |
| 158 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 159 | * @brief Callback for power state property changes |
| 160 | * |
| 161 | * Process changes to the powered on state property for the system. |
| 162 | * |
| 163 | * @param[in] msg - Data associated with the power state signal |
| 164 | */ |
| 165 | void powerStateChanged(sdbusplus::message::message& msg); |
| 166 | |
| 167 | /** |
| 168 | * @brief Adds properties to the inventory. |
| 169 | * |
| 170 | * Reads the values from the devices and writes them to the associated |
| 171 | * power supply D-Bus inventory objects. |
| 172 | * |
| 173 | * This needs to be done on startup, and each time the presence state |
| 174 | * changes. |
| 175 | */ |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 176 | void updateInventory() |
| 177 | { |
| 178 | for (auto& psu : psus) |
| 179 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 180 | psu->updateInventory(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 185 | * @brief Minimum number of power supplies to operate. |
| 186 | */ |
| 187 | int minPSUs = 1; |
| 188 | |
| 189 | /** |
| 190 | * @brief Maximum number of power supplies possible. |
| 191 | */ |
| 192 | int maxPSUs = 1; |
| 193 | |
| 194 | /** |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 195 | * @brief The vector for power supplies. |
| 196 | */ |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 197 | std::vector<std::unique_ptr<PowerSupply>> psus; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 198 | }; |
| 199 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 200 | } // namespace phosphor::power::manager |