complete refactor AdaCallback to generic arguments
This commit is contained in:
1 parent
725cea11ff
commit
0101bcc8fc
7 files changed
+23
-64
No files matched your search
@@ -393,7 +393,7 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
|
||||
Bluefruit._stopConnLed(); // stop blinking
|
||||
|
||||
// invoke stop callback
|
||||
if (_stop_cb) ada_callback_defer(NULL, _stop_cb);
|
||||
if (_stop_cb) ada_callback(NULL, _stop_cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
if ( Bluefruit.Gap.getRole(evt_conn_hdl) == BLE_GAP_ROLE_CENTRAL)
|
||||
{
|
||||
// Invoke callback
|
||||
if ( _connect_cb) ada_callback_defer(NULL, _connect_cb, evt_conn_hdl);
|
||||
if ( _connect_cb) ada_callback(NULL, _connect_cb, evt_conn_hdl);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -159,7 +159,7 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
if ( Bluefruit.Gap.getRole(evt_conn_hdl) == BLE_GAP_ROLE_CENTRAL)
|
||||
{
|
||||
// Invoke callback
|
||||
if ( _disconnect_cb) ada_callback_defer(NULL, _disconnect_cb, evt_conn_hdl, evt->evt.gap_evt.params.disconnected.reason);
|
||||
if ( _disconnect_cb) ada_callback(NULL, _disconnect_cb, evt_conn_hdl, evt->evt.gap_evt.params.disconnected.reason);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
@@ -346,7 +346,7 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
|
||||
extern void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle);
|
||||
if ( Bluefruit.connPaired() )
|
||||
{
|
||||
ada_callback_defer(NULL, _adafruit_save_bond_cccd_dfr, event->evt.common_evt.conn_handle);
|
||||
ada_callback(NULL, _adafruit_save_bond_cccd_dfr, event->evt.common_evt.conn_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +325,7 @@ void BLEScanner::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
ble_gap_evt_adv_report_t* adv_report = (ble_gap_evt_adv_report_t*) rtos_malloc( sizeof(ble_gap_evt_adv_report_t) );
|
||||
(*adv_report) = (*evt_report);
|
||||
if (_rx_cb) ada_callback_defer(adv_report, _rx_cb, adv_report);
|
||||
if (_rx_cb) ada_callback(adv_report, _rx_cb, adv_report);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -365,7 +365,7 @@ void BLEScanner::_eventHandler(ble_evt_t* evt)
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
|
||||
{
|
||||
if (_stop_cb) ada_callback_defer(NULL, _stop_cb);
|
||||
if (_stop_cb) ada_callback(NULL, _stop_cb);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
@@ -590,7 +590,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
VERIFY_STATUS( sd_ble_gap_conn_param_update(_conn_hdl, NULL), );
|
||||
}
|
||||
|
||||
if (_connect_cb) ada_callback_defer(NULL, _connect_cb, _conn_hdl);
|
||||
if (_connect_cb) ada_callback(NULL, _connect_cb, _conn_hdl);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -610,7 +610,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
// Save all configured cccd
|
||||
if (_bonded) _saveBondCCCD();
|
||||
|
||||
if (_disconnect_cb) ada_callback_defer(NULL, _disconnect_cb, _conn_hdl, evt->evt.gap_evt.params.disconnected.reason);
|
||||
if (_disconnect_cb) ada_callback(NULL, _disconnect_cb, _conn_hdl, evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID;
|
||||
_bonded = false;
|
||||
@@ -709,7 +709,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
|
||||
{
|
||||
_bonded = true;
|
||||
ada_callback_defer(NULL, _adafruit_save_bond_key_dfr, _conn_hdl);
|
||||
ada_callback(NULL, _adafruit_save_bond_key_dfr, _conn_hdl);
|
||||
}else
|
||||
{
|
||||
PRINT_HEX(status->auth_status);
|
||||
|
||||
@@ -54,26 +54,14 @@ void adafruit_callback_task(void* arg)
|
||||
void* func = cb_data->callback_func;
|
||||
uint32_t* args = cb_data->arguments;
|
||||
|
||||
switch(cb_data->callback_type)
|
||||
switch (cb_data->arg_count)
|
||||
{
|
||||
/*------------- Client Service & Chars -------------*/
|
||||
case BLEClientCharacteristic_notify_cb_t:
|
||||
((BLEClientCharacteristic::notify_cb_t) func) ( *((BLEClientCharacteristic*) args[0]), (uint8_t*) args[1], (uint16_t) args[2] );
|
||||
break;
|
||||
|
||||
case ADA_CB_DEFERRED_FUNC:
|
||||
switch (cb_data->arg_count)
|
||||
{
|
||||
case 0: ((adacb_0arg_t) func)(); break;
|
||||
case 1: ((adacb_1arg_t) func)(args[0]); break;
|
||||
case 2: ((adacb_2arg_t) func)(args[0], args[1]); break;
|
||||
case 3: ((adacb_3arg_t) func)(args[0], args[1], args[2]); break;
|
||||
case 4: ((adacb_4arg_t) func)(args[0], args[1], args[2], args[3]); break;
|
||||
case 5: ((adacb_5arg_t) func)(args[0], args[1], args[2], args[3], args[4]); break;
|
||||
|
||||
default: VERIFY_MESS(NRF_ERROR_INVALID_PARAM); break;
|
||||
}
|
||||
break;
|
||||
case 0: ((adacb_0arg_t) func)(); break;
|
||||
case 1: ((adacb_1arg_t) func)(args[0]); break;
|
||||
case 2: ((adacb_2arg_t) func)(args[0], args[1]); break;
|
||||
case 3: ((adacb_3arg_t) func)(args[0], args[1], args[2]); break;
|
||||
case 4: ((adacb_4arg_t) func)(args[0], args[1], args[2], args[3]); break;
|
||||
case 5: ((adacb_5arg_t) func)(args[0], args[1], args[2], args[3], args[4]); break;
|
||||
|
||||
default: VERIFY_MESS(NRF_ERROR_INVALID_PARAM); break;
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ typedef struct
|
||||
void* malloced_data;
|
||||
void* callback_func;
|
||||
|
||||
uint8_t callback_type;
|
||||
uint8_t arg_count;
|
||||
// uint8_t callback_type;
|
||||
// uint8_t _reserved[2];
|
||||
|
||||
uint32_t arguments[1]; // flexible array holder
|
||||
@@ -56,7 +56,7 @@ typedef struct
|
||||
|
||||
static_assert(sizeof(ada_callback_t) == 16, "Incorrect Size");
|
||||
|
||||
/*------------- Defer -------------*/
|
||||
/*------------- Defer callback type, determined by number of arguments -------------*/
|
||||
typedef void (*adacb_0arg_t) (void);
|
||||
typedef void (*adacb_1arg_t) (uint32_t);
|
||||
typedef void (*adacb_2arg_t) (uint32_t, uint32_t);
|
||||
@@ -64,31 +64,6 @@ typedef void (*adacb_3arg_t) (uint32_t, uint32_t, uint32_t);
|
||||
typedef void (*adacb_4arg_t) (uint32_t, uint32_t, uint32_t, uint32_t);
|
||||
typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* X Macros expansion for callback function
|
||||
* If callbacks from separated classes share the same prototype, only
|
||||
* one instance is enough (mandatory)
|
||||
*------------------------------------------------------------------*/
|
||||
#define ADA_CB_LOOKUP(XPAND) \
|
||||
XPAND(BLEClientCharacteristic , notify_cb_t ) \
|
||||
/*XPAND(BLEClientCharacteristic , indicate_cb_t)*/ \
|
||||
|
||||
#define ADA_CB_ENUM_XPAND(_class, _cbname) \
|
||||
_class##_##_cbname,
|
||||
|
||||
|
||||
#define ADA_CB_FUNC_XPAND(_class, _cbname) \
|
||||
static inline uint8_t ada_callback_type(_class::_cbname _func) { (void) _func; return _class##_##_cbname; }
|
||||
|
||||
enum
|
||||
{
|
||||
ADA_CB_LOOKUP(ADA_CB_ENUM_XPAND)
|
||||
|
||||
ADA_CB_DEFERRED_FUNC,
|
||||
};
|
||||
|
||||
ADA_CB_LOOKUP(ADA_CB_FUNC_XPAND)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Macros to force uint32_t casting to all arguments (up to 5)
|
||||
*------------------------------------------------------------------*/
|
||||
@@ -103,13 +78,16 @@ ADA_CB_LOOKUP(ADA_CB_FUNC_XPAND)
|
||||
|
||||
#define _ADA_CB_ARGS(...) _GET_6TH_ARG(_0, ##__VA_ARGS__, _ADA_CB_ARGS_5, _ADA_CB_ARGS_4, _ADA_CB_ARGS_3, _ADA_CB_ARGS_2, _ADA_CB_ARGS_1, _ADA_CB_ARGS_0)(__VA_ARGS__)
|
||||
|
||||
#define _ada_cb_setup(_malloced, _func, _func_type, ... ) \
|
||||
/**
|
||||
* 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 ada_callback(_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->callback_type = _func_type;\
|
||||
cb_data->arg_count = _count;\
|
||||
if ( _count ) {\
|
||||
uint32_t arguments[] = { _ADA_CB_ARGS(__VA_ARGS__) };\
|
||||
@@ -118,13 +96,6 @@ ADA_CB_LOOKUP(ADA_CB_FUNC_XPAND)
|
||||
ada_callback_queue(cb_data);\
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
* 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 ada_callback(_malloced, _func , ... ) _ada_cb_setup(_malloced, _func, ada_callback_type(_func), __VA_ARGS__)
|
||||
#define ada_callback_defer(_malloced, _func, ...) _ada_cb_setup(_malloced, _func, ADA_CB_DEFERRED_FUNC , __VA_ARGS__)
|
||||
|
||||
void ada_callback_init(void);
|
||||
void ada_callback_queue(ada_callback_t* cb_data);
|
||||
|
||||
|
||||
Reference in new issue
Block a user