Skip to contentSkip to conversation

Optimize discovery

PATCH New Luiz Augusto von Dentz 1 participant 11 messages May 10, 2017 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

v2 cover Optimize discovery

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

This reduces quite a bit the complexity of GATT discovery while reducing the amount traffic, this is achieved by using breadth-first like lookup.

Bellow is the traces with using Zephyr peripheral sample which uses a somewhat small MTU (65 bytes), it probably has even better results when using bigger MTU.

v2: Fix failing when attribute have already been discovered, result are pretty much the same except for the range is now adjusted properly.

Luiz Augusto von Dentz (10):
shared/gatt-db: Add gatt_db_get_service shared/gatt-db: Add gatt_db_service_insert_included shared/gatt-client: Insert included attribute with handle shared/gatt-db: Add gatt_db_insert_included shared/gatt-client: Simplify included discovery unit/test-gatt: Adjust the PDUs to the new behavior shared/gatt-db: Add gatt_db_insert_characteristic shared/gatt-db: Add gatt_db_insert_descriptor shared/gatt-client: Simplify characteristics discovery unit/test-gatt: Adjust the PDUs for characteristic discovery

 src/shared/gatt-client.c | 257 ++++++++++++++---------------------------------
 src/shared/gatt-db.c     | 116 +++++++++++++++++++--
 src/shared/gatt-db.h     |  30 ++++++
 unit/test-gatt.c         |  80 ++++++---------
 4 files changed, 245 insertions(+), 238 deletions(-)
Signature · 1 line
2.9.3
author maintainer
#1 lore raw

PATCH v2 1/10 shared/gatt-db: Add gatt_db_get_service

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

This adds gatt_db_get_service which can be used to lookup service by handle.

---
 src/shared/gatt-db.c | 21 ++++++++++++++++++---
 src/shared/gatt-db.h |  3 +++
 2 files changed, 21 insertions(+), 3 deletions(-)
src/shared/gatt-db.c +18 -3
index 8ef6f3b..efd61f9 100644
@@ -1316,20 +1316,35 @@ static bool find_service_for_handle(const void *data, const void *user_data)
 	return (start <= handle) && (handle <= end);
 }
 
-struct gatt_db_attribute *gatt_db_get_attribute(struct gatt_db *db,
+struct gatt_db_attribute *gatt_db_get_service(struct gatt_db *db,
 							uint16_t handle)
 {
 	struct gatt_db_service *service;
-	int i;
 
 	if (!db || !handle)
 		return NULL;
 
 	service = queue_find(db->services, find_service_for_handle,
-							UINT_TO_PTR(handle));
+						UINT_TO_PTR(handle));
 	if (!service)
 		return NULL;
 
+	return service->attributes[0];
+}
+
+struct gatt_db_attribute *gatt_db_get_attribute(struct gatt_db *db,
+							uint16_t handle)
+{
+	struct gatt_db_attribute *attrib;
+	struct gatt_db_service *service;
+	int i;
+
+	attrib = gatt_db_get_service(db, handle);
+	if (!attrib)
+		return NULL;
+
+	service = attrib->service;
+
 	for (i = 0; i < service->num_handles; i++) {
 		if (!service->attributes[i])
 			continue;
src/shared/gatt-db.h +3 -0
index 134ec63..f8277fd 100644
@@ -170,6 +170,9 @@ unsigned int gatt_db_register(struct gatt_db *db,
 					gatt_db_destroy_func_t destroy);
 bool gatt_db_unregister(struct gatt_db *db, unsigned int id);
 
+struct gatt_db_attribute *gatt_db_get_service(struct gatt_db *db,
+							uint16_t handle);
+
 struct gatt_db_attribute *gatt_db_get_attribute(struct gatt_db *db,
 							uint16_t handle);
 
Signature · 1 line
2.9.3
author maintainer
#2 lore raw

PATCH v2 2/10 shared/gatt-db: Add gatt_db_service_insert_included

In reply to Luiz Augusto von Dentz

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

This introduces gatt_db_service_insert_included which is a variant of gatt_db_service_add_included containing the handle where it should be inserted.

---
 src/shared/gatt-db.c | 40 ++++++++++++++++++++++++++++++++--------
 src/shared/gatt-db.h |  4 ++++
 2 files changed, 36 insertions(+), 8 deletions(-)
src/shared/gatt-db.c +32 -8
index efd61f9..2cb80ec 100644
@@ -834,19 +834,15 @@ gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib,
 					user_data);
 }
 
