diff --git a/changelog.md b/changelog.md index 66561853..3e338803 100644 --- a/changelog.md +++ b/changelog.md @@ -3,7 +3,9 @@ ## 0.8.2 - Fixed burning bootloader issue with MacOS -- +- Added gpstest_swuart example sketch +- Added indicate API for BLECharacteristic +- Added useAdaCallback option to setNotifyCallback(), setIndicateCallback() for BLEClientCharacteristic ## 0.8.1 diff --git a/libraries/Bluefruit52Lib/keywords.txt b/libraries/Bluefruit52Lib/keywords.txt index 5bd1ddc7..fa82d3bb 100644 --- a/libraries/Bluefruit52Lib/keywords.txt +++ b/libraries/Bluefruit52Lib/keywords.txt @@ -201,7 +201,6 @@ enableIndicate KEYWORD2 disableIndicate KEYWORD2 setNotifyCallback KEYWORD2 -useAdaCallback KEYWORD2 ####################################### # BLEScanner Methods (KEYWORD2) diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp index 12d89162..4f3112df 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp @@ -43,7 +43,9 @@ void BLEClientCharacteristic::_init(void) _cccd_handle = BLE_GATT_HANDLE_INVALID; _notify_cb = NULL; - _use_AdaCallback = true; + _indicate_cb = NULL; + + varclr(&_use_ada_cb); } BLEClientCharacteristic::BLEClientCharacteristic(void) @@ -112,11 +114,6 @@ bool BLEClientCharacteristic::discovered(void) return _chr.handle_value != BLE_GATT_HANDLE_INVALID; } -void BLEClientCharacteristic::useAdaCallback(bool enabled) -{ - _use_AdaCallback = enabled; -} - uint16_t BLEClientCharacteristic::connHandle(void) { return _service->connHandle(); @@ -331,9 +328,16 @@ uint16_t BLEClientCharacteristic::write32(int value) } -void BLEClientCharacteristic::setNotifyCallback(notify_cb_t fp) +void BLEClientCharacteristic::setNotifyCallback(notify_cb_t fp, bool useAdaCallback) { _notify_cb = fp; + _use_ada_cb.notify = useAdaCallback; +} + +void BLEClientCharacteristic::setIndicateCallback(indicate_cb_t fp, bool useAdaCallback) +{ + _indicate_cb = fp; + _use_ada_cb.indicate = useAdaCallback; } bool BLEClientCharacteristic::writeCCCD(uint16_t value) @@ -392,26 +396,45 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) { ble_gattc_evt_hvx_t* hvx = &evt->evt.gattc_evt.params.hvx; - if ( hvx->type == BLE_GATT_HVX_NOTIFICATION ) + switch ( hvx->type ) { - if (_notify_cb) - { - // use AdaCallback or invoke directly - if (_use_AdaCallback) + case BLE_GATT_HVX_NOTIFICATION: + if (_notify_cb) { - uint8_t* data = (uint8_t*) rtos_malloc(hvx->len); - if (!data) return; - memcpy(data, hvx->data, hvx->len); + // use AdaCallback or invoke directly + if (_use_ada_cb.notify) + { + uint8_t* data = (uint8_t*) rtos_malloc(hvx->len); + if (!data) return; + memcpy(data, hvx->data, hvx->len); - ada_callback(data, _notify_cb, this, data, hvx->len); - }else - { - _notify_cb(*this, hvx->data, hvx->len); + ada_callback(data, _notify_cb, this, data, hvx->len); + }else + { + _notify_cb(*this, hvx->data, hvx->len); + } } - } - }else - { + break; + case BLE_GATT_HVX_INDICATION: + if (_indicate_cb) + { + // use AdaCallback or invoke directly + if (_use_ada_cb.indicate) + { + uint8_t* data = (uint8_t*) rtos_malloc(hvx->len); + if (!data) return; + memcpy(data, hvx->data, hvx->len); + + ada_callback(data, _notify_cb, this, data, hvx->len); + }else + { + _notify_cb(*this, hvx->data, hvx->len); + } + } + break; + + default : break; } } break; diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h index 2fccdc87..6a52562d 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h @@ -100,23 +100,30 @@ class BLEClientCharacteristic bool disableIndicate (void); /*------------- Callbacks -------------*/ - void setNotifyCallback(notify_cb_t fp); - void useAdaCallback(bool enabled); + void setNotifyCallback(notify_cb_t fp, bool useAdaCallback = true); + void setIndicateCallback(indicate_cb_t fp, bool useAdaCallback = true); /*------------- Internal usage -------------*/ void _assign(ble_gattc_char_t* gattc_chr); bool _discoverDescriptor(uint16_t conn_handle, ble_gattc_handle_range_t hdl_range); private: - ble_gattc_char_t _chr; - uint16_t _cccd_handle; + ble_gattc_char_t _chr; + uint16_t _cccd_handle; BLEClientService* _service; - notify_cb_t _notify_cb; - bool _use_AdaCallback; // whether callback is invoked in separated task with AdaCallback - AdaMsg _adamsg; + /*------------- Callbacks -------------*/ + notify_cb_t _notify_cb; + indicate_cb_t _indicate_cb; + + struct ATTR_PACKED { + uint8_t notify : 1; + uint8_t indicate : 1; + } _use_ada_cb; + + void _init (void); void _eventHandler (ble_evt_t* event); diff --git a/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp b/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp index 96c241af..c57ccd12 100644 --- a/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp +++ b/libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp @@ -94,10 +94,9 @@ bool BLEAncs::begin(void) _notification.setNotifyCallback(bleancs_notification_cb); - // Data Attribute is likely requested in notification callback + // Data Attribute is most likely requested in notification callback // let's call data's callback in the ble task - _data.useAdaCallback(false); - _data.setNotifyCallback(bleancs_data_cb); + _data.setNotifyCallback(bleancs_data_cb, false); return true; }