shared/gatt-client: Consolidate code parsing services
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Sat, 18 Jul 2020 00:22:08 +0000 (17:22 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 12 Apr 2021 09:00:49 +0000 (14:30 +0530)
This makes code parsing primary/secondary services which was mostly
duplicated into a function so it can be reused.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/shared/gatt-client.c

index 8d1a27d..1d5112d 100644 (file)
@@ -1228,47 +1228,17 @@ 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];
-       struct handle_range *range;
-
-       discovery_req_clear(client);
-
-       if (!success) {
-               switch (att_ecode) {
-               case BT_ATT_ERROR_ATTRIBUTE_NOT_FOUND:
-               case BT_ATT_ERROR_UNSUPPORTED_GROUP_TYPE:
-                       success = true;
-                       att_ecode = 0;
-                       goto next;
-               default:
-                       util_debug(client->debug_callback, client->debug_data,
-                                       "Secondary service discovery failed."
-                                       " ATT ECODE: 0x%02x", att_ecode);
-                       goto done;
-               }
-       }
-
-       if (!result || !bt_gatt_iter_init(&iter, result)) {
-               success = false;
-               goto done;
-       }
 
-       util_debug(client->debug_callback, client->debug_data,
-                                       "Secondary services found: %u",
-                                       bt_gatt_result_service_count(result));
-
-       while (bt_gatt_iter_next_service(&iter, &start, &end, u128.data)) {
+       while (bt_gatt_iter_next_service(iter, &start, &end, u128.data)) {
                bt_uuid128_create(&uuid, u128);
 
                /* Log debug message */
@@ -1278,6 +1248,7 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode,
                                start, end, uuid_str);
 
                /* Store the service */
+
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
                if (client->in_svc_chngd) {
                        util_debug(client->debug_callback, client->debug_data,
@@ -1285,7 +1256,7 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode,
                        gatt_db_clear_range(client->db, start, end);
                }
 #endif
-               attr = gatt_db_insert_service(client->db, start, &uuid, false,
+               attr = gatt_db_insert_service(client->db, start, &uuid, primary,
                                                        end - start + 1);
                if (!attr) {
                        gatt_db_clear_range(client->db, start, end);
@@ -1295,8 +1266,7 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode,
                                util_debug(client->debug_callback,
                                                client->debug_data,
                                                "Failed to store service");
-                               success = false;
-                               goto done;
+                               return false;
                        }
                        /* Database has changed adjust last handle */
                        op->last = end;
@@ -1306,6 +1276,49 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode,
                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);
+
+       if (!success) {
+               switch (att_ecode) {
+               case BT_ATT_ERROR_ATTRIBUTE_NOT_FOUND:
+               case BT_ATT_ERROR_UNSUPPORTED_GROUP_TYPE:
+                       success = true;
+                       att_ecode = 0;
+                       goto next;
+               default:
+                       util_debug(client->debug_callback, client->debug_data,
+                                       "Secondary service discovery failed."
+                                       " ATT ECODE: 0x%02x", att_ecode);
+                       goto done;
+               }
+       }
+
+       if (!result || !bt_gatt_iter_init(&iter, result)) {
+               success = false;
+               goto done;
+       }
+
+       util_debug(client->debug_callback, client->debug_data,
+                                       "Secondary services found: %u",
+                                       bt_gatt_result_service_count(result));
+
+       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;
@@ -1350,11 +1363,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);
 
@@ -1382,41 +1390,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);
-
-#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
-               if (client->in_svc_chngd) {
-                       util_debug(client->debug_callback, client->debug_data,
-                               "In service changed, delete service first.");
-                       gatt_db_clear_range(client->db, start, end);
-               }
-#endif
-               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: