Create a custom power button handler
IBM has specific requirements on how the power button must behave, which
are different than what the button handler currently does.
These requirements are:
If power is off:
- A button press will power on as long as the BMC is
in the ready state.
If power is on:
- A button press less than 4s won't do anything.
- At 4s, issue a host power off and start a 10s timer.
- If the button is released within that 10s and not pressed
again, continue with the host power off.
- If the button is released within that 10s and also
pressed again in that 10s, do a hard power (chassis)
off.
- If the button is pressed throughout that 10s
issue a hard power off.
Instead of trying to integrate this behavior into the main button
handler code using even more #ifdefs in various spots, this commit
creates the concept of custom power button handlers, and then implements
one. This makes it less likely it could subtly break when changes are
made to the default code.
A 'power-button-handler' meson option is used to select the handler,
where the default uses the current behavior. A
PowerButtonHandlerFactory then creates the appropriate instance of the
handler selected by that option. If a handler is found, then the
default matches/callbacks won't be done and the handler can deal with
them as it sees fit.
Handlers are derived from a PowerButtonHandler abstract base class that
has 2 pure virtual functions:
- void pressed();
- void released(uint64_t pressTimeMS);
It will register for the power button pressed/released signals and then
call these overridden functions appropriately.
This new handler is implemented in a HostThenChassisPowerOff class.
Change-Id: I3a1df688c4393b4643d42e91c075379f9a266eef
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/src/button_handler.cpp b/src/button_handler.cpp
index f38882e..801cc9e 100755
--- a/src/button_handler.cpp
+++ b/src/button_handler.cpp
@@ -2,6 +2,8 @@
#include "button_handler.hpp"
+#include "power_button_profile_factory.hpp"
+
#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/State/Chassis/server.hpp>
#include <xyz/openbmc_project/State/Host/server.hpp>
@@ -39,13 +41,21 @@
if (!getService(POWER_DBUS_OBJECT_NAME, powerButtonIface).empty())
{
lg2::info("Starting power button handler");
- powerButtonReleased = std::make_unique<sdbusplus::bus::match_t>(
- bus,
- sdbusRule::type::signal() + sdbusRule::member("Released") +
- sdbusRule::path(POWER_DBUS_OBJECT_NAME) +
- sdbusRule::interface(powerButtonIface),
- std::bind(std::mem_fn(&Handler::powerReleased), this,
- std::placeholders::_1));
+
+ // Check for a custom handler
+ powerButtonProfile =
+ PowerButtonProfileFactory::instance().createProfile(bus);
+
+ if (!powerButtonProfile)
+ {
+ powerButtonReleased = std::make_unique<sdbusplus::bus::match_t>(
+ bus,
+ sdbusRule::type::signal() + sdbusRule::member("Released") +
+ sdbusRule::path(POWER_DBUS_OBJECT_NAME) +
+ sdbusRule::interface(powerButtonIface),
+ std::bind(std::mem_fn(&Handler::powerReleased), this,
+ std::placeholders::_1));
+ }
}
}
catch (const sdbusplus::exception_t& e)
diff --git a/src/host_then_chassis_poweroff.cpp b/src/host_then_chassis_poweroff.cpp
new file mode 100644
index 0000000..0178983
--- /dev/null
+++ b/src/host_then_chassis_poweroff.cpp
@@ -0,0 +1,237 @@
+#include "config.h"
+
+#include "host_then_chassis_poweroff.hpp"
+
+#include "power_button_profile_factory.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+#include <xyz/openbmc_project/State/BMC/server.hpp>
+#include <xyz/openbmc_project/State/Chassis/server.hpp>
+
+namespace phosphor::button
+{
+
+// Register the profile with the factory.
+static PowerButtonProfileRegister<HostThenChassisPowerOff> profileRegister;
+
+namespace service
+{
+constexpr auto bmcState = "xyz.openbmc_project.State.BMC";
+constexpr auto chassisState = "xyz.openbmc_project.State.Chassis";
+constexpr auto hostState = "xyz.openbmc_project.State.Host";
+} // namespace service
+
+namespace object_path
+{
+constexpr auto bmcState = "/xyz/openbmc_project/state/bmc0";
+constexpr auto chassisState = "/xyz/openbmc_project/state/chassis0";
+constexpr auto hostState = "/xyz/openbmc_project/state/host0";
+} // namespace object_path
+
+namespace interface
+{
+constexpr auto property = "org.freedesktop.DBus.Properties";
+constexpr auto bmcState = "xyz.openbmc_project.State.BMC";
+constexpr auto chassisState = "xyz.openbmc_project.State.Chassis";
+constexpr auto hostState = "xyz.openbmc_project.State.Host";
+} // namespace interface
+
+using namespace sdbusplus::xyz::openbmc_project::State::server;
+
+void HostThenChassisPowerOff::pressed()
+{
+ lg2::info("Power button pressed");
+
+ try
+ {
+ // If power not on - power on
+ if (!isPoweredOn())
+ {
+ if (!isBmcReady())
+ {
+ lg2::error("BMC not at ready state yet, cannot power on");
+ return;
+ }
+
+ state = PowerOpState::powerOnPress;
+ powerOn();
+ return;
+ }
+ }
+ catch (const sdbusplus::exception_t& e)
+ {
+ lg2::error(
+ "Exception while processing power button press. Cannot continue");
+ return;
+ }
+
+ // Power is on ...
+
+ if (state == PowerOpState::buttonNotPressed)
+ {
+ lg2::info("Starting countdown to power off");
+ state = PowerOpState::buttonPressed;
+ setHostOffTime();
+ timer.restart(pollInterval);
+ }
+
+ // Button press during host off to chassis off window.
+ // Causes a chassis power off.
+ else if (state == PowerOpState::buttonReleasedHostToChassisOffWindow)
+ {
+ lg2::info("Starting chassis power off due to button press");
+ state = PowerOpState::chassisOffStarted;
+ timer.setEnabled(false);
+ chassisPowerOff();
+ }
+}
+
+void HostThenChassisPowerOff::released(uint64_t /*pressTimeMS*/)
+{
+ lg2::info("Power button released");
+
+ // Button released in the host to chassis off window.
+ // Timer continues to run in case button is pressed again
+ // in the window.
+ if (state == PowerOpState::buttonPressedHostOffStarted)
+ {
+ state = PowerOpState::buttonReleasedHostToChassisOffWindow;
+ return;
+ }
+
+ state = PowerOpState::buttonNotPressed;
+ timer.setEnabled(false);
+}
+
+void HostThenChassisPowerOff::timerHandler()
+{
+ const auto now = std::chrono::steady_clock::now();
+
+ if ((state == PowerOpState::buttonPressed) && (now >= hostOffTime))
+ {
+ // Start the host power off and start the chassis
+ // power off countdown.
+ state = PowerOpState::buttonPressedHostOffStarted;
+ setChassisOffTime();
+ hostPowerOff();
+ }
+ else if ((state == PowerOpState::buttonPressedHostOffStarted) &&
+ (now >= chassisOffTime))
+ {
+ // Button still pressed and it passed the chassis off time.
+ // Issue the chassis power off.
+ state = PowerOpState::chassisOffStarted;
+ timer.setEnabled(false);
+ chassisPowerOff();
+ }
+}
+
+void HostThenChassisPowerOff::hostTransition(Host::Transition transition)
+{
+ try
+ {
+ std::variant<std::string> state = convertForMessage(transition);
+
+ lg2::info("Power button action requesting host transition of {TRANS}",
+ "TRANS", std::get<std::string>(state));
+
+ auto method = bus.new_method_call(service::hostState,
+ object_path::hostState,
+ interface::property, "Set");
+ method.append(interface::hostState, "RequestedHostTransition", state);
+
+ bus.call(method);
+ }
+ catch (const sdbusplus::exception_t& e)
+ {
+ lg2::error("Failed requesting host transition {TRANS}: {ERROR}",
+ "TRANS", convertForMessage(transition), "ERROR", e);
+ }
+}
+
+void HostThenChassisPowerOff::powerOn()
+{
+ hostTransition(Host::Transition::On);
+}
+
+void HostThenChassisPowerOff::hostPowerOff()
+{
+ hostTransition(Host::Transition::Off);
+}
+
+void HostThenChassisPowerOff::chassisPowerOff()
+{
+ lg2::info("Power button action requesting chassis power off");
+
+ try
+ {
+ std::variant<std::string> state =
+ convertForMessage(Chassis::Transition::Off);
+
+ auto method = bus.new_method_call(service::chassisState,
+ object_path::chassisState,
+ interface::property, "Set");
+ method.append(interface::chassisState, "RequestedPowerTransition",
+ state);
+
+ bus.call(method);
+ }
+ catch (const sdbusplus::exception_t& e)
+ {
+ lg2::error("Failed requesting chassis off: {ERROR}", "ERROR", e);
+ }
+}
+
+bool HostThenChassisPowerOff::isPoweredOn() const
+{
+ Chassis::PowerState chassisState;
+
+ try
+ {
+ auto method = bus.new_method_call(service::chassisState,
+ object_path::chassisState,
+ interface::property, "Get");
+ method.append(interface::chassisState, "CurrentPowerState");
+ auto result = bus.call(method);
+
+ std::variant<std::string> state;
+ result.read(state);
+
+ chassisState =
+ Chassis::convertPowerStateFromString(std::get<std::string>(state));
+ }
+ catch (const sdbusplus::exception_t& e)
+ {
+ lg2::error("Failed checking if chassis is on: {ERROR}", "ERROR", e);
+ throw;
+ }
+
+ return chassisState == Chassis::PowerState::On;
+}
+
+bool HostThenChassisPowerOff::isBmcReady() const
+{
+ BMC::BMCState bmcState;
+
+ try
+ {
+ auto method = bus.new_method_call(service::bmcState,
+ object_path::bmcState,
+ interface::property, "Get");
+ method.append(interface::bmcState, "CurrentBMCState");
+ auto result = bus.call(method);
+
+ std::variant<std::string> state;
+ result.read(state);
+
+ bmcState = BMC::convertBMCStateFromString(std::get<std::string>(state));
+ }
+ catch (const sdbusplus::exception_t& e)
+ {
+ lg2::error("Failed reading BMC state interface: {}", "ERROR", e);
+ throw;
+ }
+
+ return bmcState == BMC::BMCState::Ready;
+}
+} // namespace phosphor::button