Skip to content

Commit 1210b3e

Browse files
committed
BLEConnection add _bonded, rename _paired to _secured
1 parent 3526298 commit 1210b3e

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

libraries/Bluefruit52Lib/src/BLEConnection.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
5555
_hvn_sem = xSemaphoreCreateCounting(hvn_qsize, hvn_qsize);
5656
_wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize);
5757

58-
_paired = false;
58+
_secured = false;
5959
_hvc_sem = NULL;
6060
_hvc_received = false;
6161
_pair_sem = NULL;
@@ -85,7 +85,12 @@ bool BLEConnection::connected(void)
8585

8686
bool BLEConnection::paired (void)
8787
{
88-
return _paired;
88+
return _secured;
89+
}
90+
91+
bool BLEConnection::bonded(void)
92+
{
93+
return _bonded;
8994
}
9095

9196
uint8_t BLEConnection::getRole (void)
@@ -236,50 +241,52 @@ bool BLEConnection::saveLongTermKey(bond_keys_t const* ltkey)
236241
{
237242
bond_save_keys(_role, _conn_hdl, ltkey);
238243
_bond_id_addr = ltkey->peer_id.id_addr_info;
239-
_paired = true; // paired with new device
244+
_secured = true; // paired with new device
245+
_bonded = true;
240246
return true;
241247
}
242248

243249
bool BLEConnection::loadLongTermKey(bond_keys_t* ltkey)
244250
{
245-
VERIFY( bond_load_keys(_role, &_peer_addr, ltkey) );
251+
_bonded = bond_load_keys(_role, &_peer_addr, ltkey);
252+
VERIFY(_bonded);
246253
_bond_id_addr = ltkey->peer_id.id_addr_info;
247254
return true;
248255
}
249256

250257
bool BLEConnection::requestPairing(void)
251258
{
252259
// skip if already paired
253-
if ( _paired ) return true;
260+
if ( _secured ) return true;
254261

255262
BLEPairing* secure = &Bluefruit.Pairing;
256263

257264
// on-the-fly semaphore
258265
_pair_sem = xSemaphoreCreateBinary();
259266
VERIFY(_pair_sem);
260267

261-
bond_keys_t ltkey;
262-
263-
if ( loadLongTermKey(&ltkey) )
264-
{
265-
// We already bonded with this peer previously
266-
// Encrypt the connection using stored Longterm Key
267-
if ( secure->_encrypt(_conn_hdl, &ltkey) )
268-
{
269-
xSemaphoreTake(_pair_sem, portMAX_DELAY);
270-
271-
// Failed to pair using stored key, this happens when peer
272-
// delete bonds while we did not --> let's remove the obsolete keyfile and retry
273-
if ( !_paired )
274-
{
275-
bond_remove_key(_role, &ltkey.peer_id.id_addr_info);
276-
277-
// Re-try with a fresh session
278-
secure->_authenticate(_conn_hdl);
279-
xSemaphoreTake(_pair_sem, portMAX_DELAY);
280-
}
281-
}
282-
}else
268+
// bond_keys_t ltkey;
269+
//
270+
// if ( loadLongTermKey(&ltkey) ) // central only
271+
// {
272+
// // We already bonded with this peer previously
273+
// // Encrypt the connection using stored Longterm Key
274+
// if ( secure->_encrypt(_conn_hdl, &ltkey) )
275+
// {
276+
// xSemaphoreTake(_pair_sem, portMAX_DELAY);
277+
//
278+
// // Failed to pair using stored key, this happens when peer
279+
// // delete bonds while we did not --> let's remove the obsolete keyfile and retry
280+
// if ( !_paired )
281+
// {
282+
// bond_remove_key(_role, &ltkey.peer_id.id_addr_info);
283+
//
284+
// // Re-try with a fresh session
285+
// secure->_authenticate(_conn_hdl);
286+
// xSemaphoreTake(_pair_sem, portMAX_DELAY);
287+
// }
288+
// }
289+
// }else
283290
{
284291
// start a fresh new authentication process
285292
secure->_authenticate(_conn_hdl);
@@ -289,7 +296,7 @@ bool BLEConnection::requestPairing(void)
289296
vSemaphoreDelete(_pair_sem);
290297
_pair_sem = NULL;
291298

292-
return _paired;
299+
return _secured;
293300
}
294301

295302
bool BLEConnection::waitForIndicateConfirm(void)
@@ -324,7 +331,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
324331
// Connection is secured (paired) if encryption level > 1
325332
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
326333
{
327-
_paired = true;
334+
_secured = true;
328335

329336
// Try to restore CCCD with bonded peer, if it doesn't exist (newly bonded), initialize it
330337
if ( !loadCccd() ) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);

libraries/Bluefruit52Lib/src/BLEConnection.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class BLEConnection
5454
uint16_t _ediv;
5555

5656
bool _connected;
57-
bool _paired;
57+
bool _secured;
58+
bool _bonded; // have LTK stored in InternalFS
5859
bool _hvc_received;
5960

6061
ble_gap_addr_t _peer_addr; // resolvable connect address
@@ -74,6 +75,7 @@ class BLEConnection
7475
uint16_t handle(void);
7576
bool connected(void);
7677
bool paired(void);
78+
bool bonded(void);
7779

7880
uint8_t getRole(void);
7981
uint16_t getMtu (void);

0 commit comments

Comments
 (0)