Skip to contentSkip to conversation

shared/gatt-client: Fix not freeing notify data

PATCH Applied Luiz Augusto von Dentz 2 participants 6 messages Sep 8, 2015 – Sep 14, 2015 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/3 shared/gatt-client: Fix not freeing notify data

From: Luiz Augusto von Dentz <[email protected]>

notify_data_write_ccc takes another reference in case it succeed so the original reference should be dropped to cause the data to be freed once notify_data_write_ccc completes.

---
 src/shared/gatt-client.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
src/shared/gatt-client.c +1 -2
index 903afa7..0983852 100644
@@ -1596,8 +1596,7 @@ static void complete_unregister_notify(void *data)
 						!notify_data->chrc->ccc_handle)
 		goto done;
 
-	if (notify_data_write_ccc(notify_data, false, disable_ccc_callback))
-		return;
+	notify_data_write_ccc(notify_data, false, disable_ccc_callback);
 
 done:
 	notify_data_unref(notify_data);
Signature · 1 line
2.4.3
author maintainer
#1 lore raw

PATCH 2/3 core/gatt: Fix not able to read/write on reconnections

From: Luiz Augusto von Dentz <[email protected]>

Upon reconnecting the attributes may already be available from cache which mean the client would be able to issue commands while discovery is not complete thus btd_gatt_client_ready was not called yet.

To fix now btd_gatt_client_ready is split into btd_gatt_client_connected which is called whenever connected leaving btd_gatt_client_ready to only deal with exporting services found during the discovery.

---
 src/device.c      |  2 ++
 src/gatt-client.c | 24 +++++++++++++++++-------
 src/gatt-client.h |  1 +
 3 files changed, 20 insertions(+), 7 deletions(-)
src/device.c +2 -0
index dc362d9..2102f23 100644
@@ -4602,6 +4602,8 @@ static void gatt_client_init(struct btd_device *device)
 	}
 
 	device->gatt_cache_used = !gatt_db_isempty(device->db);
+
+	btd_gatt_client_connected(device->client_dbus);
 }
 
 static void gatt_server_init(struct btd_device *device, struct gatt_db *db)
