update BLE ESP32 lib to last version
note this version of library is compatible with this rev: https://github.com/espressif/arduino-esp32/tree/89859f7f4c07ce3e5d487a4b689dbda78f5d5c3a as stated in the wiki
This commit is contained in:
1 parent
0f118c2020
commit
751c479e59
23 files changed
+461
-72
No files matched your search
@@ -234,6 +234,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
|
||||
uint8_t ad_type;
|
||||
uint8_t sizeConsumed = 0;
|
||||
bool finished = false;
|
||||
setPayload(payload);
|
||||
|
||||
while(!finished) {
|
||||
length = *payload; // Retrieve the length of the record.
|
||||
@@ -506,7 +507,13 @@ std::string BLEAdvertisedDevice::toString() {
|
||||
return ss.str();
|
||||
} // toString
|
||||
|
||||
uint8_t* BLEAdvertisedDevice::getPayload() {
|
||||
return m_payload;
|
||||
}
|
||||
|
||||
void BLEAdvertisedDevice::setPayload(uint8_t* payload) {
|
||||
m_payload = payload;
|
||||
}
|
||||
|
||||
|
||||
#endif /* CONFIG_BT_ENABLED */
|
||||
|
||||
@@ -39,9 +39,10 @@ public:
|
||||
BLEUUID getServiceDataUUID();
|
||||
BLEUUID getServiceUUID();
|
||||
int8_t getTXPower();
|
||||
uint8_t* getPayload();
|
||||
|
||||
|
||||
bool isAdvertisingService(BLEUUID uuid);
|
||||
bool isAdvertisingService(BLEUUID uuid);
|
||||
bool haveAppearance();
|
||||
bool haveManufacturerData();
|
||||
bool haveName();
|
||||
@@ -69,6 +70,7 @@ private:
|
||||
void setServiceUUID(const char* serviceUUID);
|
||||
void setServiceUUID(BLEUUID serviceUUID);
|
||||
void setTXPower(int8_t txPower);
|
||||
void setPayload(uint8_t* payload);
|
||||
|
||||
|
||||
bool m_haveAppearance;
|
||||
@@ -92,6 +94,7 @@ private:
|
||||
int8_t m_txPower;
|
||||
std::string m_serviceData;
|
||||
BLEUUID m_serviceDataUUID;
|
||||
uint8_t* m_payload;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -91,6 +91,16 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
|
||||
m_advData.appearance = 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
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the filtering for the scan filter.
|
||||
|
||||
@@ -29,13 +29,12 @@ public:
|
||||
void setPartialServices(BLEUUID uuid);
|
||||
void setServiceData(BLEUUID uuid, std::string data);
|
||||
void setShortName(std::string name);
|
||||
void addData(std::string data); // Add data to the payload.
|
||||
std::string getPayload(); // Retrieve the current advert payload.
|
||||
|
||||
private:
|
||||
friend class BLEAdvertising;
|
||||
std::string m_payload; // The payload of the advertisement.
|
||||
|
||||
void addData(std::string data); // Add data to the payload.
|
||||
std::string getPayload(); // Retrieve the current advert payload.
|
||||
}; // BLEAdvertisementData
|
||||
|
||||
|
||||
@@ -52,6 +51,8 @@ public:
|
||||
void start();
|
||||
void stop();
|
||||
void setAppearance(uint16_t appearance);
|
||||
void setMaxInterval(uint16_t maxinterval);
|
||||
void setMinInterval(uint16_t mininterval);
|
||||
void setAdvertisementData(BLEAdvertisementData& advertisementData);
|
||||
void setScanFilter(bool scanRequertWhitelistOnly, bool connectWhitelistOnly);
|
||||
void setScanResponseData(BLEAdvertisementData& advertisementData);
|
||||
|
||||
@@ -683,6 +683,43 @@ void BLECharacteristic::setValue(std::string value) {
|
||||
setValue((uint8_t*)(value.data()), value.length());
|
||||
} // setValue
|
||||
|
||||
void BLECharacteristic::setValue(uint16_t& data16) {
|
||||
uint8_t temp[2];
|
||||
temp[0]=data16;
|
||||
temp[1]=data16>>8;
|
||||
setValue(temp, 2);
|
||||
} // setValue
|
||||
|
||||
void BLECharacteristic::setValue(uint32_t& data32) {
|
||||
uint8_t temp[4];
|
||||
temp[0]=data32;
|
||||
temp[1]=data32>>8;
|
||||
temp[2]=data32>>16;
|
||||
temp[3]=data32>>24;
|
||||
setValue(temp, 4);
|
||||
} // setValue
|
||||
|
||||
void BLECharacteristic::setValue(int& data32) {
|
||||
uint8_t temp[4];
|
||||
temp[0]=data32;
|
||||
temp[1]=data32>>8;
|
||||
temp[2]=data32>>16;
|
||||
temp[3]=data32>>24;
|
||||
setValue(temp, 4);
|
||||
} // setValue
|
||||
|
||||
void BLECharacteristic::setValue(float& data32) {
|
||||
uint8_t temp[4];
|
||||
*((float *)temp) = data32;
|
||||
setValue(temp, 4);
|
||||
} // setValue
|
||||
|
||||
void BLECharacteristic::setValue(double& data64) {
|
||||
uint8_t temp[8];
|
||||
*((double *)temp) = data64;
|
||||
setValue(temp, 8);
|
||||
} // setValue
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the Write No Response property value.
|
||||
|
||||
@@ -75,6 +75,11 @@ public:
|
||||
void setReadProperty(bool value);
|
||||
void setValue(uint8_t* data, size_t size);
|
||||
void setValue(std::string value);
|
||||
void setValue(uint16_t& data16);
|
||||
void setValue(uint32_t& data32);
|
||||
void setValue(int& data32);
|
||||
void setValue(float& data32);
|
||||
void setValue(double& data64);
|
||||
void setWriteProperty(bool value);
|
||||
void setWriteNoResponseProperty(bool value);
|
||||
std::string toString();
|
||||
|
||||
@@ -77,6 +77,7 @@ void BLEClient::clearServices() {
|
||||
delete myPair.second;
|
||||
}
|
||||
m_servicesMap.clear();
|
||||
m_haveServices = false;
|
||||
ESP_LOGD(LOG_TAG, "<< clearServices");
|
||||
} // clearServices
|
||||
|
||||
@@ -110,6 +111,7 @@ bool BLEClient::connect(BLEAddress address) {
|
||||
errRc = ::esp_ble_gattc_open(
|
||||
getGattcIf(),
|
||||
*getPeerAddress().getNative(), // address
|
||||
BLE_ADDR_TYPE_PUBLIC, // Note: This was added on 2018-04-03 when the latest ESP-IDF was detected to have changed the signature.
|
||||
1 // direct connection
|
||||
);
|
||||
if (errRc != ESP_OK) {
|
||||
@@ -165,6 +167,8 @@ void BLEClient::gattClientEventHandler(
|
||||
m_pClientCallbacks->onDisconnect(this);
|
||||
}
|
||||
m_isConnected = false;
|
||||
m_semaphoreRssiCmplEvt.give();
|
||||
m_semaphoreSearchCmplEvt.give(1);
|
||||
break;
|
||||
} // ESP_GATTC_DISCONNECT_EVT
|
||||
|
||||
@@ -212,7 +216,7 @@ void BLEClient::gattClientEventHandler(
|
||||
// - uint16_t conn_id
|
||||
//
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT: {
|
||||
m_semaphoreSearchCmplEvt.give();
|
||||
m_semaphoreSearchCmplEvt.give(0);
|
||||
break;
|
||||
} // ESP_GATTC_SEARCH_CMPL_EVT
|
||||
|
||||
@@ -365,8 +369,8 @@ std::map<std::string, BLERemoteService*>* BLEClient::getServices() {
|
||||
ESP_LOGE(LOG_TAG, "esp_ble_gattc_search_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return &m_servicesMap;
|
||||
}
|
||||
m_semaphoreSearchCmplEvt.wait("getServices");
|
||||
m_haveServices = true; // Remember that we now have services.
|
||||
// If sucessfull, remember that we now have services.
|
||||
m_haveServices = (m_semaphoreSearchCmplEvt.wait("getServices") == 0);
|
||||
ESP_LOGD(LOG_TAG, "<< getServices");
|
||||
return &m_servicesMap;
|
||||
} // getServices
|
||||
|
||||
@@ -155,7 +155,7 @@ void BLEDescriptor::handleGATTServerEvent(
|
||||
(uint32_t)m_pCharacteristic->getService()->getLastCreatedCharacteristic());
|
||||
*/
|
||||
if (m_pCharacteristic != nullptr &&
|
||||
m_bleUUID.equals(BLEUUID(param->add_char_descr.char_uuid)) &&
|
||||
m_bleUUID.equals(BLEUUID(param->add_char_descr.descr_uuid)) &&
|
||||
m_pCharacteristic->getService()->getHandle() == param->add_char_descr.service_handle &&
|
||||
m_pCharacteristic == m_pCharacteristic->getService()->getLastCreatedCharacteristic()) {
|
||||
setHandle(param->add_char_descr.attr_handle);
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "GeneralUtils.h"
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#include "esp32-hal-log.h"
|
||||
#include "esp32-hal-bt.h"
|
||||
#endif
|
||||
|
||||
static const char* LOG_TAG = "BLEDevice";
|
||||
@@ -99,9 +100,11 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
switch(event) {
|
||||
case ESP_GATTS_CONNECT_EVT: {
|
||||
BLEDevice::m_localMTU = 23;
|
||||
#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
|
||||
|
||||
@@ -148,9 +151,11 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
ESP_LOGE(LOG_TAG, "esp_ble_gattc_send_mtu_req: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
}
|
||||
}
|
||||
#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_GATTC_CONNECT_EVT
|
||||
|
||||
@@ -190,16 +195,20 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
break;
|
||||
case ESP_GAP_BLE_NC_REQ_EVT:
|
||||
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_addr, true, BLEDevice::m_securityCallbacks->onPassKeyRequest());
|
||||
}
|
||||
#endif // CONFIG_BLE_SMP_ENABLE
|
||||
break;
|
||||
/*
|
||||
* TODO should we add white/black list comparison?
|
||||
@@ -208,12 +217,14 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
/* 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_addr, true);
|
||||
}
|
||||
#endif // CONFIG_BLE_SMP_ENABLE
|
||||
break;
|
||||
/*
|
||||
*
|
||||
@@ -221,19 +232,27 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
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.
|
||||
///show the passkey number to the user to input it in the peer deivce.
|
||||
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
|
||||
if(BLEDevice::m_securityCallbacks!=nullptr){
|
||||
ESP_LOGI(LOG_TAG, "passKey = %d", param->ble_security.key_notif.passkey);
|
||||
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_LOGI(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;
|
||||
@@ -313,14 +332,21 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
if(!initialized){
|
||||
initialized = true; // Set the initialization flag to ensure we are only initialized once.
|
||||
|
||||
esp_err_t errRc = ::nvs_flash_init();
|
||||
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;
|
||||
}
|
||||
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
errRc = esp_bt_controller_init(&bt_cfg);
|
||||
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;
|
||||
@@ -340,18 +366,24 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
errRc = esp_bluedroid_init();
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
errRc = esp_bluedroid_enable();
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: 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);
|
||||
@@ -382,12 +414,14 @@ uint16_t BLEDevice::m_localMTU = 23;
|
||||
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
|
||||
@@ -507,4 +541,8 @@ esp_err_t BLEDevice::setMTU(uint16_t mtu) {
|
||||
uint16_t BLEDevice::getMTU() {
|
||||
return m_localMTU;
|
||||
}
|
||||
|
||||
bool BLEDevice::getInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
#endif // CONFIG_BT_ENABLED
|
||||
@@ -42,6 +42,7 @@ public:
|
||||
static void setSecurityCallbacks(BLESecurityCallbacks* pCallbacks);
|
||||
static esp_err_t setMTU(uint16_t mtu);
|
||||
static uint16_t getMTU();
|
||||
static bool getInitialized(); // Returns the state of the device, is it initialized or not?
|
||||
|
||||
private:
|
||||
static BLEServer *m_pServer;
|
||||
|
||||
@@ -74,6 +74,9 @@ void BLEScan::handleGAPEvent(
|
||||
// asked to stop.
|
||||
case ESP_GAP_SEARCH_INQ_CMPL_EVT: {
|
||||
m_stopped = true;
|
||||
if (m_scanCompleteCB != nullptr) {
|
||||
m_scanCompleteCB(m_scanResults);
|
||||
}
|
||||
m_semaphoreScanEnd.give();
|
||||
break;
|
||||
} // ESP_GAP_SEARCH_INQ_CMPL_EVT
|
||||
@@ -186,12 +189,14 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
|
||||
/**
|
||||
* @brief Start scanning.
|
||||
* @param [in] duration The duration in seconds for which to scan.
|
||||
* @return N/A.
|
||||
* @param [in] scanCompleteCB A function to be called when scanning has completed.
|
||||
* @return True if scan started or false if there was an error.
|
||||
*/
|
||||
BLEScanResults BLEScan::start(uint32_t duration) {
|
||||
bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
|
||||
ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration);
|
||||
|
||||
m_semaphoreScanEnd.take(std::string("start"));
|
||||
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
|
||||
|
||||
m_scanResults.m_vectorAdvertisedDevices.clear();
|
||||
|
||||
@@ -200,7 +205,7 @@ BLEScanResults BLEScan::start(uint32_t duration) {
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
m_semaphoreScanEnd.give();
|
||||
return m_scanResults;
|
||||
return false;
|
||||
}
|
||||
|
||||
errRc = ::esp_ble_gap_start_scanning(duration);
|
||||
@@ -208,14 +213,25 @@ BLEScanResults BLEScan::start(uint32_t duration) {
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
m_semaphoreScanEnd.give();
|
||||
return m_scanResults;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_stopped = false;
|
||||
|
||||
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
|
||||
|
||||
ESP_LOGD(LOG_TAG, "<< start()");
|
||||
return true;
|
||||
} // start
|
||||
|
||||
|
||||
/**
|
||||
* @brief Start scanning and block until scanning has been completed.
|
||||
* @param [in] duration The duration in seconds for which to scan.
|
||||
* @return The BLEScanResults.
|
||||
*/
|
||||
BLEScanResults BLEScan::start(uint32_t duration) {
|
||||
if(start(duration, nullptr)) {
|
||||
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
|
||||
}
|
||||
return m_scanResults;
|
||||
} // start
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ public:
|
||||
bool wantDuplicates = false);
|
||||
void setInterval(uint16_t intervalMSecs);
|
||||
void setWindow(uint16_t windowMSecs);
|
||||
bool start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults));
|
||||
BLEScanResults start(uint32_t duration);
|
||||
void stop();
|
||||
|
||||
@@ -71,6 +72,7 @@ private:
|
||||
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
|
||||
BLEScanResults m_scanResults;
|
||||
bool m_wantDuplicates;
|
||||
void (*m_scanCompleteCB)(BLEScanResults scanResults);
|
||||
}; // BLEScan
|
||||
|
||||
#endif /* CONFIG_BT_ENABLED */
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include "BLEUtils.h"
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <gatt_api.h>
|
||||
#include <unordered_set>
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#include "esp32-hal-log.h"
|
||||
@@ -70,21 +69,23 @@ BLEService* BLEServer::createService(const char* uuid) {
|
||||
* of a new service. Every service must have a unique UUID.
|
||||
* @param [in] uuid The UUID of the new service.
|
||||
* @param [in] numHandles The maximum number of handles associated with this service.
|
||||
* @param [in] inst_id With multiple services with the same UUID we need to provide inst_id value different for each service.
|
||||
* @return A reference to the new service object.
|
||||
*/
|
||||
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles) {
|
||||
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles, uint8_t inst_id) {
|
||||
ESP_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
|
||||
m_semaphoreCreateEvt.take("createService");
|
||||
|
||||
// Check that a service with the supplied UUID does not already exist.
|
||||
if (m_serviceMap.getByUUID(uuid) != nullptr) {
|
||||
ESP_LOGE(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
|
||||
ESP_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
|
||||
uuid.toString().c_str());
|
||||
m_semaphoreCreateEvt.give();
|
||||
return nullptr;
|
||||
//m_semaphoreCreateEvt.give();
|
||||
//return nullptr;
|
||||
}
|
||||
|
||||
BLEService* pService = new BLEService(uuid, numHandles);
|
||||
pService->m_id = inst_id;
|
||||
m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server.
|
||||
pService->executeCreate(this); // Perform the API calls to actually create the service.
|
||||
|
||||
@@ -322,6 +323,14 @@ void BLEServer::setCallbacks(BLEServerCallbacks* pCallbacks) {
|
||||
m_pServerCallbacks = pCallbacks;
|
||||
} // setCallbacks
|
||||
|
||||
/*
|
||||
* Remove service
|
||||
*/
|
||||
void BLEServer::removeService(BLEService *service) {
|
||||
service->stop();
|
||||
service->executeDelete();
|
||||
m_serviceMap.removeService(service);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Start advertising.
|
||||
|
||||
@@ -40,10 +40,14 @@ public:
|
||||
void setByUUID(const char* uuid, BLEService* service);
|
||||
void setByUUID(BLEUUID uuid, BLEService* service);
|
||||
std::string toString();
|
||||
BLEService* getFirst();
|
||||
BLEService* getNext();
|
||||
void removeService(BLEService *service);
|
||||
|
||||
private:
|
||||
std::map<std::string, BLEService*> m_uuidMap;
|
||||
std::map<uint16_t, BLEService*> m_handleMap;
|
||||
std::map<BLEService*, std::string> m_uuidMap;
|
||||
std::map<BLEService*, std::string>::iterator m_iterator;
|
||||
};
|
||||
|
||||
|
||||
@@ -54,10 +58,11 @@ class BLEServer {
|
||||
public:
|
||||
uint32_t getConnectedCount();
|
||||
BLEService* createService(const char* uuid);
|
||||
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15);
|
||||
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0);
|
||||
BLEAdvertising* getAdvertising();
|
||||
void setCallbacks(BLEServerCallbacks* pCallbacks);
|
||||
void startAdvertising();
|
||||
void removeService(BLEService *service);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
@@ -72,7 +72,8 @@ void BLEService::executeCreate(BLEServer *pServer) {
|
||||
m_semaphoreCreateEvt.take("executeCreate"); // Take the mutex and release at event ESP_GATTS_CREATE_EVT
|
||||
|
||||
esp_gatt_srvc_id_t srvc_id;
|
||||
srvc_id.id.inst_id = 0;
|
||||
srvc_id.is_primary = true;
|
||||
srvc_id.id.inst_id = m_id;
|
||||
srvc_id.id.uuid = *m_uuid.getNative();
|
||||
esp_err_t errRc = ::esp_ble_gatts_create_service(
|
||||
getServer()->getGattsIf(),
|
||||
@@ -90,6 +91,28 @@ void BLEService::executeCreate(BLEServer *pServer) {
|
||||
} // executeCreate
|
||||
|
||||
|
||||
/**
|
||||
* @brief Delete the service.
|
||||
* Delete the service.
|
||||
* @return N/A.
|
||||
*/
|
||||
|
||||
void BLEService::executeDelete() {
|
||||
ESP_LOGD(LOG_TAG, ">> executeDelete()");
|
||||
m_semaphoreDeleteEvt.take("executeDelete"); // Take the mutex and release at event ESP_GATTS_DELETE_EVT
|
||||
|
||||
esp_err_t errRc = ::esp_ble_gatts_delete_service( getHandle() );
|
||||
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "esp_ble_gatts_delete_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return;
|
||||
}
|
||||
|
||||
m_semaphoreDeleteEvt.wait("executeDelete");
|
||||
ESP_LOGD(LOG_TAG, "<< executeDelete");
|
||||
} // executeDelete
|
||||
|
||||
|
||||
/**
|
||||
* @brief Dump details of this BLE GATT service.
|
||||
* @return N/A.
|
||||
@@ -152,6 +175,34 @@ void BLEService::start() {
|
||||
} // start
|
||||
|
||||
|
||||
/**
|
||||
* @brief Stop the service.
|
||||
* @return Stop the service.
|
||||
*/
|
||||
void BLEService::stop() {
|
||||
// We ask the BLE runtime to start the service and then create each of the characteristics.
|
||||
// We start the service through its local handle which was returned in the ESP_GATTS_CREATE_EVT event
|
||||
// obtained as a result of calling esp_ble_gatts_create_service().
|
||||
//
|
||||
ESP_LOGD(LOG_TAG, ">> stop(): Stopping service (esp_ble_gatts_stop_service): %s", toString().c_str());
|
||||
if (m_handle == NULL_HANDLE) {
|
||||
ESP_LOGE(LOG_TAG, "<< !!! We attempted to stop a service but don't know its handle!");
|
||||
return;
|
||||
}
|
||||
|
||||
m_semaphoreStopEvt.take("stop");
|
||||
esp_err_t errRc = ::esp_ble_gatts_stop_service(m_handle);
|
||||
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_stop_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return;
|
||||
}
|
||||
m_semaphoreStopEvt.wait("stop");
|
||||
|
||||
ESP_LOGD(LOG_TAG, "<< stop()");
|
||||
} // start
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the handle associated with this service.
|
||||
* @param [in] handle The handle associated with the service.
|
||||
@@ -192,7 +243,7 @@ void BLEService::addCharacteristic(BLECharacteristic* pCharacteristic) {
|
||||
|
||||
// Check that we don't add the same characteristic twice.
|
||||
if (m_characteristicMap.getByUUID(pCharacteristic->getUUID()) != nullptr) {
|
||||
ESP_LOGE(LOG_TAG, "<< Attempt to add a characteristic but we already have one with this UUID");
|
||||
ESP_LOGW(LOG_TAG, "<< Adding a new characteristic with the same UUID as a previous one");
|
||||
//return;
|
||||
}
|
||||
|
||||
@@ -277,6 +328,19 @@ void BLEService::handleGATTServerEvent(
|
||||
break;
|
||||
} // ESP_GATTS_START_EVT
|
||||
|
||||
// ESP_GATTS_STOP_EVT
|
||||
//
|
||||
// stop:
|
||||
// esp_gatt_status_t status
|
||||
// uint16_t service_handle
|
||||
//
|
||||
case ESP_GATTS_STOP_EVT: {
|
||||
if (param->stop.service_handle == getHandle()) {
|
||||
m_semaphoreStopEvt.give();
|
||||
}
|
||||
break;
|
||||
} // ESP_GATTS_STOP_EVT
|
||||
|
||||
|
||||
// ESP_GATTS_CREATE_EVT
|
||||
// Called when a new service is registered as having been created.
|
||||
@@ -291,13 +355,28 @@ void BLEService::handleGATTServerEvent(
|
||||
// * - bool is_primary
|
||||
//
|
||||
case ESP_GATTS_CREATE_EVT: {
|
||||
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid))) {
|
||||
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_id == param->create.service_id.id.inst_id) {
|
||||
setHandle(param->create.service_handle);
|
||||
m_semaphoreCreateEvt.give();
|
||||
}
|
||||
break;
|
||||
} // ESP_GATTS_CREATE_EVT
|
||||
|
||||
|
||||
// ESP_GATTS_DELETE_EVT
|
||||
// Called when a service is deleted.
|
||||
//
|
||||
// delete:
|
||||
// * esp_gatt_status_t status
|
||||
// * uint16_t service_handle
|
||||
//
|
||||
case ESP_GATTS_DELETE_EVT: {
|
||||
if (param->del.service_handle == getHandle()) {
|
||||
m_semaphoreDeleteEvt.give();
|
||||
}
|
||||
break;
|
||||
} // ESP_GATTS_DELETE_EVT
|
||||
|
||||
default: {
|
||||
break;
|
||||
} // Default
|
||||
|
||||
@@ -57,13 +57,16 @@ public:
|
||||
BLECharacteristic* createCharacteristic(BLEUUID uuid, uint32_t properties);
|
||||
void dump();
|
||||
void executeCreate(BLEServer* pServer);
|
||||
void executeDelete();
|
||||
BLECharacteristic* getCharacteristic(const char* uuid);
|
||||
BLECharacteristic* getCharacteristic(BLEUUID uuid);
|
||||
BLEUUID getUUID();
|
||||
BLEServer* getServer();
|
||||
void start();
|
||||
void stop();
|
||||
std::string toString();
|
||||
uint16_t getHandle();
|
||||
uint8_t m_id = 0;
|
||||
|
||||
private:
|
||||
BLEService(const char* uuid, uint32_t numHandles);
|
||||
@@ -81,7 +84,9 @@ private:
|
||||
BLEUUID m_uuid;
|
||||
|
||||
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
|
||||
FreeRTOS::Semaphore m_semaphoreDeleteEvt = FreeRTOS::Semaphore("DeleteEvt");
|
||||
FreeRTOS::Semaphore m_semaphoreStartEvt = FreeRTOS::Semaphore("StartEvt");
|
||||
FreeRTOS::Semaphore m_semaphoreStopEvt = FreeRTOS::Semaphore("StopEvt");
|
||||
|
||||
uint32_t m_numHandles;
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ BLEService* BLEServiceMap::getByUUID(const char* uuid) {
|
||||
*/
|
||||
BLEService* BLEServiceMap::getByUUID(BLEUUID uuid) {
|
||||
for (auto &myPair : m_uuidMap) {
|
||||
if (myPair.second->getUUID().equals(uuid)) {
|
||||
return myPair.second;
|
||||
if (myPair.first->getUUID().equals(uuid)) {
|
||||
return myPair.first;
|
||||
}
|
||||
}
|
||||
//return m_uuidMap.at(uuid.toString());
|
||||
@@ -54,7 +54,7 @@ BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
|
||||
*/
|
||||
void BLEServiceMap::setByUUID(BLEUUID uuid,
|
||||
BLEService *service) {
|
||||
m_uuidMap.insert(std::pair<std::string, BLEService *>(uuid.toString(), service));
|
||||
m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString()));
|
||||
} // setByUUID
|
||||
|
||||
|
||||
@@ -89,7 +89,44 @@ void BLEServiceMap::handleGATTServerEvent(
|
||||
esp_ble_gatts_cb_param_t *param) {
|
||||
// Invoke the handler for every Service we have.
|
||||
for (auto &myPair : m_uuidMap) {
|
||||
myPair.second->handleGATTServerEvent(event, gatts_if, param);
|
||||
myPair.first->handleGATTServerEvent(event, gatts_if, param);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the first service in the map.
|
||||
* @return The first service in the map.
|
||||
*/
|
||||
BLEService* BLEServiceMap::getFirst() {
|
||||
m_iterator = m_uuidMap.begin();
|
||||
if (m_iterator == m_uuidMap.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
BLEService* pRet = m_iterator->first;
|
||||
m_iterator++;
|
||||
return pRet;
|
||||
} // getFirst
|
||||
|
||||
/**
|
||||
* @brief Get the next service in the map.
|
||||
* @return The next service in the map.
|
||||
*/
|
||||
BLEService* BLEServiceMap::getNext() {
|
||||
if (m_iterator == m_uuidMap.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
BLEService* pRet = m_iterator->first;
|
||||
m_iterator++;
|
||||
return pRet;
|
||||
} // getNext
|
||||
|
||||
/**
|
||||
* @brief Removes service from maps.
|
||||
* @return N/A.
|
||||
*/
|
||||
void BLEServiceMap::removeService(BLEService *service){
|
||||
m_handleMap.erase(service->getHandle());
|
||||
m_uuidMap.erase(service);
|
||||
} // removeService
|
||||
|
||||
#endif /* CONFIG_BT_ENABLED */
|
||||
@@ -1654,7 +1654,7 @@ void BLEUtils::dumpGattServerEvent(
|
||||
evtParam->add_char_descr.attr_handle,
|
||||
evtParam->add_char_descr.service_handle,
|
||||
evtParam->add_char_descr.service_handle,
|
||||
BLEUUID(evtParam->add_char_descr.char_uuid).toString().c_str());
|
||||
BLEUUID(evtParam->add_char_descr.descr_uuid).toString().c_str());
|
||||
break;
|
||||
} // ESP_GATTS_ADD_CHAR_DESCR_EVT
|
||||
|
||||
|
||||
Reference in new issue
Block a user