Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 586e497

Browse files
fix(ble): Fix typos and security start
1 parent fe67c72 commit 586e497

File tree

3 files changed

+72
-50
lines changed

3 files changed

+72
-50
lines changed

‎libraries/BLE/examples/Client_secure_static_passkey/Client_secure_static_passkey.ino

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
Note that ESP32 uses Bluedroid by default and the other SoCs use NimBLE.
1111
Bluedroid initiates security on-connect, while NimBLE initiates security on-demand.
12-
This means that in NimBLE you can read the unsecure characteristic without entering
12+
This means that in NimBLE you can read the insecure characteristic without entering
1313
the passkey. This is not possible in Bluedroid.
1414
1515
Also, the SoC stores the authentication info in the NVS memory. After a successful
@@ -26,7 +26,7 @@
2626
// The remote service we wish to connect to.
2727
static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
2828
// The characteristics of the remote service we are interested in.
29-
static BLEUUID unsecureCharUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");
29+
static BLEUUID insecureCharUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");
3030
static BLEUUID secureCharUUID("ff1d2614-e2d6-4c87-9154-6625d39ca7f8");
3131

3232
// This must match the server's passkey
@@ -35,7 +35,7 @@ static BLEUUID secureCharUUID("ff1d2614-e2d6-4c87-9154-6625d39ca7f8");
3535
static boolean doConnect = false;
3636
static boolean connected = false;
3737
static boolean doScan = false;
38-
static BLERemoteCharacteristic *pRemoteUnsecureCharacteristic;
38+
static BLERemoteCharacteristic *pRemoteInsecureCharacteristic;
3939
static BLERemoteCharacteristic *pRemoteSecureCharacteristic;
4040
static BLEAdvertisedDevice *myDevice;
4141

@@ -87,15 +87,15 @@ bool connectToServer() {
8787
}
8888
Serial.println(" - Found our service");
8989

