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/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();
+}