sensorhandler: allow preconfigured unit and scale

Allow specifying the scale and unit for a sensor in the
YAML configuration.  This can save clock time by no longer
requiring the daemon to reach out to the sensor owner for
the information.

Change-Id: I5f63286fc32fb9d64ebab7d86d1dbb7ff40591f4
Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/scripts/writesensor.mako.cpp b/scripts/writesensor.mako.cpp
index c5db13a..e081474 100644
--- a/scripts/writesensor.mako.cpp
+++ b/scripts/writesensor.mako.cpp
@@ -43,6 +43,9 @@
        multiplier = sensor.get("multiplierM", 1)
        offsetB = sensor.get("offsetB", 0)
        exp = sensor.get("bExp", 0)
+       unit = sensor.get("unit", "")
+       scale = sensor.get("scale", 0)
+       hasScale = "true" if "scale" in sensor.keys() else "false"
        valueReadingType = sensor["readingType"]
        updateFunc = interfaceDict[serviceInterface]["updateFunc"]
        updateFunc += sensor["readingType"]
@@ -61,7 +64,9 @@
        mutability = sensor.get("mutability", "Mutability::Read")
 %>
         ${sensorType},"${path}","${sensorInterface}",${readingType},${multiplier},
-        ${offsetB},${exp},${offsetB * pow(10,exp)},${updateFunc},${getFunc},Mutability(${mutability}),{
+        ${offsetB},${exp},${offsetB * pow(10,exp)},
+        ${hasScale},${scale},"${unit}",
+        ${updateFunc},${getFunc},Mutability(${mutability}),{
     % for interface,properties in interfaces.items():
             {"${interface}",{
             % for dbus_property,property_value in properties.items():
@@ -127,7 +132,7 @@
             % endfor
             }},
     % endfor
-     }
+     },
 }},
    % endif
 % endfor
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index 582f16b..3358b83 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -697,20 +697,27 @@
         "xyz.openbmc_project.Sensor.Value")
     {
         std::string result {};
-        char *raw_cstr = NULL;
-        if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
-                                            iface.interface, "Unit", NULL,
-                                            &raw_cstr))
+        if (info->unit.empty())
         {
-            log<level::WARNING>("Unit interface missing.",
-                                entry("BUS=%s", iface.bus),
-                                entry("PATH=%s", iface.path));
+            char *raw_cstr = NULL;
+            if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
+                                               iface.interface, "Unit", NULL,
+                                               &raw_cstr))
+            {
+                log<level::WARNING>("Unit interface missing.",
+                                    entry("BUS=%s", iface.bus),
+                                    entry("PATH=%s", iface.path));
+            }
+            else
+            {
+                result = raw_cstr;
+            }
+            free(raw_cstr);
         }
         else
         {
-            result = raw_cstr;
+            result = info->unit;
         }
-        free(raw_cstr);
 
         namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
         try {
@@ -758,17 +765,24 @@
     if (info->propertyInterfaces.begin()->first ==
         "xyz.openbmc_project.Sensor.Value")
     {
-        if (0 > sd_bus_get_property_trivial(bus,
-                                        iface.bus,
-                                        iface.path,
-                                        iface.interface,
-                                        "Scale",
-                                        NULL,
-                                        'x',
-                                        &result)) {
-            log<level::WARNING>("Scale interface missing.",
-                                entry("BUS=%s", iface.bus),
-                                entry("PATH=%s", iface.path));
+        if (info->hasScale)
+        {
+            result = info->scale;
+        }
+        else
+        {
+            if (0 > sd_bus_get_property_trivial(bus,
+                                                iface.bus,
+                                                iface.path,
+                                                iface.interface,
+                                                "Scale",
+                                                NULL,
+                                                'x',
+                                                &result)) {
+                log<level::WARNING>("Scale interface missing.",
+                                    entry("BUS=%s", iface.bus),
+                                    entry("PATH=%s", iface.path));
+            }
         }
     }
 
diff --git a/types.hpp b/types.hpp
index 0177c8f..a43c8c0 100644
--- a/types.hpp
+++ b/types.hpp
@@ -113,6 +113,8 @@
 using OffsetB = uint16_t;
 using Exponent = uint8_t;
 using ScaledOffset = int64_t;
+using Scale = int16_t;
+using Unit = std::string;
 
 enum class Mutability
 {
@@ -142,6 +144,9 @@
    OffsetB coefficientB;
    Exponent exponentB;
    ScaledOffset scaledOffset;
+   bool hasScale;
+   Scale scale;
+   Unit unit;
    std::function<uint8_t(SetSensorReadingReq&, const Info&)> updateFunc;
    std::function<GetSensorResponse(const Info&)> getFunc;
    Mutability mutability;