discover_system_state: Accept a node parameter

In this commit, discover_system_state is extended to accept a
command line argument such that the user can specify a particular
host. This will allow discover_system_state to run for host1,
host2, or any other host!

Resolves openbmc/openbmc#1466

Change-Id: Ib58a4bc1fd5dfe10a6455778eac779eb0b2085e0
Signed-off-by: Michael Tritz <mtritz@us.ibm.com>
diff --git a/discover_system_state.cpp b/discover_system_state.cpp
index 303b4a4..d403671 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -1,3 +1,4 @@
+#include <getopt.h>
 #include <iostream>
 #include <map>
 #include <string>
@@ -28,8 +29,6 @@
 
 constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
 
-constexpr auto HOST_PATH = "/xyz/openbmc_project/state/host0";
-
 std::string getService(sdbusplus::bus::bus& bus, std::string path,
                        std::string interface)
 {
@@ -116,10 +115,33 @@
 } // namespace state
 } // namepsace phosphor
 
-int main()
+int main(int argc, char** argv)
 {
     using namespace phosphor::logging;
 
+    std::string hostPath = "/xyz/openbmc_project/state/host0";
+    int arg;
+    int optIndex = 0;
+
+    static struct option longOpts[] =
+    {
+          {"host", required_argument, 0, 'h'},
+          {0, 0, 0, 0}
+    };
+
+    while((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
+    {
+        switch (arg)
+        {
+            case 'h':
+                hostPath = std::string("/xyz/openbmc_project/state/host") +
+                        optarg;
+                break;
+            default:
+                break;
+        }
+    }
+
     auto bus = sdbusplus::bus::new_default();
 
     using namespace settings;
@@ -156,7 +178,7 @@
         RestorePolicy::convertPolicyFromString(powerPolicy))
     {
         log<level::INFO>("power_policy=ALWAYS_POWER_ON, powering host on");
-        setProperty(bus, HOST_PATH, HOST_BUSNAME,
+        setProperty(bus, hostPath, HOST_BUSNAME,
                     "RequestedHostTransition",
                     convertForMessage(server::Host::Transition::On));
     }
@@ -166,10 +188,10 @@
         log<level::INFO>("power_policy=RESTORE, restoring last state");
 
         // Read last requested state and re-request it to execute it
-        auto hostReqState = getProperty(bus, HOST_PATH,
+        auto hostReqState = getProperty(bus, hostPath,
                                         HOST_BUSNAME,
                                         "RequestedHostTransition");
-        setProperty(bus, HOST_PATH, HOST_BUSNAME,
+        setProperty(bus, hostPath, HOST_BUSNAME,
                     "RequestedHostTransition",
                     hostReqState);
     }