capi-system-device: Change dbus-glib to gdbus 61/39761/1
authorJiyoung Yun <jy910.yun@samsung.com>
Fri, 22 May 2015 04:48:22 +0000 (13:48 +0900)
committerJiyoung Yun <jy910.yun@samsung.com>
Fri, 22 May 2015 04:48:22 +0000 (13:48 +0900)
Dbus-glib is a deprecated API for use of D-Bus from GLib applications.
Since version 2.26, Glib provides a high-level API for D-Bus, "GDBus".
So the related code with dbus-glib is changed to gdbus logic.

http://dbus.freedesktop.org/doc/dbus-glib/

Change-Id: If92a0139fc9ed04d5810ce43bd4c1efd55eafd02
Signed-off-by: Jiyoung Yun <jy910.yun@samsung.com>
CMakeLists.txt
packaging/capi-system-device.spec
src/dbus.c
src/dbus.h
src/power.c

index d6c80d6eb5a2befa88a91cfe43f4dd6a8027975a..ce868b8edfe8a9cdcc3a7e7eb3beacc4552e5d3a 100755 (executable)
@@ -11,7 +11,7 @@ SET(INC_DIR include)
 INCLUDE_DIRECTORIES(${INC_DIR})
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(${fw_name} REQUIRED dlog vconf dbus-1 dbus-glib-1 capi-base-common capi-system-info gio-2.0)
+pkg_check_modules(${fw_name} REQUIRED dlog vconf capi-base-common capi-system-info gio-2.0)
 FOREACH(flag ${${fw_name}_CFLAGS})
     SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
index 55dc25581178d09858c5f4ca57389a49cd7d3097..f063ff2d05564b0e83399ceb6eb90f9aeaf23e72 100644 (file)
@@ -11,8 +11,6 @@ BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(vconf)
-BuildRequires:  pkgconfig(dbus-1)
-BuildRequires:  pkgconfig(dbus-glib-1)
 BuildRequires:  pkgconfig(gio-2.0)
 
 %description
index 531a83455ac784cc15b951662ef8267a7edafffa..f5fda674db69670caf268fea0f736e9cbe306292 100644 (file)
@@ -21,7 +21,6 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <errno.h>
-#include <dbus/dbus-glib-lowlevel.h>
 
 #include "common.h"
 #include "dbus.h"
@@ -33,220 +32,201 @@ struct pending_call_data {
        void *data;
 };
 
-static int convert_error_to_errno(const char *err)
+static int g_dbus_error_to_errno(int code)
 {
        /**
         * if device is not supported,
         * deviced does not register the method call of the device.
         * in this case, dbus will return UNKNOWN_METHOD error.
         */
-       if (strncmp(err, DBUS_ERROR_UNKNOWN_METHOD,
-                               sizeof(DBUS_ERROR_UNKNOWN_METHOD)) == 0)
-               return -ENOTSUP;
-       else if (strncmp(err, DBUS_ERROR_ACCESS_DENIED,
-                               sizeof(DBUS_ERROR_ACCESS_DENIED)) == 0)
+       /* refer to gio/gioenums.h */
+       if (code == G_DBUS_ERROR_ACCESS_DENIED)
                return -EACCES;
+       else if (code == G_DBUS_ERROR_UNKNOWN_METHOD)
+               return -ENOTSUP;
        return -ECOMM;
 }
 
-static int append_variant(DBusMessageIter *iter, const char *sig, char *param[])
+static GVariant *append_g_variant(const char *sig, char *param[])
 {
+       GVariantBuilder builder;
        char *ch;
        int i;
-       int int_type;
-       uint64_t int64_type;
 
        if (!sig || !param)
-               return 0;
+               return NULL;
+
+       g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
 
        for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
                switch (*ch) {
                case 'i':
-                       int_type = atoi(param[i]);
-                       dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
+                       g_variant_builder_add(&builder, "i", atoi(param[i]));
                        break;
                case 'u':
-                       int_type = strtoul(param[i], NULL, 10);
-                       dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
+                       g_variant_builder_add(&builder, "u", strtoul(param[i], NULL, 10));
                        break;
                case 't':
-                       int64_type = atoll(param[i]);
-                       dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
+                       g_variant_builder_add(&builder, "t", atoll(param[i]));
                        break;
                case 's':
-                       dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &param[i]);
+                       g_variant_builder_add(&builder, "s", param[i]);
                        break;
                default:
-                       return -EINVAL;
+                       return NULL;
                }
        }
 
