From d53d59e5228a2c13910704c7636d6a68271e3d08 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 31 Oct 2018 15:04:15 +0700 Subject: [PATCH] bond clean up --- libraries/Bluefruit52Lib/src/BLEGap.cpp | 11 +++++++---- libraries/Bluefruit52Lib/src/utility/bonding.cpp | 5 ++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index 0b72a6a6..2441c9e2 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -181,7 +181,7 @@ bool BLEGap::requestPairing(uint16_t conn_hdl) xSemaphoreTake(peer->pair_sem, portMAX_DELAY); -// Failed to pair using central stored keys, this happens when + // Failed to pair using central stored keys, this happens when // Prph delete bonds while we did not --> let's remove the obsolete keyfile and move on if ( !peer->paired && (cntr_ediv != 0xffff) ) { @@ -383,9 +383,12 @@ void BLEGap::_eventHandler(ble_evt_t* evt) // Occurs if bonded + reconnection, or we initiate the pairing process if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) ) { - // Previously bonded --> secure by re-connection process --> Load & Set Sys Attr (Apply Service Context) - // Else Init Sys Attr - bond_load_cccd(peer->role, conn_hdl, peer->ediv); + // Previously bonded --> secure by re-connection process --> Load & Set SysAttr (Apply Service Context) + // Else Init SysAttr (first bonded) + if ( !bond_load_cccd(peer->role, conn_hdl, peer->ediv) ) + { + sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0); + } peer->paired = true; } diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.cpp b/libraries/Bluefruit52Lib/src/utility/bonding.cpp index 30d16ecb..f35533b9 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.cpp +++ b/libraries/Bluefruit52Lib/src/utility/bonding.cpp @@ -196,7 +196,7 @@ bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, uint16_t ediv) return true; } -bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv) +bool bond_load_cccd(uint8_t role, uint16_t conn_hdl, uint16_t ediv) { bool loaded = false; @@ -219,7 +219,7 @@ bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv) file.read(sys_attr, len); - if ( ERROR_NONE == sd_ble_gatts_sys_attr_set(cond_hdl, sys_attr, len, SVC_CONTEXT_FLAG) ) + if ( ERROR_NONE == sd_ble_gatts_sys_attr_set(conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) ) { loaded = true; BOND_LOG("Loaded CCCD from file %s ( offset = %d, len = %d bytes )", filename, file.size() - (len + 1), len); @@ -233,7 +233,6 @@ bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv) if ( !loaded ) { LOG_LV1("BOND", "CCCD setting not found"); - sd_ble_gatts_sys_attr_set(cond_hdl, NULL, 0, 0); } return loaded;