move systemd subscribe to util function

This code is replicated in multiple source files in this repo, put it in
the util repo so everyone can call it in one place.

Tested:
- Verified systemd signals seen and system boots

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I8bf0343c79012cc91dea033038894a011fd37ef5
diff --git a/bmc_state_manager.cpp b/bmc_state_manager.cpp
index 473329c..006972c 100644
--- a/bmc_state_manager.cpp
+++ b/bmc_state_manager.cpp
@@ -116,24 +116,6 @@
     return;
 }
 
-void BMC::subscribeToSystemdSignals()
-{
-    auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
-                                            SYSTEMD_INTERFACE, "Subscribe");
-
-    try
-    {
-        this->bus.call(method);
-    }
-    catch (const sdbusplus::exception_t& e)
-    {
-        error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
-        elog<InternalFailure>();
-    }
-
-    return;
-}
-
 void BMC::executeTransition(const Transition tranReq)
 {
     // HardReboot does not shutdown any services and immediately transitions
diff --git a/bmc_state_manager.hpp b/bmc_state_manager.hpp
index 29e318f..a52d184 100644
--- a/bmc_state_manager.hpp
+++ b/bmc_state_manager.hpp
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "utils.hpp"
 #include "xyz/openbmc_project/State/BMC/server.hpp"
 
 #include <linux/watchdog.h>
@@ -40,7 +41,7 @@
                 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
             [this](sdbusplus::message_t& m) { bmcStateChange(m); }))
     {
-        subscribeToSystemdSignals();
+        utils::subscribeToSystemdSignals(bus);
         discoverInitialState();
         discoverLastRebootCause();
         this->emit_object_added();
@@ -75,11 +76,6 @@
      **/
     void discoverInitialState();
 
-    /**
-     * @brief subscribe to the systemd signals
-     **/
-    void subscribeToSystemdSignals();
-
     /** @brief Execute the transition request
      *
      *  @param[in] tranReq   - Transition requested
diff --git a/chassis_state_manager.cpp b/chassis_state_manager.cpp
index db5ae96..71accee 100644
--- a/chassis_state_manager.cpp
+++ b/chassis_state_manager.cpp
@@ -72,23 +72,6 @@
     "xyz.openbmc_project.State.Decorator.PowerSystemInputs";
 constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
 
-void Chassis::subscribeToSystemdSignals()
-{
-    try
-    {
-        auto method = this->bus.new_method_call(
-            SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Subscribe");
-        this->bus.call(method);
-    }
-    catch (const sdbusplus::exception_t& e)
-    {
-        error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
-        elog<InternalFailure>();
-    }
-
-    return;
-}
-
 void Chassis::createSystemdTargetTable()
 {
     systemdTargetTable = {
diff --git a/chassis_state_manager.hpp b/chassis_state_manager.hpp
index 7e9c7b9..6edaa7d 100644
--- a/chassis_state_manager.hpp
+++ b/chassis_state_manager.hpp
@@ -2,6 +2,7 @@
 
 #include "config.h"
 
+#include "utils.hpp"
 #include "xyz/openbmc_project/State/Chassis/server.hpp"
 #include "xyz/openbmc_project/State/PowerOnHours/server.hpp"
 
@@ -59,7 +60,7 @@
             sdeventplus::Event::get_default(), [this](auto&) { pohCallback(); },
             std::chrono::hours{1}, std::chrono::minutes{1})
     {
-        subscribeToSystemdSignals();
+        utils::subscribeToSystemdSignals(bus);
 
         createSystemdTargetTable();
 
@@ -113,15 +114,6 @@
      */
     bool determineStatusOfPSUPower();
 
-    /**
-     * @brief subscribe to the systemd signals
-     *
-     * This object needs to capture when it's systemd targets complete
-     * so it can keep it's state updated
-     *
-     **/
-    void subscribeToSystemdSignals();
-
     /** @brief Start the systemd unit requested
      *
      * This function calls `StartUnit` on the systemd unit given.
diff --git a/host_state_manager.cpp b/host_state_manager.cpp
index 6ff1f56..398fca5 100644
--- a/host_state_manager.cpp
+++ b/host_state_manager.cpp
@@ -59,22 +59,6 @@
 constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
 constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
 
-void Host::subscribeToSystemdSignals()
-{
-    auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
-                                            SYSTEMD_INTERFACE, "Subscribe");
-    try
-    {
-        this->bus.call_noreply(method);
-    }
-    catch (const sdbusplus::exception_t& e)
-    {
-        error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
-        elog<InternalFailure>();
-    }
-    return;
-}
-
 void Host::determineInitialState()
 {
     if (stateActive(getTarget(server::Host::HostState::Running)) ||
diff --git a/host_state_manager.hpp b/host_state_manager.hpp
index 8e60130..fc80da8 100644
--- a/host_state_manager.hpp
+++ b/host_state_manager.hpp
@@ -3,6 +3,7 @@
 #include "config.h"
 
 #include "settings.hpp"
+#include "utils.hpp"
 #include "xyz/openbmc_project/State/Host/server.hpp"
 
 #include <cereal/access.hpp>
@@ -69,7 +70,7 @@
         settings(bus, id), id(id)
     {
         // Enable systemd signals
-        subscribeToSystemdSignals();
+        utils::subscribeToSystemdSignals(bus);
 
         // create map of target name base on host id
         createSystemdTargetMaps();
@@ -152,15 +153,6 @@
 
   private:
     /**
-     * @brief subscribe to the systemd signals
-     *
-     * This object needs to capture when it's systemd targets complete
-     * so it can keep it's state updated
-     *
-     **/
-    void subscribeToSystemdSignals();
-
-    /**
      * @brief Determine initial host state and set internally
      *
      * @return Will throw exceptions on failure
diff --git a/utils.cpp b/utils.cpp
index 9ef652f..ec85e41 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -21,12 +21,33 @@
 
 PHOSPHOR_LOG2_USING;
 
+constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
+constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
+constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
+
 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
 constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
 
-std::string getService(sdbusplus::bus_t& bus, std::string path,
+void subscribeToSystemdSignals(sdbusplus::bus::bus& bus)
+{
+    auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
+                                      SYSTEMD_INTERFACE, "Subscribe");
+
+    try
+    {
+        bus.call(method);
+    }
+    catch (const sdbusplus::exception_t& e)
+    {
+        error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
+        throw std::runtime_error("Unable to subscribe to systemd signals");
+    }
+    return;
+}
+
+std::string getService(sdbusplus::bus::bus& bus, std::string path,
                        std::string interface)
 {
     auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
diff --git a/utils.hpp b/utils.hpp
index 9c1dd18..cbb7248 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -12,6 +12,14 @@
 namespace utils
 {
 
+/** @brief Tell systemd to generate d-bus events
+ *
+ * @param[in] bus          - The Dbus bus object
+ *
+ * @return void, will throw exception on failure
+ */
+void subscribeToSystemdSignals(sdbusplus::bus::bus& bus);
+
 /** @brief Get service name from object path and interface
  *
  * @param[in] bus          - The Dbus bus object