Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 1 | #include "button_handler.hpp" |
| 2 | |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 3 | #include "settings.hpp" |
| 4 | |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <xyz/openbmc_project/State/Chassis/server.hpp> |
| 7 | #include <xyz/openbmc_project/State/Host/server.hpp> |
| 8 | |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 9 | namespace phosphor |
| 10 | { |
| 11 | namespace button |
| 12 | { |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 13 | |
| 14 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 15 | using namespace sdbusplus::xyz::openbmc_project::State::server; |
| 16 | using namespace phosphor::logging; |
| 17 | using sdbusplus::exception::SdBusError; |
| 18 | |
| 19 | constexpr auto propertyIface = "org.freedesktop.DBus.Properties"; |
| 20 | constexpr auto chassisIface = "xyz.openbmc_project.State.Chassis"; |
| 21 | constexpr auto hostIface = "xyz.openbmc_project.State.Host"; |
| 22 | constexpr auto powerButtonIface = "xyz.openbmc_project.Chassis.Buttons.Power"; |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 23 | constexpr auto idButtonIface = "xyz.openbmc_project.Chassis.Buttons.ID"; |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 24 | constexpr auto resetButtonIface = "xyz.openbmc_project.Chassis.Buttons.Reset"; |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 25 | constexpr auto mapperIface = "xyz.openbmc_project.ObjectMapper"; |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 26 | constexpr auto ledGroupIface = "xyz.openbmc_project.Led.Group"; |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 27 | |
| 28 | constexpr auto mapperObjPath = "/xyz/openbmc_project/object_mapper"; |
| 29 | constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 30 | constexpr auto ledGroupBasePath = "/xyz/openbmc_project/led/groups/"; |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 31 | |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 32 | Handler::Handler(sdbusplus::bus::bus& bus) : bus(bus) |
| 33 | { |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 34 | try |
| 35 | { |
| 36 | if (!getService(POWER_DBUS_OBJECT_NAME, powerButtonIface).empty()) |
| 37 | { |
| 38 | log<level::INFO>("Starting power button handler"); |
| 39 | powerButtonReleased = std::make_unique<sdbusplus::bus::match_t>( |
| 40 | bus, |
| 41 | sdbusRule::type::signal() + sdbusRule::member("Released") + |
| 42 | sdbusRule::path(POWER_DBUS_OBJECT_NAME) + |
| 43 | sdbusRule::interface(powerButtonIface), |
| 44 | std::bind(std::mem_fn(&Handler::powerPressed), this, |
| 45 | std::placeholders::_1)); |
| 46 | |
| 47 | powerButtonLongPressReleased = |
| 48 | std::make_unique<sdbusplus::bus::match_t>( |
| 49 | bus, |
| 50 | sdbusRule::type::signal() + |
| 51 | sdbusRule::member("PressedLong") + |
| 52 | sdbusRule::path(POWER_DBUS_OBJECT_NAME) + |
| 53 | sdbusRule::interface(powerButtonIface), |
| 54 | std::bind(std::mem_fn(&Handler::longPowerPressed), this, |
| 55 | std::placeholders::_1)); |
| 56 | } |
| 57 | } |
| 58 | catch (SdBusError& e) |
| 59 | { |
| 60 | // The button wasn't implemented |
| 61 | } |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 62 | |
| 63 | try |
| 64 | { |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 65 | if (!getService(ID_DBUS_OBJECT_NAME, idButtonIface).empty()) |
| 66 | { |
| 67 | log<level::INFO>("Registering ID button handler"); |
| 68 | idButtonReleased = std::make_unique<sdbusplus::bus::match_t>( |
| 69 | bus, |
| 70 | sdbusRule::type::signal() + sdbusRule::member("Released") + |
| 71 | sdbusRule::path(ID_DBUS_OBJECT_NAME) + |
| 72 | sdbusRule::interface(idButtonIface), |
| 73 | std::bind(std::mem_fn(&Handler::idPressed), this, |
| 74 | std::placeholders::_1)); |
| 75 | } |
| 76 | } |
| 77 | catch (SdBusError& e) |
| 78 | { |
| 79 | // The button wasn't implemented |
| 80 | } |
| 81 | |
| 82 | try |
| 83 | { |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 84 | if (!getService(RESET_DBUS_OBJECT_NAME, resetButtonIface).empty()) |
| 85 | { |
| 86 | log<level::INFO>("Registering reset button handler"); |
| 87 | resetButtonReleased = std::make_unique<sdbusplus::bus::match_t>( |
| 88 | bus, |
| 89 | sdbusRule::type::signal() + sdbusRule::member("Released") + |
| 90 | sdbusRule::path(RESET_DBUS_OBJECT_NAME) + |
| 91 | sdbusRule::interface(resetButtonIface), |
| 92 | std::bind(std::mem_fn(&Handler::resetPressed), this, |
| 93 | std::placeholders::_1)); |
| 94 | } |
| 95 | } |
| 96 | catch (SdBusError& e) |
| 97 | { |
| 98 | // The button wasn't implemented |
| 99 | } |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | std::string Handler::getService(const std::string& path, |
| 103 | const std::string& interface) const |
| 104 | { |
| 105 | auto method = bus.new_method_call(mapperService, mapperObjPath, mapperIface, |
| 106 | "GetObject"); |
| 107 | method.append(path, std::vector{interface}); |
| 108 | auto result = bus.call(method); |
| 109 | |
| 110 | std::map<std::string, std::vector<std::string>> objectData; |
| 111 | result.read(objectData); |
| 112 | |
| 113 | return objectData.begin()->first; |
| 114 | } |
| 115 | |
| 116 | bool Handler::poweredOn() const |
| 117 | { |
| 118 | auto service = getService(CHASSIS_STATE_OBJECT_NAME, chassisIface); |
| 119 | auto method = bus.new_method_call( |
| 120 | service.c_str(), CHASSIS_STATE_OBJECT_NAME, propertyIface, "Get"); |
| 121 | method.append(chassisIface, "CurrentPowerState"); |
| 122 | auto result = bus.call(method); |
| 123 | |
Patrick Williams | 5ed4cc0 | 2020-05-13 17:52:15 -0500 | [diff] [blame^] | 124 | std::variant<std::string> state; |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 125 | result.read(state); |
| 126 | |
| 127 | return Chassis::PowerState::On == |
Patrick Williams | 51f5e8b | 2020-05-13 11:33:44 -0500 | [diff] [blame] | 128 | Chassis::convertPowerStateFromString(std::get<std::string>(state)); |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void Handler::powerPressed(sdbusplus::message::message& msg) |
| 132 | { |
| 133 | auto transition = Host::Transition::On; |
| 134 | |
| 135 | try |
| 136 | { |
| 137 | if (poweredOn()) |
| 138 | { |
| 139 | transition = Host::Transition::Off; |
| 140 | } |
| 141 | |
| 142 | log<level::INFO>("Handling power button press"); |
| 143 | |
Patrick Williams | 5ed4cc0 | 2020-05-13 17:52:15 -0500 | [diff] [blame^] | 144 | std::variant<std::string> state = convertForMessage(transition); |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 145 | |
| 146 | auto service = getService(HOST_STATE_OBJECT_NAME, hostIface); |
| 147 | auto method = bus.new_method_call( |
| 148 | service.c_str(), HOST_STATE_OBJECT_NAME, propertyIface, "Set"); |
| 149 | method.append(hostIface, "RequestedHostTransition", state); |
| 150 | |
| 151 | bus.call(method); |
| 152 | } |
| 153 | catch (SdBusError& e) |
| 154 | { |
| 155 | log<level::ERR>("Failed power state change on a power button press", |
| 156 | entry("ERROR=%s", e.what())); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void Handler::longPowerPressed(sdbusplus::message::message& msg) |
| 161 | { |
| 162 | try |
| 163 | { |
| 164 | if (!poweredOn()) |
| 165 | { |
| 166 | log<level::INFO>( |
| 167 | "Power is off so ignoring long power button press"); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | log<level::INFO>("Handling long power button press"); |
| 172 | |
Patrick Williams | 5ed4cc0 | 2020-05-13 17:52:15 -0500 | [diff] [blame^] | 173 | std::variant<std::string> state = |
Matt Spinler | 963c65f | 2018-11-26 14:46:41 -0600 | [diff] [blame] | 174 | convertForMessage(Chassis::Transition::Off); |
| 175 | |
| 176 | auto service = getService(CHASSIS_STATE_OBJECT_NAME, chassisIface); |
| 177 | auto method = bus.new_method_call( |
| 178 | service.c_str(), CHASSIS_STATE_OBJECT_NAME, propertyIface, "Set"); |
| 179 | method.append(chassisIface, "RequestedPowerTransition", state); |
| 180 | |
| 181 | bus.call(method); |
| 182 | } |
| 183 | catch (SdBusError& e) |
| 184 | { |
| 185 | log<level::ERR>("Failed powering off on long power button press", |
| 186 | entry("ERROR=%s", e.what())); |
| 187 | } |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 188 | } |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 189 | |
| 190 | void Handler::resetPressed(sdbusplus::message::message& msg) |
| 191 | { |
| 192 | try |
| 193 | { |
| 194 | if (!poweredOn()) |
| 195 | { |
| 196 | log<level::INFO>("Power is off so ignoring reset button press"); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | log<level::INFO>("Handling reset button press"); |
| 201 | |
Patrick Williams | 5ed4cc0 | 2020-05-13 17:52:15 -0500 | [diff] [blame^] | 202 | std::variant<std::string> state = |
Matt Spinler | 06a5bdd | 2018-11-26 14:50:48 -0600 | [diff] [blame] | 203 | convertForMessage(Host::Transition::Reboot); |
| 204 | |
| 205 | auto service = getService(HOST_STATE_OBJECT_NAME, hostIface); |
| 206 | auto method = bus.new_method_call( |
| 207 | service.c_str(), HOST_STATE_OBJECT_NAME, propertyIface, "Set"); |
| 208 | |
| 209 | method.append(hostIface, "RequestedHostTransition", state); |
| 210 | |
| 211 | bus.call(method); |
| 212 | } |
| 213 | catch (SdBusError& e) |
| 214 | { |
| 215 | log<level::ERR>("Failed power state change on a reset button press", |
| 216 | entry("ERROR=%s", e.what())); |
| 217 | } |
| 218 | } |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 219 | |
| 220 | void Handler::idPressed(sdbusplus::message::message& msg) |
| 221 | { |
| 222 | std::string groupPath{ledGroupBasePath}; |
| 223 | groupPath += ID_LED_GROUP; |
| 224 | |
| 225 | auto service = getService(groupPath, ledGroupIface); |
| 226 | |
| 227 | if (service.empty()) |
| 228 | { |
| 229 | log<level::INFO>("No identify LED group found during ID button press", |
| 230 | entry("GROUP=%s", groupPath.c_str())); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | try |
| 235 | { |
| 236 | auto method = bus.new_method_call(service.c_str(), groupPath.c_str(), |
| 237 | propertyIface, "Get"); |
| 238 | method.append(ledGroupIface, "Asserted"); |
| 239 | auto result = bus.call(method); |
| 240 | |
Patrick Williams | 5ed4cc0 | 2020-05-13 17:52:15 -0500 | [diff] [blame^] | 241 | std::variant<bool> state; |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 242 | result.read(state); |
| 243 | |
Patrick Williams | 51f5e8b | 2020-05-13 11:33:44 -0500 | [diff] [blame] | 244 | state = !std::get<bool>(state); |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 245 | |
Patrick Williams | 51f5e8b | 2020-05-13 11:33:44 -0500 | [diff] [blame] | 246 | log<level::INFO>("Changing ID LED group state on ID LED press", |
| 247 | entry("GROUP=%s", groupPath.c_str()), |
| 248 | entry("STATE=%d", std::get<bool>(state))); |
Matt Spinler | 69f9351 | 2018-11-26 14:55:58 -0600 | [diff] [blame] | 249 | |
| 250 | method = bus.new_method_call(service.c_str(), groupPath.c_str(), |
| 251 | propertyIface, "Set"); |
| 252 | |
| 253 | method.append(ledGroupIface, "Asserted", state); |
| 254 | result = bus.call(method); |
| 255 | } |
| 256 | catch (SdBusError& e) |
| 257 | { |
| 258 | log<level::ERR>("Error toggling ID LED group on ID button press", |
| 259 | entry("ERROR=%s", e.what())); |
| 260 | } |
| 261 | } |
Matt Spinler | fb35a32 | 2018-11-26 14:30:30 -0600 | [diff] [blame] | 262 | } // namespace button |
| 263 | } // namespace phosphor |