From b602e406efadc1f0768ae9144a1d35bec9e194a0 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Wed, 4 Jul 2018 22:39:33 +0200 Subject: [PATCH] correct version of BLE libraries last working version of this library with the ESP32 env revision pointed in the wiki --- lib/ESP32_BLE_Arduino/README.md | 2 +- .../examples/BLE_iBeacon/BLE_iBeacon.ino | 104 ------------------ .../examples/BLE_notify/BLE_notify.ino | 34 ++---- .../examples/BLE_uart/BLE_uart.ino | 52 ++++----- lib/ESP32_BLE_Arduino/library.properties | 2 +- .../src/BLEAdvertisedDevice.cpp | 7 -- .../src/BLEAdvertisedDevice.h | 5 +- lib/ESP32_BLE_Arduino/src/BLEAdvertising.cpp | 10 -- lib/ESP32_BLE_Arduino/src/BLEAdvertising.h | 7 +- .../src/BLECharacteristic.cpp | 37 ------- lib/ESP32_BLE_Arduino/src/BLECharacteristic.h | 5 - lib/ESP32_BLE_Arduino/src/BLEClient.cpp | 10 +- lib/ESP32_BLE_Arduino/src/BLEDescriptor.cpp | 2 +- lib/ESP32_BLE_Arduino/src/BLEDevice.cpp | 60 ++-------- lib/ESP32_BLE_Arduino/src/BLEDevice.h | 1 - lib/ESP32_BLE_Arduino/src/BLEScan.cpp | 28 +---- lib/ESP32_BLE_Arduino/src/BLEScan.h | 2 - lib/ESP32_BLE_Arduino/src/BLEServer.cpp | 19 +--- lib/ESP32_BLE_Arduino/src/BLEServer.h | 9 +- lib/ESP32_BLE_Arduino/src/BLEService.cpp | 85 +------------- lib/ESP32_BLE_Arduino/src/BLEService.h | 5 - lib/ESP32_BLE_Arduino/src/BLEServiceMap.cpp | 45 +------- lib/ESP32_BLE_Arduino/src/BLEUtils.cpp | 2 +- 23 files changed, 72 insertions(+), 461 deletions(-) delete mode 100644 lib/ESP32_BLE_Arduino/examples/BLE_iBeacon/BLE_iBeacon.ino diff --git a/lib/ESP32_BLE_Arduino/README.md b/lib/ESP32_BLE_Arduino/README.md index e80fbe0..d7bcf50 100644 --- a/lib/ESP32_BLE_Arduino/README.md +++ b/lib/ESP32_BLE_Arduino/README.md @@ -1,5 +1,5 @@ # ESP32 BLE for Arduino -The Arduino IDE provides an excellent library package manager where versions of libraries can be downloaded and installed. This Github project provides the repository for the ESP32 BLE support for Arduino. +The Arduino IDE provides an excellent library package managed where versions of libraries can be downloaded and installed. This Github project provides the repository for the ESP32 BLE support for Arduino. The actual source of the project which is being maintained can be found here: diff --git a/lib/ESP32_BLE_Arduino/examples/BLE_iBeacon/BLE_iBeacon.ino b/lib/ESP32_BLE_Arduino/examples/BLE_iBeacon/BLE_iBeacon.ino deleted file mode 100644 index 5f6ed00..0000000 --- a/lib/ESP32_BLE_Arduino/examples/BLE_iBeacon/BLE_iBeacon.ino +++ /dev/null @@ -1,104 +0,0 @@ -/* - Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp - Ported to Arduino ESP32 by pcbreflux -*/ - - -/* - Create a BLE server that will send periodic iBeacon frames. - The design of creating the BLE server is: - 1. Create a BLE Server - 2. Create advertising data - 3. Start advertising. - 4. wait - 5. Stop advertising. - 6. deep sleep - -*/ -#include "sys/time.h" - -#include "BLEDevice.h" -#include "BLEServer.h" -#include "BLEUtils.h" -#include "BLEBeacon.h" -#include "esp_sleep.h" - -#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up -RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory -RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory - -#ifdef __cplusplus -extern "C" { -#endif - -uint8_t temprature_sens_read(); -//uint8_t g_phyFuns; - -#ifdef __cplusplus -} -#endif - -// See the following for generating UUIDs: -// https://www.uuidgenerator.net/ -BLEAdvertising *pAdvertising; -struct timeval now; - -#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/) - -void setBeacon() { - - BLEBeacon oBeacon = BLEBeacon(); - oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!) - oBeacon.setProximityUUID(BLEUUID(BEACON_UUID)); - oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16); - oBeacon.setMinor(bootcount&0xFFFF); - BLEAdvertisementData oAdvertisementData = BLEAdvertisementData(); - BLEAdvertisementData oScanResponseData = BLEAdvertisementData(); - - oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04 - - std::string strServiceData = ""; - - strServiceData += (char)26; // Len - strServiceData += (char)0xFF; // Type - strServiceData += oBeacon.getData(); - oAdvertisementData.addData(strServiceData); - - pAdvertising->setAdvertisementData(oAdvertisementData); - pAdvertising->setScanResponseData(oScanResponseData); - -} - -void setup() { - - - Serial.begin(115200); - gettimeofday(&now, NULL); - - Serial.printf("start ESP32 %d\n",bootcount++); - - Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last); - - last = now.tv_sec; - - // Create the BLE Device - BLEDevice::init(""); - - // Create the BLE Server - BLEServer *pServer = BLEDevice::createServer(); - - pAdvertising = pServer->getAdvertising(); - - setBeacon(); - // Start advertising - pAdvertising->start(); - Serial.println("Advertizing started..."); - delay(100); - pAdvertising->stop(); - Serial.printf("enter deep sleep\n"); - esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION); - Serial.printf("in deep sleep\n"); -} - -void loop() { -} diff --git a/lib/ESP32_BLE_Arduino/examples/BLE_notify/BLE_notify.ino b/lib/ESP32_BLE_Arduino/examples/BLE_notify/BLE_notify.ino index 5e915be..8d329c2 100644 --- a/lib/ESP32_BLE_Arduino/examples/BLE_notify/BLE_notify.ino +++ b/lib/ESP32_BLE_Arduino/examples/BLE_notify/BLE_notify.ino @@ -23,10 +23,8 @@ #include #include -BLEServer* pServer = NULL; -BLECharacteristic* pCharacteristic = NULL; +BLECharacteristic *pCharacteristic; bool deviceConnected = false; -bool oldDeviceConnected = false; uint8_t value = 0; // See the following for generating UUIDs: @@ -55,7 +53,7 @@ void setup() { BLEDevice::init("MyESP32"); // Create the BLE Server - pServer = BLEDevice::createServer(); + BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service @@ -83,23 +81,13 @@ void setup() { } void loop() { - // notify changed value - if (deviceConnected) { - pCharacteristic->setValue(&value, 1); - pCharacteristic->notify(); - value++; - delay(10); // bluetooth stack will go into congestion, if too many packets are sent - } - // disconnecting - if (!deviceConnected && oldDeviceConnected) { - delay(500); // give the bluetooth stack the chance to get things ready - pServer->startAdvertising(); // restart advertising - Serial.println("start advertising"); - oldDeviceConnected = deviceConnected; - } - // connecting - if (deviceConnected && !oldDeviceConnected) { - // do stuff here on connecting - oldDeviceConnected = deviceConnected; - } + + if (deviceConnected) { + Serial.printf("*** NOTIFY: %d ***\n", value); + pCharacteristic->setValue(&value, 1); + pCharacteristic->notify(); + //pCharacteristic->indicate(); + value++; + } + delay(2000); } \ No newline at end of file diff --git a/lib/ESP32_BLE_Arduino/examples/BLE_uart/BLE_uart.ino b/lib/ESP32_BLE_Arduino/examples/BLE_uart/BLE_uart.ino index 35b570b..a348a66 100644 --- a/lib/ESP32_BLE_Arduino/examples/BLE_uart/BLE_uart.ino +++ b/lib/ESP32_BLE_Arduino/examples/BLE_uart/BLE_uart.ino @@ -24,10 +24,8 @@ #include #include -BLEServer *pServer = NULL; -BLECharacteristic * pTxCharacteristic; +BLECharacteristic *pCharacteristic; bool deviceConnected = false; -bool oldDeviceConnected = false; uint8_t txValue = 0; // See the following for generating UUIDs: @@ -72,26 +70,26 @@ void setup() { BLEDevice::init("UART Service"); // Create the BLE Server - pServer = BLEDevice::createServer(); + BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a BLE Characteristic - pTxCharacteristic = pService->createCharacteristic( - CHARACTERISTIC_UUID_TX, - BLECharacteristic::PROPERTY_NOTIFY - ); + pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID_TX, + BLECharacteristic::PROPERTY_NOTIFY + ); - pTxCharacteristic->addDescriptor(new BLE2902()); + pCharacteristic->addDescriptor(new BLE2902()); - BLECharacteristic * pRxCharacteristic = pService->createCharacteristic( - CHARACTERISTIC_UUID_RX, - BLECharacteristic::PROPERTY_WRITE - ); + BLECharacteristic *pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID_RX, + BLECharacteristic::PROPERTY_WRITE + ); - pRxCharacteristic->setCallbacks(new MyCallbacks()); + pCharacteristic->setCallbacks(new MyCallbacks()); // Start the service pService->start(); @@ -103,23 +101,11 @@ void setup() { void loop() { - if (deviceConnected) { - pTxCharacteristic->setValue(&txValue, 1); - pTxCharacteristic->notify(); - txValue++; - delay(10); // bluetooth stack will go into congestion, if too many packets are sent - } - - // disconnecting - if (!deviceConnected && oldDeviceConnected) { - delay(500); // give the bluetooth stack the chance to get things ready - pServer->startAdvertising(); // restart advertising - Serial.println("start advertising"); - oldDeviceConnected = deviceConnected; - } - // connecting - if (deviceConnected && !oldDeviceConnected) { - // do stuff here on connecting - oldDeviceConnected = deviceConnected; - } + if (deviceConnected) { + Serial.printf("*** Sent Value: %d ***\n", txValue); + pCharacteristic->setValue(&txValue, 1); + pCharacteristic->notify(); + txValue++; + } + delay(1000); } diff --git a/lib/ESP32_BLE_Arduino/library.properties b/lib/ESP32_BLE_Arduino/library.properties index 23e642e..fabbf2e 100644 --- a/lib/ESP32_BLE_Arduino/library.properties +++ b/lib/ESP32_BLE_Arduino/library.properties @@ -1,5 +1,5 @@ name=ESP32 BLE Arduino -version=0.4.16 +version=0.4.9 author=Neil Kolban maintainer=Neil Kolban sentence=BLE functions for ESP32 diff --git a/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.cpp b/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.cpp index 67603df..351c5e1 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.cpp @@ -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 */ diff --git a/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.h b/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.h index a3b1e6e..41bc4c6 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.h +++ b/lib/ESP32_BLE_Arduino/src/BLEAdvertisedDevice.h @@ -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; }; /** diff --git a/lib/ESP32_BLE_Arduino/src/BLEAdvertising.cpp b/lib/ESP32_BLE_Arduino/src/BLEAdvertising.cpp index 4b3cb8d..9e01ab7 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEAdvertising.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEAdvertising.cpp @@ -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. diff --git a/lib/ESP32_BLE_Arduino/src/BLEAdvertising.h b/lib/ESP32_BLE_Arduino/src/BLEAdvertising.h index e316529..003ad1a 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEAdvertising.h +++ b/lib/ESP32_BLE_Arduino/src/BLEAdvertising.h @@ -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); diff --git a/lib/ESP32_BLE_Arduino/src/BLECharacteristic.cpp b/lib/ESP32_BLE_Arduino/src/BLECharacteristic.cpp index 931c753..5e5aa2a 100644 --- a/lib/ESP32_BLE_Arduino/src/BLECharacteristic.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLECharacteristic.cpp @@ -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. diff --git a/lib/ESP32_BLE_Arduino/src/BLECharacteristic.h b/lib/ESP32_BLE_Arduino/src/BLECharacteristic.h index b3f8d2e..10dc787 100644 --- a/lib/ESP32_BLE_Arduino/src/BLECharacteristic.h +++ b/lib/ESP32_BLE_Arduino/src/BLECharacteristic.h @@ -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(); diff --git a/lib/ESP32_BLE_Arduino/src/BLEClient.cpp b/lib/ESP32_BLE_Arduino/src/BLEClient.cpp index 141cf0f..55af054 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEClient.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEClient.cpp @@ -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* 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 diff --git a/lib/ESP32_BLE_Arduino/src/BLEDescriptor.cpp b/lib/ESP32_BLE_Arduino/src/BLEDescriptor.cpp index 58ff78b..1a72ef3 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEDescriptor.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEDescriptor.cpp @@ -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); diff --git a/lib/ESP32_BLE_Arduino/src/BLEDevice.cpp b/lib/ESP32_BLE_Arduino/src/BLEDevice.cpp index a7db454..b333ed7 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEDevice.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEDevice.cpp @@ -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 diff --git a/lib/ESP32_BLE_Arduino/src/BLEDevice.h b/lib/ESP32_BLE_Arduino/src/BLEDevice.h index 7a1b833..7f33143 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEDevice.h +++ b/lib/ESP32_BLE_Arduino/src/BLEDevice.h @@ -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; diff --git a/lib/ESP32_BLE_Arduino/src/BLEScan.cpp b/lib/ESP32_BLE_Arduino/src/BLEScan.cpp index 3046b7c..e450664 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEScan.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEScan.cpp @@ -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 diff --git a/lib/ESP32_BLE_Arduino/src/BLEScan.h b/lib/ESP32_BLE_Arduino/src/BLEScan.h index 76c7c7c..c905c43 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEScan.h +++ b/lib/ESP32_BLE_Arduino/src/BLEScan.h @@ -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 */ diff --git a/lib/ESP32_BLE_Arduino/src/BLEServer.cpp b/lib/ESP32_BLE_Arduino/src/BLEServer.cpp index d5c9de6..9d26eb5 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEServer.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEServer.cpp @@ -18,6 +18,7 @@ #include "BLEUtils.h" #include #include +#include #include #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. diff --git a/lib/ESP32_BLE_Arduino/src/BLEServer.h b/lib/ESP32_BLE_Arduino/src/BLEServer.h index 95c55d5..bc0ef05 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEServer.h +++ b/lib/ESP32_BLE_Arduino/src/BLEServer.h @@ -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 m_uuidMap; std::map m_handleMap; - std::map m_uuidMap; - std::map::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: diff --git a/lib/ESP32_BLE_Arduino/src/BLEService.cpp b/lib/ESP32_BLE_Arduino/src/BLEService.cpp index 340ea56..32bcc57 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEService.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEService.cpp @@ -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 diff --git a/lib/ESP32_BLE_Arduino/src/BLEService.h b/lib/ESP32_BLE_Arduino/src/BLEService.h index 93b4b2c..2b78e62 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEService.h +++ b/lib/ESP32_BLE_Arduino/src/BLEService.h @@ -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; diff --git a/lib/ESP32_BLE_Arduino/src/BLEServiceMap.cpp b/lib/ESP32_BLE_Arduino/src/BLEServiceMap.cpp index dd828fa..8fdbd5a 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEServiceMap.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEServiceMap.cpp @@ -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(service, uuid.toString())); + m_uuidMap.insert(std::pair(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 */ diff --git a/lib/ESP32_BLE_Arduino/src/BLEUtils.cpp b/lib/ESP32_BLE_Arduino/src/BLEUtils.cpp index a33ee27..ff4ebfa 100644 --- a/lib/ESP32_BLE_Arduino/src/BLEUtils.cpp +++ b/lib/ESP32_BLE_Arduino/src/BLEUtils.cpp @@ -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