Merge pull request #54 from causten/sensorpolling

Add support for get-sensor-reading
diff --git a/ipmid.C b/ipmid.C
index 0126779..0f4139c 100644
--- a/ipmid.C
+++ b/ipmid.C
@@ -561,44 +561,7 @@
 // Routines used by ipmi commands wanting to interact on the dbus
 //
 /////////////////////////////////////////////////////////////////////
-
-
-// Simple set routine because some methods are standard.
-int set_sensor_dbus_state(uint8_t number, const char *method, const char *value) {
-
-
-    dbus_interface_t a;
-    int r;
-    sd_bus_error error = SD_BUS_ERROR_NULL;
-    sd_bus_message *reply = NULL, *m=NULL;
-
-    fprintf(ipmidbus, "Attempting to set a dbus Sensor 0x%02x via %s with a value of %s\n",
-        number, method, value);
-
-    r = find_openbmc_path("SENSOR", number, &a);
-
-    r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
-    if (r < 0) {
-        fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
-    }
-
-    r = sd_bus_message_append(m, "s", value);
-    if (r < 0) {
-        fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
-    }
-
-    r = sd_bus_call(bus, m, 0, &error, &reply);
-    if (r < 0) {
-        fprintf(stderr, "Failed to call the method: %s", strerror(-r));
-    }
-
-    sd_bus_error_free(&error);
-    sd_bus_message_unref(m);
-
-    return 0;
-}
-
-int set_sensor_dbus_state_v(uint8_t number, const char *method, char *value) {
+int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
 
 
     dbus_interface_t a;
@@ -624,7 +587,7 @@
 
     r = sd_bus_call(bus, m, 0, &error, NULL);
     if (r < 0) {
-        fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
+        fprintf(stderr, "Failed to call the method: %s", strerror(-r));
     }
 
 
@@ -633,3 +596,38 @@
 
     return 0;
 }
+int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
+
+
+    dbus_interface_t a;
+    int r;
+    sd_bus_error error = SD_BUS_ERROR_NULL;
+    sd_bus_message *m=NULL;
+
+    fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
+        number, method, value);
+
+    r = find_openbmc_path("SENSOR", number, &a);
+
+    r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
+    if (r < 0) {
+        fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
+    }
+
+    r = sd_bus_message_append(m, "v", "y", value);
+    if (r < 0) {
+        fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
+    }
+
+
+    r = sd_bus_call(bus, m, 0, &error, NULL);
+    if (r < 0) {
+        fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
+    }
+
+
+    sd_bus_error_free(&error);
+    sd_bus_message_unref(m);
+
+    return 0;
+}
\ No newline at end of file
diff --git a/ipmid.H b/ipmid.H
index c75b4ac..679392d 100644
--- a/ipmid.H
+++ b/ipmid.H
@@ -20,4 +20,6 @@
 
 extern FILE *ipmiio, *ipmidbus, *ipmicmddetails;
 
-#endif
+int set_sensor_dbus_state_s(uint8_t , const char *, const char *);
+int set_sensor_dbus_state_y(uint8_t , const char *, const uint8_t);
+#endif
\ No newline at end of file
diff --git a/ipmisensor.C b/ipmisensor.C
index 1f60f64..b6927ac 100644
--- a/ipmisensor.C
+++ b/ipmisensor.C
@@ -2,10 +2,10 @@
 #include <string.h>
 #include <stdint.h>
 #include <malloc.h>
-
+#include <ipmid.H>
+#include "sensorhandler.h"
 
 extern uint8_t find_sensor(uint8_t);
-extern int set_sensor_dbus_state_v(uint8_t , const char *, char *);
 
 
 struct sensorRES_t {
@@ -30,19 +30,20 @@
 	uint8_t sensor_type;
 	uint8_t offset;
 	int (*func)(const sensorRES_t *, const lookup_t *, const char *);
-	char    method[16];
+	char    member[16];
 	char    assertion[64];
 	char    deassertion[64];
 };
 
-
-extern int updateDbusInterface(uint8_t , const char *, const char *) ;
-extern int set_sensor_dbus_state(uint8_t ,const char *, const char *);
+extern int updateDbusInterface(uint8_t , const char *, const char *);
+extern int find_openbmc_path(const char *, const uint8_t , dbus_interface_t *) ;
 
 
 int set_sensor_dbus_state_simple(const sensorRES_t *pRec, const lookup_t *pTable, const char *value) {
 
-	return set_sensor_dbus_state(pRec->sensor_number, pTable->method, value);
+	return set_sensor_dbus_state_s(pRec->sensor_number,
+                                   pTable->member,
+                                   value);
 }
 
 struct event_data_t {
@@ -133,18 +134,17 @@
 					break;
 	}
 
-	return set_sensor_dbus_state_v(pRec->sensor_number, pTable->method, p);
+	return set_sensor_dbus_state_s(pRec->sensor_number,
+                                   pTable->member,
+                                   p);
 }
 
 // Handling this special OEM sensor by coping what is in byte 4.  I also think that is odd
 // considering byte 3 is for sensor reading.  This seems like a misuse of the IPMI spec
 int set_sensor_dbus_state_osbootcount(const sensorRES_t *pRec, const lookup_t *pTable, const char *value) {
-	char valuestring[32];
-	char* pStr = valuestring;
-
-	sprintf(valuestring, "%d", pRec->assert_state7_0);
-
-	return set_sensor_dbus_state_v(pRec->sensor_number, pTable->method, pStr);
+	return set_sensor_dbus_state_y(pRec->sensor_number,
+                                   "setValue",
+                                   pRec->assert_state7_0);
 }
 
 int set_sensor_dbus_state_system_event(const sensorRES_t *pRec, const lookup_t *pTable, const char *value) {
@@ -167,7 +167,9 @@
 					break;
 	}
 
