Skip to content

Commit 7b43260

Browse files
committed
clean up
1 parent b487db9 commit 7b43260

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

libraries/Bluefruit52Lib/src/BLEConnection.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -252,45 +252,37 @@ bool BLEConnection::requestPairing(void)
252252
// skip if already paired
253253
if ( _paired ) return true;
254254

255-
ble_gap_sec_params_t sec_param = Bluefruit.Pairing.getSecureParam();
255+
BLEPairing* secure = &Bluefruit.Pairing;
256256

257257
// on-the-fly semaphore
258258
_pair_sem = xSemaphoreCreateBinary();
259259
VERIFY(_pair_sem);
260260

261-
bond_keys_t ltkeys;
262-
uint32_t err;
261+
bond_keys_t ltkey;
263262

264-
if ( loadLongTermKey(&ltkeys) )
263+
if ( loadLongTermKey(&ltkey) )
265264
{
266265
// We already bonded with this peer previously
267266
// Encrypt the connection using stored Longterm Key
268-
err = sd_ble_gap_encrypt(_conn_hdl, &ltkeys.peer_enc.master_id, &ltkeys.peer_enc.enc_info);
269-
PRINT_STATUS(err);
270-
271-
if ( err == NRF_SUCCESS )
267+
if ( secure->_encrypt(_conn_hdl, &ltkey) )
272268
{
273269
xSemaphoreTake(_pair_sem, portMAX_DELAY);
274270

275271
// Failed to pair using stored key, this happens when peer
276272
// delete bonds while we did not --> let's remove the obsolete keyfile and retry
277273
if ( !_paired )
278274
{
279-
bond_remove_key(_role, &ltkeys.peer_id.id_addr_info);
275+
bond_remove_key(_role, &ltkey.peer_id.id_addr_info);
280276

281277
// Re-try with a fresh session
282-
err = sd_ble_gap_authenticate(_conn_hdl, &sec_param );
283-
PRINT_STATUS(err);
284-
278+
secure->_authenticate(_conn_hdl);
285279
xSemaphoreTake(_pair_sem, portMAX_DELAY);
286280
}
287281
}
288282
}else
289283
{
290284
// start a fresh new authentication process
291-
err = sd_ble_gap_authenticate(_conn_hdl, &sec_param );
292-
PRINT_STATUS(err);
293-
285+
secure->_authenticate(_conn_hdl);
294286
xSemaphoreTake(_pair_sem, portMAX_DELAY);
295287
}
296288

libraries/Bluefruit52Lib/src/BLEPairing.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ void BLEPairing::setCompleteCallback(pair_complete_cb_t fp)
195195
_complete_cb = fp;
196196
}
197197

198+
bool BLEPairing::_authenticate(uint16_t conn_hdl)
199+
{
200+
VERIFY_STATUS(sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
201+
return true;
202+
}
203+
204+
bool BLEPairing::_encrypt(uint16_t conn_hdl, bond_keys_t const* ltkey)
205+
{
206+
VERIFY_STATUS(sd_ble_gap_encrypt(conn_hdl, &ltkey->peer_enc.master_id, &ltkey->peer_enc.enc_info), false);
207+
return true;
208+
}
209+
198210
//--------------------------------------------------------------------+
199211
/* First-time Pairing
200212
*

libraries/Bluefruit52Lib/src/BLEPairing.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BLEPairing
4242

4343
bool begin(void);
4444

45-
// Static Passkey
45+
// Use static Passkey (Legacy SC)
4646
bool setPIN(const char* pin);
4747

4848
// resolve address with IRK to see if it matches
@@ -60,6 +60,9 @@ class BLEPairing
6060
ble_gap_sec_params_t getSecureParam(void) { return _sec_param; }
6161
void _eventHandler(ble_evt_t* evt);
6262

63+
bool _authenticate(uint16_t conn_hdl);
64+
bool _encrypt(uint16_t conn_hdl, bond_keys_t const* ltkey);
65+
6366
private:
6467
ble_gap_sec_params_t _sec_param;
6568

0 commit comments

Comments
 (0)