sensorhandler: Refactor find_sensor to eliminate redundant call

find_openbmc_path was being called within find_sensor as well as
following find_sensor

Change-Id: Ie94eab7afbe34f214a07bda49bfbce729b20f237
Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
diff --git a/ipmisensor.cpp b/ipmisensor.cpp
index f39832f..10a0ec6 100644
--- a/ipmisensor.cpp
+++ b/ipmisensor.cpp
@@ -4,7 +4,7 @@
 #include <malloc.h>
 #include "sensorhandler.h"
 
-extern uint8_t find_sensor(uint8_t);
+extern uint8_t find_type_for_sensor_number(uint8_t);
 
 
 struct sensorRES_t {
@@ -261,7 +261,7 @@
 	uint8_t stype;
 	int index, i=0;
 
-	stype = find_sensor(pRec->sensor_number);
+	stype = find_type_for_sensor_number(pRec->sensor_number);
 
 	// 0xC3 types use the assertion7_0 for the value to be set
 	// so skip the reseach and call the correct event reporting
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index 892511a..69f1911 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -290,36 +290,41 @@
     }
 }
 
+uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
 
-uint8_t find_sensor(uint8_t sensor_number) {
-
-    dbus_interface_t a;
     char *p;
-    int r;
     uint8_t type;
 
-    r = find_openbmc_path("SENSOR", sensor_number, &a);
-
-    if (r < 0) { return 0; }
-
     // This is where sensors that do not exist in dbus but do
     // exist in the host code stop.  This should indicate it
     // is not a supported sensor
-    if (a.interface[0] == 0) { return 0;}
+    if (dbus_if.interface[0] == 0) { return 0;}
 
-    if (strstr(a.interface, "InventoryItem")) {
+    if (strstr(dbus_if.interface, "InventoryItem")) {
         // InventoryItems are real frus.  So need to get the
         // fru_type property
-        type = dbus_to_sensor_type_from_dbus(&a);
+        type = dbus_to_sensor_type_from_dbus(&dbus_if);
     } else {
         // Non InventoryItems
-        p = strrchr (a.path, '/');
+        p = strrchr (dbus_if.path, '/');
         type = dbus_to_sensor_type(p+1);
     }
 
     return type;
  }
 
+// Replaces find_sensor
+uint8_t find_type_for_sensor_number(uint8_t num) {
+    int r;
+    dbus_interface_t dbus_if;
+    r = find_openbmc_path("SENSOR", num, &dbus_if);
+    if (r < 0) {
+        fprintf(stderr, "Could not find sensor %d\n", num);
+        return r;
+    }
+    return get_type_from_interface(dbus_if);
+}
+
 
 
 
@@ -337,7 +342,7 @@
     // need to ask Hostboot team
     unsigned char buf[] = {0x00,0x6F};
 
-    buf[0] = find_sensor(reqptr->sennum);
+    buf[0] = find_type_for_sensor_number(reqptr->sennum);
 
     // HACK UNTIL Dbus gets updated or we find a better way
     if (buf[0] == 0) {
@@ -492,7 +497,7 @@
         return IPMI_CC_SENSOR_INVALID;
     }
 
-    type = find_sensor(reqptr->sennum);
+    type = get_type_from_interface(a);
     if(type == 0) {
         fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
         return IPMI_CC_SENSOR_INVALID;
diff --git a/testit.cpp b/testit.cpp
index a0cd2c1..072b5d7 100644
--- a/testit.cpp
+++ b/testit.cpp
@@ -12,7 +12,7 @@
     {0xFF ,0xff}
 };
 
-uint8_t find_sensor(uint8_t sensor_number) {
+uint8_t find_type_for_sensor_number(uint8_t sensor_number) {
 
     int i=0;
     uint8_t rc; 
@@ -117,4 +117,4 @@
     testprep(); check_results(updateSensorRecordFromSSRAESC(testrec_invalidnumber), "", "");
 
 	return 0;
-}
\ No newline at end of file
+}