diff --git a/.github/workflows/platformio-ci.yml b/.github/workflows/platformio-ci.yml index 82b3c07..3562921 100644 --- a/.github/workflows/platformio-ci.yml +++ b/.github/workflows/platformio-ci.yml @@ -23,6 +23,7 @@ jobs: - "SerialToSerialBLE" # - "SerialToSerialBLE_TransparentUART" - "SerialToSerialBLE_TransparentUART-NimBLE" + # - "SerialToSerialBLE_Secure" boards: - [ esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1 ] nimble: @@ -47,6 +48,11 @@ jobs: os: ubuntu-latest boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1] + - example: "SerialToSerialBLE_Secure" + nimble: false + os: ubuntu-latest + boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1] + steps: - uses: actions/checkout@v4 diff --git a/examples/SerialToSerialBLE_Secure/SerialToSerialBLE_Secure.ino b/examples/SerialToSerialBLE_Secure/SerialToSerialBLE_Secure.ino new file mode 100644 index 0000000..b6d30f8 --- /dev/null +++ b/examples/SerialToSerialBLE_Secure/SerialToSerialBLE_Secure.ino @@ -0,0 +1,86 @@ +#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; + +class AppSecurityCallbacks : public BLESecurityCallbacks { + public: + AppSecurityCallbacks() { + this->passKey = random(111111, 999999); + } + + uint32_t onPassKeyRequest(){ + ESP_LOGI(LOG_TAG, "PassKeyRequest"); + + // Generate a random passkey + this->passKey = random(111111, 999999); + + return this->passKey; + } + + void onPassKeyNotify(uint32_t pass_key){ + ESP_LOGI(LOG_TAG, "The passkey Notify number: %d", pass_key); + } + + bool onConfirmPIN(uint32_t pass_key){ + ESP_LOGI(LOG_TAG, "The passkey YES/NO number: %d", pass_key); + vTaskDelay(5000); + return true; + } + + bool onSecurityRequest(){ + ESP_LOGI(LOG_TAG, "SecurityRequest"); + return true; + } + + void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){ + ESP_LOGI(LOG_TAG, "Starting BLE work!"); + } + + private: + uint32_t passKey; +}; + +void setup() { + BLEDevice::init("ESP32-BLE-Slave"); + BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT); + BLEDevice::setSecurityCallbacks(new AppSecurityCallbacks()); + + BLEServer* pServer = BLEDevice::createServer(); + + BLESecurity *pSecurity = new BLESecurity(); + pSecurity->setKeySize(); + pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY); + pSecurity->setCapability(ESP_IO_CAP_IO); + pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK); + + Serial.begin(9600); + SerialBLE.begin(pServer); +} + +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 7d55903..6d3a214 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.2.0", + "version": "1.2.1", "description": "Customizable Arduino and ESP32 BLE Serial library, compliant with Nordic UART Service and others.", "keywords": [ "serial", diff --git a/library.properties b/library.properties index 985b323..abed17c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Serial_BLE -version=1.2.0 +version=1.2.1 author=Leonid Meleshin maintainer=Leonid Meleshin sentence=Customizable BLE Serial (UART) library.