[PATCH phosphor-host-ipmid v2 5/5] Get service name via object mapper

OpenBMC Patches openbmc-patches at stwcx.xyz
Wed Dec 16 09:20:57 AEDT 2015


From: shgoupf <shgoupf at cn.ibm.com>

---
 chassishandler.C | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 3 deletions(-)

diff --git a/chassishandler.C b/chassishandler.C
index 8aba43b..35b5086 100644
--- a/chassishandler.C
+++ b/chassishandler.C
@@ -10,10 +10,15 @@ const char  *chassis_object_name   =  "/org/openbmc/control/chassis0";
 const char  *chassis_intf_name     =  "org.openbmc.control.Chassis";
 
 // Host settings in dbus
-const char  *settings_service_name =  "org.openbmc.settings.Host";
+// Service name should be referenced by connection name got via object mapper
+// const char  *settings_service_name =  "org.openbmc.settings.Host";
 const char  *settings_object_name  =  "/org/openbmc/settings/host0";
 const char  *settings_intf_name    =  "org.freedesktop.DBus.Properties";
 
+const char  *objmapper_service_name =  "org.openbmc.objectmapper";
+const char  *objmapper_object_name  =  "/org/openbmc/objectmapper/objectmapper";
+const char  *objmapper_intf_name    =  "org.openbmc.objectmapper.ObjectMapper";
+
 char* uint8_to_char(uint8_t *a, size_t size)
 {
     char* buffer;
@@ -52,6 +57,57 @@ uint8_t* char_to_uint8(char *a, size_t size)
     return buffer;
 }
 
+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;
+    size_t buf_size = 0;
+    int r;
+
+    // Open the system bus where most system services are provided. 
+    r = sd_bus_open_system(&bus);
+    if (r < 0) {
+        fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
+        goto finish;
+    }
+
+    // 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
+    r = sd_bus_message_read(m, "a{sas}", 1, &temp_buf);
+    buf_size = strlen(temp_buf) + 1;
+    printf("IPMID connection name: %s\n", temp_buf);
+    *buf = (char*)malloc(buf_size);
+    memcpy(*buf, temp_buf, buf_size);
+
+finish:
+    sd_bus_error_free(&error);
+    sd_bus_message_unref(m);
+    sd_bus_unref(bus);
+
+    return r;
+}
+
 // TODO: object mapper should be used instead of hard-coding.
 int dbus_get_property(char* buf)
 {
@@ -60,6 +116,7 @@ int dbus_get_property(char* buf)
     sd_bus *bus = NULL;
     char* temp_buf = NULL;
     uint8_t* get_value = NULL;
+    char* connection = NULL;
     int r, i;
 
     // Open the system bus where most system services are provided. 
@@ -69,12 +126,14 @@ int dbus_get_property(char* buf)
         goto finish;
     }
 
+    object_mapper_get_connection(&connection, settings_object_name);
+    printf("connection: %s\n", 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,
-            settings_service_name,                      /* service to contact */
+            connection,                                 /* service to contact */
             settings_object_name,                       /* object path */
             settings_intf_name,                         /* interface name */
             "Get",                                      /* method name */
@@ -83,6 +142,7 @@ int dbus_get_property(char* buf)
             "ss",                                       /* input signature */
             settings_intf_name,                         /* first argument */
             "boot_flags");                              /* second argument */
+
     if (r < 0) {
         fprintf(stderr, "Failed to issue method call: %s\n", error.message);
         goto finish;
@@ -104,6 +164,7 @@ finish:
     sd_bus_error_free(&error);
     sd_bus_message_unref(m);
     sd_bus_unref(bus);
+    free(connection);
 
     return r;
 }
@@ -114,6 +175,7 @@ int dbus_set_property(const char* buf)
     sd_bus_error error = SD_BUS_ERROR_NULL;
     sd_bus_message *m = NULL;
     sd_bus *bus = NULL;
+    char* connection = NULL;
     int r;
 
     // Open the system bus where most system services are provided. 
@@ -123,12 +185,14 @@ int dbus_set_property(const char* buf)
         goto finish;
     }
 
+    object_mapper_get_connection(&connection, settings_object_name);
+    printf("connection: %s\n", 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,
-            settings_service_name,                      /* service to contact */
+            connection,                                 /* service to contact */
             settings_object_name,                       /* object path */
             settings_intf_name,                         /* interface name */
             "Set",                                      /* method name */
@@ -151,6 +215,7 @@ finish:
     sd_bus_error_free(&error);
     sd_bus_message_unref(m);
     sd_bus_unref(bus);
+    free(connection);
 
     return r;
 }
-- 
2.6.3




More information about the openbmc mailing list