Read GPIO key on startup

Create class Presence which will be responsible for
determining and monitoring presence of inventory items
and updating D-Bus accordingly. With this commit
class Presence only reads the GPIO key on startup,
more to come later.

Change-Id: I647ae11d42a813a103e6d9d8922fd0f5b2155132
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/presence/gpio_presence.hpp b/presence/gpio_presence.hpp
index 67fd4cb..3faa7e1 100644
--- a/presence/gpio_presence.hpp
+++ b/presence/gpio_presence.hpp
@@ -1,4 +1,7 @@
 #pragma once
+#include <string>
+#include <libevdev/libevdev.h>
+#include "file.hpp"
 
 namespace phosphor
 {
@@ -7,6 +10,87 @@
 namespace presence
 {
 
+/* Need a custom deleter for freeing up evdev struct */
+struct FreeEvDev
+{
+    void operator()(struct libevdev* device) const
+    {
+        libevdev_free(device);
+    }
+};
+using EvdevPtr = std::unique_ptr<struct libevdev, FreeEvDev>;
+
+/** @class Presence
+ *  @brief Responsible for determining and monitoring presence of
+ *  inventory items and updating D-Bus accordingly.
+ */
+class Presence
+{
+
+    public:
+        Presence() = delete;
+        ~Presence() = default;
+        Presence(const Presence&) = delete;
+        Presence& operator=(const Presence&) = delete;
+        Presence(Presence&&) = delete;
+        Presence& operator=(Presence&&) = delete;
+
+        /** @brief Constructs Presence object.
+         *
+         *  @param[in] inventory - Object path under inventory
+                                   to display this inventory item
+         *  @param[in] path      - Device path to read for GPIO pin state
+                                   to determine presence of inventory item
+         *  @param[in] key       - GPIO key to monitor
+         *  @param[in] name      - Pretty name of the inventory item
+         */
+        Presence(const std::string& inventory,
+                 const std::string& path,
+                 const unsigned int key,
+                 const std::string& name) :
+            inventory(inventory),
+            path(path),
+            key(key),
+            name(name),
+            fd(openDevice())
+        {
+            initEvDev();
+            determinePresence();
+        }
+
+    private:
+        /**
+         * @brief Read the GPIO device to determine initial presence and set
+         *        present property at D-Bus path.
+         **/
+        void determinePresence();
+
+        /** @brief Object path under inventory to display this inventory item */
+        const std::string inventory;
+
+        /** @brief Device path to read for GPIO pin state
+                   to determine presence of inventory item */
+        const std::string path;
+
+        /** @brief GPIO key to monitor */
+        const unsigned int key;
+
+        /** @brief Pretty name of the inventory item*/
+        const std::string name;
+
+        /** @brief Event structure */
+        EvdevPtr devicePtr;
+
+        /** @brief Opens the device and populates the descriptor */
+        int openDevice();
+
+        /** @brief File descriptor manager */
+        FileDescriptor fd;
+
+        /** @brief Initializes evdev handle with the fd */
+        void initEvDev();
+};
+
 } // namespace presence
 } // namespace gpio
 } // namespace phosphor