diff --git a/.github/workflows/arduino-ci.yml b/.github/workflows/arduino-ci.yml index 7deac45..5d05fdc 100644 --- a/.github/workflows/arduino-ci.yml +++ b/.github/workflows/arduino-ci.yml @@ -31,7 +31,10 @@ jobs: - "esp32:esp32:esp32" - "esp32:esp32:esp32s3" - "esp32:esp32:esp32c3" - nimble: [ false, true ] + nimble: + - false + - '1.4.0' + - '2.3.2' fail-fast: false steps: @@ -40,9 +43,11 @@ jobs: - uses: arduino/compile-sketches@v1 with: fqbn: ${{ matrix.fqbn }} + sketch-paths: | + - examples/SerialToSerialBLE libraries: | - source-path: ./ - - name: NimBLE-Arduino + ${{ matrix.nimble != false && format('- name: NimBLE-Arduino@{0}', matrix.nimble) || '' }} cli-compile-flags: | - --build-property - - build.extra_flags="-DBLESERIAL_USE_NIMBLE=${{ matrix.nimble }}" \ No newline at end of file + - build.extra_flags="-DBLESERIAL_USE_NIMBLE=${{ matrix.nimble != false }}" \ No newline at end of file diff --git a/.github/workflows/platformio-ci.yml b/.github/workflows/platformio-ci.yml index c06d82b..82b3c07 100644 --- a/.github/workflows/platformio-ci.yml +++ b/.github/workflows/platformio-ci.yml @@ -21,9 +21,31 @@ jobs: os: [ ubuntu-latest ] example: - "SerialToSerialBLE" + # - "SerialToSerialBLE_TransparentUART" + - "SerialToSerialBLE_TransparentUART-NimBLE" boards: - [ esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1 ] - nimble: [ false, true ] + nimble: + - false + - '^1.4.0' + - '^2.0.0' + + exclude: + - example: "SerialToSerialBLE_TransparentUART-NimBLE" + nimble: false + # - example: "SerialToSerialBLE_TransparentUART" + # nimble: '^1.4.0' + # - example: "SerialToSerialBLE_TransparentUART" + # nimble: '^2.0.0' + include: + - example: "SerialToSerialBLE_TransparentUART-NimBLE" + nimble: '^2.0.0' + os: ubuntu-latest + boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1] + - example: "SerialToSerialBLE_TransparentUART-NimBLE" + nimble: '^1.4.0' + os: ubuntu-latest + boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1] steps: - uses: actions/checkout@v4 @@ -60,11 +82,14 @@ jobs: - name: Build example run: | - pio ci --lib="." --board=${{ join(matrix.boards, ' --board=') }} ${{ matrix.nimble && '--project-option="lib_deps=h2zero/NimBLE-Arduino@^1.4.0"' || '' }} + pio ci --lib="." --board=${{ join(matrix.boards, ' --board=') }} ${{ matrix.nimble && format('--project-option="lib_deps=h2zero/NimBLE-Arduino@{0}"', matrix.nimble) || '' }} env: PLATFORMIO_CI_SRC: "./examples/${{ matrix.example }}/*.ino" + PLATFORMIO_BUILD_UNFLAGS: | + -std=gnu++11 PLATFORMIO_BUILD_FLAGS: | - -D BLESERIAL_USE_NIMBLE=${{ matrix.nimble }} + -std=gnu++17 + -D BLESERIAL_USE_NIMBLE=${{ matrix.nimble != false }} -Wall -Wextra -Wpedantic diff --git a/README.md b/README.md index b246787..88d45c1 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This library allows using Nordic UART Service (NUS) with ESP32 Arduino. [![GitHub release](https://img.shields.io/github/v/release/senseshift/arduino-ble-serial)](https://github.com/senseshift/arduino-ble-serial/releases/latest) [![PlatformIO CI](https://github.com/senseshift/arduino-ble-serial/actions/workflows/platformio-ci.yml/badge.svg)](https://github.com/senseshift/arduino-ble-serial/actions/workflows/platformio-ci.yml) -[![PlatformIO CI](https://github.com/senseshift/arduino-ble-serial/actions/workflows/arduino-ci.yml/badge.svg)](https://github.com/senseshift/arduino-ble-serial/actions/workflows/arduino-ci.yml) +[![Arduino CI](https://github.com/senseshift/arduino-ble-serial/actions/workflows/arduino-ci.yml/badge.svg)](https://github.com/senseshift/arduino-ble-serial/actions/workflows/arduino-ci.yml) [![MIT](https://img.shields.io/github/license/senseshift/arduino-ble-serial)](/LICENSE) [![GitHub contributors](https://img.shields.io/github/contributors/senseshift/arduino-ble-serial)](https://github.com/senseshift/arduino-ble-serial/graphs/contributors) diff --git a/examples/SerialToSerialBLE/SerialToSerialBLE.ino b/examples/SerialToSerialBLE/SerialToSerialBLE.ino index e2d2287..aef0fcf 100644 --- a/examples/SerialToSerialBLE/SerialToSerialBLE.ino +++ b/examples/SerialToSerialBLE/SerialToSerialBLE.ino @@ -7,9 +7,14 @@ String device_name = "ESP32-BLE-Slave"; -// Mind the empty template argument (<>), it is required for -// the code to compile with the current Arduino C++ compiler version -BLESerial<> SerialBLE; +BLESerial SerialBLE; +// If you are using an older version of Arduino IDE or C++ compiler, you may need to use +// an empty template argument (<>), as Class Template Argument Deduction (CTAD) is not +// supported in C++ versions older than C++17. For more details, see: +// https://www.cppreference.com/w/cpp/language/ctad.html +// +// Uncomment the line below if you are using an older version of Arduino IDE/C++ compiler +// BLESerial<> SerialBLE; // FOR ETL: Uncomment one of the following lines // BLESerial> SerialBLE; diff --git a/examples/SerialToSerialBLE_TransparentUART-NimBLE/SerialToSerialBLE_TransparentUART-NimBLE.ino b/examples/SerialToSerialBLE_TransparentUART-NimBLE/SerialToSerialBLE_TransparentUART-NimBLE.ino new file mode 100644 index 0000000..a9cd23b --- /dev/null +++ b/examples/SerialToSerialBLE_TransparentUART-NimBLE/SerialToSerialBLE_TransparentUART-NimBLE.ino @@ -0,0 +1,48 @@ +#include + +// FOR ETL: Uncomment the following lines +// #include +// #include +// #include + +BLESerial SerialBLE; +// If you are using an older version of Arduino IDE or C++ compiler, you may need to use +// an empty template argument (<>), as Class Template Argument Deduction (CTAD) is not +// supported in C++ versions older than C++17. For more details, see: +// https://www.cppreference.com/w/cpp/language/ctad.html +// +// Uncomment the line below if you are using an older version of Arduino IDE/C++ compiler +// BLESerial<> SerialBLE; + +// FOR ETL: Uncomment one of the following lines +// BLESerial> SerialBLE; +// OR +// BLESerial> SerialBLE; + +void setup() { + BLEDevice::init("ESP32-BLE-Slave"); + + BLEServer* pServer = BLEDevice::createServer(); + + // Transparent UART Service + // https://developerhelp.microchip.com/xwiki/bin/view/applications/ble/android-development-for-bm70rn4870/transparent-uart-service-for-bm70rn4870/ + auto pService = pServer->createService("49535343-FE7D-4AE5-8FA9-9FAFD205E455"); + + auto pRxCharacteristic = pService->createCharacteristic("49535343-1E4D-4BD9-BA61-23C647249616", NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::NOTIFY); + auto pTxCharacteristic = pService->createCharacteristic("49535343-8841-43F4-A8D4-ECBE34729BB3", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY); + + SerialBLE.begin(pRxCharacteristic, pTxCharacteristic); + + BLEAdvertising* pAdvertising = pServer->getAdvertising(); + pAdvertising->start(); +} + +void loop() { + if (Serial.available()) { + SerialBLE.write(Serial.read()); + SerialBLE.flush(); + } + if (SerialBLE.available()) { + Serial.write(SerialBLE.read()); + } +} \ No newline at end of file diff --git a/examples/SerialToSerialBLE_TransparentUART/SerialToSerialBLE_TransparentUART.ino b/examples/SerialToSerialBLE_TransparentUART/SerialToSerialBLE_TransparentUART.ino new file mode 100644 index 0000000..a615b97 --- /dev/null +++ b/examples/SerialToSerialBLE_TransparentUART/SerialToSerialBLE_TransparentUART.ino @@ -0,0 +1,48 @@ +#include + +// FOR ETL: Uncomment the following lines +// #include +// #include +// #include + +BLESerial SerialBLE; +// If you are using an older version of Arduino IDE or C++ compiler, you may need to use +// an empty template argument (<>), as Class Template Argument Deduction (CTAD) is not +// supported in C++ versions older than C++17. For more details, see: +// https://www.cppreference.com/w/cpp/language/ctad.html +// +// Uncomment the line below if you are using an older version of Arduino IDE/C++ compiler +// BLESerial<> SerialBLE; + +// FOR ETL: Uncomment one of the following lines +// BLESerial> SerialBLE; +// OR +// BLESerial> SerialBLE; + +void setup() { + BLEDevice::init("ESP32-BLE-Slave"); + + BLEServer* pServer = BLEDevice::createServer(); + + // Transparent UART Service + // https://developerhelp.microchip.com/xwiki/bin/view/applications/ble/android-development-for-bm70rn4870/transparent-uart-service-for-bm70rn4870/ + auto pService = pServer->createService("49535343-FE7D-4AE5-8FA9-9FAFD205E455"); + + auto pRxCharacteristic = pService->createCharacteristic("49535343-1E4D-4BD9-BA61-23C647249616", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_NOTIFY); + auto pTxCharacteristic = pService->createCharacteristic("49535343-8841-43F4-A8D4-ECBE34729BB3", BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); + + SerialBLE.begin(pRxCharacteristic, pTxCharacteristic); + + BLEAdvertising* pAdvertising = pServer->getAdvertising(); + pAdvertising->start(); +} + +void loop() { + if (Serial.available()) { + SerialBLE.write(Serial.read()); + SerialBLE.flush(); + } + if (SerialBLE.available()) { + Serial.write(SerialBLE.read()); + } +} \ No newline at end of file diff --git a/library.json b/library.json index 9d88316..7d55903 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json", "name": "Serial_BLE", - "version": "1.1.3", + "version": "1.2.0", "description": "Customizable Arduino and ESP32 BLE Serial library, compliant with Nordic UART Service and others.", "keywords": [ "serial", @@ -46,6 +46,6 @@ "srcDir": "src" }, "dependencies": { - "ESP32 BLE Arduino": "^2.0.0" + "BLE": "^2.0.0" } } \ No newline at end of file diff --git a/library.properties b/library.properties index ef729ec..985b323 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Serial_BLE -version=1.1.3 +version=1.2.0 author=Leonid Meleshin maintainer=Leonid Meleshin sentence=Customizable BLE Serial (UART) library. diff --git a/src/BLESerial.h b/src/BLESerial.h index fd388fb..ae86e4a 100644 --- a/src/BLESerial.h +++ b/src/BLESerial.h @@ -19,6 +19,16 @@ # include #endif // BLESERIAL_USE_NIMBLE +#if !defined(BLESERIAL_NIMBLE_VERSION_MAJOR) && defined(BLESERIAL_USE_NIMBLE) && BLESERIAL_USE_NIMBLE +# if __has_include() +# define BLESERIAL_NIMBLE_VERSION_MAJOR 2 +// # warning "Using NimBLE version 2" +# else // __has_include() +# define BLESERIAL_NIMBLE_VERSION_MAJOR 1 +// # warning "Using NimBLE version 1" +# endif // __has_include() +#endif // BLESERIAL_USE_NIMBLE + #ifndef BLESERIAL_USE_STL # define BLESERIAL_USE_STL true #endif // BLESERIAL_USE_STL @@ -262,7 +272,17 @@ class BLESerialServerCallbacks : public BLEServerCallbacks { public: explicit BLESerialServerCallbacks(BLESerial* bleSerial) : bleSerial(bleSerial) {} +#if defined(BLESERIAL_USE_NIMBLE) && BLESERIAL_USE_NIMBLE +# if defined(BLESERIAL_NIMBLE_VERSION_MAJOR) && BLESERIAL_NIMBLE_VERSION_MAJOR >= 2 +// # warning "Using NimBLE version 2 for BLESerialServerCallbacks" + void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) override +# else // BLESERIAL_NIMBLE_VERSION_MAJOR >= 2 +// # warning "Using NimBLE version 1 for BLESerialServerCallbacks" + void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc* desc) override +# endif // BLESERIAL_NIMBLE_VERSION_MAJOR >= 2 +#else // BLESERIAL_USE_NIMBLE void onDisconnect(BLEServer* pServer) override +#endif // BLESERIAL_USE_NIMBLE { auto* pAdvertising = pServer->getAdvertising(); if (pAdvertising == nullptr) { @@ -280,7 +300,17 @@ class BLESerialCharacteristicCallbacks : public BLECharacteristicCallbacks { public: explicit BLESerialCharacteristicCallbacks(BLESerial* bleSerial) : bleSerial(bleSerial) {} +#if defined(BLESERIAL_USE_NIMBLE) && BLESERIAL_USE_NIMBLE +# if defined(BLESERIAL_NIMBLE_VERSION_MAJOR) && BLESERIAL_NIMBLE_VERSION_MAJOR >= 2 +// # warning "Using NimBLE version 2 for BLESerialCharacteristicCallbacks" + void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo) override +# else // BLESERIAL_NIMBLE_VERSION_MAJOR >= 2 +// # warning "Using NimBLE version 1 for BLESerialCharacteristicCallbacks" + void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc) override +# endif // BLESERIAL_NIMBLE_VERSION_MAJOR >= 2 +#else // BLESERIAL_USE_NIMBLE void onWrite(BLECharacteristic* pCharacteristic) override +#endif // BLESERIAL_USE_NIMBLE { if (pCharacteristic != bleSerial->m_pRxCharacteristic) { return;