Combine attn handler and openpower hwdiags

The main binary is now openpower-hw-diags. This application will take a
command line option --daemon to load it as a daemon. As a daemon it will
register the attention handler portion of application as the attention
gpio event handler. If the application is not loaded as a daemon it will
operate as a stand alone application which accepts command line options
for requesting hardware analyses and diagnostics operations.

Change-Id: I6210b744cb320873d74a0757928f904ca6296846
Signed-off-by: Ben Tyner <ben.tyner@ibm.com>
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..96c0fa9
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,87 @@
+#include <libpdbg.h>
+
+#include <analyzer/analyzer_main.hpp>
+#include <attn/attn_main.hpp>
+
+#include <algorithm>
+
+/*
+ * @brief Search the command line arguments for an option
+ *
+ * @param i_begin   command line args vector begin
+ * @param i_end     command line args vector end
+ * @param i_option  configuration option to look for
+ *
+ * @return true = option found on command line
+ */
+bool getCliOption(char** i_begin, char** i_end, const std::string& i_option)
+{
+    return (i_end != std::find(i_begin, i_end, i_option));
+}
+
+/*
+ * @brief Search the command line arguments for a setting value
+ *
+ * @param i_begin   command line args vector begin
+ * @param i_end     command line args vectory end
+ * @param i_setting configuration setting to look for
+ *
+ * @return value of the setting or 0 if setting not found or value not given
+ */
+char* getCliSetting(char** i_begin, char** i_end, const std::string& i_setting)
+{
+    char** value = std::find(i_begin, i_end, i_setting);
+    if (value != i_end && ++value != i_end)
+    {
+        return *value;
+    }
+    return 0; // nullptr
+}
+
+/**
+ * @brief Attention handler application main()
+ *
+ * This is the main interface to the hardware diagnostics application. This
+ * application will either be loaded as a daemon for monitoring the attention
+ * gpio or it will be loaded as an application to analyze hardware and
+ * diagnose hadrware error conditions.
+ *
+ * Command line arguments:
+ *
+ *  analyze         analyze hardware
+ *  --daemon        load application as a daemon
+ *  --breakpoints   enable breakpoint special attn handling (in daemon mode)
+ *
+ * @return 0 = success
+ */
+int main(int argc, char* argv[])
+{
+    int rc = 0; // return code
+
+    // initialize pdbg targets
+    pdbg_targets_init(nullptr);
+
+    // TODO Handle target init fail
+
+    // check if we are being loaded as a daemon
+    if (true == getCliOption(argv, argv + argc, "--daemon"))
+    {
+        // Check command line args for breakpoint handling enable option
+        bool bp_enable = getCliOption(argv, argv + argc, "--breakpoints");
+
+        // Configure and start attention monitor
+        attn::attnDaemon(bp_enable);
+    }
+    // We are being loaded as an application, so parse the command line
+    // arguments to determine what operation is being requested.
+    else
+    {
+        // Request to analyze the hardware for error conditions
+        if (true == getCliOption(argv, argv + argc, "analyze"))
+        {
+            analyzer::analyzeHardware();
+        }
+    }
+
+    return rc;
+}