From dd57126e9355ae02126ae1a06a53a9f4561c3162 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 23 Feb 2017 23:59:23 +0700 Subject: [PATCH] move saveCCCD() to disconnect event --- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 4 +-- libraries/Bluefruit52Lib/src/bluefruit.cpp | 36 ++++++------------- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index 03d7008b..2cb83573 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -281,10 +281,8 @@ err_t BLECharacteristic::start(void) // Currently Only register to Bluefruit when having callback support // And The Characteristic must not be temporary memory aka local variable - // Or Properties is Notify and/or Indicate for saving CCCD for bonded connection if ( !_is_temp && - (_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb || - _properties.notify || _properties.indicate) ) + (_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb) ) { (void) Bluefruit._registerCharacteristic(this); } diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 6e834f49..0fa4f02b 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -107,6 +107,7 @@ AdafruitBluefruit::AdafruitBluefruit(void) for(uint8_t i=0; ievt.gap_evt.conn_handle; _peer_addr = para->peer_addr; - PRINT_HEX(_conn_hdl); - uint8_t txbuf_max; (void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max); _txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max); @@ -637,6 +636,12 @@ void AdafruitBluefruit::_poll(void) { if (_led_conn) ledOff(LED_CONN); + // Save all configured cccd + if ( _bonded ) + { + saveAllCCCD(); + } + _conn_hdl = BLE_GATT_HANDLE_INVALID; _bonded = false; varclr(&_peer_addr); @@ -722,15 +727,11 @@ void AdafruitBluefruit::_poll(void) // return security information. Otherwise NULL ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request; - PRINT_HEX(sec_request->master_id.ediv); - // EDIV_INIT_VAL if (_bond_data.own_enc.master_id.ediv == sec_request->master_id.ediv) { - PRINT_LOCATION(); sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond_data.own_enc.enc_info, &_bond_data.peer_id.id_info, NULL); } else { - PRINT_LOCATION(); sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL); } } @@ -756,11 +757,14 @@ void AdafruitBluefruit::_poll(void) case BLE_GAP_EVT_AUTH_STATUS: { ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status; + // Bonding succeeded --> save encryption keys - PRINT_HEX(status->auth_status); if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) { _bonded = true; + }else + { + PRINT_HEX(status->auth_status); } } break; @@ -770,24 +774,6 @@ void AdafruitBluefruit::_poll(void) // sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); break; - case BLE_GATTS_EVT_WRITE: - // Auto Save when CCCD is enabled/disabled if bonded - if ( _bonded ) - { - ble_gatts_evt_write_t * write = &evt->evt.gatts_evt.params.write; - - for(int i=0; i<_chars_count; i++) - { - if (_chars_list[i]->handles().cccd_handle == write->handle ) - { - saveAllCCCD(); - break; - } - } - } - break; - - default: break; }