Skip to content

Commit dd57126

Browse files
committed
move saveCCCD() to disconnect event
1 parent a08b5a7 commit dd57126

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,8 @@ err_t BLECharacteristic::start(void)
281281

282282
// Currently Only register to Bluefruit when having callback support
283283
// And The Characteristic must not be temporary memory aka local variable
284-
// Or Properties is Notify and/or Indicate for saving CCCD for bonded connection
285284
if ( !_is_temp &&
286-
(_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb ||
287-
_properties.notify || _properties.indicate) )
285+
(_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb) )
288286
{
289287
(void) Bluefruit._registerCharacteristic(this);
290288
}

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ AdafruitBluefruit::AdafruitBluefruit(void)
107107
for(uint8_t i=0; i<BLE_MAX_CHARS; i++) _chars_list[i] = NULL;
108108

109109
varclr(&_bond_data);
110+
_bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv
110111

111112
_connect_cb = NULL;
112113
_discconnect_cb = NULL;
@@ -620,8 +621,6 @@ void AdafruitBluefruit::_poll(void)
620621
_conn_hdl = evt->evt.gap_evt.conn_handle;
621622
_peer_addr = para->peer_addr;
622623

623-
PRINT_HEX(_conn_hdl);
624-
625624
uint8_t txbuf_max;
626625
(void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max);
627626
_txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
@@ -637,6 +636,12 @@ void AdafruitBluefruit::_poll(void)
637636
{
638637
if (_led_conn) ledOff(LED_CONN);
639638

639+
// Save all configured cccd
640+
if ( _bonded )
641+
{
642+
saveAllCCCD();
643+
}
644+
640645
_conn_hdl = BLE_GATT_HANDLE_INVALID;
641646
_bonded = false;
642647
varclr(&_peer_addr);
@@ -722,15 +727,11 @@ void AdafruitBluefruit::_poll(void)
722727
// return security information. Otherwise NULL
723728
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
724729

725-
PRINT_HEX(sec_request->master_id.ediv);
726-
// EDIV_INIT_VAL
727730
if (_bond_data.own_enc.master_id.ediv == sec_request->master_id.ediv)
728731
{
729-
PRINT_LOCATION();
730732
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);
731733
} else
732734
{
733-
PRINT_LOCATION();
734735
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
735736
}
736737
}
@@ -756,11 +757,14 @@ void AdafruitBluefruit::_poll(void)
756757
case BLE_GAP_EVT_AUTH_STATUS:
757758
{
758759
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
760+
759761
// Bonding succeeded --> save encryption keys
760-
PRINT_HEX(status->auth_status);
761762
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
762763
{
763764
_bonded = true;
765+
}else
766+
{
767+
PRINT_HEX(status->auth_status);
764768
}
765769
}
766770
break;
@@ -770,24 +774,6 @@ void AdafruitBluefruit::_poll(void)
770774
// sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
771775
break;
772776

773-
case BLE_GATTS_EVT_WRITE:
774-
// Auto Save when CCCD is enabled/disabled if bonded
775-
if ( _bonded )
776-
{
777-
ble_gatts_evt_write_t * write = &evt->evt.gatts_evt.params.write;
778-
779-
for(int i=0; i<_chars_count; i++)
780-
{
781-
if (_chars_list[i]->handles().cccd_handle == write->handle )
782-
{
783-
saveAllCCCD();
784-
break;
785-
}
786-
}
787-
}
788-
break;
789-
790-
791777
default: break;
792778
}
793779

0 commit comments

Comments
 (0)