Skip to content

Commit f0440b5

Browse files
committed
Pairing added secured callback, requestPairing() is non-blocking now.
1 parent 7cc1307 commit f0440b5

File tree

3 files changed

+23
-45
lines changed

3 files changed

+23
-45
lines changed

libraries/Bluefruit52Lib/src/BLEConnection.cpp

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
5656
_wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize);
5757

5858
_secured = false;
59+
_bonded = false;
5960
_hvc_sem = NULL;
6061
_hvc_received = false;
61-
_pair_sem = NULL;
6262

6363
_ediv = 0xFFFF;
6464
}
@@ -70,7 +70,6 @@ BLEConnection::~BLEConnection()
7070

7171
//------------- on-the-fly data must be freed -------------//
7272
if (_hvc_sem ) vSemaphoreDelete(_hvc_sem );
73-
if (_pair_sem ) vSemaphoreDelete(_pair_sem);
7473
}
7574

7675
uint16_t BLEConnection::handle (void)
@@ -270,44 +269,7 @@ bool BLEConnection::requestPairing(void)
270269
// skip if already paired
271270
if ( _secured ) return true;
272271

273-
BLEPairing* secure = &Bluefruit.Pairing;
274-
275-
// on-the-fly semaphore
276-
_pair_sem = xSemaphoreCreateBinary();
277-
VERIFY(_pair_sem);
278-
279-
// bond_keys_t ltkey;
280-
//
281-
// if ( loadBondKey(&ltkey) ) // central only
282-
// {
283-
// // We already bonded with this peer previously
284-
// // Encrypt the connection using stored Longterm Key
285-
// if ( secure->_encrypt(_conn_hdl, &ltkey) )
286-
// {
287-
// xSemaphoreTake(_pair_sem, portMAX_DELAY);
288-
//
289-
// // Failed to pair using stored key, this happens when peer
290-
// // delete bonds while we did not --> let's remove the obsolete keyfile and retry
291-
// if ( !_paired )
292-
// {
293-
// bond_remove_key(_role, &ltkey.peer_id.id_addr_info);
294-
//
295-
// // Re-try with a fresh session
296-
// secure->_authenticate(_conn_hdl);
297-
// xSemaphoreTake(_pair_sem, portMAX_DELAY);
298-
// }
299-
// }
300-
// }else
301-
{
302-
// start a fresh new authentication process
303-
secure->_authenticate(_conn_hdl);
304-
xSemaphoreTake(_pair_sem, portMAX_DELAY);
305-
}
306-
307-
vSemaphoreDelete(_pair_sem);
308-
_pair_sem = NULL;
309-
310-
return _secured;
272+
return Bluefruit.Pairing._authenticate(_conn_hdl);
311273
}
312274

313275
bool BLEConnection::waitForIndicateConfirm(void)
@@ -335,9 +297,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
335297

336298
case BLE_GAP_EVT_CONN_SEC_UPDATE:
337299
{
338-
// Connection is secured, we have paired/bonded
339300
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
340-
LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
341301

342302
// Connection is secured (paired) if encryption level > 1
343303
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
@@ -347,8 +307,6 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
347307
// Try to restore CCCD with bonded peer, if it doesn't exist (newly bonded), initialize it
348308
if ( !loadCccd() ) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
349309
}
350-
351-
if (_pair_sem) xSemaphoreGive(_pair_sem);
352310
}
353311
break;
354312

libraries/Bluefruit52Lib/src/BLEPairing.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ BLEPairing::BLEPairing(void)
7070
_sec_param = _sec_param_default;
7171
_passkey_cb = NULL;
7272
_complete_cb = NULL;
73+
_secured_cb = NULL;
7374
}
7475

7576
bool BLEPairing::begin(void)
@@ -201,6 +202,11 @@ void BLEPairing::setCompleteCallback(pair_complete_cb_t fp)
201202
_complete_cb = fp;
202203
}
203204

205+
void BLEPairing::setSecuredCallback(pair_secured_cb_t fp)
206+
{
207+
_secured_cb = fp;
208+
}
209+
204210
bool BLEPairing::_authenticate(uint16_t conn_hdl)
205211
{
206212
VERIFY_STATUS(sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
@@ -381,6 +387,18 @@ void BLEPairing::_eventHandler(ble_evt_t* evt)
381387
}
382388
break;
383389

390+
case BLE_GAP_EVT_CONN_SEC_UPDATE:
391+
{
392+
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
393+
LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
394+
395+
if ( conn->secured() && _secured_cb )
396+
{
397+
ada_callback(NULL, 0, _secured_cb, conn_hdl, conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
398+
}
399+
}
400+
break;
401+
384402
default: break;
385403
}
386404
}

libraries/Bluefruit52Lib/src/BLEPairing.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class BLEPairing
3737
public:
3838
typedef bool (*pair_passkey_cb_t ) (uint16_t conn_hdl, uint8_t const passkey[6], bool match_request);
3939
typedef void (*pair_complete_cb_t) (uint16_t conn_hdl, uint8_t auth_status);
40+
typedef void (*pair_secured_cb_t) (uint16_t conn_hdl, uint8_t sec_mode, uint8_t level);
4041

4142
BLEPairing(void);
4243

@@ -57,13 +58,13 @@ class BLEPairing
5758
//------------- Callbacks -------------//
5859
bool setPasskeyCallback(pair_passkey_cb_t fp);
5960
void setCompleteCallback(pair_complete_cb_t fp);
61+
void setSecuredCallback(pair_secured_cb_t fp);
6062

6163
/*------------------------------------------------------------------*/
6264
/* INTERNAL USAGE ONLY
6365
* Although declare as public, it is meant to be invoked by internal
6466
* code. User should not call these directly
6567
*------------------------------------------------------------------*/
66-
ble_gap_sec_params_t getSecureParam(void) { return _sec_param; }
6768
void _eventHandler(ble_evt_t* evt);
6869

6970
bool _authenticate(uint16_t conn_hdl);
@@ -81,6 +82,7 @@ class BLEPairing
8182

8283
pair_passkey_cb_t _passkey_cb;
8384
pair_complete_cb_t _complete_cb;
85+
pair_secured_cb_t _secured_cb;
8486
};
8587

8688
#endif /* BLEPAIRING_H_ */

0 commit comments

Comments
 (0)