From e7d668ac9e813bc9922ee7d771848bd8822d5d1f Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 8 May 2008 20:23:45 +0000 Subject: Move D-Bus watch functions into libgdbus --- audio/Makefile.am | 4 +- audio/avdtp.c | 6 +- audio/control.c | 7 +- audio/device.c | 7 +- audio/headset.c | 2 - audio/main.c | 3 +- audio/sink.c | 5 +- common/Makefile.am | 6 +- common/dbus-helper.h | 14 +- common/dbus.c | 391 --------------------------------------------------- common/dbus.h | 49 ------- daemon/database.c | 3 +- daemon/main.c | 1 - gdbus/Makefile.am | 2 +- gdbus/gdbus.h | 9 ++ gdbus/watch.c | 380 +++++++++++++++++++++++++++++++++++++++++++++++++ hcid/adapter.c | 3 +- hcid/agent.c | 3 +- hcid/dbus-common.c | 1 - hcid/dbus-database.c | 3 +- hcid/dbus-error.c | 2 +- hcid/dbus-hci.c | 3 +- hcid/dbus-sdp.c | 3 +- hcid/dbus-security.c | 4 +- hcid/dbus-service.c | 1 - hcid/dbus-test.c | 3 +- hcid/manager.c | 3 +- hcid/plugin.c | 2 - input/Makefile.am | 4 +- input/device.c | 2 - input/main.c | 3 +- input/manager.c | 1 - input/server.c | 3 +- network/Makefile.am | 4 +- network/connection.c | 1 - network/main.c | 3 +- network/manager.c | 1 - network/server.c | 1 - plugins/Makefile.am | 2 +- plugins/echo.c | 2 +- plugins/storage.c | 1 - serial/Makefile.am | 4 +- serial/main.c | 3 +- serial/manager.c | 7 +- serial/port.c | 7 +- 45 files changed, 463 insertions(+), 506 deletions(-) delete mode 100644 common/dbus.c delete mode 100644 common/dbus.h create mode 100644 gdbus/watch.c diff --git a/audio/Makefile.am b/audio/Makefile.am index 841617ea..21d48860 100644 --- a/audio/Makefile.am +++ b/audio/Makefile.am @@ -14,7 +14,7 @@ libaudio_la_SOURCES = main.c \ libaudio_la_LDFLAGS = -module -avoid-version -export-symbols-regex bluetooth_plugin_desc LDADD = $(top_builddir)/common/libhelper.a \ - @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ + @GDBUS_LIBS@ @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ if ALSA alsadir = $(libdir)/alsa-lib @@ -52,7 +52,7 @@ libgstbluetooth_la_CFLAGS = @GSTREAMER_CFLAGS@ @SBC_CFLAGS@ endif endif -AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ +AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ @GDBUS_CFLAGS@ INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/hcid -I$(top_srcdir)/sdpd diff --git a/audio/avdtp.c b/audio/avdtp.c index 3c35bfc1..300898e2 100644 --- a/audio/avdtp.c +++ b/audio/avdtp.c @@ -34,12 +34,12 @@ #include #include -#include - #include #include -#include "dbus.h" +#include +#include + #include "dbus-service.h" #include "logging.h" diff --git a/audio/control.c b/audio/control.c index a6976d05..cffcf517 100644 --- a/audio/control.c +++ b/audio/control.c @@ -37,15 +37,14 @@ #include #include -#include -#include - #include #include #include #include -#include "dbus.h" +#include +#include + #include "dbus-helper.h" #include "dbus-service.h" #include "logging.h" diff --git a/audio/device.c b/audio/device.c index 0debe220..c385c7ca 100644 --- a/audio/device.c +++ b/audio/device.c @@ -32,16 +32,15 @@ #include #include -#include -#include - #include #include #include #include #include -#include "dbus.h" +#include +#include + #include "dbus-helper.h" #include "logging.h" #include "textfile.h" diff --git a/audio/headset.c b/audio/headset.c index 859a87ba..2d635110 100644 --- a/audio/headset.c +++ b/audio/headset.c @@ -47,10 +47,8 @@ #include #include - #include -#include "dbus.h" #include "dbus-helper.h" #include "logging.h" #include "device.h" diff --git a/audio/main.c b/audio/main.c index 5b4e395b..ef606223 100644 --- a/audio/main.c +++ b/audio/main.c @@ -31,8 +31,7 @@ #include #include - -#include "dbus.h" +#include #include "plugin.h" #include "dbus-service.h" diff --git a/audio/sink.c b/audio/sink.c index b44f8de6..b755759d 100644 --- a/audio/sink.c +++ b/audio/sink.c @@ -29,12 +29,11 @@ #include #include +#include + #include #include -#include - -#include "dbus.h" #include "dbus-helper.h" #include "logging.h" diff --git a/common/Makefile.am b/common/Makefile.am index a90df324..5352d34f 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -2,9 +2,9 @@ noinst_LIBRARIES = libhelper.a libhelper_a_SOURCES = oui.h oui.c textfile.h textfile.c \ - logging.h logging.c error.h error.c dbus.h dbus.c \ - dbus-helper.h dbus-helper.c glib-helper.h glib-helper.c \ - sdp-xml.h sdp-xml.c sdp-glib.c + logging.h logging.c error.h error.c \ + dbus-helper.h dbus-helper.c \ + glib-helper.h glib-helper.c sdp-xml.h sdp-xml.c sdp-glib.c noinst_PROGRAMS = test_textfile diff --git a/common/dbus-helper.h b/common/dbus-helper.h index 57c3d09b..6c3c3a89 100644 --- a/common/dbus-helper.h +++ b/common/dbus-helper.h @@ -20,8 +20,9 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */ + #include -#include +#include #define DBUS_TYPE_STRING_ARRAY_AS_STRING (DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING) #define DBUS_TYPE_BYTE_ARRAY_AS_STRING (DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING) @@ -93,3 +94,14 @@ dbus_bool_t dbus_connection_emit_property_changed(DBusConnection *conn, const char *interface, const char *name, int type, void *value); + +static inline DBusHandlerResult send_message_and_unref(DBusConnection *conn, + DBusMessage *msg) +{ + if (msg) { + dbus_connection_send(conn, msg, NULL); + dbus_message_unref(msg); + } + + return DBUS_HANDLER_RESULT_HANDLED; +} diff --git a/common/dbus.c b/common/dbus.c deleted file mode 100644 index 4ea56c9f..00000000 --- a/common/dbus.c +++ /dev/null @@ -1,391 +0,0 @@ -/* - * - * BlueZ - Bluetooth protocol stack for Linux - * - * Copyright (C) 2004-2008 Marcel Holtmann - * - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#ifdef NEED_DBUS_WATCH_GET_UNIX_FD -#define dbus_watch_get_unix_fd dbus_watch_get_fd -#endif - -#ifdef HAVE_DBUS_GLIB -#include -#endif - -#include "dbus.h" -#include "logging.h" - -static guint listener_id = 0; -static GSList *name_listeners = NULL; - -struct name_callback { - name_cb_t func; - void *user_data; - guint id; -}; - -struct name_data { - DBusConnection *connection; - char *name; - GSList *callbacks; -}; - -static struct name_data *name_data_find(DBusConnection *connection, - const char *name) -{ - GSList *current; - - for (current = name_listeners; - current != NULL; current = current->next) { - struct name_data *data = current->data; - - if (name == NULL && data->name == NULL) { - if (connection == data->connection) - return data; - } else { - if (strcmp(name, data->name) == 0) - return data; - } - } - - return NULL; -} - -static struct name_callback *name_callback_find(GSList *callbacks, - name_cb_t func, void *user_data) -{ - GSList *current; - - for (current = callbacks; current != NULL; current = current->next) { - struct name_callback *cb = current->data; - if (cb->func == func && cb->user_data == user_data) - return cb; - } - - return NULL; -} - -static void name_data_call_and_free(struct name_data *data) -{ - GSList *l; - - for (l = data->callbacks; l != NULL; l = l->next) { - struct name_callback *cb = l->data; - if (cb->func) - cb->func(data->name, cb->user_data); - g_free(cb); - } - - g_slist_free(data->callbacks); - g_free(data->name); - g_free(data); -} - -static void name_data_free(struct name_data *data) -{ - GSList *l; - - for (l = data->callbacks; l != NULL; l = l->next) - g_free(l->data); - - g_slist_free(data->callbacks); - g_free(data->name); - g_free(data); -} - -static int name_data_add(DBusConnection *connection, const char *name, - name_cb_t func, void *user_data, guint id) -{ - int first = 1; - struct name_data *data = NULL; - struct name_callback *cb = NULL; - - cb = g_new(struct name_callback, 1); - - cb->func = func; - cb->user_data = user_data; - cb->id = id; - - data = name_data_find(connection, name); - if (data) { - first = 0; - goto done; - } - - data = g_new0(struct name_data, 1); - - data->connection = connection; - data->name = g_strdup(name); - - name_listeners = g_slist_append(name_listeners, data); - -done: - data->callbacks = g_slist_append(data->callbacks, cb); - return first; -} - -static void name_data_remove(DBusConnection *connection, - const char *name, name_cb_t func, void *user_data) -{ - struct name_data *data; - struct name_callback *cb = NULL; - - data = name_data_find(connection, name); - if (!data) - return; - - cb = name_callback_find(data->callbacks, func, user_data); - if (cb) { - data->callbacks = g_slist_remove(data->callbacks, cb); - g_free(cb); - } - - if (!data->callbacks) { - name_listeners = g_slist_remove(name_listeners, data); - name_data_free(data); - } -} - -static gboolean add_match(DBusConnection *connection, const char *name) -{ - DBusError err; - char match_string[128]; - - snprintf(match_string, sizeof(match_string), - "interface=%s,member=NameOwnerChanged,arg0=%s", - DBUS_INTERFACE_DBUS, name); - - dbus_error_init(&err); - - dbus_bus_add_match(connection, match_string, &err); - - if (dbus_error_is_set(&err)) { - error("Adding match rule \"%s\" failed: %s", match_string, - err.message); - dbus_error_free(&err); - return FALSE; - } - - return TRUE; -} - -static gboolean remove_match(DBusConnection *connection, const char *name) -{ - DBusError err; - char match_string[128]; - - snprintf(match_string, sizeof(match_string), - "interface=%s,member=NameOwnerChanged,arg0=%s", - DBUS_INTERFACE_DBUS, name); - - dbus_error_init(&err); - - dbus_bus_remove_match(connection, match_string, &err); - - if (dbus_error_is_set(&err)) { - error("Removing owner match rule for %s failed: %s", - name, err.message); - dbus_error_free(&err); - return FALSE; - } - - return TRUE; -} - -static DBusHandlerResult name_exit_filter(DBusConnection *connection, - DBusMessage *message, void *user_data) -{ - GSList *l; - struct name_data *data; - char *name, *old, *new; - - if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, - "NameOwnerChanged")) - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - - if (!dbus_message_get_args(message, NULL, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_STRING, &old, - DBUS_TYPE_STRING, &new, - DBUS_TYPE_INVALID)) { - error("Invalid arguments for NameOwnerChanged signal"); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } - - /* We are not interested of service creations */ - if (*new != '\0') - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - - data = name_data_find(connection, name); - if (!data) { - error("Got NameOwnerChanged signal for %s which has no listeners", name); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } - - for (l = data->callbacks; l != NULL; l = l->next) { - struct name_callback *cb = l->data; - cb->func(name, cb->user_data); - } - - name_listeners = g_slist_remove(name_listeners, data); - name_data_free(data); - - remove_match(connection, name); - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; -} - -guint name_listener_add(DBusConnection *connection, const char *name, - name_cb_t func, void *user_data) -{ - int first; - - if (!listener_id) { - if (!dbus_connection_add_filter(connection, - name_exit_filter, NULL, NULL)) { - error("dbus_connection_add_filter() failed"); - return 0; - } - } - - listener_id++; - first = name_data_add(connection, name, func, user_data, listener_id); - /* The filter is already added if this is not the first callback - * registration for the name */ - if (!first) - return listener_id; - - if (name) { - debug("name_listener_add(%s)", name); - - if (!add_match(connection, name)) { - name_data_remove(connection, name, func, user_data); - return 0; - } - } - - return listener_id; -} - -int name_listener_remove(DBusConnection *connection, const char *name, - name_cb_t func, void *user_data) -{ - struct name_data *data; - struct name_callback *cb; - - data = name_data_find(connection, name); - if (!data) { - error("remove_name_listener: no listener for %s", name); - return -1; - } - - cb = name_callback_find(data->callbacks, func, user_data); - if (!cb) { - error("No matching callback found for %s", name); - return -1; - } - - data->callbacks = g_slist_remove(data->callbacks, cb); - g_free(cb); - - /* Don't remove the filter if other callbacks exist */ - if (data->callbacks) - return 0; - - if (name) { - debug("name_listener_remove(%s)", name); - - if (!remove_match(connection, name)) - return -1; - } - - name_data_remove(connection, name, func, user_data); - - return 0; -} - -gboolean name_listener_id_remove(guint id) -{ - struct name_data *data; - struct name_callback *cb; - GSList *ldata, *lcb; - - for (ldata = name_listeners; ldata; ldata = ldata->next) { - data = ldata->data; - for (lcb = data->callbacks; lcb; lcb = lcb->next) { - cb = lcb->data; - if (cb->id == id) - goto remove; - } - } - - return FALSE; - -remove: - data->callbacks = g_slist_remove(data->callbacks, cb); - g_free(cb); - - /* Don't remove the filter if other callbacks exist */ - if (data->callbacks) - return TRUE; - - if (data->name) { - if (!remove_match(data->connection, data->name)) - return FALSE; - } - - name_listeners = g_slist_remove(name_listeners, data); - name_data_free(data); - - return TRUE; -} - -int name_listener_indicate_disconnect(DBusConnection *connection) -{ - struct name_data *data; - - data = name_data_find(connection, NULL); - if (!data) { - error("name_listener_indicate_disconnect: no listener found"); - return -1; - } - - debug("name_listener_indicate_disconnect"); - - name_data_call_and_free(data); - - return 0; -} diff --git a/common/dbus.h b/common/dbus.h deleted file mode 100644 index 5976a479..00000000 --- a/common/dbus.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * - * BlueZ - Bluetooth protocol stack for Linux - * - * Copyright (C) 2004-2008 Marcel Holtmann - * - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifndef __H_BLUEZ_DBUS_H__ -#define __H_BLUEZ_DBUS_H__ - -#include -#include - -typedef void (*name_cb_t)(const char *name, void *user_data); - -guint name_listener_add(DBusConnection *connection, const char *name, - name_cb_t func, void *user_data); -int name_listener_remove(DBusConnection *connection, const char *name, - name_cb_t func, void *user_data); -gboolean name_listener_id_remove(guint id); -int name_listener_indicate_disconnect(DBusConnection *connection); - -static inline DBusHandlerResult send_message_and_unref(DBusConnection *conn, DBusMessage *msg) -{ - if (msg) { - dbus_connection_send(conn, msg, NULL); - dbus_message_unref(msg); - } - - return DBUS_HANDLER_RESULT_HANDLED; -} - -#endif /* __H_BLUEZ_DBUS_H__ */ diff --git a/daemon/database.c b/daemon/database.c index 23c9381c..b7d5562d 100644 --- a/daemon/database.c +++ b/daemon/database.c @@ -36,8 +36,9 @@ #include +#include + #include "dbus-helper.h" -#include "dbus.h" #include "sdp-xml.h" #include "logging.h" diff --git a/daemon/main.c b/daemon/main.c index 7479d25e..0c735c35 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -41,7 +41,6 @@ #include "dbus-helper.h" -#include "dbus.h" #include "logging.h" #include "sdpd.h" diff --git a/gdbus/Makefile.am b/gdbus/Makefile.am index 0a481277..937b1d7f 100644 --- a/gdbus/Makefile.am +++ b/gdbus/Makefile.am @@ -1,7 +1,7 @@ noinst_LTLIBRARIES = libgdbus.la -libgdbus_la_SOURCES = gdbus.h mainloop.c +libgdbus_la_SOURCES = gdbus.h mainloop.c watch.c AM_CFLAGS = @DBUS_CFLAGS@ @GLIB_CFLAGS@ diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h index a9f2aa51..d1902a52 100644 --- a/gdbus/gdbus.h +++ b/gdbus/gdbus.h @@ -40,6 +40,15 @@ gboolean g_dbus_set_disconnect_function(DBusConnection *connection, GDBusDisconnectFunction function, void *user_data, DBusFreeFunction destroy); +typedef void (*name_cb_t)(const char *name, void *user_data); + +guint name_listener_add(DBusConnection *connection, const char *name, + name_cb_t func, void *user_data); +int name_listener_remove(DBusConnection *connection, const char *name, + name_cb_t func, void *user_data); +gboolean name_listener_id_remove(guint id); +int name_listener_indicate_disconnect(DBusConnection *connection); + #ifdef __cplusplus } #endif diff --git a/gdbus/watch.c b/gdbus/watch.c new file mode 100644 index 00000000..80be3a6a --- /dev/null +++ b/gdbus/watch.c @@ -0,0 +1,380 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2004-2008 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include +#include + +#include "gdbus.h" + +#define info(fmt...) +#define error(fmt...) +#define debug(fmt...) + +static guint listener_id = 0; +static GSList *name_listeners = NULL; + +struct name_callback { + name_cb_t func; + void *user_data; + guint id; +}; + +struct name_data { + DBusConnection *connection; + char *name; + GSList *callbacks; +}; + +static struct name_data *name_data_find(DBusConnection *connection, + const char *name) +{ + GSList *current; + + for (current = name_listeners; + current != NULL; current = current->next) { + struct name_data *data = current->data; + + if (name == NULL && data->name == NULL) { + if (connection == data->connection) + return data; + } else { + if (strcmp(name, data->name) == 0) + return data; + } + } + + return NULL; +} + +static struct name_callback *name_callback_find(GSList *callbacks, + name_cb_t func, void *user_data) +{ + GSList *current; + + for (current = callbacks; current != NULL; current = current->next) { + struct name_callback *cb = current->data; + if (cb->func == func && cb->user_data == user_data) + return cb; + } + + return NULL; +} + +static void name_data_call_and_free(struct name_data *data) +{ + GSList *l; + + for (l = data->callbacks; l != NULL; l = l->next) { + struct name_callback *cb = l->data; + if (cb->func) + cb->func(data->name, cb->user_data); + g_free(cb); + } + + g_slist_free(data->callbacks); + g_free(data->name); + g_free(data); +} + +static void name_data_free(struct name_data *data) +{ + GSList *l; + + for (l = data->callbacks; l != NULL; l = l->next) + g_free(l->data); + + g_slist_free(data->callbacks); + g_free(data->name); + g_free(data); +} + +static int name_data_add(DBusConnection *connection, const char *name, + name_cb_t func, void *user_data, guint id) +{ + int first = 1; + struct name_data *data = NULL; + struct name_callback *cb = NULL; + + cb = g_new(struct name_callback, 1); + + cb->func = func; + cb->user_data = user_data; + cb->id = id; + + data = name_data_find(connection, name); + if (data) { + first = 0; + goto done; + } + + data = g_new0(struct name_data, 1); + + data->connection = connection; + data->name = g_strdup(name); + + name_listeners = g_slist_append(name_listeners, data); + +done: + data->callbacks = g_slist_append(data->callbacks, cb); + return first; +} + +static void name_data_remove(DBusConnection *connection, + const char *name, name_cb_t func, void *user_data) +{ + struct name_data *data; + struct name_callback *cb = NULL; + + data = name_data_find(connection, name); + if (!data) + return; + + cb = name_callback_find(data->callbacks, func, user_data); + if (cb) { + data->callbacks = g_slist_remove(data->callbacks, cb); + g_free(cb); + } + + if (!data->callbacks) { + name_listeners = g_slist_remove(name_listeners, data); + name_data_free(data); + } +} + +static gboolean add_match(DBusConnection *connection, const char *name) +{ + DBusError err; + char match_string[128]; + + snprintf(match_string, sizeof(match_string), + "interface=%s,member=NameOwnerChanged,arg0=%s", + DBUS_INTERFACE_DBUS, name); + + dbus_error_init(&err); + + dbus_bus_add_match(connection, match_string, &err); + + if (dbus_error_is_set(&err)) { + error("Adding match rule \"%s\" failed: %s", match_string, + err.message); + dbus_error_free(&err); + return FALSE; + } + + return TRUE; +} + +static gboolean remove_match(DBusConnection *connection, const char *name) +{ + DBusError err; + char match_string[128]; + + snprintf(match_string, sizeof(match_string), + "interface=%s,member=NameOwnerChanged,arg0=%s", + DBUS_INTERFACE_DBUS, name); + + dbus_error_init(&err); + + dbus_bus_remove_match(connection, match_string, &err); + + if (dbus_error_is_set(&err)) { + error("Removing owner match rule for %s failed: %s", + name, err.message); + dbus_error_free(&err); + return FALSE; + } + + return TRUE; +} + +static DBusHandlerResult name_exit_filter(DBusConnection *connection, + DBusMessage *message, void *user_data) +{ + GSList *l; + struct name_data *data; + char *name, *old, *new; + + if (!dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, + "NameOwnerChanged")) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + if (!dbus_message_get_args(message, NULL, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &old, + DBUS_TYPE_STRING, &new, + DBUS_TYPE_INVALID)) { + error("Invalid arguments for NameOwnerChanged signal"); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + /* We are not interested of service creations */ + if (*new != '\0') + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + data = name_data_find(connection, name); + if (!data) { + error("Got NameOwnerChanged signal for %s which has no listeners", name); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + for (l = data->callbacks; l != NULL; l = l->next) { + struct name_callback *cb = l->data; + cb->func(name, cb->user_data); + } + + name_listeners = g_slist_remove(name_listeners, data); + name_data_free(data); + + remove_match(connection, name); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +guint name_listener_add(DBusConnection *connection, const char *name, + name_cb_t func, void *user_data) +{ + int first; + + if (!listener_id) { + if (!dbus_connection_add_filter(connection, + name_exit_filter, NULL, NULL)) { + error("dbus_connection_add_filter() failed"); + return 0; + } + } + + listener_id++; + first = name_data_add(connection, name, func, user_data, listener_id); + /* The filter is already added if this is not the first callback + * registration for the name */ + if (!first) + return listener_id; + + if (name) { + debug("name_listener_add(%s)", name); + + if (!add_match(connection, name)) { + name_data_remove(connection, name, func, user_data); + return 0; + } + } + + return listener_id; +} + +int name_listener_remove(DBusConnection *connection, const char *name, + name_cb_t func, void *user_data) +{ + struct name_data *data; + struct name_callback *cb; + + data = name_data_find(connection, name); + if (!data) { + error("remove_name_listener: no listener for %s", name); + return -1; + } + + cb = name_callback_find(data->callbacks, func, user_data); + if (!cb) { + error("No matching callback found for %s", name); + return -1; + } + + data->callbacks = g_slist_remove(data->callbacks, cb); + g_free(cb); + + /* Don't remove the filter if other callbacks exist */ + if (data->callbacks) + return 0; + + if (name) { + debug("name_listener_remove(%s)", name); + + if (!remove_match(connection, name)) + return -1; + } + + name_data_remove(connection, name, func, user_data); + + return 0; +} + +gboolean name_listener_id_remove(guint id) +{ + struct name_data *data; + struct name_callback *cb; + GSList *ldata, *lcb; + + for (ldata = name_listeners; ldata; ldata = ldata->next) { + data = ldata->data; + for (lcb = data->callbacks; lcb; lcb = lcb->next) { + cb = lcb->data; + if (cb->id == id) + goto remove; + } + } + + return FALSE; + +remove: + data->callbacks = g_slist_remove(data->callbacks, cb); + g_free(cb); + + /* Don't remove the filter if other callbacks exist */ + if (data->callbacks) + return TRUE; + + if (data->name) { + if (!remove_match(data->connection, data->name)) + return FALSE; + } + + name_listeners = g_slist_remove(name_listeners, data); + name_data_free(data); + + return TRUE; +} + +int name_listener_indicate_disconnect(DBusConnection *connection) +{ + struct name_data *data; + + data = name_data_find(connection, NULL); + if (!data) { + error("name_listener_indicate_disconnect: no listener found"); + return -1; + } + + debug("name_listener_indicate_disconnect"); + + name_data_call_and_free(data); + + return 0; +} diff --git a/hcid/adapter.c b/hcid/adapter.c index 4be0f914..4bc7245e 100644 --- a/hcid/adapter.c +++ b/hcid/adapter.c @@ -48,8 +48,9 @@ #include +#include + #include "hcid.h" -#include "dbus.h" #include "adapter.h" #include "device.h" diff --git a/hcid/agent.c b/hcid/agent.c index 772a56ee..e7bf7d96 100644 --- a/hcid/agent.c +++ b/hcid/agent.c @@ -41,7 +41,8 @@ #include -#include "dbus.h" +#include + #include "dbus-helper.h" #include "hcid.h" #include "dbus-common.h" diff --git a/hcid/dbus-common.c b/hcid/dbus-common.c index 7aa7b6d1..7a28555d 100644 --- a/hcid/dbus-common.c +++ b/hcid/dbus-common.c @@ -52,7 +52,6 @@ #include #include "hcid.h" -#include "dbus.h" #include "dbus-helper.h" #include "dbus-error.h" #include "manager.h" diff --git a/hcid/dbus-database.c b/hcid/dbus-database.c index 9519975e..370b7658 100644 --- a/hcid/dbus-database.c +++ b/hcid/dbus-database.c @@ -40,7 +40,8 @@ #include -#include "dbus.h" +#include + #include "dbus-helper.h" #include "hcid.h" #include "sdpd.h" diff --git a/hcid/dbus-error.c b/hcid/dbus-error.c index 20a55c5c..876082a4 100644 --- a/hcid/dbus-error.c +++ b/hcid/dbus-error.c @@ -34,7 +34,7 @@ #include #include "hcid.h" -#include "dbus.h" +#include "dbus-helper.h" #include "dbus-common.h" #include "dbus-error.h" #include "error.h" diff --git a/hcid/dbus-hci.c b/hcid/dbus-hci.c index 2d21c905..ec4e0f50 100644 --- a/hcid/dbus-hci.c +++ b/hcid/dbus-hci.c @@ -45,8 +45,9 @@ #include +#include + #include "hcid.h" -#include "dbus.h" #include "textfile.h" #include "manager.h" #include "adapter.h" diff --git a/hcid/dbus-sdp.c b/hcid/dbus-sdp.c index eceb889d..0b3bc85e 100644 --- a/hcid/dbus-sdp.c +++ b/hcid/dbus-sdp.c @@ -49,7 +49,8 @@ #include -#include "dbus.h" +#include + #include "dbus-helper.h" #include "hcid.h" #include "textfile.h" diff --git a/hcid/dbus-security.c b/hcid/dbus-security.c index 6b680c70..2e4a8bfe 100644 --- a/hcid/dbus-security.c +++ b/hcid/dbus-security.c @@ -42,9 +42,9 @@ #include -#include "adapter.h" +#include -#include "dbus.h" +#include "adapter.h" #include "dbus-helper.h" #include "hcid.h" #include "dbus-common.h" diff --git a/hcid/dbus-service.c b/hcid/dbus-service.c index 192ff738..59228a9d 100644 --- a/hcid/dbus-service.c +++ b/hcid/dbus-service.c @@ -45,7 +45,6 @@ #include -#include "dbus.h" #include "dbus-helper.h" #include "hcid.h" #include "server.h" diff --git a/hcid/dbus-test.c b/hcid/dbus-test.c index 37c5c2be..d683a901 100644 --- a/hcid/dbus-test.c +++ b/hcid/dbus-test.c @@ -38,7 +38,8 @@ #include -#include "dbus.h" +#include + #include "dbus-helper.h" #include "hcid.h" #include "dbus-common.h" diff --git a/hcid/manager.c b/hcid/manager.c index c4288278..0d438217 100644 --- a/hcid/manager.c +++ b/hcid/manager.c @@ -43,10 +43,11 @@ #include +#include + #include "hcid.h" #include "sdpd.h" #include "adapter.h" -#include "dbus.h" #include "dbus-helper.h" #include "dbus-common.h" #include "error.h" diff --git a/hcid/plugin.c b/hcid/plugin.c index 2a8f4375..f99966bf 100644 --- a/hcid/plugin.c +++ b/hcid/plugin.c @@ -34,10 +34,8 @@ #include -#include "dbus.h" #include "logging.h" -#include "dbus-service.h" #include "plugin.h" static GSList *plugins = NULL; diff --git a/input/Makefile.am b/input/Makefile.am index 52a46a74..a514b629 100644 --- a/input/Makefile.am +++ b/input/Makefile.am @@ -10,12 +10,12 @@ libinput_la_SOURCES = main.c \ fakehid.c fakehid.h LDADD = $(top_builddir)/common/libhelper.a \ - @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ + @GDBUS_LIBS@ @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ endif AM_LDFLAGS = -module -avoid-version -export-symbols-regex bluetooth_plugin_desc -AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ +AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ @GDBUS_CFLAGS@ INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/hcid diff --git a/input/device.c b/input/device.c index ab4e59c7..151ce08c 100644 --- a/input/device.c +++ b/input/device.c @@ -40,10 +40,8 @@ #include #include - #include -#include "dbus.h" #include "dbus-helper.h" #include "logging.h" #include "textfile.h" diff --git a/input/main.c b/input/main.c index c9432c21..b57286b2 100644 --- a/input/main.c +++ b/input/main.c @@ -29,7 +29,8 @@ #include -#include "dbus.h" +#include +#include #include "plugin.h" #include "dbus-service.h" diff --git a/input/manager.c b/input/manager.c index 1c024c9c..11e14514 100644 --- a/input/manager.c +++ b/input/manager.c @@ -42,7 +42,6 @@ #include -#include "dbus.h" #include "dbus-helper.h" #include "logging.h" #include "textfile.h" diff --git a/input/server.c b/input/server.c index ff7cdbfd..5e29e99f 100644 --- a/input/server.c +++ b/input/server.c @@ -38,13 +38,14 @@ #include #include +#include #include "logging.h" -#include "dbus.h" #include "device.h" #include "server.h" #include "storage.h" +#include "dbus-helper.h" #include "dbus-service.h" static const char* HID_UUID = "00001124-0000-1000-8000-00805f9b34fb"; diff --git a/network/Makefile.am b/network/Makefile.am index 5218ce6f..e4b5b317 100644 --- a/network/Makefile.am +++ b/network/Makefile.am @@ -10,12 +10,12 @@ libnetwork_la_SOURCES = main.c \ connection.h connection.c common.h common.c LDADD = $(top_builddir)/common/libhelper.a \ - @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ + @GDBUS_LIBS@ @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ endif AM_LDFLAGS = -module -avoid-version -export-symbols-regex bluetooth_plugin_desc -AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ +AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ @GDBUS_CFLAGS@ INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/hcid -I$(top_srcdir)/sdpd diff --git a/network/connection.c b/network/connection.c index b5ef61ba..466dd203 100644 --- a/network/connection.c +++ b/network/connection.c @@ -41,7 +41,6 @@ #include #include "logging.h" -#include "dbus.h" #include "dbus-helper.h" #include "textfile.h" #include "glib-helper.h" diff --git a/network/main.c b/network/main.c index c84ba59b..f710080f 100644 --- a/network/main.c +++ b/network/main.c @@ -29,7 +29,8 @@ #include #include -#include "dbus.h" +#include +#include #include "plugin.h" #include "dbus-service.h" diff --git a/network/manager.c b/network/manager.c index 2ad0a97d..14bea175 100644 --- a/network/manager.c +++ b/network/manager.c @@ -40,7 +40,6 @@ #include -#include "dbus.h" #include "dbus-helper.h" #include "logging.h" #include "textfile.h" diff --git a/network/server.c b/network/server.c index 046b20b3..f4fb5176 100644 --- a/network/server.c +++ b/network/server.c @@ -45,7 +45,6 @@ #include #include "logging.h" -#include "dbus.h" #include "error.h" #include "textfile.h" #include "dbus-helper.h" diff --git a/plugins/Makefile.am b/plugins/Makefile.am index a2a521dc..c403e835 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -11,7 +11,7 @@ libecho_la_SOURCES = echo.c AM_LDFLAGS = -module -avoid-version -export-symbols-regex bluetooth_plugin_desc -AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ +AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ @GDBUS_CFLAGS@ INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/hcid diff --git a/plugins/echo.c b/plugins/echo.c index 1aa15681..2c549089 100644 --- a/plugins/echo.c +++ b/plugins/echo.c @@ -34,7 +34,7 @@ #include -#include "dbus.h" +#include #include "plugin.h" #include "server.h" diff --git a/plugins/storage.c b/plugins/storage.c index 7930d268..9e65a1ad 100644 --- a/plugins/storage.c +++ b/plugins/storage.c @@ -27,7 +27,6 @@ #include -#include "dbus.h" #include "plugin.h" #include "logging.h" diff --git a/serial/Makefile.am b/serial/Makefile.am index 226ba030..2f2286bf 100644 --- a/serial/Makefile.am +++ b/serial/Makefile.am @@ -9,12 +9,12 @@ libserial_la_SOURCES = main.c \ storage.h storage.c LDADD = $(top_builddir)/common/libhelper.a \ - @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ + @GDBUS_LIBS@ @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@ endif AM_LDFLAGS = -module -avoid-version -export-symbols-regex bluetooth_plugin_desc -AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ +AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ @GLIB_CFLAGS@ @GDBUS_CFLAGS@ INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/hcid -I$(top_srcdir)/sdpd diff --git a/serial/main.c b/serial/main.c index 57a65f88..b4533a01 100644 --- a/serial/main.c +++ b/serial/main.c @@ -30,10 +30,11 @@ #include #include + +#include #include #include "plugin.h" -#include "dbus.h" #include "dbus-service.h" #include "manager.h" diff --git a/serial/manager.c b/serial/manager.c index 54e052da..a10e1631 100644 --- a/serial/manager.c +++ b/serial/manager.c @@ -40,8 +40,6 @@ #include #include -#include - #include #include #include @@ -49,7 +47,10 @@ #include #include -#include "dbus.h" +#include + +#include + #include "dbus-helper.h" #include "logging.h" #include "textfile.h" diff --git a/serial/port.c b/serial/port.c index f2c0eb61..98b751be 100644 --- a/serial/port.c +++ b/serial/port.c @@ -36,12 +36,13 @@ #include #include -#include - #include #include -#include "dbus.h" +#include + +#include + #include "dbus-helper.h" #include "logging.h" -- cgit