Skip to contentSkip to conversation

shared/gatt-db: Introduce gatt_db_service_add_ccc

PATCH In review Luiz Augusto von Dentz 2 participants 6 messages Jan 21, 2022 – Jan 28, 2022 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/4 shared/gatt-db: Introduce gatt_db_service_add_ccc

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

This adds gatt_db_service_add_ccc so the likes of plugins can use it with the defaults callbacks which is useful for tracking their states.

---
 src/shared/gatt-db.c | 53 ++++++++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-db.h |  7 ++++++
 2 files changed, 60 insertions(+)
src/shared/gatt-db.c +53 -0
index 3a02289ce..12ff5badb 100644
@@ -44,6 +44,12 @@ static const bt_uuid_t included_service_uuid = { .type = BT_UUID16,
 static const bt_uuid_t ext_desc_uuid = { .type = BT_UUID16,
 				.value.u16 = GATT_CHARAC_EXT_PROPER_UUID };
 
+struct gatt_db_ccc {
+	gatt_db_read_t read_func;
+	gatt_db_write_t write_func;
+	void *user_data;
+};
+
 struct gatt_db {
 	int ref_count;
 	struct bt_crypto *crypto;
@@ -57,6 +63,8 @@ struct gatt_db {
 
 	gatt_db_authorize_cb_t authorize;
 	void *authorize_data;
+
+	struct gatt_db_ccc *ccc;
 };
 
 struct notify {
@@ -444,6 +452,7 @@ static void gatt_db_destroy(struct gatt_db *db)
 		timeout_remove(db->hash_id);
 
 	queue_destroy(db->services, gatt_db_service_destroy);
+	free(db->ccc);
 	free(db);
 }
 
@@ -1038,6 +1047,50 @@ gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib,
 					user_data);
 }
 
+struct gatt_db_attribute *
+gatt_db_service_add_ccc(struct gatt_db_attribute *attrib, uint32_t permissions)
+{
+	struct gatt_db *db;
+	struct gatt_db_attribute *ccc;
+	bt_uuid_t uuid;
+
+	if (!attrib)
+		return NULL;
+
+	db = attrib->service->db;
+
+	if (!db->ccc)
+		return NULL;
+
+	bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);
+
+	ccc = service_insert_descriptor(attrib->service, 0, &uuid,
+					permissions,
+					db->ccc->read_func,
+					db->ccc->write_func,
+					db->ccc->user_data);
+	if (!ccc)
+		return ccc;
+
+	gatt_db_attribute_set_fixed_length(ccc, 2);
+
+	return ccc;
+}
+
+void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func,
+				gatt_db_write_t write_func, void *user_data)
+{
+	if (!db)
+		return;
+
+	if (!db->ccc)
+		db->ccc = new0(struct gatt_db_ccc, 1);
+
+	db->ccc->read_func = read_func;
+	db->ccc->write_func = write_func;
+	db->ccc->user_data = user_data;
+}
+
 static struct gatt_db_attribute *
 service_insert_included(struct gatt_db_service *service, uint16_t handle,
 					struct gatt_db_attribute *include)
src/shared/gatt-db.h +7 -0
index 321a2aba6..3de22403c 100644
@@ -92,6 +92,7 @@ gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib,
 					gatt_db_read_t read_func,
 					gatt_db_write_t write_func,
 					void *user_data);
+
 struct gatt_db_attribute *
 gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
 					uint16_t handle,
@@ -101,6 +102,9 @@ gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib,
 					gatt_db_write_t write_func,
 					void *user_data);
 
+struct gatt_db_attribute *
+gatt_db_service_add_ccc(struct gatt_db_attribute *attrib, uint32_t permissions);
+
 struct gatt_db_attribute *
 gatt_db_insert_included(struct gatt_db *db, uint16_t handle,
 			struct gatt_db_attribute *include);
@@ -192,6 +196,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);
 
+void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func,
+				gatt_db_write_t write_func, void *user_data);
+
 typedef uint8_t (*gatt_db_authorize_cb_t)(struct gatt_db_attribute *attrib,
 					uint8_t opcode, struct bt_att *att,
 					void *user_data);
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 2/4 gatt: Make use of gatt_db_service_add_ccc

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

This uses gatt_db_service_add_ccc and gatt_db_ccc_register so any ccc registered with the use of the former gets proper state tracking.

