Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/platformio-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- "SerialToSerialBLE"
# - "SerialToSerialBLE_TransparentUART"
- "SerialToSerialBLE_TransparentUART-NimBLE"
# - "SerialToSerialBLE_Secure"
boards:
- [ esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1 ]
nimble:
Expand All @@ -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

Expand Down
87 changes: 87 additions & 0 deletions examples/SerialToSerialBLE_Secure/SerialToSerialBLE_Secure.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <BLESerial.h>

// FOR ETL: Uncomment the following lines
// #include <Embedded_Template_Library.h>
// #include <etl/queue.h>
// #include <etl/circular_buffer.h>

BLESerial SerialBLE;
// If you are using older version of Arduino IDE, you may need to use
// empty template argument (<>), due to the old C++ compiler version (<=std=c++17).
// https://www.cppreference.com/w/cpp/language/ctad.html
//
// Uncomment the line below if you are using an older version of Arduino IDE
// BLESerial<> SerialBLE;

// FOR ETL: Uncomment one of the following lines
// BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
// OR
// BLESerial<etl::circular_buffer<uint8_t, 255>> SerialBLE;

uint32_t passKey;

Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This global passKey variable appears to be unused since the AppSecurityCallbacks class has its own private passKey member. Consider removing this unused global variable.

Suggested change
uint32_t passKey;

Copilot uses AI. Check for mistakes.
class AppSecurityCallbacks : public BLESecurityCallbacks {
public:
AppSecurityCallbacks() {
this->passKey = random(111111, 999999);
}

uint32_t onPassKeyRequest(){
ESP_LOGI(LOG_TAG, "PassKeyRequest");
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.

// 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);
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
}

bool onConfirmPIN(uint32_t pass_key){
ESP_LOGI(LOG_TAG, "The passkey YES/NO number: %d", pass_key);
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
vTaskDelay(5000);
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded 5000ms delay (5 seconds) is a magic number. Consider defining it as a named constant or adding a comment explaining why this specific delay is needed for the confirmation process.

Suggested change
vTaskDelay(5000);
vTaskDelay(CONFIRMATION_DELAY_MS);

Copilot uses AI. Check for mistakes.
return true;
}

bool onSecurityRequest(){
ESP_LOGI(LOG_TAG, "SecurityRequest");
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
return true;
}

void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
ESP_LOGI(LOG_TAG, "Starting BLE work!");
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
}

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();
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory allocated for BLESecurity is never freed, causing a memory leak. Consider using a smart pointer or ensuring proper cleanup, or making it a stack variable if the lifetime permits.

Copilot uses AI. Check for mistakes.
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());
}
}