From af8bd2beeeac8202e0212880f3a3f94d3e46d2e8 Mon Sep 17 00:00:00 2001 From: Richard Li Date: Mon, 14 Dec 2020 14:19:22 +0800 Subject: [PATCH 1/4] Added callbacks to BluetoothSerial for SSP authentication. --- .../BluetoothSerial/src/BluetoothSerial.cpp | 33 ++++++++++++++++++- .../BluetoothSerial/src/BluetoothSerial.h | 6 ++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index a52e48d8615..051f9cc715f 100755 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -51,6 +51,9 @@ static EventGroupHandle_t _spp_event_group = NULL; static boolean secondConnectionAttempt; static esp_spp_cb_t * custom_spp_callback = NULL; static BluetoothSerialDataCb custom_data_callback = NULL; +static esp_bd_addr_t current_bd_addr; +static ConfirmRequestCb confirm_request_callback = NULL; +static AuthCompleteCb auth_complete_callback = NULL; #define INQ_LEN 0x10 #define INQ_NUM_RSPS 20 @@ -398,8 +401,14 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa case ESP_BT_GAP_AUTH_CMPL_EVT: if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) { log_v("authentication success: %s", param->auth_cmpl.device_name); + if (auth_complete_callback) { + auth_complete_callback(true); + } } else { log_e("authentication failed, status:%d", param->auth_cmpl.stat); + if (auth_complete_callback) { + auth_complete_callback(false); + } } break; @@ -421,7 +430,13 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa case ESP_BT_GAP_CFM_REQ_EVT: log_i("ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val); - esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true); + if (confirm_request_callback) { + memcpy(current_bd_addr, param->cfm_req.bda, sizeof(esp_bd_addr_t)); + confirm_request_callback(param->cfm_req.num_val); + } + else { + esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true); + } break; case ESP_BT_GAP_KEY_NOTIF_EVT: @@ -672,6 +687,22 @@ void BluetoothSerial::end() _stop_bt(); } +void BluetoothSerial::onConfirmRequest(ConfirmRequestCb cb) +{ + confirm_request_callback = cb; +} + +void BluetoothSerial::onAuthComplete(AuthCompleteCb cb) +{ + auth_complete_callback = cb; +} + +void BluetoothSerial::confirmReply(boolean confirm) +{ + esp_bt_gap_ssp_confirm_reply(current_bd_addr, confirm); +} + + esp_err_t BluetoothSerial::register_callback(esp_spp_cb_t * callback) { custom_spp_callback = callback; diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.h b/libraries/BluetoothSerial/src/BluetoothSerial.h index 6ccb9b49e2b..04dbcb17dc1 100755 --- a/libraries/BluetoothSerial/src/BluetoothSerial.h +++ b/libraries/BluetoothSerial/src/BluetoothSerial.h @@ -25,6 +25,8 @@ #include typedef std::function BluetoothSerialDataCb; +typedef std::function ConfirmRequestCb; +typedef std::function AuthCompleteCb; class BluetoothSerial: public Stream { @@ -44,6 +46,10 @@ class BluetoothSerial: public Stream void end(void); void onData(BluetoothSerialDataCb cb); esp_err_t register_callback(esp_spp_cb_t * callback); + + void onConfirmRequest(ConfirmRequestCb cb); + void onAuthComplete(AuthCompleteCb cb); + void confirmReply(boolean confirm); void enableSSP(); bool setPin(const char *pin); From 11afa40496c1f12132f864917f4f90c10fb6eb34 Mon Sep 17 00:00:00 2001 From: Richard Li Date: Mon, 14 Dec 2020 22:43:29 +0800 Subject: [PATCH 2/4] Fixed a problem in during gap callback registering and added example. --- .../SerialToSerialBT_SSP_pairing.ino | 75 +++++++++++++++++++ .../BluetoothSerial/src/BluetoothSerial.cpp | 4 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino diff --git a/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino b/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino new file mode 100644 index 00000000000..523c4730977 --- /dev/null +++ b/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino @@ -0,0 +1,75 @@ +//This example code is in the Public Domain (or CC0 licensed, at your option.) +//By Evandro Copercini - 2018 +// +//This example creates a bridge between Serial and Classical Bluetooth (SPP) +//and also demonstrate that SerialBT have the same functionalities of a normal Serial + +#include "BluetoothSerial.h" + +#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) +#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it +#endif + +BluetoothSerial SerialBT; +boolean confirmRequestPending = true; + +void BTConfirmRequestCallback(uint32_t numVal) +{ + confirmRequestPending = true; + Serial.println(numVal); +} + +void BTAuthCompleteCallback(boolean success) +{ + confirmRequestPending = false; + if (success) + { + Serial.println("Pairing success!!"); + } + else + { + Serial.println("Pairing failed, rejected by user!!"); + } +} + + +void setup() +{ + Serial.begin(115200); + SerialBT.enableSSP(); + SerialBT.onConfirmRequest(BTConfirmRequestCallback); + SerialBT.onAuthComplete(BTAuthCompleteCallback); + SerialBT.begin("ESP32test"); //Bluetooth device name + Serial.println("The device started, now you can pair it with bluetooth!"); +} + +void loop() +{ + if (confirmRequestPending) + { + if (Serial.available()) + { + int dat = Serial.read(); + if (dat == 'Y' || dat == 'y') + { + SerialBT.confirmReply(true); + } + else + { + SerialBT.confirmReply(false); + } + } + } + else + { + if (Serial.available()) + { + SerialBT.write(Serial.read()); + } + if (SerialBT.available()) + { + Serial.write(SerialBT.read()); + } + delay(20); + } +} diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index 051f9cc715f..c9c494a4aee 100755 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -515,7 +515,9 @@ static bool _init_bt(const char *deviceName) } } - if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) { + // Why only master need this? Slave need this during pairing as well +// if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) { + if (esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) { log_e("gap register failed"); return false; } From 6dd73b062ccd51ea41f2d7856fe610e9993d0a46 Mon Sep 17 00:00:00 2001 From: Richard Li Date: Mon, 14 Dec 2020 22:50:31 +0800 Subject: [PATCH 3/4] Updated example comments. --- .../SerialToSerialBT_SSP_pairing.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino b/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino index 523c4730977..f9dfb09b4b6 100644 --- a/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino +++ b/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino @@ -1,7 +1,7 @@ //This example code is in the Public Domain (or CC0 licensed, at your option.) -//By Evandro Copercini - 2018 +//By Richard Li 2020 // -//This example creates a bridge between Serial and Classical Bluetooth (SPP) +//This example creates a bridge between Serial and Classical Bluetooth (SPP with authentication) //and also demonstrate that SerialBT have the same functionalities of a normal Serial #include "BluetoothSerial.h" From da2862f2d52d190b9633deca1d61f4fa6740b144 Mon Sep 17 00:00:00 2001 From: Richard Li Date: Mon, 14 Dec 2020 22:51:38 +0800 Subject: [PATCH 4/4] Updated comments. --- .../SerialToSerialBT_SSP_pairing.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino b/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino index f9dfb09b4b6..8791b6c7a02 100644 --- a/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino +++ b/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP_pairing/SerialToSerialBT_SSP_pairing.ino @@ -1,5 +1,5 @@ //This example code is in the Public Domain (or CC0 licensed, at your option.) -//By Richard Li 2020 +//By Richard Li - 2020 // //This example creates a bridge between Serial and Classical Bluetooth (SPP with authentication) //and also demonstrate that SerialBT have the same functionalities of a normal Serial