-struct gatt_db_attribute *
-gatt_db_service_add_included(struct gatt_db_attribute *attrib,
+static struct gatt_db_attribute *
+service_insert_included(struct gatt_db_service *service, uint16_t handle,
 					struct gatt_db_attribute *include)
 {
-	struct gatt_db_service *service, *included;
+	struct gatt_db_service *included;
 	uint8_t value[MAX_INCLUDED_VALUE_LEN];
 	uint16_t included_handle, len = 0;
 	int index;
 
-	if (!attrib || !include)
-		return NULL;
-
-	service = attrib->service;
 	included = include->service;
 
 	/* Adjust include to point to the first attribute */
@@ -873,7 +869,14 @@ gatt_db_service_add_included(struct gatt_db_attribute *attrib,
 	if (!index)
 		return NULL;
 
-	service->attributes[index] = new_attribute(service, 0,
+	/* Check if handle is in within service range */
+	if (handle && handle <= service->attributes[0]->handle)
+		return NULL;
+
+	if (!handle)
+		handle = get_handle_at_index(service, index - 1) + 1;
+
+	service->attributes[index] = new_attribute(service, handle,
 							&included_service_uuid,
 							value, len);
 	if (!service->attributes[index])
@@ -889,6 +892,27 @@ gatt_db_service_add_included(struct gatt_db_attribute *attrib,
 	return attribute_update(service, index);
 }
 
+struct gatt_db_attribute *
+gatt_db_service_add_included(struct gatt_db_attribute *attrib,
+					struct gatt_db_attribute *include)
+{
+	if (!attrib || !include)
+		return NULL;
+
+	return service_insert_included(attrib->service, 0, include);
+}
+
+struct gatt_db_attribute *
+gatt_db_service_insert_included(struct gatt_db_attribute *attrib,
+				uint16_t handle,
+				struct gatt_db_attribute *include)
+{
+	if (!attrib || !handle || !include)
+		return NULL;
+
+	return service_insert_included(attrib->service, handle, include);
+}
+
 bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active)
 {
 	struct gatt_db_service *service;
src/shared/gatt-db.h +4 -0
index f8277fd..19583e7 100644
@@ -96,6 +96,10 @@ gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
 struct gatt_db_attribute *
 gatt_db_service_add_included(struct gatt_db_attribute *attrib,
 					struct gatt_db_attribute *include);
+struct gatt_db_attribute *
+gatt_db_service_insert_included(struct gatt_db_attribute *attrib,
+				uint16_t handle,
+				struct gatt_db_attribute *include);
 
 bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active);
 bool gatt_db_service_get_active(struct gatt_db_attribute *attrib);
Signature · 1 line
2.9.3
author maintainer
#3 lore raw

PATCH v2 3/10 shared/gatt-client: Insert included attribute with handle

In reply to Luiz Augusto von Dentz

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

This ensures the local representation matches exactly the handle used by the remote in its database.

---
 src/shared/gatt-client.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/shared/gatt-client.c +1 -1
index 0134721..3caed5f 100644
@@ -457,7 +457,7 @@ static void discover_incl_cb(bool success, uint8_t att_ecode,
 		if (!tmp)
 			goto failed;
 
-		tmp = gatt_db_service_add_included(attr, tmp);
+		tmp = gatt_db_service_insert_included(attr, handle, tmp);
 		if (!tmp)
 			goto failed;
 
Signature · 1 line
2.9.3
author maintainer
#4 lore raw

PATCH v2 4/10 shared/gatt-db: Add gatt_db_insert_included

In reply to Luiz Augusto von Dentz

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

This introduces gatt_db_insert_included which is similar to gatt_db_service_insert_included but doesn't require the caller to know the service where the attribute should be inserted.

---
 src/shared/gatt-db.c | 13 +++++++++++++
 src/shared/gatt-db.h |  4 ++++
 2 files changed, 17 insertions(+)
src/shared/gatt-db.c +13 -0
index 2cb80ec..376e705 100644
@@ -913,6 +913,19 @@ gatt_db_service_insert_included(struct gatt_db_attribute *attrib,
 	return service_insert_included(attrib->service, handle, include);
 }
 
+struct gatt_db_attribute *
+gatt_db_insert_included(struct gatt_db *db, uint16_t handle,
+			struct gatt_db_attribute *include)
+{
+	struct gatt_db_attribute *attrib;
+
+	attrib = gatt_db_get_service(db, handle);
+	if (!attrib)
+		return NULL;
+
+	return service_insert_included(attrib->service, handle, include);
+}
+
 bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active)
 {
 	struct gatt_db_service *service;
src/shared/gatt-db.h +4 -0
index 19583e7..fa7897b 100644
@@ -94,6 +94,10 @@ gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
 					void *user_data);
 
 struct gatt_db_attribute *
+gatt_db_insert_included(struct gatt_db *db, uint16_t handle,
+			struct gatt_db_attribute *include);
+
+struct gatt_db_attribute *
 gatt_db_service_add_included(struct gatt_db_attribute *attrib,
 					struct gatt_db_attribute *include);
 struct gatt_db_attribute *
Signature · 1 line
2.9.3
author maintainer
#5 lore raw

PATCH v2 5/10 shared/gatt-client: Simplify included discovery

In reply to Luiz Augusto von Dentz

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

Instead of doing include per service in a depth-first fashion do it in breadth-first like its done for primary and secondary since it is now possible to insert the include handle, not just append at the end, it is not longer necessary to fetch the attribute in order to have included handle.

As a bonus this can also speed up the discovery procedure since it will result in less traffic over the air.

---
 src/shared/gatt-client.c | 130 +++++++++++++++--------------------------------
 1 file changed, 42 insertions(+), 88 deletions(-)
src/shared/gatt-client.c +42 -88
index 3caed5f..a5f3c18 100644
@@ -315,13 +315,14 @@ struct discovery_op {
 	struct bt_gatt_client *client;
 	struct queue *pending_svcs;
 	struct queue *pending_chrcs;
-	struct queue *svcs;
 	struct queue *ext_prop_desc;
 	struct gatt_db_attribute *cur_svc;
 	bool success;
 	uint16_t start;
 	uint16_t end;
 	uint16_t last;
+	uint16_t svc_first;
+	uint16_t svc_last;
 	int ref_count;
 	discovery_op_complete_func_t complete_func;
 	discovery_op_fail_func_t failure_func;
@@ -331,7 +332,6 @@ static void discovery_op_free(struct discovery_op *op)
 {
 	queue_destroy(op->pending_svcs, NULL);
 	queue_destroy(op->pending_chrcs, free);
-	queue_destroy(op->svcs, NULL);
 	queue_destroy(op->ext_prop_desc, NULL);
 	free(op);
 }
@@ -357,7 +357,6 @@ static struct discovery_op *discovery_op_create(struct bt_gatt_client *client,
 	op = new0(struct discovery_op, 1);
 	op->pending_svcs = queue_new();
 	op->pending_chrcs = queue_new();
-	op->svcs = queue_new();
 	op->ext_prop_desc = queue_new();
 	op->client = client;
 	op->complete_func = complete_func;
@@ -408,7 +407,7 @@ static void discover_incl_cb(bool success, uint8_t att_ecode,
 	struct discovery_op *op = user_data;
 	struct bt_gatt_client *client = op->client;
 	struct bt_gatt_iter iter;
-	struct gatt_db_attribute *attr, *tmp;
+	struct gatt_db_attribute *attr;
 	uint16_t handle, start, end;
 	uint128_t u128;
 	bt_uuid_t uuid;
@@ -424,11 +423,6 @@ static void discover_incl_cb(bool success, uint8_t att_ecode,
 		goto failed;
 	}
 
-	/* Get the currently processed service */
-	attr = op->cur_svc;
-	if (!attr)
-		goto failed;
-
 	if (!result || !bt_gatt_iter_init(&iter, result))
 		goto failed;
 
@@ -453,12 +447,12 @@ static void discover_incl_cb(bool success, uint8_t att_ecode,
 				"handle: 0x%04x, start: 0x%04x, end: 0x%04x,"
 				"uuid: %s", handle, start, end, uuid_str);
 
-		tmp = gatt_db_get_attribute(client->db, start);
-		if (!tmp)
+		attr = gatt_db_get_attribute(client->db, start);
+		if (!attr)
 			goto failed;
 
-		tmp = gatt_db_service_insert_included(attr, handle, tmp);
-		if (!tmp)
+		attr = gatt_db_insert_included(client->db, handle, attr);
+		if (!attr)
 			goto failed;
 
 		/*
@@ -467,62 +461,32 @@ static void discover_incl_cb(bool success, uint8_t att_ecode,
 		 * these entries, the correct handle must be assigned to the new
 		 * attribute.
 		 */
-		if (gatt_db_attribute_get_handle(tmp) != handle)
+		if (gatt_db_attribute_get_handle(attr) != handle)
 			goto failed;
 	}
 
 next:
 	/* Move on to the next service */
 	attr = queue_pop_head(op->pending_svcs);
-	if (!attr) {
-		/*
-		 * We have processed all include definitions. Move on to
-		 * characteristics.
-		 */
-		attr = queue_pop_head(op->svcs);
-		if (!attr)
-			goto failed;
-
-		if (!gatt_db_attribute_get_service_handles(attr, &start, &end))
-			goto failed;
-
-		op->cur_svc = attr;
-
-		client->discovery_req = bt_gatt_discover_characteristics(
-							client->att,
-							start, end,
-							discover_chrcs_cb,
-							discovery_op_ref(op),
-							discovery_op_unref);
-		if (client->discovery_req)
-			return;
-
-		util_debug(client->debug_callback, client->debug_data,
-				"Failed to start characteristic discovery");
-		discovery_op_unref(op);
+	if (!attr)
 		goto failed;
-	}
 
-	queue_push_tail(op->svcs, attr);
-	op->cur_svc = attr;
 	if (!gatt_db_attribute_get_service_handles(attr, &start, &end))
 		goto failed;
 
-	if (start == end)
-		goto next;
+	op->cur_svc = attr;
 
-	client->discovery_req = bt_gatt_discover_included_services(client->att,
+	client->discovery_req = bt_gatt_discover_characteristics(client->att,
 							start, end,
-							discover_incl_cb,
+							discover_chrcs_cb,
 							discovery_op_ref(op),
 							discovery_op_unref);
 	if (client->discovery_req)
 		return;
 
 	util_debug(client->debug_callback, client->debug_data,
-					"Failed to start included discovery");
+				"Failed to start characteristic discovery");
 	discovery_op_unref(op);
-
 failed:
 	discovery_op_complete(op, false, att_ecode);
 }
@@ -674,7 +638,7 @@ static void ext_prop_read_cb(bool success, uint8_t att_ecode,
 		/* Done with the current service */
 		gatt_db_service_set_active(op->cur_svc, true);
 
-		next_srv = queue_pop_head(op->svcs);
+		next_srv = queue_pop_head(op->pending_svcs);
 		if (!next_srv)
 			goto done;
 
@@ -782,7 +746,7 @@ next:
 	/* Done with the current service */
 	gatt_db_service_set_active(op->cur_svc, true);
 
-	attr = queue_pop_head(op->svcs);
+	attr = queue_pop_head(op->pending_svcs);
 	if (!attr)
 		goto done;
 
@@ -888,7 +852,7 @@ next:
 	/* Done with the current service */
 	gatt_db_service_set_active(op->cur_svc, true);
 
-	attr = queue_pop_head(op->svcs);
+	attr = queue_pop_head(op->pending_svcs);
 	if (!attr)
 		goto done;
 
@@ -920,6 +884,26 @@ done:
 	discovery_op_complete(op, success, att_ecode);
 }
 
+static void discovery_found_service(struct discovery_op *op,
+					struct gatt_db_attribute *attr,
+					uint16_t start, uint16_t end)
+{
+	/* Skip if service already active */
+	if (!gatt_db_service_get_active(attr)) {
+		queue_push_tail(op->pending_svcs, attr);
+
+		/* Update discovery range */
+		if (!op->svc_first || op->svc_first > start)
+			op->svc_first = start;
+		if (op->svc_last < end)
+			op->svc_last = end;
+	}
+
+	/* Update last handle */
+	if (end > op->last)
+		op->last = end;
+}
+
 static void discover_secondary_cb(bool success, uint8_t att_ecode,
 						struct bt_gatt_result *result,
 						void *user_data)
@@ -984,39 +968,14 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode,
 			op->last = end;
 		}
 
-		/* Skip if service already active */
-		if (!gatt_db_service_get_active(attr))
-			queue_push_tail(op->pending_svcs, attr);
-
-		/* Update last handle */
-		if (end > op->last)
-			op->last = end;
+		/* Update pending list */
+		discovery_found_service(op, attr, start, end);
 	}
 
 next:
-	/* Sequentially discover included services */
-	attr = queue_pop_head(op->pending_svcs);
-
-	/* Complete with success if queue is empty */
-	if (!attr) {
-		success = true;
-		goto done;
-	}
-
-	/*
-	 * Store the service in the svcs queue to be reused during
-	 * characteristics discovery later.
-	 */
-	queue_push_tail(op->svcs, attr);
-	op->cur_svc = attr;
-
-	if (!gatt_db_attribute_get_service_handles(attr, &start, &end)) {
-		success = false;
-		goto done;
-	}
-
 	client->discovery_req = bt_gatt_discover_included_services(client->att,
-							start, end,
+							op->svc_first,
+							op->svc_last,
 							discover_incl_cb,
 							discovery_op_ref(op),
 							discovery_op_unref);
@@ -1097,13 +1056,8 @@ static void discover_primary_cb(bool success, uint8_t att_ecode,
 			op->last = end;
 		}
 
-		/* Skip if service already active */
-		if (!gatt_db_service_get_active(attr))
-			queue_push_tail(op->pending_svcs, attr);
-
-		/* Update last handle */
-		if (end > op->last)
-			op->last = end;
+		/* Update pending list */
+		discovery_found_service(op, attr, start, end);
 	}
 
 secondary:
Signature · 1 line
2.9.3
author maintainer
#6 lore raw

PATCH v2 6/10 unit/test-gatt: Adjust the PDUs to the new behavior

In reply to Luiz Augusto von Dentz

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

The new behavior of bt_gatt_client is to discovery the entire range (0x0001-0xffff) not service by service.

---
 unit/test-gatt.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
unit/test-gatt.c +6 -14
index e0d29c7..3a784e9 100644
@@ -134,10 +134,8 @@ struct context {
 		raw_pdu(0x01, 0x10, 0x09, 0x00, 0x0a),			\
 		raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x01, 0x28),	\
 		raw_pdu(0x01, 0x10, 0x01, 0x00, 0x0a),			\
-		raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x02, 0x28),	\
+		raw_pdu(0x08, 0x01, 0x00, 0x08, 0x00, 0x02, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),			\
-		raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x02, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),			\
 		raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x02, 0x00, 0x02, 0x03, 0x00, 0x00,	\
 				0x2a),					\
