sensor yaml: allow custom sensor names

Currently, we can only name sensors based on data from the dbus object
implementing a sensor's interface. While this mostly works, there may
not be a sensible name to extract in all cases.

This change introduces an optional sensorName property to the sensor
YAML description, which allows a custom name to be used.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
Change-Id: I3f3b8ef581c70fbde94db122d4f3c0ca181c6ff7
diff --git a/include/ipmid/types.hpp b/include/ipmid/types.hpp
index 3e64cb4..fa4d58c 100644
--- a/include/ipmid/types.hpp
+++ b/include/ipmid/types.hpp
@@ -168,6 +168,7 @@
     std::function<uint8_t(SetSensorReadingReq&, const Info&)> updateFunc;
     std::function<GetSensorResponse(const Info&)> getFunc;
     Mutability mutability;
+    SensorName sensorName;
     std::function<SensorName(const Info&)> sensorNameFunc;
     DbusInterfaceMap propertyInterfaces;
 };
diff --git a/scripts/sensor-example.yaml b/scripts/sensor-example.yaml
index c0fbe1e..9760cd0 100644
--- a/scripts/sensor-example.yaml
+++ b/scripts/sensor-example.yaml
@@ -93,7 +93,8 @@
             type: uint32_t
   path: /xyz/openbmc_project/state/host1
   readingType: readingAssertion
-  sensorNamePattern: nameProperty
+  # set an explicit name for the sensor
+  sensorName: RebootAttempts
   sensorReadingType: 0x6F
   mutability: Mutability::Write|Mutability::Read
   sensorType: 0xC3
diff --git a/scripts/writesensor.mako.cpp b/scripts/writesensor.mako.cpp
index 69a44f4..8b26805 100644
--- a/scripts/writesensor.mako.cpp
+++ b/scripts/writesensor.mako.cpp
@@ -3,6 +3,7 @@
 // !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
 <%
 interfaceDict = {}
+sensorNameMaxLength = 16
 %>\
 %for key in sensorDict.keys():
 <%
@@ -52,12 +53,19 @@
        scale = sensor.get("scale", 0)
        hasScale = "true" if "scale" in sensor.keys() else "false"
        valueReadingType = sensor["readingType"]
-       sensorNamePattern = sensor.get("sensorNamePattern", "nameLeaf")
-       sensorNameFunc = "get::" + sensorNamePattern
        updateFunc = interfaceDict[serviceInterface]["updateFunc"]
        updateFunc += sensor["readingType"]
        getFunc = interfaceDict[serviceInterface]["getFunc"]
        getFunc += sensor["readingType"]
+       sensorName = sensor.get("sensorName", None)
+       if sensorName:
+           assert len(sensorName) <= sensorNameMaxLength, \
+                   "sensor name '%s' is too long (%d bytes max)" % \
+                   (sensorName, sensorNameMaxLength)
+       else:
+           sensorNameFunc = "get::" + sensor.get("sensorNamePattern",
+                   "nameLeaf")
+
        if "readingAssertion" == valueReadingType or "readingData" == valueReadingType:
            for interface,properties in interfaces.items():
                for dbus_property,property_value in properties.items():
@@ -87,7 +95,11 @@
         .updateFunc = ${updateFunc},
         .getFunc = ${getFunc},
         .mutability = Mutability(${mutability}),
+    % if sensorName:
+        .sensorName = "${sensorName}",
+    % else:
         .sensorNameFunc = ${sensorNameFunc},
+    % endif
         .propertyInterfaces = {
     % for interface,properties in interfaces.items():
             {"${interface}",{
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index d2fac66..e23ea48 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -715,7 +715,12 @@
     }
 
     /* ID string */
-    auto id_string = info->sensorNameFunc(*info);
+    auto id_string = info->sensorName;
+
+    if (id_string.empty())
+    {
+        id_string = info->sensorNameFunc(*info);
+    }
 
     if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
     {