| George Liu | d6a1bae | 2022-06-20 13:47:31 +0800 | [diff] [blame] | 1 | #include "button_handler.hpp" |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 2 | |
| Delphine CC Chiu | 15c60e2 | 2024-04-12 13:01:32 -0500 | [diff] [blame] | 3 | #include "config.hpp" |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 4 | #include "gpio.hpp" |
| Matt Spinler | 1a309f7 | 2023-04-04 13:12:19 -0500 | [diff] [blame] | 5 | #include "power_button_profile_factory.hpp" |
| 6 | |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Chassis/Buttons/Power/server.hpp> |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 9 | #include <xyz/openbmc_project/State/Chassis/server.hpp> |
| 10 | #include <xyz/openbmc_project/State/Host/server.hpp> |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 11 | |
| 12 | #include <fstream> |
| 13 | #include <iostream> |
| 14 | #include <string> |
| 15 | |
| Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 16 | namespace phosphor |
| 17 | { |
| 18 | namespace button |
| 19 | { |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 20 | |
| 21 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 22 | using namespace sdbusplus::xyz::openbmc_project::State::server; |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 23 | using namespace sdbusplus::xyz::openbmc_project::Chassis::Buttons::server; |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 24 | using GetSubTreePathsType = std::vector<std::string>; |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 25 | |
| 26 | const std::map<std::string, Chassis::Transition> chassisPwrCtls = { |
| 27 | {"chassis-on", Chassis::Transition::On}, |
| 28 | {"chassis-off", Chassis::Transition::Off}, |
| 29 | {"chassis-cycle", Chassis::Transition::PowerCycle}}; |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 30 | |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 31 | constexpr auto chassisIface = "xyz.openbmc_project.State.Chassis"; |
| 32 | constexpr auto hostIface = "xyz.openbmc_project.State.Host"; |
| 33 | constexpr auto powerButtonIface = "xyz.openbmc_project.Chassis.Buttons.Power"; |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 34 | constexpr auto idButtonIface = "xyz.openbmc_project.Chassis.Buttons.ID"; |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 35 | constexpr auto resetButtonIface = "xyz.openbmc_project.Chassis.Buttons.Reset"; |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 36 | constexpr auto ledGroupIface = "xyz.openbmc_project.Led.Group"; |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 37 | constexpr auto ledGroupBasePath = "/xyz/openbmc_project/led/groups/"; |
| 38 | constexpr auto hostSelectorIface = |
| 39 | "xyz.openbmc_project.Chassis.Buttons.HostSelector"; |
| 40 | constexpr auto debugHostSelectorIface = |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 41 | "xyz.openbmc_project.Chassis.Buttons.Button"; |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 42 | |
| 43 | constexpr auto propertyIface = "org.freedesktop.DBus.Properties"; |
| 44 | constexpr auto mapperIface = "xyz.openbmc_project.ObjectMapper"; |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 45 | |
| 46 | constexpr auto mapperObjPath = "/xyz/openbmc_project/object_mapper"; |
| 47 | constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 48 | constexpr auto BMC_POSITION = 0; |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 49 | |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 50 | std::vector<std::map<uint16_t, Chassis::Transition>> multiPwrBtnActConf; |
| 51 | |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 52 | Handler::Handler(sdbusplus::bus_t& bus) : bus(bus) |
| Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 53 | { |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 54 | /* So far, there are two modes for multi-host power control |
| 55 | - host select button mode, e.g.: Yosemite V2 |
| 56 | only one power button with host select switch, |
| 57 | which's interface for handling target host, |
| 58 | in the case, hostSelectButtonMode = true |
| 59 | - multi power button mode, e.g.: Greatlakes |
| 60 | each slot/sled has its own power button, |
| 61 | in the case, hostSelectButtonMode = false */ |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 62 | |
| 63 | std::ifstream gpios{gpioDefFile}; |
| 64 | auto configDefJson = nlohmann::json::parse(gpios, nullptr, true); |
| 65 | nlohmann::json gpioDefs = configDefJson["gpio_definitions"]; |
| 66 | |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 67 | bool isHostSelectorExists = |
| 68 | !getService(HS_DBUS_OBJECT_NAME, hostSelectorIface).empty(); |
| 69 | |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 70 | for (const auto& gpioConfig : gpioDefs) |
| 71 | { |
| 72 | if (gpioConfig.contains("multi-action")) |
| 73 | { |
| 74 | std::map<uint16_t, Chassis::Transition> mapEntry; |
| 75 | const auto& multiActCfg = gpioConfig["multi-action"]; |
| 76 | for (const auto& ActCfg : multiActCfg) |
| 77 | { |
| 78 | auto chassisPwrCtl = chassisPwrCtls.find(ActCfg["action"]); |
| 79 | if (chassisPwrCtl != chassisPwrCtls.end()) |
| 80 | { |
| 81 | auto duration = ActCfg["duration"].get<uint16_t>(); |
| 82 | mapEntry[duration] = chassisPwrCtl->second; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | lg2::error("unknown power button action"); |
| 87 | } |
| 88 | } |
| 89 | multiPwrBtnActConf.emplace_back(mapEntry); |
| 90 | } |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 91 | |
| 92 | if (gpioConfig.value("name", "") == "HOST_SELECTOR") |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 93 | { |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 94 | virtualButton = gpioConfig.value("virtual_button", false); |
| 95 | lg2::debug("Change virtualButton flag to: {VIRTUAL}", "VIRTUAL", |
| 96 | virtualButton); |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 100 | hostSelectButtonMode = isHostSelectorExists && !virtualButton; |
| 101 | lg2::debug("hostSelectButtonMode: {MODE}", "MODE", hostSelectButtonMode); |
| 102 | |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 103 | try |
| 104 | { |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 105 | static const int depth = 1; |
| 106 | GetSubTreePathsType powerButtonSubTreePaths; |
| 107 | auto method = bus.new_method_call(mapperService, mapperObjPath, |
| 108 | mapperIface, "GetSubTreePaths"); |
| 109 | method.append("/xyz/openbmc_project/Chassis/Buttons", depth, |
| 110 | GetSubTreePathsType({powerButtonIface})); |
| 111 | auto reply = bus.call(method); |
| 112 | reply.read(powerButtonSubTreePaths); |
| 113 | if (!powerButtonSubTreePaths.empty()) |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 114 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 115 | lg2::info("Starting power button handler"); |
| Matt Spinler | 1a309f7 | 2023-04-04 13:12:19 -0500 | [diff] [blame] | 116 | |
| 117 | // Check for a custom handler |
| 118 | powerButtonProfile = |
| 119 | PowerButtonProfileFactory::instance().createProfile(bus); |
| 120 | |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 121 | // Fallback: register for all power buttons found |
| Matt Spinler | 1a309f7 | 2023-04-04 13:12:19 -0500 | [diff] [blame] | 122 | if (!powerButtonProfile) |
| 123 | { |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 124 | for (const auto& path : powerButtonSubTreePaths) |
| 125 | { |
| 126 | lg2::debug("Registering power button handler for: {PATH}", |
| 127 | "PATH", path); |
| 128 | std::unique_ptr<sdbusplus::bus::match_t> |
| 129 | multiPowerReleaseMatch = |
| 130 | std::make_unique<sdbusplus::bus::match_t>( |
| 131 | bus, |
| 132 | sdbusRule::type::signal() + |
| 133 | sdbusRule::member("Released") + |
| 134 | sdbusRule::path(path) + |
| 135 | sdbusRule::interface(powerButtonIface), |
| 136 | std::bind(std::mem_fn(&Handler::powerReleased), |
| 137 | this, std::placeholders::_1)); |
| 138 | multiPowerButtonReleased.emplace_back( |
| 139 | std::move(multiPowerReleaseMatch)); |
| 140 | } |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 141 | } |
| 142 | } |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 143 | } |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 144 | catch (const sdbusplus::exception_t& e) |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 145 | { |
| Liora Guo | fbd3bd5 | 2025-10-09 16:47:52 +0800 | [diff] [blame^] | 146 | lg2::error("Error creating power button handlers: '{ERROR_MESSAGE}'", |
| 147 | "ERROR_MESSAGE", e.what()); |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 148 | } |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 149 | |
| 150 | try |
| 151 | { |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 152 | if (!getService(ID_DBUS_OBJECT_NAME, idButtonIface).empty()) |
| 153 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 154 | lg2::info("Registering ID button handler"); |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 155 | idButtonReleased = std::make_unique<sdbusplus::bus::match_t>( |
| 156 | bus, |
| 157 | sdbusRule::type::signal() + sdbusRule::member("Released") + |
| 158 | sdbusRule::path(ID_DBUS_OBJECT_NAME) + |
| 159 | sdbusRule::interface(idButtonIface), |
| Naveen Moses | ab8dac5 | 2022-07-15 19:32:27 +0530 | [diff] [blame] | 160 | std::bind(std::mem_fn(&Handler::idReleased), this, |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 161 | std::placeholders::_1)); |
| 162 | } |
| 163 | } |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 164 | catch (const sdbusplus::exception_t& e) |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 165 | { |
| 166 | // The button wasn't implemented |
| 167 | } |
| 168 | |
| 169 | try |
| 170 | { |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 171 | if (!getService(RESET_DBUS_OBJECT_NAME, resetButtonIface).empty()) |
| 172 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 173 | lg2::info("Registering reset button handler"); |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 174 | resetButtonReleased = std::make_unique<sdbusplus::bus::match_t>( |
| 175 | bus, |
| 176 | sdbusRule::type::signal() + sdbusRule::member("Released") + |
| 177 | sdbusRule::path(RESET_DBUS_OBJECT_NAME) + |
| 178 | sdbusRule::interface(resetButtonIface), |
| Naveen Moses | ab8dac5 | 2022-07-15 19:32:27 +0530 | [diff] [blame] | 179 | std::bind(std::mem_fn(&Handler::resetReleased), this, |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 180 | std::placeholders::_1)); |
| 181 | } |
| 182 | } |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 183 | catch (const sdbusplus::exception_t& e) |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 184 | { |
| 185 | // The button wasn't implemented |
| 186 | } |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 187 | try |
| 188 | { |
| 189 | if (!getService(DBG_HS_DBUS_OBJECT_NAME, debugHostSelectorIface) |
| 190 | .empty()) |
| 191 | { |
| 192 | lg2::info("Registering debug host selector button handler"); |
| 193 | debugHSButtonReleased = std::make_unique<sdbusplus::bus::match_t>( |
| 194 | bus, |
| 195 | sdbusRule::type::signal() + sdbusRule::member("Released") + |
| 196 | sdbusRule::path(DBG_HS_DBUS_OBJECT_NAME) + |
| 197 | sdbusRule::interface(debugHostSelectorIface), |
| 198 | std::bind(std::mem_fn(&Handler::debugHostSelectorReleased), |
| 199 | this, std::placeholders::_1)); |
| 200 | } |
| 201 | } |
| Patrick Williams | e3b4e11 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 202 | catch (const sdbusplus::exception_t& e) |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 203 | { |
| 204 | // The button wasn't implemented |
| 205 | } |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 206 | } |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 207 | bool Handler::isMultiHost() |
| 208 | { |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 209 | if (numberOfChassis() != 1) |
| 210 | { |
| 211 | return true; |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | return (hostSelectButtonMode); |
| 216 | } |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 217 | } |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 218 | std::string Handler::getService(const std::string& path, |
| 219 | const std::string& interface) const |
| 220 | { |
| 221 | auto method = bus.new_method_call(mapperService, mapperObjPath, mapperIface, |
| 222 | "GetObject"); |
| 223 | method.append(path, std::vector{interface}); |
| Thang Q. Nguyen | 4250785 | 2022-08-12 09:11:35 +0700 | [diff] [blame] | 224 | try |
| 225 | { |
| 226 | auto result = bus.call(method); |
| Patrick Williams | 60dc341 | 2025-11-05 00:05:37 -0500 | [diff] [blame] | 227 | auto objectData = |
| 228 | result.unpack<std::map<std::string, std::vector<std::string>>>(); |
| 229 | |
| Thang Q. Nguyen | 4250785 | 2022-08-12 09:11:35 +0700 | [diff] [blame] | 230 | return objectData.begin()->first; |
| 231 | } |
| Patrick Williams | e3b4e11 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 232 | catch (const sdbusplus::exception_t& e) |
| Thang Q. Nguyen | 4250785 | 2022-08-12 09:11:35 +0700 | [diff] [blame] | 233 | { |
| 234 | return std::string(); |
| 235 | } |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 236 | } |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 237 | size_t Handler::getHostSelectorValue() |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 238 | { |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 239 | auto HSService = getService(HS_DBUS_OBJECT_NAME, hostSelectorIface); |
| 240 | |
| 241 | if (HSService.empty()) |
| 242 | { |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 243 | lg2::info("Host selector dbus object not available"); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 244 | throw std::invalid_argument("Host selector dbus object not available"); |
| 245 | } |
| 246 | |
| 247 | try |
| 248 | { |
| 249 | auto method = bus.new_method_call( |
| 250 | HSService.c_str(), HS_DBUS_OBJECT_NAME, propertyIface, "Get"); |
| 251 | method.append(hostSelectorIface, "Position"); |
| 252 | auto result = bus.call(method); |
| 253 | |
| Patrick Williams | 60dc341 | 2025-11-05 00:05:37 -0500 | [diff] [blame] | 254 | auto HSPositionVariant = result.unpack<std::variant<size_t>>(); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 255 | |
| 256 | auto position = std::get<size_t>(HSPositionVariant); |
| 257 | return position; |
| 258 | } |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 259 | catch (const sdbusplus::exception_t& e) |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 260 | { |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 261 | lg2::error("Error reading host selector position: {ERROR}", "ERROR", e); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 262 | throw; |
| 263 | } |
| 264 | } |
| 265 | bool Handler::poweredOn(size_t hostNumber) const |
| 266 | { |
| Potin Lai | c0fee46 | 2022-12-23 14:19:23 +0800 | [diff] [blame] | 267 | auto hostObjectName = HOST_STATE_OBJECT_NAME + std::to_string(hostNumber); |
| 268 | auto service = getService(hostObjectName.c_str(), hostIface); |
| 269 | auto method = bus.new_method_call(service.c_str(), hostObjectName.c_str(), |
| 270 | propertyIface, "Get"); |
| 271 | method.append(hostIface, "CurrentHostState"); |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 272 | auto result = bus.call(method); |
| 273 | |
| Patrick Williams | 60dc341 | 2025-11-05 00:05:37 -0500 | [diff] [blame] | 274 | auto state = result.unpack<std::variant<std::string>>(); |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 275 | |
| Potin Lai | c0fee46 | 2022-12-23 14:19:23 +0800 | [diff] [blame] | 276 | return Host::HostState::Off != |
| 277 | Host::convertHostStateFromString(std::get<std::string>(state)); |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 278 | } |
| 279 | |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 280 | void Handler::handlePowerEvent(PowerEvent powerEventType, |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 281 | const std::string& objectPath, |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 282 | std::chrono::microseconds duration) |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 283 | { |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 284 | std::string objPathName; |
| 285 | std::string dbusIfaceName; |
| 286 | std::string transitionName; |
| 287 | std::variant<Host::Transition, Chassis::Transition> transition; |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 288 | |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 289 | size_t hostNumber = 0; |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 290 | std::string hostNumStr = |
| 291 | objectPath.substr(std::string(POWER_DBUS_OBJECT_NAME).length()); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 292 | auto isMultiHostSystem = isMultiHost(); |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 293 | |
| 294 | if (hostSelectButtonMode) |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 295 | { |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 296 | hostNumber = getHostSelectorValue(); |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 297 | lg2::info("Multi-host system detected : {POSITION}", "POSITION", |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 298 | hostNumber); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 299 | |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 300 | hostNumStr = std::to_string(hostNumber); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 301 | |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 302 | // ignore power and reset button events if BMC is selected. |
| 303 | if (isMultiHostSystem && (hostNumber == BMC_POSITION) && |
| 304 | (powerEventType != PowerEvent::powerReleased) && |
| 305 | (duration <= LONG_PRESS_TIME_MS)) |
| 306 | { |
| 307 | lg2::info( |
| 308 | "handlePowerEvent : BMC selected on multi-host system. ignoring power and reset button events..."); |
| 309 | return; |
| 310 | } |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | switch (powerEventType) |
| 314 | { |
| Naveen Moses | ab8dac5 | 2022-07-15 19:32:27 +0530 | [diff] [blame] | 315 | case PowerEvent::powerReleased: |
| George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 316 | { |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 317 | for (const auto& iter : multiPwrBtnActConf[stoi(hostNumStr) - 1]) |
| 318 | { |
| 319 | if (duration > std::chrono::milliseconds(iter.first)) |
| 320 | { |
| 321 | dbusIfaceName = chassisIface; |
| 322 | transitionName = "RequestedPowerTransition"; |
| 323 | objPathName = CHASSIS_STATE_OBJECT_NAME + hostNumStr; |
| 324 | transition = iter.second; |
| 325 | } |
| 326 | } |
| 327 | break; |
| 328 | |
| Patrick Williams | 971312a | 2024-04-12 13:45:50 -0500 | [diff] [blame] | 329 | if (duration <= LONG_PRESS_TIME_MS) |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 330 | { |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 331 | objPathName = HOST_STATE_OBJECT_NAME + hostNumStr; |
| 332 | dbusIfaceName = hostIface; |
| 333 | transitionName = "RequestedHostTransition"; |
| 334 | |
| 335 | transition = Host::Transition::On; |
| 336 | |
| 337 | if (poweredOn(hostNumber)) |
| 338 | { |
| 339 | transition = Host::Transition::Off; |
| 340 | } |
| 341 | lg2::info("handlePowerEvent : Handle power button press "); |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 342 | break; |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 343 | } |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 344 | else |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 345 | { |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 346 | dbusIfaceName = chassisIface; |
| 347 | transitionName = "RequestedPowerTransition"; |
| 348 | objPathName = CHASSIS_STATE_OBJECT_NAME + hostNumStr; |
| 349 | transition = Chassis::Transition::Off; |
| 350 | |
| 351 | /* multi host system : |
| 352 | hosts (1 to N) - host shutdown |
| 353 | bmc (0) - sled cycle |
| 354 | single host system : |
| 355 | host(0) - host shutdown |
| 356 | */ |
| 357 | if (isMultiHostSystem && (hostNumber == BMC_POSITION)) |
| 358 | { |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 359 | #if CHASSIS_SYSTEM_RESET_ENABLED |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 360 | objPathName = CHASSISSYSTEM_STATE_OBJECT_NAME + hostNumStr; |
| 361 | transition = Chassis::Transition::PowerCycle; |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 362 | #else |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 363 | return; |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 364 | #endif |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 365 | } |
| 366 | else if (!poweredOn(hostNumber)) |
| 367 | { |
| 368 | lg2::info( |
| 369 | "Power is off so ignoring long power button press"); |
| 370 | return; |
| 371 | } |
| 372 | lg2::info("handlePowerEvent : handle long power button press"); |
| 373 | break; |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 374 | } |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 375 | } |
| Naveen Moses | ab8dac5 | 2022-07-15 19:32:27 +0530 | [diff] [blame] | 376 | case PowerEvent::resetReleased: |
| George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 377 | { |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 378 | objPathName = HOST_STATE_OBJECT_NAME + hostNumStr; |
| 379 | dbusIfaceName = hostIface; |
| 380 | transitionName = "RequestedHostTransition"; |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 381 | |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 382 | if (!poweredOn(hostNumber)) |
| 383 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 384 | lg2::info("Power is off so ignoring reset button press"); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 385 | return; |
| 386 | } |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 387 | |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 388 | lg2::info("Handling reset button press"); |
| HuyLe | 9456ffc | 2023-06-22 15:21:22 +0700 | [diff] [blame] | 389 | #ifdef ENABLE_RESET_BUTTON_DO_WARM_REBOOT |
| 390 | transition = Host::Transition::ForceWarmReboot; |
| 391 | #else |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 392 | transition = Host::Transition::Reboot; |
| HuyLe | 9456ffc | 2023-06-22 15:21:22 +0700 | [diff] [blame] | 393 | #endif |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 394 | break; |
| 395 | } |
| George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 396 | default: |
| 397 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 398 | lg2::error("{EVENT} is invalid power event. skipping...", "EVENT", |
| Patrick Williams | 3610608 | 2024-02-26 10:26:43 -0600 | [diff] [blame] | 399 | powerEventType); |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 400 | |
| 401 | return; |
| 402 | } |
| 403 | } |
| 404 | auto service = getService(objPathName.c_str(), dbusIfaceName); |
| 405 | auto method = bus.new_method_call(service.c_str(), objPathName.c_str(), |
| 406 | propertyIface, "Set"); |
| 407 | method.append(dbusIfaceName, transitionName, transition); |
| 408 | bus.call(method); |
| 409 | } |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 410 | |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 411 | void Handler::powerReleased(sdbusplus::message_t& msg) |
| Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 412 | { |
| 413 | try |
| 414 | { |
| Patrick Williams | 60dc341 | 2025-11-05 00:05:37 -0500 | [diff] [blame] | 415 | auto time = msg.unpack<uint64_t>(); |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 416 | |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 417 | handlePowerEvent(PowerEvent::powerReleased, msg.get_path(), |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 418 | std::chrono::microseconds(time)); |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 419 | } |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 420 | catch (const sdbusplus::exception_t& e) |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 421 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 422 | lg2::error("Failed power state change on a power button press: {ERROR}", |
| 423 | "ERROR", e); |
| Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 424 | } |
| 425 | } |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 426 | |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 427 | void Handler::resetReleased(sdbusplus::message_t& msg) |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 428 | { |
| 429 | try |
| 430 | { |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 431 | // No need to calculate duration, set to 0. |
| Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 432 | handlePowerEvent(PowerEvent::resetReleased, msg.get_path(), |
| DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 433 | std::chrono::microseconds(0)); |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 434 | } |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 435 | catch (const sdbusplus::exception_t& e) |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 436 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 437 | lg2::error("Failed power state change on a reset button press: {ERROR}", |
| 438 | "ERROR", e); |
| Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 439 | } |
| 440 | } |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 441 | |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 442 | void Handler::idReleased(sdbusplus::message_t& /* msg */) |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 443 | { |
| 444 | std::string groupPath{ledGroupBasePath}; |
| 445 | groupPath += ID_LED_GROUP; |
| 446 | |
| 447 | auto service = getService(groupPath, ledGroupIface); |
| 448 | |
| 449 | if (service.empty()) |
| 450 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 451 | lg2::info("No found {GROUP} during ID button press:", "GROUP", |
| 452 | groupPath); |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
| 456 | try |
| 457 | { |
| 458 | auto method = bus.new_method_call(service.c_str(), groupPath.c_str(), |
| 459 | propertyIface, "Get"); |
| 460 | method.append(ledGroupIface, "Asserted"); |
| 461 | auto result = bus.call(method); |
| 462 | |
| Patrick Williams | 60dc341 | 2025-11-05 00:05:37 -0500 | [diff] [blame] | 463 | auto state = result.unpack<std::variant<bool>>(); |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 464 | |
| Patrick Williams | 51f5e8b | 2020-05-13 11:33:44 -0500 | [diff] [blame] | 465 | state = !std::get<bool>(state); |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 466 | |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 467 | lg2::info( |
| 468 | "Changing ID LED group state on ID LED press, GROUP = {GROUP}, STATE = {STATE}", |
| 469 | "GROUP", groupPath, "STATE", std::get<bool>(state)); |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 470 | |
| 471 | method = bus.new_method_call(service.c_str(), groupPath.c_str(), |
| 472 | propertyIface, "Set"); |
| 473 | |
| 474 | method.append(ledGroupIface, "Asserted", state); |
| 475 | result = bus.call(method); |
| 476 | } |
| Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 477 | catch (const sdbusplus::exception_t& e) |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 478 | { |
| George Liu | 9fb1597 | 2022-06-20 14:54:38 +0800 | [diff] [blame] | 479 | lg2::error("Error toggling ID LED group on ID button press: {ERROR}", |
| 480 | "ERROR", e); |
| Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 481 | } |
| 482 | } |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 483 | |
| 484 | void Handler::increaseHostSelectorPosition() |
| 485 | { |
| 486 | try |
| 487 | { |
| 488 | auto HSService = getService(HS_DBUS_OBJECT_NAME, hostSelectorIface); |
| 489 | |
| 490 | if (HSService.empty()) |
| 491 | { |
| 492 | lg2::error("Host selector service not available"); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | auto method = |
| 497 | bus.new_method_call(HSService.c_str(), HS_DBUS_OBJECT_NAME, |
| 498 | phosphor::button::propertyIface, "GetAll"); |
| 499 | method.append(phosphor::button::hostSelectorIface); |
| 500 | auto result = bus.call(method); |
| Patrick Williams | 60dc341 | 2025-11-05 00:05:37 -0500 | [diff] [blame] | 501 | auto properties = result.unpack< |
| 502 | std::unordered_map<std::string, std::variant<size_t>>>(); |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 503 | |
| 504 | auto maxPosition = std::get<size_t>(properties.at("MaxPosition")); |
| 505 | auto position = std::get<size_t>(properties.at("Position")); |
| 506 | |
| 507 | std::variant<size_t> HSPositionVariant = |
| 508 | (position < maxPosition) ? (position + 1) : 0; |
| 509 | |
| 510 | method = bus.new_method_call(HSService.c_str(), HS_DBUS_OBJECT_NAME, |
| 511 | phosphor::button::propertyIface, "Set"); |
| 512 | method.append(phosphor::button::hostSelectorIface, "Position"); |
| 513 | |
| 514 | method.append(HSPositionVariant); |
| 515 | result = bus.call(method); |
| 516 | } |
| Patrick Williams | e3b4e11 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 517 | catch (const sdbusplus::exception_t& e) |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 518 | { |
| 519 | lg2::error("Error modifying host selector position : {ERROR}", "ERROR", |
| 520 | e); |
| 521 | } |
| 522 | } |
| 523 | |
| Patrick Williams | e3b4e11 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 524 | void Handler::debugHostSelectorReleased(sdbusplus::message_t& /* msg */) |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 525 | { |
| 526 | try |
| 527 | { |
| 528 | increaseHostSelectorPosition(); |
| 529 | } |
| Patrick Williams | e3b4e11 | 2022-11-26 09:41:58 -0600 | [diff] [blame] | 530 | catch (const sdbusplus::exception_t& e) |
| Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 531 | { |
| 532 | lg2::error( |
| 533 | "Failed power process debug host selector button press : {ERROR}", |
| 534 | "ERROR", e); |
| 535 | } |
| 536 | } |
| 537 | |
| Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 538 | } // namespace button |
| 539 | } // namespace phosphor |