@@ -163,10 +161,8 @@ struct context {
 		raw_pdu(0x01, 0x10, 0x0b, 0x00, 0x0a),			\
 		raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x01, 0x28),	\
 		raw_pdu(0x01, 0x10, 0x01, 0x00, 0x0a),			\
-		raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x02, 0x28),	\
+		raw_pdu(0x08, 0x01, 0x00, 0x0a, 0x00, 0x02, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),			\
-		raw_pdu(0x08, 0x05, 0x00, 0x0a, 0x00, 0x02, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),			\
 		raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x02, 0x00, 0x02, 0x03, 0x00, 0x00,	\
 				0x2a),					\
@@ -193,10 +189,8 @@ struct context {
 		raw_pdu(0x01, 0x10, 0x21, 0x03, 0x0a),			\
 		raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x01, 0x28),	\
 		raw_pdu(0x01, 0x10, 0x01, 0x00, 0x0a),			\
-		raw_pdu(0x08, 0x00, 0x01, 0x21, 0x01, 0x02, 0x28),	\
+		raw_pdu(0x08, 0x00, 0x01, 0x20, 0x03, 0x02, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x00, 0x01, 0x0a),			\
-		raw_pdu(0x08, 0x00, 0x03, 0x20, 0x03, 0x02, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x00, 0x03, 0x0a),			\
 		raw_pdu(0x08, 0x00, 0x01, 0x21, 0x01, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x10, 0x01, 0x02, 0x11, 0x01, 0x00,	\
 			0x2a, 0x20, 0x01, 0x02, 0x21, 0x01, 0x01, 0x2a),\
