Add sensor instance id command line argument.

Introduce `-i|--sensor-id` flag to explicitly set sensor
suffix for dbus name.

Sample usage:

   phosphor-hwmon-readd -i test_sensor_id -o /apb/...

Will register the service with the following busname:

   xyz.openbmc_project.Hwmon-test_sensor_id.Hwmon1

This change required as a part of privilege separation work:
  https://github.com/openbmc/openbmc/issues/3383

Signed-off-by: Anton D. Kachalov <gmouse@google.com>
Change-Id: I48ff9c3efe0edb84718ff8f695e7e932af5445de
diff --git a/README.md b/README.md
index 8a1948f..5cf2ff4 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@
 
 The algorithm is <PREFIX>-<ID>.Hwmon<N> where PREFIX is an autoconf
 configurable prefix (BUSNAME_PREFIX, xyz.openbmc_project by default),
-ID is a std::hash of the /sys/devices path backing the hwmon class
-instance, and N is the implemented phosphor-hwmon D-Bus API version.
+ID is either a std::hash of the /sys/devices path backing the hwmon class
+instance or provided suffix value from the command line, and N is
+the implemented phosphor-hwmon D-Bus API version.
 ```
diff --git a/mainloop.cpp b/mainloop.cpp
index 40c429a..75c8c12 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -309,11 +309,13 @@
 MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
                    const std::string& path, const std::string& devPath,
                    const char* prefix, const char* root,
+                   const std::string& instanceId,
                    const hwmonio::HwmonIOInterface* ioIntf) :
     _bus(std::move(bus)),
     _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
     _devPath(devPath), _prefix(prefix), _root(root), _state(),
-    _ioAccess(ioIntf), _event(sdeventplus::Event::get_default()),
+    _instanceId(instanceId), _ioAccess(ioIntf),
+    _event(sdeventplus::Event::get_default()),
     _timer(_event, std::bind(&MainLoop::read, this))
 {
     // Strip off any trailing slashes.
@@ -403,9 +405,13 @@
 
     {
         std::stringstream ss;
-        ss << _prefix << "-"
-           << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
-           << ".Hwmon1";
+        std::string id = _instanceId;
+        if (id.empty())
+        {
+            id =
+                std::to_string(std::hash<std::string>{}(_devPath + _pathParam));
+        }
+        ss << _prefix << "-" << id << ".Hwmon1";
 
         _bus.request_name(ss.str().c_str());
     }
diff --git a/mainloop.hpp b/mainloop.hpp
index b3de022..af92479 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -45,6 +45,7 @@
      *  @param[in] devPath - physical device sysfs path.
      *  @param[in] prefix - DBus busname prefix.
      *  @param[in] root - DBus sensors namespace root.
+     *  @param[in] instanceId - override value to identify instance on d-bus.
      *
      *  Any DBus objects are created relative to the DBus
      *  sensors namespace root.
@@ -55,6 +56,7 @@
     MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
              const std::string& path, const std::string& devPath,
              const char* prefix, const char* root,
+             const std::string& instanceId,
              const hwmonio::HwmonIOInterface* ioIntf);
 
     /** @brief Setup polling timer in a sd event loop and attach to D-Bus
@@ -105,6 +107,8 @@
     const char* _root;
     /** @brief DBus object state. */
     SensorState _state;
+    /** @brief DBus instance id specified by command line. */
+    std::string _instanceId;
     /** @brief Sleep interval in microseconds. */
     uint64_t _interval = default_interval;
     /** @brief Hwmon sysfs access. */
diff --git a/readd.cpp b/readd.cpp
index cba043e..06e7d8c 100644
--- a/readd.cpp
+++ b/readd.cpp
@@ -34,10 +34,12 @@
     // Read arguments.
     std::string syspath = "";
     std::string devpath = "";
+    std::string sensor_id = "";
 
     CLI::App app{"OpenBMC Hwmon Daemon"};
     app.add_option("-p,--path", syspath, "sysfs location to monitor");
     app.add_option("-o,--dev-path", devpath, "device path to monitor");
+    app.add_option("-i,--sensor-id", sensor_id, "dbus sensor instance id");
 
     CLI11_PARSE(app, argc, argv);
 
@@ -80,7 +82,7 @@
 
     hwmonio::HwmonIO io(path);
     MainLoop loop(sdbusplus::bus::new_default(), param, path, calloutPath,
-                  BUSNAME_PREFIX, SENSOR_ROOT, &io);
+                  BUSNAME_PREFIX, SENSOR_ROOT, sensor_id, &io);
     loop.run();
 
     return 0;
diff --git a/xyz.openbmc_project.Hwmon@.service b/xyz.openbmc_project.Hwmon@.service
index b930b57..29ab262 100644
--- a/xyz.openbmc_project.Hwmon@.service
+++ b/xyz.openbmc_project.Hwmon@.service
@@ -5,6 +5,6 @@
 
 [Service]
 Restart=on-failure
-ExecStart=/usr/bin/env phosphor-hwmon-readd -o %I
+ExecStart=/usr/bin/env phosphor-hwmon-readd -i ${HW_SENSOR_ID} -o %I
 SyslogIdentifier=phosphor-hwmon-readd
 EnvironmentFile=/etc/default/obmc/hwmon/%I.conf