sensorhandler: modify Get Sensor Reading to use dbus interface

Change-Id: Ice16bda6c151b0865f3fa6e8234b8e2456d5b887
Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index 1d0f4c9..eacc274 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -1,4 +1,5 @@
 #include <mapper.h>
+#include <math.h>
 #include <stdio.h>
 #include <string.h>
 #include <bitset>
@@ -556,6 +557,9 @@
 
     *data_len=0;
 
+    int64_t raw_value, scale;
+    ipmi::sensor::Info sensor;
+
     switch(type) {
         case 0xC3:
         case 0xC2:
@@ -584,6 +588,58 @@
             resp->indication[1] = 0;
             break;
 
+        case IPMI_SENSOR_TEMP:
+        case IPMI_SENSOR_VOLTAGE:
+        case IPMI_SENSOR_CURRENT:
+        case IPMI_SENSOR_FAN:
+            // Get reading for /xyz/openbmc_project/Sensor/Value.interface
+
+            // Get value
+            r = sd_bus_get_property_trivial(bus,
+                                            a.bus,
+                                            a.path,
+                                            a.interface,
+                                            "Value",
+                                            NULL,
+                                            'x',
+                                            &raw_value);
+            if (r < 0) {
+                fprintf(stderr,
+                        "Failed to call sd_bus_get_property:%d,  %s, 'value'\n",
+                        r,
+                        strerror(-r));
+                fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
+                        a.bus, a.path, a.interface);
+                break;
+            }
+
+            // Get scale
+            r = sd_bus_get_property_trivial(bus,
+                                            a.bus,
+                                            a.path,
+                                            a.interface,
+                                            "Scale",
+                                            NULL,
+                                            'x',
+                                            &scale);
+            if (r < 0) {
+                fprintf(stderr,
+                        "Failed to call sd_bus_get_property:%d,  %s (scale)\n",
+                        r,
+                        strerror(-r));
+                fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
+                        a.bus, a.path, a.interface);
+                break;
+            }
+
+            resp->value = raw_value * pow(10,scale);
+            resp->operation = 0;
+            resp->indication[0] = 0;
+            resp->indication[1] = 0;
+            rc = IPMI_CC_OK;
+            *data_len=sizeof(sensorreadingresp_t);
+            break;
+
         default:
             *data_len=0;
             rc = IPMI_CC_SENSOR_INVALID;
diff --git a/sensorhandler.h b/sensorhandler.h
index d58f640..cedb2e4 100644
--- a/sensorhandler.h
+++ b/sensorhandler.h
@@ -11,6 +11,15 @@
     IPMI_CMD_SET_SENSOR      = 0x30,
 };
 
+// Discrete sensor types.
+enum ipmi_sensor_types
+{
+    IPMI_SENSOR_TEMP    = 0x01,
+    IPMI_SENSOR_VOLTAGE = 0x02,
+    IPMI_SENSOR_CURRENT = 0x03,
+    IPMI_SENSOR_FAN     = 0x04,
+};
+
 #define MAX_DBUS_PATH 128
 struct dbus_interface_t {
     uint8_t  sensornumber;