summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/src/BLEDevice.cpp
blob: 4dbbc6e50afaf4e86871fe60103ceeb4e8c2e723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
 * BLE.cpp
 *
 *  Created on: Mar 16, 2017
 *      Author: kolban
 */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
#include <esp_err.h>
#include <nvs_flash.h>
#include <esp_bt.h>            // ESP32 BLE 
#include <esp_bt_device.h>     // ESP32 BLE 
#include <esp_bt_main.h>       // ESP32 BLE 
#include <esp_gap_ble_api.h>   // ESP32 BLE 
#include <esp_gatts_api.h>     // ESP32 BLE 
#include <esp_gattc_api.h>     // ESP32 BLE 
#include <esp_gatt_common_api.h>// ESP32 BLE 
#include <esp_err.h>           // ESP32 ESP-IDF 
#include <map>                 // Part of C++ Standard library 
#include <sstream>             // Part of C++ Standard library 
#include <iomanip>             // Part of C++ Standard library 
 
#include "BLEDevice.h"
#include "BLEClient.h"
#include "BLEUtils.h"
#include "GeneralUtils.h"
 
#if defined(ARDUINO_ARCH_ESP32)
#include "esp32-hal-bt.h"
#endif
 
#if defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG = "BLEDevice";
#endif
 
 
/**
 * Singletons for the BLEDevice.
 */
BLEServer* BLEDevice::m_pServer = nullptr;
BLEScan*   BLEDevice::m_pScan   = nullptr;
BLEClient* BLEDevice::m_pClient = nullptr;
bool       initialized          = false;  
esp_ble_sec_act_t BLEDevice::m_securityLevel = (esp_ble_sec_act_t)0;
BLESecurityCallbacks* BLEDevice::m_securityCallbacks = nullptr;
uint16_t   BLEDevice::m_localMTU = 23;  // not sure if this variable is useful 
BLEAdvertising* BLEDevice::m_bleAdvertising = nullptr;
uint16_t BLEDevice::m_appId = 0;
std::map<uint16_tconn_status_t> BLEDevice::m_connectedClientsMap;
gap_event_handler BLEDevice::m_customGapHandler = nullptr;
gattc_event_handler BLEDevice::m_customGattcHandler = nullptr;
gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
 
/**
 * @brief Create a new instance of a client.
 * @return A new instance of the client.
 */
/* STATIC */ BLEClient* BLEDevice::createClient() {
ESP_LOGD(LOG_TAG, ">> createClient");
#ifndef CONFIG_GATTC_ENABLE  // Check that BLE GATTC is enabled in make menuconfig 
ESP_LOGE(LOG_TAG, "BLE GATTC is not enabled - CONFIG_GATTC_ENABLE not defined");
abort();
#endif  // CONFIG_GATTC_ENABLE 
m_pClient = new BLEClient();
ESP_LOGD(LOG_TAG, "<< createClient");
return m_pClient;
} // createClient 
 
 
/**
 * @brief Create a new instance of a server.
 * @return A new instance of the server.
 */
/* STATIC */ BLEServer* BLEDevice::createServer() {
ESP_LOGD(LOG_TAG, ">> createServer");
#ifndef CONFIG_GATTS_ENABLE  // Check that BLE GATTS is enabled in make menuconfig 
ESP_LOGE(LOG_TAG, "BLE GATTS is not enabled - CONFIG_GATTS_ENABLE not defined");
abort();
#endif // CONFIG_GATTS_ENABLE 
m_pServer = new BLEServer();
m_pServer->createApp(m_appId++);
ESP_LOGD(LOG_TAG, "<< createServer");
return m_pServer;
} // createServer 
 
 
/**
 * @brief Handle GATT server events.
 *
 * @param [in] event The event that has been newly received.
 * @param [in] gatts_if The connection to the GATT interface.
 * @param [in] param Parameters for the event.
 */
