Add code to accept command line arguments
Accepts command line arguments for path, interested GPIO transition
and an optional systemd unit file to be called into when the GPIO
transitions per expectation.
Change-Id: I98d967d36cfbb768bc9d0dd04517575fa7e408fe
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..aa81c04
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,5 @@
+sbin_PROGRAMS = phosphor-gpio-monitor
+
+phosphor_gpio_monitor_SOURCES = \
+ argument.cpp \
+ mainapp.cpp
diff --git a/argument.cpp b/argument.cpp
new file mode 100644
index 0000000..41cf6f7
--- /dev/null
+++ b/argument.cpp
@@ -0,0 +1,95 @@
+/**
+ * Copyright © 2016 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 <cassert>
+#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:r:t:?h";
+const option ArgumentParser::options[] =
+{
+ { "path", required_argument, nullptr, 'p' },
+ { "key", required_argument, nullptr, 'k' },
+ { "polarity", required_argument, nullptr, 'r' },
+ { "target", required_argument, nullptr, 't' },
+ { "help", no_argument, nullptr, 'h' },
+ { 0, 0, 0, 0},
+};
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+ int 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 << " --path=<path> Path of input device."
+ " Ex: /dev/input/event2\n";
+ std::cerr << " --key=<key> Input GPIO key number\n";
+ std::cerr << " --polarity=<polarity> Asertion polarity to look for."
+ " This is 0 / 1 \n";
+ std::cerr << " --target=<systemd unit> Systemd unit to be called on GPIO"
+ " state change\n";
+}
+} // namespace gpio
+} // namespace phosphor
diff --git a/argument.hpp b/argument.hpp
new file mode 100644
index 0000000..a6cccc7
--- /dev/null
+++ b/argument.hpp
@@ -0,0 +1,64 @@
+#pragma once
+
+#include <getopt.h>
+#include <map>
+#include <string>
+
+namespace phosphor
+{
+namespace gpio
+{
+/** @brief Class - Encapsulates parsing command line options and
+ * populating arguments
+ */
+class ArgumentParser
+{
+ public:
+ ArgumentParser() = delete;
+ ~ArgumentParser() = default;
+ ArgumentParser(const ArgumentParser&) = delete;
+ ArgumentParser& operator=(const ArgumentParser&) = delete;
+ ArgumentParser(ArgumentParser&&) = default;
+ ArgumentParser& operator=(ArgumentParser&&) = default;
+
+ /** @brief Contructs Argument object
+ *
+ * @param argc - the main function's argc passed as is
+ * @param argv - the main function's argv passed as is
+ * @return Object constructed
+ */
+ ArgumentParser(int argc, char** argv);
+
+ /** @brief Given an option, returns its argument(optarg)
+ *
+ * @param opt - command line option string
+ *
+ * @return argument which is a standard optarg
+ */
+ const std::string& operator[](const std::string& opt);
+
+ /** @brief Displays usage
+ *
+ * @param argv - the main function's argv passed as is
+ */
+ static void usage(char** argv);
+
+ /** @brief Set to 'true' when an option is passed */
+ static const std::string trueString;
+
+ /** @brief Set to '' when an option is not passed */
+ static const std::string emptyString;
+
+ private:
+ /** @brief Option to argument mapping */
+ std::map<const std::string, std::string> arguments;
+
+ /** @brief Array of struct options as needed by getopt_long */
+ static const option options[];
+
+ /** @brief optstring as needed by getopt_long */
+ static const char* optionStr;
+};
+
+} // namespace gpio
+} // namespace phosphor
diff --git a/bootstrap.sh b/bootstrap.sh
new file mode 100755
index 0000000..50b75b7
--- /dev/null
+++ b/bootstrap.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+AUTOCONF_FILES="Makefile.in aclocal.m4 ar-lib autom4te.cache compile \
+ config.guess config.h.in config.sub configure depcomp install-sh \
+ ltmain.sh missing *libtool test-driver"
+
+case $1 in
+ clean)
+ test -f Makefile && make maintainer-clean
+ for file in ${AUTOCONF_FILES}; do
+ find -name "$file" | xargs -r rm -rf
+ done
+ exit 0
+ ;;
+esac
+
+autoreconf -i
+echo 'Run "./configure ${CONFIGURE_FLAGS} && make"'
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..d7b4f75
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,23 @@
+# Initialization
+AC_PREREQ([2.69])
+AC_INIT([phosphor-gpio-monitor], [1.0], [https://github.com/openbmc/phosphor-gpio-monitor/issues])
+AC_LANG([C++])
+AC_CONFIG_HEADERS([config.h])
+AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
+AM_SILENT_RULES([yes])
+
+# Checks for programs.
+AC_PROG_CXX
+AC_PROG_INSTALL
+AC_PROG_MAKE_SET
+
+# Checks for typedefs, structures, and compiler characteristics.
+AX_CXX_COMPILE_STDCXX_14([noext])
+AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
+
+# For linking
+LT_INIT
+
+# Create configured output
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/mainapp.cpp b/mainapp.cpp
new file mode 100644
index 0000000..671b73c
--- /dev/null
+++ b/mainapp.cpp
@@ -0,0 +1,64 @@
+/**
+ * Copyright © 2016 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 <string>
+#include "argument.hpp"
+
+static void exitWithError(const char* err, char** argv)
+{
+ phosphor::gpio::ArgumentParser::usage(argv);
+ std::cerr << "ERROR: " << err << "\n";
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char** argv)
+{
+ // Read arguments.
+ auto options = phosphor::gpio::ArgumentParser(argc, argv);
+
+ // Parse out path argument.
+ auto path = (options)["path"];
+ if (path == phosphor::gpio::ArgumentParser::emptyString)
+ {
+ exitWithError("path not specified.", argv);
+ }
+
+ // Parse out key number that we are interested in
+ // Its integer mapping to the GPIO key configured by the kernel
+ auto key = (options)["key"];
+ if (key == phosphor::gpio::ArgumentParser::emptyString)
+ {
+ exitWithError("Key not specified.", argv);
+ }
+ // TODO : Convert key to integer
+
+ // Parse out assertion polarity interested in
+ // Its either 1 or 0 for press / release
+ auto polarity = (options)["polarity"];
+ if (polarity == phosphor::gpio::ArgumentParser::emptyString)
+ {
+ exitWithError("Polarity not specified.", argv);
+ }
+ // TODO : Convert polarity to integer
+
+ // Parse out target argument. It is fine if the caller does not
+ // pass this if they are not interested in calling into any target
+ // on meeting a condition.
+ auto target = (options)["target"];
+
+ return 0;
+}