improve connection requestPairing()
move loadCccd() to
This commit is contained in:
1 parent
e6bcfcb4c0
commit
b555131ca2
4 files changed
+44
-51
No files matched your search
+8
-2
@@ -107,9 +107,15 @@ extern "C"
|
||||
* - status value if called with 1 parameter e.g VERIFY_STATUS(status)
|
||||
* - 2 parameter if called with 2 parameters e.g VERIFY_STATUS(status, errorcode)
|
||||
*/
|
||||
#define VERIFY_STATUS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_err_str)
|
||||
#define VERIFY_STATUS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_err_str)
|
||||
|
||||
#define VERIFY_ERROR(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL)
|
||||
#define PRINT_STATUS(_exp) do \
|
||||
{ \
|
||||
int32_t _status = (int32_t) _exp; \
|
||||
if ( 0 != _status ) VERIFY_MESS(_status, dbg_err_str); \
|
||||
} while(0) \
|
||||
|
||||
#define VERIFY_ERROR(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* VERIFY
|
||||
|
||||
@@ -263,44 +263,42 @@ bool BLEConnection::requestPairing(void)
|
||||
|
||||
// on-the-fly semaphore
|
||||
_pair_sem = xSemaphoreCreateBinary();
|
||||
VERIFY(_pair_sem);
|
||||
|
||||
if ( _role == BLE_GAP_ROLE_PERIPH )
|
||||
bond_keys_t ltkeys;
|
||||
uint32_t err;
|
||||
|
||||
if ( _loadLongTermKey(<keys) )
|
||||
{
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
|
||||
xSemaphoreTake(_pair_sem, portMAX_DELAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t cntr_ediv = 0xFFFF;
|
||||
bond_keys_t bkeys;
|
||||
// We already bonded with this peer previously
|
||||
// Encrypt the connection using stored Longterm Key
|
||||
err = sd_ble_gap_encrypt(_conn_hdl, <keys.peer_enc.master_id, <keys.peer_enc.enc_info);
|
||||
PRINT_STATUS(err);
|
||||
|
||||
// Check to see if we did bonded with current prph previously
|
||||
// TODO currently only matches key using fixed address
|
||||
if ( bond_find_cntr(&_peer_addr, &bkeys) )
|
||||
if ( err == NRF_SUCCESS )
|
||||
{
|
||||
cntr_ediv = bkeys.peer_enc.master_id.ediv;
|
||||
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, cntr_ediv);
|
||||
VERIFY_STATUS( sd_ble_gap_encrypt(_conn_hdl, &bkeys.peer_enc.master_id, &bkeys.peer_enc.enc_info), false);
|
||||
|
||||
}else
|
||||
{
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
|
||||
}
|
||||
|
||||
xSemaphoreTake(_pair_sem, portMAX_DELAY);
|
||||
|
||||
// Failed to pair using central stored keys, this happens when
|
||||
// Prph delete bonds while we did not --> let's remove the obsolete keyfile and retry
|
||||
if ( !_paired && (cntr_ediv != 0xffff) )
|
||||
{
|
||||
// FIXME central remove key
|
||||
bond_remove_key(BLE_GAP_ROLE_CENTRAL, cntr_ediv);
|
||||
|
||||
// Re-try with a fresh session
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &sec_param ), false);
|
||||
|
||||
xSemaphoreTake(_pair_sem, portMAX_DELAY);
|
||||
|
||||
// Failed to pair using stored key, this happens when peer
|
||||
// delete bonds while we did not --> let's remove the obsolete keyfile and retry
|
||||
if ( !_paired )
|
||||
{
|
||||
bond_remove_key(_role, <keys.peer_id.id_addr_info);
|
||||
|
||||
// Re-try with a fresh session
|
||||
err = sd_ble_gap_authenticate(_conn_hdl, &sec_param );
|
||||
PRINT_STATUS(err);
|
||||
|
||||
xSemaphoreTake(_pair_sem, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
}else
|
||||
{
|
||||
// start a fresh new authentication process
|
||||
err = sd_ble_gap_authenticate(_conn_hdl, &sec_param );
|
||||
PRINT_STATUS(err);
|
||||
|
||||
xSemaphoreTake(_pair_sem, portMAX_DELAY);
|
||||
}
|
||||
|
||||
vSemaphoreDelete(_pair_sem);
|
||||
@@ -340,6 +338,9 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
|
||||
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
|
||||
{
|
||||
_paired = true;
|
||||
|
||||
// Try to restore CCCD with bonded peer, if it doesn't exist (newly bonded), initialize it
|
||||
if ( !loadCccd() ) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
|
||||
}
|
||||
|
||||
if (_pair_sem) xSemaphoreGive(_pair_sem);
|
||||
|
||||
@@ -115,8 +115,8 @@ class BLEConnection
|
||||
*------------------------------------------------------------------*/
|
||||
void _eventHandler(ble_evt_t* evt);
|
||||
|
||||
bool _saveLongTermKey(bond_keys_t const* bkeys);
|
||||
bool _loadLongTermKey(bond_keys_t* bkeys);
|
||||
bool _saveLongTermKey(bond_keys_t const* ltkey);
|
||||
bool _loadLongTermKey(bond_keys_t* ltkey);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -362,22 +362,8 @@ void BLEPairing::_eventHandler(ble_evt_t* evt)
|
||||
case BLE_GAP_EVT_CONN_SEC_UPDATE:
|
||||
{
|
||||
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
|
||||
(void) conn_sec;
|
||||
LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
|
||||
|
||||
// Connection is secured (paired) if encryption level > 1
|
||||
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
|
||||
{
|
||||
// Previously bonded --> secure by re-connection process --> Load & Set SysAttr (Apply Service Context)
|
||||
// Else Init SysAttr (first bonded)
|
||||
if ( !conn->loadCccd() )
|
||||
{
|
||||
sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0);
|
||||
}
|
||||
|
||||
// _paired = true;
|
||||
}
|
||||
|
||||
// if (_pair_sem) xSemaphoreGive(_pair_sem);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
Reference in new issue
Block a user