Use mapper calls to lookup objects

This is a reaction to a managers.System API update.  The
update removes the bus name field from the 'GetObject'
method responses.

Change-Id: I51fb1d1bad20317a429e5b06712475929b6fb4fb
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/Makefile b/Makefile
index 7a3b6af..08900ca 100644
--- a/Makefile
+++ b/Makefile
@@ -50,13 +50,13 @@
 	$(CXX) -std=c++14 -fpic -c $< $(CXXFLAGS) $(INC_FLAG) $(IPMID_PATH) -o $@
 
 $(LIB_APP): $(LIB_APP_OBJ)
-	$(CXX) $^ -shared $(LDFLAGS) $(LIB_FLAG) -o $@
+	$(CXX) $^ -shared $(LDFLAGS) $(LIB_FLAG) -o $@ -lmapper
 
 $(LIB_HOST_SRV): $(LIB_HOST_SRV_OBJ)
 	$(CXX) $^ -shared $(LDFLAGS) $(LIB_FLAG) -o $@
 
 $(DAEMON): $(DAEMON_OBJ)
-	$(CXX) $^ $(LDFLAGS) $(LIB_FLAG) -o $@ -ldl
+	$(CXX) $^ $(LDFLAGS) $(LIB_FLAG) -o $@ -lmapper -ldl
 
 $(TESTER): $(TESTER_OBJ)
 	$(CXX) $^ $(LDFLAGS) $(LIB_FLAG) -o $@ -ldl
diff --git a/chassishandler.C b/chassishandler.C
index 6a9a28f..ded9157 100644
--- a/chassishandler.C
+++ b/chassishandler.C
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
+#include <mapper.h>
 
 
 //Defines
@@ -27,80 +28,6 @@
 const char *settings_intf_name    =  "org.freedesktop.DBus.Properties";
 const char *host_intf_name        =  "org.openbmc.settings.Host";
 
