shared/gatt-server: Add bt_gatt_server_set_authorize
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 25 Apr 2018 12:06:47 +0000 (15:06 +0300)
committerhimanshu <h.himanshu@samsung.com>
Tue, 11 Feb 2020 08:57:47 +0000 (14:27 +0530)
bt_gatt_server_set_authorize can be used to set the callback to
authorize attribute operations which is required in order to send
out of sync error as that has to happen even in case the handle is
not valid.

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

index 13c6b6c..fec66b3 100644 (file)
@@ -115,6 +115,9 @@ struct bt_gatt_server {
        bt_gatt_server_destroy_func_t debug_destroy;
        void *debug_data;
 
+       bt_gatt_server_authorize_cb_t authorize;
+       void *authorize_data;
+
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        bt_gatt_server_mtu_changed_callback_t mtu_chngd_callback;
        bt_gatt_server_destroy_func_t mtu_chngd_destroy;
@@ -799,6 +802,16 @@ static void write_complete_cb(struct gatt_db_attribute *attr, int err,
        async_write_op_destroy(op);
 }
 
+static uint8_t authorize_req(struct bt_gatt_server *server,
+                                       uint8_t opcode, uint16_t handle)
+{
+       if (!server->authorize)
+               return 0;
+
+       return server->authorize(server->att, opcode, handle,
+                                               server->authorize_data);
+}
+
 static void write_cb(uint8_t opcode, const void *pdu,
                                        uint16_t length, void *user_data)
 {
@@ -813,6 +826,10 @@ static void write_cb(uint8_t opcode, const void *pdu,
                goto error;
        }
 
+       ecode = authorize_req(server, opcode, handle);
+       if (ecode)
+               goto error;
+
        handle = get_le16(pdu);
        attr = gatt_db_get_attribute(server->db, handle);
        if (!attr) {
@@ -939,6 +956,10 @@ static void handle_read_req(struct bt_gatt_server *server, uint8_t opcode,
        uint8_t ecode;
        struct async_read_op *op = NULL;
 
+       ecode = authorize_req(server, opcode, handle);
+       if (ecode)
+               goto error;
+
        attr = gatt_db_get_attribute(server->db, handle);
        if (!attr) {
                ecode = BT_ATT_ERROR_INVALID_HANDLE;
@@ -1781,6 +1802,19 @@ bool bt_gatt_server_send_indication(struct bt_gatt_server *server,
        return result;
 }
 
+bool bt_gatt_server_set_authorize(struct bt_gatt_server *server,
+                                       bt_gatt_server_authorize_cb_t cb,
+                                       void *user_data)
+{
+       if (!server)
+               return false;
+
+       server->authorize = cb;
+       server->authorize_data = user_data;
+
+       return true;
+}
+
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
 bool bt_gatt_server_set_mtu_changed(struct bt_gatt_server *server,
                        bt_gatt_server_mtu_changed_callback_t callback,
index 730cceb..7d83669 100755 (executable)
@@ -48,6 +48,13 @@ bool bt_gatt_server_set_debug(struct bt_gatt_server *server,
                                        void *user_data,
                                        bt_gatt_server_destroy_func_t destroy);
 
+typedef uint8_t (*bt_gatt_server_authorize_cb_t)(struct bt_att *att,
+                                       uint8_t opcode, uint16_t handle,
+                                       void *user_data);
+bool bt_gatt_server_set_authorize(struct bt_gatt_server *server,
+                                       bt_gatt_server_authorize_cb_t cb,
+                                       void *user_data);
+
 bool bt_gatt_server_send_notification(struct bt_gatt_server *server,
                                        uint16_t handle, const uint8_t *value,
                                        uint16_t length);