Skip to content

Commit 231f862

Browse files
committed
enhance bonding process
- auto remove obsolete key on central and re-try with a fresh session.
1 parent f3136ab commit 231f862

File tree

3 files changed

+46
-44
lines changed

3 files changed

+46
-44
lines changed

libraries/Bluefruit52Lib/src/BLEGap.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,19 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
171171
// skip if already paired
172172
if ( peer->paired ) return true;
173173

174-
// Check to see if we did bonded with current prph
174+
uint16_t cntr_ediv = 0xFFFF;
175+
175176
if ( peer->role == BLE_GAP_ROLE_CENTRAL )
176177
{
178+
// Check to see if we did bonded with current prph previously
177179
bond_data_t bdata;
178180

179181
if ( bond_find_cntr(&peer->addr, &bdata) )
180182
{
181-
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, bdata.peer_enc.master_id.ediv);
183+
cntr_ediv = bdata.peer_enc.master_id.ediv;
184+
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, cntr_ediv);
182185
VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bdata.peer_enc.master_id, &bdata.peer_enc.enc_info), false);
186+
183187
}else
184188
{
185189
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
@@ -194,6 +198,18 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
194198

195199
xSemaphoreTake(peer->pair_sem, portMAX_DELAY);
196200

201+
// Failed to pair using centra stored keys, this happens when
202+
// Prph delete bonds while we did not --> let's remove the obsolete keyfile and move on
203+
if ( !peer->paired && (cntr_ediv != 0xffff) )
204+
{
205+
bond_remove_key(BLE_GAP_ROLE_CENTRAL, cntr_ediv);
206+
207+
// Re-try with a fresh session
208+
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
209+
210+
xSemaphoreTake(peer->pair_sem, portMAX_DELAY);
211+
}
212+
197213
vSemaphoreDelete(peer->pair_sem);
198214
peer->pair_sem = NULL;
199215

libraries/Bluefruit52Lib/src/utility/bonding.cpp

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
#define printBondDir(role)
4848
#endif
4949

50+
static void get_fname(char* fname, uint8_t role, uint16_t ediv)
51+
{
52+
sprintf(fname, ( role == BLE_GAP_ROLE_PERIPH ) ? BOND_FNAME_PRPH : BOND_FNAME_CNTR, ediv);
53+
}
54+
5055
/*------------------------------------------------------------------*/
5156
/* Saving Bond Data to Nffs in following layout
5257
* - _bond_data 80 bytes
@@ -69,13 +74,7 @@ void bond_init(void)
6974
static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
7075
{
7176
char filename[BOND_FNAME_LEN];
72-
if ( role == BLE_GAP_ROLE_PERIPH )
73-
{
74-
sprintf(filename, BOND_FNAME_PRPH, bdata->own_enc.master_id.ediv);
75-
}else
76-
{
77-
sprintf(filename, BOND_FNAME_CNTR, bdata->peer_enc.master_id.ediv);
78-
}
77+
get_fname(filename, role, role == BLE_GAP_ROLE_PERIPH ? bdata->own_enc.master_id.ediv : bdata->peer_enc.master_id.ediv);
7978

8079
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
8180
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
10099
}
101100

102101
file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN);
103-
104102
file.close();
105103

106-
PRINT_HEX(bdata->own_enc.master_id.ediv);
107-
PRINT_HEX(bdata->peer_enc.master_id.ediv);
108-
109-
110104
if (result)
111105
{
112106
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)
132126
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
133127
{
134128
char filename[BOND_FNAME_LEN];
135-
if ( role == BLE_GAP_ROLE_PERIPH )
136-
{
137-
sprintf(filename, BOND_FNAME_PRPH, ediv);
138-
}else
139-
{
140-
sprintf(filename, BOND_FNAME_CNTR, ediv);
141-
}
129+
get_fname(filename, role, ediv);
142130

143131
bool result = (Nffs.readFile(filename, bdata, sizeof(bond_data_t)) > 0);
144132

@@ -169,13 +157,7 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
169157
{
170158
// save to file
171159
char filename[BOND_FNAME_LEN];
172-
if ( role == BLE_GAP_ROLE_PERIPH )
173-
{
174-
sprintf(filename, BOND_FNAME_PRPH, ediv);
175-
}else
176-
{
177-
sprintf(filename, BOND_FNAME_CNTR, ediv);
178-
}
160+
get_fname(filename, role, ediv);
179161

180162
if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
181163
{
@@ -205,13 +187,7 @@ bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv)
205187
bool loaded = false;
206188

207189
char filename[BOND_FNAME_LEN];
208-
if ( role == BLE_GAP_ROLE_PERIPH )
209-
{
210-
sprintf(filename, BOND_FNAME_PRPH, ediv);
211-
}else
212-
{
213-
sprintf(filename, BOND_FNAME_CNTR, ediv);
214-
}
190+
get_fname(filename, role, ediv);
215191

216192
NffsFile file(filename, FS_ACCESS_READ);
217193

@@ -353,3 +329,11 @@ void bond_clear_all(void)
353329
(void) Nffs.mkdir_p(BOND_DIR_PRPH);
354330
(void) Nffs.mkdir_p(BOND_DIR_CNTR);
355331
}
332+
333+
void bond_remove_key(uint8_t role, uint16_t ediv)
334+
{
335+
char filename[BOND_FNAME_LEN];
336+
get_fname(filename, role, ediv);
337+
338+
Nffs.remove(filename);
339+
}

libraries/Bluefruit52Lib/src/utility/bonding.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,20 @@ enum
6161
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
6262
};
6363

64-
void bond_init(void);
65-
void bond_clear_prph(void);
66-
void bond_clear_cntr(void);
67-
void bond_clear_all(void);
64+
void bond_init(void);
65+
void bond_clear_prph(void);
66+
void bond_clear_cntr(void);
67+
void bond_clear_all(void);
6868

69-
void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata);
70-
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata);
69+
void bond_remove_key(uint8_t role, uint16_t ediv);
7170

72-
void bond_save_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
73-
bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
71+
void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata);
72+
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata);
7473

75-
void bond_print_list(uint8_t role);
74+
void bond_save_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
75+
bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
76+
77+
void bond_print_list(uint8_t role);
7678

7779
bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata);
7880

0 commit comments

Comments
 (0)