-const char *objmapper_service_name =  "org.openbmc.ObjectMapper";
-const char *objmapper_object_name  =  "/org/openbmc/ObjectMapper";
-const char *objmapper_intf_name    =  "org.openbmc.ObjectMapper";
-
-int object_mapper_get_connection(char **buf, const char *obj_path)
-{
-    sd_bus_error error = SD_BUS_ERROR_NULL;
-    sd_bus_message *m = NULL;
-    sd_bus *bus = NULL;
-    char *temp_buf = NULL, *intf = NULL;
-    size_t buf_size = 0;
-    int r;
-
-    // Get the system bus where most system services are provided.
-    bus = ipmid_get_sd_bus_connection();
-
-    /*
-     * Bus, service, object path, interface and method are provided to call
-     * the method.
-     * Signatures and input arguments are provided by the arguments at the
-     * end.
-     */
-    r = sd_bus_call_method(bus,
-                           objmapper_service_name,                      /* service to contact */
-                           objmapper_object_name,                       /* object path */
-                           objmapper_intf_name,                         /* interface name */
-                           "GetObject",                                 /* method name */
-                           &error,                                      /* object to return error in */
-                           &m,                                          /* return message on success */
-                           "s",                                         /* input signature */
-                           obj_path                                     /* first argument */
-                          );
-
-    if (r < 0) {
-        fprintf(stderr, "Failed to issue method call: %s\n", error.message);
-        goto finish;
-    }
-
-    // Get the key, aka, the connection name
-    sd_bus_message_read(m, "a{sas}", 1, &temp_buf, 1, &intf);
-
-    /*
-     * TODO: check the return code. Currently for no reason the message
-     * parsing of object mapper is always complaining about
-     * "Device or resource busy", but the result seems OK for now. Need
-     * further checks.
-     * TODO: The following code is preserved in the comments so that it can be
-     * resumed after the problem aforementioned is resolved.
-     *r = sd_bus_message_read(m, "a{sas}", 1, &temp_buf, 1, &intf);
-     *if (r < 0) {
-     *    fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r));
-     *    goto finish;
-     *}
-     */
-
-    buf_size = strlen(temp_buf) + 1;
-    printf("IPMID connection name: %s\n", temp_buf);
-    *buf = (char *)malloc(buf_size);
-
-    if (*buf == NULL) {
-        fprintf(stderr, "Malloc failed for get_sys_boot_options");
-        r = -1;
-        goto finish;
-    }
-
-    memcpy(*buf, temp_buf, buf_size);
-
-finish:
-    sd_bus_error_free(&error);
-    sd_bus_message_unref(m);
-
-    return r;
-}
-
 int dbus_get_property(const char *name, char **buf)
 {
     sd_bus_error error = SD_BUS_ERROR_NULL;
@@ -110,18 +37,15 @@
     char *connection = NULL;
     int r;
 
-    r = object_mapper_get_connection(&connection, settings_object_name);
+    // Get the system bus where most system services are provided.
+    bus = ipmid_get_sd_bus_connection();
 
+    r = mapper_get_service(bus, settings_object_name, &connection);
     if (r < 0) {
         fprintf(stderr, "Failed to get connection, return value: %d.\n", r);
         goto finish;
     }
 
-    printf("connection: %s\n", connection);
-
-    // Get the system bus where most system services are provided.
-    bus = ipmid_get_sd_bus_connection();
-
     /*
      * Bus, service, object path, interface and method are provided to call
      * the method.
@@ -178,18 +102,15 @@
     char *connection = NULL;
     int r;
 
-    r = object_mapper_get_connection(&connection, settings_object_name);
+    // Get the system bus where most system services are provided.
+    bus = ipmid_get_sd_bus_connection();
 
+    r = mapper_get_service(bus, settings_object_name, &connection);
     if (r < 0) {
         fprintf(stderr, "Failed to get connection, return value: %d.\n", r);
         goto finish;
     }
 
-    printf("connection: %s\n", connection);
-
-    // Get the system bus where most system services are provided.
-    bus = ipmid_get_sd_bus_connection();
-
     /*
      * Bus, service, object path, interface and method are provided to call
      * the method.
diff --git a/ipmid.C b/ipmid.C
index 960f5dc..9590448 100644
--- a/ipmid.C
+++ b/ipmid.C
@@ -11,6 +11,7 @@
 #include "ipmid.H"
 #include <sys/time.h>
 #include <errno.h>
+#include <mapper.h>
 #include "sensorhandler.h"
 
 sd_bus *bus = NULL;
@@ -525,30 +526,40 @@
 // 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) {
-
-    const char  *busname = "org.openbmc.managers.System";
+    char  *busname = NULL;
+    const char  *iface = "org.openbmc.managers.System";
     const char  *objname = "/org/openbmc/managers/System";
-
-    char  *str1, *str2, *str3;
+    char  *str1 = NULL, *str2, *str3;
     sd_bus_error error = SD_BUS_ERROR_NULL;
     sd_bus_message *reply = NULL;
 
 
     int r;
+    r = mapper_get_service(bus, objname, &busname);
+    if (r < 0) {
+        fprintf(stderr, "Failed to get system manager busname: %s\n", strerror(-r));
+        goto final;
+    }
 
-    r = sd_bus_call_method(bus,busname,objname,busname, "getObjectFromByteId",
+    r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
                            &error, &reply, "sy", type, num);
     if (r < 0) {
         fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
         goto final;
     }
 
-    r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3);
+    r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
     if (r < 0) {
         fprintf(stderr, "Failed to get a response: %s", strerror(-r));
         goto final;
     }
 
+    r = mapper_get_service(bus, str2, &str1);
+    if (r < 0) {
+        fprintf(stderr, "Failed to get item busname: %s\n", strerror(-r));
+        goto final;
+    }
+
     strncpy(interface->bus, str1, MAX_DBUS_PATH);
     strncpy(interface->path, str2, MAX_DBUS_PATH);
     strncpy(interface->interface, str3, MAX_DBUS_PATH);
@@ -559,6 +570,8 @@
 
     sd_bus_error_free(&error);
     reply = sd_bus_message_unref(reply);
+    free(busname);
+    free(str1);
 
     return r;
 }