diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index a1faf997..b33d6c75 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -22,3 +22,36 @@ jobs: with: github-token: ${{ secrets.GITHUB_TOKEN }} fqbn: ${{ matrix.fqbn }} + + build-for-esp32: + runs-on: ubuntu-latest + + strategy: + matrix: + fqbn: + - esp32:esp32:esp32 + - esp32:esp32:esp32s3 + - esp32:esp32:esp32c3 + # future bluetooth chips + #- esp32:esp32:esp32c2 + #- esp32:esp32:esp32c6 + #- esp32:esp32:esp32h2 + + steps: + - uses: actions/checkout@v3 + - uses: arduino/compile-sketches@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + fqbn: ${{ matrix.fqbn }} + platforms: | + - name: esp32:esp32 + source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json + sketch-paths: | + - examples/Central/Scan + - examples/Central/PeripheralExplorer + - examples/Central/ScanCallback + - examples/Central/SensorTagButton + - examples/Peripheral/Advertising/EnhancedAdvertising + - examples/Peripheral/Advertising/RawDataAdvertising + cli-compile-flags: | + - --warnings="none" diff --git a/library.properties b/library.properties index e2a3f15f..0648b56a 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=ArduinoBLE -version=1.3.1 +version=1.3.2 author=Arduino maintainer=Arduino sentence=Enables Bluetooth® Low Energy connectivity on the Arduino MKR WiFi 1010, Arduino UNO WiFi Rev.2, Arduino Nano 33 IoT, Arduino Nano 33 BLE and Nicla Sense ME. paragraph=This library supports creating a Bluetooth® Low Energy peripheral & central mode. category=Communication url=https://www.arduino.cc/en/Reference/ArduinoBLE -architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla +architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla,esp32 includes=ArduinoBLE.h diff --git a/src/BLEService.cpp b/src/BLEService.cpp index b3f33739..ed54f9ff 100644 --- a/src/BLEService.cpp +++ b/src/BLEService.cpp @@ -63,6 +63,13 @@ BLEService::BLEService(const BLEService& other) } } +void BLEService::clear() +{ + if (_local) { + _local->clear(); + } +} + BLEService::~BLEService() { if (_local && _local->release() == 0) { diff --git a/src/BLEService.h b/src/BLEService.h index a8d8b010..5b2440d8 100644 --- a/src/BLEService.h +++ b/src/BLEService.h @@ -33,6 +33,7 @@ class BLEService { virtual ~BLEService(); const char* uuid() const; + void clear(); void addCharacteristic(BLECharacteristic& characteristic); diff --git a/src/local/BLELocalAttribute.cpp b/src/local/BLELocalAttribute.cpp index ce3010a0..9929fa41 100644 --- a/src/local/BLELocalAttribute.cpp +++ b/src/local/BLELocalAttribute.cpp @@ -56,6 +56,11 @@ int BLELocalAttribute::retain() return _refCount; } +bool BLELocalAttribute::active() +{ + return _refCount > 0; +} + int BLELocalAttribute::release() { _refCount--; diff --git a/src/local/BLELocalAttribute.h b/src/local/BLELocalAttribute.h index 2af948c3..53a0abfe 100644 --- a/src/local/BLELocalAttribute.h +++ b/src/local/BLELocalAttribute.h @@ -44,6 +44,7 @@ class BLELocalAttribute int retain(); int release(); + bool active(); protected: friend class ATTClass; diff --git a/src/local/BLELocalCharacteristic.cpp b/src/local/BLELocalCharacteristic.cpp index 63208b05..ef19e726 100644 --- a/src/local/BLELocalCharacteristic.cpp +++ b/src/local/BLELocalCharacteristic.cpp @@ -46,6 +46,7 @@ BLELocalCharacteristic::BLELocalCharacteristic(const char* uuid, uint16_t permis if (permissions & (BLENotify | BLEIndicate)) { BLELocalDescriptor* cccd = new BLELocalDescriptor("2902", (uint8_t*)&_cccdValue, sizeof(_cccdValue)); + cccd->retain(); _descriptors.add(cccd); } diff --git a/src/local/BLELocalService.cpp b/src/local/BLELocalService.cpp index 58957342..7fda3cb4 100644 --- a/src/local/BLELocalService.cpp +++ b/src/local/BLELocalService.cpp @@ -28,6 +28,12 @@ BLELocalService::BLELocalService(const char* uuid) : { } +void BLELocalService::clear() { + _characteristics.clear(); + _startHandle = 0; + _endHandle = 0; +} + BLELocalService::~BLELocalService() { for (unsigned int i = 0; i < characteristicCount(); i++) { @@ -37,8 +43,7 @@ BLELocalService::~BLELocalService() delete c; } } - - _characteristics.clear(); + clear(); } enum BLEAttributeType BLELocalService::type() const diff --git a/src/local/BLELocalService.h b/src/local/BLELocalService.h index e0179a93..f17c610c 100644 --- a/src/local/BLELocalService.h +++ b/src/local/BLELocalService.h @@ -36,6 +36,7 @@ class BLELocalService : public BLELocalAttribute { virtual enum BLEAttributeType type() const; void addCharacteristic(BLECharacteristic& characteristic); + void clear(); protected: friend class ATTClass; diff --git a/src/utility/ATT.cpp b/src/utility/ATT.cpp index 439b88e8..acdf5a9f 100644 --- a/src/utility/ATT.cpp +++ b/src/utility/ATT.cpp @@ -1272,7 +1272,7 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op } return; } - // Check permssion + // Check permission if((characteristic->permissions() &( BLEPermission::BLEEncryption >> 8)) > 0 && (getPeerEncryption(connectionHandle) & PEER_ENCRYPTION::ENCRYPTED_AES) == 0){ holdResponse = true; @@ -1941,4 +1941,4 @@ int ATTClass::getPeerResolvedAddress(uint16_t connectionHandle, uint8_t resolved #if !defined(FAKE_ATT) ATTClass ATTObj; ATTClass& ATT = ATTObj; -#endif \ No newline at end of file +#endif diff --git a/src/utility/BLEUuid.cpp b/src/utility/BLEUuid.cpp index fba6244a..0465ea9a 100644 --- a/src/utility/BLEUuid.cpp +++ b/src/utility/BLEUuid.cpp @@ -30,6 +30,11 @@ BLEUuid::BLEUuid(const char * str) : memset(_data, 0x00, sizeof(_data)); _length = 0; + + if (str == NULL) { + return; + } + for (int i = strlen(str) - 1; i >= 0 && _length < BLE_UUID_MAX_LENGTH; i -= 2) { if (str[i] == '-') { i++; diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index 98d5f4ad..be914f6f 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -149,6 +149,7 @@ void GATTClass::addService(BLELocalService* service) { service->retain(); _attributes.add(service); + _services.add(service); uint16_t startHandle = attributeCount(); @@ -160,6 +161,7 @@ void GATTClass::addService(BLELocalService* service) characteristic->setHandle(attributeCount()); // add the characteristic again to make space of the characteristic value handle + characteristic->retain(); _attributes.add(characteristic); for (unsigned int j = 0; j < characteristic->descriptorCount(); j++) { @@ -183,8 +185,13 @@ void GATTClass::clearAttributes() delete a; } } - _attributes.clear(); + + for (unsigned int i = 0; i < _services.size(); i++) { + _services.get(i)->clear(); + } + _services.clear(); + } #if !defined(FAKE_GATT) diff --git a/src/utility/GATT.h b/src/utility/GATT.h index fd916709..51d1537e 100644 --- a/src/utility/GATT.h +++ b/src/utility/GATT.h @@ -59,6 +59,7 @@ class GATTClass { private: BLELinkedList _attributes; + BLELinkedList _services; BLELocalService* _genericAccessService; BLELocalCharacteristic* _deviceNameCharacteristic; diff --git a/src/utility/HCI.cpp b/src/utility/HCI.cpp index 773f4f18..1902abe7 100644 --- a/src/utility/HCI.cpp +++ b/src/utility/HCI.cpp @@ -460,14 +460,14 @@ int HCIClass::leConnUpdate(uint16_t handle, uint16_t minInterval, uint16_t maxIn return sendCommand(OGF_LE_CTL << 10 | OCF_LE_CONN_UPDATE, sizeof(leConnUpdateData), &leConnUpdateData); } -int HCIClass::saveNewAddress(uint8_t addressType, uint8_t* address, uint8_t* peerIrk, uint8_t* localIrk){ +void HCIClass::saveNewAddress(uint8_t addressType, uint8_t* address, uint8_t* peerIrk, uint8_t* localIrk){ if(_storeIRK!=0){ _storeIRK(address, peerIrk); } // Again... this should work // leAddResolvingAddress(addressType, address, peerIrk, localIrk); } -int HCIClass::leAddResolvingAddress(uint8_t addressType, uint8_t* peerAddress, uint8_t* peerIrk, uint8_t* localIrk){ +void HCIClass::leAddResolvingAddress(uint8_t addressType, uint8_t* peerAddress, uint8_t* peerIrk, uint8_t* localIrk){ leStopResolvingAddresses(); struct __attribute__ ((packed)) AddDevice { @@ -527,7 +527,7 @@ int HCIClass::leReadPeerResolvableAddress(uint8_t peerAddressType, uint8_t* peer return res; } -int HCIClass::writeLK(uint8_t peerAddress[], uint8_t LK[]){ +void HCIClass::writeLK(uint8_t peerAddress[], uint8_t LK[]){ struct __attribute__ ((packed)) StoreLK { uint8_t nKeys; uint8_t BD_ADDR[6]; @@ -538,7 +538,7 @@ int HCIClass::writeLK(uint8_t peerAddress[], uint8_t LK[]){ for(int i=0; i<16; i++) storeLK.LTK[15-i] = LK[i]; HCI.sendCommand(OGF_HOST_CTL << 10 | 0x11, sizeof(storeLK), &storeLK); } -int HCIClass::readStoredLKs(){ +void HCIClass::readStoredLKs(){ uint8_t BD_ADDR[6]; readStoredLK(BD_ADDR, 1); } @@ -1116,6 +1116,10 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[]) uint8_t eirData[31]; } *leAdvertisingReport = (EvtLeAdvertisingReport*)&pdata[sizeof(HCIEventHdr) + sizeof(LeMetaEventHeader)]; + if(leAdvertisingReport->eirLength > sizeof(leAdvertisingReport->eirData)){ + return ; + } + if (leAdvertisingReport->status == 0x01) { // last byte is RSSI int8_t rssi = leAdvertisingReport->eirData[leAdvertisingReport->eirLength]; diff --git a/src/utility/HCI.h b/src/utility/HCI.h index af46265a..0a530ceb 100644 --- a/src/utility/HCI.h +++ b/src/utility/HCI.h @@ -35,7 +35,7 @@ enum LE_COMMAND { ENCRYPT = 0x0017, RANDOM = 0x0018, LONG_TERM_KEY_REPLY = 0x001A, - LONG_TERM_KEY_NEGATIVE_REPLY = 0x1B, + LONG_TERM_KEY_NEGATIVE_REPLY = 0x001B, READ_LOCAL_P256 = 0x0025, GENERATE_DH_KEY_V1 = 0x0026, GENERATE_DH_KEY_V2 = 0x005E @@ -99,15 +99,15 @@ class HCIClass { virtual AuthReq localAuthreq(); virtual uint8_t localIOCap(); - virtual int saveNewAddress(uint8_t addressType, uint8_t* address, uint8_t* peerIrk, uint8_t* remoteIrk); - virtual int leAddResolvingAddress(uint8_t addressType, uint8_t* address, uint8_t* peerIrk, uint8_t* remoteIrk); + virtual void saveNewAddress(uint8_t addressType, uint8_t* address, uint8_t* peerIrk, uint8_t* remoteIrk); + virtual void leAddResolvingAddress(uint8_t addressType, uint8_t* address, uint8_t* peerIrk, uint8_t* remoteIrk); virtual int leStopResolvingAddresses(); virtual int leStartResolvingAddresses(); virtual int leReadPeerResolvableAddress(uint8_t peerAddressType, uint8_t* peerIdentityAddress, uint8_t* peerResolvableAddress); - virtual int readStoredLKs(); + virtual void readStoredLKs(); virtual int readStoredLK(uint8_t BD_ADDR[], uint8_t read_all = 0); - virtual int writeLK(uint8_t peerAddress[], uint8_t LK[]); + virtual void writeLK(uint8_t peerAddress[], uint8_t LK[]); virtual int tryResolveAddress(uint8_t* BDAddr, uint8_t* address); virtual int sendAclPkt(uint16_t handle, uint8_t cid, uint8_t plen, void* data); diff --git a/src/utility/HCIUartTransport.cpp b/src/utility/HCIUartTransport.cpp index 37ce95e7..1b1a513a 100644 --- a/src/utility/HCIUartTransport.cpp +++ b/src/utility/HCIUartTransport.cpp @@ -17,7 +17,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#if !defined(ARDUINO_ARCH_MBED) || defined(TARGET_NANO_RP2040_CONNECT) +#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) || defined(TARGET_NANO_RP2040_CONNECT) #include "HCIUartTransport.h" diff --git a/src/utility/HCIVirtualTransport.cpp b/src/utility/HCIVirtualTransport.cpp new file mode 100644 index 00000000..046a0e72 --- /dev/null +++ b/src/utility/HCIVirtualTransport.cpp @@ -0,0 +1,142 @@ +/* + This file is part of the ArduinoBLE library. + Copyright (c) 2018 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#if defined(ESP32) + +#include "HCIVirtualTransport.h" + +StreamBufferHandle_t rec_buffer; +StreamBufferHandle_t send_buffer; +TaskHandle_t bleHandle; + + +static void notify_host_send_available(void) +{ +} + +static int notify_host_recv(uint8_t *data, uint16_t length) +{ + xStreamBufferSend(rec_buffer,data,length,portMAX_DELAY); // !!!potentially waiting forever + return 0; +} + +static esp_vhci_host_callback_t vhci_host_cb = { + notify_host_send_available, + notify_host_recv +}; + +void bleTask(void *pvParameters) +{ + esp_vhci_host_register_callback(&vhci_host_cb); + size_t length; + uint8_t mybuf[258]; + + while(true){ + length = xStreamBufferReceive(send_buffer,mybuf,258,portMAX_DELAY); + while (!esp_vhci_host_check_send_available()) {} + esp_vhci_host_send_packet(mybuf, length); + } +} + + +HCIVirtualTransportClass::HCIVirtualTransportClass() +{ +} + +HCIVirtualTransportClass::~HCIVirtualTransportClass() +{ +} + +int HCIVirtualTransportClass::begin() +{ + btStarted(); // this somehow stops the arduino ide from initializing bluedroid + + rec_buffer = xStreamBufferCreate(258, 1); + send_buffer = xStreamBufferCreate(258, 1); + + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + +#if CONFIG_IDF_TARGET_ESP32 + bt_cfg.mode = ESP_BT_MODE_BLE; //original esp32 chip +#else + bt_cfg.bluetooth_mode = ESP_BT_MODE_BLE; //different api for newer models +#endif + + esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); + esp_bt_controller_init(&bt_cfg); + esp_bt_controller_enable(ESP_BT_MODE_BLE); + xTaskCreatePinnedToCore(&bleTask, "bleTask", 2048, NULL, 5, &bleHandle, 0); + return 1; +} + +void HCIVirtualTransportClass::end() +{ + vStreamBufferDelete(rec_buffer); + vStreamBufferDelete(send_buffer); + esp_bt_controller_disable(); + esp_bt_controller_deinit(); + vTaskDelete(bleHandle); +} + +void HCIVirtualTransportClass::wait(unsigned long timeout) +{ + for (unsigned long start = (esp_timer_get_time() / 1000ULL); ((esp_timer_get_time() / 1000ULL) - start) < timeout;) { + if (available()) { + break; + } + } +} + +int HCIVirtualTransportClass::available() +{ + size_t bytes = xStreamBufferBytesAvailable(rec_buffer); + return bytes; +} + +// never called +int HCIVirtualTransportClass::peek() +{ + return -1; +} + +int HCIVirtualTransportClass::read() +{ + uint8_t c; + if(xStreamBufferReceive(rec_buffer, &c, 1, portMAX_DELAY)) { + return c; + } + return -1; +} + +size_t HCIVirtualTransportClass::write(const uint8_t* data, size_t length) +{ + size_t result = xStreamBufferSend(send_buffer,data,length,portMAX_DELAY); + return result; +} + +HCIVirtualTransportClass HCIVirtualTransport; + +HCITransportInterface& HCITransport = HCIVirtualTransport; + +#endif diff --git a/src/utility/HCIVirtualTransport.h b/src/utility/HCIVirtualTransport.h new file mode 100644 index 00000000..0da43cac --- /dev/null +++ b/src/utility/HCIVirtualTransport.h @@ -0,0 +1,50 @@ +/* + This file is part of the ArduinoBLE library. + Copyright (c) 2018 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "HCITransport.h" +#include +#include +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/stream_buffer.h" + +#include "esp_bt.h" +#include "nvs_flash.h" + +#include "esp32-hal-bt.h" // this is needed to disable bluedroid + + +class HCIVirtualTransportClass : public HCITransportInterface { +public: + HCIVirtualTransportClass(); + virtual ~HCIVirtualTransportClass(); + + virtual int begin(); + virtual void end(); + + virtual void wait(unsigned long timeout); + + virtual int available(); + virtual int peek(); + virtual int read(); + + virtual size_t write(const uint8_t* data, size_t length); +}; \ No newline at end of file