correct version of BLE libraries

last working version of this library with the ESP32 env revision pointed in the wiki
This commit is contained in:
Florian committed 2018-07-04 22:39:33 +02:00
1 parent 9dd7b97b11
commit b602e406ef
23 files changed
+72 -461

No files matched your search

@@ -234,7 +234,6 @@ 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.
@@ -507,13 +506,7 @@ 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,10 +39,9 @@ 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();
@@ -70,7 +69,6 @@ private:
void setServiceUUID(const char* serviceUUID);
void setServiceUUID(BLEUUID serviceUUID);
void setTXPower(int8_t txPower);
void setPayload(uint8_t* payload);
bool m_haveAppearance;
@@ -94,7 +92,6 @@ private:
int8_t m_txPower;
std::string m_serviceData;
BLEUUID m_serviceDataUUID;
uint8_t* m_payload;
};
/**
@@ -91,16 +91,6 @@ 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.
+3 -4
View File
@@ -29,12 +29,13 @@ 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
@@ -51,8 +52,6 @@ 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,43 +683,6 @@ 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,11 +75,6 @@ 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();
+3 -7
View File
@@ -77,7 +77,6 @@ void BLEClient::clearServices() {
delete myPair.second;
}
m_servicesMap.clear();
m_haveServices = false;
ESP_LOGD(LOG_TAG, "<< clearServices");
} // clearServices
@@ -111,7 +110,6 @@ 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) {
@@ -167,8 +165,6 @@ void BLEClient::gattClientEventHandler(
m_pClientCallbacks->onDisconnect(this);
}
m_isConnected = false;
m_semaphoreRssiCmplEvt.give();
m_semaphoreSearchCmplEvt.give(1);
break;
} // ESP_GATTC_DISCONNECT_EVT
@@ -216,7 +212,7 @@ void BLEClient::gattClientEventHandler(
// - uint16_t conn_id
//
case ESP_GATTC_SEARCH_CMPL_EVT: {
m_semaphoreSearchCmplEvt.give(0);
m_semaphoreSearchCmplEvt.give();
break;
} // ESP_GATTC_SEARCH_CMPL_EVT
@@ -369,8 +365,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;
}
// If sucessfull, remember that we now have services.
m_haveServices = (m_semaphoreSearchCmplEvt.wait("getServices") == 0);
m_semaphoreSearchCmplEvt.wait("getServices");
m_haveServices = true; // Remember that we now have services.
ESP_LOGD(LOG_TAG, "<< getServices");
return &m_servicesMap;
} // getServices
+1 -1
View File
@@ -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.descr_uuid)) &&
m_bleUUID.equals(BLEUUID(param->add_char_descr.char_uuid)) &&
m_pCharacteristic->getService()->getHandle() == param->add_char_descr.service_handle &&
m_pCharacteristic == m_pCharacteristic->getService()->getLastCreatedCharacteristic()) {
setHandle(param->add_char_descr.attr_handle);
+11 -49
View File
@@ -30,7 +30,6 @@
#include "GeneralUtils.h"
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#include "esp32-hal-bt.h"
#endif
static const char* LOG_TAG = "BLEDevice";
@@ -100,11 +99,9 @@ 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
@@ -151,11 +148,9 @@ 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
@@ -195,20 +190,16 @@ 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?
@@ -217,14 +208,12 @@ 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;
/*
*
@@ -232,27 +221,19 @@ 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;
@@ -332,21 +313,14 @@ 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 = ESP_OK;
#ifdef ARDUINO_ARCH_ESP32
if (!btStart()) {
errRc = ESP_FAIL;
return;
}
#else
errRc = ::nvs_flash_init();
esp_err_t 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;
@@ -366,24 +340,18 @@ uint16_t BLEDevice::m_localMTU = 23;
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;
}
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_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);
@@ -414,14 +382,12 @@ 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
@@ -541,8 +507,4 @@ esp_err_t BLEDevice::setMTU(uint16_t mtu) {
uint16_t BLEDevice::getMTU() {
return m_localMTU;
}
bool BLEDevice::getInitialized() {
return initialized;
}
#endif // CONFIG_BT_ENABLED
-1
View File
@@ -42,7 +42,6 @@ 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;
+6 -22
View File
@@ -74,9 +74,6 @@ 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
@@ -189,14 +186,12 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
/**
* @brief Start scanning.
* @param [in] duration The duration in seconds for which to scan.
* @param [in] scanCompleteCB A function to be called when scanning has completed.
* @return True if scan started or false if there was an error.
* @return N/A.
*/
bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
BLEScanResults BLEScan::start(uint32_t duration) {
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();
@@ -205,7 +200,7 @@ bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
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 false;
return m_scanResults;
}
errRc = ::esp_ble_gap_start_scanning(duration);
@@ -213,25 +208,14 @@ bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
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 false;
return m_scanResults;
}
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
-2
View File
@@ -53,7 +53,6 @@ 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();
@@ -72,7 +71,6 @@ private:
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
BLEScanResults m_scanResults;
bool m_wantDuplicates;
void (*m_scanCompleteCB)(BLEScanResults scanResults);
}; // BLEScan
#endif /* CONFIG_BT_ENABLED */
+5 -14
View File
@@ -18,6 +18,7 @@
#include "BLEUtils.h"
#include <string.h>
#include <string>
#include <gatt_api.h>
#include <unordered_set>
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
@@ -69,23 +70,21 @@ 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, uint8_t inst_id) {
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles) {
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_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
ESP_LOGE(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.
@@ -323,14 +322,6 @@ 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.
+2 -7
View File
@@ -40,14 +40,10 @@ 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;
};
@@ -58,11 +54,10 @@ class BLEServer {
public:
uint32_t getConnectedCount();
BLEService* createService(const char* uuid);
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0);
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15);
BLEAdvertising* getAdvertising();
void setCallbacks(BLEServerCallbacks* pCallbacks);
void startAdvertising();
void removeService(BLEService *service);
private:
+3 -82
View File
@@ -72,8 +72,7 @@ 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.is_primary = true;
srvc_id.id.inst_id = m_id;
srvc_id.id.inst_id = 0;
srvc_id.id.uuid = *m_uuid.getNative();
esp_err_t errRc = ::esp_ble_gatts_create_service(
getServer()->getGattsIf(),
@@ -91,28 +90,6 @@ 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.
@@ -175,34 +152,6 @@ 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.
@@ -243,7 +192,7 @@ void BLEService::addCharacteristic(BLECharacteristic* pCharacteristic) {
// Check that we don't add the same characteristic twice.
if (m_characteristicMap.getByUUID(pCharacteristic->getUUID()) != nullptr) {
ESP_LOGW(LOG_TAG, "<< Adding a new characteristic with the same UUID as a previous one");
ESP_LOGE(LOG_TAG, "<< Attempt to add a characteristic but we already have one with this UUID");
//return;
}
@@ -328,19 +277,6 @@ 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.
@@ -355,28 +291,13 @@ void BLEService::handleGATTServerEvent(
// * - bool is_primary
//
case ESP_GATTS_CREATE_EVT: {
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_id == param->create.service_id.id.inst_id) {
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid))) {
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
-5
View File
@@ -57,16 +57,13 @@ 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);
@@ -84,9 +81,7 @@ 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;
+4 -41
View File
@@ -27,8 +27,8 @@ BLEService* BLEServiceMap::getByUUID(const char* uuid) {
*/
BLEService* BLEServiceMap::getByUUID(BLEUUID uuid) {
for (auto &myPair : m_uuidMap) {
if (myPair.first->getUUID().equals(uuid)) {
return myPair.first;
if (myPair.second->getUUID().equals(uuid)) {
return myPair.second;
}
}
//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<BLEService *, std::string>(service, uuid.toString()));
m_uuidMap.insert(std::pair<std::string, BLEService *>(uuid.toString(), service));
} // setByUUID
@@ -89,44 +89,7 @@ void BLEServiceMap::handleGATTServerEvent(
esp_ble_gatts_cb_param_t *param) {
// Invoke the handler for every Service we have.
for (auto &myPair : m_uuidMap) {
myPair.first->handleGATTServerEvent(event, gatts_if, param);
myPair.second->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 */
+1 -1
View File
@@ -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.descr_uuid).toString().c_str());
BLEUUID(evtParam->add_char_descr.char_uuid).toString().c_str());
break;
} // ESP_GATTS_ADD_CHAR_DESCR_EVT