Add discovery of initial state

Added the method discoverInitialState that will be
used to determine if the obmc-standby.target is
either active or inactive.  This catches the race
condition that could happen if just our app was
restarted and the signal had already been given
and we had missed it.

Change-Id: Iadcdcbd2d1045fa62491e9af6e2402d4b17c4a60
Signed-off-by: Josh D. King <jdking@us.ibm.com>
diff --git a/bmc_state_manager.cpp b/bmc_state_manager.cpp
index a0308cd..35a62e8 100644
--- a/bmc_state_manager.cpp
+++ b/bmc_state_manager.cpp
@@ -17,6 +17,7 @@
 
 constexpr auto obmcStandbyTarget = "obmc-standby.target";
 constexpr auto signalDone = "done";
+constexpr auto activeState = "active";
 
 /* Map a transition to it's systemd target */
 const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE =
@@ -28,6 +29,52 @@
 constexpr auto SYSTEMD_OBJ_PATH  = "/org/freedesktop/systemd1";
 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
 
+constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
+constexpr auto SYSTEMD_TGT_PATH      = "/org/freedesktop/systemd1/unit/"
+                                       "obmc_2dstandby_2etarget";
+
+void BMC::discoverInitialState()
+{
+    sdbusplus::message::variant<std::string> currentState;
+
+    auto method = this->bus.new_method_call(SYSTEMD_SERVICE,
+                                            SYSTEMD_TGT_PATH,
+                                            SYSTEMD_PRP_INTERFACE,
+                                            "Get");
+
+    method.append("org.freedesktop.systemd1.Unit", "ActiveState");
+
+    auto result = this->bus.call(method);
+
+    //Is obmc-standby.target active or inactive?
+    result.read(currentState);
+
+    if(currentState == activeState)
+    {
+        log<level::INFO>("Setting the BMCState field",
+                         entry("CURRENT_BMC_STATE=%s",
+                               "BMC_READY"));
+        this->currentBMCState(BMCState::Ready);
+
+        //Unsubscribe so we stop processing all other signals
+        method = this->bus.new_method_call(SYSTEMD_SERVICE,
+                                           SYSTEMD_OBJ_PATH,
+                                           SYSTEMD_INTERFACE,
+                                           "Unsubscribe");
+        this->bus.call(method);
+        this->stateSignal.release();
+    }
+    else
+    {
+        log<level::INFO>("Setting the BMCState field",
+                         entry("CURRENT_BMC_STATE=%s",
+                               "BMC_NOTREADY"));
+        this->currentBMCState(BMCState::NotReady);
+    }
+
+    return;
+}
+
 void BMC::subscribeToSystemdSignals()
 {
     auto method = this->bus.new_method_call(SYSTEMD_SERVICE,