diff --git a/common/glib-ectomy.c b/common/glib-ectomy.c index dff48ef69..aa1007ee6 100644 --- a/common/glib-ectomy.c +++ b/common/glib-ectomy.c @@ -231,6 +231,11 @@ static void timeout_handlers_prepare(GMainContext *context) context->timeout = (timeout != LONG_MAX ? timeout: -1); } +static int timeout_cmp(const void *t1, const void *t2) +{ + return t1-t2; +} + static void timeout_handlers_check(GMainContext *context) { struct slist *l = context->ltimeout; @@ -249,9 +254,17 @@ static void timeout_handlers_check(GMainContext *context) continue; if (t->function(t->data)) { - /* if false/expired: remove it from the list */ - context->ltimeout = slist_remove(context->ltimeout, t); - free(t); + struct slist *match; + /* if false/expired: remove it from the list + * Before remove check again in order to cover the situation + * when the handler is removed/freed by the callback function + */ + match = slist_find(context->ltimeout, t, timeout_cmp); + if (match) { + t = match->data; + context->ltimeout = slist_remove(context->ltimeout, t); + free(t); + } } else { glong secs, msecs; /* update the next expiration time */ diff --git a/hcid/dbus-adapter.c b/hcid/dbus-adapter.c index 56c54c104..1fdcbdda5 100644 --- a/hcid/dbus-adapter.c +++ b/hcid/dbus-adapter.c @@ -1626,29 +1626,6 @@ static DBusHandlerResult handle_dev_disconnect_remote_device_req(DBusConnection } -static int bonding_timeout_handler(void *data) -{ - struct hci_dbus_data *dbus_data = data; - int dd = -1; - - if (!dbus_data->bonding) - return -1; - - dd = hci_open_dev(dbus_data->dev_id); - if (dd < 0) { - error("HCI device open failed: hci%d", dbus_data->dev_id); - return -1; - } - - hci_send_cmd(dd, OGF_LINK_CTL, OCF_PIN_CODE_NEG_REPLY, HCI_PIN_OR_KEY_MISSING, - &dbus_data->bonding->bdaddr); - - if (dd >= 0) - close(dd); - - return -1; -} - static DBusHandlerResult handle_dev_create_bonding_req(DBusConnection *conn, DBusMessage *msg, void *data) { char filename[PATH_MAX + 1]; @@ -1761,11 +1738,6 @@ static DBusHandlerResult handle_dev_create_bonding_req(DBusConnection *conn, DBu dbus_data->bonding = bonding_request_new(&peer_bdaddr); dbus_data->bonding->disconnect = disconnect; dbus_data->bonding->rq = dbus_message_ref(msg); - /* - * Create a timeout to disconnect automatically if no passkey is provided - * Broadcom chips doesn't handle timeout for PIN code request command - */ - dbus_data->bonding->timeout = g_timeout_add(BONDING_TIMEOUT, bonding_timeout_handler, dbus_data); dbus_data->requestor_name = strdup(dbus_message_get_sender(msg)); diff --git a/hcid/dbus.c b/hcid/dbus.c index 6530a1306..7c1d3e344 100644 --- a/hcid/dbus.c +++ b/hcid/dbus.c @@ -54,6 +54,13 @@ static int experimental = 0; #define MAX_CONN_NUMBER 10 #define RECONNECT_RETRY_TIMEOUT 5000 +#define DISPATCH_TIMEOUT 0 + +typedef struct +{ + uint32_t id; + DBusTimeout *timeout; +} timeout_handler_t; void hcid_dbus_set_experimental(void) { @@ -80,7 +87,6 @@ void bonding_request_free(struct bonding_request_info *dev ) dbus_message_unref(dev->rq); if (dev->cancel) dbus_message_unref(dev->cancel); - g_timeout_remove(dev->timeout); free(dev); } } @@ -1299,6 +1305,18 @@ failed: * Section reserved to D-Bus watch functions * *****************************************************************/ +static int message_dispatch_cb(void *data) +{ + dbus_connection_ref(connection); + + /* Dispatch messages */ + while(dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS); + + dbus_connection_unref(connection); + + return -1; +} + static gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data) { DBusWatch *watch = (DBusWatch *) data; @@ -1311,12 +1329,8 @@ static gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data) dbus_watch_handle(watch, flags); - dbus_connection_ref(connection); - - /* Dispatch messages */ - while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS); - - dbus_connection_unref(connection); + if (dbus_connection_get_dispatch_status(connection) == DBUS_DISPATCH_DATA_REMAINS) + g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, NULL); return TRUE; } @@ -1371,6 +1385,64 @@ static void watch_toggled(DBusWatch *watch, void *data) remove_watch(watch, data); } +static int timeout_handler_dispatch(gpointer data) +{ + timeout_handler_t *handler = data; + + dbus_timeout_handle(handler->timeout); + + return -1; +} + +static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) +{ + timeout_handler_t *handler; + + if (!dbus_timeout_get_enabled (timeout)) + return TRUE; + + handler = malloc(sizeof(timeout_handler_t)); + memset(handler, 0, sizeof(timeout_handler_t)); + + handler->timeout = timeout; + handler->id = g_timeout_add(dbus_timeout_get_interval(timeout), + timeout_handler_dispatch, handler); + + dbus_timeout_set_data(timeout, handler, NULL); + + return TRUE; +} + +static void remove_timeout(DBusTimeout *timeout, void *data) +{ + timeout_handler_t *handler; + + handler = dbus_timeout_get_data(timeout); + if(handler == NULL) + return; + + g_timeout_remove(handler->id); + + free(handler); +} + +static void timeout_toggled(DBusTimeout *timeout, void *data) +{ + if (dbus_timeout_get_enabled(timeout)) + add_timeout(timeout, data); + else + remove_timeout(timeout, data); +} + +static void wakeup_main_cb(void *data) +{ + if (!dbus_connection_get_is_connected(connection)) + return; + + if (dbus_connection_get_dispatch_status(connection) == DBUS_DISPATCH_DATA_REMAINS) + g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, NULL); +} + int hcid_dbus_init(void) { int ret_val; @@ -1412,6 +1484,12 @@ int hcid_dbus_init(void) dbus_connection_set_watch_functions(connection, add_watch, remove_watch, watch_toggled, NULL, NULL); + dbus_connection_set_timeout_functions(connection, + add_timeout, remove_timeout, timeout_toggled, NULL, NULL); + + dbus_connection_set_wakeup_main_function(connection, + wakeup_main_cb, NULL, NULL); + return 0; } diff --git a/hcid/dbus.h b/hcid/dbus.h index e4d8b963d..5bab94cea 100644 --- a/hcid/dbus.h +++ b/hcid/dbus.h @@ -88,7 +88,6 @@ struct bonding_request_info { DBusMessage *rq; DBusMessage *cancel; int disconnect; /* disconnect after finish */ - int timeout; /* timeout id */ }; struct active_conn_info {