Skip to content

Commit 52d8654

Browse files
committed
fix notify crash if not connected
start to add PIN support
1 parent 2de6846 commit 52d8654

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

boards.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ feather52.build.nrfutil.macosx=/usr/local/bin/nrfutil
5050
feather52.menu.debug.l0=Level 0 (Release)
5151
feather52.menu.debug.l0.build.debug_flags=-DCFG_DEBUG=0
5252

53-
feather52.menu.debug.l1=Level 1 (Error Mess)
53+
feather52.menu.debug.l1=Level 1 (Error Message)
5454
feather52.menu.debug.l1.build.debug_flags=-DCFG_DEBUG=1
5555

56-
feather52.menu.debug.l2=Level 2 (TBD)
56+
feather52.menu.debug.l2=Level 2 (Full Debug)
5757
feather52.menu.debug.l2.build.debug_flags=-DCFG_DEBUG=2

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ void BLECharacteristic::eventHandler(ble_evt_t* event)
343343
}
344344
}
345345

346+
/*------------------------------------------------------------------*/
347+
/* WRITE
348+
*------------------------------------------------------------------*/
349+
346350
err_t BLECharacteristic::write(const uint8_t* data, int len)
347351
{
348352
return write(data, len, 0);
@@ -389,9 +393,13 @@ err_t BLECharacteristic::write(uint8_t num)
389393
return write( (uint8_t*) &num, sizeof(num));
390394
}
391395

396+
/*------------------------------------------------------------------*/
397+
/* NOTIFY
398+
*------------------------------------------------------------------*/
392399
bool BLECharacteristic::notifyEnabled(void)
393400
{
394401
VERIFY( _properties.notify && Bluefruit.connected() );
402+
395403
uint16_t cccd;
396404
ble_gatts_value_t value =
397405
{
@@ -407,24 +415,32 @@ bool BLECharacteristic::notifyEnabled(void)
407415

408416
err_t BLECharacteristic::notify(const uint8_t* data, int len, uint16_t offset)
409417
{
410-
if ( !_properties.notify ) return NRF_ERROR_INVALID_PARAM;
418+
VERIFY( _properties.notify, NRF_ERROR_INVALID_PARAM);
411419

412-
// Whether Txbuf available or not we still update the chars'value
413-
(void) Bluefruit.txbuf_get(100);
420+
// Must already connected for enabled to be tru
421+
if ( notifyEnabled() )
422+
{
423+
// Whether Txbuf available or not we still update the chars'value
424+
(void) Bluefruit.txbuf_get(100);
414425

415-
// could not exceed max len
416-
uint16_t actual_len = min16(len, _max_len);
426+
// could not exceed max len
427+
uint16_t actual_len = min16(len, _max_len);
417428

418-
ble_gatts_hvx_params_t hvx_params =
419-
{
420-
.handle = _handles.value_handle,
421-
.type = BLE_GATT_HVX_NOTIFICATION,
422-
.offset = offset,
423-
.p_len = &actual_len,
424-
.p_data = (uint8_t*) data,
425-
};
429+
ble_gatts_hvx_params_t hvx_params =
430+
{
431+
.handle = _handles.value_handle,
432+
.type = BLE_GATT_HVX_NOTIFICATION,
433+
.offset = offset,
434+
.p_len = &actual_len,
435+
.p_data = (uint8_t*) data,
436+
};
426437

427-
VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params) );
438+
VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params) );
439+
}
440+
else
441+
{
442+
write(data, len);
443+
}
428444

429445
return ERROR_NONE;
430446
}

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ AdafruitBluefruit::AdafruitBluefruit(void)
103103
_conn_hdl = BLE_GATT_HANDLE_INVALID;
104104
_bonded = false;
105105

106+
_auth_type = BLE_GAP_AUTH_KEY_TYPE_NONE;
107+
varclr(_pin);
108+
106109
_chars_count = 0;
107110
for(uint8_t i=0; i<BLE_MAX_CHARS; i++) _chars_list[i] = NULL;
108111

@@ -477,7 +480,24 @@ bool AdafruitBluefruit::txbuf_get(uint32_t ms)
477480
return xSemaphoreTake(_txbuf_sem, ms2tick(ms));
478481
}
479482

