From b2289a008f71dbf6c4fbfd4f6644c695d32b2ad7 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Mon, 22 Jul 2019 20:58:46 +0200 Subject: [PATCH] apps/btshell: Fix full discovery During full discovery, if last service does not have any characteristic but its end handle is 65535 (which is allowed by Core spec), discovery will loop forever since it assumes service was discovered if either start handle == end handle or there are characteristics discovered. This patch fixes this by adding "discovered" property to service struct which is used to check if service was already discovered. --- apps/btshell/src/btshell.h | 2 ++ apps/btshell/src/main.c | 43 +++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/apps/btshell/src/btshell.h b/apps/btshell/src/btshell.h index 94ab730fc..838ee83a8 100644 --- a/apps/btshell/src/btshell.h +++ b/apps/btshell/src/btshell.h @@ -64,6 +64,7 @@ struct btshell_svc { SLIST_ENTRY(btshell_svc) next; struct ble_gatt_svc svc; struct btshell_chr_list chrs; + bool discovered; }; SLIST_HEAD(btshell_svc_list, btshell_svc); @@ -95,6 +96,7 @@ int btshell_disc_svcs(uint16_t conn_handle); int btshell_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid); int btshell_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle); +int btshell_disc_all_chrs_in_svc(uint16_t conn_handle, struct btshell_svc *svc); int btshell_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const ble_uuid_t *uuid); int btshell_disc_all_dscs(uint16_t conn_handle, uint16_t start_handle, diff --git a/apps/btshell/src/main.c b/apps/btshell/src/main.c index dc987d0d6..a3fac102d 100644 --- a/apps/btshell/src/main.c +++ b/apps/btshell/src/main.c @@ -749,9 +749,8 @@ btshell_disc_full_chrs(uint16_t conn_handle) } SLIST_FOREACH(svc, &conn->svcs, next) { - if (!svc_is_empty(svc) && SLIST_EMPTY(&svc->chrs)) { - rc = btshell_disc_all_chrs(conn_handle, svc->svc.start_handle, - svc->svc.end_handle); + if (!svc->discovered) { + rc = btshell_disc_all_chrs_in_svc(conn_handle, svc); if (rc != 0) { btshell_full_disc_complete(rc); } @@ -815,6 +814,33 @@ btshell_on_disc_c(uint16_t conn_handle, const struct ble_gatt_error *error, return 0; } +static int +btshell_on_disc_c_in_s(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) +{ + struct btshell_svc *svc = arg; + + switch (error->status) { + case 0: + btshell_chr_add(conn_handle, svc->svc.start_handle, chr); + break; + + case BLE_HS_EDONE: + svc->discovered = true; + console_printf("characteristic discovery successful\n"); + if (btshell_full_disc_prev_chr_val > 0) { + btshell_disc_full_chrs(conn_handle); + } + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + static int btshell_on_disc_d(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, @@ -1455,6 +1481,17 @@ btshell_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle, return rc; } +int +btshell_disc_all_chrs_in_svc(uint16_t conn_handle, struct btshell_svc *svc) +{ + int rc; + + rc = ble_gattc_disc_all_chrs(conn_handle, svc->svc.start_handle, + svc->svc.end_handle, btshell_on_disc_c_in_s, + svc); + return rc; +} + int btshell_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const ble_uuid_t *uuid)