8000 Features, bugfixes and changes by chegewara · Pull Request #729 · nkolban/esp32-snippets · GitHub
[go: up one dir, main page]

Skip to content

Features, bugfixes and changes #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Few more fixes, arduino compatibility tested
  • Loading branch information
chegewara committed Nov 19, 2018
commit 011ea72506b7287a3b3a0742d68e3bb7cc6d276c
179 changes: 57 additions & 122 deletions cpp_utils/BLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,22 @@
#include <esp_err.h>
#include "BLEUtils.h"
#include "GeneralUtils.h"
#include "string.h"
#include "mbedtls/md.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"


#define BLOCK_SIZE 64
#define HMAC MBEDTLS_MD_SHA256

#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
static bool is_advertising = false;

static const char* LOG_TAG = "BLEAdvertising";


/**
* @brief Construct a default advertising object.
*
*/
BLEAdvertising::BLEAdvertising() {
m_advData.set_scan_rsp = false;
m_advData.include_name = true;
m_advData.include_txpower = false;
m_advData.include_txpower = true;
m_advData.min_interval = 0x20;
m_advData.max_interval = 0x40;
m_advData.appearance = 0x00;
Expand All @@ -63,70 +56,12 @@ BLEAdvertising::BLEAdvertising() {
m_advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
m_advParams.channel_map = ADV_CHNL_ALL;
m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
m_advParams.peer_addr_type = BLE_ADDR_TYPE_PUBLIC;

m_customAdvData = false; // No custom advertising data
m_customScanResponseData = false; // No custom scan response data
} // BLEAdvertising

void BLEAdvertising::setPrivateAddress(esp_ble_addr_type_t type) {
esp_bd_addr_t addr;
m_advParams.own_addr_type = type;

if(type == BLE_ADDR_TYPE_RPA_PUBLIC) {
esp_ble_gap_config_local_privacy(true);
return;
}
else{
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_context entropy;
mbedtls_md_context_t ctx;

char pers[] = "aes generate key";
int ret;
unsigned char key[BLOCK_SIZE] = {0};
const char inp[] = "random static address";
unsigned char outp[BLOCK_SIZE/2];

mbedtls_entropy_init( &entropy );
mbedtls_ctr_drbg_init( &ctr_drbg );

if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(unsigned char *) pers, strlen( pers ) ) ) != 0 )
{
printf( " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret );
goto exit;
}

if( ( ret = mbedtls_ctr_drbg_random( &ctr_drbg, key, BLOCK_SIZE ) ) != 0 )
{
printf( " failed\n ! mbedtls_ctr_drbg_random returned -0x%04x\n", -ret );
goto exit;
}

mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(HMAC), true);
mbedtls_md_hmac_starts(&ctx, (const unsigned char *)key, BLOCK_SIZE);
mbedtls_md_hmac_update(&ctx, (const unsigned char *)inp, strlen(inp));
mbedtls_md_hmac_finish(&ctx, outp);
mbedtls_md_free(&ctx);
ESP_LOG_BUFFER_HEX("random key", key, BLOCK_SIZE);
ESP_LOG_BUFFER_HEX("HASH step 2", outp, BLOCK_SIZE/2);

memcpy(addr, outp, 6);
}
10000 if(type == BLE_ADDR_TYPE_RANDOM) {
addr[0] &= 0x3F; // <--- Format of non-resolvable private address 00xx xxxx
}
else{
addr[0] |= 0xC0; // <--- Format of static address 11xx xxxx
}

esp_ble_gap_set_rand_addr(addr);
ESP_LOG_BUFFER_HEX("random address", addr, 6);
exit:
return;
}


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

void BLEAdvertising::setMinInterval(uint16_t mininterval) {
m_advData.min_interval = mininterval;
m_advParams.adv_int_min = mininterval;
} // setMinInterval

void BLEAdvertising::setMaxInterval(uint16_t maxinterval) {
m_advData.max_interval = maxinterval;
m_advParams.adv_int_max = maxinterval;
} // setMaxInterval

void BLEAdvertising::setMinPreferred(uint16_t mininterval) {
m_advData.min_interval = mininterval;
} //

void BLEAdvertising::setMaxPreferred(uint16_t maxinterval) {
m_advData.max_interval = maxinterval;
} //

void BLEAdvertising::setScanResponse(bool set) {
m_scanResp = set;
}

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


