adacallback

- peripherl connect callback
- central characteristic notify callback
This commit is contained in:
hathach committed 2017-04-04 12:38:40 +07:00
1 parent a770b995c3
commit aa32c41ff8
9 files changed
+83 -56

No files matched your search

+1 -1
View File
@@ -70,7 +70,7 @@
* 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 VA_ARGS_NUM(...) 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, \
@@ -18,6 +18,15 @@ BLEAncs bleancs;
char buffer[128];
// Check BLEAncs.h for ancsNotification_t
const char* EVENT_STR[] = { "Added", "Modified", "Removed" };
const char* CAT_STR [] =
{
"Other" , "Incoming Call" , "Missed Call", "Voice Mail" ,
"Social" , "Schedule" , "Email" , "News" ,
"Health and Fitness", "Business and Finance", "Location" , "Entertainment"
};
void setup()
{
Serial.begin(115200);
@@ -61,52 +70,27 @@ void loop()
if ( !Bluefruit.connected() ) return ;
// Discover service if not yet
if ( !bleancs.discovered() )
// if ( bleancs.discovered() )
{
Serial.print("Discovering ANCS ... ");
if ( bleancs.discover( Bluefruit.connHandle() ) )
{
Serial.println("Found it");
// ANCS requires pairing to work, it makes sense to request security here as well
Serial.println("Attempt to bond/pair with iOS, please press PAIR on your phone");
if ( Bluefruit.requestPairing() )
{
Serial.println("Enable Notification Source's CCCD");
bleancs.enableNotification();
Serial.println("| Notification | Category (count) | Title | Message | App ID | App Name |");
Serial.println("-------------------------------------------------------------------------------------------------------------");
}
}
}
}
void ancs_notification_callback(ancsNotification_t* notif)
{
int n;
// Check BLEAncs.h for ancsNotification_t
const char* event_str[] = { "Added", "Modified", "Removed" };
const char* cat_str [] =
{
"Other" , "Incoming Call" , "Missed Call", "Voice Mail" ,
"Social" , "Schedule" , "Email" , "News" ,
"Health and Fitness", "Business and Finance", "Location" , "Entertainment"
};
Serial.printf("| %-13s | ", event_str[notif->eventID]);
Serial.printf("| %-13s | ", EVENT_STR[notif->eventID]);
// Print Category with padding
n = Serial.printf("%s (%d)", cat_str[notif->categoryID], notif->categoryCount);
n = Serial.printf("%s (%d)", CAT_STR[notif->categoryID], notif->categoryCount);
for (int i=n; i<20; i++) Serial.print(' ');
Serial.print(" | ");
// Get notification title
// memset(buffer, 0, sizeof(buffer));
// n = bleancs.getAttribute(notif->uid, ANCS_NOTIF_ATTR_TITLE, buffer, sizeof(buffer));
// PRINT_INT(n);
// Serial.print(buffer);
//memset(buffer, 0, sizeof(buffer));
//n = bleancs.getAttribute(notif->uid, ANCS_NOTIF_ATTR_TITLE, buffer, sizeof(buffer));
//PRINT_INT(n);
//Serial.print(buffer);
Serial.println();
}
@@ -114,6 +98,23 @@ void ancs_notification_callback(ancsNotification_t* notif)
void connect_callback(void)
{
Serial.println("Connected");
Serial.print("Discovering ANCS ... ");
if ( bleancs.discover( Bluefruit.connHandle() ) )
{
Serial.println("Found it");
// ANCS requires pairing to work, it makes sense to request security here as well
Serial.println("Attempt to bond/pair with iOS, please press PAIR on your phone");
if ( Bluefruit.requestPairing() )
{
Serial.println("Enable Notification");
bleancs.enableNotification();
Serial.println("| Notification | Category (count) | Title | Message | App ID | App Name |");
Serial.println("-------------------------------------------------------------------------------------------------------------");
}
}
}
void disconnect_callback(uint8_t reason)
+15 -5
View File
@@ -47,19 +47,29 @@ void adafruit_callback_task(void* arg)
ada_callback_t* cb_data;
if ( xQueueReceive(_cb_queue, (void*) &cb_data, portMAX_DELAY) )
{
PRINT_HEX(cb_data);
PRINT_INT(cb_data->callback_type);
PRINT_HEX(cb_data->malloced_data);
void* func = cb_data->callback_func;
uint32_t* args = cb_data->arguments;
switch(cb_data->callback_type)
{
case AdafruitBluefruit_connect_callback_t:
((AdafruitBluefruit::connect_callback_t) func) ();
break;
case BLECentral_connect_callback_t:
{
BLECentral::connect_callback_t func = (BLECentral::connect_callback_t) cb_data->callback_func;
func(cb_data->arguments[0]);
}
((BLECentral::connect_callback_t) func)( (uint16_t) args[0]);
break;
default: VERIFY_MESS(NRF_ERROR_INVALID_PARAM);
case BLECentralCharacteristic_notify_cb_t:
((BLECentralCharacteristic::notify_cb_t) func) ( *((BLECentralCharacteristic*) args[0]), (uint8_t*) args[1], (uint16_t) args[2] );
break;
default: VERIFY_MESS(NRF_ERROR_INVALID_PARAM); break;
}
// free up resource
+19 -11
View File
@@ -39,7 +39,7 @@
#include <Arduino.h>
#include "bluefruit.h"
#define CFG_CALLBACK_TASK_STACKSIZE (512*1)
#define CFG_CALLBACK_TASK_STACKSIZE (512*2)
#define CFG_CALLBACK_QUEUE_LENGTH 10
typedef struct
@@ -61,15 +61,23 @@ 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) \
/* Bluefruit */ \
XPAND(AdafruitBluefruit , connect_callback_t ) \
XPAND(AdafruitBluefruit , disconnect_callback_t ) \
/* Central */ \
XPAND(BLECentral , scan_callback_t ) \
XPAND(BLECentral , connect_callback_t ) \
XPAND(BLECentral , disconnect_callback_t ) \
/* Central Characteristic */ \
XPAND(BLECentralCharacteristic , notify_cb_t ) \
/*XPAND(BLECentralCharacteristic , indicate_cb_t )*/ \
#define ADA_CB_ENUM_XPAND(_class, _cbname) _class##_##_cbname,\
#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; }
static inline uint8_t ada_callback_type(_class::_cbname _func) { (void) _func; return _class##_##_cbname; }
enum
{
@@ -98,15 +106,15 @@ ADA_CB_LOOKUP(ADA_CB_FUNC_XPAND)
*/
#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) ); \
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 = ada_callback_type(_func);\
cb_data->arg_count = count;\
if ( count ) {\
cb_data->arg_count = _count;\
if ( _count ) {\
uint32_t arguments[] = { _ADA_CB_ARGS(__VA_ARGS__) };\
memcpy(cb_data->arguments, arguments, 4*count);\
memcpy(cb_data->arguments, arguments, 4*_count);\
}\
ada_callback_queue(cb_data);\
}while(0)
+1 -1
View File
@@ -52,9 +52,9 @@ class AdafruitBluefruit;
class BLECentral
{
public:
typedef void (*scan_callback_t ) (ble_gap_evt_adv_report_t*);
typedef void (*connect_callback_t ) (uint16_t conn_handle);
typedef void (*disconnect_callback_t ) (uint16_t conn_handle, uint8_t reason);
typedef void (*scan_callback_t ) (ble_gap_evt_adv_report_t*);
BLECentral(void); // Constructor
void begin(void);
@@ -35,6 +35,7 @@
/**************************************************************************/
#include "bluefruit.h"
#include "AdaCallback.h"
#define MAX_DESCIRPTORS 8
@@ -262,7 +263,15 @@ void BLECentralCharacteristic::_eventHandler(ble_evt_t* evt)
if ( hvx->type == BLE_GATT_HVX_NOTIFICATION )
{
if (_notify_cb) _notify_cb(*this, hvx->data, hvx->len);
if (_notify_cb)
{
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
{
@@ -46,6 +46,9 @@ class BLECentralService;
class BLECentralCharacteristic
{
public:
typedef void (*notify_cb_t ) (BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
typedef void (*indicate_cb_t) (BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
BLEUuid uuid;
BLECentralCharacteristic(void);
@@ -75,9 +78,6 @@ class BLECentralCharacteristic
/*------------- Callbacks -------------*/
typedef void (*notify_cb_t ) (BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
typedef void (*indicate_cb_t) (BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
void setNotifyCallback(notify_cb_t fp);
void assign(ble_gattc_char_t* gattc_chr);
+1 -1
View File
@@ -493,7 +493,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
VERIFY_STATUS( sd_ble_gap_conn_param_update(_conn_hdl, NULL), );
}
if ( _connect_cb ) _connect_cb();
if ( _connect_cb ) ada_callback(NULL, _connect_cb);
}
}
break;
@@ -97,7 +97,6 @@ bool BLEAncs::begin(void)
_notification.setNotifyCallback(bleancs_notification_cb);
_data.setNotifyCallback(bleancs_data_cb);
return true;
}