From: Luiz Augusto von Dentz <luiz.von.dentz@xxxxxxxxx> gatt_db_set_authorize can be used to set the callback to authorize attribute operations. --- src/shared/gatt-db.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/shared/gatt-db.h | 6 ++++++ 2 files changed, 47 insertions(+) diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c index 7dc93268b..4d3653a53 100644 --- a/src/shared/gatt-db.c +++ b/src/shared/gatt-db.c @@ -67,12 +67,16 @@ struct gatt_db { struct queue *notify_list; unsigned int next_notify_id; + + gatt_db_authorize_cb_t authorize; + void *authorize_data; }; struct notify { unsigned int id; gatt_db_attribute_cb_t service_added; gatt_db_attribute_cb_t service_removed; + gatt_db_authorize_cb_t authorize_cb; gatt_db_destroy_func_t destroy; void *user_data; }; @@ -736,6 +740,18 @@ bool gatt_db_unregister(struct gatt_db *db, unsigned int id) return true; } +bool gatt_db_set_authorize(struct gatt_db *db, gatt_db_authorize_cb_t cb, + void *user_data) +{ + if (!db) + return false; + + db->authorize = cb; + db->authorize_data = user_data; + + return true; +} + static uint16_t get_attribute_index(struct gatt_db_service *service, int end_offset) { @@ -1821,6 +1837,17 @@ static bool read_timeout(void *user_data) return false; } +static uint8_t attribute_authorize(struct gatt_db_attribute *attrib, + uint8_t opcode, struct bt_att *att) +{ + struct gatt_db *db = attrib->service->db; + + if (!db->authorize) + return 0; + + return db->authorize(attrib, opcode, att, db->authorize_data); +} + bool gatt_db_attribute_read(struct gatt_db_attribute *attrib, uint16_t offset, uint8_t opcode, struct bt_att *att, gatt_db_attribute_read_t func, void *user_data) @@ -1832,6 +1859,13 @@ bool gatt_db_attribute_read(struct gatt_db_attribute *attrib, uint16_t offset, if (attrib->read_func) { struct pending_read *p; + uint8_t err; + + err = attribute_authorize(attrib, opcode, att); + if (err) { + func(attrib, err, NULL, 0, user_data); + return true; + } p = new0(struct pending_read, 1); p->attrib = attrib; @@ -1913,6 +1947,13 @@ bool gatt_db_attribute_write(struct gatt_db_attribute *attrib, uint16_t offset, if (attrib->write_func) { struct pending_write *p; + uint8_t err; + + err = attribute_authorize(attrib, opcode, att); + if (err) { + func(attrib, err, user_data); + return true; + } p = new0(struct pending_write, 1); p->attrib = attrib; diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h index f3ab803d1..398d69ad8 100644 --- a/src/shared/gatt-db.h +++ b/src/shared/gatt-db.h @@ -198,6 +198,12 @@ 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); +typedef uint8_t (*gatt_db_authorize_cb_t)(struct gatt_db_attribute *attrib, + uint8_t opcode, struct bt_att *att, + void *user_data); +bool gatt_db_set_authorize(struct gatt_db *db, gatt_db_authorize_cb_t cb, + void *user_data); + struct gatt_db_attribute *gatt_db_get_service(struct gatt_db *db, uint16_t handle); -- 2.17.2