mainloop: add uniqueness to busname

There can be multiple daemon instances that have the
same devpath which leads to a collision which errors silently.
This adds uniqueness to the path, which was no longer human-readable
to avoid this collision situation.

Tested: Ran on quanta-q71l with unique devices and two instances
pointing to the same device and it correctly set them up with unique
but deterministic bus names.
Change-Id: Id5aea3c3df5f793b28557a74995608ec40792a43
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/mainloop.cpp b/mainloop.cpp
index bfc010e..221c8c9 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -20,6 +20,7 @@
 #include <cstring>
 #include <string>
 #include <unordered_set>
+#include <sstream>
 
 #include <phosphor-logging/elog-errors.hpp>
 #include "config.h"
@@ -380,12 +381,14 @@
 
 MainLoop::MainLoop(
     sdbusplus::bus::bus&& bus,
+    const std::string& param,
     const std::string& path,
     const std::string& devPath,
     const char* prefix,
     const char* root)
     : _bus(std::move(bus)),
       _manager(_bus, root),
+      _pathParam(param),
       _hwmonRoot(),
       _instance(),
       _devPath(devPath),
@@ -481,12 +484,13 @@
     }
 
     {
-        std::string busname{_prefix};
-        busname.append(1, '-');
-        busname.append(
-                std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
-        busname.append(".Hwmon1");
-        _bus.request_name(busname.c_str());
+        std::stringstream ss;
+        ss << _prefix
+           << "-"
+           << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
+           << ".Hwmon1";
+
+        _bus.request_name(ss.str().c_str());
     }
 
     {
diff --git a/mainloop.hpp b/mainloop.hpp
index fec576d..d420c28 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -41,6 +41,7 @@
         /** @brief Constructor
          *
          *  @param[in] bus - sdbusplus bus client connection.
+         *  @param[in] param - the path parameter provided
          *  @param[in] path - hwmon sysfs instance to manage
          *  @param[in] devPath - physical device sysfs path.
          *  @param[in] prefix - DBus busname prefix.
@@ -54,6 +55,7 @@
          */
         MainLoop(
             sdbusplus::bus::bus&& bus,
+            const std::string& param,
             const std::string& path,
             const std::string& devPath,
             const char* prefix,
@@ -84,6 +86,8 @@
         sdbusplus::bus::bus _bus;
         /** @brief sdbusplus freedesktop.ObjectManager storage. */
         sdbusplus::server::manager::manager _manager;
+        /** @brief the parameter path used. */
+        std::string _pathParam;
         /** @brief hwmon sysfs class path. */
         std::string _hwmonRoot;
         /** @brief hwmon sysfs instance. */
diff --git a/readd.cpp b/readd.cpp
index 3c2c2d2..78edc77 100644
--- a/readd.cpp
+++ b/readd.cpp
@@ -35,6 +35,7 @@
 
     // Parse out path argument.
     auto path = (*options)["dev-path"];
+    auto param = path;
     if (path != ArgumentParser::empty_string)
     {
         // This path may either be a device path (starts with
@@ -52,6 +53,7 @@
     if (path == ArgumentParser::empty_string)
     {
         path = (*options)["path"];
+        param = path;
     }
 
     if (path == ArgumentParser::empty_string)
@@ -71,6 +73,7 @@
 
     MainLoop loop(
         sdbusplus::bus::new_default(),
+        param,
         path,
         calloutPath,
         BUSNAME_PREFIX,