// We have a vector of service UUIDs that we wish to advertise. In order to use the
// ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
// representations. If we have 1 or more services to advertise then we allocate enough
// storage to host them and then copy them in one at a time into the contiguous storage.
int numServices = m_serviceUUIDs.size();
if (numServices > 0) {
m_advData.service_uuid_len = 16*numServices;
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
m_advData.service_uuid_len = 16 * numServices;
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
uint8_t* p = m_advData.p_service_uuid;
for (int i=0; i<numServices; i++) {
for (int i = 0; i < numServices; i++) {
ESP_LOGD(LOG_TAG, "- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128();
memcpy(p, serviceUUID128.getNative()->uuid.uuid128, 16);
p+=16;
p += 16;
}
} else {
m_advData.service_uuid_len = 0;
Expand All @@ -262,45 +205,40 @@ void BLEAdvertising::start() {

esp_err_t errRc;

if(!is_advertising){

// m_semaphoreSetAdv.take("config_adv");
if (m_customAdvData == false) {
if (!m_customAdvData) {
// Set the configuration for advertising.
m_advData.set_scan_rsp = false;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
// m_semaphoreSetAdv.give();
return;
}
m_advData.set_scan_rsp = false;
m_advData.include_name = !m_scanResp;
m_advData.include_txpower = !m_scanResp;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
}

// m_semaphoreSetAdv.take("config_rsp");
if (m_customScanResponseData == false) {
m_advData.set_scan_rsp = true;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
// m_semaphoreSetAdv.give();
return;
}
if (!m_customScanResponseData && m_scanResp) {
m_advData.set_scan_rsp = true;
m_advData.include_name = m_scanResp;
m_advData.include_txpower = m_scanResp;
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
}

// If we had services to advertise then we previously allocated some storage for them.
// Here we release that storage.
if (m_advData.service_uuid_len > 0) {
delete[] m_advData.p_service_uuid;
m_advData.p_service_uuid = nullptr;
}
is_advertising = true;

// m_semaphoreSetAdv.take("start");
// Start advertising.
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_start_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
// m_semaphoreSetAdv.give();
return;
}
ESP_LOGD(LOG_TAG, "<< start");
Expand All @@ -314,7 +252,6 @@ void BLEAdvertising::start() {
*/
void BLEAdvertising::stop() {
ESP_LOGD(LOG_TAG, ">> stop");
// m_semaphoreSetAdv.take("stop");
esp_err_t errRc = ::esp_ble_gap_stop_advertising();
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gap_stop_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
Expand Down Expand Up @@ -346,8 +283,7 @@ void BLEAdvertisementData::setAppearance(uint16_t appearance) {
char cdata[2];
cdata[0] = 3;
cdata[1] = ESP_BLE_AD_TYPE_APPEARANCE; // 0x19
addData(std::string(cdata, 2) + std::string((char *)&appearance,2));
esp_ble_gap_config_local_icon(appearance);
addData(std::string(cdata, 2) + std::string((char*) &appearance, 2));
} // setAppearance


Expand All @@ -357,28 +293,28 @@ void BLEAdvertisementData::setAppearance(uint16_t appearance) {
*/
void BLEAdvertisementData::setCompleteServices(BLEUUID uuid) {
char cdata[2];
switch(uuid.bitSize()) {
switch (uuid.bitSize()) {
case 16: {
// [Len] [0x02] [LL] [HH]
cdata[0] = 3;
cdata[1] = ESP_BLE_AD_TYPE_16SRV_CMPL; // 0x03
addData(std::string(cdata, 2) + std::string((char *)&uuid.getNative()->uuid.uuid16,2));
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid16, 2));
break;
}

case 32: {
// [Len] [0x04] [LL] [LL] [HH] [HH]
cdata[0] = 5;
cdata[1] = ESP_BLE_AD_TYPE_32SRV_CMPL; // 0x05
addData(std::string(cdata, 2) + std::string((char *)&uuid.getNative()->uuid.uuid32,4));
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid32, 4));
break;
}

case 128: {
// [Len] [0x04] [0] [1] ... [15]
cdata[0] = 17;
cdata[1] = ESP_BLE_AD_TYPE_128SRV_CMPL; // 0x07
addData(std::string(cdata, 2) + std::string((char *)uuid.getNative()->uuid.uuid128,16)); 7802
addData(std::string(cdata, 2) + std::string((char*) uuid.getNative()->uuid.uuid128, 16));
break;
}

Expand Down Expand Up @@ -418,7 +354,7 @@ void BLEAdvertisementData::setManufacturerData(std::string data) {
char cdata[2];
cdata[0] = data.length() + 1;
cdata[1] = ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE; // 0xff
addData(std::string(cdata, 2) + data);
addData(std::string(cdata, 2) + data);
ESP_LOGD("BLEAdvertisementData", "<< setManufacturerData");
} // setManufacturerData

Expand All @@ -432,7 +368,7 @@ void BLEAdvertisementData::setName(std::string name) {
char cdata[2];
cdata[0] = name.length() + 1;
cdata[1] = ESP_BLE_AD_TYPE_NAME_CMPL; // 0x09
addData(std::string(cdata, 2) + name);
addData(std::string(cdata, 2) + name);
ESP_LOGD("BLEAdvertisementData", "<< setName");
} // setName

Expand All @@ -443,28 +379,28 @@ void BLEAdvertisementData::setName(std::string name) {
*/
void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
char cdata[2];
switch(uuid.bitSize()) {
switch (uuid.bitSize()) {
case 16: {
// [Len] [0x02] [LL] [HH]
cdata[0] = 3;
cdata[1] = ESP_BLE_AD_TYPE_16SRV_PART; // 0x02
addData(std::string(cdata, 2) + std::string((char *)&uuid.getNative()->uuid.uuid16,2));
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->uuid.uuid16, 2));
break;
}

case 32: {
// [Len] [0x04] [LL] [LL] [HH] [HH]
cdata[0] = 5;
cdata[1] = ESP_BLE_AD_TYPE_32SRV_PART; // 0x04
addData(std::string(cdata, 2) + std::string((char *)&uuid.getNative()->uuid.uuid32,4));
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->uuid.uuid32, 4));
break;
}

case 128: {
// [Len] [0x04] [0] [1] ... [15]
cdata[0] = 17;
cdata[1] = ESP_BLE_AD_TYPE_128SRV_PART; // 0x06
addData(std::string(cdata, 2) + std::string((char *)uuid.getNative()->uuid.uuid128,16));
addData(std::string(cdata, 2) + std::string((char *) &uuid.getNative()->uuid.uuid128, 16));
break;
}

Expand All @@ -481,28 +417,28 @@ void BLEAdvertisementData::setPartialServices(BLEUUID uuid) {
*/
void BLEAdvertisementData::setServiceData(BLEUUID uuid, std::string data) {
char cdata[2];
switch(uuid.bitSize()) {
switch (uuid.bitSize()) {
case 16: {
// [Len] [0x16] [UUID16] data
cdata[0] = data.length() + 3;
cdata[1] = ESP_BLE_AD_TYPE_SERVICE_DATA; // 0x16
addData(std::string(cdata, 2) + std::string((char *)&uuid.getNative()->uuid.uuid16,2) + data);
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid16, 2) + data);
break;
}

case 32: {
// [Len] [0x20] [UUID32] data
cdata[0] = data.length() + 5;
cdata[1] = ESP_BLE_AD_TYPE_32SERVICE_DATA; // 0x20
addData(std::string(cdata, 2) + std::string((char *)&uuid.getNative()->uuid.uuid32,4) + data);
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid32, 4) + data);
break;
}

case 128: {
// [Len] [0x21] [UUID128] data
cdata[0] = data.length() + 17;
cdata[1] = ESP_BLE_AD_TYPE_128SERVICE_DATA; // 0x21
addData(std::string(cdata, 2) + std::string((char *)uuid.getNative()->uuid.uuid128,16) + data);
addData(std::string(cdata, 2) + std::string((char*) &uuid.getNative()->uuid.uuid128, 16) + data);
break;
}

Expand All @@ -521,12 +457,11 @@ void BLEAdvertisementData::setShortName(std::string name) {
char cdata[2];
cdata[0] = name.length() + 1;
cdata[1] = ESP_BLE_AD_TYPE_NAME_SHORT; // 0x08
addData(std::string(cdata, 2) + name);
addData(std::string(cdata, 2) + name);
ESP_LOGD("BLEAdvertisementData", "<< setShortName");
} // setShortName



/**
* @brief Retrieve the payload that is to be advertised.
* @return The payload that is to be advertised.
Expand All @@ -535,11 +470,11 @@ std::string BLEAdvertisementData::getPayload() {
return m_payload;
} // getPayload

void BLEAdvertising::gapEventHandler(
void BLEAdvertising::handleGAPEvent(
esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t* param) {

ESP_LOGD(LOG_TAG, "gapEventHandler [event no: %d]", (int)event);
ESP_LOGD(LOG_TAG, "handleGAPEvent [event no: %d]", (int)event);

switch(event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: {
Expand All @@ -565,4 +500,4 @@ void BLEAdvertising::gapEventHandler(
}


#endif /* CONFIG_BT_ENABLED */
#endif /* CONFIG_BT_ENABLED */
Loading
0