Skip to content

Commit 4cd971f

Browse files
committed
move sys_attr (CCCD) into bond_data
1 parent 4b2e80d commit 4cd971f

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ AdafruitBluefruit::AdafruitBluefruit(void)
106106
_chars_count = 0;
107107
for(uint8_t i=0; i<BLE_MAX_CHARS; i++) _chars_list[i] = NULL;
108108

109-
varclr(&_bond);
110-
111-
_sys_attr = NULL;
112-
_sys_attr_len = 0;
109+
varclr(&_bond_data);
113110

114111
_connect_cb = NULL;
115112
_discconnect_cb = NULL;
@@ -481,6 +478,11 @@ uint16_t AdafruitBluefruit::connHandle(void)
481478
return _conn_hdl;
482479
}
483480

481+
bool AdafruitBluefruit::connBonded(void)
482+
{
483+
return _bonded;
484+
}
485+
484486
ble_gap_addr_t AdafruitBluefruit::peerAddr(void)
485487
{
486488
return _peer_addr;
@@ -495,18 +497,17 @@ err_t AdafruitBluefruit::saveAllCCCD(void)
495497
{
496498
uint16_t len=0;
497499
sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
498-
//PRINT_INT(len);
499500

500501
// Free and re-malloc if not enough
501-
if ( _sys_attr_len < len )
502+
if ( _bond_data.sys_attr_len < len )
502503
{
503-
if (_sys_attr) rtos_free(_sys_attr);
504+
if (_bond_data.sys_attr) rtos_free(_bond_data.sys_attr);
504505

505-
_sys_attr = (uint8_t*) rtos_malloc( len );
506-
_sys_attr_len = len;
506+
_bond_data.sys_attr = (uint8_t*) rtos_malloc( len );
507+
_bond_data.sys_attr_len = len;
507508
}
508509

509-
return sd_ble_gatts_sys_attr_get(_conn_hdl, _sys_attr, &_sys_attr_len, SVC_CONTEXT_FLAG);
510+
return sd_ble_gatts_sys_attr_get(_conn_hdl, _bond_data.sys_attr, &_bond_data.sys_attr_len, SVC_CONTEXT_FLAG);
510511
}
511512

512513
/*------------------------------------------------------------------*/
@@ -619,6 +620,8 @@ void AdafruitBluefruit::_poll(void)
619620
_conn_hdl = evt->evt.gap_evt.conn_handle;
620621
_peer_addr = para->peer_addr;
621622

623+
PRINT_HEX(_conn_hdl);
624+
622625
uint8_t txbuf_max;
623626
(void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max);
624627
_txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
@@ -695,15 +698,15 @@ void AdafruitBluefruit::_poll(void)
695698
ble_gap_sec_keyset_t keyset =
696699
{
697700
.keys_own = {
698-
.p_enc_key = &_bond.own_enc,
701+
.p_enc_key = &_bond_data.own_enc,
699702
.p_id_key = NULL,
700703
.p_sign_key = NULL,
701704
.p_pk = NULL
702705
},
703706

704707
.keys_peer = {
705-
.p_enc_key = &_bond.peer_enc,
706-
.p_id_key = &_bond.peer_id,
708+
.p_enc_key = &_bond_data.peer_enc,
709+
.p_id_key = &_bond_data.peer_id,
707710
.p_sign_key = NULL,
708711
.p_pk = NULL
709712
}
@@ -720,10 +723,11 @@ void AdafruitBluefruit::_poll(void)
720723
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
721724

722725
PRINT_HEX(sec_request->master_id.ediv);
723-
if (_bond.own_enc.master_id.ediv == sec_request->master_id.ediv)
726+
// EDIV_INIT_VAL
727+
if (_bond_data.own_enc.master_id.ediv == sec_request->master_id.ediv)
724728
{
725729
PRINT_LOCATION();
726-
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond.own_enc.enc_info, &_bond.peer_id.id_info, NULL);
730+
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);
727731
} else
728732
{
729733
PRINT_LOCATION();
@@ -745,7 +749,7 @@ void AdafruitBluefruit::_poll(void)
745749
_bonded = true;
746750

747751
// Connection is secured, Apply Service Context
748-
sd_ble_gatts_sys_attr_set(_conn_hdl, _sys_attr, _sys_attr_len, SVC_CONTEXT_FLAG);
752+
sd_ble_gatts_sys_attr_set(_conn_hdl, _bond_data.sys_attr, _bond_data.sys_attr_len, SVC_CONTEXT_FLAG);
749753
}
750754
break;
751755

@@ -762,11 +766,12 @@ void AdafruitBluefruit::_poll(void)
762766
break;
763767

764768
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
765-
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
769+
// TODO Look up bonded information and apply application context
770+
// sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
766771
break;
767772

768-
// Save CCCD when enabled/disabled if bonded
769773
case BLE_GATTS_EVT_WRITE:
774+
// Auto Save when CCCD is enabled/disabled if bonded
770775
if ( _bonded )
771776
{
772777
ble_gatts_evt_write_t * write = &evt->evt.gatts_evt.params.write;

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class AdafruitBluefruit
117117
err_t setConnIntervalMS(uint16_t min_ms, uint16_t max_ms);
118118

119119
uint16_t connHandle(void);
120+
bool connBonded(void);
120121
ble_gap_addr_t peerAddr(void);
121122

122123
bool txbuf_get(uint32_t ms);
@@ -167,17 +168,21 @@ class AdafruitBluefruit
167168
bool _bonded;
168169

169170
// TODO move to bonding place
171+
public: // temporary
170172
struct {
173+
// Keys
171174
ble_gap_enc_key_t own_enc;
172175
ble_gap_enc_key_t peer_enc;
173176
ble_gap_id_key_t peer_id;
174-
}_bond;
175177

178+
// System Attr (aka CCCDs)
179+
uint8_t* sys_attr;
180+
uint16_t sys_attr_len;
181+
}_bond_data;
182+
private:
176183

177184
ble_gap_addr_t _peer_addr;
178185

179-
uint8_t* _sys_attr;
180-
uint16_t _sys_attr_len;
181186

182187
// Transmission Buffer Count for HVX notification, max is seen at 7
183188
SemaphoreHandle_t _txbuf_sem;

0 commit comments

Comments
 (0)