Skip to content

Commit f7f4daa

Browse files
committed
fix memory issue with discovery characteristic
- when Number of found chars > Number of expected chars
1 parent e33fe93 commit f7f4daa

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636

3737
#include "bluefruit.h"
3838

39-
#define MAX_DESCIRPTORS 8
40-
4139
void BLEClientCharacteristic::_init(void)
4240
{
4341
varclr(&_chr);
@@ -122,6 +120,8 @@ BLEClientService& BLEClientCharacteristic::parentService (void)
122120

123121
bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle)
124122
{
123+
enum { MAX_DESCIRPTORS = 8 };
124+
125125
struct {
126126
uint16_t count;
127127
ble_gattc_desc_t descs[MAX_DESCIRPTORS];
@@ -186,7 +186,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
186186
_adamsg.prepare( (void*) data, len);
187187
VERIFY_STATUS ( sd_ble_gattc_write(_service->connHandle(), &param) );
188188

189-
// len is alwasy 0 in BLE_GATTC_EVT_WRITE_RSP for BLE_GATT_OP_WRITE_REQ
189+
// len is always 0 in BLE_GATTC_EVT_WRITE_RSP for BLE_GATT_OP_WRITE_REQ
190190
count = (_adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT) < 0 ? 0 : len);
191191
}
192192
else

libraries/Bluefruit52Lib/src/BLEClientService.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ uint16_t BLEClientService::connHandle(void)
9191
return _conn_hdl;
9292
}
9393

94+
void BLEClientService::setHandleRange(ble_gattc_handle_range_t handle_range)
95+
{
96+
_hdl_range = handle_range;
97+
}
98+
9499
void BLEClientService::setHandleRange(uint16_t start_hdl, uint16_t end_hdl)
95100
{
96101
_hdl_range.start_handle = start_hdl;

libraries/Bluefruit52Lib/src/BLEClientService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class BLEClientService
6363

6464
uint16_t connHandle(void);
6565
void setHandleRange(uint16_t start_hdl, uint16_t end_hdl);
66+
void setHandleRange(ble_gattc_handle_range_t handle_range);
6667

6768
friend class BLEGatt;
6869
};

libraries/Bluefruit52Lib/src/BLEDiscovery.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ bool BLEDiscovery::_discoverService(uint16_t conn_handle, BLEClientService& svc,
8888
if ( (disc_svc.count) && (svc.uuid == disc_svc.services[0].uuid) )
8989
{
9090
_hdl_range = disc_svc.services[0].handle_range;
91-
svc.setHandleRange(_hdl_range.start_handle, _hdl_range.end_handle);
91+
svc.setHandleRange(_hdl_range);
9292

93-
LOG_LV2(Discover, "[SVC] Found 0x%04X, Handle start = %d, end = %d", disc_svc.services[0].uuid.uuid, _hdl_range.start_handle, _hdl_range.end_handle);
93+
LOG_LV2(Discover, "[SVC] Found 0x%04X, Handle start = %d, end = %d\n-----------------", disc_svc.services[0].uuid.uuid, _hdl_range.start_handle, _hdl_range.end_handle);
9494

9595
// increase for next discovery
9696
_hdl_range.start_handle++;
@@ -102,11 +102,14 @@ bool BLEDiscovery::_discoverService(uint16_t conn_handle, BLEClientService& svc,
102102

103103
uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientCharacteristic* chr[], uint8_t count)
104104
{
105-
uint8_t found = 0;
105+
// We could found more characteristic than we looking for. Buffer must be large enough
106+
enum { MAX_DISC_CHARS = 4 };
106107

107-
uint16_t bufsize = sizeof(ble_gattc_evt_char_disc_rsp_t) + (count-1)*sizeof(ble_gattc_char_t);
108+
uint16_t bufsize = sizeof(ble_gattc_evt_char_disc_rsp_t) + (MAX_DISC_CHARS-1)*sizeof(ble_gattc_char_t);
108109
ble_gattc_evt_char_disc_rsp_t* disc_chr = (ble_gattc_evt_char_disc_rsp_t*) rtos_malloc( bufsize );
109110

111+
uint8_t found = 0;
112+
110113
while( found < count )
111114
{
112115
LOG_LV2(Discover, "[CHR] Handle start = %d, end = %d", _hdl_range.start_handle, _hdl_range.end_handle);
@@ -129,7 +132,7 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
129132
{
130133
if ( chr[i]->uuid == disc_chr->chars[d].uuid )
131134
{
132-
LOG_LV2(Discover, "[CHR] Found 0x%04X, handle = %d", disc_chr->chars[d].uuid.uuid, disc_chr->chars[d].handle_value);
135+
LOG_LV2(Discover, "[CHR] Found 0x%04X, handle = %d\n-----------------", disc_chr->chars[d].uuid.uuid, disc_chr->chars[d].handle_value);
133136

134137
// characteristic assign overload
135138
chr[i]->assign(&disc_chr->chars[d]);

0 commit comments

Comments
 (0)