Skip to content

Commit 011ea72

Browse files
committed
Few more fixes, arduino compatibility tested
1 parent ae045aa commit 011ea72

11 files changed

+137
-218
lines changed

cpp_utils/BLEAdvertising.cpp

Lines changed: 57 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,22 @@
2323
#include <esp_err.h>
2424
#include "BLEUtils.h"
2525
#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
3426

3527
#ifdef ARDUINO_ARCH_ESP32
3628
#include "esp32-hal-log.h"
3729
#endif
38-
static bool is_advertising = false;
30+
3931
static const char* LOG_TAG = "BLEAdvertising";
4032

33+
4134
/**
4235
* @brief Construct a default advertising object.
4336
*
4437
*/
4538
BLEAdvertising::BLEAdvertising() {
4639
m_advData.set_scan_rsp = false;
4740
m_advData.include_name = true;
48-
m_advData.include_txpower = false;
41+
m_advData.include_txpower = true;
4942
m_advData.min_interval = 0x20;
5043
m_advData.max_interval = 0x40;
5144
m_advData.appearance = 0x00;
@@ -63,70 +56,12 @@ BLEAdvertising::BLEAdvertising() {
6356
m_advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
6457
m_advParams.channel_map = ADV_CHNL_ALL;
6558
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
59+
m_advParams.peer_addr_type = BLE_ADDR_TYPE_PUBLIC;
6660

6761
m_customAdvData = false; // No custom advertising data
6862
m_customScanResponseData = false; // No custom scan response data
6963
} // BLEAdvertising
7064

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-
13065

13166
/**
13267
* @brief Add a service uuid to exposed list of services.
@@ -158,22 +93,31 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
15893
} // setAppearance
15994

16095
void BLEAdvertising::setMinInterval(uint16_t mininterval) {
161-
m_advData.min_interval = mininterval;
16296
m_advParams.adv_int_min = mininterval;
16397
} // setMinInterval
16498

16599
void BLEAdvertising::setMaxInterval(uint16_t maxinterval) {
166-
m_advData.max_interval = maxinterval;
167100
m_advParams.adv_int_max = maxinterval;
168101
} // setMaxInterval
169102

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+
}
170114

171115
/**
172116
* @brief Set the filtering for the scan filter.
173117
* @param [in] scanRequestWhitelistOnly If true, only allow scan requests from those on the white list.
174118
* @param [in] connectWhitelistOnly If true, only allow connections from those on the white list.
175119
*/
176-
void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
120+
void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly) {
177121
ESP_LOGD(LOG_TAG, ">> setScanFilter: scanRequestWhitelistOnly: %d, connectWhitelistOnly: %d", scanRequestWhitelistOnly, connectWhitelistOnly);
178122
if (!scanRequestWhitelistOnly && !connectWhitelistOnly) {
179123
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
@@ -239,21 +183,20 @@ void BLEAdvertising::setScanResponseData(BLEAdvertisementData& advertisementData
239183
void BLEAdvertising::start() {
240184
ESP_LOGD(LOG_TAG, ">> start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);
241185

242-
243186
// We have a vector of service UUIDs that we wish to advertise. In order to use the
244187
// ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
245188
// representations. If we have 1 or more services to advertise then we allocate enough
246189
// storage to host them and then copy them in one at a time into the contiguous storage.
247190
int numServices = m_serviceUUIDs.size();
248191
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];
251194
uint8_t* p = m_advData.p_service_uuid;
252-
for (int i=0; i<numServices; i++) {
195+
for (int i = 0; i < numServices; i++) {
253196
ESP_LOGD(LOG_TAG, "- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
254197
BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128();
255198
memcpy(p, serviceUUID128.getNative()->uuid.uuid128, 16);
256-
p+=16;
199+
p += 16;
257200
}
258201
} else {
259202
m_advData.service_uuid_len = 0;
@@ -262,45 +205,40 @@ void BLEAdvertising::start() {
262205

263206
esp_err_t errRc;
264207

265-
if(!is_advertising){
266-
267-
// m_semaphoreSetAdv.take("config_adv");
268-
if (m_customAdvData == false) {
208+
if (!m_customAdvData) {
269209
// 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;
277217
}
218+
}
278219

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;
288228
}
289229
}
230+
290231
// If we had services to advertise then we previously allocated some storage for them.
291232
// Here we release that storage.
292233
if (m_advData.service_uuid_len > 0) {
293234
delete[] m_advData.p_service_uuid;
294235
m_advData.p_service_uuid = nullptr;
295236
}
296-
is_advertising = true;
297237

298-
// m_semaphoreSetAdv.take("start");
299238
// Start advertising.
300239
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
301240
if (errRc != ESP_OK) {
302241
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_start_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
303-
// m_semaphoreSetAdv.give();
304242
return;
305243
}
306244
ESP_LOGD(LOG_TAG, "<< start");
@@ -314,7 +252,6 @@ void BLEAdvertising::start() {
314252
*/
315253
void BLEAdvertising::stop() {
316254
ESP_LOGD(LOG_TAG, ">> stop");
317-
// m_semaphoreSetAdv.take("stop");
318255
esp_err_t errRc = ::esp_ble_gap_stop_advertising();
319256
if (errRc != ESP_OK) {
320257
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) {
346283
char cdata[2];
347284
cdata[0] = 3;
348285
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));
351287
} // setAppearance
352288

353289

@@ -357,28 +293,28 @@ void BLEAdvertisementData::setAppearance(uint16_t appearance) {
357293
*/
358294
void BLEAdvertisementData::setCompleteServices(BLEUUID uuid) {
359295
char cdata[2];
360-
switch(uuid.bitSize()) {
296+
switch (uuid.bitSize()) {
361297
case 16: {
362298
// [Len] [0x02] [LL] [HH]
363299
cdata[0] = 3;
364300
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));
366302
break;
367303
}
368304

369305
case 32: {
370306
// [Len] [0x04] [LL] [LL] [HH] [HH]
371307
cdata[0] = 5;
372308
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));
374310
break;
375311
}
376312

377313
case 128: {
378314
// [Len] [0x04] [0] [1] ... [15]
379315
cdata[0] = 17;
380316
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));
382318
break;
383319
}
384320

@@ -418,7 +354,7 @@ void BLEAdvertisementData::setManufacturerData(std::string data) {
418354
char cdata[2];
419355
cdata[0] = data.length() + 1;
420356
cdata[1] = ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE; // 0xff
421-
addData(std::string(cdata, 2) + data);
357+
addData(std::string(cdata, 2) + data);
422358
ESP_LOGD("BLEAdvertisementData", "<< setManufacturerData");
423359
} // setManufacturerData
424360

@@ -432,7 +368,7 @@ void BLEAdvertisementData::setName(std::string name) {
432368
char cdata[2];
433369
cdata[0] = name.length() + 1;
434370
cdata[1] = ESP_BLE_AD_TYPE_NAME_CMPL; // 0x09
435-
addData(std::string(cdata, 2) + name);
371+
addData(std::string(cdata, 2) + name);
436372
ESP_LOGD("BLEAdvertisementData", "<< setName");
437373
} // setName
438374

@@ -443,28 +379,28 @@ void BLEAdvertisementData::setName(std::string name) {
443379
*/
444380
void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
445381
char cdata[2];
446-
switch(uuid.bitSize()) {
382+
switch (uuid.bitSize()) {
447383
case 16: {
448384
// [Len] [0x02] [LL] [HH]
449385
cdata[0] = 3;
450386
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));
452388
break;
453389
}
454390

455391
case 32: {
456392
// [Len] [0x04] [LL] [LL] [HH] [HH]
457393
cdata[0] = 5;
458394
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));
460396
break;
461397
}
462398

463399
case 128: {
464400
// [Len] [0x04] [0] [1] ... [15]
465401
cdata[0] = 17;
466402
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));
468404
break;
469405
}
470406

@@ -481,28 +417,28 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
481417
*/
482418
void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
483419
char cdata[2];
484-
switch(uuid.bitSize()) {
420+
switch (uuid.bitSize()) {
485421
case 16: {
486422
// [Len] [0x16] [UUID16] data
487423
cdata[0] = data.length() + 3;
488424
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);
490426
break;
491427
}
492428

493429
case 32: {
494430
// [Len] [0x20] [UUID32] data
495431
cdata[0] = data.length() + 5;
496432
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);
498434
break;
499435
}
500436

501437
case 128: {
502438
// [Len] [0x21] [UUID128] data
503439
cdata[0] = data.length() + 17;
504440
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);
506442
break;
507443
}
508444