/* STATIC */ void BLEDevice::gattServerEventHandler(
   esp_gatts_cb_event_t      event,
   esp_gatt_if_t             gatts_if,
   esp_ble_gatts_cb_param_t* param
) {
ESP_LOGD(LOG_TAG, "gattServerEventHandler [esp_gatt_if: %d] ... %s",
gatts_if,
BLEUtils::gattServerEventTypeToString(event).c_str());
 
BLEUtils::dumpGattServerEvent(event, gatts_if, param);
 
switch (event) {
case ESP_GATTS_CONNECT_EVT: {
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
if(BLEDevice::m_securityLevel){
esp_ble_set_encryption(param->connect.remote_bda, BLEDevice::m_securityLevel);
}
#endif // CONFIG_BLE_SMP_ENABLE 
break;
} // ESP_GATTS_CONNECT_EVT 
 
default{
break;
}
} // switch 
 
 
if (BLEDevice::m_pServer != nullptr{
BLEDevice::m_pServer->handleGATTServerEvent(event, gatts_if, param);
}
 
if(m_customGattsHandler != nullptr{
m_customGattsHandler(event, gatts_if, param);
}
 
} // gattServerEventHandler 
 
 
/**
 * @brief Handle GATT client events.
 *
 * Handler for the GATT client events.
 *
 * @param [in] event
 * @param [in] gattc_if
 * @param [in] param
 */
/* STATIC */ void BLEDevice::gattClientEventHandler(
esp_gattc_cb_event_t      event,
esp_gatt_if_t             gattc_if,
esp_ble_gattc_cb_param_t* param) {
 
ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s",
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
BLEUtils::dumpGattClientEvent(event, gattc_if, param);
 
switch(event) {
case ESP_GATTC_CONNECT_EVT: {
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
if(BLEDevice::m_securityLevel){
esp_ble_set_encryption(param->connect.remote_bda, BLEDevice::m_securityLevel);
}
#endif // CONFIG_BLE_SMP_ENABLE 
break;
} // ESP_GATTS_CONNECT_EVT 
 
default:
break;
} // switch 
for(auto &myPair : BLEDevice::getPeerDevices(true)) {
conn_status_t conn_status = (conn_status_t)myPair.second;
if(((BLEClient*)conn_status.peer_device)->getGattcIf() == gattc_if || ((BLEClient*)conn_status.peer_device)->getGattcIf() == ESP_GATT_IF_NONE || gattc_if == ESP_GATT_IF_NONE){
((BLEClient*)conn_status.peer_device)->gattClientEventHandler(event, gattc_if, param);
}
}
 
if(m_customGattcHandler != nullptr{
m_customGattcHandler(event, gattc_if, param);
}
 
 
} // gattClientEventHandler 
 
 
/**
 * @brief Handle GAP events.
 */
/* STATIC */ void BLEDevice::gapEventHandler(
esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t *param) {
 
BLEUtils::dumpGapEvent(event, param);
 
switch(event) {
 
 case ESP_GAP_BLE_OOB_REQ_EVT:                                /* OOB request event */
 ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_OOB_REQ_EVT");
 break;
 case ESP_GAP_BLE_LOCAL_IR_EVT:                               /* BLE local IR event */
 ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_LOCAL_IR_EVT");
 break;
 case ESP_GAP_BLE_LOCAL_ER_EVT:                               /* BLE local ER event */
 ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_LOCAL_ER_EVT");
 break;
 case ESP_GAP_BLE_NC_REQ_EVT: /*  NUMERIC CONFIRMATION  */
 ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_NC_REQ_EVT");
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
if(BLEDevice::m_securityCallbacks != nullptr){
   esp_ble_confirm_reply(param->ble_security.ble_req.bd_addr, BLEDevice::m_securityCallbacks->onConfirmPIN(param->ble_security.key_notif.passkey));
}
#endif // CONFIG_BLE_SMP_ENABLE 
 break;
 case ESP_GAP_BLE_PASSKEY_REQ_EVT:                           /* passkey request event */
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_PASSKEY_REQ_EVT: ");
// esp_log_buffer_hex(LOG_TAG, m_remote_bda, sizeof(m_remote_bda)); 
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
if(BLEDevice::m_securityCallbacks != nullptr){
esp_ble_passkey_reply(param->ble_security.ble_req.bd_addrtrue, BLEDevice::m_securityCallbacks->onPassKeyRequest());
}
#endif // CONFIG_BLE_SMP_ENABLE 
break;
/*
 * TODO should we add white/black list comparison?
 */
 case ESP_GAP_BLE_SEC_REQ_EVT:
 /* send the positive(true) security response to the peer device to accept the security request.
 If not accept the security request, should sent the security response with negative(false) accept value*/
 ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_SEC_REQ_EVT");
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
if(BLEDevice::m_securityCallbacks!=nullptr){
esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, BLEDevice::m_securityCallbacks->onSecurityRequest());
}
else{
esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addrtrue);
}
#endif // CONFIG_BLE_SMP_ENABLE 
break;
 /*
  *
  */
 case ESP_GAP_BLE_PASSKEY_NOTIF_EVT:  //the app will receive this evt when the IO  has Output capability and the peer device IO has Input capability. 
 //display the passkey number to the user to input it in the peer deivce within 30 seconds 
 ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_PASSKEY_NOTIF_EVT");
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
ESP_LOGI(LOG_TAG, "passKey = %d", param->ble_security.key_notif.passkey);
if(BLEDevice::m_securityCallbacks!=nullptr){
BLEDevice::m_securityCallbacks->onPassKeyNotify(param->ble_security.key_notif.passkey);
}
#endif // CONFIG_BLE_SMP_ENABLE 
 break;
 case ESP_GAP_BLE_KEY_EVT:
 //shows the ble key type info share with peer device to the user. 
 ESP_LOGD(LOG_TAG, "ESP_GAP_BLE_KEY_EVT");
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
 ESP_LOGI(LOG_TAG, "key type = %s", BLESecurity::esp_key_type_to_str(param->ble_security.ble_key.key_type));
#endif // CONFIG_BLE_SMP_ENABLE 
 break;
 case ESP_GAP_BLE_AUTH_CMPL_EVT:
 ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_AUTH_CMPL_EVT");
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
 if(BLEDevice::m_securityCallbacks != nullptr){
 BLEDevice::m_securityCallbacks->onAuthenticationComplete(param->ble_security.auth_cmpl);
 }
#endif // CONFIG_BLE_SMP_ENABLE 
 break;
default{
break;
}
} // switch 
 
if (BLEDevice::m_pClient != nullptr{
BLEDevice::m_pClient->handleGAPEvent(event, param);
}
 
if (BLEDevice::m_pScan != nullptr{
BLEDevice::getScan()->handleGAPEvent(event, param);
}
 
if(m_bleAdvertising != nullptr{
BLEDevice::getAdvertising()->handleGAPEvent(event, param);
}
 
if(m_customGapHandler != nullptr{
BLEDevice::m_customGapHandler(event, param);
}
 
} // gapEventHandler 
 
 
/**
 * @brief Get the BLE device address.
 * @return The BLE device address.
 */
/* STATIC*/ BLEAddress BLEDevice::getAddress() {
const uint8_t* bdAddr = esp_bt_dev_get_address();
esp_bd_addr_t addr;
memcpy(addr, bdAddr, sizeof(addr));
return BLEAddress(addr);
} // getAddress 
 
 
/**
 * @brief Retrieve the Scan object that we use for scanning.
 * @return The scanning object reference.  This is a singleton object.  The caller should not
 * try and release/delete it.
 */
/* STATIC */ BLEScan* BLEDevice::getScan() {
//ESP_LOGD(LOG_TAG, ">> getScan"); 
if (m_pScan == nullptr{
m_pScan = new BLEScan();
//ESP_LOGD(LOG_TAG, " - creating a new scan object"); 
}
//ESP_LOGD(LOG_TAG, "<< getScan: Returning object at 0x%x", (uint32_t)m_pScan); 
return m_pScan;
} // getScan 
 
 
/**
 * @brief Get the value of a characteristic of a service on a remote device.
 * @param [in] bdAddress
 * @param [in] serviceUUID
 * @param [in] characteristicUUID
 */
/* STATIC */ std::string BLEDevice::getValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID) {
ESP_LOGD(LOG_TAG, ">> getValue: bdAddress: %s, serviceUUID: %s, characteristicUUID: %s", bdAddress.toString().c_str(), serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
BLEClient* pClient = createClient();
pClient->connect(bdAddress);
std::string ret = pClient->getValue(serviceUUID, characteristicUUID);
pClient->disconnect();
ESP_LOGD(LOG_TAG, "<< getValue");
return ret;
} // getValue 
 
 
/**
 * @brief Initialize the %BLE environment.
 * @param deviceName The device name of the device.
 */
/* STATIC */ void BLEDevice::init(std::string deviceName) {
if(!initialized){
initialized = true// Set the initialization flag to ensure we are only initialized once. 
 
esp_err_t errRc = ESP_OK;
#ifdef ARDUINO_ARCH_ESP32
if (!btStart()) {
errRc = ESP_FAIL;
return;
}
#else
errRc = ::nvs_flash_init();
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "nvs_flash_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
 
#ifndef CLASSIC_BT_ENABLED
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);  
#endif
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
errRc = esp_bt_controller_init(&bt_cfg);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
 
#ifndef CLASSIC_BT_ENABLED
errRc = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
#else
errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
#endif
#endif
 
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
errRc = esp_bluedroid_init();
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
}
 
if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
errRc = esp_bluedroid_enable();
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
}
 
errRc = esp_ble_gap_register_callback(BLEDevice::gapEventHandler);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
 
#ifdef CONFIG_GATTC_ENABLE   // Check that BLE client is configured in make menuconfig 
errRc = esp_ble_gattc_register_callback(BLEDevice::gattClientEventHandler);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
#endif   // CONFIG_GATTC_ENABLE 
 
#ifdef CONFIG_GATTS_ENABLE  // Check that BLE server is configured in make menuconfig 
errRc = esp_ble_gatts_register_callback(BLEDevice::gattServerEventHandler);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gatts_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
#endif   // CONFIG_GATTS_ENABLE 
 
errRc = ::esp_ble_gap_set_device_name(deviceName.c_str());
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_device_name: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
};
 
#ifdef CONFIG_BLE_SMP_ENABLE   // Check that BLE SMP (security) is configured in make menuconfig 
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
errRc = ::esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_security_param: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
};
#endif // CONFIG_BLE_SMP_ENABLE 
}
vTaskDelay(200 / portTICK_PERIOD_MS); // Delay for 200 msecs as a workaround to an apparent Arduino environment issue. 
} // init 
 
void BLEDevice::uninit() {
  if (initialized) {
    initialized = false;
    esp_bluedroid_disable();
    esp_bt_controller_disable();
    esp_bt_controller_deinit();
  }
}
 
/**
 * @brief Set the transmission power.
 * The power level can be one of:
 * * ESP_PWR_LVL_N14
 * * ESP_PWR_LVL_N11
 * * ESP_PWR_LVL_N8
 * * ESP_PWR_LVL_N5
 * * ESP_PWR_LVL_N2
 * * ESP_PWR_LVL_P1
 * * ESP_PWR_LVL_P4
 * * ESP_PWR_LVL_P7
 * @param [in] powerLevel.
 */
/* STATIC */ void BLEDevice::setPower(esp_power_level_t powerLevel) {
ESP_LOGD(LOG_TAG, ">> setPower: %d", powerLevel);
esp_err_t errRc = ::esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, powerLevel);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_tx_power_set: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
};
ESP_LOGD(LOG_TAG, "<< setPower");
} // setPower 
 
 
/**
 * @brief Set the value of a characteristic of a service on a remote device.
 * @param [in] bdAddress
 * @param [in] serviceUUID
 * @param [in] characteristicUUID
 */
