43
43
*/
44
44
BLECentral::BLECentral (void )
45
45
{
46
- _conn_hdl = BLE_CONN_HANDLE_INVALID;
46
+ _conn_hdl = BLE_CONN_HANDLE_INVALID;
47
47
48
- _evt_sem = NULL ;
49
- _evt_data = NULL ;
48
+ _evt_sem = NULL ;
49
+ _evt_buf = NULL ;
50
+ _evt_bufsize = 0 ;
50
51
51
- _scan_cb = NULL ;
52
- _scan_param = (ble_gap_scan_params_t ) {
52
+ _scan_cb = NULL ;
53
+ _scan_param = (ble_gap_scan_params_t ) {
53
54
.active = 1 ,
54
55
.selective = 0 ,
55
56
.p_whitelist = NULL ,
@@ -58,6 +59,9 @@ BLECentral::BLECentral(void)
58
59
.timeout = 0 , // no timeout
59
60
};
60
61
62
+ _hdl_range.start_handle = 1 ;
63
+ _hdl_range.end_handle = 0xffff ;
64
+
61
65
_connect_cb = NULL ;
62
66
_discconnect_cb = NULL ;
63
67
}
@@ -201,39 +205,42 @@ void BLECentral::setDisconnectCallback( disconnect_callback_t fp)
201
205
/* ------------------------------------------------------------------*/
202
206
/* DISCOVERY
203
207
*------------------------------------------------------------------*/
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)
205
209
{
206
210
uuid.begin (); // add uuid128 if needed
207
211
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 ;
209
213
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) ;
212
216
213
- bool result = false ;
217
+ VERIFY_STATUS ( sd_ble_gattc_primary_services_discover (_conn_hdl, start_handle, &uuid. _uuid ), false ) ;
214
218
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 ;
216
221
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 ) )
218
224
{
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 ;
221
229
}
222
230
223
- free (_evt_data);
224
-
225
- return result;
231
+ return false ;
226
232
}
227
233
228
- bool BLECentral::discoverCharacteristic (ble_gattc_handle_range_t handle_range)
234
+ COMMENT_OUT (
235
+ bool BLECentral::discoverCharacteristic ()
229
236
{
230
237
// uuid.begin(); // add uuid128 if needed
231
238
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 );
233
240
234
241
return true ;
235
242
}
236
-
243
+ )
237
244
238
245
/* *
239
246
* Event is forwarded from Bluefruit Poll() method
@@ -293,22 +300,45 @@ void BLECentral::_event_handler(ble_evt_t* evt)
293
300
294
301
if ( _conn_hdl == gattc->conn_handle )
295
302
{
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 )
300
304
{
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
+ }
302
319
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 ;
305
324
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 ;
307
328
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
+ }
309
341
}
310
-
311
- xSemaphoreGive (_evt_sem);
312
342
}
313
343
}
314
344
break ;
0 commit comments