@@ -521,12 +457,11 @@ void BLEAdvertisementData::setShortName(std::string name) {
521457
char cdata[2];
522458
cdata[0] = name.length() + 1;
523459
cdata[1] = ESP_BLE_AD_TYPE_NAME_SHORT; // 0x08
524-
addData(std::string(cdata, 2) + name);
460+
addData(std::string(cdata, 2) + name);
525461
ESP_LOGD("BLEAdvertisementData", "<< setShortName");
526462
} // setShortName
527463

528464

529-
530465
/**
531466
* @brief Retrieve the payload that is to be advertised.
532467
* @return The payload that is to be advertised.
@@ -535,11 +470,11 @@ std::string BLEAdvertisementData::getPayload() {
535470
return m_payload;
536471
} // getPayload
537472

538-
void BLEAdvertising::gapEventHandler(
473+
void BLEAdvertising::handleGAPEvent(
539474
esp_gap_ble_cb_event_t event,
540475
esp_ble_gap_cb_param_t* param) {
541476

542-
ESP_LOGD(LOG_TAG, "gapEventHandler [event no: %d]", (int)event);
477+
ESP_LOGD(LOG_TAG, "handleGAPEvent [event no: %d]", (int)event);
543478

544479
switch(event) {
545480
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: {
@@ -565,4 +500,4 @@ void BLEAdvertising::gapEventHandler(
565500
}
566501

567502

568-
#endif /* CONFIG_BT_ENABLED */
503+
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)