/* STATIC */ void BLEDevice::setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value) {
ESP_LOGD(LOG_TAG, ">> setValue: bdAddress: %s, serviceUUID: %s, characteristicUUID: %s", bdAddress.toString().c_str(), serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
BLEClient* pClient = createClient();
pClient->connect(bdAddress);
pClient->setValue(serviceUUID, characteristicUUID, value);
pClient->disconnect();
} // setValue 
 
 
/**
 * @brief Return a string representation of the nature of this device.
 * @return A string representation of the nature of this device.
 */
/* STATIC */ std::string BLEDevice::toString() {
std::ostringstream oss;
oss << "BD Address: " << getAddress().toString();
return oss.str();
} // toString 
 
 
/**
 * @brief Add an entry to the BLE white list.
 * @param [in] address The address to add to the white list.
 */
void BLEDevice::whiteListAdd(BLEAddress address) {
ESP_LOGD(LOG_TAG, ">> whiteListAdd: %s", address.toString().c_str());
esp_err_t errRc = esp_ble_gap_update_whitelist(true, *address.getNative());  // True to add an entry. 
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_update_whitelist: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
ESP_LOGD(LOG_TAG, "<< whiteListAdd");
} // whiteListAdd 
 
 
/**
 * @brief Remove an entry from the BLE white list.
 * @param [in] address The address to remove from the white list.
 */
void BLEDevice::whiteListRemove(BLEAddress address) {
ESP_LOGD(LOG_TAG, ">> whiteListRemove: %s", address.toString().c_str());
esp_err_t errRc = esp_ble_gap_update_whitelist(false, *address.getNative());  // False to remove an entry. 
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_update_whitelist: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
ESP_LOGD(LOG_TAG, "<< whiteListRemove");
} // whiteListRemove 
 
/*
 * @brief Set encryption level that will be negotiated with peer device durng connection
 * @param [in] level Requested encryption level
 */
void BLEDevice::setEncryptionLevel(esp_ble_sec_act_t level) {
BLEDevice::m_securityLevel = level;
}
 
/*
 * @brief Set callbacks that will be used to handle encryption negotiation events and authentication events
 * @param [in] cllbacks Pointer to BLESecurityCallbacks class callback
 */
void BLEDevice::setSecurityCallbacks(BLESecurityCallbacks* callbacks) {
BLEDevice::m_securityCallbacks = callbacks;
}
 
/*
 * @brief Setup local mtu that will be used to negotiate mtu during request from client peer
 * @param [in] mtu Value to set local mtu, should be larger than 23 and lower or equal to 517
 */
esp_err_t BLEDevice::setMTU(uint16_t mtu) {
ESP_LOGD(LOG_TAG, ">> setLocalMTU: %d", mtu);
esp_err_t err = esp_ble_gatt_set_local_mtu(mtu);
if (err == ESP_OK) {
m_localMTU = mtu;
} else {
ESP_LOGE(LOG_TAG, "can't set local mtu value: %d", mtu);
}
ESP_LOGD(LOG_TAG, "<< setLocalMTU");
return err;
}
 
/*
 * @brief Get local MTU value set during mtu request or default value
 */
uint16_t BLEDevice::getMTU() {
return m_localMTU;
}
 
bool BLEDevice::getInitialized() {
return initialized;
}
 
BLEAdvertising* BLEDevice::getAdvertising() {
if(m_bleAdvertising == nullptr{
m_bleAdvertising = new BLEAdvertising();
ESP_LOGI(LOG_TAG, "create advertising");
}
ESP_LOGD(LOG_TAG, "get advertising");
return m_bleAdvertising; 
}
 
void BLEDevice::startAdvertising() {
ESP_LOGD(LOG_TAG, ">> startAdvertising");
getAdvertising()->start();
ESP_LOGD(LOG_TAG, "<< startAdvertising");
} // startAdvertising 
 
/* multi connect support */
/* requires a little more work */
std::map<uint16_tconn_status_t> BLEDevice::getPeerDevices(bool _client) {
return m_connectedClientsMap;
}
 
BLEClient* BLEDevice::getClientByGattIf(uint16_t conn_id) {
return (BLEClient*)m_connectedClientsMap.find(conn_id)->second.peer_device;
}
 
void BLEDevice::updatePeerDevice(void* peer, bool _client, uint16_t conn_id) {
ESP_LOGD(LOG_TAG, "update conn_id: %d, GATT role: %s", conn_id, _client? "client":"server");
std::map<uint16_tconn_status_t>::iterator it = m_connectedClientsMap.find(ESP_GATT_IF_NONE);
if (it != m_connectedClientsMap.end()) {
std::swap(m_connectedClientsMap[conn_id], it->second);
m_connectedClientsMap.erase(it);
}else{
it = m_connectedClientsMap.find(conn_id);
if (it != m_connectedClientsMap.end()) {
conn_status_t _st = it->second;
_st.peer_device = peer;
std::swap(m_connectedClientsMap[conn_id], _st);
}
}
}
 
void BLEDevice::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
ESP_LOGI(LOG_TAG, "add conn_id: %d, GATT role: %s", conn_id, _client? "client":"server");
conn_status_t status = {
.peer_device = peer,
.connected = true,
.mtu = 23
};
 
m_connectedClientsMap.insert(std::pair<uint16_tconn_status_t>(conn_id, status));
}
 
void BLEDevice::removePeerDevice(uint16_t conn_id, bool _client) {
ESP_LOGI(LOG_TAG, "remove: %d, GATT role %s", conn_id, _client?"client":"server");
if(m_connectedClientsMap.find(conn_id) != m_connectedClientsMap.end())
m_connectedClientsMap.erase(conn_id);
}
 
/* multi connect support */
 
/**
 * @brief de-Initialize the %BLE environment.
 * @param release_memory release the internal BT stack memory
 */
/* STATIC */ void BLEDevice::deinit(bool release_memory) {
    if (!initialized) return;
 
    esp_bluedroid_disable();
    esp_bluedroid_deinit();
    esp_bt_controller_disable();
    esp_bt_controller_deinit();
#ifndef ARDUINO_ARCH_ESP32
    if (release_memory) {
        esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);  // <-- require tests because we released classic BT memory and this can cause crash (most likely not, esp-idf takes care of it) 
    } else {
        initialized = false;   
    } 
#endif
}
 
void BLEDevice::setCustomGapHandler(gap_event_handler handler) {
m_customGapHandler = handler;
}
 
void BLEDevice::setCustomGattcHandler(gattc_event_handler handler) {
m_customGattcHandler = handler;
}
 
void BLEDevice::setCustomGattsHandler(gatts_event_handler handler) {
m_customGattsHandler = handler;
}
 
#endif // CONFIG_BT_ENABLED