diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index ca2b900b..f88449be 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -171,15 +171,19 @@ bool BLEGap::requestPairing(uint16_t conn_hdl) // skip if already paired if ( peer->paired ) return true; - // Check to see if we did bonded with current prph + uint16_t cntr_ediv = 0xFFFF; + if ( peer->role == BLE_GAP_ROLE_CENTRAL ) { + // Check to see if we did bonded with current prph previously bond_data_t bdata; if ( bond_find_cntr(&peer->addr, &bdata) ) { - LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, bdata.peer_enc.master_id.ediv); + cntr_ediv = bdata.peer_enc.master_id.ediv; + LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, cntr_ediv); VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bdata.peer_enc.master_id, &bdata.peer_enc.enc_info), false); + }else { VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false); @@ -194,6 +198,18 @@ bool BLEGap::requestPairing(uint16_t conn_hdl) xSemaphoreTake(peer->pair_sem, portMAX_DELAY); + // Failed to pair using centra stored keys, this happens when + // Prph delete bonds while we did not --> let's remove the obsolete keyfile and move on + if ( !peer->paired && (cntr_ediv != 0xffff) ) + { + bond_remove_key(BLE_GAP_ROLE_CENTRAL, cntr_ediv); + + // Re-try with a fresh session + VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false); + + xSemaphoreTake(peer->pair_sem, portMAX_DELAY); + } + vSemaphoreDelete(peer->pair_sem); peer->pair_sem = NULL; diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.cpp b/libraries/Bluefruit52Lib/src/utility/bonding.cpp index 6d3bf5dd..81bcf143 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.cpp +++ b/libraries/Bluefruit52Lib/src/utility/bonding.cpp @@ -47,6 +47,11 @@ #define printBondDir(role) #endif +static void get_fname(char* fname, uint8_t role, uint16_t ediv) +{ + sprintf(fname, ( role == BLE_GAP_ROLE_PERIPH ) ? BOND_FNAME_PRPH : BOND_FNAME_CNTR, ediv); +} + /*------------------------------------------------------------------*/ /* Saving Bond Data to Nffs in following layout * - _bond_data 80 bytes @@ -69,13 +74,7 @@ void bond_init(void) static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata) { char filename[BOND_FNAME_LEN]; - if ( role == BLE_GAP_ROLE_PERIPH ) - { - sprintf(filename, BOND_FNAME_PRPH, bdata->own_enc.master_id.ediv); - }else - { - sprintf(filename, BOND_FNAME_CNTR, bdata->peer_enc.master_id.ediv); - } + get_fname(filename, role, role == BLE_GAP_ROLE_PERIPH ? bdata->own_enc.master_id.ediv : bdata->peer_enc.master_id.ediv); char devname[CFG_MAX_DEVNAME_LEN] = { 0 }; Bluefruit.Gap.getPeerName(conn_hdl, devname, CFG_MAX_DEVNAME_LEN); @@ -100,13 +99,8 @@ static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bda } file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN); - file.close(); - PRINT_HEX(bdata->own_enc.master_id.ediv); - PRINT_HEX(bdata->peer_enc.master_id.ediv); - - if (result) { LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename); @@ -132,13 +126,7 @@ void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata) bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata) { char filename[BOND_FNAME_LEN]; - if ( role == BLE_GAP_ROLE_PERIPH ) - { - sprintf(filename, BOND_FNAME_PRPH, ediv); - }else - { - sprintf(filename, BOND_FNAME_CNTR, ediv); - } + get_fname(filename, role, ediv); bool result = (Nffs.readFile(filename, bdata, sizeof(bond_data_t)) > 0); @@ -169,13 +157,7 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv) { // save to file char filename[BOND_FNAME_LEN]; - if ( role == BLE_GAP_ROLE_PERIPH ) - { - sprintf(filename, BOND_FNAME_PRPH, ediv); - }else - { - sprintf(filename, BOND_FNAME_CNTR, ediv); - } + get_fname(filename, role, ediv); if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) ) { @@ -205,13 +187,7 @@ bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv) bool loaded = false; char filename[BOND_FNAME_LEN]; - if ( role == BLE_GAP_ROLE_PERIPH ) - { - sprintf(filename, BOND_FNAME_PRPH, ediv); - }else - { - sprintf(filename, BOND_FNAME_CNTR, ediv); - } + get_fname(filename, role, ediv); NffsFile file(filename, FS_ACCESS_READ); @@ -353,3 +329,11 @@ void bond_clear_all(void) (void) Nffs.mkdir_p(BOND_DIR_PRPH); (void) Nffs.mkdir_p(BOND_DIR_CNTR); } + +void bond_remove_key(uint8_t role, uint16_t ediv) +{ + char filename[BOND_FNAME_LEN]; + get_fname(filename, role, ediv); + + Nffs.remove(filename); +} diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.h b/libraries/Bluefruit52Lib/src/utility/bonding.h index 1e2fae71..f6740312 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.h +++ b/libraries/Bluefruit52Lib/src/utility/bonding.h @@ -61,18 +61,20 @@ enum BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN }; -void bond_init(void); -void bond_clear_prph(void); -void bond_clear_cntr(void); -void bond_clear_all(void); +void bond_init(void); +void bond_clear_prph(void); +void bond_clear_cntr(void); +void bond_clear_all(void); -void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata); -bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata); +void bond_remove_key(uint8_t role, uint16_t ediv); -void bond_save_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv); -bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv); +void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata); +bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata); -void bond_print_list(uint8_t role); +void bond_save_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv); +bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv); + +void bond_print_list(uint8_t role); bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata);