crit-service: refactor unit state checking

Break this out into generic function so it can be utilized to also look
at the new new quiesce target in a future commit

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I1277606d10c1d8e0eafc120ad1fd6cdf80013abd
diff --git a/bmc_state_manager.cpp b/bmc_state_manager.cpp
index e522576..4575a76 100644
--- a/bmc_state_manager.cpp
+++ b/bmc_state_manager.cpp
@@ -43,7 +43,7 @@
 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
 constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
 
-void BMC::discoverInitialState()
+std::string BMC::getUnitState(const std::string& unitToCheck)
 {
     std::variant<std::string> currentState;
     sdbusplus::message::object_path unitTargetPath;
@@ -51,7 +51,7 @@
     auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
                                             SYSTEMD_INTERFACE, "GetUnit");
 
-    method.append(obmcStandbyTarget);
+    method.append(unitToCheck);
 
     try
     {
@@ -61,7 +61,7 @@
     catch (const sdbusplus::exception::exception& e)
     {
         error("Error in GetUnit call: {ERROR}", "ERROR", e);
-        return;
+        return std::string{};
     }
 
     method = this->bus.new_method_call(
@@ -81,18 +81,23 @@
     catch (const sdbusplus::exception::exception& e)
     {
         info("Error in ActiveState Get: {ERROR}", "ERROR", e);
-        return;
+        return std::string{};
     }
+    return (std::get<std::string>(currentState));
+}
 
-    auto currentStateStr = std::get<std::string>(currentState);
+void BMC::discoverInitialState()
+{
+    auto currentStateStr = getUnitState(obmcStandbyTarget);
     if (currentStateStr == activeState)
     {
         info("Setting the BMCState field to 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");
+        auto method =
+            this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
+                                      SYSTEMD_INTERFACE, "Unsubscribe");
         try
         {
             this->bus.call(method);
diff --git a/bmc_state_manager.hpp b/bmc_state_manager.hpp
index 4ea9784..ee97070 100644
--- a/bmc_state_manager.hpp
+++ b/bmc_state_manager.hpp
@@ -70,6 +70,10 @@
 
   private:
     /**
+     * @brief Retrieve input systemd unit state
+     **/
+    std::string getUnitState(const std::string& unitToCheck);
+    /**
      * @brief discover the state of the bmc
      **/
     void discoverInitialState();