enhance ancs data parser
note: Get Application Attribute does not work yet. Probably due to the write response > 20 bytes
This commit is contained in:
1 parent
ecf9df2fae
commit
c0dedf4f37
4 files changed
+117
-54
No files matched your search
@@ -80,7 +80,7 @@ void ancs_notification_callback(ancsNotification_t* notif)
|
||||
{
|
||||
int n;
|
||||
Serial.printf("| %-8s | ", 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(' ');
|
||||
@@ -96,11 +96,19 @@ void ancs_notification_callback(ancsNotification_t* notif)
|
||||
bleancs.getAttribute(notif->uid, ANCS_ATTR_MESSAGE, buffer, sizeof(buffer));
|
||||
Serial.printf("%-15s | ", buffer);
|
||||
|
||||
// Get App ID
|
||||
// Get App ID and store to app_id
|
||||
char app_id[64] = { 0 };
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
bleancs.getAttribute(notif->uid, ANCS_ATTR_APP_IDENTIFIER, buffer, sizeof(buffer));
|
||||
Serial.printf("%-20s | ", buffer);
|
||||
strcpy(app_id, buffer);
|
||||
Serial.printf("%-20s | ", app_id);
|
||||
|
||||
// Get Application Name ( not work yet)
|
||||
// memset(buffer, 0, sizeof(buffer));
|
||||
// n = bleancs.getAppAttribute(app_id, ANCS_APP_ATTR_DISPLAY_NAME, buffer, sizeof(buffer));
|
||||
// PRINT_INT(n);
|
||||
// Serial.printf("%-20s | ", buffer);
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -117,7 +125,8 @@ void connect_callback(void)
|
||||
Serial.println("Attempt to bond/pair with iOS, please press PAIR on your phone");
|
||||
if ( Bluefruit.requestPairing() )
|
||||
{
|
||||
Serial.println("Enable Notification");
|
||||
Serial.println("Enable Notification");
|
||||
Serial.println();
|
||||
bleancs.enableNotification();
|
||||
|
||||
Serial.println("| Event | Category (count) | Title | Message | App ID | App Name |");
|
||||
|
||||
@@ -158,7 +158,12 @@ uint16_t BLECentralCharacteristic::write_resp(const void* data, int len)
|
||||
.p_value = (uint8_t* ) u8data
|
||||
};
|
||||
|
||||
if ( ERROR_NONE != sd_ble_gattc_write(conn_handle, ¶m) ) break;
|
||||
uint32_t status = sd_ble_gattc_write(conn_handle, ¶m);
|
||||
if ( ERROR_NONE != status )
|
||||
{
|
||||
VERIFY_MESS(status);
|
||||
break;
|
||||
}
|
||||
|
||||
if ( !xSemaphoreTake(_sem, ms2tick(BLE_GENERIC_TIMEOUT) ) ) break;
|
||||
|
||||
@@ -298,7 +303,7 @@ void BLECentralCharacteristic::_eventHandler(ble_evt_t* evt)
|
||||
if (wr_rsp->write_op == BLE_GATT_OP_WRITE_REQ &&_sem)
|
||||
{
|
||||
// TODO check evt->evt.gattc_evt.gatt_status
|
||||
xSemaphoreGive(_sem);
|
||||
if (_sem) xSemaphoreGive(_sem);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
|
||||
#include "bluefruit.h"
|
||||
|
||||
#define BLE_ANCS_TIMEOUT (4*BLE_GENERIC_TIMEOUT)
|
||||
|
||||
void bleancs_notification_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
void bleancs_data_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
|
||||
@@ -88,7 +90,7 @@ bool BLEAncs::begin(void)
|
||||
// Initialize Discovery module if needed
|
||||
if ( !Bluefruit.Discovery.begun() ) Bluefruit.Discovery.begin();
|
||||
|
||||
_sem = xSemaphoreCreateBinary();
|
||||
_sem = xSemaphoreCreateCounting(10, 0);
|
||||
|
||||
_control.begin();
|
||||
_notification.begin();
|
||||
@@ -161,14 +163,10 @@ typedef struct ATTR_PACKED
|
||||
|
||||
VERIFY_STATIC( sizeof(get_notif_attr_t) == 8);
|
||||
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t cmd;
|
||||
|
||||
}get_app_attr_t;
|
||||
|
||||
uint16_t BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_t bufsize)
|
||||
{
|
||||
VERIFY ( attr < ANCS_ATTR_INVALID, 0);
|
||||
|
||||
// command ID | uid | attr (+ len)
|
||||
get_notif_attr_t command =
|
||||
{
|
||||
@@ -185,55 +183,109 @@ uint16_t BLEAncs::getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_
|
||||
cmdlen = 8;
|
||||
}
|
||||
|
||||
// Configure event buffer to hold data
|
||||
_evt_buf = buffer;
|
||||
_evt_bufsize = bufsize;
|
||||
|
||||
VERIFY( cmdlen == _control.write_resp(&command, cmdlen) );
|
||||
// Write command using write response
|
||||
PRINT_BUFFER(&command, cmdlen);
|
||||
VERIFY( cmdlen == _control.write_resp(&command, cmdlen), 0);
|
||||
|
||||
// wait for data arrived (in handleData() )
|
||||
if ( !xSemaphoreTake(_sem, ms2tick(BLE_GENERIC_TIMEOUT*10)) ) return 0;
|
||||
// wait for 1st response's data arrived to parse attribute length
|
||||
if ( !xSemaphoreTake(_sem, ms2tick(BLE_ANCS_TIMEOUT)) ) return 0;
|
||||
|
||||
// calculate actual byte copied
|
||||
bufsize -= _evt_bufsize;
|
||||
uint16_t attr_len = ((get_notif_attr_t*) buffer)->len;
|
||||
|
||||
// wait until all data received
|
||||
// Note (bufsize-_evt_bufsize) is number of bytes received so far
|
||||
while ( ((attr_len + sizeof(get_notif_attr_t)) > (bufsize-_evt_bufsize))
|
||||
&& (_evt_bufsize > 0) )
|
||||
{
|
||||
if ( !xSemaphoreTake(_sem, ms2tick(BLE_ANCS_TIMEOUT)) ) break;
|
||||
}
|
||||
|
||||
uint16_t actual_attrlen = bufsize - _evt_bufsize - sizeof(get_notif_attr_t);
|
||||
|
||||
// Shift out the Command data, left only Attribute data
|
||||
memmove(buffer, buffer+sizeof(get_notif_attr_t), actual_attrlen);
|
||||
|
||||
// including null-terminator for some string application
|
||||
((char*) buffer)[actual_attrlen] = 0;
|
||||
|
||||
// Clear event buffer
|
||||
_evt_buf = NULL;
|
||||
_evt_bufsize = 0;
|
||||
|
||||
return bufsize;
|
||||
return actual_attrlen;
|
||||
|
||||
}
|
||||
|
||||
uint16_t BLEAncs::getAppAttribute(const char* appid, uint8_t attr, void* buffer, uint16_t bufsize)
|
||||
{
|
||||
VERIFY ( attr < ANCS_APP_ATTR_INVALID, 0);
|
||||
|
||||
// command ID | App ID (including Null terminator) | Attr
|
||||
uint8_t cmdlen = 1 + strlen(appid)+1 + 1;
|
||||
uint8_t* command = (uint8_t*) rtos_malloc( cmdlen );
|
||||
|
||||
command[0] = ANCS_CMD_GET_APP_ATTR;
|
||||
strcpy( (char*) command+1, appid);
|
||||
command[cmdlen-1] = attr;
|
||||
|
||||
// Configure event buffer to hold data
|
||||
_evt_buf = buffer;
|
||||
_evt_bufsize = bufsize;
|
||||
|
||||
// Write command using write response
|
||||
uint8_t* appcmd = command;
|
||||
PRINT_BUFFER(appcmd, cmdlen);
|
||||
(void) _control.write_resp(command, cmdlen);
|
||||
rtos_free(command);
|
||||
|
||||
// Phase 1: Get data until Attribute Length is known
|
||||
while ( (cmdlen+2) > (bufsize-_evt_bufsize) &&
|
||||
(_evt_bufsize > 0))
|
||||
{
|
||||
if ( !xSemaphoreTake(_sem, ms2tick(BLE_ANCS_TIMEOUT)) ) return 0;
|
||||
}
|
||||
|
||||
uint16_t attr_len;
|
||||
memcpy(&attr_len, buffer+cmdlen, 2);
|
||||
|
||||
// Phase 2: Get data until all attribute data received
|
||||
// Note (bufsize-_evt_bufsize) is number of bytes received so far
|
||||
while ( (attr_len + cmdlen+2) > (bufsize-_evt_bufsize) &&
|
||||
(_evt_bufsize > 0) )
|
||||
{
|
||||
if ( !xSemaphoreTake(_sem, ms2tick(BLE_ANCS_TIMEOUT)) ) break;
|
||||
}
|
||||
|
||||
uint16_t actual_attrlen = bufsize - _evt_bufsize - (cmdlen+2);
|
||||
|
||||
// Shift out the Command data, left only Attribute data
|
||||
memmove(buffer, buffer+cmdlen+2, actual_attrlen);
|
||||
|
||||
// including null-terminator for some string application
|
||||
((char*) buffer)[actual_attrlen] = 0;
|
||||
|
||||
// Clear event buffer
|
||||
_evt_buf = NULL;
|
||||
_evt_bufsize = 0;
|
||||
|
||||
return actual_attrlen;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
if ( data[0] == ANCS_CMD_GET_NOTIFICATION_ATTR)
|
||||
{
|
||||
attr_len = ((get_notif_attr_t*) data)->len;
|
||||
|
||||
data += sizeof(get_notif_attr_t);
|
||||
len -= sizeof(get_notif_attr_t);
|
||||
}
|
||||
else if ( data[0] == ANCS_CMD_GET_APP_ATTR)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// PRINT_INT(attr_len);
|
||||
PRINT_BUFFER(data, len);
|
||||
|
||||
if ( _evt_bufsize && _evt_buf )
|
||||
{
|
||||
@@ -244,16 +296,8 @@ void BLEAncs::_handleData(uint8_t* data, uint16_t len)
|
||||
_evt_buf += cplen;
|
||||
_evt_bufsize -= cplen;
|
||||
|
||||
// PRINT_INT(cplen);
|
||||
xSemaphoreGive(_sem);
|
||||
}
|
||||
|
||||
attr_len -= len;
|
||||
|
||||
// PRINT_INT(attr_len);
|
||||
// PRINT_INT(_evt_bufsize);
|
||||
|
||||
// all data arrived, signal semaphore
|
||||
if (attr_len == 0) xSemaphoreGive(_sem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -89,7 +89,9 @@ enum
|
||||
ANCS_ATTR_MESSAGE_SIZE ,
|
||||
ANCS_ATTR_DATE , // UTC#35 yyyyMMdd'T'HHmmSS
|
||||
ANCS_ATTR_POSITIVE_ACTION_LABEL ,
|
||||
ANCS_ATTR_NEGATIVE_ACTION_LABEL
|
||||
ANCS_ATTR_NEGATIVE_ACTION_LABEL ,
|
||||
|
||||
ANCS_ATTR_INVALID
|
||||
};
|
||||
|
||||
// Action ID
|
||||
@@ -102,7 +104,9 @@ enum
|
||||
// Application Attribute ID
|
||||
enum
|
||||
{
|
||||
ANCS_APP_ATTR_DISPLAY_NAME
|
||||
ANCS_APP_ATTR_DISPLAY_NAME,
|
||||
|
||||
ANCS_APP_ATTR_INVALID
|
||||
};
|
||||
|
||||
typedef struct
|
||||
@@ -140,6 +144,7 @@ class BLEAncs : public BLECentralService
|
||||
bool disableNotification(void);
|
||||
|
||||
uint16_t getAttribute(uint32_t uid, uint8_t attr, void* buffer, uint16_t bufsize);
|
||||
uint16_t getAppAttribute(const char* appid, uint8_t attr, void* buffer, uint16_t bufsize);
|
||||
|
||||
protected:
|
||||
virtual void disconnect(void);
|
||||
|
||||
Reference in new issue
Block a user