90-
// Obtain a reference to the unsecure characteristic
91-
pRemoteUnsecureCharacteristic = pRemoteService->getCharacteristic(unsecureCharUUID);
92-
if (pRemoteUnsecureCharacteristic == nullptr) {
93-
Serial.print("Failed to find unsecure characteristic UUID: ");
94-
Serial.println(unsecureCharUUID.toString().c_str());
90+
// Obtain a reference to the insecure characteristic
91+
pRemoteInsecureCharacteristic = pRemoteService->getCharacteristic(insecureCharUUID);
92+
if (pRemoteInsecureCharacteristic == nullptr) {
93+
Serial.print("Failed to find insecure characteristic UUID: ");
94+
Serial.println(insecureCharUUID.toString().c_str());
9595
pClient->disconnect();
9696
return false;
9797
}
98-
Serial.println(" - Found unsecure characteristic");
98+
Serial.println(" - Found insecure characteristic");
9999

100100
// Obtain a reference to the secure characteristic
101101
pRemoteSecureCharacteristic = pRemoteService->getCharacteristic(secureCharUUID);
@@ -107,10 +107,10 @@ bool connectToServer() {
107107
}
108108
Serial.println(" - Found secure characteristic");
109109

110-
// Read the value of the unsecure characteristic (should work without authentication)
111-
if (pRemoteUnsecureCharacteristic->canRead()) {
112-
String value = pRemoteUnsecureCharacteristic->readValue();
113-
Serial.print("Unsecure characteristic value: ");
110+
// Read the value of the insecure characteristic (should work without authentication)
111+
if (pRemoteInsecureCharacteristic->canRead()) {
112+
String value = pRemoteInsecureCharacteristic->readValue();
113+
Serial.print("Insecure characteristic value: ");
114114
Serial.println(value.c_str());
115115
}
116116

@@ -127,9 +127,9 @@ bool connectToServer() {
127127
}
128128

129129
// Register for notifications on both characteristics if they support it
130-
if (pRemoteUnsecureCharacteristic->canNotify()) {
131-
pRemoteUnsecureCharacteristic->registerForNotify(notifyCallback);
132-
Serial.println(" - Registered for unsecure characteristic notifications");
130+
if (pRemoteInsecureCharacteristic->canNotify()) {
131+
pRemoteInsecureCharacteristic->registerForNotify(notifyCallback);
132+
Serial.println(" - Registered for insecure characteristic notifications");
133133
}
134134

135135
if (pRemoteSecureCharacteristic->canNotify()) {
@@ -173,7 +173,7 @@ void setup() {
173173
BLESecurity *pSecurity = new BLESecurity();
174174

175175
// Set security parameters
176-
// Deafult parameters:
176+
// Default parameters:
177177
// - IO capability is set to NONE
178178
// - Initiator and responder key distribution flags are set to both encryption and identity keys.
179179
// - Passkey is set to BLE_SM_DEFAULT_PASSKEY (123456). It will warn if you don't change it.
@@ -213,11 +213,11 @@ void loop() {
213213

214214
// If we are connected to a peer BLE Server, demonstrate secure communication
215215
if (connected) {
216-
// Write to the unsecure characteristic
217-
String unsecureValue = "Client time: " + String(millis() / 1000);
218-
if (pRemoteUnsecureCharacteristic->canWrite()) {
219-
pRemoteUnsecureCharacteristic->writeValue(unsecureValue.c_str(), unsecureValue.length());
220-
Serial.println("Wrote to unsecure characteristic: " + unsecureValue);
216+
// Write to the insecure characteristic
217+
String insecureValue = "Client time: " + String(millis() / 1000);
218+
if (pRemoteInsecureCharacteristic->canWrite()) {
219+
pRemoteInsecureCharacteristic->writeValue(insecureValue.c_str(), insecureValue.length());
220+
Serial.println("Wrote to insecure characteristic: " + insecureValue);
221221
}
222222

223223
// Write to the secure characteristic

‎libraries/BLE/examples/Server_secure_static_passkey/Server_secure_static_passkey.ino

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
IO capability using a static passkey.
66
The server will accept connections from devices that have the same passkey set.
77
The example passkey is set to 123456.
8-
The server will create a service and a secure and an unsecure characteristic
8+
The server will create a service and a secure and an insecure characteristic
99
to be used as example.
1010
1111
This server is designed to be used with the Client_secure_static_passkey example.
1212
1313
Note that ESP32 uses Bluedroid by default and the other SoCs use NimBLE.
1414
Bluedroid initiates security on-connect, while NimBLE initiates security on-demand.
15-
This means that in NimBLE you can read the unsecure characteristic without entering
15+
This means that in NimBLE you can read the insecure characteristic without entering
1616
the passkey. This is not possible in Bluedroid.
1717
1818
Also, the SoC stores the authentication info in the NVS memory. After a successful
@@ -33,7 +33,7 @@
3333
// https://www.uuidgenerator.net/
3434

3535
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
36-
#define UNSECURE_CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
36+
#define Insecure_CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
3737
#define SECURE_CHARACTERISTIC_UUID "ff1d2614-e2d6-4c87-9154-6625d39ca7f8"
3838

3939
// This is an example passkey. You should use a different or random passkey.
@@ -51,7 +51,7 @@ void setup() {
5151
BLESecurity *pSecurity = new BLESecurity();
5252

5353
// Set security parameters
54-
// Deafult parameters:
54+
// Default parameters:
5555
// - IO capability is set to NONE
5656
// - Initiator and responder key distribution flags are set to both encryption and identity keys.
5757
// - Passkey is set to BLE_SM_DEFAULT_PASSKEY (123456). It will warn if you don't change it.
@@ -71,8 +71,8 @@ void setup() {
7171

7272
BLEService *pService = pServer->createService(SERVICE_UUID);
7373

74-
uint32_t unsecure_properties = BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE;
75-
uint32_t secure_properties = unsecure_properties;
74+
uint32_t insecure_properties = BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE;
75+
uint32_t secure_properties = insecure_properties;
7676

7777
// NimBLE uses properties to secure characteristics.
7878
// These special permission properties are not supported by Bluedroid and will be ignored.
@@ -81,21 +81,21 @@ void setup() {
8181
secure_properties |= BLECharacteristic::PROPERTY_READ_ENC | BLECharacteristic::PROPERTY_WRITE_ENC;
8282

8383
BLECharacteristic *pSecureCharacteristic = pService->createCharacteristic(SECURE_CHARACTERISTIC_UUID, secure_properties);
84-
BLECharacteristic *pUnsecureCharacteristic = pService->createCharacteristic(UNSECURE_CHARACTERISTIC_UUID, unsecure_properties);
84+
BLECharacteristic *pInsecureCharacteristic = pService->createCharacteristic(Insecure_CHARACTERISTIC_UUID, insecure_properties);
8585

8686
// Bluedroid uses permissions to secure characteristics.
8787
// This is the same as using the properties above.
8888
// NimBLE does not use permissions and will ignore these calls.
8989
// This can be removed if only using NimBLE (any SoC except ESP32).
9090
pSecureCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
91-
pUnsecureCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
91+
pInsecureCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
9292

9393
// Set value for secure characteristic
9494
pSecureCharacteristic->setValue("Secure Hello World!");
9595

96-
// Set value for unsecure characteristic
96+
// Set value for insecure characteristic
9797
// When using NimBLE you will be able to read this characteristic without entering the passkey.
98-
pUnsecureCharacteristic->setValue("Unsecure Hello World!");
98+
pInsecureCharacteristic->setValue("Insecure Hello World!");
9999

100100
pService->start();
101101
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();

‎libraries/BLE/src/BLESecurity.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ uint32_t BLESecurity::m_passkey = BLE_SM_DEFAULT_PASSKEY;
5959

6060
#if defined(CONFIG_BLUEDROID_ENABLED)
6161
uint8_t BLESecurity::m_keySize = 16;
62-
esp_ble_sec_act_t BLESecurity::m_securityLevel = (esp_ble_sec_act_t)0;
62+
esp_ble_sec_act_t BLESecurity::m_securityLevel;
6363
#endif
6464

6565
/***************************************************************************
@@ -216,7 +216,7 @@ void BLESecurity::setAuthenticationMode(bool bonding, bool mitm, bool sc) {
216216
// It should return the passkey that the peer device is showing on its output.
217217
// This might not be called if the client has a static passkey set.
218218
uint32_t BLESecurityCallbacks::onPassKeyRequest() {
219-
Serial.println("BLESecurityCallbacks: *ATTENTION* Using unsecure onPassKeyRequest.");
219+
Serial.println("BLESecurityCallbacks: *ATTENTION* Using insecure onPassKeyRequest.");
220220
Serial.println("BLESecurityCallbacks: *ATTENTION* Please implement onPassKeyRequest with a suitable passkey in your BLESecurityCallbacks class");
221221
Serial.printf("BLESecurityCallbacks: Default passkey: %06d\n", BLE_SM_DEFAULT_PASSKEY);
222222
return BLE_SM_DEFAULT_PASSKEY;
@@ -239,7 +239,7 @@ bool BLESecurityCallbacks::onSecurityRequest() {
239239
// This callback is called by both devices when both have the DisplayYesNo capability.
240240
// It should return true if both devices display the same passkey.
241241
bool BLESecurityCallbacks::onConfirmPIN(uint32_t pin) {
242-
Serial.println("BLESecurityCallbacks: *ATTENTION* Using unsecure onConfirmPIN. It will accept any passkey.");
242+
Serial.println("BLESecurityCallbacks: *ATTENTION* Using insecure onConfirmPIN. It will accept any passkey.");
243243
Serial.println("BLESecurityCallbacks: *ATTENTION* Please implement onConfirmPIN with a suitable confirmation logic in your BLESecurityCallbacks class");
244244
return true;
245245
}
@@ -251,7 +251,7 @@ bool BLESecurityCallbacks::onConfirmPIN(uint32_t pin) {
251251
// otherwise it is requesting to write.
252252
// It should return true if the authorization is granted, false otherwise.
253253
bool BLESecurityCallbacks::onAuthorizationRequest(uint16_t connHandle, uint16_t attrHandle, bool isRead) {
254-
Serial.println("BLESecurityCallbacks: *ATTENTION* Using unsecure onAuthorizationRequest. It will accept any authorization request.");
254+
Serial.println("BLESecurityCallbacks: *ATTENTION* Using insecure onAuthorizationRequest. It will accept any authorization request.");
255255
Serial.println(
256256
"BLESecurityCallbacks: *ATTENTION* Please implement onAuthorizationRequest with a suitable authorization logic in your BLESecurityCallbacks class"
257257
);
@@ -272,17 +272,28 @@ bool BLESecurity::startSecurity(esp_bd_addr_t bd_addr, int *rcPtr) {
272272
#ifdef CONFIG_BLE_SMP_ENABLE
273273
if (m_securityStarted) {
274274
log_w("Security already started for bd_addr=%s", BLEAddress(bd_addr).toString().c_str());
275+
if (rcPtr) {
276+
*rcPtr = ESP_OK;
277+
}
275278
return true;
276279
}
277280

278-
int rc = esp_ble_set_encryption(bd_addr, m_securityLevel);
279-
if (rc != ESP_OK) {
280-
log_e("esp_ble_set_encryption: rc=%d %s", rc, GeneralUtils::errorToString(rc));
281-
}
282-
if (rcPtr) {
283-
*rcPtr = rc;
281+
if (m_securityEnabled) {
282+
int rc = esp_ble_set_encryption(bd_addr, m_securityLevel);
283+
if (rc != ESP_OK) {
284+
log_e("esp_ble_set_encryption: rc=%d %s", rc, GeneralUtils::errorToString(rc));
285+
}
286+
if (rcPtr) {
287+
*rcPtr = rc;
288+
}
289+
m_securityStarted = (rc == ESP_OK);
290+
} else {
291+
log_e("Security is not enabled. Can't start security.");
292+
if (rcPtr) {
293+
*rcPtr = ESP_FAIL;
294+
}
295+
return false;
284296
}
285-
m_securityStarted = (rc == ESP_OK);
286297
return m_securityStarted;
287298
#else
288299
log_e("Bluedroid SMP is not enabled. Can't start security.");
@@ -324,17 +335,28 @@ void BLESecurityCallbacks::onAuthenticationComplete(esp_ble_auth_cmpl_t param) {
324335
bool BLESecurity::startSecurity(uint16_t connHandle, int *rcPtr) {
325336
if (m_securityStarted) {
326337
log_w("Security already started for connHandle=%d", connHandle);
338+
if (rcPtr) {
339+
*rcPtr = 0;
340+
}
327341
return true;
328342
}
329343

330-
int rc = ble_gap_security_initiate(connHandle);
331-
if (rc != 0) {
332-
log_e("ble_gap_security_initiate: rc=%d %s", rc, BLEUtils::returnCodeToString(rc));
333-
}
334-
if (rcPtr) {
335-
*rcPtr = rc;
344+
if (m_securityEnabled) {
345+
int rc = ble_gap_security_initiate(connHandle);
346+
if (rc != 0) {
347+
log_e("ble_gap_security_initiate: rc=%d %s", rc, BLEUtils::returnCodeToString(rc));
348+
}
349+
if (rcPtr) {
350+
*rcPtr = rc;
351+
}
352+
m_securityStarted = (rc == 0 || rc == BLE_HS_EALREADY);
353+
} else {
354+
log_e("Security is not enabled. Can't start security.");
355+
if (rcPtr) {
356+
*rcPtr = ESP_FAIL;
357+
}
358+
return false;
336359
}
337-
m_securityStarted = (rc == 0 || rc == BLE_HS_EALREADY);
338360
return m_securityStarted;
339361
}
340362

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /