util::dbus: Make common the transition host support

Move transition host support from attention handler specific code to
common util code.

Signed-off-by: Ben Tyner <ben.tyner@ibm.com>
Change-Id: I7fb4970354aaeca65fcc074107f99262e504ac34
diff --git a/attn/attn_common.cpp b/attn/attn_common.cpp
index 4f333e8..89d28b9 100644
--- a/attn/attn_common.cpp
+++ b/attn/attn_common.cpp
@@ -12,32 +12,6 @@
 namespace attn
 {
 
-/** @brief Transition the host state */
-void transitionHost(const HostState i_hostState)
-{
-    // The host quiesce code will handle the instruction-stop task(s)
-    // thread_stop_all(); // in libpdbg
-
-    // We will be transitioning host by starting appropriate dbus target
-    std::string target = "obmc-host-quiesce@0.target"; // quiesce is default
-
-    // crash (mpipl) mode state requested
-    if (HostState::Crash == i_hostState)
-    {
-        target = "obmc-host-crash@0.target";
-    }
-
-    auto bus    = sdbusplus::bus::new_system();
-    auto method = bus.new_method_call(
-        "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
-        "org.freedesktop.systemd1.Manager", "StartUnit");
-
-    method.append(target);    // target unit to start
-    method.append("replace"); // mode = replace conflicting queued jobs
-
-    bus.call_noreply(method); // start the service
-}
-
 /** @brief Traces some regs for hostboot */
 void addHbStatusRegs()
 {
diff --git a/attn/attn_common.hpp b/attn/attn_common.hpp
index 67d3f71..6f4428d 100644
--- a/attn/attn_common.hpp
+++ b/attn/attn_common.hpp
@@ -33,22 +33,6 @@
     ATTN_PDBG_SCOM = 3
 };
 
-enum class HostState
-{
-    Quiesce,
-    Diagnostic,
-    Crash
-};
-
-/**
- * @brief Transition the host state
- *
- * We will transition the host state by starting the appropriate dbus target.
- *
- * @param i_hostState the state to transition the host to
- */
-void transitionHost(const HostState i_hostState);
-
 /**
  * @brief Traces some regs for hostboot
  *
diff --git a/attn/ti_handler.cpp b/attn/ti_handler.cpp
index 69191ab..fb0893f 100644
--- a/attn/ti_handler.cpp
+++ b/attn/ti_handler.cpp
@@ -5,6 +5,7 @@
 #include <attn/ti_handler.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/exception.hpp>
+#include <util/dbus.hpp>
 
 #include <iomanip>
 #include <iostream>
@@ -100,12 +101,12 @@
     if (autoRebootEnabled())
     {
         // If autoreboot is enabled we will start crash (mpipl) mode target
-        transitionHost(HostState::Crash);
+        util::dbus::transitionHost(util::dbus::HostState::Crash);
     }
     else
     {
         // If autoreboot is disabled we will quiesce the host
-        transitionHost(HostState::Quiesce);
+        util::dbus::transitionHost(util::dbus::HostState::Quiesce);
     }
 }
 
@@ -253,7 +254,7 @@
 
     if (true == terminateHost)
     {
-        transitionHost(HostState::Quiesce);
+        util::dbus::transitionHost(util::dbus::HostState::Quiesce);
     }
 }
 
diff --git a/attn/vital_handler.cpp b/attn/vital_handler.cpp
index b0a771e..910c005 100644
--- a/attn/vital_handler.cpp
+++ b/attn/vital_handler.cpp
@@ -2,6 +2,7 @@
 #include <attn/attn_common.hpp>
 #include <attn/attn_logging.hpp>
 #include <sdbusplus/bus.hpp>
+#include <util/dbus.hpp>
 
 namespace attn
 {
@@ -28,7 +29,7 @@
     else
     {
         // transition host state after analyses
-        transitionHost(HostState::Quiesce);
+        util::dbus::transitionHost(util::dbus::HostState::Quiesce);
 
         // generate pel
         eventVital();
diff --git a/util/dbus.cpp b/util/dbus.cpp
index 7e8a234..968a44f 100644
--- a/util/dbus.cpp
+++ b/util/dbus.cpp
@@ -165,6 +165,38 @@
     return names;
 }
 
+/** @brief Transition the host state */
+void transitionHost(const HostState i_hostState)
+{
+    try
+    {
+        // We will be transitioning host by starting appropriate dbus target
+        std::string target = "obmc-host-quiesce@0.target"; // quiesce is default
+
+        // crash (mpipl) mode state requested
+        if (HostState::Crash == i_hostState)
+        {
+            target = "obmc-host-crash@0.target";
+        }
+
+        auto bus    = sdbusplus::bus::new_system();
+        auto method = bus.new_method_call(
+            "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
+            "org.freedesktop.systemd1.Manager", "StartUnit");
+
+        method.append(target);    // target unit to start
+        method.append("replace"); // mode = replace conflicting queued jobs
+
+        bus.call_noreply(method); // start the service
+    }
+    catch (const sdbusplus::exception::SdBusError& e)
+    {
+        trace::err("util::dbus::transitionHost exception");
+        std::string traceMsg = std::string(e.what());
+        trace::err(traceMsg.c_str());
+    }
+}
+
 } // namespace dbus
 
 } // namespace util
diff --git a/util/dbus.hpp b/util/dbus.hpp
index 03fb5be..66a2ad4 100644
--- a/util/dbus.hpp
+++ b/util/dbus.hpp
@@ -64,6 +64,23 @@
  */
 std::vector<std::string> systemNames();
 
+/** @brief Host states for util::dbus host state operations */
+enum class HostState
+{
+    Quiesce,
+    Diagnostic,
+    Crash
+};
+
+/**
+ * @brief Transition the host state
+ *
+ * We will transition the host state by starting the appropriate dbus target.
+ *
+ * @param i_hostState the state to transition the host to
+ */
+void transitionHost(const HostState i_hostState);
+
 } // namespace dbus
 
 } // namespace util