sensorhandler: Refactor find_openbmc_path to check sensor map first

Change-Id: I846d538643af77705d5160bf4d9127f758b5c749
Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index 69f1911..9355fe3 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -108,11 +108,11 @@
     return r;
 }
 
+int get_bus_for_path(const char *path, char **busname) {
+    return mapper_get_service(bus, path, busname);
+}
 
-// Use a lookup table to find the interface name of a specific sensor
-// This will be used until an alternative is found.  this is the first
-// step for mapping IPMI
-int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
+int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
     char  *busname = NULL;
     const char  *iface = "org.openbmc.managers.System";
     const char  *objname = "/org/openbmc/managers/System";
@@ -122,7 +122,7 @@
 
 
     int r;
-    r = mapper_get_service(bus, objname, &busname);
+    r = get_bus_for_path(objname, &busname);
     if (r < 0) {
         fprintf(stderr, "Failed to get %s busname: %s\n",
                 objname, strerror(-r));
@@ -142,7 +142,7 @@
         goto final;
     }
 
-    r = mapper_get_service(bus, str2, &str1);
+    r = get_bus_for_path(str2, &str1);
     if (r < 0) {
         fprintf(stderr, "Failed to get %s busname: %s\n",
                 str2, strerror(-r));
@@ -165,6 +165,46 @@
     return r;
 }
 
+// Use a lookup table to find the interface name of a specific sensor
+// This will be used until an alternative is found.  this is the first
+// step for mapping IPMI
+int find_openbmc_path(uint8_t num, dbus_interface_t *interface) {
+    int rc;
+
+    // When the sensor map does not contain the sensor requested,
+    // fall back to the legacy DBus lookup (deprecated)
+    const auto& sensor_it = sensors.find(num);
+    if (sensor_it == sensors.end())
+    {
+        return legacy_dbus_openbmc_path("SENSOR", num, interface);
+    }
+
+    const auto& info = sensor_it->second;
+
+    char* busname;
+    rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
+    if (rc < 0) {
+        fprintf(stderr, "Failed to get %s busname: %s\n",
+                info.sensorPath.c_str(),
+                busname);
+        goto final;
+    }
+
+    interface->sensortype = info.sensorType;
+    strcpy(interface->bus, busname);
+    strcpy(interface->path, info.sensorPath.c_str());
+    // Take the interface name from the beginning of the DbusInterfaceMap. This
+    // works for the Value interface but may not suffice for more complex
+    // sensors.
+    // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
+    strcpy(interface->interface, info.sensorInterfaces.begin()->first.c_str());
+    interface->sensornumber = num;
+
+final:
+    free(busname);
+    return rc;
+}
+
 
 /////////////////////////////////////////////////////////////////////
 //
@@ -182,7 +222,7 @@
     fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
         number, method, value);
 
-    r = find_openbmc_path("SENSOR", number, &a);
+    r = find_openbmc_path(number, &a);
 
     if (r < 0) {
         fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
@@ -224,7 +264,7 @@
     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 = find_openbmc_path(number, &a);
 
     if (r < 0) {
         fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
@@ -317,7 +357,7 @@
 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);
+    r = find_openbmc_path(num, &dbus_if);
     if (r < 0) {
         fprintf(stderr, "Could not find sensor %d\n", num);
         return r;
@@ -490,7 +530,7 @@
 
     printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
 
-    r = find_openbmc_path("SENSOR", reqptr->sennum, &a);
+    r = find_openbmc_path(reqptr->sennum, &a);
 
     if (r < 0) {
         fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
diff --git a/sensorhandler.h b/sensorhandler.h
index e908973..d58f640 100644
--- a/sensorhandler.h
+++ b/sensorhandler.h
@@ -23,7 +23,7 @@
 
 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);
-int find_openbmc_path(const char *, const uint8_t , dbus_interface_t *);
+int find_openbmc_path(uint8_t , dbus_interface_t *);
 
 /**
  * @struct SetSensorReadingReq
diff --git a/storageaddsel.cpp b/storageaddsel.cpp
index b8b7a8d..9cebf9e 100644
--- a/storageaddsel.cpp
+++ b/storageaddsel.cpp
@@ -58,7 +58,7 @@
 	const char *p;
 	int r;
 
-	r = find_openbmc_path("SENSOR", sensor_number, &a);
+	r = find_openbmc_path(sensor_number, &a);
 
 	if ((r < 0) || (a.bus[0] == 0)) {
 		// Just make a generic message for errors that
@@ -140,7 +140,7 @@
 
 	sensor = p->sensornumber;
 
-	find_openbmc_path("SENSOR", sensor, &dbusint);
+	find_openbmc_path(sensor, &dbusint);
 
 	// Simply no associations if the sensor can not be found
 	if (strlen(dbusint.path) < 1) {
diff --git a/testaddsel.cpp b/testaddsel.cpp
index 4023629..90f63a2 100644
--- a/testaddsel.cpp
+++ b/testaddsel.cpp
@@ -15,7 +15,7 @@
 // Use a lookup table to find the interface name of a specific sensor
 // This will be used until an alternative is found.  this is the first
 // step for mapping IPMI
-int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
+int find_openbmc_path(const uint8_t num, dbus_interface_t *interface) {
 
     const char  *objname = "/org/openbmc/managers/System";