Add abstract factory to create button iface objects
A abstract factory class is implemented to return
the instance of button interface class based on the
button iface formfactor name provided as
parameter to the abstract factory createInstance
method.
Signed-off-by: Naveen Moses <naveen.mosess@hcl.com>
Change-Id: Ia791a2b6f52d09dd87da0e50a709fc72ac9d1bd7
diff --git a/src/power_button.cpp b/src/power_button.cpp
index feadeee..2e69154 100644
--- a/src/power_button.cpp
+++ b/src/power_button.cpp
@@ -16,6 +16,9 @@
#include "power_button.hpp"
+// add the button iface class to registry
+static ButtonIFRegister<PowerButton> buttonRegister;
+
void PowerButton::simPress()
{
pressed();
@@ -25,3 +28,66 @@
{
pressedLong();
}
+
+void PowerButton::updatePressedTime()
+{
+ pressedTime = std::chrono::steady_clock::now();
+}
+
+auto PowerButton::getPressTime() const
+{
+ return pressedTime;
+}
+
+void PowerButton::handleEvent(sd_event_source* es, int fd, uint32_t revents)
+{
+
+ int n = -1;
+ char buf = '0';
+
+ n = ::lseek(fd, 0, SEEK_SET);
+
+ if (n < 0)
+ {
+ phosphor::logging::log<phosphor::logging::level::ERR>(
+ "POWER_BUTTON: lseek error!");
+ return;
+ }
+
+ n = ::read(fd, &buf, sizeof(buf));
+ if (n < 0)
+ {
+ phosphor::logging::log<phosphor::logging::level::ERR>(
+ "POWER_BUTTON: read error!");
+ return;
+ }
+
+ if (buf == '0')
+ {
+ phosphor::logging::log<phosphor::logging::level::DEBUG>(
+ "POWER_BUTTON: pressed");
+
+ updatePressedTime();
+ // emit pressed signal
+ pressed();
+ }
+ else
+ {
+ phosphor::logging::log<phosphor::logging::level::DEBUG>(
+ "POWER_BUTTON: released");
+
+ auto now = std::chrono::steady_clock::now();
+ auto d = std::chrono::duration_cast<std::chrono::milliseconds>(
+ now - getPressTime());
+
+ if (d > std::chrono::milliseconds(LONG_PRESS_TIME_MS))
+ {
+ pressedLong();
+ }
+ else
+ {
+ // released
+ released();
+ }
+ }
+}