Skip to content

Commit 4747a9b

Browse files
committed
add useAdaCallback option to setNotifyCallback(), setIndicateCallback() for client char
1 parent f86c094 commit 4747a9b

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
## 0.8.2
44

55
- Fixed burning bootloader issue with MacOS
6-
-
6+
- Added gpstest_swuart example sketch
7+
- Added indicate API for BLECharacteristic
8+
- Added useAdaCallback option to setNotifyCallback(), setIndicateCallback() for BLEClientCharacteristic
79

810
## 0.8.1
911

libraries/Bluefruit52Lib/keywords.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ enableIndicate KEYWORD2
201201
disableIndicate KEYWORD2
202202

203203
setNotifyCallback KEYWORD2
204-
useAdaCallback KEYWORD2
205204

206205
#######################################
207206
# BLEScanner Methods (KEYWORD2)

libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ void BLEClientCharacteristic::_init(void)
4343
_cccd_handle = BLE_GATT_HANDLE_INVALID;
4444

4545
_notify_cb = NULL;
46-
_use_AdaCallback = true;
46+
_indicate_cb = NULL;
47+
48+
varclr(&_use_ada_cb);
4749
}
4850

4951
BLEClientCharacteristic::BLEClientCharacteristic(void)
@@ -112,11 +114,6 @@ bool BLEClientCharacteristic::discovered(void)
112114
return _chr.handle_value != BLE_GATT_HANDLE_INVALID;
113115
}
114116

115-
void BLEClientCharacteristic::useAdaCallback(bool enabled)
116-
{
117-
_use_AdaCallback = enabled;
118-
}
119-
120117
uint16_t BLEClientCharacteristic::connHandle(void)
121118
{
122119
return _service->connHandle();
@@ -331,9 +328,16 @@ uint16_t BLEClientCharacteristic::write32(int value)
331328
}
332329

333330

334-
void BLEClientCharacteristic::setNotifyCallback(notify_cb_t fp)
331+
void BLEClientCharacteristic::setNotifyCallback(notify_cb_t fp, bool useAdaCallback)
335332
{
336333
_notify_cb = fp;
334+
_use_ada_cb.notify = useAdaCallback;
335+
}
336+
337+
void BLEClientCharacteristic::setIndicateCallback(indicate_cb_t fp, bool useAdaCallback)
338+
{
339+
_indicate_cb = fp;
340+
_use_ada_cb.indicate = useAdaCallback;
337341
}
338342

339343
bool BLEClientCharacteristic::writeCCCD(uint16_t value)
@@ -392,26 +396,45 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
392396
{
393397
ble_gattc_evt_hvx_t* hvx = &evt->evt.gattc_evt.params.hvx;
394398

395-
if ( hvx->type == BLE_GATT_HVX_NOTIFICATION )
399+
switch ( hvx->type )
396400
{
397-
if (_notify_cb)
398-
{
399-
// use AdaCallback or invoke directly
400-
if (_use_AdaCallback)
401+
case BLE_GATT_HVX_NOTIFICATION:
402+
if (_notify_cb)
401403
{
402-
uint8_t* data = (uint8_t*) rtos_malloc(hvx->len);
403-
if (!data) return;
404-
memcpy(data, hvx->data, hvx->len);
404+
// use AdaCallback or invoke directly
405+
if (_use_ada_cb.notify)
406+
{
407+
uint8_t* data = (uint8_t*) rtos_malloc(hvx->len);
408+
if (!data) return;
409+
memcpy(data, hvx->data, hvx->len);
410+
411+
ada_callback(data, _notify_cb, this, data, hvx->len);
412+
}else
413+
{
414+
_notify_cb(*this, hvx->data, hvx->len);
415+
}
416+
}
417+
break;
405418

406-
ada_callback(data, _notify_cb, this, data, hvx->len);
407-
}else
419+
case BLE_GATT_HVX_INDICATION:
420+
if (_indicate_cb)
408421
{
409-
_notify_cb(*this, hvx->data, hvx->len);
422+
// use AdaCallback or invoke directly
423+
if (_use_ada_cb.indicate)
424+
{
425+
uint8_t* data = (uint8_t*) rtos_malloc(hvx->len);
426+
if (!data) return;
427+
memcpy(data, hvx->data, hvx->len);
428+
429+
ada_callback(data, _notify_cb, this, data, hvx->len);
430+
}else
431+
{
432+
_notify_cb(*this, hvx->data, hvx->len);
433+
}
410434
}
411-
}
412-
}else
413-
{
435+
break;
414436

437+
default : break;
415438
}
416439
}
417440
break;

libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,30 @@ class BLEClientCharacteristic
100100
bool disableIndicate (void);
101101

102102
/*------------- Callbacks -------------*/
103-
void setNotifyCallback(notify_cb_t fp);
104-
void useAdaCallback(bool enabled);
103+
void setNotifyCallback(notify_cb_t fp, bool useAdaCallback = true);
104+
void setIndicateCallback(indicate_cb_t fp, bool useAdaCallback = true);
105105

106106
/*------------- Internal usage -------------*/
107107
void _assign(ble_gattc_char_t* gattc_chr);
108108
bool _discoverDescriptor(uint16_t conn_handle, ble_gattc_handle_range_t hdl_range);
109109

110110
private:
111-
ble_gattc_char_t _chr;
112-
uint16_t _cccd_handle;
111+
ble_gattc_char_t _chr;
112+
uint16_t _cccd_handle;
113113

114114
BLEClientService* _service;
115-
notify_cb_t _notify_cb;
116-
bool _use_AdaCallback; // whether callback is invoked in separated task with AdaCallback
117-
118115
AdaMsg _adamsg;
119116

117+
/*------------- Callbacks -------------*/
118+
notify_cb_t _notify_cb;
119+
indicate_cb_t _indicate_cb;
120+
121+
struct ATTR_PACKED {
122+
uint8_t notify : 1;
123+
uint8_t indicate : 1;
124+
} _use_ada_cb;
125+
126+
120127
void _init (void);
121128
void _eventHandler (ble_evt_t* event);
122129

libraries/Bluefruit52Lib/src/clients/BLEAncs.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ bool BLEAncs::begin(void)
9494

9595
_notification.setNotifyCallback(bleancs_notification_cb);
9696

97-
// Data Attribute is likely requested in notification callback
97+
// Data Attribute is most likely requested in notification callback
9898
// let's call data's callback in the ble task
99-
_data.useAdaCallback(false);
100-
_data.setNotifyCallback(bleancs_data_cb);
99+
_data.setNotifyCallback(bleancs_data_cb, false);
101100

102101
return true;
103102
}

0 commit comments

Comments
 (0)