fix notify crash if not connected

start to add PIN support
This commit is contained in:
hathach committed 2017-02-24 22:36:58 +07:00
1 parent 2de6846c28
commit 52d8654525
4 files changed
+84 -27

No files matched your search

+2 -2
View File
@@ -50,8 +50,8 @@ feather52.build.nrfutil.macosx=/usr/local/bin/nrfutil
feather52.menu.debug.l0=Level 0 (Release)
feather52.menu.debug.l0.build.debug_flags=-DCFG_DEBUG=0
feather52.menu.debug.l1=Level 1 (Error Mess)
feather52.menu.debug.l1=Level 1 (Error Message)
feather52.menu.debug.l1.build.debug_flags=-DCFG_DEBUG=1
feather52.menu.debug.l2=Level 2 (TBD)
feather52.menu.debug.l2=Level 2 (Full Debug)
feather52.menu.debug.l2.build.debug_flags=-DCFG_DEBUG=2
@@ -343,6 +343,10 @@ void BLECharacteristic::eventHandler(ble_evt_t* event)
}
}
/*------------------------------------------------------------------*/
/* WRITE
*------------------------------------------------------------------*/
err_t BLECharacteristic::write(const uint8_t* data, int len)
{
return write(data, len, 0);
@@ -389,9 +393,13 @@ err_t BLECharacteristic::write(uint8_t num)
return write( (uint8_t*) &num, sizeof(num));
}
/*------------------------------------------------------------------*/
/* NOTIFY
*------------------------------------------------------------------*/
bool BLECharacteristic::notifyEnabled(void)
{
VERIFY( _properties.notify && Bluefruit.connected() );
uint16_t cccd;
ble_gatts_value_t value =
{
@@ -407,24 +415,32 @@ bool BLECharacteristic::notifyEnabled(void)
err_t BLECharacteristic::notify(const uint8_t* data, int len, uint16_t offset)
{
if ( !_properties.notify ) return NRF_ERROR_INVALID_PARAM;
VERIFY( _properties.notify, NRF_ERROR_INVALID_PARAM);
// Whether Txbuf available or not we still update the chars'value
(void) Bluefruit.txbuf_get(100);
// could not exceed max len
uint16_t actual_len = min16(len, _max_len);
ble_gatts_hvx_params_t hvx_params =
// Must already connected for enabled to be tru
if ( notifyEnabled() )
{
.handle = _handles.value_handle,
.type = BLE_GATT_HVX_NOTIFICATION,
.offset = offset,
.p_len = &actual_len,
.p_data = (uint8_t*) data,
};
// Whether Txbuf available or not we still update the chars'value
(void) Bluefruit.txbuf_get(100);
VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params) );
// could not exceed max len
uint16_t actual_len = min16(len, _max_len);
ble_gatts_hvx_params_t hvx_params =
{
.handle = _handles.value_handle,
.type = BLE_GATT_HVX_NOTIFICATION,
.offset = offset,
.p_len = &actual_len,
.p_data = (uint8_t*) data,
};
VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params) );
}
else
{
write(data, len);
}
return ERROR_NONE;
}
+41 -5
View File
@@ -103,6 +103,9 @@ AdafruitBluefruit::AdafruitBluefruit(void)
_conn_hdl = BLE_GATT_HANDLE_INVALID;
_bonded = false;
_auth_type = BLE_GAP_AUTH_KEY_TYPE_NONE;
varclr(_pin);
_chars_count = 0;
for(uint8_t i=0; i<BLE_MAX_CHARS; i++) _chars_list[i] = NULL;
@@ -477,7 +480,24 @@ bool AdafruitBluefruit::txbuf_get(uint32_t ms)
return xSemaphoreTake(_txbuf_sem, ms2tick(ms));
}
err_t AdafruitBluefruit::saveAllCCCD(void)
bool AdafruitBluefruit::setPIN(const char* pin)
{
VERIFY ( strlen(pin) == BLE_GAP_PASSKEY_LEN );
_auth_type = BLE_GAP_AUTH_KEY_TYPE_PASSKEY;
memcpy(_pin, pin, BLE_GAP_PASSKEY_LEN);
// BLE_GAP_OPT_PASSKEY
// ble_opt_t opt
// uint8_t passkey[] = STATIC_PASSKEY;
// m_static_pin_option.gap.passkey.p_passkey = passkey;
//err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option);
return true;
}
err_t AdafruitBluefruit::_saveBondedCCCD(void)
{
uint16_t len=0;
sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
@@ -622,7 +642,7 @@ void AdafruitBluefruit::_poll(void)
// Save all configured cccd
if ( _bonded )
{
saveAllCCCD();
_saveBondedCCCD();
}
_conn_hdl = BLE_GATT_HANDLE_INVALID;
@@ -672,10 +692,10 @@ void AdafruitBluefruit::_poll(void)
ble_gap_sec_params_t sec_para =
{
.bond = 1,
.mitm = 0, //CFG_PIN_ENABLED ? nvm_data.core.passkey_enable : 0,
.mitm = 0,
.lesc = 0,
.keypress = 0,
.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 ,
.io_caps = BLE_GAP_IO_CAPS_NONE,
.oob = 0,
.min_key_size = 7,
.max_key_size = 16,
@@ -683,6 +703,13 @@ void AdafruitBluefruit::_poll(void)
.kdist_peer = { .enc = 1, .id = 1},
};
// Change security parameter according to authentication type
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
{
sec_para.mitm = 1;
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
}
ble_gap_sec_keyset_t keyset =
{
.keys_own = {
@@ -721,7 +748,16 @@ void AdafruitBluefruit::_poll(void)
break;
case BLE_GAP_EVT_PASSKEY_DISPLAY: break;
case BLE_GAP_EVT_PASSKEY_DISPLAY:
{
ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
PRINT_INT(passkey_display->match_request);
PRINT_BUFFER(passkey_display->passkey, 6);
// sd_ble_gap_auth_key_reply
}
break;
case BLE_GAP_EVT_CONN_SEC_UPDATE:
{
+10 -5
View File
@@ -106,8 +106,8 @@ class AdafruitBluefruit
/*------------------------------------------------------------------*/
/*
*------------------------------------------------------------------*/
void disconnect(void);
bool connected(void);
void disconnect(void);
err_t setConnInterval (uint16_t min, uint16_t max);
err_t setConnIntervalMS(uint16_t min_ms, uint16_t max_ms);
@@ -117,10 +117,11 @@ class AdafruitBluefruit
ble_gap_addr_t peerAddr(void);
bool txbuf_get(uint32_t ms);
err_t saveAllCCCD(void);
bool setPIN(const char* pin);
/*------------------------------------------------------------------*/
/* Central
/* Central API object
*------------------------------------------------------------------*/
BLECentral Central;
@@ -142,11 +143,12 @@ class AdafruitBluefruit
bool _central_enabled;
SemaphoreHandle_t _ble_event_sem;
BLEDfu _dfu_svc;
TimerHandle_t _led_blink_th;
bool _led_conn;
BLEDfu _dfu_svc;
// ADV Data
struct {
uint8_t data[BLE_GAP_ADV_MAX_SIZE];
@@ -163,6 +165,9 @@ class AdafruitBluefruit
uint16_t _conn_hdl;
bool _bonded;
uint8_t _auth_type;
char _pin[BLE_GAP_PASSKEY_LEN];
// TODO move to bonding place
public: // temporary
struct {
@@ -179,7 +184,6 @@ private:
ble_gap_addr_t _peer_addr;
// Transmission Buffer Count for HVX notification, max is seen at 7
SemaphoreHandle_t _txbuf_sem;
@@ -192,6 +196,7 @@ private:
void _startConnLed(void);
void _stopConnLed(void);
err_t _saveBondedCCCD(void);
void _poll(void);
friend void adafruit_bluefruit_task(void* arg);