Add support for sensorName field in yaml

We want to have ability to specify the human readable names for the OCC
sensors. E.g. `CPU0_OCC` instead of `occ_4_0050`.

This commit allows to specify a `sensorName` field in the YAML config
for each sensor, and this name will be used in the sensor object DBus
path. If the field is not specified, previous behavior will be used.

Tested: the command `busctl tree org.open_power.OCC.Control --list` must
show specified names.

Change-Id: I2f05f7bf44120554ea07b9ee0aac9cfbd33ac376
Signed-off-by: Alexander Filippov <a.filippov@yadro.com>
diff --git a/occ_status.hpp b/occ_status.hpp
index 859b5b0..1617d12 100644
--- a/occ_status.hpp
+++ b/occ_status.hpp
@@ -31,6 +31,12 @@
 // IPMI sensor ID for a given OCC instance
 using sensorID = uint8_t;
 
+// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
+using sensorName = std::string;
+
+// OCC sensors definitions in the map
+using sensorDefs = std::tuple<sensorID, sensorName>;
+
 // OCC sysfs name prefix
 const std::string sysfsName = "occ-hwmon";
 
@@ -60,9 +66,8 @@
     Status(sdbusplus::bus::bus& bus, EventPtr& event, const char* path,
            const Manager& manager,
            std::function<void(bool)> callBack = nullptr) :
-        Interface(bus, path, true),
-        bus(bus), path(path), callBack(callBack),
-        instance(((this->path.back() - '0'))),
+        Interface(bus, getDbusPath(path).c_str(), true),
+        bus(bus), path(path), callBack(callBack), instance(getInstance(path)),
         device(event,
 #ifdef I2C_OCC
                fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
@@ -140,8 +145,8 @@
     /** @brief OCC instance number. Ex, 0,1, etc */
     int instance;
 
-    /** @brief OCC instance to Sensor ID mapping */
-    static const std::map<instanceID, sensorID> sensorMap;
+    /** @brief OCC instance to Sensor definitions mapping */
+    static const std::map<instanceID, sensorDefs> sensorMap;
 
     /** @brief OCC device object to do bind and unbind */
     Device device;
@@ -169,6 +174,39 @@
     /** @brief Sends a message to host control command handler to reset OCC
      */
     void resetOCC();
+
+    /** @brief Determines the instance ID by specified object path.
+     *  @param[in]  path  Estimated OCC Dbus object path
+     *  @return  Instance number
+     */
+    static int getInstance(const std::string& path)
+    {
+        return (path.empty() ? 0 : path.back() - '0');
+    }
+
+    /** @brief Override the sensor name with name from the definition.
+     *  @param[in]  estimatedPath - Estimated OCC Dbus object path
+     *  @return  Fixed OCC DBus object path
+     */
+    static std::string getDbusPath(const std::string& estimatedPath)
+    {
+        if (!estimatedPath.empty())
+        {
+            auto it = sensorMap.find(getInstance(estimatedPath));
+            if (sensorMap.end() != it)
+            {
+                auto& name = std::get<1>(it->second);
+                if (!name.empty() && name != "None")
+                {
+                    auto path = fs::path(estimatedPath);
+                    path.replace_filename(name);
+                    return path.string();
+                }
+            }
+        }
+
+        return estimatedPath;
+    }
 };
 
 } // namespace occ