Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 1 | #pragma once |
Matt Spinler | 1a309f7 | 2023-04-04 13:12:19 -0500 | [diff] [blame] | 2 | |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 3 | #include "config.hpp" |
Matt Spinler | 1a309f7 | 2023-04-04 13:12:19 -0500 | [diff] [blame] | 4 | #include "power_button_profile.hpp" |
| 5 | |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <sdbusplus/bus/match.hpp> |
| 8 | |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 9 | #include <algorithm> |
| 10 | #include <numeric> |
| 11 | #include <sstream> |
| 12 | #include <string> |
| 13 | |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace button |
| 17 | { |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 18 | enum class PowerEvent |
| 19 | { |
| 20 | powerPressed, |
Naveen Moses | ab8dac5 | 2022-07-15 19:32:27 +0530 | [diff] [blame] | 21 | resetPressed, |
| 22 | powerReleased, |
Naveen Moses | ab8dac5 | 2022-07-15 19:32:27 +0530 | [diff] [blame] | 23 | resetReleased |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 24 | }; |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 25 | |
| 26 | enum class PwrCtl |
| 27 | { |
| 28 | chassisOn, |
| 29 | chassisOff, |
| 30 | chassisCycle, |
| 31 | }; |
| 32 | |
| 33 | inline static size_t numberOfChassis() |
| 34 | { |
| 35 | return instances.size(); |
| 36 | } |
| 37 | |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 38 | /** |
| 39 | * @class Handler |
| 40 | * |
| 41 | * This class acts on the signals generated by the |
| 42 | * xyz.openbmc_project.Chassis.Buttons code when |
| 43 | * it detects button presses. |
| 44 | * |
| 45 | * There are 3 buttons supported - Power, ID, and Reset. |
| 46 | * As not all systems may implement each button, this class will |
| 47 | * check for that button on D-Bus before listening for its signals. |
| 48 | */ |
| 49 | class Handler |
| 50 | { |
| 51 | public: |
| 52 | Handler() = delete; |
| 53 | ~Handler() = default; |
| 54 | Handler(const Handler&) = delete; |
| 55 | Handler& operator=(const Handler&) = delete; |
| 56 | Handler(Handler&&) = delete; |
| 57 | Handler& operator=(Handler&&) = delete; |
| 58 | |
| 59 | /** |
| 60 | * @brief Constructor |
| 61 | * |
| 62 | * @param[in] bus - sdbusplus connection object |
| 63 | */ |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 64 | explicit Handler(sdbusplus::bus_t& bus); |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | /** |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 68 | * @brief The handler for a power button press |
| 69 | * |
DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 70 | * It will do power action according to the pressing duration. |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 71 | * |
| 72 | * @param[in] msg - sdbusplus message from signal |
| 73 | */ |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 74 | void powerReleased(sdbusplus::message_t& msg); |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 75 | |
| 76 | /** |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 77 | * @brief The handler for an ID button press |
| 78 | * |
| 79 | * Toggles the ID LED group |
| 80 | * |
| 81 | * @param[in] msg - sdbusplus message from signal |
| 82 | */ |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 83 | void idReleased(sdbusplus::message_t& msg); |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 84 | |
| 85 | /** |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 86 | * @brief The handler for a reset button press |
| 87 | * |
| 88 | * Reboots the host if it is powered on. |
| 89 | * |
| 90 | * @param[in] msg - sdbusplus message from signal |
| 91 | */ |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 92 | void resetReleased(sdbusplus::message_t& msg); |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 93 | |
| 94 | /** |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 95 | * @brief The handler for a OCP debug card host selector button press |
| 96 | * |
| 97 | * In multi host system increases host position by 1 up to max host |
| 98 | * position. |
| 99 | * |
| 100 | * @param[in] msg - sdbusplus message from signal |
| 101 | */ |
| 102 | |
Patrick Williams | e3b4e11 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 103 | void debugHostSelectorReleased(sdbusplus::message_t& msg); |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 104 | |
| 105 | /** |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 106 | * @brief Checks if system is powered on |
| 107 | * |
| 108 | * @return true if powered on, false else |
| 109 | */ |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 110 | bool poweredOn(size_t hostNumber) const; |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 111 | |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 112 | /* |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 113 | * @return std::string - the D-Bus service name if found, else |
| 114 | * an empty string |
| 115 | */ |
| 116 | std::string getService(const std::string& path, |
| 117 | const std::string& interface) const; |
| 118 | |
| 119 | /** |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 120 | * @brief gets the valid host selector value in multi host |
| 121 | * system |
| 122 | * |
| 123 | * @return size_t throws exception if host selector position is |
| 124 | * invalid or not available. |
| 125 | */ |
| 126 | |
| 127 | size_t getHostSelectorValue(); |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 128 | /** |
| 129 | * @brief increases the host selector position property |
| 130 | * by 1 upto max host selector position |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 134 | |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 135 | void increaseHostSelectorPosition(); |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 136 | /** |
| 137 | * @brief checks if the system has multi host |
| 138 | * based on the host selector property availability |
| 139 | * |
| 140 | * @return bool returns true if multi host system |
| 141 | * else returns false. |
| 142 | */ |
| 143 | bool isMultiHost(); |
| 144 | /** |
| 145 | * @brief trigger the power ctrl event based on the |
| 146 | * button press event type. |
| 147 | * |
| 148 | * @return void |
| 149 | */ |
DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 150 | void handlePowerEvent(PowerEvent powerEventType, |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 151 | const std::string& objectPath, |
DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 152 | std::chrono::microseconds duration); |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 153 | |
| 154 | /** |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 155 | * @brief sdbusplus connection object |
| 156 | */ |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 157 | sdbusplus::bus_t& bus; |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * @brief Matches on the power button released signal |
| 161 | */ |
| 162 | std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased; |
| 163 | |
| 164 | /** |
| 165 | * @brief Matches on the power button long press released signal |
| 166 | */ |
Naveen Moses | ab8dac5 | 2022-07-15 19:32:27 +0530 | [diff] [blame] | 167 | std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed; |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 168 | |
| 169 | /** |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 170 | * @brief Matches on the multi power button released signal |
| 171 | */ |
| 172 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> |
| 173 | multiPowerButtonReleased; |
| 174 | |
| 175 | /** |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 176 | * @brief Matches on the ID button released signal |
| 177 | */ |
| 178 | std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased; |
| 179 | |
| 180 | /** |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 181 | * @brief Matches on the reset button released signal |
| 182 | */ |
| 183 | std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased; |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * @brief Matches on the ocp debug host selector button released signal |
| 187 | */ |
| 188 | std::unique_ptr<sdbusplus::bus::match_t> debugHSButtonReleased; |
Matt Spinler | 1a309f7 | 2023-04-04 13:12:19 -0500 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * @brief The custom power handler profile object. |
| 192 | */ |
| 193 | std::unique_ptr<PowerButtonProfile> powerButtonProfile; |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 194 | |
| 195 | /** |
| 196 | * @brief Flag to indicate multi power button mode(false) or host select |
| 197 | * button mode(true) |
| 198 | */ |
| 199 | bool hostSelectButtonMode = false; |
| 200 | |
| 201 | /** |
| 202 | * @brief Flag to indicate if the button supports multi action |
| 203 | */ |
| 204 | bool isButtonMultiActionSupport = true; |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | } // namespace button |
| 208 | } // namespace phosphor |