Faisal Awada | 9864f83 | 2025-05-30 12:21:00 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "power_supply.hpp" |
| 4 | #include "types.hpp" |
| 5 | #include "utility.hpp" |
| 6 | |
| 7 | #include <phosphor-logging/lg2.hpp> |
| 8 | #include <sdbusplus/bus.hpp> |
| 9 | #include <sdbusplus/bus/match.hpp> |
| 10 | #include <sdbusplus/server/manager.hpp> |
| 11 | #include <sdbusplus/server/object.hpp> |
| 12 | #include <sdeventplus/event.hpp> |
| 13 | #include <sdeventplus/utility/timer.hpp> |
| 14 | #include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp> |
| 15 | |
| 16 | #include <map> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | struct SupportedPsuConfiguration |
| 21 | { |
| 22 | int powerSupplyCount; |
| 23 | std::vector<uint64_t> inputVoltage; |
| 24 | bool powerConfigFullLoad; |
| 25 | }; |
| 26 | |
| 27 | using namespace phosphor::power::psu; |
| 28 | |
| 29 | namespace phosphor::power::chassis |
| 30 | { |
| 31 | |
Faisal Awada | 348168b | 2025-07-08 11:23:02 -0500 | [diff] [blame] | 32 | constexpr uint64_t invalidObjectPathUniqueId = 9999; |
Faisal Awada | 9864f83 | 2025-05-30 12:21:00 -0500 | [diff] [blame] | 33 | using PowerSystemInputsInterface = sdbusplus::xyz::openbmc_project::State:: |
| 34 | Decorator::server::PowerSystemInputs; |
| 35 | using PowerSystemInputsObject = |
| 36 | sdbusplus::server::object_t<PowerSystemInputsInterface>; |
| 37 | |
| 38 | // Validation timeout. Allow 30s to detect if new EM interfaces show up in D-Bus |
| 39 | // before performing the validation. |
| 40 | // Previously the timer was set to 10 seconds was too short, it results in |
| 41 | // incorrect errors being logged, but no real consequence of longer timeout. |
| 42 | constexpr auto validationTimeout = std::chrono::seconds(30); |
| 43 | |
| 44 | /** |
| 45 | * @class Chassis |
| 46 | * |
| 47 | * @brief This class will create an object used to manage and monitor a list of |
| 48 | * power supply devices attached to the chassis. |
| 49 | */ |
| 50 | class Chassis |
| 51 | { |
| 52 | public: |
| 53 | Chassis() = delete; |
| 54 | ~Chassis() = default; |
| 55 | Chassis(const Chassis&) = delete; |
| 56 | Chassis& operator=(const Chassis&) = delete; |
| 57 | Chassis(Chassis&&) = delete; |
| 58 | Chassis& operator=(Chassis&&) = delete; |
| 59 | |
| 60 | /** |
| 61 | * @brief Constructor to read configuration from D-Bus. |
| 62 | * |
| 63 | * @param[in] bus - D-Bus bus object |
| 64 | * @param[in] chassisPath - Chassis path |
Faisal Awada | 348168b | 2025-07-08 11:23:02 -0500 | [diff] [blame] | 65 | * @param[in] event - Event loop object |
Faisal Awada | 9864f83 | 2025-05-30 12:21:00 -0500 | [diff] [blame] | 66 | */ |
Faisal Awada | 348168b | 2025-07-08 11:23:02 -0500 | [diff] [blame] | 67 | Chassis(sdbusplus::bus_t& bus, const std::string& chassisPath, |
| 68 | const sdeventplus::Event& e); |
| 69 | |
| 70 | /** |
| 71 | * @brief Retrieves the unique identifier of the chassis. |
| 72 | * |
| 73 | * @return uint64_t The unique 64 bits identifier of the chassis. |
| 74 | */ |
| 75 | uint64_t getChassisId() |
| 76 | { |
| 77 | return chassisPathUniqueId; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @brief Analyze the status of each of the power supplies. Log errors for |
| 82 | * faults, when and where appropriate. |
| 83 | */ |
| 84 | void analyze(); |
Faisal Awada | 9864f83 | 2025-05-30 12:21:00 -0500 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * @brief Get the status of Power on. |
| 88 | */ |
| 89 | bool isPowerOn() |
| 90 | { |
| 91 | return powerOn; |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | /** |
| 96 | * @brief The D-Bus object |
| 97 | */ |
| 98 | sdbusplus::bus_t& bus; |
| 99 | |
| 100 | /** |
| 101 | * @brief The timer that performs power supply validation as the entity |
| 102 | * manager interfaces show up in d-bus. |
| 103 | */ |
| 104 | std::unique_ptr< |
| 105 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 106 | validationTimer; |
| 107 | |
| 108 | /** @brief True if the power is on. */ |
| 109 | bool powerOn = false; |
| 110 | |
| 111 | /** @brief Used to subscribe to D-Bus power supply presence changes */ |
| 112 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> presenceMatches; |
| 113 | |
| 114 | /** |
| 115 | * @brief Flag to indicate if the validateConfig() function should be run. |
| 116 | * Set to false once the configuration has been validated to avoid |
| 117 | * running multiple times due to interfaces added signal. Set to |
| 118 | * true during power off to trigger the validation on power on. |
| 119 | */ |
| 120 | bool runValidateConfig = true; |
| 121 | |
| 122 | /** |
| 123 | * @brief Map of supported PSU configurations that include the model name |
| 124 | * and their properties. |
| 125 | */ |
| 126 | std::map<std::string, SupportedPsuConfiguration> supportedConfigs; |
| 127 | |
| 128 | /** |
| 129 | * @brief The vector for power supplies. |
| 130 | */ |
| 131 | std::vector<std::unique_ptr<PowerSupply>> psus; |
| 132 | |
| 133 | /** |
| 134 | * @brief The device driver name for all power supplies. |
| 135 | */ |
| 136 | std::string driverName; |
| 137 | |
| 138 | /** |
| 139 | * @brief Chassis D-Bus object path |
| 140 | */ |
| 141 | std::string chassisPath; |
| 142 | |
| 143 | /** |
| 144 | * @brief The Chassis path unique ID |
| 145 | */ |
Faisal Awada | 348168b | 2025-07-08 11:23:02 -0500 | [diff] [blame] | 146 | uint64_t chassisPathUniqueId = invalidObjectPathUniqueId; |
| 147 | |
| 148 | /** |
| 149 | * @brief Declares a constant reference to an sdeventplus::Event to manage |
| 150 | * async processing. |
| 151 | */ |
| 152 | const sdeventplus::Event& eventLoop; |
Faisal Awada | 9864f83 | 2025-05-30 12:21:00 -0500 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * @brief Get PSU properties from D-Bus, use that to build a power supply |
| 156 | * object. |
| 157 | * |
| 158 | * @param[in] properties - A map of property names and values |
| 159 | */ |
| 160 | void getPSUProperties(util::DbusPropertyMap& properties); |
| 161 | |
| 162 | /** |
| 163 | * @brief Get PSU configuration from D-Bus |
| 164 | */ |
| 165 | void getPSUConfiguration(); |
| 166 | |
| 167 | /** |
| 168 | * @brief Initialize the chassis's supported configuration from the |
| 169 | * Supported Configuration D-Bus object provided by the Entity |
| 170 | * Manager. |
| 171 | */ |
| 172 | void getSupportedConfiguration(); |
| 173 | |
| 174 | /** |
| 175 | * @brief Callback for inventory property changes |
| 176 | * |
| 177 | * Process change of the Power Supply presence. |
| 178 | * |
| 179 | * @param[in] msg - Data associated with the Present change signal |
| 180 | **/ |
| 181 | void psuPresenceChanged(sdbusplus::message_t& msg); |
| 182 | |
| 183 | /** |
| 184 | * @brief Helper function to populate the PSU supported configuration |
| 185 | * |
| 186 | * @param[in] properties - A map of property names and values |
| 187 | */ |
| 188 | void populateSupportedConfiguration( |
| 189 | const util::DbusPropertyMap& properties); |
| 190 | |
| 191 | /** |
| 192 | * @brief Build the device driver name for the power supply. |
| 193 | * |
| 194 | * @param[in] i2cbus - i2c bus |
| 195 | * @param[in] i2caddr - i2c bus address |
| 196 | */ |
| 197 | void buildDriverName(uint64_t i2cbus, uint64_t i2caddr); |
| 198 | |
| 199 | /** |
| 200 | * @brief Find PSU with device driver name, then populate the device |
| 201 | * driver name to all PSUs (including missing PSUs). |
| 202 | */ |
| 203 | void populateDriverName(); |
| 204 | |
| 205 | /** |
| 206 | * @brief Get chassis path unique ID. |
| 207 | * |
Faisal Awada | 348168b | 2025-07-08 11:23:02 -0500 | [diff] [blame] | 208 | * @param [in] path - Chassis path. |
| 209 | * @return uint64_t - Chassis path unique ID. |
Faisal Awada | 9864f83 | 2025-05-30 12:21:00 -0500 | [diff] [blame] | 210 | */ |
Faisal Awada | 348168b | 2025-07-08 11:23:02 -0500 | [diff] [blame] | 211 | uint64_t getChassisPathUniqueId(const std::string& path); |
Faisal Awada | 9864f83 | 2025-05-30 12:21:00 -0500 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace phosphor::power::chassis |