From f0440b55ea2c365e24b4b12ce0d3e977334276c1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 18 Mar 2020 11:25:52 +0700 Subject: [PATCH] Pairing added secured callback, requestPairing() is non-blocking now. --- .../Bluefruit52Lib/src/BLEConnection.cpp | 46 +------------------ libraries/Bluefruit52Lib/src/BLEPairing.cpp | 18 ++++++++ libraries/Bluefruit52Lib/src/BLEPairing.h | 4 +- 3 files changed, 23 insertions(+), 45 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index f3f5702d..6daad956 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -56,9 +56,9 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e _wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize); _secured = false; + _bonded = false; _hvc_sem = NULL; _hvc_received = false; - _pair_sem = NULL; _ediv = 0xFFFF; } @@ -70,7 +70,6 @@ BLEConnection::~BLEConnection() //------------- on-the-fly data must be freed -------------// if (_hvc_sem ) vSemaphoreDelete(_hvc_sem ); - if (_pair_sem ) vSemaphoreDelete(_pair_sem); } uint16_t BLEConnection::handle (void) @@ -270,44 +269,7 @@ bool BLEConnection::requestPairing(void) // skip if already paired if ( _secured ) return true; - BLEPairing* secure = &Bluefruit.Pairing; - - // on-the-fly semaphore - _pair_sem = xSemaphoreCreateBinary(); - VERIFY(_pair_sem); - -// bond_keys_t ltkey; -// -// if ( loadBondKey(<key) ) // central only -// { -// // We already bonded with this peer previously -// // Encrypt the connection using stored Longterm Key -// if ( secure->_encrypt(_conn_hdl, <key) ) -// { -// xSemaphoreTake(_pair_sem, portMAX_DELAY); -// -// // Failed to pair using stored key, this happens when peer -// // delete bonds while we did not --> let's remove the obsolete keyfile and retry -// if ( !_paired ) -// { -// bond_remove_key(_role, <key.peer_id.id_addr_info); -// -// // Re-try with a fresh session -// secure->_authenticate(_conn_hdl); -// xSemaphoreTake(_pair_sem, portMAX_DELAY); -// } -// } -// }else - { - // start a fresh new authentication process - secure->_authenticate(_conn_hdl); - xSemaphoreTake(_pair_sem, portMAX_DELAY); - } - - vSemaphoreDelete(_pair_sem); - _pair_sem = NULL; - - return _secured; + return Bluefruit.Pairing._authenticate(_conn_hdl); } bool BLEConnection::waitForIndicateConfirm(void) @@ -335,9 +297,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt) case BLE_GAP_EVT_CONN_SEC_UPDATE: { - // Connection is secured, we have paired/bonded const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec; - LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv); // Connection is secured (paired) if encryption level > 1 if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) ) @@ -347,8 +307,6 @@ void BLEConnection::_eventHandler(ble_evt_t* evt) // Try to restore CCCD with bonded peer, if it doesn't exist (newly bonded), initialize it if ( !loadCccd() ) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); } - - if (_pair_sem) xSemaphoreGive(_pair_sem); } break; diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.cpp b/libraries/Bluefruit52Lib/src/BLEPairing.cpp index 43f48d39..be7711c9 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.cpp +++ b/libraries/Bluefruit52Lib/src/BLEPairing.cpp @@ -70,6 +70,7 @@ BLEPairing::BLEPairing(void) _sec_param = _sec_param_default; _passkey_cb = NULL; _complete_cb = NULL; + _secured_cb = NULL; } bool BLEPairing::begin(void) @@ -201,6 +202,11 @@ void BLEPairing::setCompleteCallback(pair_complete_cb_t fp) _complete_cb = fp; } +void BLEPairing::setSecuredCallback(pair_secured_cb_t fp) +{ + _secured_cb = fp; +} + bool BLEPairing::_authenticate(uint16_t conn_hdl) { VERIFY_STATUS(sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false); @@ -381,6 +387,18 @@ void BLEPairing::_eventHandler(ble_evt_t* evt) } break; + case BLE_GAP_EVT_CONN_SEC_UPDATE: + { + const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec; + LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv); + + if ( conn->secured() && _secured_cb ) + { + ada_callback(NULL, 0, _secured_cb, conn_hdl, conn_sec->sec_mode.sm, conn_sec->sec_mode.lv); + } + } + break; + default: break; } } diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.h b/libraries/Bluefruit52Lib/src/BLEPairing.h index 6a249f06..9c17f8b9 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.h +++ b/libraries/Bluefruit52Lib/src/BLEPairing.h @@ -37,6 +37,7 @@ class BLEPairing public: typedef bool (*pair_passkey_cb_t ) (uint16_t conn_hdl, uint8_t const passkey[6], bool match_request); typedef void (*pair_complete_cb_t) (uint16_t conn_hdl, uint8_t auth_status); + typedef void (*pair_secured_cb_t) (uint16_t conn_hdl, uint8_t sec_mode, uint8_t level); BLEPairing(void); @@ -57,13 +58,13 @@ class BLEPairing //------------- Callbacks -------------// bool setPasskeyCallback(pair_passkey_cb_t fp); void setCompleteCallback(pair_complete_cb_t fp); + void setSecuredCallback(pair_secured_cb_t fp); /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY * Although declare as public, it is meant to be invoked by internal * code. User should not call these directly *------------------------------------------------------------------*/ - ble_gap_sec_params_t getSecureParam(void) { return _sec_param; } void _eventHandler(ble_evt_t* evt); bool _authenticate(uint16_t conn_hdl); @@ -81,6 +82,7 @@ class BLEPairing pair_passkey_cb_t _passkey_cb; pair_complete_cb_t _complete_cb; + pair_secured_cb_t _secured_cb; }; #endif /* BLEPAIRING_H_ */