From b555131ca216fff9c303531a51e13a66d7117f99 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 14 Mar 2020 16:29:51 +0700 Subject: [PATCH] improve connection requestPairing() move loadCccd() to --- cores/nRF5/verify.h | 10 ++- .../Bluefruit52Lib/src/BLEConnection.cpp | 65 ++++++++++--------- libraries/Bluefruit52Lib/src/BLEConnection.h | 4 +- libraries/Bluefruit52Lib/src/BLEPairing.cpp | 16 +---- 4 files changed, 44 insertions(+), 51 deletions(-) diff --git a/cores/nRF5/verify.h b/cores/nRF5/verify.h index c66cb4b3..c5bcc53a 100644 --- a/cores/nRF5/verify.h +++ b/cores/nRF5/verify.h @@ -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 diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.cpp b/libraries/Bluefruit52Lib/src/BLEConnection.cpp index f7bf291d..2dc8f7ae 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.cpp +++ b/libraries/Bluefruit52Lib/src/BLEConnection.cpp @@ -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); diff --git a/libraries/Bluefruit52Lib/src/BLEConnection.h b/libraries/Bluefruit52Lib/src/BLEConnection.h index 403782e5..a082c008 100644 --- a/libraries/Bluefruit52Lib/src/BLEConnection.h +++ b/libraries/Bluefruit52Lib/src/BLEConnection.h @@ -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); }; diff --git a/libraries/Bluefruit52Lib/src/BLEPairing.cpp b/libraries/Bluefruit52Lib/src/BLEPairing.cpp index 97316c64..ce7affa6 100644 --- a/libraries/Bluefruit52Lib/src/BLEPairing.cpp +++ b/libraries/Bluefruit52Lib/src/BLEPairing.cpp @@ -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;