shared/gatt-db: Add gatt_db_set_authorize
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Fri, 2 Mar 2018 08:05:04 +0000 (10:05 +0200)
committerhimanshu <h.himanshu@samsung.com>
Tue, 11 Feb 2020 08:57:47 +0000 (14:27 +0530)
gatt_db_set_authorize can be used to set the callback to authorize
attribute operations.

Change-Id: Ibf79fc30ce75cff20c235371507c05ad02739105
Signed-off-by: himanshu <h.himanshu@samsung.com>
src/shared/gatt-db.c
src/shared/gatt-db.h

index c8d5868..286e9c6 100644 (file)
@@ -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;
 };
@@ -742,6 +746,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)
 {
@@ -1830,6 +1846,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)
@@ -1841,6 +1868,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;
@@ -1922,6 +1956,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;
index 8393915..c981d63 100644 (file)
@@ -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);