---
 src/gatt-database.c | 62 ++++++++++++++++++++-------------------------
 1 file changed, 28 insertions(+), 34 deletions(-)
src/gatt-database.c +28 -34
index dbe9415a3..25641da8a 100644
@@ -1018,13 +1018,6 @@ static void gatt_ccc_write_cb(struct gatt_db_attribute *attrib,
 		goto done;
 	}
 
-	ccc_cb = queue_find(database->ccc_callbacks, ccc_cb_match_handle,
-			UINT_TO_PTR(gatt_db_attribute_get_handle(attrib)));
-	if (!ccc_cb) {
-		ecode = BT_ATT_ERROR_UNLIKELY;
-		goto done;
-	}
-
 	if (len == 1)
 		val = *value;
 	else
@@ -1034,7 +1027,9 @@ static void gatt_ccc_write_cb(struct gatt_db_attribute *attrib,
 	if (val == ccc->value)
 		goto done;
 
-	if (ccc_cb->callback) {
+	ccc_cb = queue_find(database->ccc_callbacks, ccc_cb_match_handle,
+			UINT_TO_PTR(gatt_db_attribute_get_handle(attrib)));
+	if (ccc_cb) {
 		struct pending_op *op;
 
 		op = pending_ccc_new(att, attrib, val,
@@ -1056,6 +1051,22 @@ done:
 	gatt_db_attribute_write_result(attrib, id, ecode);
 }
 
+static void ccc_add_cb(struct btd_gatt_database *database,
+			struct gatt_db_attribute *ccc,
+			btd_gatt_database_ccc_write_t callback,
+			void *user_data, btd_gatt_database_destroy_t destroy)
+{
+	struct ccc_cb_data *ccc_cb;
+
+	ccc_cb = new0(struct ccc_cb_data, 1);
+	ccc_cb->handle = gatt_db_attribute_get_handle(ccc);
+	ccc_cb->callback = callback;
+	ccc_cb->destroy = destroy;
+	ccc_cb->user_data = user_data;
+
+	queue_push_tail(database->ccc_callbacks, ccc_cb);
+}
+
 static struct gatt_db_attribute *
 service_add_ccc(struct gatt_db_attribute *service,
 				struct btd_gatt_database *database,
@@ -1064,34 +1075,14 @@ service_add_ccc(struct gatt_db_attribute *service,
 				btd_gatt_database_destroy_t destroy)
 {
 	struct gatt_db_attribute *ccc;
-	struct ccc_cb_data *ccc_cb;
-	bt_uuid_t uuid;
-
-	ccc_cb = new0(struct ccc_cb_data, 1);
-
-	/*
-	 * Provide a way for the permissions on a characteristic to dictate
-	 * the permissions on the CCC
-	 */
-	perm |= BT_ATT_PERM_READ | BT_ATT_PERM_WRITE;
-
-	bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);
-	ccc = gatt_db_service_add_descriptor(service, &uuid, perm,
-				gatt_ccc_read_cb, gatt_ccc_write_cb, database);
-	if (!ccc) {
-		error("Failed to create CCC entry in database");
-		free(ccc_cb);
-		return NULL;
-	}
-
-	gatt_db_attribute_set_fixed_length(ccc, 2);
 
-	ccc_cb->handle = gatt_db_attribute_get_handle(ccc);
-	ccc_cb->callback = write_callback;
-	ccc_cb->destroy = destroy;
-	ccc_cb->user_data = user_data;
+	ccc = gatt_db_service_add_ccc(service, perm);
+	if (!ccc)
+		return ccc;
 
-	queue_push_tail(database->ccc_callbacks, ccc_cb);
+	/* Only add ccc_cb if callback is set */
+	if (write_callback)
+		ccc_add_cb(database, ccc, write_callback, user_data, destroy);
 
 	return ccc;
 }
@@ -1310,6 +1301,9 @@ static void populate_devinfo_service(struct btd_gatt_database *database)
 
 static void register_core_services(struct btd_gatt_database *database)
 {
+	gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb,
+								database);
+
 	populate_gap_service(database);
 	populate_gatt_service(database);
 	populate_devinfo_service(database);
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH 3/4 shared/gatt-db: Introduce gatt_db_attribute_notify

In reply to Luiz Augusto von Dentz

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

This introduces gatt_db_attribute_notify which can be used to trigger a notification using the callback set by gatt_db_ccc_register.

---
 src/gatt-database.c  |  2 +-
 src/shared/gatt-db.c | 46 ++++++++++++++++++++++++++++++++++++++++----
 src/shared/gatt-db.h | 13 ++++++++++++-
 3 files changed, 55 insertions(+), 6 deletions(-)
src/gatt-database.c +1 -1
index 25641da8a..dc75762f3 100644
@@ -1302,7 +1302,7 @@ static void populate_devinfo_service(struct btd_gatt_database *database)
 static void register_core_services(struct btd_gatt_database *database)
 {
 	gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb,
-								database);
+							NULL, database);
 
 	populate_gap_service(database);
 	populate_gatt_service(database);
src/shared/gatt-db.c +42 -4

103 lines not on this page · raw message

src/shared/gatt-db.h +12 -1
index 3de22403c..acd8f6a81 100644
@@ -47,6 +47,10 @@ typedef void (*gatt_db_write_t) (struct gatt_db_attribute *attrib,
 					const uint8_t *value, size_t len,
 					uint8_t opcode, struct bt_att *att,
 					void *user_data);
+typedef void (*gatt_db_notify_t) (struct gatt_db_attribute *attrib,
+					struct gatt_db_attribute *ccc,
+					const uint8_t *value, size_t len,
+					struct bt_att *att, void *user_data);
 
 struct gatt_db_attribute *
 gatt_db_service_add_characteristic(struct gatt_db_attribute *attrib,
@@ -197,7 +201,9 @@ unsigned int gatt_db_register(struct gatt_db *db,
 bool gatt_db_unregister(struct gatt_db *db, unsigned int id);
 
 void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func,
-				gatt_db_write_t write_func, void *user_data);
+					gatt_db_write_t write_func,
+					gatt_db_notify_t notify_func,
+					void *user_data);
 
 typedef uint8_t (*gatt_db_authorize_cb_t)(struct gatt_db_attribute *attrib,
 					uint8_t opcode, struct bt_att *att,
@@ -275,6 +281,11 @@ bool gatt_db_attribute_write(struct gatt_db_attribute *attrib, uint16_t offset,
 bool gatt_db_attribute_write_result(struct gatt_db_attribute *attrib,
 						unsigned int id, int err);
 
+bool gatt_db_attribute_notify(struct gatt_db_attribute *attrib,
+					struct gatt_db_attribute *ccc,
+					const uint8_t *value, size_t len,
+					struct bt_att *att);
+
 bool gatt_db_attribute_reset(struct gatt_db_attribute *attrib);
 
 void *gatt_db_attribute_get_user_data(struct gatt_db_attribute *attrib);
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH 4/4 gatt: Make use of gatt_db_attribute_notify

In reply to Luiz Augusto von Dentz

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

This makes use of gatt_db_attribute_notify to send indications of Service Changed.

---
 src/gatt-database.c | 73 ++++++++++++++++++++++++++++++++++++---------
 1 file changed, 59 insertions(+), 14 deletions(-)
src/gatt-database.c +59 -14
index dc75762f3..d3518ebfc 100644
@@ -378,6 +378,18 @@ static bool get_dst_info(struct bt_att *att, bdaddr_t *dst, uint8_t *dst_type)
 	return true;
 }
 
+static struct device_state *
+find_device_state_by_att(struct btd_gatt_database *database, struct bt_att *att)
+{
+	bdaddr_t bdaddr;
+	uint8_t bdaddr_type;
+
+	if (!get_dst_info(att, &bdaddr, &bdaddr_type))
+		return NULL;
+
+	return find_device_state(database, &bdaddr, bdaddr_type);
+}
+
 static struct device_state *get_device_state(struct btd_gatt_database *database,
 						struct bt_att *att)
 {
@@ -1299,16 +1311,6 @@ static void populate_devinfo_service(struct btd_gatt_database *database)
 	database_add_record(database, service);
 }
 
-static void register_core_services(struct btd_gatt_database *database)
-{
-	gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb,
-							NULL, database);
-
-	populate_gap_service(database);
-	populate_gatt_service(database);
-	populate_devinfo_service(database);
-}
-
 static void conf_cb(void *user_data)
 {
 	GDBusProxy *proxy = user_data;
@@ -1432,6 +1434,49 @@ remove:
 	}
 }
 
+static void gatt_notify_cb(struct gatt_db_attribute *attrib,
+					struct gatt_db_attribute *ccc,
+					const uint8_t *value, size_t len,
+					struct bt_att *att, void *user_data)
+{
+	struct btd_gatt_database *database = user_data;
+	struct notify notify;
+
+	memset(&notify, 0, sizeof(notify));
+
+	notify.database = database;
+	notify.handle = gatt_db_attribute_get_handle(attrib);
+	notify.ccc_handle = gatt_db_attribute_get_handle(ccc);
+	notify.value = (void *) value;
+	notify.len = len;
+
+	if (attrib == database->svc_chngd)
+		notify.conf = service_changed_conf;
+
+	/* If a specific att is provided notify only to that device */
+	if (att) {
+		struct device_state *state;
+
+		state = find_device_state_by_att(database, att);
+		if (!state)
+			return;
+
+		send_notification_to_device(state, &notify);
+	} else
+		queue_foreach(database->device_states,
+				send_notification_to_device, &notify);
+}
+
+static void register_core_services(struct btd_gatt_database *database)
+{
+	gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb,
+						gatt_notify_cb, database);
+
+	populate_gap_service(database);
+	populate_gatt_service(database);
+	populate_devinfo_service(database);
+}
+
 static void send_notification_to_devices(struct btd_gatt_database *database,
 					uint16_t handle, uint8_t *value,
 					uint16_t len, uint16_t ccc_handle,
@@ -1478,8 +1523,8 @@ static void send_service_changed(struct btd_gatt_database *database,
 	put_le16(start, value);
 	put_le16(end, value + 2);
 
-	send_notification_to_devices(database, handle, value, sizeof(value),
-					ccc_handle, service_changed_conf, NULL);
+	gatt_db_attribute_notify(database->svc_chngd, database->svc_chngd_ccc,
+				 value, sizeof(value), NULL);
 }
 
 static void gatt_db_service_added(struct gatt_db_attribute *attrib,
@@ -3917,6 +3962,6 @@ void btd_gatt_database_restore_svc_chng_ccc(struct btd_gatt_database *database)
 	put_le16(0x0001, value);
 	put_le16(0xffff, value + 2);
 
-	send_notification_to_devices(database, handle, value, sizeof(value),
-					ccc_handle, service_changed_conf, NULL);
+	gatt_db_attribute_notify(database->svc_chngd, database->svc_chngd_ccc,
+				 value, sizeof(value), NULL);
 }
Signature · 1 line
2.34.1
#4 lore raw
In reply to Luiz Augusto von Dentz

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=607383

---Test result---

Test Summary:

CheckPatch                    PASS      2.01 seconds
GitLint                       PASS      1.36 seconds
Prep - Setup ELL              PASS      38.22 seconds
Build - Prep                  PASS      0.41 seconds
Build - Configure             PASS      7.70 seconds
Build - Make                  PASS      1301.25 seconds
Make Check                    PASS      11.02 seconds
Make Check w/Valgrind         PASS      399.26 seconds
Make Distcheck                PASS      212.75 seconds
Build w/ext ELL - Configure   PASS      7.61 seconds
Build w/ext ELL - Make        PASS      1290.35 seconds
Incremental Build with patchesPASS      5245.61 seconds

---
Regards,
Linux Bluetooth

author maintainer
#5 lore raw

Hi,

Quoted 27 lines · On Fri, Jan 21, 2022 at 3:40 PM <[email protected]> wrote:

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=607383

---Test result---

Test Summary:

CheckPatch                    PASS      2.01 seconds
GitLint                       PASS      1.36 seconds
Prep - Setup ELL              PASS      38.22 seconds
Build - Prep                  PASS      0.41 seconds
Build - Configure             PASS      7.70 seconds
Build - Make                  PASS      1301.25 seconds
Make Check                    PASS      11.02 seconds
Make Check w/Valgrind         PASS      399.26 seconds
Make Distcheck                PASS      212.75 seconds
Build w/ext ELL - Configure   PASS      7.61 seconds
Build w/ext ELL - Make        PASS      1290.35 seconds
Incremental Build with patchesPASS      5245.61 seconds
Quoted 3 lines

---
Regards,
Linux Bluetooth

Pushed.

Signature · 1 line
Luiz Augusto von Dentz