Skip to content

Commit 7cc1307

Browse files
committed
central auto removes failed bond keys
1 parent d0e5946 commit 7cc1307

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

libraries/Bluefruit52Lib/src/BLECentral.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ void BLECentral::_eventHandler(ble_evt_t* evt)
142142
case BLE_GAP_EVT_CONNECTED:
143143
{
144144
// Try to secure connection if we bonded previously
145-
static bond_keys_t ltkey;
146-
if ( conn->loadLongTermKey(&ltkey) )
145+
bond_keys_t ltkey;
146+
if ( conn->loadBondKey(&ltkey) )
147147
{
148148
BLEPairing* secure = &Bluefruit.Pairing;
149149
secure->_encrypt(conn_hdl, &ltkey);
@@ -154,6 +154,15 @@ void BLECentral::_eventHandler(ble_evt_t* evt)
154154
case BLE_GAP_EVT_DISCONNECTED:
155155
break;
156156

157+
case BLE_GAP_EVT_CONN_SEC_UPDATE:
158+
if ( conn->bonded() && !conn->secured() )
159+
{
160+
// Bonded but failed to secure connection
161+
// Peer must have removed LTKey, we should remove ours as well
162+
conn->removeBondKey();
163+
}
164+
break;
165+
157166
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
158167
{
159168
// Peripheral request to change connection parameter

libraries/Bluefruit52Lib/src/BLEConnection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ bool BLEConnection::loadBondKey(bond_keys_t* ltkey)
258258
return true;
259259
}
260260

261+
bool BLEConnection::removeBondKey(void)
262+
{
263+
VERIFY(_bonded);
264+
bond_remove_key(_role, &_bond_id_addr);
265+
return true;
266+
}
267+
261268
bool BLEConnection::requestPairing(void)
262269
{
263270
// skip if already paired

libraries/Bluefruit52Lib/src/utility/bonding.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,6 @@ void bond_remove_key(uint8_t role, ble_gap_addr_t const* id_addr)
356356
get_fname(filename, role, id_addr->addr);
357357

358358
InternalFS.remove(filename);
359+
360+
BOND_LOG("Removed keys from file %s", filename);
359361
}

0 commit comments

Comments
 (0)