sensor: Add support for multiple sensor naming pattern

Change-Id: Ibf58d4a4f5a2783a9152f4b5f685e28ef527c5e2
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/sensordatahandler.cpp b/sensordatahandler.cpp
index 3aba53f..7db8fe0 100644
--- a/sensordatahandler.cpp
+++ b/sensordatahandler.cpp
@@ -110,6 +110,20 @@
 namespace get
 {
 
+SensorName nameParentLeaf(const Info& sensorInfo)
+{
+    const auto pos = sensorInfo.sensorPath.find_last_of('/');
+    const auto leaf = sensorInfo.sensorPath.substr(pos + 1);
+
+    const auto remaining = sensorInfo.sensorPath.substr(0, pos);
+
+    const auto parentPos = remaining.find_last_of('/');
+    auto parent = remaining.substr(parentPos + 1);
+
+    parent += "_" + leaf;
+    return parent;
+}
+
 GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
                                      const InstancePath& path,
                                      const DbusInterface& interface)
diff --git a/sensordatahandler.hpp b/sensordatahandler.hpp
index 1b05342..01b8e4f 100644
--- a/sensordatahandler.hpp
+++ b/sensordatahandler.hpp
@@ -49,6 +49,60 @@
 namespace get
 {
 
+/** @brief Populate sensor name from the D-Bus property associated with the
+ *         sensor. In the example entry from the yaml, the name of the D-bus
+ *         property "AttemptsLeft" is the sensor name.
+ *
+ *         0x07:
+ *            sensorType: 195
+ *            path: /xyz/openbmc_project/state/host0
+ *            sensorReadingType: 0x6F
+ *            serviceInterface: org.freedesktop.DBus.Properties
+ *            readingType: readingAssertion
+ *            sensorNamePattern: nameProperty
+ *            interfaces:
+ *              xyz.openbmc_project.Control.Boot.RebootAttempts:
+ *                AttemptsLeft:
+ *                    Offsets:
+ *                        0xFF:
+ *                          type: uint32_t
+ *
+ *
+ *  @param[in] sensorInfo - Dbus info related to sensor.
+ *
+ *  @return On success return the sensor name for the sensor.
+ */
+inline SensorName nameProperty(const Info& sensorInfo)
+{
+    return sensorInfo.propertyInterfaces.begin()->second.begin()->first;
+}
+
+/** @brief Populate sensor name from the D-Bus object associated with the
+ *         sensor. If the object path is /system/chassis/motherboard/dimm0 then
+ *         the leaf dimm0 is considered as the sensor name.
+ *
+ *  @param[in] sensorInfo - Dbus info related to sensor.
+ *
+ *  @return On success return the sensor name for the sensor.
+ */
+inline SensorName nameLeaf(const Info& sensorInfo)
+{
+    return sensorInfo.sensorPath.substr(
+            sensorInfo.sensorPath.find_last_of('/') + 1,
+            sensorInfo.sensorPath.length());
+}
+
+/** @brief Populate sensor name from the D-Bus object associated with the
+ *         sensor. If the object path is /system/chassis/motherboard/cpu0/core0
+ *         then the sensor name is cpu0_core0. The leaf and the parent is put
+ *         together to get the sensor name.
+ *
+ *  @param[in] sensorInfo - Dbus info related to sensor.
+ *
+ *  @return On success return the sensor name for the sensor.
+ */
+SensorName nameParentLeaf(const Info& sensorInfo);
+
 /**
  *  @brief Helper function to map the dbus info to sensor's assertion status
  *         for the get sensor reading command.
diff --git a/types.hpp b/types.hpp
index 62f7db7..2580063 100644
--- a/types.hpp
+++ b/types.hpp
@@ -115,6 +115,9 @@
 using ScaledOffset = int64_t;
 using Scale = int16_t;
 using Unit = std::string;
+using EntityType = uint8_t;
+using EntityInst = uint8_t;
+using SensorName = std::string;
 
 enum class Mutability
 {