Skip to content

Commit 0be3625

Browse files
authored
🔀 Merge pull request #11 from senseshift/feature/ble-security
Add BLESecurity example
2 parents 1780ced + 6df7922 commit 0be3625

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

.github/workflows/platformio-ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
- "SerialToSerialBLE"
2424
# - "SerialToSerialBLE_TransparentUART"
2525
- "SerialToSerialBLE_TransparentUART-NimBLE"
26+
# - "SerialToSerialBLE_Secure"
2627
boards:
2728
- [ esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1 ]
2829
nimble:
@@ -47,6 +48,11 @@ jobs:
4748
os: ubuntu-latest
4849
boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1]
4950

51+
- example: "SerialToSerialBLE_Secure"
52+
nimble: false
53+
os: ubuntu-latest
54+
boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1]
55+
5056
steps:
5157
- uses: actions/checkout@v4
5258

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <BLESerial.h>
2+
3+
// FOR ETL: Uncomment the following lines
4+
// #include <Embedded_Template_Library.h>
5+
// #include <etl/queue.h>
6+
// #include <etl/circular_buffer.h>
7+
8+
BLESerial SerialBLE;
9+
// If you are using an older version of Arduino IDE or C++ compiler, you may need to use
10+
// an empty template argument (<>), as Class Template Argument Deduction (CTAD) is not
11+
// supported in C++ versions older than C++17. For more details, see:
12+
// https://www.cppreference.com/w/cpp/language/ctad.html
13+
//
14+
// Uncomment the line below if you are using an older version of Arduino IDE/C++ compiler
15+
// BLESerial<> SerialBLE;
16+
17+
// FOR ETL: Uncomment one of the following lines
18+
// BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
19+
// OR
20+
// BLESerial<etl::circular_buffer<uint8_t, 255>> SerialBLE;
21+
22+
class AppSecurityCallbacks : public BLESecurityCallbacks {
23+
public:
24+
AppSecurityCallbacks() {
25+
this->passKey = random(111111, 999999);
26+
}
27+
28+
uint32_t onPassKeyRequest(){
29+
ESP_LOGI(LOG_TAG, "PassKeyRequest");
30+
31+
// Generate a random passkey
32+
this->passKey = random(111111, 999999);
33+
34+
return this->passKey;
35+
}
36+
37+
void onPassKeyNotify(uint32_t pass_key){
38+
ESP_LOGI(LOG_TAG, "The passkey Notify number: %d", pass_key);
39+
}
40+
41+
bool onConfirmPIN(uint32_t pass_key){
42+
ESP_LOGI(LOG_TAG, "The passkey YES/NO number: %d", pass_key);
43+
vTaskDelay(5000);
44+
return true;
45+
}
46+
47+
bool onSecurityRequest(){
48+
ESP_LOGI(LOG_TAG, "SecurityRequest");
49+
return true;
50+
}
51+
52+
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
53+
ESP_LOGI(LOG_TAG, "Starting BLE work!");
54+
}
55+
56+
private:
57+
uint32_t passKey;
58+
};
59+
60+
void setup() {
61+
BLEDevice::init("ESP32-BLE-Slave");
62+
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
63+
BLEDevice::setSecurityCallbacks(new AppSecurityCallbacks());
64+
65+
BLEServer* pServer = BLEDevice::createServer();
66+
67+
BLESecurity *pSecurity = new BLESecurity();
68+
pSecurity->setKeySize();
69+
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
70+
pSecurity->setCapability(ESP_IO_CAP_IO);
71+
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
72+
73+
Serial.begin(9600);
74+
SerialBLE.begin(pServer);
75+
}
76+
77+
void loop() {
78+
if (Serial.available()) {
79+
SerialBLE.write(Serial.read());
80+
SerialBLE.flush();
81+
}
82+
83+
if (SerialBLE.available()) {
84+
Serial.write(SerialBLE.read());
85+
}
86+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
33
"name": "Serial_BLE",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"description": "Customizable Arduino and ESP32 BLE Serial library, compliant with Nordic UART Service and others.",
66
"keywords": [
77
"serial",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Serial_BLE
2-
version=1.2.0
2+
version=1.2.1
33
author=Leonid Meleshin <[email protected]>
44
maintainer=Leonid Meleshin <[email protected]>
55
sentence=Customizable BLE Serial (UART) library.

0 commit comments

Comments
 (0)