From: Luiz Augusto von Dentz <luiz.von.dentz@xxxxxxxxx> This checks if a range is within service handles skipping if it is not, it also convert the direct search to use gatt_db_foreach_service_in_range instead so all operation that do lookups in a range can benefit from such optimization. --- src/shared/gatt-db.c | 49 +++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c index b5f7094da..3b15c9a03 100644 --- a/src/shared/gatt-db.c +++ b/src/shared/gatt-db.c @@ -773,7 +773,15 @@ static uint16_t get_attribute_index(struct gatt_db_service *service, static uint16_t get_handle_at_index(struct gatt_db_service *service, int index) { - return service->attributes[index]->handle; + if (index >= 0) + return service->attributes[index]->handle; + + for (index = service->num_handles - 1; index > 0; index--) { + if (service->attributes[index]) + return service->attributes[index]->handle; + } + + return service->attributes[0]->handle; } static struct gatt_db_attribute * @@ -1180,11 +1188,10 @@ struct find_by_type_value_data { unsigned int num_of_res; }; -static void find_by_type(void *data, void *user_data) +static void find_by_type(struct gatt_db_attribute *attribute, void *user_data) { struct find_by_type_value_data *search_data = user_data; - struct gatt_db_service *service = data; - struct gatt_db_attribute *attribute; + struct gatt_db_service *service = attribute->service; int i; if (!service->active) @@ -1235,7 +1242,8 @@ unsigned int gatt_db_find_by_type(struct gatt_db *db, uint16_t start_handle, data.func = func; data.user_data = user_data; - queue_foreach(db->services, find_by_type, &data); + gatt_db_foreach_service_in_range(db, NULL, find_by_type, &data, + start_handle, end_handle); return data.num_of_res; } @@ -1259,7 +1267,8 @@ unsigned int gatt_db_find_by_type_value(struct gatt_db *db, data.value = value; data.value_len = value_len; - queue_foreach(db->services, find_by_type, &data); + gatt_db_foreach_service_in_range(db, NULL, find_by_type, &data, + start_handle, end_handle); return data.num_of_res; } @@ -1271,11 +1280,10 @@ struct read_by_type_data { uint16_t end_handle; }; -static void read_by_type(void *data, void *user_data) +static void read_by_type(struct gatt_db_attribute *attribute, void *user_data) { struct read_by_type_data *search_data = user_data; - struct gatt_db_service *service = data; - struct gatt_db_attribute *attribute; + struct gatt_db_service *service = attribute->service; int i; if (!service->active) @@ -1310,7 +1318,8 @@ void gatt_db_read_by_type(struct gatt_db *db, uint16_t start_handle, data.end_handle = end_handle; data.queue = queue; - queue_foreach(db->services, read_by_type, &data); + gatt_db_foreach_service_in_range(db, NULL, read_by_type, &data, + start_handle, end_handle); } @@ -1320,21 +1329,16 @@ struct find_information_data { uint16_t end_handle; }; -static void find_information(void *data, void *user_data) +static void find_information(struct gatt_db_attribute *attribute, + void *user_data) { struct find_information_data *search_data = user_data; - struct gatt_db_service *service = data; - struct gatt_db_attribute *attribute; + struct gatt_db_service *service = attribute->service; int i; if (!service->active) return; - /* Check if service is in range */ - if ((service->attributes[0]->handle + service->num_handles - 1) < - search_data->start_handle) - return; - for (i = 0; i < service->num_handles; i++) { attribute = service->attributes[i]; if (!attribute) @@ -1360,7 +1364,8 @@ void gatt_db_find_information(struct gatt_db *db, uint16_t start_handle, data.end_handle = end_handle; data.queue = queue; - queue_foreach(db->services, find_information, &data); + gatt_db_foreach_service_in_range(db, NULL, find_information, &data, + start_handle, end_handle); } void gatt_db_foreach_service(struct gatt_db *db, const bt_uuid_t *uuid, @@ -1382,12 +1387,14 @@ static void foreach_service_in_range(void *data, void *user_data) { struct gatt_db_service *service = data; struct foreach_data *foreach_data = user_data; - uint16_t svc_start; + uint16_t svc_start, svc_end; bt_uuid_t uuid; svc_start = get_handle_at_index(service, 0); + svc_end = get_handle_at_index(service, -1); - if (svc_start > foreach_data->end || svc_start < foreach_data->start) + /* Check if service is within requested range */ + if (svc_start > foreach_data->end || svc_end < foreach_data->start) return; if (foreach_data->uuid) { -- 2.20.1