Skip to content

Commit b555131

Browse files
committed
improve connection requestPairing()
move loadCccd() to
1 parent e6bcfcb commit b555131

File tree

4 files changed

+42
-49
lines changed

4 files changed

+42
-49
lines changed

cores/nRF5/verify.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,15 @@ extern "C"
107107
* - status value if called with 1 parameter e.g VERIFY_STATUS(status)
108108
* - 2 parameter if called with 2 parameters e.g VERIFY_STATUS(status, errorcode)
109109
*/
110-
#define VERIFY_STATUS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_err_str)
110+
#define VERIFY_STATUS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_err_str)
111111

112-
#define VERIFY_ERROR(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL)
112+
#define PRINT_STATUS(_exp) do \
113+
{ \
114+
int32_t _status = (int32_t) _exp; \
115+
if ( 0 != _status ) VERIFY_MESS(_status, dbg_err_str); \
116+
} while(0) \
117+
118+
#define VERIFY_ERROR(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL)
113119

114120
/*------------------------------------------------------------------*/
115121
/* VERIFY

libraries/Bluefruit52Lib/src/BLEConnection.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -263,44 +263,42 @@ bool BLEConnection::requestPairing(void)
263263

264264
// on-the-fly semaphore
265265
_pair_sem = xSemaphoreCreateBinary();
266+
VERIFY(_pair_sem);
266267

267-
if ( _role == BLE_GAP_ROLE_PERIPH )
268-
{
269-
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
270-
xSemaphoreTake(_pair_sem, portMAX_DELAY);
271-
}
272-
else
273-
{
274-
uint16_t cntr_ediv = 0xFFFF;
275-
bond_keys_t bkeys;
268+
bond_keys_t ltkeys;
269+
uint32_t err;
276270

277-
// Check to see if we did bonded with current prph previously
278-
// TODO currently only matches key using fixed address
279-
if ( bond_find_cntr(&_peer_addr, &bkeys) )
280-
{
281-
cntr_ediv = bkeys.peer_enc.master_id.ediv;
282-
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, cntr_ediv);
283-
VERIFY_STATUS( sd_ble_gap_encrypt(_conn_hdl, &bkeys.peer_enc.master_id, &bkeys.peer_enc.enc_info), false);
271+
if ( _loadLongTermKey(&ltkeys) )
272+
{
273+
// We already bonded with this peer previously
274+
// Encrypt the connection using stored Longterm Key
275+
err = sd_ble_gap_encrypt(_conn_hdl, &ltkeys.peer_enc.master_id, &ltkeys.peer_enc.enc_info);
276+
PRINT_STATUS(err);
284277

285-
}else
278+
if ( err == NRF_SUCCESS )
286279
{
287-
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
288-
}
289-
290-
xSemaphoreTake(_pair_sem, portMAX_DELAY);
280+
xSemaphoreTake(_pair_sem, portMAX_DELAY);
291281

292-
// Failed to pair using central stored keys, this happens when
293-
// Prph delete bonds while we did not --> let's remove the obsolete keyfile and retry
294-
if ( !_paired && (cntr_ediv != 0xffff) )
295-
{
296-
// FIXME central remove key
297-
bond_remove_key(BLE_GAP_ROLE_CENTRAL, cntr_ediv);
282+
// Failed to pair using stored key, this happens when peer
283+
// delete bonds while we did not --> let's remove the obsolete keyfile and retry
284+
if ( !_paired )
285+
{
286+
bond_remove_key(_role, &ltkeys.peer_id.id_addr_info);
298287

299-
// Re-try with a fresh session
300-
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
288+
// Re-try with a fresh session
289+
err = sd_ble_gap_authenticate(_conn_hdl, &sec_param );
290+
PRINT_STATUS(err);
301291

302-
xSemaphoreTake(_pair_sem, portMAX_DELAY);
292+
xSemaphoreTake(_pair_sem, portMAX_DELAY);
293+
}
303294
}
295+
}else
296+
{
297+
// start a fresh new authentication process
298+
err = sd_ble_gap_authenticate(_conn_hdl, &sec_param );
299+
PRINT_STATUS(err);
300+
301+
xSemaphoreTake(_pair_sem, portMAX_DELAY);
304302
}
305303

306304
vSemaphoreDelete(_pair_sem);
@@ -340,6 +338,9 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
340338
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
341339
{
342340
_paired = true;
341+
342+
// Try to restore CCCD with bonded peer, if it doesn't exist (newly bonded), initialize it
343+
if ( !loadCccd() ) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
343344
}
344345

345346
if (_pair_sem) xSemaphoreGive(_pair_sem);

libraries/Bluefruit52Lib/src/BLEConnection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ class BLEConnection
115115
*------------------------------------------------------------------*/
116116
void _eventHandler(ble_evt_t* evt);
117117

118-
bool _saveLongTermKey(bond_keys_t const* bkeys);
119-
bool _loadLongTermKey(bond_keys_t* bkeys);
118+
bool _saveLongTermKey(bond_keys_t const* ltkey);
119+
bool _loadLongTermKey(bond_keys_t* ltkey);
120120
};
121121

122122

libraries/Bluefruit52Lib/src/BLEPairing.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,22 +362,8 @@ void BLEPairing::_eventHandler(ble_evt_t* evt)
362362
case BLE_GAP_EVT_CONN_SEC_UPDATE:
363363
{
364364
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
365+
(void) conn_sec;
365366
LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
366-
367-
// Connection is secured (paired) if encryption level > 1
368-
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
369-
{
370-
// Previously bonded --> secure by re-connection process --> Load & Set SysAttr (Apply Service Context)
371-
// Else Init SysAttr (first bonded)
372-
if ( !conn->loadCccd() )
373-
{
374-
sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0);
375-
}
376-
377-
// _paired = true;
378-
}
379-
380-
// if (_pair_sem) xSemaphoreGive(_pair_sem);
381367
}
382368
break;
383369

0 commit comments

Comments
 (0)