From 1d9d23ddced19ded26c1460684cb7314a56160ac Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 1 Apr 2017 01:17:42 +0700 Subject: [PATCH] start to add callback task --- .../examples/Peripheral/ancs/ancs.ino | 23 ++++++++++++++++--- libraries/Bluefruit52Lib/src/bluefruit.cpp | 18 +++++++++++---- .../Bluefruit52Lib/src/services/BLEAncs.cpp | 20 +++++++++++++--- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino b/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino index 3dd25850..2d285bd9 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino @@ -16,6 +16,8 @@ // BLE Service BLEAncs bleancs; +char buffer[128]; + void setup() { Serial.begin(115200); @@ -71,7 +73,10 @@ void loop() if ( Bluefruit.requestPairing() ) { Serial.println("Enable Notification Source's CCCD"); - bleancs.enableNotification(); + bleancs.enableNotification(); + + Serial.println("| Notification | Category (count) | Title | Message | App ID | App Name |"); + Serial.println("-------------------------------------------------------------------------------------------------------------"); } } } @@ -79,6 +84,8 @@ void loop() 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 [] = @@ -88,8 +95,18 @@ void ancs_notification_callback(ancsNotification_t* notif) "Health and Fitness", "Business and Finance", "Location" , "Entertainment" }; - Serial.printf("[Notification %s] ", event_str[notif->eventID]); - Serial.printf("%s (%d) ", cat_str[notif->categoryID], notif->categoryCount); + Serial.printf("| %-13s | ", event_str[notif->eventID]); + + // Print Category with padding + 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); Serial.println(); } diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index d5bc9a03..3c355b75 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -44,7 +44,7 @@ #define CFG_ADV_BLINKY_INTERVAL 500 #define CFG_BLE_TASK_STACKSIZE (512*3) -#define CFG_WORKER_TASK_STACKSIZE (512*1) +#define CFG_CALLBACK_TASK_STACKSIZE (512*1) #define CFG_SOC_TASK_STACKSIZE (200) #define CFG_BOND_NFFS_DIR "/adafruit/bond" @@ -63,8 +63,7 @@ extern "C" void adafruit_ble_task(void* arg); void adafruit_soc_task(void* arg); - -COMMENT_OUT( void adafruit_worker_task(void* arg); ) +void adafruit_callback_task(void* arg); /*------------------------------------------------------------------*/ @@ -212,6 +211,9 @@ 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); + NVIC_SetPriority(SD_EVT_IRQn, 6); NVIC_EnableIRQ(SD_EVT_IRQn); @@ -416,7 +418,15 @@ void adafruit_soc_task(void* arg) } } -COMMENT_OUT(void adafruit_worker_task(void* arg) { } ) +void adafruit_callback_task(void* arg) +{ + (void) arg; + + while(1) + { + + } +} /*------------------------------------------------------------------*/ /* BLE Event handler diff --git a/libraries/Bluefruit52Lib/src/services/BLEAncs.cpp b/libraries/Bluefruit52Lib/src/services/BLEAncs.cpp index d1ad9c0b..7feda53d 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAncs.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAncs.cpp @@ -164,7 +164,7 @@ typedef struct ATTR_PACKED }get_app_attr_t; -bool BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_t bufsize) +uint16_t BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_t bufsize) { // command ID | uid | attr (+ len) get_notif_attr_t command = @@ -188,18 +188,23 @@ bool BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_t bu VERIFY( cmdlen == _control.write_resp(&command, cmdlen) ); // wait for data arrived (in handleData() ) - if ( !xSemaphoreTake(_sem, BLE_GENERIC_TIMEOUT) ) return false; + if ( !xSemaphoreTake(_sem, ms2tick(BLE_GENERIC_TIMEOUT*10)) ) return 0; + + // calculate actual byte copied + bufsize -= _evt_bufsize; _evt_buf = NULL; _evt_bufsize = 0; - return true; + return bufsize; } void BLEAncs::_handleNotification(uint8_t* data, uint16_t len) { if ( len != 8 ) return; + PRINT_BUFFER(data, len); + if ( _notif_cb ) _notif_cb((ancsNotification_t*) data); } @@ -207,6 +212,8 @@ void BLEAncs::_handleData(uint8_t* data, uint16_t len) { static uint16_t attr_len = 0; + PRINT_BUFFER(data, len); + // Parse Attribute Length if ( attr_len == 0 ) { @@ -223,6 +230,8 @@ void BLEAncs::_handleData(uint8_t* data, uint16_t len) } } + PRINT_INT(attr_len); + if ( _evt_bufsize && _evt_buf ) { uint16_t cplen = min16(len, _evt_bufsize); @@ -231,10 +240,15 @@ void BLEAncs::_handleData(uint8_t* data, uint16_t len) _evt_buf += cplen; _evt_bufsize -= cplen; + + PRINT_INT(cplen); } attr_len -= len; + PRINT_INT(attr_len); + PRINT_INT(_evt_bufsize); + // all data arrived, signal semaphore if (attr_len == 0) xSemaphoreGive(_sem); }