This commit is contained in:
hathach committed 2020-03-16 22:44:24 +07:00
1 parent b487db9603
commit 7b432602aa
3 files changed
+23 -16

No files matched your search

+7 -15
View File
@@ -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(&ltkeys) )
if ( loadLongTermKey(&ltkey) )
{
// We already bonded with this peer previously
// Encrypt the connection using stored Longterm Key
err = sd_ble_gap_encrypt(_conn_hdl, &ltkeys.peer_enc.master_id, &ltkeys.peer_enc.enc_info);
PRINT_STATUS(err);
if ( err == NRF_SUCCESS )
if ( secure->_encrypt(_conn_hdl, &ltkey) )
{
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, &ltkeys.peer_id.id_addr_info);
bond_remove_key(_role, &ltkey.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);
}
@@ -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, &ltkey->peer_enc.master_id, &ltkey->peer_enc.enc_info), false);
return true;
}
//--------------------------------------------------------------------+
/* First-time Pairing
*
+4 -1
View File
@@ -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;