20
20
#include < sstream>
21
21
#include < iomanip>
22
22
23
- #include " BLEUtils.h"
24
23
#include " BLE.h"
25
- #include " BLEDevice.h"
24
+ #include " BLERemoteDevice.h"
25
+ #include " BLEUtils.h"
26
26
#include " BLEXXXCharacteristic.h"
27
+ #include " GeneralUtils.h"
27
28
28
29
static char LOG_TAG[] = " BLE" ;
29
30
30
- extern " C" {
31
- char *espToString (esp_err_t value);
32
- }
33
31
34
32
static EventGroupHandle_t g_eventGroup;
35
33
#define EVENT_GROUP_SCAN_COMPLETE (1 <<0 )
@@ -42,7 +40,7 @@ static EventGroupHandle_t g_eventGroup;
42
40
* Note that this address is binary and should not be directly printed. The value of the map is
43
41
* the BLEDevice object that this device represents.
44
42
*/
45
- static std::map<ble_address, BLEDevice > g_devices;
43
+ static std::map<std::string, BLERemoteDevice > g_devices;
46
44
47
45
BLEServer *BLE::m_bleServer;
48
46
@@ -215,7 +213,7 @@ static void gatt_client_event_handler(
215
213
216
214
switch (event) {
217
215
case ESP_GATTC_OPEN_EVT: {
218
- BLEDevice *pDevice = BLEUtils::findByAddress (std::string ((char *)param->open .remote_bda , 6 ));
216
+ BLERemoteDevice *pDevice = BLEUtils::findByAddress (std::string ((char *)param->open .remote_bda , 6 ));
219
217
BLEUtils::registerByConnId (param->open .conn_id , pDevice);
220
218
pDevice->dump ();
221
219
pDevice->onConnected (param->open .status );
@@ -232,13 +230,13 @@ static void gatt_client_event_handler(
232
230
* * bool is_primary
233
231
*/
234
232
case ESP_GATTC_SEARCH_RES_EVT: {
235
- BLEDevice *pDevice = BLEUtils::findByConnId (param->search_res .conn_id );
233
+ BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->search_res .conn_id );
236
234
pDevice->addService (param->search_res .srvc_id );
237
235
break ;
238
236
}
239
237
240
238
case ESP_GATTC_SEARCH_CMPL_EVT: {
241
- BLEDevice *pDevice = BLEUtils::findByConnId (param->search_cmpl .conn_id );
239
+ BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->search_cmpl .conn_id );
242
240
pDevice->onSearchComplete ();
243
241
break ;
244
242
}
@@ -253,7 +251,7 @@ static void gatt_client_event_handler(
253
251
*/
254
252
case ESP_GATTC_GET_CHAR_EVT: {
255
253
if (param->get_char .status == ESP_GATT_OK) {
256
- BLEDevice *pDevice = BLEUtils::findByConnId (param->get_char .conn_id );
254
+ BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->get_char .conn_id );
257
255
BLECharacteristicXXX characteristic (param->get_char .conn_id ,
258
256
param->get_char .srvc_id , param->get_char .char_id , param->get_char .char_prop );
259
257
pDevice->onCharacteristic (characteristic);
@@ -275,7 +273,7 @@ static void gatt_client_event_handler(
275
273
*/
276
274
case ESP_GATTC_READ_CHAR_EVT: {
277
275
if (param->read .status == ESP_GATT_OK) {
278
- BLEDevice *pDevice = BLEUtils::findByConnId (param->read .conn_id );
276
+ BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->read .conn_id );
279
277
std::string data = std::string ((char *)param->read .value , param->read .value_len );
280
278
pDevice->onRead (data);
281
279
}
@@ -299,10 +297,9 @@ static void gap_event_handler(
299
297
BLEUtils::dumpGapEvent (event, param);
300
298
301
299
switch (event) {
300
+ /*
302
301
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
303
- BLEDevice device;
304
-
305
- ESP_LOGD (LOG_TAG, " search_evt: %s" , bt_gap_search_event_type_to_string (param->scan_rst .search_evt ).c_str ());
302
+ BLERemoteDevice device;
306
303
307
304
if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) {
308
305
//ESP_LOGD(tag, "num_resps: %d", param->scan_rst.num_resps);
@@ -315,23 +312,24 @@ static void gap_event_handler(
315
312
//ESP_LOGD(tag, "rssi: %d", param->scan_rst.rssi);
316
313
//ESP_LOGD(tag, "addr_type: %s", bt_addr_t_to_string(param->scan_rst.ble_addr_type));
317
314
//ESP_LOGD(tag, "flag: %d", param->scan_rst.flag);
318
- device.setAddress (std::string (( char *) param->scan_rst .bda , 6 ));
315
+ device.setAddress(BLEAddress( param->scan_rst.bda));
319
316
device.setRSSI(param->scan_rst.rssi);
320
317
device.setAdFlag(param->scan_rst.flag);
321
318
//device.dump();
322
319
device.parsePayload((uint8_t *)param->scan_rst.ble_adv);
323
- g_devices.insert (std::pair<std::string,BLEDevice >(device.getAddress (),device));
320
+ g_devices.insert(std::pair<std::string,BLERemoteDevice >(device.getAddress().toString (),device));
324
321
//dump_adv_payload(param->scan_rst.ble_adv);
325
322
} else {
326
- ESP_LOGD (LOG_TAG, " Unhandled search_evt type!" );
323
+ ESP_LOGD(LOG_TAG, "Un-handled search_evt type!");
327
324
}
328
325
break;
329
326
} // ESP_GAP_BLE_SCAN_RESULT_EVT
327
+ */
330
328
331
329
case ESP_GAP_BLE_SEC_REQ_EVT: {
332
330
esp_err_t errRc = ::esp_ble_gap_security_rsp (param->ble_security .ble_req .bd_addr , true );
333
331
if (errRc != ESP_OK) {
334
- ESP_LOGE (LOG_TAG, " esp_ble_gap_security_rsp: rc=%d %s" , errRc, espToString (errRc));
332
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_security_rsp: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
335
333
}
336
334
break ;
337
335
}
@@ -352,7 +350,7 @@ static void gap_event_handler(
352
350
/* *
353
351
* @brief Get the current set of known devices.
354
352
*/
355
- std::map<ble_address, BLEDevice > getDevices () {
353
+ std::map<std::string, BLERemoteDevice > getDevices () {
356
354
return g_devices;
357
355
} // getDevices
358
356
@@ -365,51 +363,51 @@ std::map<ble_address, BLEDevice> getDevices() {
365
363
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
366
364
esp_err_t errRc = esp_bt_controller_init (&bt_cfg);
367
365
if (errRc != ESP_OK) {
368
- ESP_LOGE (LOG_TAG, " esp_bt_controller_init: rc=%d %s" , errRc, espToString (errRc));
366
+ ESP_LOGE (LOG_TAG, " esp_bt_controller_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
369
367
return ;
370
368
}
371
369
372
370
373
371
errRc = esp_bt_controller_enable (ESP_BT_MODE_BTDM);
374
372
if (errRc != ESP_OK) {
375
- ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, espToString (errRc));
373
+ ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
376
374
return ;
377
375
}
378
376
379
377
errRc = esp_bluedroid_init ();
380
378
if (errRc != ESP_OK) {
381
- ESP_LOGE (LOG_TAG, " esp_bluedroid_init: rc=%d %s" , errRc, espToString (errRc));
379
+ ESP_LOGE (LOG_TAG, " esp_bluedroid_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
382
380
return ;
383
381
}
384
382
385
383
errRc = esp_bluedroid_enable ();
386
384
if (errRc != ESP_OK) {
387
- ESP_LOGE (LOG_TAG, " esp_bluedroid_enable: rc=%d %s" , errRc, espToString (errRc));
385
+ ESP_LOGE (LOG_TAG, " esp_bluedroid_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
388
386
return ;
389
387
}
390
388
391
389
errRc = esp_ble_gap_register_callback (gap_event_handler);
392
390
if (errRc != ESP_OK) {
393
- ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback: rc=%d %s" , errRc, espToString (errRc));
391
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
394
392
return ;
395
393
}
396
394
397
395
errRc = esp_ble_gatts_register_callback (gatt_server_event_handler);
398
396
if (errRc != ESP_OK) {
399
- ESP_LOGE (LOG_TAG, " esp_ble_gatts_register_callback: rc=%d %s" , errRc, espToString (errRc));
397
+ ESP_LOGE (LOG_TAG, " esp_ble_gatts_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
400
398
return ;
401
399
}
402
400
403
401
errRc = ::esp_ble_gap_set_device_name (deviceName.c_str ());
404
402
if (errRc != ESP_OK) {
405
- ESP_LOGE (LOG_TAG, " esp_ble_gap_set_device_name: rc=%d %s" , errRc, espToString (errRc));
403
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_set_device_name: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
406
404
return ;
407
405
};
408
406
409
407
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
410
408
errRc = ::esp_ble_gap_set_security_param (ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof (uint8_t ));
411
409
if (errRc != ESP_OK) {
412
- ESP_LOGE (LOG_TAG, " esp_ble_gap_set_security_param: rc=%d %s" , errRc, espToString (errRc));
410
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_set_security_param: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
413
411
return ;
414
412
};
415
413
@@ -424,44 +422,44 @@ void BLE::initClient() {
424
422
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
425
423
esp_err_t errRc = esp_bt_controller_init (&bt_cfg);
426
424
if (errRc != ESP_OK) {
427
- ESP_LOGE (LOG_TAG, " esp_bt_controller_init: rc=%d %s" , errRc, espToString (errRc));
425
+ ESP_LOGE (LOG_TAG, " esp_bt_controller_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
428
426
return ;
429
427
}
430
428
431
429
432
430
errRc = esp_bt_controller_enable (ESP_BT_MODE_BTDM);
433
431
if (errRc != ESP_OK) {
434
- ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, espToString (errRc));
432
+ ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
435
433
return ;
436
434
}
437
435
438
436
errRc = esp_bluedroid_init ();
439
437
if (errRc != ESP_OK) {
440
- ESP_LOGE (LOG_TAG, " esp_bluedroid_init: rc=%d %s" , errRc, espToString (errRc));
438
+ ESP_LOGE (LOG_TAG, " esp_bluedroid_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
441
439
return ;
442
440
}
443
441
444
442
errRc = esp_bluedroid_enable ();
445
443
if (errRc != ESP_OK) {
446
- ESP_LOGE (LOG_TAG, " esp_bluedroid_enable: rc=%d %s" , errRc, espToString (errRc));
444
+ ESP_LOGE (LOG_TAG, " esp_bluedroid_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
447
445
return ;
448
446
}
449
447
450
448
errRc = esp_ble_gap_register_callback (gap_event_handler);
451
449
if (errRc != ESP_OK) {
452
- ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback: rc=%d %s" , errRc, espToString (errRc));
450
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
453
451
return ;
454
452
}
455
453
456
454
errRc = esp_ble_gattc_register_callback (gatt_client_event_handler);
457
455
if (errRc != ESP_OK) {
458
- ESP_LOGE (LOG_TAG, " esp_ble_gattc_register_callback: rc=%d %s" , errRc, espToString (errRc));
456
+ ESP_LOGE (LOG_TAG, " esp_ble_gattc_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
459
457
return ;
460
458
}
461
459
462
460
errRc = esp_ble_gattc_app_register (0 );
463
461
if (errRc != ESP_OK) {
464
- ESP_LOGE (LOG_TAG, " esp_ble_gattc_app_register: rc=%d %s" , errRc, espToString (errRc));
462
+ ESP_LOGE (LOG_TAG, " esp_ble_gattc_app_register: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
465
463
return ;
466
464
}
467
465
g_eventGroup = xEventGroupCreate ();
@@ -479,6 +477,7 @@ void BLE::initClient() {
479
477
* @param [in] scanType The type of scanning requested. The choices are `BLE_SCA_TYPE_PASSIVE` and `BLE_SCAN_TYPE_ACTIVE`.
480
478
* The distinction between them is whether or not the advertizer has a scan response requested from it.
481
479
*/
480
+ /*
482
481
void BLE::scan(int duration, esp_ble_scan_type_t scan_type) {
483
482
g_devices.clear();
484
483
static esp_ble_scan_params_t ble_scan_params;
@@ -489,7 +488,7 @@ void BLE::scan(int duration, esp_ble_scan_type_t scan_type) {
489
488
ble_scan_params.scan_window = 0x30;
490
489
esp_err_t errRc = esp_ble_gap_set_scan_params(&ble_scan_params);
491
490
if (errRc != ESP_OK) {
492
- ESP_LOGE (LOG_TAG, " esp_ble_gap_set_scan_params: rc=%d %s" , errRc, espToString (errRc));
491
+ ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: rc=%d %s", errRc, GeneralUtils::errorToString (errRc));
493
492
return;
494
493
}
495
494
@@ -498,15 +497,13 @@ void BLE::scan(int duration, esp_ble_scan_type_t scan_type) {
498
497
ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: rc=%d", errRc);
499
498
return;
500
499
}
501
- /*
502
- * Block until done
503
- */
500
+
504
501
xEventGroupWaitBits(g_eventGroup,
505
502
EVENT_GROUP_SCAN_COMPLETE,
506
503
1, // Clear on exit
507
504
0, // Wait for all bits
508
505
portMAX_DELAY);
509
506
ESP_LOGD(LOG_TAG, "Scan complete! - BLE:scan() returning.");
510
507
} // scan
511
-
508
+ */
512
509
#endif // CONFIG_BT_ENABLED
0 commit comments