From d41dda9b104dd2c8645530ecd7d1a6d140842468 Mon Sep 17 00:00:00 2001 From: Steven James Date: Mon, 13 Jul 2020 15:18:07 -0400 Subject: [PATCH 1/3] Fix BLE Characteristic discovery when the central device returns more than 4 Characteristics in a discovery request --- libraries/Bluefruit52Lib/src/BLEDiscovery.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp index 84276889..193e534f 100644 --- a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp +++ b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp @@ -109,7 +109,8 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar // We could found more characteristic than we looking for. Buffer must be large enough enum { MAX_DISC_CHARS = 4 }; - uint16_t bufsize = sizeof(ble_gattc_evt_char_disc_rsp_t) + (MAX_DISC_CHARS-1)*sizeof(ble_gattc_char_t); + // -1 because the first ble_gattc_char_t is built in to ble_gattc_evt_char_disc_rsp_t + uint16_t bufsize = sizeof(ble_gattc_evt_char_disc_rsp_t) + (MAX_DISC_CHARS-1)*sizeof(ble_gattc_char_t); ble_gattc_evt_char_disc_rsp_t* disc_chr = (ble_gattc_evt_char_disc_rsp_t*) rtos_malloc( bufsize ); uint8_t found = 0; @@ -129,6 +130,9 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar // timeout or has no data (due to GATT Error) if ( bytecount <= 0 ) break; + if ( disc_chr->count > MAX_DISC_CHARS ) // if we truncated the response, adjust the count to match + disc_chr->count = MAX_DISC_CHARS; + // Look for matched uuid in the discovered list for(uint8_t d=0 ; dcount; d++) { From 38127c2fa33da4d0f66579ae8d9841729752cdb4 Mon Sep 17 00:00:00 2001 From: Steven James Date: Thu, 16 Jul 2020 13:59:55 -0400 Subject: [PATCH 2/3] Clean up formatting --- libraries/Bluefruit52Lib/src/BLEDiscovery.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp index 193e534f..0e2b487e 100644 --- a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp +++ b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp @@ -130,8 +130,8 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar // timeout or has no data (due to GATT Error) if ( bytecount <= 0 ) break; - if ( disc_chr->count > MAX_DISC_CHARS ) // if we truncated the response, adjust the count to match - disc_chr->count = MAX_DISC_CHARS; + // if we truncated the response, adjust the count to match + if ( disc_chr->count > MAX_DISC_CHARS ) disc_chr->count = MAX_DISC_CHARS; // Look for matched uuid in the discovered list for(uint8_t d=0 ; dcount; d++) From df5f6c48a9479ba7602b6f99d5facab7bbde982d Mon Sep 17 00:00:00 2001 From: Steven James Date: Thu, 16 Jul 2020 14:00:30 -0400 Subject: [PATCH 3/3] MAX_DISC_CHARS = 8 to avoid excessive OTA queries for discovery. --- libraries/Bluefruit52Lib/src/BLEDiscovery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp index 0e2b487e..880d7097 100644 --- a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp +++ b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp @@ -107,7 +107,7 @@ bool BLEDiscovery::_discoverService(uint16_t conn_handle, BLEClientService& svc, uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientCharacteristic* chr[], uint8_t count) { // We could found more characteristic than we looking for. Buffer must be large enough - enum { MAX_DISC_CHARS = 4 }; + enum { MAX_DISC_CHARS = 8 }; // -1 because the first ble_gattc_char_t is built in to ble_gattc_evt_char_disc_rsp_t uint16_t bufsize = sizeof(ble_gattc_evt_char_disc_rsp_t) + (MAX_DISC_CHARS-1)*sizeof(ble_gattc_char_t);