Refactored code to support INSTANCES as string

Modified code in meson build to support INSTANCES flag as string
type which is passed from machine layer bb file.

Tested: Tested and verified the INSTANCES value.

Signed-off-by: Jayashree Dhanapal <jayashree-d@hcl.com>
Change-Id: I91a7610157c30abf2cbc4783350d8b0a744a6ba9
diff --git a/include/commandutils.hpp b/include/commandutils.hpp
index 17762a9..e318f13 100644
--- a/include/commandutils.hpp
+++ b/include/commandutils.hpp
@@ -18,9 +18,51 @@
 #include <sdbusplus/bus.hpp>
 
 #include <iostream>
+#include "config.h"
 
 static constexpr bool debug = false;
 
+static void instances(std::string s, std::vector<std::string>& host)
+{
+    size_t pos = 0;
+
+    while ((pos = s.find(" ")) != std::string::npos)
+    {
+        host.push_back(s.substr(0, pos));
+        s.erase(0, pos + 1);
+    }
+    host.push_back(s);
+}
+
+inline std::optional<size_t> findHost(size_t id)
+{
+    std::vector<std::string> hosts = {};
+
+    if (hostInstances == "0")
+    {
+        return id;
+    }
+
+    instances(hostInstances, hosts);
+
+    std::sort(hosts.begin(), hosts.end());
+
+    /*  ctx host ID for host 1 is 0, host 2 is 1 ... host N is N-1
+     *  Therefore, host Index is incremented to 1 to map with the dbus
+     *  object path created.
+     */
+    std::string num = std::to_string(id + 1);
+
+    auto instance = std::lower_bound(hosts.begin(), hosts.end(), num);
+
+    if (instance != std::end(hosts))
+    {
+        return id + 1;
+    }
+
+    return std::nullopt;
+}
+
 inline static void printRegistration(unsigned int netfn, unsigned int cmd)
 {
     if constexpr (debug)
diff --git a/meson.build b/meson.build
index 7f7af29..4aedd81 100644
--- a/meson.build
+++ b/meson.build
@@ -30,11 +30,12 @@
   host_instances = get_option('host-instances')
 endif
 
-add_project_arguments(
-  cpp.get_supported_arguments([
-    '-DINSTANCES=' + host_instances,
-  ]),
-  language : 'cpp')
+conf_data = configuration_data()
+conf_data.set_quoted('INSTANCES',host_instances)
+
+configure_file(input: 'meson_config.h.in',
+               output: 'config.h',
+               configuration: conf_data)
 
 if not get_option('bic').disabled()
   add_project_arguments(
diff --git a/meson_config.h.in b/meson_config.h.in
new file mode 100644
index 0000000..ff7109c
--- /dev/null
+++ b/meson_config.h.in
@@ -0,0 +1,3 @@
+#pragma once
+
+inline static const std::string hostInstances = @INSTANCES@;
diff --git a/src/oemcommands.cpp b/src/oemcommands.cpp
index 65ef69f..50b7a71 100644
--- a/src/oemcommands.cpp
+++ b/src/oemcommands.cpp
@@ -188,49 +188,6 @@
 static constexpr auto bootModeProp = "BootMode";
 static constexpr auto bootTypeProp = "BootType";
 
-auto instances(std::string s)
-{
-    std::string delimiter = " ";
-    size_t pos = 0;
-    std::string token;
-    std::vector<std::string> host;
-
-    while ((pos = s.find(delimiter)) != std::string::npos)
-    {
-        token = s.substr(0, pos);
-        host.push_back(token);
-        s.erase(0, pos + delimiter.length());
-    }
-    host.push_back(s);
-
-    return host;
-}
-
-std::optional<size_t> findHost(size_t id)
-{
-    std::string str = INSTANCES;
-    size_t hostId;
-
-    if (INSTANCES == "0")
-    {
-        hostId = id;
-    }
-    else
-    {
-        static const auto hosts = instances(str);
-        std::string num = std::to_string(id + 1);
-        auto instance = std::lower_bound(hosts.begin(), hosts.end(), num);
-
-        if ((instance == hosts.end()) || (*instance != num))
-        {
-            return std::nullopt;
-        }
-        hostId = id + 1;
-    }
-
-    return hostId;
-}
-
 std::tuple<std::string, std::string> objPath(size_t id)
 {
     std::string hostName = "host" + std::to_string(id);
@@ -352,7 +309,7 @@
 bool isMultiHostPlatform()
 {
     bool platform;
-    if (INSTANCES == "0")
+    if (hostInstances == "0")
     {
         platform = false;
     }
@@ -767,7 +724,7 @@
     }
 
     std::copy(std::begin(data), std::end(data), bootSeq);
-    std::optional<size_t> hostId = ipmi::boot::findHost(ctx->hostIdx);
+    std::optional<size_t> hostId = findHost(ctx->hostIdx);
 
     if (!hostId)
     {
@@ -791,7 +748,7 @@
     uint8_t bootSeq[SIZE_BOOT_ORDER];
     uint8_t mode = 0;
 
-    std::optional<size_t> hostId = ipmi::boot::findHost(ctx->hostIdx);
+    std::optional<size_t> hostId = findHost(ctx->hostIdx);
 
     if (!hostId)
     {
diff --git a/src/usb-dbg.cpp b/src/usb-dbg.cpp
index 3579ef1..7b76310 100644
--- a/src/usb-dbg.cpp
+++ b/src/usb-dbg.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <usb-dbg.hpp>
+#include <commandutils.hpp>
 
 namespace ipmi
 {
@@ -943,7 +944,7 @@
             hostPosition = getSelectorPosition();
         }
 
-        if (hostPosition == BMC_POSITION || INSTANCES == "0")
+        if (hostPosition == BMC_POSITION || hostInstances == "0")
         {
             frame_info.append("FRU:spb", 0);
         }