Remove power button long-press
So far, counting power button's pressing duration is supported.
There's no need to handle redundant case for button long-press.
Tested:
Press buttons and check corresponding behaviors.
Change-Id: I96d9160a10de6d4572148c55e2b7c2a9221d42e2
Signed-off-by: DelphineCCChiu <Delphine_CC_Chiu@wiwynn.com>
diff --git a/inc/button_handler.hpp b/inc/button_handler.hpp
old mode 100644
new mode 100755
index 1f74f18..e398a0a
--- a/inc/button_handler.hpp
+++ b/inc/button_handler.hpp
@@ -9,10 +9,8 @@
enum class PowerEvent
{
powerPressed,
- longPowerPressed,
resetPressed,
powerReleased,
- longPowerReleased,
resetReleased
};
/**
@@ -47,24 +45,13 @@
/**
* @brief The handler for a power button press
*
- * It will power on the system if it's currently off,
- * else it will soft power it off.
+ * It will do power action according to the pressing duration.
*
* @param[in] msg - sdbusplus message from signal
*/
void powerReleased(sdbusplus::message_t& msg);
/**
- * @brief The handler for a long power button press
- *
- * If the system is currently powered on, it will
- * perform an immediate power off.
- *
- * @param[in] msg - sdbusplus message from signal
- */
- void longPowerPressed(sdbusplus::message_t& msg);
-
- /**
* @brief The handler for an ID button press
*
* Toggles the ID LED group
@@ -138,7 +125,8 @@
*
* @return void
*/
- void handlePowerEvent(PowerEvent powerEventType);
+ void handlePowerEvent(PowerEvent powerEventType,
+ std::chrono::microseconds duration);
/**
* @brief sdbusplus connection object
diff --git a/src/button_handler.cpp b/src/button_handler.cpp
old mode 100644
new mode 100755
index 27a9333..f38882e
--- a/src/button_handler.cpp
+++ b/src/button_handler.cpp
@@ -46,14 +46,6 @@
sdbusRule::interface(powerButtonIface),
std::bind(std::mem_fn(&Handler::powerReleased), this,
std::placeholders::_1));
-
- powerButtonLongPressed = std::make_unique<sdbusplus::bus::match_t>(
- bus,
- sdbusRule::type::signal() + sdbusRule::member("PressedLong") +
- sdbusRule::path(POWER_DBUS_OBJECT_NAME) +
- sdbusRule::interface(powerButtonIface),
- std::bind(std::mem_fn(&Handler::longPowerPressed), this,
- std::placeholders::_1));
}
}
catch (const sdbusplus::exception_t& e)
@@ -186,12 +178,15 @@
Host::convertHostStateFromString(std::get<std::string>(state));
}
-void Handler::handlePowerEvent(PowerEvent powerEventType)
+void Handler::handlePowerEvent(PowerEvent powerEventType,
+ std::chrono::microseconds duration)
{
std::string objPathName;
std::string dbusIfaceName;
std::string transitionName;
std::variant<Host::Transition, Chassis::Transition> transition;
+ uint64_t durationMs =
+ std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
size_t hostNumber = 0;
auto isMultiHostSystem = isMultiHost();
@@ -206,7 +201,8 @@
// ignore power and reset button events if BMC is selected.
if (isMultiHostSystem && (hostNumber == BMC_POSITION) &&
- (powerEventType != PowerEvent::longPowerPressed))
+ (powerEventType != PowerEvent::powerReleased) &&
+ (durationMs <= LONG_PRESS_TIME_MS))
{
lg2::info(
"handlePowerEvent : BMC selected on multi-host system. ignoring power and reset button events...");
@@ -217,52 +213,54 @@
{
case PowerEvent::powerReleased:
{
- objPathName = HOST_STATE_OBJECT_NAME + hostNumStr;
- dbusIfaceName = hostIface;
- transitionName = "RequestedHostTransition";
-
- transition = Host::Transition::On;
-
- if (poweredOn(hostNumber))
+ if (durationMs <= LONG_PRESS_TIME_MS)
{
- transition = Host::Transition::Off;
+ objPathName = HOST_STATE_OBJECT_NAME + hostNumStr;
+ dbusIfaceName = hostIface;
+ transitionName = "RequestedHostTransition";
+
+ transition = Host::Transition::On;
+
+ if (poweredOn(hostNumber))
+ {
+ transition = Host::Transition::Off;
+ }
+ lg2::info("handlePowerEvent : Handle power button press ");
+
+ break;
}
- lg2::info("handlePowerEvent : Handle power button press ");
-
- break;
- }
- case PowerEvent::longPowerPressed:
- {
- dbusIfaceName = chassisIface;
- transitionName = "RequestedPowerTransition";
- objPathName = CHASSIS_STATE_OBJECT_NAME + hostNumStr;
- transition = Chassis::Transition::Off;
-
- /* multi host system :
- hosts (1 to N) - host shutdown
- bmc (0) - sled cycle
- single host system :
- host(0) - host shutdown
- */
- if (isMultiHostSystem && (hostNumber == BMC_POSITION))
+ else
{
+ dbusIfaceName = chassisIface;
+ transitionName = "RequestedPowerTransition";
+ objPathName = CHASSIS_STATE_OBJECT_NAME + hostNumStr;
+ transition = Chassis::Transition::Off;
+
+ /* multi host system :
+ hosts (1 to N) - host shutdown
+ bmc (0) - sled cycle
+ single host system :
+ host(0) - host shutdown
+ */
+ if (isMultiHostSystem && (hostNumber == BMC_POSITION))
+ {
#if CHASSIS_SYSTEM_RESET_ENABLED
- objPathName = CHASSISSYSTEM_STATE_OBJECT_NAME + hostNumStr;
- transition = Chassis::Transition::PowerCycle;
+ objPathName = CHASSISSYSTEM_STATE_OBJECT_NAME + hostNumStr;
+ transition = Chassis::Transition::PowerCycle;
#else
- return;
+ return;
#endif
+ }
+ else if (!poweredOn(hostNumber))
+ {
+ lg2::info(
+ "Power is off so ignoring long power button press");
+ return;
+ }
+ lg2::info("handlePowerEvent : handle long power button press");
+ break;
}
- else if (!poweredOn(hostNumber))
- {
- lg2::info("Power is off so ignoring long power button press");
- return;
- }
- lg2::info("handlePowerEvent : handle long power button press");
-
- break;
}
-
case PowerEvent::resetReleased:
{
objPathName = HOST_STATE_OBJECT_NAME + hostNumStr;
@@ -294,11 +292,15 @@
method.append(dbusIfaceName, transitionName, transition);
bus.call(method);
}
-void Handler::powerReleased(sdbusplus::message_t& /* msg */)
+void Handler::powerReleased(sdbusplus::message_t& msg)
{
try
{
- handlePowerEvent(PowerEvent::powerReleased);
+ uint64_t time;
+ msg.read(time);
+
+ handlePowerEvent(PowerEvent::powerReleased,
+ std::chrono::microseconds(time));
}
catch (const sdbusplus::exception_t& e)
{
@@ -306,24 +308,14 @@
"ERROR", e);
}
}
-void Handler::longPowerPressed(sdbusplus::message_t& /* msg */)
-{
- try
- {
- handlePowerEvent(PowerEvent::longPowerPressed);
- }
- catch (const sdbusplus::exception_t& e)
- {
- lg2::error("Failed powering off on long power button press: {ERROR}",
- "ERROR", e);
- }
-}
void Handler::resetReleased(sdbusplus::message_t& /* msg */)
{
try
{
- handlePowerEvent(PowerEvent::resetReleased);
+ // No need to calculate duration, set to 0.
+ handlePowerEvent(PowerEvent::resetReleased,
+ std::chrono::microseconds(0));
}
catch (const sdbusplus::exception_t& e)
{
diff --git a/src/power_button.cpp b/src/power_button.cpp
index 195030a..80334a8 100644
--- a/src/power_button.cpp
+++ b/src/power_button.cpp
@@ -77,18 +77,9 @@
"POWER_BUTTON: released");
auto now = std::chrono::steady_clock::now();
- auto d = std::chrono::duration_cast<std::chrono::milliseconds>(
+ auto d = std::chrono::duration_cast<std::chrono::microseconds>(
now - getPressTime());
-
- if (d > std::chrono::milliseconds(LONG_PRESS_TIME_MS))
- {
- pressedLong();
- }
- else
- {
- // released
- released(std::chrono::duration_cast<std::chrono::microseconds>(d)
- .count());
- }
+ // released
+ released(d.count());
}
}