From fac14943fa2595f9be8b97b92932f4beb1ae7a75 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Fri, 17 Jul 2020 17:22:08 -0700 Subject: [PATCH] shared/gatt-client: Consolidate code parsing services This makes code parsing primary/secondary services which was mostly duplicated into a function so it can be reused. --- src/shared/gatt-client.c | 117 +++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 67 deletions(-) diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c index 285942095..f15466673 100644 --- a/src/shared/gatt-client.c +++ b/src/shared/gatt-client.c @@ -1107,18 +1107,56 @@ static void discovery_found_service(struct discovery_op *op, op->last = end; } -static void discover_secondary_cb(bool success, uint8_t att_ecode, - struct bt_gatt_result *result, - void *user_data) +static bool discovery_parse_services(struct discovery_op *op, bool primary, + struct bt_gatt_iter *iter) { - struct discovery_op *op = user_data; struct bt_gatt_client *client = op->client; - struct bt_gatt_iter iter; struct gatt_db_attribute *attr; uint16_t start, end; uint128_t u128; bt_uuid_t uuid; char uuid_str[MAX_LEN_UUID_STR]; + + while (bt_gatt_iter_next_service(iter, &start, &end, u128.data)) { + bt_uuid128_create(&uuid, u128); + + /* Log debug message */ + bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str)); + util_debug(client->debug_callback, client->debug_data, + "start: 0x%04x, end: 0x%04x, uuid: %s", + start, end, uuid_str); + + /* Store the service */ + attr = gatt_db_insert_service(client->db, start, &uuid, primary, + end - start + 1); + if (!attr) { + gatt_db_clear_range(client->db, start, end); + attr = gatt_db_insert_service(client->db, start, &uuid, + false, end - start + 1); + if (!attr) { + util_debug(client->debug_callback, + client->debug_data, + "Failed to store service"); + return false; + } + /* Database has changed adjust last handle */ + op->last = end; + } + + /* Update pending list */ + discovery_found_service(op, attr, start, end); + } + + return true; +} + +static void discover_secondary_cb(bool success, uint8_t att_ecode, + struct bt_gatt_result *result, + void *user_data) +{ + struct discovery_op *op = user_data; + struct bt_gatt_client *client = op->client; + struct bt_gatt_iter iter; struct handle_range *range; discovery_req_clear(client); @@ -1147,37 +1185,12 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode, "Secondary services found: %u", bt_gatt_result_service_count(result)); - while (bt_gatt_iter_next_service(&iter, &start, &end, u128.data)) { - bt_uuid128_create(&uuid, u128); - - /* Log debug message */ - bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str)); - util_debug(client->debug_callback, client->debug_data, - "start: 0x%04x, end: 0x%04x, uuid: %s", - start, end, uuid_str); - - /* Store the service */ - attr = gatt_db_insert_service(client->db, start, &uuid, false, - end - start + 1); - if (!attr) { - gatt_db_clear_range(client->db, start, end); - attr = gatt_db_insert_service(client->db, start, &uuid, - false, end - start + 1); - if (!attr) { - util_debug(client->debug_callback, - client->debug_data, - "Failed to store service"); - success = false; - goto done; - } - /* Database has changed adjust last handle */ - op->last = end; - } - - /* Update pending list */ - discovery_found_service(op, attr, start, end); + if (!discovery_parse_services(op, false, &iter)) { + success = false; + goto done; } + next: if (queue_isempty(op->pending_svcs) || queue_isempty(op->discov_ranges)) goto done; @@ -1214,11 +1227,6 @@ static void discover_primary_cb(bool success, uint8_t att_ecode, struct discovery_op *op = user_data; struct bt_gatt_client *client = op->client; struct bt_gatt_iter iter; - struct gatt_db_attribute *attr; - uint16_t start, end; - uint128_t u128; - bt_uuid_t uuid; - char uuid_str[MAX_LEN_UUID_STR]; discovery_req_clear(client); @@ -1246,34 +1254,9 @@ static void discover_primary_cb(bool success, uint8_t att_ecode, "Primary services found: %u", bt_gatt_result_service_count(result)); - while (bt_gatt_iter_next_service(&iter, &start, &end, u128.data)) { - bt_uuid128_create(&uuid, u128); - - /* Log debug message. */ - bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str)); - util_debug(client->debug_callback, client->debug_data, - "start: 0x%04x, end: 0x%04x, uuid: %s", - start, end, uuid_str); - - attr = gatt_db_insert_service(client->db, start, &uuid, true, - end - start + 1); - if (!attr) { - gatt_db_clear_range(client->db, start, end); - attr = gatt_db_insert_service(client->db, start, &uuid, - true, end - start + 1); - if (!attr) { - util_debug(client->debug_callback, - client->debug_data, - "Failed to store service"); - success = false; - goto done; - } - /* Database has changed adjust last handle */ - op->last = end; - } - - /* Update pending list */ - discovery_found_service(op, attr, start, end); + if (!discovery_parse_services(op, true, &iter)) { + success = false; + goto done; } secondary: -- 2.47.3