480-
err_t AdafruitBluefruit::saveAllCCCD(void)
483+
bool AdafruitBluefruit::setPIN(const char* pin)
484+
{
485+
VERIFY ( strlen(pin) == BLE_GAP_PASSKEY_LEN );
486+
487+
_auth_type = BLE_GAP_AUTH_KEY_TYPE_PASSKEY;
488+
memcpy(_pin, pin, BLE_GAP_PASSKEY_LEN);
489+
490+
// BLE_GAP_OPT_PASSKEY
491+
492+
// ble_opt_t opt
493+
// uint8_t passkey[] = STATIC_PASSKEY;
494+
// m_static_pin_option.gap.passkey.p_passkey = passkey;
495+
//err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option);
496+
497+
return true;
498+
}
499+
500+
err_t AdafruitBluefruit::_saveBondedCCCD(void)
481501
{
482502
uint16_t len=0;
483503
sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
@@ -622,7 +642,7 @@ void AdafruitBluefruit::_poll(void)
622642
// Save all configured cccd
623643
if ( _bonded )
624644
{
625-
saveAllCCCD();
645+
_saveBondedCCCD();
626646
}
627647

628648
_conn_hdl = BLE_GATT_HANDLE_INVALID;
@@ -672,17 +692,24 @@ void AdafruitBluefruit::_poll(void)
672692
ble_gap_sec_params_t sec_para =
673693
{
674694
.bond = 1,
675-
.mitm = 0, //CFG_PIN_ENABLED ? nvm_data.core.passkey_enable : 0,
695+
.mitm = 0,
676696
.lesc = 0,
677697
.keypress = 0,
678-
.io_caps = BLE_GAP_IO_CAPS_NONE, // (CFG_PIN_ENABLED && nvm_data.core.passkey_enable) ? BLE_GAP_IO_CAPS_DISPLAY_ONLY : BLE_GAP_IO_CAPS_NONE ,
698+
.io_caps = BLE_GAP_IO_CAPS_NONE,
679699
.oob = 0,
680700
.min_key_size = 7,
681701
.max_key_size = 16,
682702
.kdist_own = { .enc = 1, .id = 1},
683703
.kdist_peer = { .enc = 1, .id = 1},
684704
};
685705

706+
// Change security parameter according to authentication type
707+
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
708+
{
709+
sec_para.mitm = 1;
710+
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
711+
}
712+
686713
ble_gap_sec_keyset_t keyset =
687714
{
688715
.keys_own = {
@@ -721,7 +748,16 @@ void AdafruitBluefruit::_poll(void)
721748
break;
722749

723750

724-
case BLE_GAP_EVT_PASSKEY_DISPLAY: break;
751+
case BLE_GAP_EVT_PASSKEY_DISPLAY:
752+
{
753+
ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
754+
755+
PRINT_INT(passkey_display->match_request);
756+
PRINT_BUFFER(passkey_display->passkey, 6);
757+
758+
// sd_ble_gap_auth_key_reply
759+
}
760+
break;
725761

726762
case BLE_GAP_EVT_CONN_SEC_UPDATE:
727763
{

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ class AdafruitBluefruit
106106
/*------------------------------------------------------------------*/
107107
/*
108108
*------------------------------------------------------------------*/
109-
void disconnect(void);
110109
bool connected(void);
110+
void disconnect(void);
111111

112112
err_t setConnInterval (uint16_t min, uint16_t max);
113113
err_t setConnIntervalMS(uint16_t min_ms, uint16_t max_ms);
@@ -117,10 +117,11 @@ class AdafruitBluefruit
117117
ble_gap_addr_t peerAddr(void);
118118

119119
bool txbuf_get(uint32_t ms);
120-
err_t saveAllCCCD(void);
120+
121+
bool setPIN(const char* pin);
121122

122123
/*------------------------------------------------------------------*/
123-
/* Central
124+
/* Central API object
124125
*------------------------------------------------------------------*/
125126
BLECentral Central;
126127

@@ -142,11 +143,12 @@ class AdafruitBluefruit
142143
bool _central_enabled;
143144

144145
SemaphoreHandle_t _ble_event_sem;
145-
BLEDfu _dfu_svc;
146146

147147
TimerHandle_t _led_blink_th;
148148
bool _led_conn;
149149

150+
BLEDfu _dfu_svc;
151+
150152
// ADV Data
151153
struct {
152154
uint8_t data[BLE_GAP_ADV_MAX_SIZE];
@@ -163,6 +165,9 @@ class AdafruitBluefruit
163165
uint16_t _conn_hdl;
164166
bool _bonded;
165167

168+
uint8_t _auth_type;
169+
char _pin[BLE_GAP_PASSKEY_LEN];
170+
166171
// TODO move to bonding place
167172
public: // temporary
168173
struct {
@@ -179,7 +184,6 @@ class AdafruitBluefruit
179184

180185
ble_gap_addr_t _peer_addr;
181186

182-
183187
// Transmission Buffer Count for HVX notification, max is seen at 7
184188
SemaphoreHandle_t _txbuf_sem;
185189

@@ -192,6 +196,7 @@ class AdafruitBluefruit
192196
void _startConnLed(void);
193197
void _stopConnLed(void);
194198

199+
err_t _saveBondedCCCD(void);
195200
void _poll(void);
196201

197202
friend void adafruit_bluefruit_task(void* arg);

0 commit comments

Comments
 (0)