ancs_client: drop ancs_client_event_t

This commit is contained in:
Matthias Ringwald 2016-01-29 22:35:46 +01:00
parent 36f83f4111
commit a481587475
7 changed files with 107 additions and 29 deletions

View File

@ -55,6 +55,7 @@
#include "btstack_run_loop.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "gap.h"
#include "hci.h"
@ -95,9 +96,9 @@ static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *
}
}
static void ancs_callback(ancs_event_t * event){
static void ancs_callback(uint8_t packet_type, uint8_t *packet, uint16_t size){
const char * attribute_name;
switch (event->type){
switch (packet[0]){
case ANCS_CLIENT_CONNECTED:
printf("ANCS Client: Connected\n");
break;
@ -105,9 +106,9 @@ static void ancs_callback(ancs_event_t * event){
printf("ANCS Client: Disconnected\n");
break;
case ANCS_CLIENT_NOTIFICATION:
attribute_name = ancs_client_attribute_name_for_id(event->attribute_id);
attribute_name = ancs_client_attribute_name_for_id(ancs_client_notification_event_get_attribute_id(packet));
if (!attribute_name) break;
printf("Notification: %s - %s\n", attribute_name, event->text);
printf("Notification: %s - %s\n", attribute_name, ancs_client_notification_event_get_text(packet));
break;
default:
break;

View File

@ -3,6 +3,7 @@
#include "ble/att_server.h"
#include "ble/gatt_client.h"
#include "ancs_client.h"
#include "btstack_event.h"
#include "ble/sm.h"
#include <SPI.h>
@ -69,10 +70,11 @@ void loop(void){
* the GATT Client needs to be used direclty.
*/
/* LISTING_START(ANCSCallback): ANCS Callback */
void ancs_callback(ancs_event_t * event){
void ancs_callback(uint8_t packet_type, uint8_t *packet, uint16_t size){
const char * attribute_name;
switch (event->type){
switch (packet[0]){
case ANCS_CLIENT_CONNECTED:
Serial.println("ANCS Client: Connected");
break;
@ -80,12 +82,12 @@ void ancs_callback(ancs_event_t * event){
Serial.println("ANCS Client: Disconnected");
break;
case ANCS_CLIENT_NOTIFICATION:
attribute_name = ancs_client_attribute_name_for_id(event->attribute_id);
attribute_name = ancs_client_attribute_name_for_id(ancs_client_notification_event_get_attribute_id(packet));
if (!attribute_name) break;
Serial.print("Notification: ");
Serial.print(attribute_name);
Serial.print(" - ");
Serial.println(event->text);
Serial.println(ancs_client_notification_event_get_text(packet));
break;
default:
break;

View File

@ -102,20 +102,32 @@ static uint16_t ancs_bytes_received;
static uint16_t ancs_bytes_needed;
static uint8_t ancs_attribute_id;
static uint16_t ancs_attribute_len;
static void (*client_handler)(ancs_event_t * event);
static void (*client_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
void ancs_client_register_callback(void (*handler)(ancs_event_t * event)){
void ancs_client_register_callback(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
client_handler = handler;
}
static void notify_client(int event_type){
static void notify_client_text(int event_type){
if (!client_handler) return;
ancs_event_t event;
event.type = event_type;
event.handle = gc_handle;
event.attribute_id = ancs_attribute_id;
event.text = ancs_notification_buffer;
(*client_handler)(&event);
uint8_t event[6 + sizeof(ancs_notification_buffer) + 1];
event[0] = event_type;
event[1] = 6 + ancs_attribute_len;
bt_store_16(event, 2, gc_handle);
bt_store_16(event, 4, ancs_attribute_id);
memcpy(&event[6], ancs_notification_buffer, ancs_attribute_len);
// we're nice
event[6+ancs_attribute_len] = 0;
(*client_handler)(HCI_EVENT_PACKET, event, event[1] + 2);
}
static void notify_client_simple(int event_type){
if (!client_handler) return;
uint8_t event[4];
event[0] = event_type;
event[1] = 2;
bt_store_16(event, 2, gc_handle);
(*client_handler)(HCI_EVENT_PACKET, event, sizeof(event));
}
static void ancs_chunk_parser_init(void){
@ -152,7 +164,7 @@ static void ancs_chunk_parser_handle_byte(uint8_t data){
break;
case W4_ATTRIBUTE_COMPLETE:
ancs_notification_buffer[ancs_bytes_received] = 0;
notify_client(ANCS_CLIENT_NOTIFICATION);
notify_client_text(ANCS_CLIENT_NOTIFICATION);
ancs_bytes_received = 0;
ancs_bytes_needed = 1;
chunk_parser_state = W4_ATTRIBUTE_ID;
@ -217,7 +229,7 @@ static void handle_gatt_client_event(uint8_t packet_type, uint8_t *packet, uint1
return;
case HCI_EVENT_DISCONNECTION_COMPLETE:
notify_client(ANCS_CLIENT_DISCONNECTED);
notify_client_simple(ANCS_CLIENT_DISCONNECTED);
return;
default:
@ -301,7 +313,7 @@ static void handle_gatt_client_event(uint8_t packet_type, uint8_t *packet, uint1
case GATT_QUERY_COMPLETE:
printf("ANCS Data Source subscribed\n");
tc_state = TC_SUBSCRIBED;
notify_client(ANCS_CLIENT_CONNECTED);
notify_client_simple(ANCS_CLIENT_CONNECTED);
break;
default:
break;

View File

@ -46,15 +46,8 @@ extern "C" {
/* API_START */
typedef struct ancs_event{
uint8_t type;
uint16_t handle;
uint16_t attribute_id;
const char * text;
} ancs_event_t;
void ancs_client_init(void);
void ancs_client_register_callback(void (*handler)(ancs_event_t * event));
void ancs_client_register_callback(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size));
const char * ancs_client_attribute_name_for_id(int id);
/* API_END */

View File

@ -708,8 +708,25 @@
#define HFP_SUBEVENT_MICROPHONE_VOLUME 0x17
// ANCS Client
/**
* @format H
* @param handle
*/
#define ANCS_CLIENT_CONNECTED 0xF0
/**
* @format H2T
* @param handle
* @param attribute_id
* @param text
*/
#define ANCS_CLIENT_NOTIFICATION 0xF1
/**
* @format H
* @param handle
*/
#define ANCS_CLIENT_DISCONNECTED 0xF2
// #define HCI_EVENT_HFP_META 0xxx

View File

@ -167,6 +167,56 @@ static inline uint32_t sdp_query_service_record_handle_event_get_record_handle(c
return READ_BT_32(event, 6);
}
/**
* @brief Get field handle from event ancs_client_connected_event
* @param Event packet
* @return handle
* @note: btstack_type H
*/
static inline hci_con_handle_t ancs_client_connected_event_get_handle(const uint8_t * event){
return READ_BT_16(event, 2);
}
/**
* @brief Get field handle from event ancs_client_notification_event
* @param Event packet
* @return handle
* @note: btstack_type H
*/
static inline hci_con_handle_t ancs_client_notification_event_get_handle(const uint8_t * event){
return READ_BT_16(event, 2);
}
/**
* @brief Get field attribute_id from event ancs_client_notification_event
* @param Event packet
* @return attribute_id
* @note: btstack_type 2
*/
static inline uint16_t ancs_client_notification_event_get_attribute_id(const uint8_t * event){
return READ_BT_16(event, 4);
}
/**
* @brief Get field text from event ancs_client_notification_event
* @param Event packet
* @return text
* @note: btstack_type T
*/
static inline const char * ancs_client_notification_event_get_text(const uint8_t * event){
return (const char *) &event[6];
}
/**
* @brief Get field handle from event ancs_client_disconnected_event
* @param Event packet
* @return handle
* @note: btstack_type H
*/
static inline hci_con_handle_t ancs_client_disconnected_event_get_handle(const uint8_t * event){
return READ_BT_16(event, 2);
}
/* API_END */

View File

@ -207,7 +207,10 @@ def create_events(events):
'SDP_QUERY_COMPLETE',
'SDP_QUERY_RFCOMM_SERVICE',
'SDP_QUERY_ATTRIBUTE_BYTE',
'SDP_QUERY_SERVICE_RECORD_HANDLE']:
'SDP_QUERY_SERVICE_RECORD_HANDLE',
'ANCS_CLIENT_CONNECTED',
'ANCS_CLIENT_NOTIFICATION',
'ANCS_CLIENT_DISCONNECTED']:
continue
event_name = format_function_name(event_name)
length_name = ''