enhance ada_callback, add malloc len for cleaner usage

This commit is contained in:
hathach committed 2019-02-14 12:50:17 +07:00
1 parent 333f151ff5
commit e13922c58b
11 files changed
+51 -75

No files matched your search

+1 -1
View File
@@ -145,7 +145,7 @@ void GPIOTE_IRQHandler()
if (channelMap[ch] != -1 && callbacksInt[ch]) {
if ( callbackDeferred[ch] ) {
// Adafruit defer callback to non-isr if configured so
ada_callback_fromISR(NULL, callbacksInt[ch]);
ada_callback_fromISR(NULL, 0, callbacksInt[ch]);
}else{
callbacksInt[ch]();
}
+27 -17
View File
@@ -53,7 +53,6 @@ typedef struct
uint8_t arg_count;
bool from_isr;
// uint8_t callback_type;
// uint8_t _reserved[2];
uint32_t arguments[1]; // flexible array holder
@@ -85,27 +84,38 @@ typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
/**
* Macro function is called by other module with all intended parameters.
* The first parameter is malloced Pointer (NULL if not), so that callback could know to free memory
*/
#define _cb_setup(_from_isr, _malloced, _func, ... ) \
do { \
uint8_t const _count = VA_ARGS_NUM(__VA_ARGS__);\
ada_callback_t* cb_data = (ada_callback_t*) rtos_malloc( sizeof(ada_callback_t) + (_count ? (_count-1)*4 : 0) ); \
cb_data->malloced_data = _malloced;\
cb_data->callback_func = (void*)_func;\
cb_data->arg_count = _count;\
if ( _count ) {\
uint32_t arguments[] = { _ADA_CB_ARGS(__VA_ARGS__) };\
memcpy(cb_data->arguments, arguments, 4*_count);\
}\
ada_callback_queue(cb_data, _from_isr);\
#define _cb_setup(_from_isr, _mdata, _mlen, _func, ... ) \
do { \
uint8_t const _count = VA_ARGS_NUM(__VA_ARGS__); \
ada_callback_t* cb_data = (ada_callback_t*) rtos_malloc( sizeof(ada_callback_t) + (_count ? (_count-1)*4 : 0) );\
cb_data->malloced_data = NULL; \
if ( (_mdata) && (_mlen) ) { \
cb_data->malloced_data = rtos_malloc(_mlen); \
memcpy(cb_data->malloced_data, _mdata, _mlen); \
} \
cb_data->callback_func = (void*)_func; \
cb_data->arg_count = _count; \
if ( _count ) { \
uint32_t arguments[] = { _ADA_CB_ARGS(__VA_ARGS__) }; \
/* argument most likely is _mdata if used, change it to malloced one */\
if ( (_mdata) && (_mlen) ) { \
for(uint8_t i=0; i<_count; i++) {\
if (arguments[i] == ((uint32_t) (_mdata))) { arguments[i] = ((uint32_t) cb_data->malloced_data); } \
}\
}\
memcpy(cb_data->arguments, arguments, 4*_count); \
} \
ada_callback_queue(cb_data, _from_isr); \
} while(0)
/**
* Schedule an function and parameters to be invoked in Ada Callback Task
* Macro can take at least 2 and at max 7 arguments
* - 1st arg : pointer data that need to be freed with free(pointer) after function is invoked
* - 2nd arg : function to be invoked
* - 1st arg : data pointer that need to be allocated and copied (e.g local variable). NULL if not used
* Ada callback will malloc, copy and free after complete.
* - 2nd arg : data pointer length, zero if not used
* - 3rd arg : function to be invoked
* - 3rd-7th arg : function argument, will be cast to uint32_t
*/
#define ada_callback(... ) _cb_setup(false, __VA_ARGS__)
@@ -113,7 +123,7 @@ typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
/**
* Similar to ada_callback() but invoke in ISR-context
*/
#define ada_callback_fromISR(... ) _cb_setup(true , __VA_ARGS__)
#define ada_callback_fromISR(... ) _cb_setup(true, __VA_ARGS__)
void ada_callback_init(void);
void ada_callback_queue(ada_callback_t* cb_data, bool from_isr);
+1 -1
View File
@@ -136,7 +136,7 @@ static uint8_t crypto_loadKeys(void)
void crypto_scheduleStoreKeys(void)
{
// crypto_storing = 1;
ada_callback(NULL, crypto_storeKeys);
ada_callback(NULL, 0, crypto_storeKeys);
}
void crypto_storeKeys(void)
@@ -457,7 +457,7 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
Bluefruit._stopConnLed(); // stop blinking
// invoke stop callback
if (_stop_cb) ada_callback(NULL, _stop_cb);
if (_stop_cb) ada_callback(NULL, 0, _stop_cb);
}
}
}
@@ -425,16 +425,10 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
{
if ( _use_ada_cb.write_authorize )
{
uint8_t* data = (uint8_t*) rtos_malloc(sizeof(request->request.write));
VERIFY(data,);
memcpy(data, &request->request.write, sizeof(request->request.write));
// data is free after callback
ada_callback(data, _wr_authorize_cb, this, data);
ada_callback(wr_req, sizeof(*wr_req), _wr_authorize_cb, this, wr_req);
}else
{
_wr_authorize_cb(this, &request->request.write);
_wr_authorize_cb(this, wr_req);
}
}
break;
@@ -445,19 +439,15 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
if ( (request->type == BLE_GATTS_AUTHORIZE_TYPE_READ) && (_rd_authorize_cb != NULL))
{
ble_gatts_evt_read_t * rd_req = &request->request.read;
if ( _use_ada_cb.read_authorize )
{
uint8_t* data = (uint8_t*) rtos_malloc(sizeof(request->request.read));
VERIFY(data,);
memcpy(data, &request->request.read, sizeof(request->request.read));
// data is free after callback
ada_callback(data, _rd_authorize_cb, this, data);
ada_callback(rd_req, sizeof(*rd_req), _rd_authorize_cb, this, rd_req);
}
else
{
_rd_authorize_cb(this, &request->request.read);
_rd_authorize_cb(this, rd_req);
}
}
}
@@ -478,14 +468,9 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
{
if (_use_ada_cb.write)
{
uint8_t* data = (uint8_t*) rtos_malloc(request->len);
VERIFY(data,);
memcpy(data, request->data, request->len);
ada_callback(data, _wr_cb, this, data, request->len, request->offset);
ada_callback(request->data, request->len, _wr_cb, this, request->data, request->len, request->offset);
}else
{
// invoke directly if cannot allocate memory for data
_wr_cb(this, request->data, request->len, request->offset);
}
}
@@ -503,7 +488,7 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
if ( _use_ada_cb.cccd_write )
{
ada_callback(NULL, _cccd_wr_cb, this, value);
ada_callback(NULL, 0, _cccd_wr_cb, this, value);
}
else
{
@@ -413,12 +413,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
// 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);
// data is free after callback
ada_callback(data, _notify_cb, this, data, hvx->len);
ada_callback(hvx->data, hvx->len, _notify_cb, this, hvx->data, hvx->len);
}else
{
_notify_cb(this, hvx->data, hvx->len);
@@ -432,12 +427,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
// 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);
// data is free by callback
ada_callback(data, _indicate_cb, this, data, hvx->len);
ada_callback(hvx->data, hvx->len, _indicate_cb, this, hvx->data, hvx->len);
}else
{
_indicate_cb(this, hvx->data, hvx->len);
+2 -6
View File
@@ -370,11 +370,7 @@ void BLEScanner::_eventHandler(ble_evt_t* evt)
if ( invoke_cb )
{
ble_gap_evt_adv_report_t* adv_report = (ble_gap_evt_adv_report_t*) rtos_malloc( sizeof(ble_gap_evt_adv_report_t) );
VERIFY(adv_report,);
(*adv_report) = (*evt_report);
if (_rx_cb) ada_callback(adv_report, _rx_cb, adv_report);
if (_rx_cb) ada_callback(evt_report, sizeof(*evt_report), _rx_cb, evt_report);
}else
{
// continue scanning since report is filtered and callback is not invoked
@@ -410,7 +406,7 @@ void BLEScanner::_eventHandler(ble_evt_t* evt)
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
{
_runnning = false;
if (_stop_cb) ada_callback(NULL, _stop_cb);
if (_stop_cb) ada_callback(NULL, 0, _stop_cb);
}
break;
+5 -5
View File
@@ -817,10 +817,10 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
// Invoke connect callback
if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
{
if ( Periph._connect_cb ) ada_callback(NULL, Periph._connect_cb, conn_hdl);
if ( Periph._connect_cb ) ada_callback(NULL, 0, Periph._connect_cb, conn_hdl);
}else
{
if ( Central._connect_cb ) ada_callback(NULL, Central._connect_cb, conn_hdl);
if ( Central._connect_cb ) ada_callback(NULL, 0, Central._connect_cb, conn_hdl);
}
}
break;
@@ -846,10 +846,10 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
// Invoke disconnect callback
if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
{
if ( Periph._disconnect_cb ) ada_callback(NULL, Periph._disconnect_cb, conn_hdl, para->reason);
if ( Periph._disconnect_cb ) ada_callback(NULL, 0, Periph._disconnect_cb, conn_hdl, para->reason);
}else
{
if ( Central._disconnect_cb ) ada_callback(NULL, Central._disconnect_cb, conn_hdl, para->reason);
if ( Central._disconnect_cb ) ada_callback(NULL, 0, Central._disconnect_cb, conn_hdl, para->reason);
}
delete _connection[conn_hdl];
@@ -863,7 +863,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
ble_gap_evt_rssi_changed_t const * rssi_changed = &evt->evt.gap_evt.params.rssi_changed;
if ( _rssi_cb )
{
ada_callback(NULL, _rssi_cb, conn_hdl, rssi_changed->rssi);
ada_callback(NULL, 0, _rssi_cb, conn_hdl, rssi_changed->rssi);
}
}
break;
@@ -126,13 +126,8 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_keys_t* bk
bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_keys_t* bkeys)
{
uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_keys_t) );
VERIFY(buf);
memcpy(buf, bkeys, sizeof(bond_keys_t));
// queue to execute in Ada Callback thread
ada_callback(buf, bond_save_keys_dfr, role, conn_hdl, buf);
ada_callback(bkeys, sizeof(bond_keys_t), bond_save_keys_dfr, role, conn_hdl, bkeys);
return true;
}
@@ -191,7 +186,7 @@ bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
VERIFY(ediv != 0xFFFF);
// queue to execute in Ada Callback thread
ada_callback(NULL, bond_save_cccd_dfr, role, conn_hdl, ediv);
ada_callback(NULL, 0, bond_save_cccd_dfr, role, conn_hdl, ediv);
return true;
}
+1 -1
View File
@@ -195,7 +195,7 @@ void HwRotaryEncoder::_irq_handler(void)
if ( _cb )
{
int32_t step = read();
if ( step ) ada_callback_fromISR(NULL, _cb, step);
if ( step ) ada_callback_fromISR(NULL, 0, _cb, step);
}
}
}
+1 -1
View File
@@ -138,7 +138,7 @@ void SwRotaryEncoder::_irq_handler(void)
_abs += step;
_a_last = bita;
if (_cb) ada_callback_fromISR(NULL, _cb, step);
if (_cb) ada_callback_fromISR(NULL, 0, _cb, step);
}
}