diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index 9c41fd0d..59d47335 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -252,23 +252,19 @@ bool BLEConnection::requestPairing(void) // skip if already paired if ( _paired ) return true; - ble_gap_sec_params_t sec_param = Bluefruit.Pairing.getSecureParam(); + BLEPairing* secure = &Bluefruit.Pairing; // on-the-fly semaphore _pair_sem = xSemaphoreCreateBinary(); VERIFY(_pair_sem); - bond_keys_t ltkeys; - uint32_t err; + bond_keys_t ltkey; - if ( loadLongTermKey(<keys) ) + if ( loadLongTermKey(<key) ) { // We already bonded with this peer previously // Encrypt the connection using stored Longterm Key - err = sd_ble_gap_encrypt(_conn_hdl, <keys.peer_enc.master_id, <keys.peer_enc.enc_info); - PRINT_STATUS(err); - - if ( err == NRF_SUCCESS ) + if ( secure->_encrypt(_conn_hdl, <key) ) { xSemaphoreTake(_pair_sem, portMAX_DELAY); @@ -276,21 +272,17 @@ bool BLEConnection::requestPairing(void) // delete bonds while we did not --> let's remove the obsolete keyfile and retry if ( !_paired ) { - bond_remove_key(_role, <keys.peer_id.id_addr_info); + bond_remove_key(_role, <key.peer_id.id_addr_info); // Re-try with a fresh session - err = sd_ble_gap_authenticate(_conn_hdl, &sec_param ); - PRINT_STATUS(err); - + secure->_authenticate(_conn_hdl); xSemaphoreTake(_pair_sem, portMAX_DELAY); } } }else { // start a fresh new authentication process - err = sd_ble_gap_authenticate(_conn_hdl, &sec_param ); - PRINT_STATUS(err); - + secure->_authenticate(_conn_hdl); xSemaphoreTake(_pair_sem, portMAX_DELAY); } diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.cpp b/libraries/Bluefruit52Lib/src/BLEPairing.cpp index d348c1be..ed20438d 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.cpp +++ b/libraries/Bluefruit52Lib/src/BLEPairing.cpp @@ -195,6 +195,18 @@ void BLEPairing::setCompleteCallback(pair_complete_cb_t fp) _complete_cb = fp; } +bool BLEPairing::_authenticate(uint16_t conn_hdl) +{ + VERIFY_STATUS(sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false); + return true; +} + +bool BLEPairing::_encrypt(uint16_t conn_hdl, bond_keys_t const* ltkey) +{ + VERIFY_STATUS(sd_ble_gap_encrypt(conn_hdl, <key->peer_enc.master_id, <key->peer_enc.enc_info), false); + return true; +} + //--------------------------------------------------------------------+ /* First-time Pairing * diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.h b/libraries/Bluefruit52Lib/src/BLEPairing.h index 27b2e876..4701b5bd 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.h +++ b/libraries/Bluefruit52Lib/src/BLEPairing.h @@ -42,7 +42,7 @@ class BLEPairing bool begin(void); - // Static Passkey + // Use static Passkey (Legacy SC) bool setPIN(const char* pin); // resolve address with IRK to see if it matches @@ -60,6 +60,9 @@ class BLEPairing ble_gap_sec_params_t getSecureParam(void) { return _sec_param; } void _eventHandler(ble_evt_t* evt); + bool _authenticate(uint16_t conn_hdl); + bool _encrypt(uint16_t conn_hdl, bond_keys_t const* ltkey); + private: ble_gap_sec_params_t _sec_param;