discover-system-state: add multi-host support

Add HostObjects class to fetch paths of settings d-bus objects under
/xyz/openbmc_project/control/hostX .

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I1dc55ec389e94e6c85aa7031b304f8c4ed15dc71
diff --git a/discover_system_state.cpp b/discover_system_state.cpp
index 6ea73d1..e7f0978 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -39,6 +39,7 @@
 {
     using namespace phosphor::logging;
 
+    size_t hostId = 0;
     std::string hostPath = "/xyz/openbmc_project/state/host0";
     int arg;
     int optIndex = 0;
@@ -51,6 +52,7 @@
         switch (arg)
         {
             case 'h':
+                hostId = std::stoul(optarg);
                 hostPath =
                     std::string("/xyz/openbmc_project/state/host") + optarg;
                 break;
@@ -62,7 +64,7 @@
     auto bus = sdbusplus::bus::new_default();
 
     using namespace settings;
-    Objects settings(bus);
+    HostObjects settings(bus, hostId);
 
     using namespace phosphor::state::manager;
     namespace server = sdbusplus::xyz::openbmc_project::State::server;
diff --git a/host_state_manager.hpp b/host_state_manager.hpp
index c3394f0..0e796f5 100644
--- a/host_state_manager.hpp
+++ b/host_state_manager.hpp
@@ -69,7 +69,7 @@
                 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
             std::bind(std::mem_fn(&Host::sysStateChangeJobNew), this,
                       std::placeholders::_1)),
-        settings(bus), id(id)
+        settings(bus, id), id(id)
     {
         // Enable systemd signals
         subscribeToSystemdSignals();
@@ -303,8 +303,8 @@
     /** @brief Used to subscribe to dbus systemd JobNew signal **/
     sdbusplus::bus::match_t systemdSignalJobNew;
 
-    // Settings objects of interest
-    settings::Objects settings;
+    // Settings host objects of interest
+    settings::HostObjects settings;
 
     /** @brief Host id. **/
     const size_t id = 0;
diff --git a/service_files/phosphor-discover-system-state@.service b/service_files/phosphor-discover-system-state@.service
index 4fbb186..345266c 100644
--- a/service_files/phosphor-discover-system-state@.service
+++ b/service_files/phosphor-discover-system-state@.service
@@ -1,13 +1,13 @@
 [Unit]
 Description=Reboot If Enabled
-Wants=mapper-wait@-xyz-openbmc_project-control-host0-power_restore_policy.service
-After=mapper-wait@-xyz-openbmc_project-control-host0-power_restore_policy.service
+Wants=mapper-wait@-xyz-openbmc_project-control-host%i-power_restore_policy.service
+After=mapper-wait@-xyz-openbmc_project-control-host%i-power_restore_policy.service
 Wants=mapper-wait@-xyz-openbmc_project-state-host%i.service
 After=mapper-wait@-xyz-openbmc_project-state-host%i.service
 Wants=mapper-wait@-xyz-openbmc_project-state-chassis%i.service
 After=mapper-wait@-xyz-openbmc_project-state-chassis%i.service
-Wants=mapper-wait@-xyz-openbmc_project-state-bmc%i.service
-After=mapper-wait@-xyz-openbmc_project-state-bmc%i.service
+Wants=mapper-wait@-xyz-openbmc_project-state-bmc0.service
+After=mapper-wait@-xyz-openbmc_project-state-bmc0.service
 After=op-reset-chassis-on@%i.service
 ConditionPathExists=!/run/openbmc/chassis@%i-on
 
diff --git a/settings.cpp b/settings.cpp
index ff5d36f..8146261 100644
--- a/settings.cpp
+++ b/settings.cpp
@@ -18,7 +18,7 @@
 constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
 constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
 
-Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus)
+Objects::Objects(sdbusplus::bus::bus& bus, const Path& root) : bus(bus)
 {
     std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
     auto depth = 0;
@@ -127,4 +127,8 @@
     return result.begin()->first;
 }
 
+HostObjects::HostObjects(sdbusplus::bus::bus& bus, size_t id) :
+    Objects(bus, Path("/xyz/openbmc_project/control/host") + std::to_string(id))
+{}
+
 } // namespace settings
diff --git a/settings.hpp b/settings.hpp
index dcdfe8a..b54f202 100644
--- a/settings.hpp
+++ b/settings.hpp
@@ -11,7 +11,7 @@
 using Service = std::string;
 using Interface = std::string;
 
-constexpr auto root = "/";
+constexpr auto defaultRoot = "/";
 constexpr auto autoRebootIntf = "xyz.openbmc_project.Control.Boot.RebootPolicy";
 constexpr auto powerRestoreIntf =
     "xyz.openbmc_project.Control.Power.RestorePolicy";
@@ -24,9 +24,10 @@
   public:
     /** @brief Constructor - fetch settings objects
      *
-     * @param[in] bus - The Dbus bus object
+     * @param[in] bus  - The Dbus bus object
+     * @param[in] root - The root object path
      */
-    explicit Objects(sdbusplus::bus::bus& bus);
+    explicit Objects(sdbusplus::bus::bus& bus, const Path& root = defaultRoot);
     Objects(const Objects&) = delete;
     Objects& operator=(const Objects&) = delete;
     Objects(Objects&&) = delete;
@@ -60,4 +61,20 @@
     sdbusplus::bus::bus& bus;
 };
 
+/** @class HostObjects
+ *  @brief Fetch paths of settings d-bus objects of Host
+ *  @note  IMPORTANT: This class only supports settings under the
+ *         /xyz/openbmc_project/control/hostX object paths
+ */
+struct HostObjects : public Objects
+{
+  public:
+    /** @brief Constructor - fetch settings objects of Host
+     *
+     * @param[in] bus - The Dbus bus object
+     * @param[in] id  - The Host id
+     */
+    HostObjects(sdbusplus::bus::bus& bus, size_t id);
+};
+
 } // namespace settings