Add base support for GPIO presence

This is the starting code needed to build the application.

Change-Id: I46112a222afbc057ac4fc4f8fb2b4c83822023df
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/.gitignore b/.gitignore
index 50e667c..00cb02d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -42,6 +42,7 @@
 /config.log
 /config.status
 /phosphor-gpio-monitor
+/presence/phosphor-gpio-presence
 Makefile
 .deps
 /*-libtool
diff --git a/Makefile.am b/Makefile.am
index d664672..bbe24fc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,4 +17,4 @@
                                  $(PHOSPHOR_LOGGING_CFLAGS) \
                                  $(LIBEVDEV_CFLAGS)
 
-SUBDIRS = test gpio-util
+SUBDIRS = test gpio-util presence
diff --git a/configure.ac b/configure.ac
index 1a2bb98..7abd732 100644
--- a/configure.ac
+++ b/configure.ac
@@ -53,5 +53,5 @@
 )
 
 # Create configured output
-AC_CONFIG_FILES([Makefile gpio-util/Makefile test/Makefile])
+AC_CONFIG_FILES([Makefile gpio-util/Makefile presence/Makefile test/Makefile])
 AC_OUTPUT
diff --git a/presence/Makefile.am b/presence/Makefile.am
new file mode 100644
index 0000000..f66c0e7
--- /dev/null
+++ b/presence/Makefile.am
@@ -0,0 +1,17 @@
+AM_DEFAULT_SOURCE_EXT = .cpp
+AM_CPPFLAGS = -iquote ${top_srcdir}
+
+sbin_PROGRAMS = \
+	phosphor-gpio-presence
+
+phosphor_gpio_presence_SOURCES = \
+	main.cpp \
+	argument.cpp \
+	gpio_presence.cpp
+
+phosphor_gpio_presence_CXXFLAGS = \
+	$(PHOSPHOR_LOGGING_CFLAGS)
+
+phosphor_gpio_presence_LDADD = \
+	$(PHOSPHOR_LOGGING_LIBS)
+
diff --git a/presence/argument.cpp b/presence/argument.cpp
new file mode 100644
index 0000000..0fa94d0
--- /dev/null
+++ b/presence/argument.cpp
@@ -0,0 +1,97 @@
+/**
+ * Copyright © 2017 IBM 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 <iostream>
+#include <iterator>
+#include <algorithm>
+#include "argument.hpp"
+
+namespace phosphor
+{
+namespace gpio
+{
+
+using namespace std::string_literals;
+
+const std::string ArgumentParser::trueString = "true"s;
+const std::string ArgumentParser::emptyString = ""s;
+
+const char* ArgumentParser::optionStr = "p:k:n:i:?h";
+const option ArgumentParser::options[] =
+{
+    { "path",      required_argument,  nullptr,   'p' },
+    { "key",       required_argument,  nullptr,   'k' },
+    { "name",      required_argument,  nullptr,   'n' },
+    { "inventory", required_argument,  nullptr,   'i' },
+    { "help",      no_argument,        nullptr,   'h' },
+    { 0, 0, 0, 0},
+};
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+    auto option = 0;
+    while (-1 != (option = getopt_long(argc, argv,
+                                       optionStr, options, nullptr)))
+    {
+        if ((option == '?') || (option == 'h'))
+        {
+            usage(argv);
+            exit(-1);
+        }
+
+        auto i = &options[0];
+        while ((i->val != option) && (i->val != 0))
+        {
+            ++i;
+        }
+
+        if (i->val)
+        {
+            arguments[i->name] = (i->has_arg ? optarg : trueString);
+        }
+    }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+    auto i = arguments.find(opt);
+    if (i == arguments.end())
+    {
+        return emptyString;
+    }
+    else
+    {
+        return i->second;
+    }
+}
+
+void ArgumentParser::usage(char** argv)
+{
+    std::cerr << "Usage: " << argv[0] << " [options]\n";
+    std::cerr << "Options:\n";
+    std::cerr << "  --help                  Print this menu\n";
+    std::cerr << "  --inventory=<inventory> Object path under inventory"
+              " that will be created\n";
+    std::cerr << "  --path=<path>           Path of device to read for GPIO pin"
+              " state to determine presence of inventory item\n";
+    std::cerr << "  --key=<key>             Input GPIO key number\n";
+    std::cerr << "  --name=<name>           Pretty name of the inventory"
+              " item\n";
+    std::cerr << std::flush;
+    exit(-1);
+}
+} // namespace gpio
+} // namespace phosphor
diff --git a/presence/gpio_presence.cpp b/presence/gpio_presence.cpp
new file mode 100644
index 0000000..11a32cd
--- /dev/null
+++ b/presence/gpio_presence.cpp
@@ -0,0 +1,13 @@
+#include "gpio_presence.hpp"
+
+namespace phosphor
+{
+namespace gpio
+{
+namespace presence
+{
+
+} // namespace presence
+} // namespace gpio
+} // namespace phosphor
+
diff --git a/presence/gpio_presence.hpp b/presence/gpio_presence.hpp
new file mode 100644
index 0000000..67fd4cb
--- /dev/null
+++ b/presence/gpio_presence.hpp
@@ -0,0 +1,13 @@
+#pragma once
+
+namespace phosphor
+{
+namespace gpio
+{
+namespace presence
+{
+
+} // namespace presence
+} // namespace gpio
+} // namespace phosphor
+
diff --git a/presence/main.cpp b/presence/main.cpp
new file mode 100644
index 0000000..3c7c3cf
--- /dev/null
+++ b/presence/main.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <phosphor-logging/log.hpp>
+#include "argument.hpp"
+#include "gpio_presence.hpp"
+
+using namespace phosphor::logging;
+using namespace phosphor::gpio;
+
+int main(int argc, char* argv[])
+{
+    auto options = ArgumentParser(argc, argv);
+
+    auto inventory = options["inventory"];
+    auto key = options["key"];
+    auto path = options["path"];
+    if (argc < 4)
+    {
+        std::cerr << "Too few arguments\n";
+        options.usage(argv);
+    }
+
+    if (inventory == ArgumentParser::emptyString)
+    {
+        std::cerr << "Inventory argument required\n";
+        options.usage(argv);
+    }
+
+    if (key == ArgumentParser::emptyString)
+    {
+        std::cerr << "GPIO key argument required\n";
+        options.usage(argv);
+    }
+
+    if (path == ArgumentParser::emptyString)
+    {
+        std::cerr << "Device path argument required\n";
+        options.usage(argv);
+    }
+
+    return 0;
+}
+