Merge pull request #535 from pyro9/fix_discovery

Fix  BLEDiscovery::discoverCharacteristic when the central device returns more than 4 Characteristics in a discovery request
This commit is contained in:
Ha Thach authored and GitHub committed 2020-07-17 13:19:26 +07:00
commit 1bf80af139
1 file changed
+6 -2
@@ -107,9 +107,10 @@ 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 };
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 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 ; d<disc_chr->count; d++)
{