Skip to content

Commit ad6f48c

Browse files
committed
update discoveryService
1 parent 660c4c7 commit ad6f48c

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

libraries/Bluefruit52Lib/src/BLECentral.cpp

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@
4343
*/
4444
BLECentral::BLECentral(void)
4545
{
46-
_conn_hdl = BLE_CONN_HANDLE_INVALID;
46+
_conn_hdl = BLE_CONN_HANDLE_INVALID;
4747

48-
_evt_sem = NULL;
49-
_evt_data = NULL;
48+
_evt_sem = NULL;
49+
_evt_buf = NULL;
50+
_evt_bufsize = 0;
5051

51-
_scan_cb = NULL;
52-
_scan_param = (ble_gap_scan_params_t) {
52+
_scan_cb = NULL;
53+
_scan_param = (ble_gap_scan_params_t) {
5354
.active = 1,
5455
.selective = 0,
5556
.p_whitelist = NULL,
@@ -58,6 +59,9 @@ BLECentral::BLECentral(void)
5859
.timeout = 0, // no timeout
5960
};
6061

62+
_hdl_range.start_handle = 1;
63+
_hdl_range.end_handle = 0xffff;
64+
6165
_connect_cb = NULL;
6266
_discconnect_cb = NULL;
6367
}
@@ -201,39 +205,42 @@ void BLECentral::setDisconnectCallback( disconnect_callback_t fp)
201205
/*------------------------------------------------------------------*/
202206
/* DISCOVERY
203207
*------------------------------------------------------------------*/
204-
bool BLECentral::discoverService(BLEUuid uuid, ble_gattc_handle_range_t* handle_range, uint16_t start_handle)
208+
bool BLECentral::discoverService(BLEUuid uuid, uint16_t start_handle)
205209
{
206210
uuid.begin(); // add uuid128 if needed
207211

208-
VERIFY_STATUS( sd_ble_gattc_primary_services_discover(_conn_hdl, start_handle, &uuid._uuid), false );
212+
ble_gattc_evt_prim_srvc_disc_rsp_t discover_svc;
209213

210-
// wait for discovery event
211-
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || !_evt_data ) return false;
214+
_evt_buf = &discover_svc;
215+
_evt_bufsize = sizeof(discover_svc);
212216

213-
bool result = false;
217+
VERIFY_STATUS( sd_ble_gattc_primary_services_discover(_conn_hdl, start_handle, &uuid._uuid), false );
214218

215-
ble_gattc_evt_prim_srvc_disc_rsp_t* discover_svc = (ble_gattc_evt_prim_srvc_disc_rsp_t*) _evt_data;
219+
// wait for discovery event: timeout or has no data
220+
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return false;
216221

217-
if ( (discover_svc->count == 1) && (uuid == discover_svc->services[0].uuid) )
222+
// Check the discovered UUID with input one
223+
if ( (discover_svc.count == 1) && (uuid == discover_svc.services[0].uuid) )
218224
{
219-
(*handle_range) = discover_svc->services[0].handle_range;
220-
result = true;
225+
_hdl_range = discover_svc.services[0].handle_range;
226+
PRINT_INT(_hdl_range.start_handle);
227+
PRINT_INT(_hdl_range.end_handle);
228+
return true;
221229
}
222230

223-
free(_evt_data);
224-
225-
return result;
231+
return false;
226232
}
227233

228-
bool BLECentral::discoverCharacteristic(ble_gattc_handle_range_t handle_range)
234+
COMMENT_OUT(
235+
bool BLECentral::discoverCharacteristic()
229236
{
230237
// uuid.begin(); // add uuid128 if needed
231238

232-
VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &handle_range), false );
239+
VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &_hdl_range), false );
233240

234241
return true;
235242
}
236-
243+
)
237244

238245
/**
239246
* Event is forwarded from Bluefruit Poll() method
@@ -293,22 +300,45 @@ void BLECentral::_event_handler(ble_evt_t* evt)
293300

294301
if ( _conn_hdl == gattc->conn_handle )
295302
{
296-
if ( _evt_data ) rtos_free(_evt_data);
297-
_evt_data = NULL;
298-
299-
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS)
303+
if ( _evt_buf )
300304
{
301-
ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp;
305+
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS)
306+
{
307+
ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp;
308+
309+
// len of the discovered services
310+
uint16_t len = sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t) + (svc_rsp->count-1)*sizeof(ble_gattc_service_t);
311+
_evt_bufsize = min16(_evt_bufsize, len);
312+
313+
memcpy(_evt_buf, svc_rsp, _evt_bufsize);
314+
}else
315+
{
316+
_evt_bufsize = 0;
317+
}
318+
}
302319

303-
// len of the discovered services
304-
uint16_t len = sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t) + (svc_rsp->count-1)*sizeof(ble_gattc_service_t);
320+
xSemaphoreGive(_evt_sem);
321+
}
322+
}
323+
break;
305324

306-
_evt_data = rtos_malloc( len );
325+
case BLE_GATTC_EVT_CHAR_DISC_RSP:
326+
{
327+
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
307328

308-
memcpy(_evt_data, svc_rsp, len);
329+
if ( _conn_hdl == gattc->conn_handle )
330+
{
331+
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS)
332+
{
333+
ble_gattc_evt_char_disc_rsp_t* chr_rsp = &gattc->params.char_disc_rsp;
334+
335+
PRINT_INT(chr_rsp->count);
336+
for(uint8_t i=0; i<chr_rsp->count; i++)
337+
{
338+
PRINT_HEX(chr_rsp->chars[i].uuid.type);
339+
PRINT_HEX(chr_rsp->chars[i].uuid.uuid);
340+
}
309341
}
310-
311-
xSemaphoreGive(_evt_sem);
312342
}
313343
}
314344
break;

libraries/Bluefruit52Lib/src/BLECentral.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class BLECentral
8282
/*------------------------------------------------------------------*/
8383
/* GATTC Discovery
8484
*------------------------------------------------------------------*/
85-
bool discoverService(BLEUuid uuid, ble_gattc_handle_range_t* handle_range, uint16_t start_handle = 1);
86-
bool discoverCharacteristic(ble_gattc_handle_range_t handle_range);
85+
bool discoverService(BLEUuid uuid, uint16_t start_handle = 1);
86+
// bool discoverCharacteristic();
8787

8888
/*------------------------------------------------------------------*/
8989
/* CALLBACKS
@@ -98,11 +98,14 @@ class BLECentral
9898
uint16_t _conn_hdl;
9999

100100
SemaphoreHandle_t _evt_sem;
101-
void* _evt_data;
101+
void* _evt_buf;
102+
uint16_t _evt_bufsize;
102103

103104
ble_gap_scan_params_t _scan_param;
104105
scan_callback_t _scan_cb;
105106

107+
ble_gattc_handle_range_t _hdl_range;
108+
106109
connect_callback_t _connect_cb;
107110
disconnect_callback_t _discconnect_cb;
108111

platform.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818

1919
name=Adafruit Boards
20-
version=0.5.0
20+
version=0.5.1
2121

2222
# Compile variables
2323
# -----------------

0 commit comments

Comments
 (0)