Skip to content

Commit f3136ab

Browse files
committed
improve bonding with on-the-fly semaphore
enhance central bond
1 parent e81f60c commit f3136ab

File tree

6 files changed

+100
-36
lines changed

6 files changed

+100
-36
lines changed

libraries/Bluefruit52Lib/src/BLEGap.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,35 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
168168
{
169169
gap_peer_t* peer = &_peers[conn_hdl];
170170

171-
// skip if already bonded
171+
// skip if already paired
172172
if ( peer->paired ) return true;
173173

174+
// Check to see if we did bonded with current prph
175+
if ( peer->role == BLE_GAP_ROLE_CENTRAL )
176+
{
177+
bond_data_t bdata;
174178

175-
176-
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
177-
uint32_t start = millis();
178-
179-
// timeout in 30 seconds
180-
while ( ! ((volatile bool) peer->paired) && (start + 30000 > millis()) )
179+
if ( bond_find_cntr(&peer->addr, &bdata) )
180+
{
181+
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, bdata.peer_enc.master_id.ediv);
182+
VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bdata.peer_enc.master_id, &bdata.peer_enc.enc_info), false);
183+
}else
184+
{
185+
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
186+
}
187+
}else
181188
{
182-
yield();
189+
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
183190
}
184191

192+
// Wait for pairing process using on-the-fly semaphore
193+
peer->pair_sem = xSemaphoreCreateBinary();
194+
195+
xSemaphoreTake(peer->pair_sem, portMAX_DELAY);
196+
197+
vSemaphoreDelete(peer->pair_sem);
198+
peer->pair_sem = NULL;
199+
185200
return peer->paired;
186201
}
187202

@@ -372,15 +387,22 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
372387
break;
373388

374389
case BLE_GAP_EVT_CONN_SEC_UPDATE:
375-
// Connection is secured aka Paired
390+
{
391+
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
376392

377-
// Previously bonded --> secure by re-connection process
378-
// --> Load & Set Sys Attr (Apply Service Context)
379-
// Else Init Sys Attr
380-
bond_load_cccd(peer->role, conn_hdl, peer->ediv);
393+
// Connection is secured (paired)
394+
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
395+
{
396+
// Previously bonded --> secure by re-connection process
397+
// --> Load & Set Sys Attr (Apply Service Context)
398+
// Else Init Sys Attr
399+
bond_load_cccd(peer->role, conn_hdl, peer->ediv);
381400

382-
// Paired is Bonded (as we always save keys)
383-
peer->paired = true;
401+
peer->paired = true;
402+
}
403+
404+
if (peer->pair_sem) xSemaphoreGive(peer->pair_sem);
405+
}
384406
break;
385407

386408
case BLE_GAP_EVT_PASSKEY_DISPLAY:

libraries/Bluefruit52Lib/src/BLEGap.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ class BLEGap
105105
SemaphoreHandle_t hvn_tx_sem;
106106
SemaphoreHandle_t wrcmd_tx_sem;
107107

108-
SemaphoreHandle_t indicate_confirm_sem;
109108
bool hvc_received;
109+
110+
// On-demand semaphore that are created on the fly
111+
SemaphoreHandle_t hvc_sem;
112+
SemaphoreHandle_t pair_sem;
110113
} gap_peer_t;
111114

112115
gap_peer_t* _get_peer(uint16_t conn_hdl) { return &_peers[conn_hdl]; }

libraries/Bluefruit52Lib/src/BLEGatt.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl)
8080
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(conn_hdl);
8181

8282
// hvi confirm semaphore is created on the fly
83-
peer->indicate_confirm_sem = xSemaphoreCreateBinary();
83+
peer->hvc_sem = xSemaphoreCreateBinary();
8484

85-
xSemaphoreTake(peer->indicate_confirm_sem, portMAX_DELAY);
85+
xSemaphoreTake(peer->hvc_sem, portMAX_DELAY);
8686

87-
vSemaphoreDelete(peer->indicate_confirm_sem);
88-
peer->indicate_confirm_sem = NULL;
87+
vSemaphoreDelete(peer->hvc_sem);
88+
peer->hvc_sem = NULL;
8989

