added AdaCallback implemetation

This commit is contained in:
hathach committed 2017-04-03 22:58:35 +07:00
1 parent d955303a78
commit a770b995c3
7 files changed
+165 -51

No files matched your search

+27
View File
@@ -62,6 +62,33 @@
typeof (b) _b = (b); \
_a < _b ? _a : _b; })
/*------------------------------------------------------------------*/
/* Count number of arguments of __VA_ARGS__
* - reference https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s
* - _GET_NTH_ARG() takes args >= N (64) but only expand to Nth one (64th)
* - _RSEQ_N() is reverse sequential to N to add padding to have
* Nth position is the same as the number of arguments
* - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma)
*------------------------------------------------------------------*/
#define NARG(...) NARG_(_0, ##__VA_ARGS__,_RSEQ_N())
#define NARG_(...) _GET_NTH_ARG(__VA_ARGS__)
#define _GET_NTH_ARG( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define _RSEQ_N() \
62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
//--------------------------------------------------------------------+
// MACROS
//--------------------------------------------------------------------+
@@ -65,6 +65,21 @@ void connect_callback(uint16_t conn_handle)
{
(void) conn_handle;
Serial.println("Connected");
Serial.print("Discovering BLE Uart Service ... ");
if ( bleCentralUart.discover( Bluefruit.Central.connHandle() ) )
{
Serial.println("Found it");
Serial.println("Enable TXD's notify");
bleCentralUart.enableTXD();
Serial.println("Ready to receive from peripheral");
}else
{
Serial.println("Found NONE");
}
}
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
@@ -93,23 +108,7 @@ void loop()
if ( Bluefruit.Central.connected() )
{
// Not discovered yet
if ( !bleCentralUart.discovered() )
{
Serial.print("Discovering BLE Uart Service ... ");
if ( bleCentralUart.discover( Bluefruit.Central.connHandle() ) )
{
Serial.println("Found it");
Serial.println("Enable TXD's notify");
bleCentralUart.enableTXD();
Serial.println("Ready to receive from peripheral");
}else
{
Serial.println("Found NONE");
}
}else
if ( bleCentralUart.discovered() )
{
// Discovered means in working state
// Get Serial input and send to Peripheral
+40 -15
View File
@@ -34,26 +34,51 @@
*/
/**************************************************************************/
#include <Arduino.h>
#include "bluefruit.h"
#include "AdaCallback.h"
enum
static QueueHandle_t _cb_queue = NULL;
void adafruit_callback_task(void* arg)
{
(void) arg;
};
while(1)
{
ada_callback_t* cb_data;
if ( xQueueReceive(_cb_queue, (void*) &cb_data, portMAX_DELAY) )
{
switch(cb_data->callback_type)
{
case AdafruitBluefruit_connect_callback_t:
break;
case BLECentral_connect_callback_t:
{
BLECentral::connect_callback_t func = (BLECentral::connect_callback_t) cb_data->callback_func;
func(cb_data->arguments[0]);
}
break;
typedef struct
default: VERIFY_MESS(NRF_ERROR_INVALID_PARAM);
}
// free up resource
if (cb_data->malloced_data) rtos_free(cb_data->malloced_data);
rtos_free(cb_data);
}
}
}
void ada_callback_queue(ada_callback_t* cb_data)
{
uint8_t callback_type;
uint8_t arg_count;
int8_t malloced_idx;
void* arguments[];
}ada_callback_t;
//#define ada_callback(_type, )
//void ada_callback()
xQueueSend(_cb_queue, (void*) &cb_data, BLE_GENERIC_TIMEOUT);
}
void ada_callback_init(void)
{
// queue to hold "Pointer to callback data"
_cb_queue = xQueueCreate(CFG_CALLBACK_QUEUE_LENGTH, sizeof(ada_callback_t*));
TaskHandle_t callback_task_hdl;
xTaskCreate( adafruit_callback_task, "Callback", CFG_CALLBACK_TASK_STACKSIZE, NULL, TASK_PRIO_NORMAL, &callback_task_hdl);
}
@@ -36,8 +36,82 @@
#ifndef ADACALLBACK_H_
#define ADACALLBACK_H_
#include <Arduino.h>
#include "bluefruit.h"
#define CFG_CALLBACK_TASK_STACKSIZE (512*1)
#define CFG_CALLBACK_QUEUE_LENGTH 10
typedef struct
{
void* malloced_data;
void* callback_func;
uint8_t callback_type;
uint8_t arg_count;
// uint8_t _reserved[2];
uint32_t arguments[1]; // flexible array holder
}ada_callback_t;
static_assert(sizeof(ada_callback_t) == 16, "Incorrect Size");
/*------------------------------------------------------------------*/
/* X Macros expansion
*------------------------------------------------------------------*/
#define ADA_CB_LOOKUP(XPAND) \
XPAND(AdafruitBluefruit, connect_callback_t) \
XPAND(BLECentral, connect_callback_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) { return _class##_##_cbname; }
enum
{
ADA_CB_LOOKUP(ADA_CB_ENUM_XPAND)
};
ADA_CB_LOOKUP(ADA_CB_FUNC_XPAND)
/*------------------------------------------------------------------*/
/* Macros to force uint32_t casting to all arguments (up to 5)
*------------------------------------------------------------------*/
#define _ADA_CB_ARGS_0() (uint32_t) 0
#define _ADA_CB_ARGS_1(_1) (uint32_t) _1
#define _ADA_CB_ARGS_2(_1, _2) _ADA_CB_ARGS_1(_1), (uint32_t) _2
#define _ADA_CB_ARGS_3(_1, _2, _3) _ADA_CB_ARGS_2(_1, _2), (uint32_t) _3
#define _ADA_CB_ARGS_4(_1, _2, _3, _4) _ADA_CB_ARGS_3(_1, _2, _3), (uint32_t) _4
#define _ADA_CB_ARGS_5(_1, _2, _3, _4,_5) _ADA_CB_ARGS_4(_1, _2, _3, _4), (uint32_t) _5
#define _GET_6TH_ARG(_0, _1, _2, _3, _4, _5, _6, ... ) _6
#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__)
/**
* 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 = NARG(__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 = ada_callback_type(_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);\
}while(0)
void ada_callback_init(void);
void ada_callback_queue(ada_callback_t* cb_data);
#endif /* ADACALLBACK_H_ */
+5 -1
View File
@@ -35,6 +35,7 @@
/**************************************************************************/
#include "bluefruit.h"
#include "AdaCallback.h"
/**
* Constructor
@@ -242,7 +243,10 @@ void BLECentral::_event_handler(ble_evt_t* evt)
// TODO multiple connections
_conn_hdl = evt->evt.gap_evt.conn_handle;
if ( _connect_cb ) _connect_cb(evt->evt.gap_evt.conn_handle);
if ( _connect_cb )
{
ada_callback(NULL, _connect_cb, evt->evt.gap_evt.conn_handle);
}
}
}
break;
+3 -15
View File
@@ -35,6 +35,7 @@
/**************************************************************************/
#include "bluefruit.h"
#include "AdaCallback.h"
#include <Nffs.h>
#define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
@@ -44,7 +45,6 @@
#define CFG_ADV_BLINKY_INTERVAL 500
#define CFG_BLE_TASK_STACKSIZE (512*3)
#define CFG_CALLBACK_TASK_STACKSIZE (512*1)
#define CFG_SOC_TASK_STACKSIZE (200)
#define CFG_BOND_NFFS_DIR "/adafruit/bond"
@@ -63,8 +63,6 @@ extern "C"
void adafruit_ble_task(void* arg);
void adafruit_soc_task(void* arg);
void adafruit_callback_task(void* arg);
/*------------------------------------------------------------------*/
/* INTERNAL FUNCTION
@@ -211,8 +209,8 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
TaskHandle_t soc_task_hdl;
xTaskCreate( adafruit_soc_task, "SD SOC", CFG_SOC_TASK_STACKSIZE, NULL, TASK_PRIO_HIGH, &soc_task_hdl);
// TaskHandle_t callback_task_hdl;
// xTaskCreate( adafruit_callback_task, "Worker", CFG_CALLBACK_TASK_STACKSIZE, NULL, TASK_PRIO_NORMAL, &callback_task_hdl);
ada_callback_init();
NVIC_SetPriority(SD_EVT_IRQn, 6);
NVIC_EnableIRQ(SD_EVT_IRQn);
@@ -418,16 +416,6 @@ void adafruit_soc_task(void* arg)
}
}
void adafruit_callback_task(void* arg)
{
(void) arg;
while(1)
{
}
}
/*------------------------------------------------------------------*/
/* BLE Event handler
*------------------------------------------------------------------*/
-3
View File
@@ -72,9 +72,6 @@
#include "central_services/BLECentralUart.h"
#include "AdaCallback.h"
#define BLE_MAX_DATA_PER_MTU (GATT_MTU_SIZE_DEFAULT - 3)
extern "C"