src/gatt-client.c +17 -7
index d8a3429..225aa42 100644
@@ -1825,20 +1825,14 @@ fail:
 
 void btd_gatt_client_ready(struct btd_gatt_client *client)
 {
-	struct bt_gatt_client *gatt;
-
 	if (!client)
 		return;
 
-	gatt = btd_device_get_gatt_client(client->device);
-	if (!gatt) {
+	if (!client->gatt) {
 		error("GATT client not initialized");
 		return;
 	}
 
-	bt_gatt_client_unref(client->gatt);
-	client->gatt = bt_gatt_client_ref(gatt);
-
 	client->ready = true;
 
 	DBG("GATT client ready");
@@ -1852,6 +1846,22 @@ void btd_gatt_client_ready(struct btd_gatt_client *client)
 	queue_foreach(client->all_notify_clients, register_notify, client);
 }
 
+void btd_gatt_client_connected(struct btd_gatt_client *client)
+{
+	struct bt_gatt_client *gatt;
+
+	gatt = btd_device_get_gatt_client(client->device);
+	if (!gatt) {
+		error("GATT client not initialized");
+		return;
+	}
+
+	DBG("Device connected.");
+
+	bt_gatt_client_unref(client->gatt);
+	client->gatt = bt_gatt_client_ref(gatt);
+}
+
 void btd_gatt_client_service_added(struct btd_gatt_client *client,
 					struct gatt_db_attribute *attrib)
 {
src/gatt-client.h +1 -0
index a18db17..92a9255 100644
@@ -23,6 +23,7 @@ struct btd_gatt_client *btd_gatt_client_new(struct btd_device *device);
 void btd_gatt_client_destroy(struct btd_gatt_client *client);
 
 void btd_gatt_client_ready(struct btd_gatt_client *client);
+void btd_gatt_client_connected(struct btd_gatt_client *client);
 void btd_gatt_client_service_added(struct btd_gatt_client *client,
 					struct gatt_db_attribute *attrib);
 void btd_gatt_client_service_removed(struct btd_gatt_client *client,
Signature · 1 line
2.4.3
#2 lore raw

Re: [PATCH BlueZ 2/3] core/gatt: Fix not able to read/write on reconnections

Hi Luiz,

This patch fixes all my issues with writing just after connecting. Thanks alot!

Quoted 96 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Upon reconnecting the attributes may already be available from cache which mean the client would be able to issue commands while discovery is not complete thus btd_gatt_client_ready was not called yet.

To fix now btd_gatt_client_ready is split into btd_gatt_client_connected which is called whenever connected leaving btd_gatt_client_ready to only deal with exporting services found during the discovery.

---
 src/device.c      |  2 ++
 src/gatt-client.c | 24 +++++++++++++++++-------
 src/gatt-client.h |  1 +
 3 files changed, 20 insertions(+), 7 deletions(-)
src/device.c +2 -0
index dc362d9..2102f23 100644
@@ -4602,6 +4602,8 @@ static void gatt_client_init(struct btd_device *device)
        }
 
        device->gatt_cache_used = !gatt_db_isempty(device->db);
+
+       btd_gatt_client_connected(device->client_dbus);
 }
 
 static void gatt_server_init(struct btd_device *device, struct gatt_db *db)
src/gatt-client.c +17 -7
index d8a3429..225aa42 100644
@@ -1825,20 +1825,14 @@ fail:
 
 void btd_gatt_client_ready(struct btd_gatt_client *client)
 {
-       struct bt_gatt_client *gatt;
-
        if (!client)
                return;
 
-       gatt = btd_device_get_gatt_client(client->device);
-       if (!gatt) {
+       if (!client->gatt) {
                error("GATT client not initialized");
                return;
        }
 
-       bt_gatt_client_unref(client->gatt);
-       client->gatt = bt_gatt_client_ref(gatt);
-
        client->ready = true;
 
        DBG("GATT client ready");
@@ -1852,6 +1846,22 @@ void btd_gatt_client_ready(struct btd_gatt_client *client)
        queue_foreach(client->all_notify_clients, register_notify, client);
 }
 
+void btd_gatt_client_connected(struct btd_gatt_client *client)
+{
+       struct bt_gatt_client *gatt;
+
+       gatt = btd_device_get_gatt_client(client->device);
+       if (!gatt) {
+               error("GATT client not initialized");
+               return;
+       }
+
+       DBG("Device connected.");
+
+       bt_gatt_client_unref(client->gatt);
+       client->gatt = bt_gatt_client_ref(gatt);
+}
+
 void btd_gatt_client_service_added(struct btd_gatt_client *client,
                                        struct gatt_db_attribute *attrib)
 {
src/gatt-client.h +1 -1
index a18db17..92a9255 100644
@@ -23,6 +23,7 @@ struct btd_gatt_client *btd_gatt_client_new(struct btd_device *device);
 void btd_gatt_client_destroy(struct btd_gatt_client *client);
 
 void btd_gatt_client_ready(struct btd_gatt_client *client);
+void btd_gatt_client_connected(struct btd_gatt_client *client);
 void btd_gatt_client_service_added(struct btd_gatt_client *client,
                                        struct gatt_db_attribute *attrib);
 void btd_gatt_client_service_removed(struct btd_gatt_client *client,
--

2.4.3

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

Re: [PATCH BlueZ 2/3] core/gatt: Fix not able to read/write on reconnections

Hi Luiz,

Quoted 5 lines · Jakub Pawlowski

Hi Luiz,

This patch fixes all my issues with writing just after connecting. Thanks alot!

So reading and writing works perfectly, even better than I fought - I have some short flows that execute before services get re-discovered, but registering for notifications just after connection fails.

I can call DBus method register for notifications before I connect , and request will succeed, but I would really be registered for notifications after btd_gatt_client_ready is called.
This cause a race condition:

1. App Call DBus register for notifications.
2. App call Connect on Device.
3. App Write - write succeds immediately.
4. Device sends back notification which I don't receive in App (I can see it coming in btmon).
5. Now btd_gatt_client_ready is called, and App is really being registered for notifications (too late).

I'm working on fixing that. It whould require registering for notifications right after connecting, before serivces get discovered.

Quoted 98 lines
Quoted 96 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Upon reconnecting the attributes may already be available from cache which mean the client would be able to issue commands while discovery is not complete thus btd_gatt_client_ready was not called yet.

To fix now btd_gatt_client_ready is split into btd_gatt_client_connected which is called whenever connected leaving btd_gatt_client_ready to only deal with exporting services found during the discovery.

---
 src/device.c      |  2 ++
 src/gatt-client.c | 24 +++++++++++++++++-------
 src/gatt-client.h |  1 +
 3 files changed, 20 insertions(+), 7 deletions(-)
src/device.c +2 -0
index dc362d9..2102f23 100644
@@ -4602,6 +4602,8 @@ static void gatt_client_init(struct btd_device *device)
        }
 
        device->gatt_cache_used = !gatt_db_isempty(device->db);
+
+       btd_gatt_client_connected(device->client_dbus);
 }
 
 static void gatt_server_init(struct btd_device *device, struct gatt_db *db)
src/gatt-client.c +17 -7
index d8a3429..225aa42 100644
@@ -1825,20 +1825,14 @@ fail:
 
 void btd_gatt_client_ready(struct btd_gatt_client *client)
 {
-       struct bt_gatt_client *gatt;
-
        if (!client)
                return;
 
-       gatt = btd_device_get_gatt_client(client->device);
-       if (!gatt) {
+       if (!client->gatt) {
                error("GATT client not initialized");
                return;
        }
 
-       bt_gatt_client_unref(client->gatt);
-       client->gatt = bt_gatt_client_ref(gatt);
-
        client->ready = true;
 
        DBG("GATT client ready");
@@ -1852,6 +1846,22 @@ void btd_gatt_client_ready(struct btd_gatt_client *client)
        queue_foreach(client->all_notify_clients, register_notify, client);
 }
 
+void btd_gatt_client_connected(struct btd_gatt_client *client)
+{
+       struct bt_gatt_client *gatt;
+
+       gatt = btd_device_get_gatt_client(client->device);
+       if (!gatt) {
+               error("GATT client not initialized");
+               return;
+       }
+
+       DBG("Device connected.");
+
+       bt_gatt_client_unref(client->gatt);
+       client->gatt = bt_gatt_client_ref(gatt);
+}
+
 void btd_gatt_client_service_added(struct btd_gatt_client *client,
                                        struct gatt_db_attribute *attrib)
 {
src/gatt-client.h +1 -1
index a18db17..92a9255 100644
@@ -23,6 +23,7 @@ struct btd_gatt_client *btd_gatt_client_new(struct btd_device *device);
 void btd_gatt_client_destroy(struct btd_gatt_client *client);
 
 void btd_gatt_client_ready(struct btd_gatt_client *client);
+void btd_gatt_client_connected(struct btd_gatt_client *client);
 void btd_gatt_client_service_added(struct btd_gatt_client *client,
                                        struct gatt_db_attribute *attrib);
 void btd_gatt_client_service_removed(struct btd_gatt_client *client,
--

2.4.3

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#4 lore raw

PATCH 3/3 core/device: Only reload database if empty

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

There is no need to reload the database once it has been populated.

---
 src/device.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/device.c +2 -1
index 2102f23..8184508 100644
@@ -4717,7 +4717,8 @@ bool device_attach_att(struct btd_device *dev, GIOChannel *io)
 	dst = device_get_address(dev);
 	ba2str(dst, dstaddr);
 
-	load_gatt_db(dev, srcaddr, dstaddr);
+	if (gatt_db_isempty(dev->db))
+		load_gatt_db(dev, srcaddr, dstaddr);
 
 	gatt_client_init(dev);
 	gatt_server_init(dev, btd_gatt_database_get_db(database));
Signature · 1 line
2.4.3
author maintainer Applied
#5 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 24 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

notify_data_write_ccc takes another reference in case it succeed so the original reference should be dropped to cause the data to be freed once notify_data_write_ccc completes.

---
 src/shared/gatt-client.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
src/shared/gatt-client.c +1 -3
index 903afa7..0983852 100644
@@ -1596,8 +1596,7 @@ static void complete_unregister_notify(void *data)
                                                !notify_data->chrc->ccc_handle)
                goto done;
 
-       if (notify_data_write_ccc(notify_data, false, disable_ccc_callback))
-               return;
+       notify_data_write_ccc(notify_data, false, disable_ccc_callback);
 
 done:
        notify_data_unref(notify_data);
--
Quoted 1 line

2.4.3

Applied.

Signature · 1 line
Luiz Augusto von Dentz