-	return set_sensor_dbus_state_v(pRec->sensor_number, pTable->method, p);
+	return set_sensor_dbus_state_s(pRec->sensor_number,
+                                   pTable->member,
+                                   p);
 }
 
 
diff --git a/sensorhandler.C b/sensorhandler.C
index c96b7a7..d171bf5 100644
--- a/sensorhandler.C
+++ b/sensorhandler.C
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
+#include <systemd/sd-bus.h>
 
 extern int updateSensorRecordFromSSRAESC(const void *);
 extern int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) ;
@@ -32,6 +33,7 @@
     {0x12, 0x6F, "SYSTEM_EVENT"},
     {0xC7, 0x03, "SYSTEM"},
     {0xC7, 0x03, "MAIN_PLANAR"},
+    {0xC2, 0x6F, "PowerCap"},
     {0xFF, 0x00, ""},
 };
 
@@ -40,6 +42,11 @@
     uint8_t sennum;
 }  __attribute__ ((packed)) ;
 
+struct sensorreadingresp_t {
+    uint8_t value;
+    uint8_t operation;
+    uint8_t indication[2];
+}  __attribute__ ((packed)) ;
 
 uint8_t dbus_to_sensor_type(char *p) {
 
@@ -104,6 +111,10 @@
     return r;
  }
 
+
+
+
+
 ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
                              ipmi_request_t request, ipmi_response_t response,
                              ipmi_data_len_t data_len, ipmi_context_t context)
@@ -149,6 +160,72 @@
     return rc;
 }
 
+
+ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
+                             ipmi_request_t request, ipmi_response_t response,
+                             ipmi_data_len_t data_len, ipmi_context_t context)
+{
+    sensor_data_t *reqptr = (sensor_data_t*)request;
+    ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
+    uint8_t type;
+    sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
+    int r;
+    dbus_interface_t a;
+    sd_bus *bus = ipmid_get_sd_bus_connection();
+    sd_bus_message *reply = NULL;
+    uint8_t reading;
+
+
+    printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
+
+    r = find_openbmc_path("SENSOR", reqptr->sennum, &a);
+
+    type = find_sensor(reqptr->sennum);
+
+    fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
+
+    *data_len=0;
+
+    switch(type) {
+        case 0xC3:
+        case 0xC2:
+            r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "y");
+            if (r < 0) {
+                fprintf(stderr, "Failed to call sd_bus_get_property:%d,  %s\n", r, strerror(-r));
+                fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
+                        a.bus, a.path, a.interface);
+                break;
+            }
+
+            r = sd_bus_message_read(reply, "y", &reading);
+            if (r < 0) {
+                fprintf(stderr, "Failed to read byte: %s\n", strerror(-r));
+                break;
+            }
+
+            printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
+
+            rc = IPMI_CC_OK;
+            *data_len=sizeof(sensorreadingresp_t);
+
+            resp->value         = reading;
+            resp->operation     = 0;
+            resp->indication[0] = 0;
+            resp->indication[1] = 0;
+            break;
+
+        default:
+            *data_len=0;
+            rc = IPMI_CC_SENSOR_INVALID;
+            break;
+    }
+
+
+    sd_bus_message_unref(reply);
+
+    return rc;
+}
+
 ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
                              ipmi_request_t request, ipmi_response_t response,
                              ipmi_data_len_t data_len, ipmi_context_t context)
@@ -173,5 +250,8 @@
     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
     ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, NULL, ipmi_sen_set_sensor);
 
+    printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
+    ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, NULL, ipmi_sen_get_sensor_reading);
+
     return;
 }
diff --git a/sensorhandler.h b/sensorhandler.h
index 7b89a18..dd940dc 100644
--- a/sensorhandler.h
+++ b/sensorhandler.h
@@ -6,6 +6,7 @@
 // IPMI commands for net functions.
 enum ipmi_netfn_sen_cmds
 {
+    IPMI_CMD_GET_SENSOR_READING = 0x2D,
     IPMI_CMD_GET_SENSOR_TYPE = 0x2F,
     IPMI_CMD_SET_SENSOR      = 0x30,
 };
diff --git a/testit.C b/testit.C
index bfd9334..a0cd2c1 100644
--- a/testit.C
+++ b/testit.C
@@ -37,25 +37,31 @@
 char g_results_value[64];
 
 
-int set_sensor_dbus_state_v(uint8_t number, const char *method, char *value) {
-    printf("Attempting to log Variant Sensor 0x%02x via %s with a value of %s\n",
-        number, method, value);
+int set_sensor_dbus_state_s(unsigned char number, const char *member, const char *value) {
+    printf("Attempting to log 0x%02x via %s with a value of %s\n",
+        number, member, value);
 
-    strcpy(g_results_method, method);
+    strcpy(g_results_method, member);
     strcpy(g_results_value, value);
 
     return 0;
 }
 
-int set_sensor_dbus_state(uint8_t number, const char *method, const char *value) {
+int set_sensor_dbus_state_y(unsigned char number, char const* member, uint8_t value) {
+    
+    char val[2];
 
-	printf("Attempting to log Sensor 0x%02x via %s with a value of %s\n",
-		number, method, value);
 
-    strcpy(g_results_method, method);
-    strcpy(g_results_value, value);
+    printf("Attempting to log Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
+        number, member, value);
 
-	return 0;
+
+    snprintf(val, 2, "%d", value);
+
+    strcpy(g_results_method, member);
+    strcpy(g_results_value, val);
+
+    return 0;
 }