sched-host-tran: support multi-host

The State.Host objects are now templated, so the ScheduledHostTransition
similarly needs to be templated.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I60751dbe5fff5e3c0c2c7a19515524cf5f6bc8f7
diff --git a/scheduled_host_transition.cpp b/scheduled_host_transition.cpp
index ed843cd..8ae81d2 100644
--- a/scheduled_host_transition.cpp
+++ b/scheduled_host_transition.cpp
@@ -94,7 +94,7 @@
 
 void ScheduledHostTransition::hostTransition()
 {
-    auto hostPath = std::string{HOST_OBJPATH} + '0';
+    auto hostPath = std::string{HOST_OBJPATH} + std::to_string(id);
 
     // Set RestartCause to indicate this transition is occurring due to a
     // scheduled host transition as long as it's not an off request
diff --git a/scheduled_host_transition.hpp b/scheduled_host_transition.hpp
index 4d917af..ec97c52 100644
--- a/scheduled_host_transition.hpp
+++ b/scheduled_host_transition.hpp
@@ -30,10 +30,10 @@
 {
   public:
     ScheduledHostTransition(sdbusplus::bus::bus& bus, const char* objPath,
-                            const sdeventplus::Event& event) :
+                            size_t id, const sdeventplus::Event& event) :
         ScheduledHostTransitionInherit(
             bus, objPath, ScheduledHostTransition::action::defer_emit),
-        bus(bus), event(event),
+        bus(bus), id(id), event(event),
         timer(event, std::bind(&ScheduledHostTransition::callback, this))
     {
         initialize();
@@ -62,6 +62,9 @@
     /** @brief sdbusplus bus client connection */
     sdbusplus::bus::bus& bus;
 
+    /** @brief Host id. **/
+    const size_t id = 0;
+
     /** @brief sdbusplus event */
     const sdeventplus::Event& event;
 
diff --git a/scheduled_host_transition_main.cpp b/scheduled_host_transition_main.cpp
index c116f01..600ee72 100644
--- a/scheduled_host_transition_main.cpp
+++ b/scheduled_host_transition_main.cpp
@@ -2,14 +2,36 @@
 
 #include "scheduled_host_transition.hpp"
 
+#include <getopt.h>
+
 #include <sdbusplus/bus.hpp>
 
 #include <cstdlib>
 #include <exception>
 #include <filesystem>
 
-int main()
+int main(int argc, char** argv)
 {
+    size_t hostId = 0;
+
+    int arg;
+    int optIndex = 0;
+
+    static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
+                                       {0, 0, 0, 0}};
+
+    while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
+    {
+        switch (arg)
+        {
+            case 'h':
+                hostId = std::stoul(optarg);
+                break;
+            default:
+                break;
+        }
+    }
+
     namespace fs = std::filesystem;
 
     // Get a default event loop
@@ -19,7 +41,7 @@
     auto bus = sdbusplus::bus::new_default();
 
     // For now, we only have one instance of the host
-    auto objPathInst = std::string{HOST_OBJPATH} + '0';
+    auto objPathInst = std::string{HOST_OBJPATH} + std::to_string(hostId);
 
     // Check SCHEDULED_HOST_TRANSITION_PERSIST_PATH
     auto dir = fs::path(SCHEDULED_HOST_TRANSITION_PERSIST_PATH).parent_path();
@@ -32,9 +54,11 @@
     sdbusplus::server::manager::manager objManager(bus, objPathInst.c_str());
 
     phosphor::state::manager::ScheduledHostTransition manager(
-        bus, objPathInst.c_str(), event);
+        bus, objPathInst.c_str(), hostId, event);
 
-    bus.request_name(SCHEDULED_HOST_TRANSITION_BUSNAME);
+    bus.request_name((std::string{SCHEDULED_HOST_TRANSITION_BUSNAME} +
+                      std::to_string(hostId))
+                         .c_str());
 
     // Attach the bus to sd_event to service user requests
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
diff --git a/service_files/meson.build b/service_files/meson.build
index 87659ca..f6ff41a 100644
--- a/service_files/meson.build
+++ b/service_files/meson.build
@@ -10,7 +10,7 @@
     'xyz.openbmc_project.State.Chassis@.service',
     'xyz.openbmc_project.State.Host@.service',
     'xyz.openbmc_project.State.Hypervisor.service',
-    'xyz.openbmc_project.State.ScheduledHostTransition.service',
+    'xyz.openbmc_project.State.ScheduledHostTransition@.service',
     'phosphor-clear-one-time@.service',
     'phosphor-set-host-transition-to-off@.service',
     'phosphor-set-host-transition-to-running@.service',
diff --git a/service_files/xyz.openbmc_project.State.ScheduledHostTransition.service b/service_files/xyz.openbmc_project.State.ScheduledHostTransition.service
deleted file mode 100644
index 9964724..0000000
--- a/service_files/xyz.openbmc_project.State.ScheduledHostTransition.service
+++ /dev/null
@@ -1,18 +0,0 @@
-[Unit]
-Description=Phosphor Scheduled Host Transition Manager
-Before=mapper-wait@-xyz-openbmc_project-state-scheduledhosttransition.service
-Wants=obmc-mapper.target
-After=obmc-mapper.target
-Wants=xyz.openbmc_project.State.Host.service
-After=xyz.openbmc_project.State.Host.service
-Wants=xyz.openbmc_project.Logging.service
-After=xyz.openbmc_project.Logging.service
-
-[Service]
-ExecStart=/usr/bin/phosphor-scheduled-host-transition
-Restart=always
-Type=dbus
-BusName=xyz.openbmc_project.State.ScheduledHostTransition
-
-[Install]
-WantedBy=multi-user.target
diff --git a/service_files/xyz.openbmc_project.State.ScheduledHostTransition@.service b/service_files/xyz.openbmc_project.State.ScheduledHostTransition@.service
new file mode 100644
index 0000000..7c7ab5d
--- /dev/null
+++ b/service_files/xyz.openbmc_project.State.ScheduledHostTransition@.service
@@ -0,0 +1,17 @@
+[Unit]
+Description=Phosphor Scheduled Host%i Transition Manager
+Wants=obmc-mapper.target
+After=obmc-mapper.target
+Wants=xyz.openbmc_project.State.Host%i.service
+After=xyz.openbmc_project.State.Host%i.service
+Wants=xyz.openbmc_project.Logging.service
+After=xyz.openbmc_project.Logging.service
+
+[Service]
+ExecStart=/usr/bin/phosphor-scheduled-host-transition --host %i
+Restart=always
+Type=dbus
+BusName=xyz.openbmc_project.State.ScheduledHostTransition%i
+
+[Install]
+WantedBy=multi-user.target
diff --git a/test/test_scheduled_host_transition.cpp b/test/test_scheduled_host_transition.cpp
index fc4c6a0..972248c 100644
--- a/test/test_scheduled_host_transition.cpp
+++ b/test/test_scheduled_host_transition.cpp
@@ -31,7 +31,7 @@
 
     TestScheduledHostTransition() :
         event(sdeventplus::Event::get_default()),
-        scheduledHostTransition(mockedBus, "", event)
+        scheduledHostTransition(mockedBus, "", 0, event)
     {
         // Empty
     }