-       return 0;
+       return g_variant_builder_end(&builder);
 }
 
 int dbus_method_sync(const char *dest, const char *path,
                const char *interface, const char *method,
                const char *sig, char *param[])
 {
-       DBusConnection *conn;
-       DBusMessage *msg;
-       DBusMessageIter iter;
-       DBusMessage *reply;
-       DBusError err;
-       int ret, result;
-
-       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       GDBusConnection *conn;
+       GDBusProxy *proxy;
+       GError *err = NULL;
+       GVariant *output;
+       int result;
+
+#if !GLIB_CHECK_VERSION(2,35,0)
+       g_type_init();
+#endif
+
+       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
        if (!conn) {
-               _E("dbus_bus_get error");
-               return -EPERM;
+               _E("g_bus_get_sync error : %s-%s (%d-%s)",
+                               interface, method, err->code, err->message);
+               result = g_dbus_error_to_errno(err->code);
+               g_clear_error(&err);
+               return result;
        }
 
-       msg = dbus_message_new_method_call(dest, path, interface, method);
-       if (!msg) {
-               _E("dbus_message_new_method_call(%s:%s-%s)",
-                       path, interface, method);
-               return -EBADMSG;
+       proxy = g_dbus_proxy_new_sync(conn,
+                       G_DBUS_PROXY_FLAGS_NONE,
+                       NULL,      /* GDBusinterfaceinfo */
+                       dest,      /* bus name */
+                       path,      /* object path */
+                       interface, /* interface name */
+                       NULL,      /* GCancellable */
+                       &err);
+       if (!proxy) {
+               _E("g_dbus_proxy_new_sync error : %s-%s (%d-%s)",
+                               interface, method, err->code, err->message);
+               result = g_dbus_error_to_errno(err->code);
+               g_clear_error(&err);
+               return result;
        }
 
-       dbus_message_iter_init_append(msg, &iter);
-       ret = append_variant(&iter, sig, param);
-       if (ret < 0) {
-               _E("append_variant error(%d) %s %s:%s-%s",
-                       ret, dest, path, interface, method);
-               dbus_message_unref(msg);
-               return ret;
+       output = g_dbus_proxy_call_sync(proxy,
+                       method,                       /* method name */
+                       append_g_variant(sig, param), /* parameters */
+                       G_DBUS_CALL_FLAGS_NONE,
+                       DBUS_REPLY_TIMEOUT,           /* timeout */
+                       NULL,                         /* GCancellable */
+                       &err);
+       if (!output) {
+               _E("g_dbus_proxy_call_sync error : %s-%s (%d-%s)",
+                               interface, method, err->code, err->message);
+               result = g_dbus_error_to_errno(err->code);
+               g_clear_error(&err);
+               g_object_unref(proxy);
+               return result;
        }
 
-       dbus_error_init(&err);
-
-       reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
-       dbus_message_unref(msg);
-       if (!reply) {
-               _E("dbus_connection_send error(%s:%s) %s %s:%s-%s",
-                       err.name, err.message, dest, path, interface, method);
-               ret = convert_error_to_errno(err.name);
-               dbus_error_free(&err);
-               return ret;
-       }
+       /* get output value */
+       g_variant_get(output, "(i)", &result);
 
-       ret = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
-       dbus_message_unref(reply);
-       if (!ret) {
-               _E("no message : [%s:%s] %s %s:%s-%s",
-                       err.name, err.message, dest, path, interface, method);
-               dbus_error_free(&err);
-               return -ENOMSG;
-       }
+       g_variant_unref(output);
+       g_object_unref(proxy);
 
        return result;
 }
 
-static void cb_pending(DBusPendingCall *pending, void *user_data)
+static void cb_pending(GDBusProxy *proxy,
+               GAsyncResult *res,
+               gpointer user_data)
 {
-       DBusMessage *msg;
-       DBusError err;
        struct pending_call_data *data = user_data;
-       int ret;
-
-       ret = dbus_pending_call_get_completed(pending);
-       if (!ret) {
-               _I("dbus_pending_call_get_completed() fail");
-               dbus_pending_call_unref(pending);
-               return;
-       }
-
-       dbus_error_init(&err);
-       msg = dbus_pending_call_steal_reply(pending);
-       if (!msg) {
-               _E("no message : [%s:%s]", err.name, err.message);
-
-               if (data->func) {
-                       dbus_set_error(&err, "org.tizen.system.deviced.NoReply",
-                                       "There was no reply to this method call");
-                       data->func(data->data, NULL, &err);
-                       dbus_error_free(&err);
-               }
-               return;
-       }
-
-       ret = dbus_set_error_from_message(&err, msg);
-       if (ret) {
-               _E("error msg : [%s:%s]", err.name, err.message);
-
-               if (data->func)
-                       data->func(data->data, NULL, &err);
-               dbus_error_free(&err);
-       } else {
-               if (data->func)
-                       data->func(data->data, msg, &err);
-       }
-
-       dbus_message_unref(msg);
-       dbus_pending_call_unref(pending);
+       GError *err = NULL;
+       GVariant *output;
+
+       output = g_dbus_proxy_call_finish(proxy,
+                       res, /* GAsyncResult */
+                       &err);
+       if (!output)
+               _E("g_dbus_proxy_call_finish error : %d-%s",
+                               err->code, err->message);
+
+       if (data && data->func)
+               data->func(data->data, output, err);
+
+       if (err)
+               g_clear_error(&err);
+       if (output)
+               g_variant_unref(output);
+       g_object_unref(proxy);
 }
 
 int dbus_method_async_with_reply(const char *dest, const char *path,
                const char *interface, const char *method,
-               const char *sig, char *param[], dbus_pending_cb cb, int timeout, void *data)
+               const char *sig, char *param[],
+               dbus_pending_cb cb, int timeout, void *data)
 {
-       DBusConnection *conn;
-       DBusMessage *msg;
-       DBusMessageIter iter;
-       DBusPendingCall *pending = NULL;
+       GDBusConnection *conn;
+       GDBusProxy *proxy;
+       GError *err = NULL;
        struct pending_call_data *pdata;
-       int ret;
-
-       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
-       if (!conn) {
-               _E("dbus_bus_get error");
-               return -EPERM;
-       }
+       int result;
 
-       /* this function should be invoked to receive dbus messages
-        * does nothing if it's already been done */
-       dbus_connection_setup_with_g_main(conn, NULL);
+#if !GLIB_CHECK_VERSION(2,35,0)
+       g_type_init();
+#endif
 
-       msg = dbus_message_new_method_call(dest, path, interface, method);
-       if (!msg) {
-               _E("dbus_message_new_method_call(%s:%s-%s)",
-                       path, interface, method);
-               return -EBADMSG;
+       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+       if (!conn) {
+               _E("g_bus_get_sync error : %s-%s (%d-%s)",
+                               interface, method, err->code, err->message);
+               result = g_dbus_error_to_errno(err->code);
+               g_clear_error(&err);
+               return result;
        }
 
-       dbus_message_iter_init_append(msg, &iter);
-       ret = append_variant(&iter, sig, param);
-       if (ret < 0) {
-               _E("append_variant error(%d)%s %s:%s-%s",
-                       ret, dest, path, interface, method);
-               dbus_message_unref(msg);
-               return ret;
+       proxy = g_dbus_proxy_new_sync(conn,
+                       G_DBUS_PROXY_FLAGS_NONE,
+                       NULL,      /* GDBusinterfaceinfo */
+                       dest,      /* bus name */
+                       path,      /* object path */
+                       interface, /* interface name */
+                       NULL,      /* GCancellable */
+                       &err);
+       if (!proxy) {
+               _E("g_dbus_proxy_new_sync error : %s-%s (%d-%s)",
+                               interface, method, err->code, err->message);
+               result = g_dbus_error_to_errno(err->code);
+               g_clear_error(&err);
+               return result;
        }
 
-       ret = dbus_connection_send_with_reply(conn, msg, &pending, timeout);
-       if (!ret) {
-               dbus_message_unref(msg);
-               _E("dbus_connection_send error(%s %s:%s-%s)",
-                       dest, path, interface, method);
-               return -ECOMM;
+       pdata = malloc(sizeof(struct pending_call_data));
+       if (!pdata) {
+               _E("malloc error : %s-%s",
+                               interface, method);
+               return -ENOMEM;
        }
 
-       dbus_message_unref(msg);
-
-       if (cb && pending) {
-               pdata = malloc(sizeof(struct pending_call_data));
-               if (!pdata)
-                       return -ENOMEM;
-
-               pdata->func = cb;
-               pdata->data = data;
-
-               ret = dbus_pending_call_set_notify(pending, cb_pending, pdata, free);
-               if (!ret) {
-                       free(pdata);
-                       dbus_pending_call_cancel(pending);
-                       return -ECOMM;
-               }
-       }
+       pdata->func = cb;
+       pdata->data = data;
+
+       g_dbus_proxy_call(proxy,
+                       method,                          /* method name */
+                       append_g_variant(sig, param),    /* parameters */
+                       G_DBUS_CALL_FLAGS_NONE,
+                       DBUS_REPLY_TIMEOUT,              /* timeout */
+                       NULL,                            /* GCancellable */
+                       (GAsyncReadyCallback)cb_pending, /* GAsyncReadyCallback */
+                       pdata);                          /* user data */
 
        return 0;
 }
index 0beac19464a74e99b9352bc55d82e12eb0c5915c..eb4da1f2d34e2557be1d182a59b9e0281b349c3a 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef __DBUS_H__
 #define __DBUS_H__
 
-#include <dbus/dbus.h>
+#include <gio/gio.h>
 
 #define DEVICED_BUS_NAME               "org.tizen.system.deviced"
 #define DEVICED_OBJECT_PATH            "/Org/Tizen/System/DeviceD"
@@ -50,7 +50,11 @@ int dbus_method_sync(const char *dest, const char *path,
                const char *interface, const char *method,
                const char *sig, char *param[]);
 
-typedef void (*dbus_pending_cb)(void *data, DBusMessage *msg, DBusError *err);
+/**
+ * If result is NULL, err is set.
+ * Do not invoke g_variant_unref() with result.
+ */
+typedef void (*dbus_pending_cb)(void *data, GVariant *result, GError *err);
 
 int dbus_method_async_with_reply(const char *dest, const char *path,
                const char *interface, const char *method,
index b7a0f37b8d7a383b6e233822b0934335e8998cc6..9fcd05f582110a785874aa712d03e92984ad4ac9 100644 (file)
@@ -75,23 +75,17 @@ static char *get_state_str(display_state_e state)
        return NULL;
 }
 
-static void lock_cb(void *data, DBusMessage *msg, DBusError *unused)
+static void lock_cb(void *data, GVariant *result, GError *err)
 {
-       DBusError err;
-       int ret, val;
-
-       if (!msg)
-               return;
+       int ret;
 
-       dbus_error_init(&err);
-       ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
-       if (!ret) {
-               _E("no message [%s:%s]", err.name, err.message);
-               dbus_error_free(&err);
+       if (!result) {
+               _E("no message : %s", err->message);
                return;
        }
 
-       _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_LOCK_STATE, val);
+       g_variant_get(result, "(i)", &ret);
+       _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_LOCK_STATE, ret);
 }
 
 static int lock_state(display_state_e state, unsigned int flag, int timeout_ms)
@@ -123,23 +117,17 @@ static int lock_state(display_state_e state, unsigned int flag, int timeout_ms)
                        METHOD_LOCK_STATE, "sssi", arr, lock_cb, -1, NULL);
 }
 
-static void unlock_cb(void *data, DBusMessage *msg, DBusError *unused)
+static void unlock_cb(void *data, GVariant *result, GError *err)
 {
-       DBusError err;
-       int ret, val;
-
-       if (!msg)
-               return;
+       int ret;
 
-       dbus_error_init(&err);
-       ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
-       if (!ret) {
-               _E("no message [%s:%s]", err.name, err.message);
-               dbus_error_free(&err);
+       if (!result) {
+               _E("no message : %s", err->message);
                return;
        }
 
-       _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_UNLOCK_STATE, val);
+       g_variant_get(result, "(i)", &ret);
+       _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_UNLOCK_STATE, ret);
 }
 
 static int unlock_state(display_state_e state, unsigned int flag)
@@ -180,12 +168,7 @@ int device_power_request_lock(power_lock_e type, int timeout_ms)
        else
                return DEVICE_ERROR_INVALID_PARAMETER;
 
-       if (ret == -ECOMM)
-               return DEVICE_ERROR_PERMISSION_DENIED;
-       else if (ret < 0)
-               return DEVICE_ERROR_OPERATION_FAILED;
-
-       return DEVICE_ERROR_NONE;
+       return errno_to_device_error(ret);
 }
 
 int device_power_release_lock(power_lock_e type)
@@ -201,12 +184,7 @@ int device_power_release_lock(power_lock_e type)
        else
                return DEVICE_ERROR_INVALID_PARAMETER;
 
-       if (ret == -ECOMM)
-               return DEVICE_ERROR_PERMISSION_DENIED;
-       else if (ret < 0)
-               return DEVICE_ERROR_OPERATION_FAILED;
-
-       return DEVICE_ERROR_NONE;
+       return errno_to_device_error(ret);
 }
 
 int device_power_wakeup(bool dim)