Return-Path: From: Lukasz Rymanowski To: linux-bluetooth@vger.kernel.org Cc: Lukasz Rymanowski Subject: [PATCH v2 2/6] android/gatt: Unsubscribe from autoconnect on unregister Date: Wed, 3 Sep 2014 08:58:03 +0200 Message-Id: <1409727487-19074-2-git-send-email-lukasz.rymanowski@tieto.com> In-Reply-To: <1409727487-19074-1-git-send-email-lukasz.rymanowski@tieto.com> References: <1409727487-19074-1-git-send-email-lukasz.rymanowski@tieto.com> Sender: linux-bluetooth-owner@vger.kernel.org List-ID: When application does unregister, lets make sure that BfA does not keep any auto connect devices for this app. --- android/gatt.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/android/gatt.c b/android/gatt.c index a3486ca..dea253e 100644 --- a/android/gatt.c +++ b/android/gatt.c @@ -370,6 +370,13 @@ static bool match_device_by_state(const void *data, const void *user_data) return true; } +static bool match_device_by_ac_id(const void *data, const void *user_data) +{ + const struct gatt_device *dev = data; + + return queue_find(dev->autoconnect_apps, match_by_value, user_data); +} + static bool match_pending_device(const void *data, const void *user_data) { const struct gatt_device *dev = data; @@ -442,6 +449,11 @@ static struct gatt_device *find_device_by_state(uint32_t state) UINT_TO_PTR(state)); } +static struct gatt_device *find_device_by_autoconnect_app_id(uint32_t id) +{ + return queue_find(gatt_devices, match_device_by_ac_id, + UINT_TO_PTR(id)); +} static bool match_srvc_by_element_id(const void *data, const void *user_data) { const struct element_id *exp_id = user_data; @@ -1865,10 +1877,41 @@ static bool trigger_connection(struct app_connection *connection) return ret; } +static void clear_autoconnect_devices(int app_id) +{ + struct gatt_device *dev; + + /* + * Check if application was registered for any devices to + * reconnect + */ + dev = find_device_by_autoconnect_app_id(app_id); + while (dev) { + queue_remove(dev->autoconnect_apps, INT_TO_PTR(app_id)); + + if (queue_isempty(dev->autoconnect_apps)) { + bt_auto_connect_remove(&dev->bdaddr); + + if (dev->state == DEVICE_CONNECT_INIT) + device_set_state(dev, DEVICE_DISCONNECTED); + + device_unref(dev); + } + + dev = find_device_by_autoconnect_app_id(app_id); + }; +} + static uint8_t unregister_app(int client_if) { struct gatt_app *cl; + /* + * Make sure that there is no devices in auto connect list for this + * application + */ + clear_autoconnect_devices(client_if); + cl = queue_remove_if(gatt_apps, match_app_by_id, INT_TO_PTR(client_if)); if (!cl) { error("gatt: client_if=%d not found", client_if); -- 1.8.4