Implement phosphor-buttons based C++ and sdbusplus interfaces.

This phosphor-buttons module handles all the physical buttons
like Reset/Power/ID button pressed/released signals and the
simulate signals on button pressed/released in WebUI.

All the signals are exposed into dbus and let other
modules be aware.

Change-Id: I52144718ef54d403c5221e5afd0216b9494f8523
Signed-off-by: Kuiying Wang <kuiying.wang@intel.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..3940f27
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,49 @@
+cmake_minimum_required(VERSION 2.8.10 FATAL_ERROR)
+project(buttons CXX)
+set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_RPATH} ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
+set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+
+include(GNUInstallDirs)
+
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
+
+set(POWER_DBUS_OBJECT_NAME "xyz/openbmc_project/Chassis/Buttons/Power")
+set(RESET_DBUS_OBJECT_NAME "xyz/openbmc_project/Chassis/Buttons/Reset")
+set(ID_DBUS_OBJECT_NAME "xyz/openbmc_project/Chassis/Buttons/ID")
+
+add_definitions(-DPOWER_DBUS_OBJECT_NAME="/${POWER_DBUS_OBJECT_NAME}0")
+add_definitions(-DRESET_DBUS_OBJECT_NAME="/${RESET_DBUS_OBJECT_NAME}0")
+add_definitions(-DID_DBUS_OBJECT_NAME="/${ID_DBUS_OBJECT_NAME}0")
+set(SRC_FILES src/power_button.cpp
+    src/reset_button.cpp
+    src/id_button.cpp
+    src/main.cpp
+    src/gpio.cpp
+)
+
+# import sdbusplus
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(SDBUSPLUSPLUS sdbusplus REQUIRED)
+include_directories(${SDBUSPLUSPLUS_INCLUDE_DIRS})
+link_directories(${SDBUSPLUSPLUS_LIBRARY_DIRS})
+find_program(SDBUSPLUSPLUS sdbus++)
+
+# import phosphor-logging
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(LOGGING phosphor-logging REQUIRED)
+include_directories(${LOGGING_INCLUDE_DIRS})
+link_directories(${LOGGING_LIBRARY_DIRS})
+
+# phosphor-dbus-interfaces
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(DBUSINTERFACE phosphor-dbus-interfaces REQUIRED)
+include_directories(${DBUSINTERFACE_INCLUDE_DIRS})
+link_directories(${DBUSINTERFACE_LIBRARY_DIRS})
+
+add_executable(${PROJECT_NAME} ${SRC_FILES} )
+target_link_libraries(${PROJECT_NAME} "${SDBUSPLUSPLUS_LIBRARIES} -lphosphor_dbus  -lstdc++fs")
+
+install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/MAINTAINERS b/MAINTAINERS
new file mode 100644
index 0000000..beb8c51
--- /dev/null
+++ b/MAINTAINERS
@@ -0,0 +1,47 @@
+How to use this list:
+    Find the most specific section entry (described below) that matches where
+    your change lives and add the reviewers (R) and maintainers (M) as
+    reviewers. You can use the same method to track down who knows a particular
+    code base best.
+
+    Your change/query may span multiple entries; that is okay.
+
+    If you do not find an entry that describes your request at all, someone
+    forgot to update this list; please at least file an issue or send an email
+    to a maintainer, but preferably you should just update this document.
+
+Description of section entries:
+
+    Section entries are structured according to the following scheme:
+
+    X:  NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>
+    X:  ...
+    .
+    .
+    .
+
+    Where REPO_NAME is the name of the repository within the OpenBMC GitHub
+    organization; FILE_PATH is a file path within the repository, possibly with
+    wildcards; X is a tag of one of the following types:
+
+    M:  Denotes maintainer; has fields NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>;
+        if omitted from an entry, assume one of the maintainers from the
+        MAINTAINERS entry.
+    R:  Denotes reviewer; has fields NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>;
+        these people are to be added as reviewers for a change matching the repo
+        path.
+    F:  Denotes forked from an external repository; has fields URL.
+
+    Line comments are to be denoted "# SOME COMMENT" (typical shell style
+    comment); it is important to follow the correct syntax and semantics as we
+    may want to use automated tools with this file in the future.
+
+    A change cannot be added to an OpenBMC repository without a MAINTAINER's
+    approval; thus, a MAINTAINER should always be listed as a reviewer.
+
+START OF MAINTAINERS LIST
+-------------------------
+
+M:  Kuiying Wang <kuiying.wang@intel.com> <kuiyingw!>
+M:  Ed Tanous <ed.tanous@intel.com> <edtanous!>
+M:  Vernon Mauery <veron.mauery@linux.intel.com> <vmauery!>
\ No newline at end of file
diff --git a/inc/common.hpp b/inc/common.hpp
new file mode 100644
index 0000000..387b626
--- /dev/null
+++ b/inc/common.hpp
@@ -0,0 +1,25 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#pragma once
+struct EventDeleter
+{
+    void operator()(sd_event* event) const
+    {
+        event = sd_event_unref(event);
+    }
+};
+using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
\ No newline at end of file
diff --git a/inc/gpio.hpp b/inc/gpio.hpp
new file mode 100644
index 0000000..6387c93
--- /dev/null
+++ b/inc/gpio.hpp
@@ -0,0 +1,18 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+#pragma once
+int configGpio(const char* gpioName, int* fd, sdbusplus::bus::bus& bus);
+void closeGpio(int fd);
\ No newline at end of file
diff --git a/inc/id_button.hpp b/inc/id_button.hpp
new file mode 100644
index 0000000..cc664b2
--- /dev/null
+++ b/inc/id_button.hpp
@@ -0,0 +1,139 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#pragma once
+#include <phosphor-logging/elog-errors.hpp>
+#include <unistd.h>
+#include "xyz/openbmc_project/Chassis/Common/error.hpp"
+#include "xyz/openbmc_project/Chassis/Buttons/ID/server.hpp"
+#include "common.hpp"
+#include "gpio.hpp"
+
+const static constexpr char *ID_BUTTON = "ID_BTN";
+
+struct IDButton
+    : sdbusplus::server::object::object<
+        sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>
+{
+
+    IDButton(sdbusplus::bus::bus& bus, const char* path,
+             EventPtr& event,
+             sd_event_io_handler_t handler = IDButton::EventHandler) :
+        sdbusplus::server::object::object<
+            sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>(
+            bus, path),
+        fd(-1), bus(bus), event(event), callbackHandler(handler)
+    {
+
+        int ret = -1;
+
+        // config gpio
+        ret = ::configGpio(ID_BUTTON, &fd, bus);
+        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();
+        }
+
+        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();
+        }
+    }
+
+    ~IDButton()
+    {
+        ::closeGpio(fd);
+    }
+
+    void simPress() override;
+
+    static int EventHandler(sd_event_source* es, int fd,
+                            uint32_t revents, void* userdata)
+    {
+
+        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;
+    }
+
+  private:
+    int fd;
+    sdbusplus::bus::bus& bus;
+    EventPtr& event;
+    sd_event_io_handler_t callbackHandler;
+};
diff --git a/inc/power_button.hpp b/inc/power_button.hpp
new file mode 100644
index 0000000..94d15c6
--- /dev/null
+++ b/inc/power_button.hpp
@@ -0,0 +1,140 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#pragma once
+#include <phosphor-logging/elog-errors.hpp>
+#include <unistd.h>
+#include "xyz/openbmc_project/Chassis/Common/error.hpp"
+#include "xyz/openbmc_project/Chassis/Buttons/Power/server.hpp"
+#include "common.hpp"
+#include "gpio.hpp"
+
+const static constexpr char* POWER_BUTTON = "POWER_BUTTON";
+
+struct PowerButton
+    : sdbusplus::server::object::object<
+          sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>
+{
+
+    PowerButton(sdbusplus::bus::bus& bus, const char* path,
+                EventPtr& event,
+                sd_event_io_handler_t handler = PowerButton::EventHandler) :
+        sdbusplus::server::object::object<
+            sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
+            bus, path),
+        fd(-1), bus(bus), event(event), callbackHandler(handler)
+    {
+
+        int ret = -1;
+
+        // config gpio
+        ret = ::configGpio(POWER_BUTTON, &fd, bus);
+        if (ret < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "POWER_BUTTON: failed to config GPIO");
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+
+        ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
+                              callbackHandler, this);
+        if (ret < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "POWER_BUTTON: failed to add to event loop");
+            ::closeGpio(fd);
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+    }
+
+    ~PowerButton()
+    {
+        ::closeGpio(fd);
+    }
+
+    void simPress() override;
+    void simLongPress() override;
+
+    static int EventHandler(sd_event_source* es, int fd,
+                            uint32_t revents, void* userdata)
+    {
+
+        int n = -1;
+        char buf = '0';
+
+        if (!userdata)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "POWER_BUTTON: userdata null!");
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+
+        PowerButton* powerButton = static_cast<PowerButton*>(userdata);
+
+        if (!powerButton)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "POWER_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>(
+                "POWER_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>(
+                "POWER_BUTTON: read error!");
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+
+        if (buf == '0')
+        {
+            phosphor::logging::log<phosphor::logging::level::DEBUG>(
+                "POWER_BUTTON: pressed");
+            // emit pressed signal
+            powerButton->pressed();
+        }
+        else
+        {
+            phosphor::logging::log<phosphor::logging::level::DEBUG>(
+                "POWER_BUTTON: released");
+            // released
+            powerButton->released();
+        }
+
+        return 0;
+    }
+
+  private:
+    int fd;
+    sdbusplus::bus::bus& bus;
+    EventPtr& event;
+    sd_event_io_handler_t callbackHandler;
+};
diff --git a/inc/reset_button.hpp b/inc/reset_button.hpp
new file mode 100644
index 0000000..8cf0545
--- /dev/null
+++ b/inc/reset_button.hpp
@@ -0,0 +1,139 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#pragma once
+#include <phosphor-logging/elog-errors.hpp>
+#include <unistd.h>
+#include "xyz/openbmc_project/Chassis/Common/error.hpp"
+#include "xyz/openbmc_project/Chassis/Buttons/Reset/server.hpp"
+#include "common.hpp"
+#include "gpio.hpp"
+
+const static constexpr char* RESET_BUTTON = "RESET_BUTTON";
+
+struct ResetButton
+    : sdbusplus::server::object::object<
+          sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Reset>
+{
+
+    ResetButton(sdbusplus::bus::bus& bus, const char* path,
+                EventPtr& event,
+                sd_event_io_handler_t handler = ResetButton::EventHandler) :
+        sdbusplus::server::object::object<
+            sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Reset>(
+            bus, path),
+        fd(-1), bus(bus), event(event), callbackHandler(handler)
+    {
+
+        int ret = -1;
+
+        // config gpio
+        ret = ::configGpio(RESET_BUTTON, &fd, bus);
+        if (ret < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "RESET_BUTTON: failed to config GPIO");
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+
+        ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
+                              callbackHandler, this);
+        if (ret < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "RESET_BUTTON: failed to add to event loop");
+            ::closeGpio(fd);
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+    }
+
+    ~ResetButton()
+    {
+        ::closeGpio(fd);
+    }
+
+    void simPress() override;
+
+    static int EventHandler(sd_event_source* es, int fd,
+                            uint32_t revents, void* userdata)
+    {
+
+        int n = -1;
+        char buf = '0';
+
+        if (!userdata)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "RESET_BUTTON: userdata null!");
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+
+        ResetButton* resetButton = static_cast<ResetButton*>(userdata);
+
+        if (!resetButton)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "RESET_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>(
+                "RESET_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>(
+                "RESET_BUTTON: read error!");
+            throw sdbusplus::xyz::openbmc_project::Chassis::Common::
+                Error::IOError();
+        }
+
+        if (buf == '0')
+        {
+            phosphor::logging::log<phosphor::logging::level::DEBUG>(
+                "RESET_BUTTON: pressed");
+            // emit pressed signal
+            resetButton->pressed();
+        }
+        else
+        {
+            phosphor::logging::log<phosphor::logging::level::DEBUG>(
+                "RESET_BUTTON: released");
+            // released
+            resetButton->released();
+        }
+
+        return 0;
+    }
+
+  private:
+    int fd;
+    sdbusplus::bus::bus& bus;
+    EventPtr& event;
+    sd_event_io_handler_t callbackHandler;
+};
diff --git a/src/gpio.cpp b/src/gpio.cpp
new file mode 100644
index 0000000..72c569a
--- /dev/null
+++ b/src/gpio.cpp
@@ -0,0 +1,193 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#include <experimental/filesystem>
+#include <fcntl.h>
+#include <fstream>
+#include <phosphor-logging/elog-errors.hpp>
+#include <unistd.h>
+#include <xyz/openbmc_project/Common/error.hpp>
+#include "gpio.hpp"
+
+const static constexpr char* SYSMGR_SERVICE = "org.openbmc.managers.System";
+const static constexpr char* SYSMGR_OBJ_PATH = "/org/openbmc/managers/System";
+const static constexpr char* SYSMGR_INTERFACE = "org.openbmc.managers.System";
+
+void closeGpio(int fd)
+{
+    if (fd > 0)
+    {
+        ::close(fd);
+    }
+}
+
+int configGpio(const char* gpioName, int* fd, sdbusplus::bus::bus& bus)
+{
+    auto method = bus.new_method_call(SYSMGR_SERVICE, SYSMGR_OBJ_PATH,
+                                      SYSMGR_INTERFACE, "gpioInit");
+
+    method.append(gpioName);
+
+    auto result = bus.call(method);
+
+    if (result.is_method_error())
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "bus call error!");
+        return -1;
+    }
+
+    int32_t gpioNum;
+    std::string gpioDev;
+    std::string gpioDirection;
+
+    result.read(gpioDev, gpioNum, gpioDirection);
+
+    std::string devPath;
+
+    std::fstream stream;
+
+    stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
+
+    devPath.clear();
+    devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/value";
+
+    std::experimental::filesystem::path fullPath(devPath);
+
+    if (std::experimental::filesystem::exists(fullPath))
+    {
+        phosphor::logging::log<phosphor::logging::level::INFO>(
+            "GPIO exported",
+            phosphor::logging::entry("PATH=%s", devPath.c_str()));
+    }
+    else
+    {
+        devPath.clear();
+        devPath = gpioDev + "/export";
+
+        stream.open(devPath, std::fstream::out);
+        try
+        {
+            stream << gpioNum;
+            stream.close();
+        }
+
+        catch (const std::exception &e)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error in writing!",
+                phosphor::logging::entry("PATH=%s", devPath.c_str()),
+                phosphor::logging::entry("NUM=%d", gpioNum));
+            return -1;
+        }
+    }
+
+    if (gpioDirection == "out")
+    {
+        devPath.clear();
+        devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/value";
+
+        uint32_t currentValue;
+
+        stream.open(devPath, std::fstream::in);
+        try
+        {
+            stream >> currentValue;
+            stream.close();
+        }
+
+        catch (const std::exception& e)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error in reading!",
+                phosphor::logging::entry("PATH=%s", devPath.c_str()));
+            return -1;
+        }
+
+        const char* direction = currentValue ? "high" : "low";
+
+        devPath.clear();
+        devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/direction";
+
+        stream.open(devPath, std::fstream::out);
+        try
+        {
+            stream << direction;
+            stream.close();
+        }
+
+        catch (const std::exception& e)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error in writing!");
+            return -1;
+        }
+    }
+    else if (gpioDirection == "in")
+    {
+        devPath.clear();
+        devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/direction";
+
+        stream.open(devPath, std::fstream::out);
+        try
+        {
+            stream << gpioDirection;
+            stream.close();
+        }
+
+        catch (const std::exception& e)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error in writing!");
+            return -1;
+        }
+    }
+    else if ((gpioDirection == "both"))
+    {
+
+        // For gpio configured as ‘both’, it is an interrupt pin and trigged on
+        // both rising and falling signals
+        devPath.clear();
+        devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/edge";
+
+        stream.open(devPath, std::fstream::out);
+        try
+        {
+            stream << gpioDirection;
+            stream.close();
+        }
+
+        catch (const std::exception& e)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error in writing!");
+            return -1;
+        }
+    }
+
+    devPath.clear();
+    devPath = gpioDev + "/gpio" + std::to_string(gpioNum) + "/value";
+
+    *fd = ::open(devPath.c_str(), O_RDWR | O_NONBLOCK);
+
+    if (*fd < 0)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>("open error!");
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/src/id_button.cpp b/src/id_button.cpp
new file mode 100644
index 0000000..c543556
--- /dev/null
+++ b/src/id_button.cpp
@@ -0,0 +1,22 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#include "id_button.hpp"
+
+void IDButton::simPress()
+{
+    pressed();
+}
\ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..bd4ec2a
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,68 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#include "reset_button.hpp"
+#include "power_button.hpp"
+#include "id_button.hpp"
+
+int main(int argc, char* argv[])
+{
+    int ret = 0;
+
+    phosphor::logging::log<phosphor::logging::level::INFO>(
+        "Start power button service...");
+
+    sd_event* event = nullptr;
+    ret = sd_event_default(&event);
+    if (ret < 0)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Error creating a default sd_event handler");
+        return ret;
+    }
+    EventPtr eventP{event};
+    event = nullptr;
+
+    sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    sdbusplus::server::manager::manager objManager{
+        bus, "/xyz/openbmc_project/Chassis/Buttons"};
+
+    bus.request_name("xyz.openbmc_project.Chassis.Buttons");
+
+    PowerButton powerButton{bus, POWER_DBUS_OBJECT_NAME, eventP};
+
+    ResetButton resetButton{bus, RESET_DBUS_OBJECT_NAME, eventP};
+
+    IDButton idButton{bus, ID_DBUS_OBJECT_NAME, eventP};
+
+    try
+    {
+        bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
+        ret = sd_event_loop(eventP.get());
+        if (ret < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error occurred during the sd_event_loop",
+                phosphor::logging::entry("RET=%d", ret));
+        }
+    }
+    catch (std::exception& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+        ret = -1;
+    }
+    return ret;
+}
diff --git a/src/power_button.cpp b/src/power_button.cpp
new file mode 100644
index 0000000..feadeee
--- /dev/null
+++ b/src/power_button.cpp
@@ -0,0 +1,27 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#include "power_button.hpp"
+
+void PowerButton::simPress()
+{
+    pressed();
+}
+
+void PowerButton::simLongPress()
+{
+    pressedLong();
+}
diff --git a/src/reset_button.cpp b/src/reset_button.cpp
new file mode 100644
index 0000000..1747b71
--- /dev/null
+++ b/src/reset_button.cpp
@@ -0,0 +1,24 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#include "xyz/openbmc_project/Chassis/Buttons/Reset/server.hpp"
+
+#include "reset_button.hpp"
+
+void ResetButton::simPress()
+{
+    pressed();
+}