util::dbus: Make common the hostRunningState code

Move the code for getting the host running state from attention handler
specific code to common utility code.

Signed-off-by: Ben Tyner <ben.tyner@ibm.com>
Change-Id: I457662cc13aa4dc89b321f238cd060e7e7486d1b
diff --git a/attn/attn_dbus.cpp b/attn/attn_dbus.cpp
index a82e5ce..a9af4c6 100644
--- a/attn/attn_dbus.cpp
+++ b/attn/attn_dbus.cpp
@@ -1,7 +1,6 @@
 #include <attn_common.hpp>
 #include <attn_dbus.hpp>
 #include <attn_logging.hpp>
-#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
 
 #include <string>
 #include <vector>
@@ -224,66 +223,4 @@
     return fd; // file descriptor or -1
 }
 
-/** @brief Get the running state of the host */
-HostRunningState hostRunningState()
-{
-    HostRunningState host = HostRunningState::Unknown;
-
-    // dbus specifics
-    constexpr auto path      = "/xyz/openbmc_project/state/host0";
-    constexpr auto interface = "xyz.openbmc_project.State.Boot.Progress";
-    constexpr auto extended  = "org.freedesktop.DBus.Properties";
-    constexpr auto function  = "Get";
-
-    sdbusplus::message::message method;
-
-    if (0 == dbusMethod(path, interface, function, method, extended))
-    {
-        try
-        {
-            // additional dbus call parameters
-            method.append(interface, "BootProgress");
-
-            // using system dbus
-            auto bus      = sdbusplus::bus::new_system();
-            auto response = bus.call(method);
-
-            // reply will be a variant
-            std::variant<std::string, bool, std::vector<uint8_t>,
-                         std::vector<std::string>>
-                reply;
-
-            // parse dbus response into reply
-            response.read(reply);
-
-            // get boot progress (string) and convert to boot stage
-            std::string bootProgress(std::get<std::string>(reply));
-
-            using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::
-                server::Progress::ProgressStages;
-
-            BootProgress stage = sdbusplus::xyz::openbmc_project::State::Boot::
-                server::Progress::convertProgressStagesFromString(bootProgress);
-
-            if ((stage == BootProgress::SystemInitComplete) ||
-                (stage == BootProgress::OSStart) ||
-                (stage == BootProgress::OSRunning))
-            {
-                host = HostRunningState::Started;
-            }
-            else
-            {
-                host = HostRunningState::NotStarted;
-            }
-        }
-        catch (const sdbusplus::exception::SdBusError& e)
-        {
-            trace<level::ERROR>("hostRunningState exception");
-            std::string traceMsg = std::string(e.what(), maxTraceLen);
-            trace<level::ERROR>(traceMsg.c_str());
-        }
-    }
-
-    return host;
-}
 } // namespace attn
diff --git a/attn/attn_dbus.hpp b/attn/attn_dbus.hpp
index 088bf59..59291a7 100644
--- a/attn/attn_dbus.hpp
+++ b/attn/attn_dbus.hpp
@@ -8,13 +8,6 @@
 namespace attn
 {
 
-enum class HostRunningState
-{
-    Unknown,
-    NotStarted,
-    Started
-};
-
 /**
  * Create a dbus method
  *
@@ -72,14 +65,4 @@
  */
 int getPel(const uint32_t i_pelId);
 
-/**
- * Get the host running state
- *
- * Use host boot progress to determine if a host has been started. If host
- * boot progress can not be determined then host state will be unknown.
- *
- * @return HostType == "Unknown", "Started or "NotStarted"
- */
-HostRunningState hostRunningState();
-
 } // namespace attn
diff --git a/attn/attn_handler.cpp b/attn/attn_handler.cpp
index c1a4b48..85172ea 100644
--- a/attn/attn_handler.cpp
+++ b/attn/attn_handler.cpp
@@ -14,6 +14,7 @@
 #include <attn/bp_handler.hpp>
 #include <attn/ti_handler.hpp>
 #include <attn/vital_handler.hpp>
+#include <util/dbus.hpp>
 
 #include <algorithm>
 #include <iomanip>
@@ -271,13 +272,17 @@
                 {
                     trace<level::INFO>("TI info data ptr is invalid");
 
-                    HostRunningState runningState = hostRunningState();
-                    std::string stateString       = "host state unknown";
+                    util::dbus::HostRunningState runningState =
+                        util::dbus::hostRunningState();
 
-                    if ((HostRunningState::Started == runningState) ||
-                        (HostRunningState::Unknown == runningState))
+                    std::string stateString = "host state unknown";
+
+                    if ((util::dbus::HostRunningState::Started ==
+                         runningState) ||
+                        (util::dbus::HostRunningState::Unknown == runningState))
                     {
-                        if (HostRunningState::Started == runningState)
+                        if (util::dbus::HostRunningState::Started ==
+                            runningState)
                         {
                             stateString = "host started";
                         }
diff --git a/util/dbus.cpp b/util/dbus.cpp
index e9526df..635a73c 100644
--- a/util/dbus.cpp
+++ b/util/dbus.cpp
@@ -1,5 +1,6 @@
 #include <util/dbus.hpp>
 #include <util/trace.hpp>
+#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
 
 namespace util
 {
@@ -225,6 +226,53 @@
     return autoReboot;
 }
 
+/** @brief Get the running state of the host */
+HostRunningState hostRunningState()
+{
+    // assume not able to get host running state
+    HostRunningState host = HostRunningState::Unknown;
+
+    constexpr auto interface = "xyz.openbmc_project.State.Boot.Progress";
+
+    DBusService service;
+    DBusPath path;
+
+    // find a dbus object and path that implements the interface
+    if (0 == find(interface, path, service))
+    {
+        DBusValue value;
+
+        // boot progress is implemented as a property
+        constexpr auto property = "BootProgress";
+
+        if (0 == getProperty(interface, path, service, property, value))
+        {
+            // return value is a variant, progress is in the vector of strings
+            std::string bootProgress(std::get<std::string>(value));
+
+            // convert boot progress to host state
+            using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::
+                server::Progress::ProgressStages;
+
+            BootProgress stage = sdbusplus::xyz::openbmc_project::State::Boot::
+                server::Progress::convertProgressStagesFromString(bootProgress);
+
+            if ((stage == BootProgress::SystemInitComplete) ||
+                (stage == BootProgress::OSStart) ||
+                (stage == BootProgress::OSRunning))
+            {
+                host = HostRunningState::Started;
+            }
+            else
+            {
+                host = HostRunningState::NotStarted;
+            }
+        }
+    }
+
+    return host;
+}
+
 } // namespace dbus
 
 } // namespace util
diff --git a/util/dbus.hpp b/util/dbus.hpp
index d1e152f..aaf3cdc 100644
--- a/util/dbus.hpp
+++ b/util/dbus.hpp
@@ -64,7 +64,7 @@
  */
 std::vector<std::string> systemNames();
 
-/** @brief Host states for util::dbus host state operations */
+/** @brief Host transition states for host transition operations */
 enum class HostState
 {
     Quiesce,
@@ -89,6 +89,24 @@
  */
 bool autoRebootEnabled();
 
+/** @brief Host running states for host running operations */
+enum class HostRunningState
+{
+    Unknown,
+    NotStarted,
+    Started
+};
+
+/**
+ * Get the host running state
+ *
+ * Use host boot progress to determine if a host has been started. If host
+ * boot progress can not be determined then host state will be unknown.
+ *
+ * @return HostType == "Unknown", "Started or "NotStarted"
+ */
+HostRunningState hostRunningState();
+
 } // namespace dbus
 
 } // namespace util