@@ -263,44 +263,42 @@ bool BLEConnection::requestPairing(void)
263
263
264
264
// on-the-fly semaphore
265
265
_pair_sem = xSemaphoreCreateBinary ();
266
+ VERIFY (_pair_sem);
266
267
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;
276
270
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 (<keys) )
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, <keys.peer_enc .master_id , <keys.peer_enc .enc_info );
276
+ PRINT_STATUS (err);
284
277
285
- } else
278
+ if ( err == NRF_SUCCESS )
286
279
{
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);
291
281
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, <keys.peer_id .id_addr_info );
298
287
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);
301
291
302
- xSemaphoreTake (_pair_sem, portMAX_DELAY);
292
+ xSemaphoreTake (_pair_sem, portMAX_DELAY);
293
+ }
303
294
}
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);
304
302
}
305
303
306
304
vSemaphoreDelete (_pair_sem);
@@ -340,6 +338,9 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
340
338
if ( !( conn_sec->sec_mode .sm == 1 && conn_sec->sec_mode .lv == 1 ) )
341
339
{
342
340
_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 );
343
344
}
344
345
345
346
if (_pair_sem) xSemaphoreGive (_pair_sem);
0 commit comments