@@ -249,13 +243,11 @@ struct context {
 		raw_pdu(0x01, 0x10, 0x11, 0x00, 0x0a)
 
 #define INCLUDE_DISC_SMALL_DB						\
-		raw_pdu(0x08, 0x10, 0xf0, 0x18, 0xf0, 0x02, 0x28),	\
+		raw_pdu(0x08, 0x01, 0x00, 0xff, 0xff, 0x02, 0x28),	\
 		raw_pdu(0x09, 0x08, 0x11, 0xf0, 0x01, 0x00, 0x0f, 0x00,	\
 			0x0a, 0x18),					\
-		raw_pdu(0x08, 0x12, 0xf0, 0x18, 0xf0, 0x02, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x12, 0xf0, 0x0a),			\
-		raw_pdu(0x08, 0x01, 0x00, 0x10, 0x00, 0x02, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a)
+		raw_pdu(0x08, 0x12, 0xf0, 0xff, 0xff, 0x02, 0x28),	\
+		raw_pdu(0x01, 0x08, 0x12, 0xf0, 0x0a)
 
 #define CHARACTERISTIC_DISC_SMALL_DB					\
 		raw_pdu(0x08, 0x10, 0xf0, 0x18, 0xf0, 0x03, 0x28),	\
Signature · 1 line
2.9.3
author maintainer
#7 lore raw

PATCH v2 7/10 shared/gatt-db: Add gatt_db_insert_characteristic

In reply to Luiz Augusto von Dentz

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

This introduces gatt_db_insert_characteristic which is similar to gatt_db_service_insert_charactersitic but doesn't require the caller to know the service where the attribute should be inserted.

---
 src/shared/gatt-db.c | 22 ++++++++++++++++++++++
 src/shared/gatt-db.h | 10 ++++++++++
 2 files changed, 32 insertions(+)
src/shared/gatt-db.c +22 -0
index 376e705..39892ee 100644
@@ -733,6 +733,28 @@ service_insert_characteristic(struct gatt_db_service *service,
 }
 
 struct gatt_db_attribute *
+gatt_db_insert_characteristic(struct gatt_db *db,
+					uint16_t handle,
+					const bt_uuid_t *uuid,
+					uint32_t permissions,
+					uint8_t properties,
+					gatt_db_read_t read_func,
+					gatt_db_write_t write_func,
+					void *user_data)
+{
+	struct gatt_db_attribute *attrib;
+
+	attrib = gatt_db_get_service(db, handle);
+	if (!attrib)
+		return NULL;
+
+	return service_insert_characteristic(attrib->service, handle, uuid,
+						permissions, properties,
+						read_func, write_func,
+						user_data);
+}
+
+struct gatt_db_attribute *
 gatt_db_service_insert_characteristic(struct gatt_db_attribute *attrib,
 					uint16_t handle,
 					const bt_uuid_t *uuid,
src/shared/gatt-db.h +10 -0
index fa7897b..6c1ce7a 100644
@@ -78,6 +78,16 @@ gatt_db_service_insert_characteristic(struct gatt_db_attribute *attrib,
 					void *user_data);
 
 struct gatt_db_attribute *
+gatt_db_insert_characteristic(struct gatt_db *db,
+					uint16_t handle,
+					const bt_uuid_t *uuid,
+					uint32_t permissions,
+					uint8_t properties,
+					gatt_db_read_t read_func,
+					gatt_db_write_t write_func,
+					void *user_data);
+
+struct gatt_db_attribute *
 gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib,
 					const bt_uuid_t *uuid,
 					uint32_t permissions,
Signature · 1 line
2.9.3
author maintainer
#8 lore raw

PATCH v2 8/10 shared/gatt-db: Add gatt_db_insert_descriptor

In reply to Luiz Augusto von Dentz

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

This introduces gatt_db_insert_descriptor which is similar to gatt_db_service_insert_descriptor but doesn't require the caller to know the service where the attribute should be inserted.

---
 src/shared/gatt-db.c | 20 ++++++++++++++++++++
 src/shared/gatt-db.h |  9 +++++++++
 2 files changed, 29 insertions(+)
src/shared/gatt-db.c +20 -0
index 39892ee..dc72014 100644
@@ -824,6 +824,26 @@ service_insert_descriptor(struct gatt_db_service *service,
 }
 
 struct gatt_db_attribute *
+gatt_db_insert_descriptor(struct gatt_db *db,
+					uint16_t handle,
+					const bt_uuid_t *uuid,
+					uint32_t permissions,
+					gatt_db_read_t read_func,
+					gatt_db_write_t write_func,
+					void *user_data)
+{
+	struct gatt_db_attribute *attrib;
+
+	attrib = gatt_db_get_service(db, handle);
+	if (!attrib)
+		return NULL;
+
+	return service_insert_descriptor(attrib->service, handle, uuid,
+					permissions, read_func, write_func,
+					user_data);
+}
+
+struct gatt_db_attribute *
 gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
 					uint16_t handle,
 					const bt_uuid_t *uuid,
src/shared/gatt-db.h +9 -0
index 6c1ce7a..f4ec51c 100644
@@ -88,6 +88,15 @@ gatt_db_insert_characteristic(struct gatt_db *db,
 					void *user_data);
 
 struct gatt_db_attribute *
+gatt_db_insert_descriptor(struct gatt_db *db,
+					uint16_t handle,
+					const bt_uuid_t *uuid,
+					uint32_t permissions,
+					gatt_db_read_t read_func,
+					gatt_db_write_t write_func,
+					void *user_data);
+
+struct gatt_db_attribute *
 gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib,
 					const bt_uuid_t *uuid,
 					uint32_t permissions,
Signature · 1 line
2.9.3
author maintainer
#9 lore raw

PATCH v2 9/10 shared/gatt-client: Simplify characteristics discovery

In reply to Luiz Augusto von Dentz

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

Instead of discovering characteristics per service in a depth-first fashion do it in breadth-first like its done for primary, secondary and included.

As a bonus this can also speed up the discovery procedure since it will result in less traffic over the air.

---
 src/shared/gatt-client.c | 139 ++++++++++++++---------------------------------
 1 file changed, 41 insertions(+), 98 deletions(-)
src/shared/gatt-client.c +41 -98
index a5f3c18..7557600 100644
@@ -466,18 +466,9 @@ static void discover_incl_cb(bool success, uint8_t att_ecode,
 	}
 
 next:
-	/* Move on to the next service */
-	attr = queue_pop_head(op->pending_svcs);
-	if (!attr)
-		goto failed;
-
-	if (!gatt_db_attribute_get_service_handles(attr, &start, &end))
-		goto failed;
-
-	op->cur_svc = attr;
-
 	client->discovery_req = bt_gatt_discover_characteristics(client->att,
-							start, end,
+							op->svc_first,
+							op->svc_last,
 							discover_chrcs_cb,
 							discovery_op_ref(op),
 							discovery_op_unref);
@@ -513,7 +504,10 @@ static bool discover_descs(struct discovery_op *op, bool *discovering)
 	*discovering = false;
 
 	while ((chrc_data = queue_pop_head(op->pending_chrcs))) {
-		attr = gatt_db_service_insert_characteristic(op->cur_svc,
+		struct gatt_db_attribute *svc;
+		uint16_t start, end;
+
+		attr = gatt_db_insert_characteristic(client->db,
 							chrc_data->value_handle,
 							&chrc_data->uuid, 0,
 							chrc_data->properties,
@@ -530,6 +524,25 @@ static bool discover_descs(struct discovery_op *op, bool *discovering)
 							chrc_data->value_handle)
 			goto failed;
 
+		/* Adjust current service */
+		svc = gatt_db_get_service(client->db, chrc_data->value_handle);
+		if (op->cur_svc != svc) {
+			queue_remove(op->pending_svcs, svc);
+
+			/* Done with the current service */
+			gatt_db_service_set_active(op->cur_svc, true);
+			op->cur_svc = svc;
+		}
+
+		gatt_db_attribute_get_service_handles(svc, &start, &end);
+
+		/*
+		 * Ajust end_handle in case the next chrc is not within the
+		 * same service.
+		 */
+		if (chrc_data->end_handle > end)
+			chrc_data->end_handle = end;
+
 		/*
 		 * check for descriptors presence, before initializing the
 		 * desc_handle and avoid integer overflow during desc_handle
@@ -539,6 +552,7 @@ static bool discover_descs(struct discovery_op *op, bool *discovering)
 			free(chrc_data);
 			continue;
 		}
+
 		desc_start = chrc_data->value_handle + 1;
 
 		client->discovery_req = bt_gatt_discover_descriptors(
@@ -609,8 +623,6 @@ static void ext_prop_read_cb(bool success, uint8_t att_ecode,
 	struct bt_gatt_client *client = op->client;
 	bool discovering;
 	struct gatt_db_attribute *desc_attr = NULL;
-	struct gatt_db_attribute *next_srv;
-	uint16_t start, end;
 
 	util_debug(client->debug_callback, client->debug_data,
 				"Ext. prop value: 0x%04x", (uint16_t)value[0]);
@@ -627,42 +639,16 @@ static void ext_prop_read_cb(bool success, uint8_t att_ecode,
 	if (read_ext_prop_desc(op))
 		return;
 
-	/* Continue with discovery */
-	do {
-		if (!discover_descs(op, &discovering))
-			goto failed;
-
-		if (discovering)
-			return;
-
-		/* Done with the current service */
-		gatt_db_service_set_active(op->cur_svc, true);
-
-		next_srv = queue_pop_head(op->pending_svcs);
-		if (!next_srv)
-			goto done;
-
-		if (!gatt_db_attribute_get_service_handles(next_srv, &start,
-									&end))
+	if (!discover_descs(op, &discovering))
 			goto failed;
 
-	} while (start == end);
-
-	/* Move on to the next service */
-	op->cur_svc = next_srv;
-
-	client->discovery_req = bt_gatt_discover_characteristics(client->att,
-							start, end,
-							discover_chrcs_cb,
-							discovery_op_ref(op),
-							discovery_op_unref);
-	if (client->discovery_req)
+	if (discovering)
 		return;
 
-	util_debug(client->debug_callback, client->debug_data,
-				"Failed to start characteristic discovery");
+	/* Done with the current service */
+	gatt_db_service_set_active(op->cur_svc, true);
 
-	discovery_op_unref(op);
+	goto done;
 
 failed:
 	success = false;
@@ -679,7 +665,7 @@ static void discover_descs_cb(bool success, uint8_t att_ecode,
 	struct bt_gatt_client *client = op->client;
 	struct bt_gatt_iter iter;
 	struct gatt_db_attribute *attr;
-	uint16_t handle, start, end;
+	uint16_t handle;
 	uint128_t u128;
 	bt_uuid_t uuid;
 	char uuid_str[MAX_LEN_UUID_STR];
@@ -719,11 +705,15 @@ static void discover_descs_cb(bool success, uint8_t att_ecode,
 						"handle: 0x%04x, uuid: %s",
 						handle, uuid_str);
 
-		attr = gatt_db_service_insert_descriptor(op->cur_svc, handle,
+		attr = gatt_db_insert_descriptor(client->db, handle,
 							&uuid, 0, NULL, NULL,
 							NULL);
-		if (!attr)
+		if (!attr) {
+			util_debug(client->debug_callback, client->debug_data,
+				"Failed to insert descriptor at 0x%04x",
+				handle);
 			goto failed;
+		}
 
 		if (gatt_db_attribute_get_handle(attr) != handle)
 			goto failed;
@@ -746,30 +736,7 @@ next:
 	/* Done with the current service */
 	gatt_db_service_set_active(op->cur_svc, true);
 
-	attr = queue_pop_head(op->pending_svcs);
-	if (!attr)
-		goto done;
-
-	if (!gatt_db_attribute_get_service_handles(attr, &start, &end))
-		goto failed;
-
-	if (start == end)
-		goto next;
-
-	/* Move on to the next service */
-	op->cur_svc = attr;
-
-	client->discovery_req = bt_gatt_discover_characteristics(client->att,
-							start, end,
-							discover_chrcs_cb,
-							discovery_op_ref(op),
-							discovery_op_unref);
-	if (client->discovery_req)
-		return;
-
-	util_debug(client->debug_callback, client->debug_data,
-				"Failed to start characteristic discovery");
-	discovery_op_unref(op);
+	goto done;
 
 failed:
 	success = false;
@@ -785,7 +752,6 @@ static void discover_chrcs_cb(bool success, uint8_t att_ecode,
 	struct discovery_op *op = user_data;
 	struct bt_gatt_client *client = op->client;
 	struct bt_gatt_iter iter;
-	struct gatt_db_attribute *attr;
 	struct chrc *chrc_data;
 	uint16_t start, end, value;
 	uint8_t properties;
@@ -806,7 +772,7 @@ static void discover_chrcs_cb(bool success, uint8_t att_ecode,
 		goto done;
 	}
 
-	if (!op->cur_svc || !result || !bt_gatt_iter_init(&iter, result))
+	if (!result || !bt_gatt_iter_init(&iter, result))
 		goto failed;
 
 	chrc_count = bt_gatt_result_characteristic_count(result);
@@ -852,30 +818,7 @@ next:
 	/* Done with the current service */
 	gatt_db_service_set_active(op->cur_svc, true);
 
-	attr = queue_pop_head(op->pending_svcs);
-	if (!attr)
-		goto done;
-
-	if (!gatt_db_attribute_get_service_handles(attr, &start, &end))
-		goto failed;
-
-	if (start == end)
-		goto next;
-
-	/* Move on to the next service */
-	op->cur_svc = attr;
-
-	client->discovery_req = bt_gatt_discover_characteristics(client->att,
-							start, end,
-							discover_chrcs_cb,
-							discovery_op_ref(op),
-							discovery_op_unref);
-	if (client->discovery_req)
-		return;
-
-	util_debug(client->debug_callback, client->debug_data,
-				"Failed to start characteristic discovery");
-	discovery_op_unref(op);
+	goto done;
 
 failed:
 	success = false;
Signature · 1 line
2.9.3
author maintainer
#10 lore raw

PATCH v2 10/10 unit/test-gatt: Adjust the PDUs for characteristic discovery

In reply to Luiz Augusto von Dentz

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

The new behavior of bt_gatt_client is to discovery the characteristics of the entire range (0x0001-0xffff) not service by service.

---
 unit/test-gatt.c | 60 +++++++++++++++++++++++++-------------------------------
 1 file changed, 27 insertions(+), 33 deletions(-)
unit/test-gatt.c +27 -33
index 3a784e9..15638dc 100644
@@ -136,18 +136,16 @@ struct context {
 		raw_pdu(0x01, 0x10, 0x01, 0x00, 0x0a),			\
 		raw_pdu(0x08, 0x01, 0x00, 0x08, 0x00, 0x02, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),			\
-		raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x01, 0x00, 0x08, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x02, 0x00, 0x02, 0x03, 0x00, 0x00,	\
 				0x2a),					\
-		raw_pdu(0x08, 0x03, 0x00, 0x04, 0x00, 0x03, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x03, 0x00, 0x0a),			\
-		raw_pdu(0x04, 0x04, 0x00, 0x04, 0x00),			\
-		raw_pdu(0x05, 0x01, 0x04, 0x00, 0x01, 0x29),		\
-		raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x03, 0x00, 0x08, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x06, 0x00, 0x0a, 0x07, 0x00, 0x29,	\
 				0x2a),					\
 		raw_pdu(0x08, 0x07, 0x00, 0x08, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x07, 0x00, 0x0a),			\
+		raw_pdu(0x04, 0x04, 0x00, 0x04, 0x00),			\
+		raw_pdu(0x05, 0x01, 0x04, 0x00, 0x01, 0x29),		\
 		raw_pdu(0x04, 0x08, 0x00, 0x08, 0x00),			\
 		raw_pdu(0x05, 0x01, 0x08, 0x00, 0x01, 0x29)
 
@@ -163,18 +161,16 @@ struct context {
 		raw_pdu(0x01, 0x10, 0x01, 0x00, 0x0a),			\
 		raw_pdu(0x08, 0x01, 0x00, 0x0a, 0x00, 0x02, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),			\
-		raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x01, 0x00, 0x0a, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x02, 0x00, 0x02, 0x03, 0x00, 0x00,	\
 				0x2a),					\
-		raw_pdu(0x08, 0x03, 0x00, 0x04, 0x00, 0x03, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x03, 0x00, 0x0a),			\
-		raw_pdu(0x04, 0x04, 0x00, 0x04, 0x00),			\
-		raw_pdu(0x05, 0x01, 0x04, 0x00, 0x01, 0x29),		\
-		raw_pdu(0x08, 0x05, 0x00, 0x0a, 0x00, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x03, 0x00, 0x0a, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x07, 0x00, 0x0a, 0x08, 0x00, 0x29,	\
 				0x2a),					\
 		raw_pdu(0x08, 0x08, 0x00, 0x0a, 0x00, 0x03, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x08, 0x00, 0x0a),			\
+		raw_pdu(0x04, 0x04, 0x00, 0x04, 0x00),			\
+		raw_pdu(0x05, 0x01, 0x04, 0x00, 0x01, 0x29),		\
 		raw_pdu(0x04, 0x09, 0x00, 0x0a, 0x00),			\
 		raw_pdu(0x05, 0x01, 0x0a, 0x00, 0x01, 0x29)
 
@@ -191,18 +187,16 @@ struct context {
 		raw_pdu(0x01, 0x10, 0x01, 0x00, 0x0a),			\
 		raw_pdu(0x08, 0x00, 0x01, 0x20, 0x03, 0x02, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x00, 0x01, 0x0a),			\
-		raw_pdu(0x08, 0x00, 0x01, 0x21, 0x01, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x00, 0x01, 0x20, 0x03, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x10, 0x01, 0x02, 0x11, 0x01, 0x00,	\
 			0x2a, 0x20, 0x01, 0x02, 0x21, 0x01, 0x01, 0x2a),\
-		raw_pdu(0x08, 0x21, 0x01, 0x21, 0x01, 0x03, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x21, 0x01, 0x0a),			\
-		raw_pdu(0x04, 0x12, 0x01, 0x1f, 0x01),			\
-		raw_pdu(0x01, 0x04, 0x12, 0x01, 0x0a),			\
-		raw_pdu(0x08, 0x00, 0x03, 0x20, 0x03, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x21, 0x01, 0x20, 0x03, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x10, 0x03, 0x0a, 0x11, 0x03, 0x29,	\
 			0x2a),						\
 		raw_pdu(0x08, 0x11, 0x03, 0x20, 0x03, 0x03, 0x28),	\
 		raw_pdu(0x01, 0x08, 0x11, 0x03, 0x0a),			\
+		raw_pdu(0x04, 0x12, 0x01, 0x1f, 0x01),			\
+		raw_pdu(0x01, 0x04, 0x12, 0x01, 0x0a),			\
 		raw_pdu(0x04, 0x12, 0x03, 0x20, 0x03),			\
 		raw_pdu(0x05, 0x01, 0x20, 0x03, 0x02, 0x29)
 
@@ -250,34 +244,32 @@ struct context {
 		raw_pdu(0x01, 0x08, 0x12, 0xf0, 0x0a)
 
 #define CHARACTERISTIC_DISC_SMALL_DB					\
-		raw_pdu(0x08, 0x10, 0xf0, 0x18, 0xf0, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x01, 0x00, 0xff, 0xff, 0x03, 0x28),	\
+		raw_pdu(0x09, 0x07, 0x02, 0x00, 0xb2, 0x03, 0x00, 0x29,	\
+			0x2a),						\
+		raw_pdu(0x08, 0x03, 0x00, 0xff, 0xff, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x12, 0xf0, 0x02, 0x13, 0xf0, 0x00,	\
 			0x2a),						\
-		raw_pdu(0x08, 0x13, 0xf0, 0x18, 0xf0, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x13, 0xf0, 0xff, 0xff, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x15, 0x14, 0xf0, 0x82, 0x15, 0xf0, 0xef,	\
 			0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x00,	\
 			0x00, 0x00, 0x00, 0x09, 0xB0, 0x00, 0x00),	\
-		raw_pdu(0x08, 0x15, 0xf0, 0x18, 0xf0, 0x03, 0x28),	\
+		raw_pdu(0x08, 0x15, 0xf0, 0xff, 0xff, 0x03, 0x28),	\
 		raw_pdu(0x09, 0x07, 0x17, 0xf0, 0x02, 0x18, 0xf0, 0x01,	\
 			0x2a),						\
-		raw_pdu(0x08, 0x18, 0xf0, 0x18, 0xf0, 0x03, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x18, 0xf0, 0x0a),			\
-		raw_pdu(0x04, 0x16, 0xf0, 0x16, 0xf0),			\
-		raw_pdu(0x05, 0x01, 0x16, 0xf0, 0x00, 0x29),		\
-		raw_pdu(0x0a, 0x16, 0xf0),				\
-		raw_pdu(0x0b, 0x01, 0x00),				\
-		raw_pdu(0x08, 0x01, 0x00, 0x10, 0x00, 0x03, 0x28),	\
-		raw_pdu(0x09, 0x07, 0x02, 0x00, 0xb2, 0x03, 0x00, 0x29,	\
-			0x2a),						\
-		raw_pdu(0x08, 0x03, 0x00, 0x10, 0x00, 0x03, 0x28),	\
-		raw_pdu(0x01, 0x08, 0x03, 0x00, 0x0a)
+		raw_pdu(0x08, 0x18, 0xf0, 0xff, 0xff, 0x03, 0x28),	\
+		raw_pdu(0x01, 0x08, 0x18, 0xf0, 0x0a)
 
 #define DESCRIPTOR_DISC_SMALL_DB					\
 		raw_pdu(0x04, 0x04, 0x00, 0x10, 0x00),			\
 		raw_pdu(0x05, 0x01, 0x04, 0x00, 0x02, 0x29, 0x05, 0x00,	\
 			0x01, 0x29),					\
 		raw_pdu(0x04, 0x06, 0x00, 0x10, 0x00),			\
-		raw_pdu(0x01, 0x04, 0x06, 0x00, 0x0a)
+		raw_pdu(0x01, 0x04, 0x06, 0x00, 0x0a),			\
+		raw_pdu(0x04, 0x16, 0xf0, 0x16, 0xf0),			\
+		raw_pdu(0x05, 0x01, 0x16, 0xf0, 0x00, 0x29),		\
+		raw_pdu(0x0a, 0x16, 0xf0),				\
+		raw_pdu(0x0b, 0x01, 0x00)
 
 #define SMALL_DB_DISCOVERY_PDUS						\
 		PRIMARY_DISC_SMALL_DB,					\
@@ -450,6 +442,8 @@ static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
 
 	util_hexdump('>', buf, len, test_debug, "GATT: ");
 
+	util_hexdump('=', pdu->data, pdu->size, test_debug, "PDU: ");
+
 	g_assert_cmpint(len, ==, pdu->size);
 
 	g_assert(memcmp(buf, pdu->data, pdu->size) == 0);
Signature · 1 line
2.9.3