diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2012-12-15 01:50:58 +0100 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2012-12-19 03:49:11 +0100 |
commit | 2fa46089e724a84f5e2d92ef3716e6935e83fd49 (patch) | |
tree | 59d7bf8e6faa0a270f3496676c744dfcf69bd975 /gdbus | |
parent | 6c2dc5d1598a0f29f044a481f71666f0ee292c91 (diff) | |
download | connman-2fa46089e724a84f5e2d92ef3716e6935e83fd49.tar.gz connman-2fa46089e724a84f5e2d92ef3716e6935e83fd49.tar.bz2 connman-2fa46089e724a84f5e2d92ef3716e6935e83fd49.zip |
gdbus: Add support for D-Bus client method calls
Diffstat (limited to 'gdbus')
-rw-r--r-- | gdbus/client.c | 74 | ||||
-rw-r--r-- | gdbus/gdbus.h | 8 |
2 files changed, 82 insertions, 0 deletions
diff --git a/gdbus/client.c b/gdbus/client.c index e32cefed..750242c4 100644 --- a/gdbus/client.c +++ b/gdbus/client.c @@ -421,6 +421,80 @@ gboolean g_dbus_proxy_set_property_basic(GDBusProxy *proxy, return TRUE; } +struct method_call_data { + GDBusReturnFunction function; + void *user_data; + GDBusDestroyFunction destroy; +}; + +static void method_call_reply(DBusPendingCall *call, void *user_data) +{ + struct method_call_data *data = user_data; + DBusMessage *reply = dbus_pending_call_steal_reply(call); + + if (data->function) + data->function(reply, data->user_data); + + if (data->destroy) + data->destroy(data->user_data); + + dbus_message_unref(reply); +} + +gboolean g_dbus_proxy_method_call(GDBusProxy *proxy, const char *method, + GDBusSetupFunction setup, + GDBusReturnFunction function, void *user_data, + GDBusDestroyFunction destroy) +{ + struct method_call_data *data; + GDBusClient *client; + DBusMessage *msg; + DBusPendingCall *call; + + if (proxy == NULL || method == NULL) + return FALSE; + + client = proxy->client; + if (client == NULL) + return FALSE; + + data = g_try_new0(struct method_call_data, 1); + if (data == NULL) + return FALSE; + + data->function = function; + data->user_data = user_data; + data->destroy = destroy; + + msg = dbus_message_new_method_call(client->service_name, + proxy->obj_path, proxy->interface, method); + if (msg == NULL) { + g_free(data); + return FALSE; + } + + if (setup) { + DBusMessageIter iter; + + dbus_message_iter_init_append(msg, &iter); + setup(&iter, data->user_data); + } + + if (dbus_connection_send_with_reply(client->dbus_conn, msg, + &call, -1) == FALSE) { + dbus_message_unref(msg); + g_free(data); + return FALSE; + } + + dbus_pending_call_set_notify(call, method_call_reply, data, g_free); + dbus_pending_call_unref(call); + + dbus_message_unref(msg); + + return TRUE; +} + static void add_property(GDBusProxy *proxy, const char *name, DBusMessageIter *iter) { diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h index 57c2685a..5f1209f3 100644 --- a/gdbus/gdbus.h +++ b/gdbus/gdbus.h @@ -286,6 +286,14 @@ gboolean g_dbus_proxy_set_property_basic(GDBusProxy *proxy, GDBusResultFunction function, void *user_data, GDBusDestroyFunction destroy); +typedef void (* GDBusSetupFunction) (DBusMessageIter *iter, void *user_data); +typedef void (* GDBusReturnFunction) (DBusMessage *message, void *user_data); + +gboolean g_dbus_proxy_method_call(GDBusProxy *proxy, const char *method, + GDBusSetupFunction setup, + GDBusReturnFunction function, void *user_data, + GDBusDestroyFunction destroy); + typedef struct GDBusClient GDBusClient; GDBusClient *g_dbus_client_new(DBusConnection *connection, |