9090
return peer->hvc_received;
9191
}
@@ -237,7 +237,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
237237
LOG_LV2("GATTS", "Confirm received handle = 0x%04X", evt->evt.gatts_evt.params.hvc.handle);
238238
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
239239

240-
if ( peer->indicate_confirm_sem ) xSemaphoreGive(peer->indicate_confirm_sem);
240+
if ( peer->hvc_sem ) xSemaphoreGive(peer->hvc_sem);
241241
peer->hvc_received = true;
242242
}
243243
break;
@@ -248,7 +248,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
248248

249249
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
250250

251-
if ( peer->indicate_confirm_sem ) xSemaphoreGive(peer->indicate_confirm_sem);
251+
if ( peer->hvc_sem ) xSemaphoreGive(peer->hvc_sem);
252252
peer->hvc_received = false;
253253
}
254254
break;

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,13 @@ void AdafruitBluefruit::printInfo(void)
731731
Serial.println();
732732

733733
/*------------- List the paried device -------------*/
734-
Serial.printf(title_fmt, "Peripheral Paired Devices\n");
735-
bond_print_list(BLE_GAP_ROLE_PERIPH);
734+
Serial.printf(title_fmt, "Peripheral Paired Devices");
736735
Serial.println();
736+
bond_print_list(BLE_GAP_ROLE_PERIPH);
737737

738-
Serial.printf(title_fmt, "Central Paired Devices\n");
739-
bond_print_list(BLE_GAP_ROLE_CENTRAL);
738+
Serial.printf(title_fmt, "Central Paired Devices");
740739
Serial.println();
740+
bond_print_list(BLE_GAP_ROLE_CENTRAL);
741741

742742
Serial.println();
743743
}

libraries/Bluefruit52Lib/src/utility/bonding.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bda
7474
sprintf(filename, BOND_FNAME_PRPH, bdata->own_enc.master_id.ediv);
7575
}else
7676
{
77-
sprintf(filename, BOND_FNAME_CNTR, bdata->own_enc.master_id.ediv);
77+
sprintf(filename, BOND_FNAME_CNTR, bdata->peer_enc.master_id.ediv);
7878
}
7979

8080
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
@@ -103,6 +103,10 @@ static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bda
103103

104104
file.close();
105105

106+
PRINT_HEX(bdata->own_enc.master_id.ediv);
107+
PRINT_HEX(bdata->peer_enc.master_id.ediv);
108+
109+
106110
if (result)
107111
{
108112
LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename);
@@ -285,6 +289,39 @@ void bond_print_list(uint8_t role)
285289
cprintf("\n");
286290
}
287291

292+
293+
bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
294+
{
295+
bool found = false;
296+
297+
NffsDir dir(BOND_DIR_CNTR);
298+
NffsDirEntry dirEntry;
299+
300+
while( dir.read(&dirEntry) && !found )
301+
{
302+
// Read bond data of each stored file
303+
if ( !dirEntry.isDirectory() )
304+
{
305+
NffsFile file(BOND_DIR_CNTR, dirEntry, FS_ACCESS_READ);
306+
if ( file.read( (uint8_t*)bdata, sizeof(bond_data_t)) )
307+
{
308+
if ( !memcmp(addr->addr, bdata->peer_id.id_addr_info.addr, 6) )
309+
{
310+
// Compare static address
311+
found = true;
312+
}else if ( addr->addr_type == BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE )
313+
{
314+
// Resolving private address
315+
}
316+
}
317+
318+
file.close();
319+
}
320+
}
321+
322+
return found;
323+
}
324+
288325
/*------------------------------------------------------------------*/
289326
/* DELETE
290327
*------------------------------------------------------------------*/

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_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);
7171

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);
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);
7474

75-
void bond_print_list(uint8_t role);
75+
void bond_print_list(uint8_t role);
76+
77+
bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata);
7678

7779

7880

0 commit comments

Comments
 (0)