23
23
#include < esp_err.h>
24
24
#include " BLEUtils.h"
25
25
#include " GeneralUtils.h"
26
- #include " string.h"
27
- #include " mbedtls/md.h"
28
- #include " mbedtls/entropy.h"
29
- #include " mbedtls/ctr_drbg.h"
30
-
31
-
32
- #define BLOCK_SIZE 64
33
- #define HMAC MBEDTLS_MD_SHA256
34
26
35
27
#ifdef ARDUINO_ARCH_ESP32
36
28
#include " esp32-hal-log.h"
37
29
#endif
38
- static bool is_advertising = false ;
30
+
39
31
static const char * LOG_TAG = " BLEAdvertising" ;
40
32
33
+
41
34
/* *
42
35
* @brief Construct a default advertising object.
43
36
*
44
37
*/
45
38
BLEAdvertising::BLEAdvertising () {
46
39
m_advData.set_scan_rsp = false ;
47
40
m_advData.include_name = true ;
48
- m_advData.include_txpower = false ;
41
+ m_advData.include_txpower = true ;
49
42
m_advData.min_interval = 0x20 ;
50
43
m_advData.max_interval = 0x40 ;
51
44
m_advData.appearance = 0x00 ;
@@ -63,70 +56,12 @@ BLEAdvertising::BLEAdvertising() {
63
56
m_advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
64
57
m_advParams.channel_map = ADV_CHNL_ALL;
65
58
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
59
+ m_advParams.peer_addr_type = BLE_ADDR_TYPE_PUBLIC;
66
60
67
61
m_customAdvData = false ; // No custom advertising data
68
62
m_customScanResponseData = false ; // No custom scan response data
69
63
} // BLEAdvertising
70
64
71
- void BLEAdvertising::setPrivateAddress (esp_ble_addr_type_t type) {
72
- esp_bd_addr_t addr;
73
- m_advParams.own_addr_type = type;
74
-
75
- if (type == BLE_ADDR_TYPE_RPA_PUBLIC) {
76
- esp_ble_gap_config_local_privacy (true );
77
- return ;
78
- }
79
- else {
80
- mbedtls_ctr_drbg_context ctr_drbg;
81
- mbedtls_entropy_context entropy;
82
- mbedtls_md_context_t ctx;
83
-
84
- char pers[] = " aes generate key" ;
85
- int ret;
86
- unsigned char key[BLOCK_SIZE] = {0 };
87
- const char inp[] = " random static address" ;
88
- unsigned char outp[BLOCK_SIZE/2 ];
89
-
90
- mbedtls_entropy_init ( &entropy );
91
- mbedtls_ctr_drbg_init ( &ctr_drbg );
92
-
93
- if ( ( ret = mbedtls_ctr_drbg_seed ( &ctr_drbg, mbedtls_entropy_func, &entropy,
94
- (unsigned char *) pers, strlen ( pers ) ) ) != 0 )
95
- {
96
- printf ( " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n " , -ret );
97
- goto exit;
98
- }
99
-
100
- if ( ( ret = mbedtls_ctr_drbg_random ( &ctr_drbg, key, BLOCK_SIZE ) ) != 0 )
101
- {
102
- printf ( " failed\n ! mbedtls_ctr_drbg_random returned -0x%04x\n " , -ret );
103
- goto exit;
104
- }
105
-
106
- mbedtls_md_init (&ctx);
107
- mbedtls_md_setup (&ctx, mbedtls_md_info_from_type (HMAC), true );
108
- mbedtls_md_hmac_starts (&ctx, (const unsigned char *)key, BLOCK_SIZE);
109
- mbedtls_md_hmac_update (&ctx, (const unsigned char *)inp, strlen (inp));
110
- mbedtls_md_hmac_finish (&ctx, outp);
111
- mbedtls_md_free (&ctx);
112
- ESP_LOG_BUFFER_HEX (" random key" , key, BLOCK_SIZE);
113
- ESP_LOG_BUFFER_HEX (" HASH step 2" , outp, BLOCK_SIZE/2 );
114
-
115
- memcpy (addr, outp, 6 );
116
- }
117
- if (type == BLE_ADDR_TYPE_RANDOM) {
118
- addr[0 ] &= 0x3F ; // <--- Format of non-resolvable private address 00xx xxxx
119
- }
120
- else {
121
- addr[0 ] |= 0xC0 ; // <--- Format of static address 11xx xxxx
122
- }
123
-
124
- esp_ble_gap_set_rand_addr (addr);
125
- ESP_LOG_BUFFER_HEX (" random address" , addr, 6 );
126
- exit:
127
- return ;
128
- }
129
-
130
65
131
66
/* *
132
67
* @brief Add a service uuid to exposed list of services.
@@ -158,22 +93,31 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
158
93
} // setAppearance
159
94
160
95
void BLEAdvertising::setMinInterval (uint16_t mininterval) {
161
- m_advData.min_interval = mininterval;
162
96
m_advParams.adv_int_min = mininterval;
163
97
} // setMinInterval
164
98
165
99
void BLEAdvertising::setMaxInterval (uint16_t maxinterval) {
166
- m_advData.max_interval = maxinterval;
167
100
m_advParams.adv_int_max = maxinterval;
168
101
} // setMaxInterval
169
102
103
+ void BLEAdvertising::setMinPreferred (uint16_t mininterval) {
104
+ m_advData.min_interval = mininterval;
105
+ } //
106
+
107
+ void BLEAdvertising::setMaxPreferred (uint16_t maxinterval) {
108
+ m_advData.max_interval = maxinterval;
109
+ } //
110
+
111
+ void BLEAdvertising::setScanResponse (bool set) {
112
+ m_scanResp = set;
113
+ }
170
114
171
115
/* *
172
116
* @brief Set the filtering for the scan filter.
173
117
* @param [in] scanRequestWhitelistOnly If true, only allow scan requests from those on the white list.
174
118
* @param [in] connectWhitelistOnly If true, only allow connections from those on the white list.
175
119
*/
176
- void BLEAdvertising::setScanFilter (bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
120
+ void BLEAdvertising::setScanFilter (bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
177
121
ESP_LOGD (LOG_TAG, " >> setScanFilter: scanRequestWhitelistOnly: %d, connectWhitelistOnly: %d" , scanRequestWhitelistOnly, connectWhitelistOnly);
178
122
if (!scanRequestWhitelistOnly && !connectWhitelistOnly) {
179
123
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
@@ -239,21 +183,20 @@ void BLEAdvertising::setScanResponseData(BLEAdvertisementData& advertisementData
239
183
void BLEAdvertising::start () {
240
184
ESP_LOGD (LOG_TAG, " >> start: customAdvData: %d, customScanResponseData: %d" , m_customAdvData, m_customScanResponseData);
241
185
242
-
243
186
// We have a vector of service UUIDs that we wish to advertise. In order to use the
244
187
// ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
245
188
// representations. If we have 1 or more services to advertise then we allocate enough
246
189
// storage to host them and then copy them in one at a time into the contiguous storage.
247
190
int numServices = m_serviceUUIDs.size ();
248
191
if (numServices > 0 ) {
249
- m_advData.service_uuid_len = 16 * numServices;
250
- m_advData.p_service_uuid = new uint8_t [m_advData.service_uuid_len ];
192
+ m_advData.service_uuid_len = 16 * numServices;
193
+ m_advData.p_service_uuid = new uint8_t [m_advData.service_uuid_len ];
251
194
uint8_t * p = m_advData.p_service_uuid ;
252
- for (int i= 0 ; i< numServices; i++) {
195
+ for (int i = 0 ; i < numServices; i++) {
253
196
ESP_LOGD (LOG_TAG, " - advertising service: %s" , m_serviceUUIDs[i].toString ().c_str ());
254
197
BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128 ();
255
198
memcpy (p, serviceUUID128.getNative ()->uuid .uuid128 , 16 );
256
- p+= 16 ;
199
+ p += 16 ;
257
200
}
258
201
} else {
259
202
m_advData.service_uuid_len = 0 ;
@@ -262,45 +205,40 @@ void BLEAdvertising::start() {
262
205
263
206
esp_err_t errRc;
264
207
265
- if (!is_advertising){
266
-
267
- // m_semaphoreSetAdv.take("config_adv");
268
- if (m_customAdvData == false ) {
208
+ if (!m_customAdvData) {
269
209
// Set the configuration for advertising.
270
- m_advData.set_scan_rsp = false ;
271
- errRc = :: esp_ble_gap_config_adv_data (&m_advData) ;
272
- if (errRc != ESP_OK) {
273
- ESP_LOGE (LOG_TAG, " << esp_ble_gap_config_adv_data: rc=%d %s " , errRc, GeneralUtils::errorToString (errRc) );
274
- // m_semaphoreSetAdv.give();
275
- return ;
276
- }
210
+ m_advData.set_scan_rsp = false ;
211
+ m_advData. include_name = !m_scanResp ;
212
+ m_advData. include_txpower = !m_scanResp;
213
+ errRc = :: esp_ble_gap_config_adv_data (&m_advData );
214
+ if (errRc != ESP_OK) {
215
+ ESP_LOGE (LOG_TAG, " << esp_ble_gap_config_adv_data: rc=%d %s " , errRc, GeneralUtils::errorToString (errRc)) ;
216
+ return ;
277
217
}
218
+ }
278
219
279
- // m_semaphoreSetAdv.take("config_rsp");
280
- if (m_customScanResponseData == false ) {
281
- m_advData.set_scan_rsp = true ;
282
- errRc = ::esp_ble_gap_config_adv_data (&m_advData);
283
- if (errRc != ESP_OK) {
284
- ESP_LOGE (LOG_TAG, " << esp_ble_gap_config_adv_data (Scan response): rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
285
- // m_semaphoreSetAdv.give();
286
- return ;
287
- }
220
+ if (!m_customScanResponseData && m_scanResp) {
221
+ m_advData.set_scan_rsp = true ;
222
+ m_advData.include_name = m_scanResp;
223
+ m_advData.include_txpower = m_scanResp;
224
+ errRc = ::esp_ble_gap_config_adv_data (&m_advData);
225
+ if (errRc != ESP_OK) {
226
+ ESP_LOGE (LOG_TAG, " << esp_ble_gap_config_adv_data (Scan response): rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
227
+ return ;
288
228
}
289
229
}
230
+
290
231
// If we had services to advertise then we previously allocated some storage for them.
291
232
// Here we release that storage.
292
233
if (m_advData.service_uuid_len > 0 ) {
293
234
delete[] m_advData.p_service_uuid ;
294
235
m_advData.p_service_uuid = nullptr ;
295
236
}
296
- is_advertising = true ;
297
237
298
- // m_semaphoreSetAdv.take("start");
299
238
// Start advertising.
300
239
errRc = ::esp_ble_gap_start_advertising (&m_advParams);
301
240
if (errRc != ESP_OK) {
302
241
ESP_LOGE (LOG_TAG, " << esp_ble_gap_start_advertising: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
303
- // m_semaphoreSetAdv.give();
304
242
return ;
305
243
}
306
244
ESP_LOGD (LOG_TAG, " << start" );
@@ -314,7 +252,6 @@ void BLEAdvertising::start() {
314
252
*/
315
253
void BLEAdvertising::stop () {
316
254
ESP_LOGD (LOG_TAG, " >> stop" );
317
- // m_semaphoreSetAdv.take("stop");
318
255
esp_err_t errRc = ::esp_ble_gap_stop_advertising ();
319
256
if (errRc != ESP_OK) {
320
257
ESP_LOGE (LOG_TAG, " esp_ble_gap_stop_advertising: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
@@ -346,8 +283,7 @@ void BLEAdvertisementData::setAppearance(uint16_t appearance) {
346
283
char cdata[2 ];
347
284
cdata[0 ] = 3 ;
348
285
cdata[1 ] = ESP_BLE_AD_TYPE_APPEARANCE; // 0x19
349
- addData (std::string (cdata, 2 ) + std::string ((char *)&appearance,2 ));
350
- esp_ble_gap_config_local_icon (appearance);
286
+ addData (std::string (cdata, 2 ) + std::string ((char *) &appearance, 2 ));
351
287
} // setAppearance
352
288
353
289
@@ -357,28 +293,28 @@ void BLEAdvertisementData::setAppearance(uint16_t appearance) {
357
293
*/
358
294
void BLEAdvertisementData::setCompleteServices (BLEUUID uuid) {
359
295
char cdata[2 ];
360
- switch (uuid.bitSize ()) {
296
+ switch (uuid.bitSize ()) {
361
297
case 16 : {
362
298
// [Len] [0x02] [LL] [HH]
363
299
cdata[0 ] = 3 ;
364
300
cdata[1 ] = ESP_BLE_AD_TYPE_16SRV_CMPL; // 0x03
365
- addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid16 ,2 ));
301
+ addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid16 , 2 ));
366
302
break ;
367
303
}
368
304
369
305
case 32 : {
370
306
// [Len] [0x04] [LL] [LL] [HH] [HH]
371
307
cdata[0 ] = 5 ;
372
308
cdata[1 ] = ESP_BLE_AD_TYPE_32SRV_CMPL; // 0x05
373
- addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid32 ,4 ));
309
+ addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid32 , 4 ));
374
310
break ;
375
311
}
376
312
377
313
case 128 : {
378
314
// [Len] [0x04] [0] [1] ... [15]
379
315
cdata[0 ] = 17 ;
380
316
cdata[1 ] = ESP_BLE_AD_TYPE_128SRV_CMPL; // 0x07
381
- addData (std::string (cdata, 2 ) + std::string ((char *) uuid.getNative ()->uuid .uuid128 ,16 ));
317
+ addData (std::string (cdata, 2 ) + std::string ((char *) uuid.getNative ()->uuid .uuid128 , 16 ));
382
318
break ;
383
319
}
384
320
@@ -418,7 +354,7 @@ void BLEAdvertisementData::setManufacturerData(std::string data) {
418
354
char cdata[2 ];
419
355
cdata[0 ] = data.length () + 1 ;
420
356
cdata[1 ] = ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE; // 0xff
421
- addData (std::string (cdata, 2 ) + data);
357
+ addData (std::string (cdata, 2 ) + data);
422
358
ESP_LOGD (" BLEAdvertisementData" , " << setManufacturerData" );
423
359
} // setManufacturerData
424
360
@@ -432,7 +368,7 @@ void BLEAdvertisementData::setName(std::string name) {
432
368
char cdata[2 ];
433
369
cdata[0 ] = name.length () + 1 ;
434
370
cdata[1 ] = ESP_BLE_AD_TYPE_NAME_CMPL; // 0x09
435
- addData (std::string (cdata, 2 ) + name);
371
+ addData (std::string (cdata, 2 ) + name);
436
372
ESP_LOGD (" BLEAdvertisementData" , " << setName" );
437
373
} // setName
438
374
@@ -443,28 +379,28 @@ void BLEAdvertisementData::setName(std::string name) {
443
379
*/
444
380
void BLEAdvertisementData::setPartialServices (BLEUUID uuid) {
445
381
char cdata[2 ];
446
- switch (uuid.bitSize ()) {
382
+ switch (uuid.bitSize ()) {
447
383
case 16 : {
448
384
// [Len] [0x02] [LL] [HH]
449
385
cdata[0 ] = 3 ;
450
386
cdata[1 ] = ESP_BLE_AD_TYPE_16SRV_PART; // 0x02
451
- addData (std::string (cdata, 2 ) + std::string ((char *)&uuid.getNative ()->uuid .uuid16 ,2 ));
387
+ addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid16 , 2 ));
452
388
break ;
453
389
}
454
390
455
391
case 32 : {
456
392
// [Len] [0x04] [LL] [LL] [HH] [HH]
457
393
cdata[0 ] = 5 ;
458
394
cdata[1 ] = ESP_BLE_AD_TYPE_32SRV_PART; // 0x04
459
- addData (std::string (cdata, 2 ) + std::string ((char *)&uuid.getNative ()->uuid .uuid32 ,4 ));
395
+ addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid32 , 4 ));
460
396
break ;
461
397
}
462
398
463
399
case 128 : {
464
400
// [Len] [0x04] [0] [1] ... [15]
465
401
cdata[0 ] = 17 ;
466
402
cdata[1 ] = ESP_BLE_AD_TYPE_128SRV_PART; // 0x06
467
- addData (std::string (cdata, 2 ) + std::string ((char *)uuid.getNative ()->uuid .uuid128 ,16 ));
403
+ addData (std::string (cdata, 2 ) + std::string ((char *) & uuid.getNative ()->uuid .uuid128 , 16 ));
468
404
break ;
469
405
}
470
406
@@ -481,28 +417,28 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
481
417
*/
482
418
void BLEAdvertisementData::setServiceData (BLEUUID uuid, std::string data) {
483
419
char cdata[2 ];
484
- switch (uuid.bitSize ()) {
420
+ switch (uuid.bitSize ()) {
485
421
case 16 : {
486
422
// [Len] [0x16] [UUID16] data
487
423
cdata[0 ] = data.length () + 3 ;
488
424
cdata[1 ] = ESP_BLE_AD_TYPE_SERVICE_DATA; // 0x16
489
- addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid16 ,2 ) + data);
425
+ addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid16 , 2 ) + data);
490
426
break ;
491
427
}
492
428
493
429
case 32 : {
494
430
// [Len] [0x20] [UUID32] data
495
431
cdata[0 ] = data.length () + 5 ;
496
432
cdata[1 ] = ESP_BLE_AD_TYPE_32SERVICE_DATA; // 0x20
497
- addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid32 ,4 ) + data);
433
+ addData (std::string (cdata, 2 ) + std::string ((char *) &uuid.getNative ()->uuid .uuid32 , 4 ) + data);
498
434
break ;
499
435
}
500
436
501
437
case 128 : {
502
438
// [Len] [0x21] [UUID128] data
503
439
cdata[0 ] = data.length () + 17 ;
504
440
cdata[1 ] = ESP_BLE_AD_TYPE_128SERVICE_DATA; // 0x21
505
- addData (std::string (cdata, 2 ) + std::string ((char *) uuid.getNative ()->uuid .uuid128 ,16 ) + data);
441
+ addData (std::string (cdata, 2 ) + std::string ((char *) & uuid.getNative ()->uuid .uuid128 , 16 ) + data);
506
442
break ;
507
443
}
508
444
@@ -521,12 +457,11 @@ void BLEAdvertisementData::setShortName(std::string name) {
521
457
char cdata[2 ];
522
458
cdata[0 ] = name.length () + 1 ;
523
459
cdata[1 ] = ESP_BLE_AD_TYPE_NAME_SHORT; // 0x08
524
- addData (std::string (cdata, 2 ) + name);
460
+ addData (std::string (cdata, 2 ) + name);
525
461
ESP_LOGD (" BLEAdvertisementData" , " << setShortName" );
526
462
} // setShortName
527
463
528
464
529
-
530
465
/* *
531
466
* @brief Retrieve the payload that is to be advertised.
532
467
* @return The payload that is to be advertised.
@@ -535,11 +470,11 @@ std::string BLEAdvertisementData::getPayload() {
535
470
return m_payload;
536
471
} // getPayload
537
472
538
- void BLEAdvertising::gapEventHandler (
473
+ void BLEAdvertising::handleGAPEvent (
539
474
esp_gap_ble_cb_event_t event,
540
475
esp_ble_gap_cb_param_t * param) {
541
476
542
- ESP_LOGD (LOG_TAG, " gapEventHandler [event no: %d]" , (int )event);
477
+ ESP_LOGD (LOG_TAG, " handleGAPEvent [event no: %d]" , (int )event);
543
478
544
479
switch (event) {
545
480
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: {
@@ -565,4 +500,4 @@ void BLEAdvertising::gapEventHandler(
565
500
}
566
501
567
502
568
- #endif /* CONFIG_BT_ENABLED */
503
+ #endif /* CONFIG_BT_ENABLED */
0 commit comments