nvmesensor: Use slave address from configuration json

Replace hardcoded slave address with address from the configuration
This patch is for non-compliant nvme devices (e.g.
configurations/nvme_p4000.json).
Tested: Manually by verifying the slave address in Entity Manager
configuration is used by the nvmesensor binary

Change-Id: I56b8187578c22a9be98ccd2c50a7b9838a087e52
Signed-off-by: Nnamdi Ajah <nnamdi@cloudflare.com>
diff --git a/src/NVMeSensorMain.cpp b/src/NVMeSensorMain.cpp
index ef1abbe..9cd453e 100644
--- a/src/NVMeSensorMain.cpp
+++ b/src/NVMeSensorMain.cpp
@@ -23,6 +23,8 @@
 #include <optional>
 #include <regex>
 
+static constexpr uint8_t nvmeMiDefaultSlaveAddr = 0x6A;
+
 static NVMEMap nvmeDeviceMap;
 
 NVMEMap& getNVMEMap()
@@ -44,6 +46,21 @@
     return std::visit(VariantToIntVisitor(), findBus->second);
 }
 
+static uint8_t extractSlaveAddr(const std::string& path,
+                                const SensorBaseConfigMap& properties)
+{
+    auto findSlaveAddr = properties.find("Address");
+    if (findSlaveAddr == properties.end())
+    {
+        std::cerr << "could not determine slave address for " << path << "\n"
+                  << "using default as specified in nvme-mi"
+                  << "\n";
+        return nvmeMiDefaultSlaveAddr;
+    }
+
+    return std::visit(VariantToUnsignedIntVisitor(), findSlaveAddr->second);
+}
+
 static std::optional<std::string>
     extractSensorName(const std::string& path,
                       const SensorBaseConfigMap& properties)
@@ -138,6 +155,7 @@
             extractBusNumber(interfacePath, sensorConfig);
         std::optional<std::string> sensorName =
             extractSensorName(interfacePath, sensorConfig);
+        uint8_t slaveAddr = extractSlaveAddr(interfacePath, sensorConfig);
         std::optional<int> rootBus = deriveRootBus(busNumber);
 
         if (!(busNumber && sensorName && rootBus))
@@ -163,7 +181,8 @@
             std::shared_ptr<NVMeSensor> sensorPtr =
                 std::make_shared<NVMeSensor>(
                     objectServer, io, dbusConnection, *sensorName,
-                    std::move(sensorThresholds), interfacePath, *busNumber);
+                    std::move(sensorThresholds), interfacePath, *busNumber,
+                    slaveAddr);
 
             context->addSensor(sensorPtr);
         }