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/inc/id_button.hpp b/inc/id_button.hpp
index cb32125..c58d264 100644
--- a/inc/id_button.hpp
+++ b/inc/id_button.hpp
@@ -15,6 +15,8 @@
 */
 
 #pragma once
+#include "button_factory.hpp"
+#include "button_interface.hpp"
 #include "common.hpp"
 #include "gpio.hpp"
 #include "xyz/openbmc_project/Chassis/Buttons/ID/server.hpp"
@@ -24,132 +26,40 @@
 
 #include <phosphor-logging/elog-errors.hpp>
 
-const static constexpr char* ID_BUTTON = "ID_BTN";
+static constexpr std::string_view ID_BUTTON = "ID_BTN";
 
-struct IDButton
-    : sdbusplus::server::object::object<
-          sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>
+class IDButton
+    : public sdbusplus::server::object::object<
+          sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>,
+      public ButtonIface
 {
 
+  public:
     IDButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
-             buttonConfig& buttonCfg,
-             sd_event_io_handler_t handler = IDButton::EventHandler) :
+             buttonConfig& buttonCfg) :
         sdbusplus::server::object::object<
             sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>(
             bus, path),
-        fd(-1), buttonIFConfig(buttonCfg), bus(bus), event(event),
-        callbackHandler(handler)
+        ButtonIface(bus, event, buttonCfg)
     {
-
-        int ret = -1;
-
-        // config group gpio based on the gpio defs read from the json file
-        ret = configGroupGpio(bus, buttonIFConfig);
-
-        if (ret < 0)
-        {
-            phosphor::logging::log<phosphor::logging::level::ERR>(
-                "ID_BUTTON: failed to config GPIO");
-            throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
-                IOError();
-        }
-        // initialize the button io fd from the buttonConfig
-        // which has fd stored when configGroupGpio is called
-        fd = buttonIFConfig.gpios[0].fd;
-
-        char buf;
-        ::read(fd, &buf, sizeof(buf));
-
-        ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
-                              callbackHandler, this);
-        if (ret < 0)
-        {
-            phosphor::logging::log<phosphor::logging::level::ERR>(
-                "ID_BUTTON: failed to add to event loop");
-            ::closeGpio(fd);
-            throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
-                IOError();
-        }
+        init();
     }
 
     ~IDButton()
     {
-        ::closeGpio(fd);
+        deInit();
     }
 
     void simPress() override;
 
-    static const std::string getGpioName()
+    static constexpr std::string_view getFormFactorName()
     {
         return ID_BUTTON;
     }
-
-    static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
-                            void* userdata)
+    static constexpr const char* getDbusObjectPath()
     {
-
-        int n = -1;
-        char buf = '0';
-
-        if (!userdata)
-        {
-            phosphor::logging::log<phosphor::logging::level::ERR>(
-                "ID_BUTTON: userdata null!");
-            throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
-                IOError();
-        }
-
-        IDButton* idButton = static_cast<IDButton*>(userdata);
-
-        if (!idButton)
-        {
-            phosphor::logging::log<phosphor::logging::level::ERR>(
-                "ID_BUTTON: null pointer!");
-            throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
-                IOError();
-        }
-
-        n = ::lseek(fd, 0, SEEK_SET);
-
-        if (n < 0)
-        {
-            phosphor::logging::log<phosphor::logging::level::ERR>(
-                "ID_BUTTON: lseek error!");
-            throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
-                IOError();
-        }
-
-        n = ::read(fd, &buf, sizeof(buf));
-        if (n < 0)
-        {
-            phosphor::logging::log<phosphor::logging::level::ERR>(
-                "ID_BUTTON: read error!");
-            throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
-                IOError();
-        }
-
-        if (buf == '0')
-        {
-            phosphor::logging::log<phosphor::logging::level::DEBUG>(
-                "ID_BUTTON: pressed");
-            // emit pressed signal
-            idButton->pressed();
-        }
-        else
-        {
-            phosphor::logging::log<phosphor::logging::level::DEBUG>(
-                "ID_BUTTON: released");
-            // released
-            idButton->released();
-        }
-
-        return 0;
+        return ID_DBUS_OBJECT_NAME;
     }
 
-  private:
-    int fd;
-    buttonConfig buttonIFConfig;
-    sdbusplus::bus::bus& bus;
-    EventPtr& event;
-    sd_event_io_handler_t callbackHandler;
+    void handleEvent(sd_event_source* es, int fd, uint32_t revents) override;
 };