#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
-#include <dbus/dbus-glib-lowlevel.h>
#include "common.h"
#include "dbus.h"
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, ¶m[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;
}
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)
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)
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)
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)