update ancs_client_lib and embedded examples for new gatt client events

This commit is contained in:
Matthias Ringwald 2015-10-15 15:15:54 +02:00
parent 8e945f2ed4
commit 4f8c7aafb7
6 changed files with 222 additions and 948 deletions

View File

@ -43,6 +43,7 @@
#include <string.h>
#include <btstack/run_loop.h>
#include <btstack/sdp_util.h>
#include "ancs_client_lib.h"
@ -159,9 +160,30 @@ static void ancs_chunk_parser_handle_byte(uint8_t data){
}
}
static void handle_gatt_client_event(le_event_t * event){
static void extract_service(le_service_t * service, uint8_t * packet){
service->start_group_handle = READ_BT_16(packet, 4);
service->end_group_handle = READ_BT_16(packet, 6);
service->uuid16 = 0;
swap128(&packet[8], service->uuid128);
if (sdp_has_blueooth_base_uuid(service->uuid128)){
service->uuid16 = READ_NET_32(service->uuid128, 0);
}
}
static void extract_characteristic(le_characteristic_t * characteristic, uint8_t * packet){
characteristic->start_handle = READ_BT_16(packet, 4);
characteristic->value_handle = READ_BT_16(packet, 6);
characteristic->end_handle = READ_BT_16(packet, 8);
characteristic->properties = READ_BT_16(packet, 10);
characteristic->uuid16 = 0;
swap128(&packet[12], characteristic->uuid128);
if (sdp_has_blueooth_base_uuid(characteristic->uuid128)){
characteristic->uuid16 = READ_NET_32(characteristic->uuid128, 0);
}
}
static void handle_gatt_client_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
uint8_t * packet = (uint8_t*) event;
int connection_encrypted;
// handle connect / disconncet events first
@ -203,12 +225,15 @@ static void handle_gatt_client_event(le_event_t * event){
}
le_characteristic_t characteristic;
le_characteristic_value_event_t * value_event;
uint8_t * value;
uint16_t value_handle;
uint16_t value_length;
switch(tc_state){
case TC_W4_SERVICE_RESULT:
switch(event->type){
switch(packet[0]){
case GATT_SERVICE_QUERY_RESULT:
ancs_service = ((le_service_event_t *) event)->service;
extract_service(&ancs_service, packet);
ancs_service_found = 1;
break;
case GATT_QUERY_COMPLETE:
@ -227,9 +252,9 @@ static void handle_gatt_client_event(le_event_t * event){
break;
case TC_W4_CHARACTERISTIC_RESULT:
switch(event->type){
switch(packet[0]){
case GATT_CHARACTERISTIC_QUERY_RESULT:
characteristic = ((le_characteristic_event_t *) event)->characteristic;
extract_characteristic(&characteristic, packet);
if (memcmp(characteristic.uuid128, ancs_notification_source_uuid, 16) == 0){
printf("ANCS Notification Source Characterisic found\n");
ancs_notification_source_characteristic = characteristic;
@ -260,7 +285,7 @@ static void handle_gatt_client_event(le_event_t * event){
}
break;
case TC_W4_NOTIFICATION_SOURCE_SUBSCRIBED:
switch(event->type){
switch(packet[0]){
case GATT_QUERY_COMPLETE:
printf("ANCS Notification Source subscribed\n");
tc_state = TC_W4_DATA_SOURCE_SUBSCRIBED;
@ -272,7 +297,7 @@ static void handle_gatt_client_event(le_event_t * event){
}
break;
case TC_W4_DATA_SOURCE_SUBSCRIBED:
switch(event->type){
switch(packet[0]){
case GATT_QUERY_COMPLETE:
printf("ANCS Data Source subscribed\n");
tc_state = TC_SUBSCRIBED;
@ -283,17 +308,21 @@ static void handle_gatt_client_event(le_event_t * event){
}
break;
case TC_SUBSCRIBED:
if ( event->type != GATT_NOTIFICATION && event->type != GATT_INDICATION ) break;
value_event = (le_characteristic_value_event_t *) event;
if (value_event->value_handle == ancs_data_source_characteristic.value_handle){
if (packet[0] != GATT_NOTIFICATION && packet[0] != GATT_INDICATION ) break;
value_handle = READ_BT_16(packet, 4);
value_length = READ_BT_16(packet, 6);
value = &packet[8];
if (value_handle == ancs_data_source_characteristic.value_handle){
int i;
for (i=0;i<value_event->blob_length;i++) {
ancs_chunk_parser_handle_byte(value_event->blob[i]);
for (i=0;i<value_length;i++) {
ancs_chunk_parser_handle_byte(value[i]);
}
} else if (value_event->value_handle == ancs_notification_source_characteristic.value_handle){
ancs_notification_uid = READ_BT_32(value_event->blob, 4);
} else if (value_handle == ancs_notification_source_characteristic.value_handle){
ancs_notification_uid = READ_BT_32(value, 4);
printf("Notification received: EventID %02x, EventFlags %02x, CategoryID %02x, CategoryCount %u, UID %04x\n",
value_event->blob[0], value_event->blob[1], value_event->blob[2], value_event->blob[3], (int) ancs_notification_uid);
value[0], value[1], value[2], value[3], (int) ancs_notification_uid);
static uint8_t get_notification_attributes[] = {0, 0,0,0,0, 0, 1,32,0, 2,32,0, 3,32,0, 4, 5};
bt_store_32(get_notification_attributes, 1, ancs_notification_uid);
ancs_notification_uid = 0;
@ -302,7 +331,7 @@ static void handle_gatt_client_event(le_event_t * event){
sizeof(get_notification_attributes), get_notification_attributes);
} else {
printf("Unknown Source: ");
printf_hexdump(value_event->blob , value_event->blob_length);
printf_hexdump(value , value_length);
}
break;
default:

View File

@ -161,9 +161,6 @@ ancs_client: ancs_client.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SERVER_OB
led_counter: ${CORE_OBJ} ${COMMON_OBJ} led_counter.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
ble_client: ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_CLIENT_OBJ} ${SM_REAL} ble_client.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
gap_le_advertisements: ${CORE_OBJ} ${COMMON_OBJ} ad_parser.c gap_le_advertisements.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@

View File

@ -1,836 +0,0 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
// *****************************************************************************
//
// BLE Client
//
// *****************************************************************************
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <btstack/run_loop.h>
#include <btstack/hci_cmds.h>
#include <btstack/utils.h>
#include <btstack/sdp_util.h>
#include "btstack-config.h"
#include "ad_parser.h"
#include "debug.h"
#include "btstack_memory.h"
#include "hci.h"
#include "hci_dump.h"
#include "l2cap.h"
#include "att.h"
#include "gatt_client.h"
typedef struct ad_event {
uint8_t type;
uint8_t event_type;
uint8_t address_type;
bd_addr_t address;
uint8_t rssi;
uint8_t length;
uint8_t * data;
} ad_event_t;
void (*ble_client_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = NULL;
static void ble_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
void ble_client_init(void){
gatt_client_init();
l2cap_register_packet_handler(ble_packet_handler);
}
void ble_client_register_gatt_client_event_packet_handler(void (*le_callback)(le_event_t* event)){
gatt_client_register_packet_handler(le_callback);
}
void ble_client_register_hci_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
ble_client_packet_handler = handler;
}
static void ble_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return;
// forward to app
if (!ble_client_packet_handler) return;
ble_client_packet_handler(packet_type, packet, size);
}
// TEST CODE
static void printUUID(uint8_t * uuid128, uint16_t uuid16){
printf(", uuid ");
if (uuid16){
printf(" 0x%02x",uuid16);
} else {
printUUID128(uuid128);
}
printf("\n");
}
static void dump_characteristic(le_characteristic_t * characteristic){
printf(" *** characteristic *** properties %x, start handle 0x%02x, value handle 0x%02x, end handle 0x%02x",
characteristic->properties, characteristic->start_handle, characteristic->value_handle, characteristic->end_handle);
printUUID(characteristic->uuid128, characteristic->uuid16);
}
static void dump_ad_event(ad_event_t * e){
printf(" *** adv. event *** evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", e->event_type,
e->address_type, bd_addr_to_str(e->address), e->rssi, e->length);
printf_hexdump(e->data, e->length);
}
static void dump_service(le_service_t * service){
printf(" *** service *** start group handle %02x, end group handle %02x", service->start_group_handle, service->end_group_handle);
printUUID(service->uuid128, service->uuid16);
}
static void dump_descriptor(le_event_t * event){
le_characteristic_descriptor_event_t * devent = (le_characteristic_descriptor_event_t *) event;
le_characteristic_descriptor_t descriptor = devent->characteristic_descriptor;
printf(" *** descriptor *** handle 0x%02x, value ", descriptor.handle);
printf_hexdump(devent->value, devent->value_length);
printUUID(descriptor.uuid128, descriptor.uuid16);
}
static void dump_characteristic_value(le_characteristic_value_event_t * event){
printf(" *** characteristic value of length %d *** ", event->blob_length );
printf_hexdump(event->blob , event->blob_length);
printf("\n");
}
gatt_client_t test_gatt_client_context;
uint16_t test_gatt_client_handle;
le_service_t services[40];
le_characteristic_t characteristics[100];
le_service_t service;
le_characteristic_t enable_characteristic;
le_characteristic_t config_characteristic;
le_characteristic_descriptor_t descriptor;
int service_count = 0;
int service_index = 0;
int characteristic_count = 0;
int characteristic_index = 0;
uint8_t acc_service_uuid[16] = {0xf0, 0x00, 0xaa, 0x10, 0x04, 0x51, 0x40, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t acc_chr_client_config_uuid[16] = {0xf0, 0x00, 0xaa, 0x11, 0x04, 0x51, 0x40, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t acc_chr_enable_uuid[16] = {0xf0, 0x00, 0xaa, 0x12, 0x04, 0x51, 0x40, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t acc_enable[1] = {0x01};
uint8_t acc_disable[1] = {0x00};
static uint8_t test_device_addr_type = 0;
// pick one:
//static bd_addr_t test_device_addr = {0x00, 0x1b, 0xdc, 0x05, 0xb5, 0xdc};
static bd_addr_t sensor_tag1_addr = {0x1c, 0xba, 0x8c, 0x20, 0xc7, 0xf6};// SensorTag 1
static bd_addr_t sensor_tag2_addr = {0x34, 0xb1, 0xf7, 0xd1, 0x77, 0x9b}; // SensorTag 2
// static bd_addr_t test_device_addr = {0x00, 0x02, 0x72, 0xdc, 0x31, 0xc1}; // delock40
typedef enum {
TC_IDLE,
TC_W4_SCAN_RESULT,
TC_W4_CONNECT,
TC_W4_SERVICE_RESULT,
TC_W4_SERVICE_WITH_UUID_RESULT,
TC_W4_CHARACTERISTIC_RESULT,
TC_W4_CHARACTERISTIC_WITH_UUID_RESULT,
TC_W4_CHARACTERISTIC_DESCRIPTOR_RESULT,
TC_W4_INCLUDED_SERVICE_RESULT,
TC_W4_READ_RESULT,
TC_W4_READ_LONG_RESULT,
TC_W2_WRITE_WITHOUT_RESPONSE,
TC_W4_WRITE_WITHOUT_RESPONSE_SENT,
TC_W4_WRITE_RESULT,
TC_W4_LONG_WRITE_RESULT,
TC_W4_RELIABLE_WRITE_RESULT,
TC_W4_ACC_ENABLE,
TC_W4_ACC_CLIENT_CONFIG_CHARACTERISTIC_RESULT,
TC_W4_ACC_SUBSCRIBE,
TC_W4_ACC_DATA,
TC_W4_DISCONNECT,
TC_DISCONNECTED
} tc_state_t;
tc_state_t tc_state = TC_IDLE;
le_characteristic_t characteristic;
uint8_t chr_long_value[26] = {
0x76, 0x75, 0x74, 0x73, 0x72,
0x71, 0x70, 0x6f, 0x6e, 0x6d,
0x6c, 0x6b, 0x6a, 0x69, 0x68,
0x67, 0x66, 0x65, 0x64, 0x63,
0x62, 0x61, 0x60, 0x5f, 0x5e,
0x5d};
uint8_t chr_short_value[1] = {0x86};
// void sm_test_basic_cmds(le_event_t * event){
// le_service_t service;
// le_characteristic_descriptor_t descriptor;
// switch(tc_state){
// case TC_W4_CONNECT:
// if (event->type != GATT_CONNECTION_COMPLETE) break;
// tc_state = TC_W4_SERVICE_RESULT;
// printf("\n test client - CONNECTED, query services now\n");
// le_central_discover_primary_services(&test_gatt_client_context);
// break;
// case TC_W4_SERVICE_RESULT:
// switch(event->type){
// case GATT_SERVICE_QUERY_RESULT:
// service = ((le_service_event_t *) event)->service;
// dump_service(&service);
// services[service_count++] = service;
// break;
// case GATT_SERVICE_QUERY_COMPLETE:
// tc_state = TC_W4_SERVICE_WITH_UUID_RESULT;
// printf("\n test client - SERVICE by SERVICE UUID QUERY\n");
// le_central_discover_primary_services_by_uuid16(&test_gatt_client_context, 0xffff);
// break;
// default:
// break;
// }
// break;
// case TC_W4_SERVICE_WITH_UUID_RESULT:
// switch(event->type){
// case GATT_SERVICE_QUERY_RESULT:
// service = ((le_service_event_t *) event)->service;
// dump_service(&service);
// break;
// case GATT_SERVICE_QUERY_COMPLETE:
// tc_state = TC_W4_CHARACTERISTIC_RESULT;
// service_index = 0;
// printf("\n test client - CHARACTERISTIC for SERVICE 0x%02x QUERY\n", services[service_index].uuid16);
// le_central_discover_characteristics_for_service(&test_gatt_client_context, &services[service_index]);
// break;
// default:
// break;
// }
// break;
// case TC_W4_CHARACTERISTIC_RESULT:
// switch(event->type){
// case GATT_CHARACTERISTIC_QUERY_RESULT:
// characteristic = ((le_characteristic_event_t *) event)->characteristic;
// dump_characteristic(&characteristic);
// characteristics[characteristic_count++] = characteristic;
// break;
// case GATT_CHARACTERISTIC_QUERY_COMPLETE:
// if (service_index < service_count) {
// tc_state = TC_W4_CHARACTERISTIC_RESULT;
// service = services[service_index++];
// printf("\n test client - CHARACTERISTIC for SERVICE 0x%02x QUERY\n", service.uuid16);
// le_central_discover_characteristics_for_service(&test_gatt_client_context, &service);
// break;
// }
// tc_state = TC_W4_CHARACTERISTIC_WITH_UUID_RESULT;
// service_index = 0;
// characteristic_index = 0;
// printf("\n test client - CHARACTERISTIC for SERVICE 0x%02x QUERY with UUID 0x%02x\n",
// services[0].uuid16, characteristics[0].uuid16);
// le_central_discover_characteristics_for_handle_range_by_uuid16(&test_gatt_client_context,
// services[0].start_group_handle, services[0].end_group_handle, characteristics[0].uuid16);
// break;
// default:
// break;
// }
// break;
// case TC_W4_CHARACTERISTIC_WITH_UUID_RESULT:
// switch(event->type){
// case GATT_CHARACTERISTIC_QUERY_RESULT:
// characteristic = ((le_characteristic_event_t *) event)->characteristic;
// dump_characteristic(&characteristic);
// break;
// case GATT_CHARACTERISTIC_QUERY_COMPLETE:
// tc_state = TC_W4_CHARACTERISTIC_DESCRIPTOR_RESULT;
// printf("\n test client - DESCRIPTOR for CHARACTERISTIC \n");
// dump_characteristic(&characteristics[0]);
// le_central_discover_characteristic_descriptors(&test_gatt_client_context, &characteristics[0]);
// break;
// default:
// break;
// }
// break;
// case TC_W4_CHARACTERISTIC_DESCRIPTOR_RESULT:
// switch(event->type){
// case GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
// descriptor = ((le_characteristic_descriptor_event_t *) event)->characteristic_descriptor;
// dump_descriptor(&descriptor);
// break;
// case GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_COMPLETE:
// service_index = 0;
// service = services[service_index];
// tc_state = TC_W4_INCLUDED_SERVICE_RESULT;
// printf("\n test client - INCLUDED SERVICE QUERY, for service %02x\n", service.uuid16);
// le_central_find_included_services_for_service(&test_gatt_client_context, &service);
// break;
// default:
// break;
// }
// break;
// case TC_W4_INCLUDED_SERVICE_RESULT:
// switch(event->type){
// case GATT_INCLUDED_SERVICE_QUERY_RESULT:
// service = ((le_service_event_t *) event)->service;
// printf(" *** found included service *** start group handle %02x, end group handle %02x \n", service.start_group_handle, service.end_group_handle);
// break;
// case GATT_INCLUDED_SERVICE_QUERY_COMPLETE:
// if (service_index < service_count) {
// service = services[service_index++];
// printf("\n test client - INCLUDED SERVICE QUERY, for service %02x\n", service.uuid16);
// le_central_find_included_services_for_service(&test_gatt_client_context, &service);
// break;
// }
// tc_state = TC_W4_DISCONNECT;
// printf("\n\n test client - DISCONNECT ");
// le_central_disconnect(&test_device);
// break;
// default:
// break;
// }
// break;
// default:
// break;
// }
// }
// void sm_test_reads(le_event_t * event){
// switch(tc_state){
// // 59 5a 5d f200
// case TC_W4_CONNECT:
// if (event->type != GATT_CONNECTION_COMPLETE) break;
// tc_state = TC_W4_CHARACTERISTIC_WITH_UUID_RESULT;
// printf("\n test client - CONNECTED, query characteristic now\n");
// le_central_discover_characteristics_for_handle_range_by_uuid16(&test_gatt_client_context, 0x59, 0x5d, 0xf200);
// break;
// case TC_W4_CHARACTERISTIC_WITH_UUID_RESULT:
// switch(event->type){
// case GATT_CHARACTERISTIC_QUERY_RESULT:
// characteristic = ((le_characteristic_event_t *) event)->characteristic;
// dump_characteristic(&characteristic);
// break;
// case GATT_CHARACTERISTIC_QUERY_COMPLETE:
// tc_state = TC_W4_READ_LONG_RESULT;
// printf("\n\n test client - LONG VALUE for CHARACTERISTIC \n");
// le_central_write_value_of_characteristic_without_response(&test_gatt_client_context, characteristic.value_handle, 1, chr_short_value);
// break;
// default:
// break;
// }
// break;
// case TC_W4_READ_LONG_RESULT:
// switch (event->type){
// case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
// dump_characteristic_value((le_characteristic_value_event_t *)event);
// break;
// case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_COMPLETE:
// tc_state = TC_W4_DISCONNECT;
// printf("\n\n test client - DISCONNECT ");
// le_central_disconnect(&test_device);
// break;
// default:
// printf("TC_W4_READ_LONG_RESULT\n");
// break;
// }
// break;
// default:
// break;
// }
// }
// void sm_test_write_no_response(le_event_t * event){
// switch(tc_state){
// case TC_W4_SCAN_RESULT: {
// if (event->type != GATT_ADVERTISEMENT) break;
// printf("test client - SCAN ACTIVE\n");
// ad_event_t * ad_event = (ad_event_t*) event;
// dump_ad_event(ad_event);
// // copy found addr
// test_device_addr_type = ad_event->address_type;
// bd_addr_t found_device_addr;
// memcpy(found_device_addr, ad_event->address, 6);
// if (memcmp(&found_device_addr, &test_device_addr, 6) != 0) break;
// // memcpy(test_device_addr, ad_event->address, 6);
// tc_state = TC_W4_CONNECT;
// le_central_stop_scan();
// le_central_connect(test_device, test_device_addr_type, test_device_addr);
// break;
// }
// // 59 5a 5d f200
// case TC_W4_CONNECT:
// if (event->type != GATT_CONNECTION_COMPLETE) break;
// tc_state = TC_W4_CHARACTERISTIC_WITH_UUID_RESULT;
// printf("\n test client - CONNECTED, query characteristic now\n");
// le_central_discover_characteristics_for_handle_range_by_uuid16(&test_gatt_client_context, 0x59, 0x5d, 0xf200);
// break;
// case TC_W4_CHARACTERISTIC_WITH_UUID_RESULT:
// switch(event->type){
// case GATT_CHARACTERISTIC_QUERY_RESULT:
// characteristic = ((le_characteristic_event_t *) event)->characteristic;
// dump_characteristic(&characteristic);
// break;
// case GATT_CHARACTERISTIC_QUERY_COMPLETE:{
// printf("\n\n test client - Write VALUE for CHARACTERISTIC \n");
// tc_state = TC_W2_WRITE_WITHOUT_RESPONSE;
// le_command_status_t status = le_central_write_value_of_characteristic_without_response(&test_gatt_client_context, characteristic.value_handle, 1, chr_short_value);
// if (status != BLE_PERIPHERAL_OK) break;
// tc_state = TC_W4_READ_LONG_RESULT;
// le_central_read_long_value_of_characteristic(&test_gatt_client_context, &characteristic);
// break;
// }
// default:
// break;
// }
// break;
// case TC_W4_READ_LONG_RESULT:
// switch (event->type){
// case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
// dump_characteristic_value((le_characteristic_value_event_t *)event);
// break;
// case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_COMPLETE:
// tc_state = TC_W4_DISCONNECT;
// printf("\n\n test client - DISCONNECT ");
// le_central_disconnect(&test_device);
// break;
// default:
// printf("TC_W4_READ_LONG_RESULT\n");
// break;
// }
// break;
// default:
// break;
// }
// }
// void sm_test_long_writes(le_event_t * event){
// switch(tc_state){
// case TC_W4_CONNECT:
// if (event->type != GATT_CONNECTION_COMPLETE) break;
// tc_state = TC_W4_CHARACTERISTIC_WITH_UUID_RESULT;
// printf("\n test client - CONNECTED, query characteristic now\n");
// le_central_discover_characteristics_for_handle_range_by_uuid16(&test_gatt_client_context, 0x59, 0x5d, 0xf200);
// break;
// case TC_W4_CHARACTERISTIC_WITH_UUID_RESULT:
// switch(event->type){
// case GATT_CHARACTERISTIC_QUERY_RESULT:
// characteristic = ((le_characteristic_event_t *) event)->characteristic;
// dump_characteristic(&characteristic);
// break;
// case GATT_CHARACTERISTIC_QUERY_COMPLETE:{
// printf("\n\n test client - Write LONG VALUE for CHARACTERISTIC \n");
// tc_state = TC_W4_WRITE_RESULT;
// // le_central_write_value_of_characteristic(&test_gatt_client_context, characteristic.value_handle, 1, chr_short_value);
// // le_central_write_long_value_of_characteristic(&test_gatt_client_context, characteristic.value_handle, 26, chr_long_value);
// le_central_reliable_write_long_value_of_characteristic(&test_gatt_client_context, characteristic.value_handle, 26, chr_long_value);
// break;
// }
// default:
// break;
// }
// break;
// case TC_W4_WRITE_RESULT:
// printf("\n\n test client - Write LONG VALUE for CHARACTERISTIC \n");
// tc_state = TC_W4_READ_LONG_RESULT;
// le_central_read_long_value_of_characteristic(&test_gatt_client_context, &characteristic);
// break;
// case TC_W4_READ_LONG_RESULT:
// switch (event->type){
// case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
// dump_characteristic_value((le_characteristic_value_event_t *)event);
// break;
// case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_COMPLETE:
// tc_state = TC_W4_DISCONNECT;
// printf("\n\n test client - DISCONNECT ");
// le_central_disconnect(&test_device);
// break;
// default:
// printf("TC_W4_READ_LONG_RESULT\n");
// break;
// }
// break;
// default:
// printf("Client, unhandled state %d\n", tc_state);
// break;
// }
// }
//
//void sm_test_read_characteristic_descriptor(le_event_t * event){
// switch(tc_state){
// case TC_W4_CONNECT:
// if (event->type != GATT_CONNECTION_COMPLETE) break;
// tc_state = TC_W4_SERVICE_RESULT;
// printf("\n test client - CONNECTED, query ACC service\n");
// le_central_discover_primary_services_by_uuid128(&test_gatt_client_context, acc_service_uuid);
// break;
//
// case TC_W4_SERVICE_RESULT:
// switch(event->type){
// case GATT_SERVICE_QUERY_RESULT:
// service = ((le_service_event_t *) event)->service;
// dump_service(&service);
// break;
// case GATT_SERVICE_QUERY_COMPLETE:
// tc_state = TC_W4_CHARACTERISTIC_RESULT;
// printf("\n test client - FIND ENABLE CHARACTERISTIC for SERVICE QUERY : \n");
// dump_service(&service);
// le_central_discover_characteristics_for_service_by_uuid128(&test_gatt_client_context, &service, acc_chr_enable_uuid);
// break;
// default:
// break;
// }
// break;
//
// case TC_W4_CHARACTERISTIC_RESULT:
// switch(event->type){
// case GATT_CHARACTERISTIC_QUERY_RESULT:
// enable_characteristic = ((le_characteristic_event_t *) event)->characteristic;
// dump_characteristic(&enable_characteristic);
// break;
// case GATT_CHARACTERISTIC_QUERY_COMPLETE:
// tc_state = TC_W4_CHARACTERISTIC_DESCRIPTOR_RESULT;
// printf("\n test client - ACC ENABLE\n");
// le_central_discover_characteristic_descriptors(&test_gatt_client_context, &enable_characteristic);
// break;
// default:
// break;
// }
// break;
//
// case TC_W4_CHARACTERISTIC_DESCRIPTOR_RESULT:
// switch(event->type){
// case GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
// descriptor = ((le_characteristic_descriptor_event_t *) event)->characteristic_descriptor;
// dump_descriptor(&descriptor);
// break;
// case GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_COMPLETE:
// le_central_read_characteristic_descriptor(&test_gatt_client_context, &descriptor);
// break;
// case GATT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:{
// descriptor = ((le_characteristic_descriptor_event_t *) event)->characteristic_descriptor;
// dump_descriptor(&descriptor);
// tc_state = TC_W4_DISCONNECT;
// printf("\n\n test client - DISCONNECT ");
// le_central_disconnect(&test_device);
// break;
// }
// default:
// break;
// }
// break;
//
// default:
// printf("Client, unhandled state %d\n", tc_state);
// break;
// }
//
//}
//static void handle_gatt_client_event(le_event_t * event){
// switch(tc_state){
// case TC_W4_SERVICE_RESULT:
// switch(event->type){
// case GATT_SERVICE_QUERY_RESULT:
// service = ((le_service_event_t *) event)->service;
// dump_service(&service);
// break;
// case GATT_QUERY_COMPLETE:
// tc_state = TC_W4_CHARACTERISTIC_RESULT;
// printf("\n test client - FIND ENABLE CHARACTERISTIC for SERVICE QUERY : \n");
// dump_service(&service);
// gatt_client_discover_characteristics_for_service_by_uuid128(&test_gatt_client_context, &service, acc_chr_enable_uuid);
// break;
// default:
// break;
// }
// break;
//
// case TC_W4_CHARACTERISTIC_RESULT:
// switch(event->type){
// case GATT_CHARACTERISTIC_QUERY_RESULT:
// enable_characteristic = ((le_characteristic_event_t *) event)->characteristic;
// dump_characteristic(&enable_characteristic);
// break;
// case GATT_QUERY_COMPLETE:
// tc_state = TC_W4_CHARACTERISTIC_DESCRIPTOR_RESULT;
// printf("\n test client - ACC ENABLE\n");
// gatt_client_discover_characteristic_descriptors(&test_gatt_client_context, &enable_characteristic);
// break;
// default:
// break;
// }
// break;
//
// case TC_W4_CHARACTERISTIC_DESCRIPTOR_RESULT:
// switch(event->type){
// case GATT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
// dump_descriptor(event);
// break;
//
// case GATT_QUERY_COMPLETE:
// tc_state = TC_W4_DISCONNECT;
// printf("\n\n test client - DISCONNECT ");
// gap_disconnect(test_gatt_client_handle);
// break;
//
// default:
// break;
// }
// break;
//
// default:
// printf("Client, unhandled state %d\n", tc_state);
// break;
// }
//
//
//}
void handle_gatt_client_event(le_event_t * event){
switch(tc_state){
case TC_W4_SERVICE_RESULT:
switch(event->type){
case GATT_SERVICE_QUERY_RESULT:
service = ((le_service_event_t *) event)->service;
dump_service(&service);
break;
case GATT_QUERY_COMPLETE:
tc_state = TC_W4_CHARACTERISTIC_RESULT;
printf("\n test client - ENABLE CHARACTERISTIC for SERVICE QUERY 1: \n");
dump_service(&service);
gatt_client_discover_characteristics_for_service_by_uuid128(&test_gatt_client_context, &service, acc_chr_enable_uuid);
break;
default:
break;
}
break;
case TC_W4_CHARACTERISTIC_RESULT:
switch(event->type){
case GATT_CHARACTERISTIC_QUERY_RESULT:
enable_characteristic = ((le_characteristic_event_t *) event)->characteristic;
dump_characteristic(&enable_characteristic);
break;
case GATT_QUERY_COMPLETE:
tc_state = TC_W4_ACC_ENABLE;
printf("\n test client - ACC ENABLE\n");
gatt_client_write_value_of_characteristic(&test_gatt_client_context, enable_characteristic.value_handle, 1, acc_enable);
break;
default:
break;
}
break;
case TC_W4_ACC_ENABLE:
tc_state = TC_W4_ACC_CLIENT_CONFIG_CHARACTERISTIC_RESULT;
printf("\n test client - CLIENT CONFIG CHARACTERISTIC for SERVICE QUERY with UUID");
printUUID128(service.uuid128);
printf("\n");
gatt_client_discover_characteristics_for_service_by_uuid128(&test_gatt_client_context, &service, acc_chr_client_config_uuid);
break;
case TC_W4_ACC_CLIENT_CONFIG_CHARACTERISTIC_RESULT:
switch(event->type){
case GATT_CHARACTERISTIC_QUERY_RESULT:
config_characteristic = ((le_characteristic_event_t *) event)->characteristic;
dump_characteristic(&config_characteristic);
break;
case GATT_QUERY_COMPLETE:
tc_state = TC_W4_ACC_DATA;
printf("\n test client - ACC Client Configuration\n");
gatt_client_write_client_characteristic_configuration(&test_gatt_client_context, &config_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
break;
default:
break;
}
break;
case TC_W4_ACC_DATA:
printf("ACC Client Data: ");
if ( event->type != GATT_NOTIFICATION && event->type != GATT_INDICATION ) break;
dump_characteristic_value((le_characteristic_value_event_t *) event);
break;
default:
printf("Client, unhandled state %d\n", tc_state);
break;
}
}
static void handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
le_command_status_t status;
if (packet_type != HCI_EVENT_PACKET) return;
switch (packet[0]) {
case HCI_EVENT_DISCONNECTION_COMPLETE:
printf("test client - DISCONNECTED\n");
break;
case GAP_LE_ADVERTISING_REPORT:
if (tc_state != TC_W4_SCAN_RESULT) return;
printf("test client - SCAN ACTIVE\n");
ad_event_t ad_event;
int pos = 2;
ad_event.event_type = packet[pos++];
ad_event.address_type = packet[pos++];
memcpy(ad_event.address, &packet[pos], 6);
pos += 6;
ad_event.rssi = packet[pos++];
ad_event.length = packet[pos++];
ad_event.data = &packet[pos];
pos += ad_event.length;
dump_ad_event(&ad_event);
test_device_addr_type = ad_event.address_type;
bd_addr_t found_device_addr;
memcpy(found_device_addr, ad_event.address, 6);
swapX(ad_event.address, found_device_addr, 6);
if (memcmp(&found_device_addr, &sensor_tag1_addr, 6) == 0
|| memcmp(&found_device_addr, &sensor_tag2_addr, 6) == 0) {
tc_state = TC_W4_CONNECT;
le_central_stop_scan();
le_central_connect(found_device_addr, test_device_addr_type);
}
break;
case BTSTACK_EVENT_STATE:
// BTstack activated, get started
if (packet[2] == HCI_STATE_WORKING) {
printf("BTstack activated, get started!\n");
tc_state = TC_W4_SCAN_RESULT;
le_central_start_scan();
}
break;
case HCI_EVENT_LE_META:
switch (packet[2]) {
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: {
if (tc_state != TC_W4_CONNECT) return;
tc_state = TC_W4_SERVICE_RESULT;
printf("\n test client - CONNECTED, query ACC service\n");
test_gatt_client_handle = READ_BT_16(packet, 4);
gatt_client_start(&test_gatt_client_context, test_gatt_client_handle);
// let's start
gatt_client_discover_primary_services_by_uuid128(&test_gatt_client_context, acc_service_uuid);
break;
}
default:
break;
}
break;
case DAEMON_EVENT_HCI_PACKET_SENT:
switch(tc_state){
case TC_W2_WRITE_WITHOUT_RESPONSE:
status = gatt_client_write_value_of_characteristic_without_response(&test_gatt_client_context, characteristic.value_handle, 1, chr_short_value);
if (status != BLE_PERIPHERAL_OK) break;
tc_state = TC_W4_READ_LONG_RESULT;
gatt_client_read_long_value_of_characteristic(&test_gatt_client_context, &characteristic);
default:
break;
}
default:
break;
}
}
int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
l2cap_init();
ble_client_init();
ble_client_register_gatt_client_event_packet_handler(handle_gatt_client_event);
ble_client_register_hci_packet_handler(handle_hci_event);
// turn on!
hci_power_control(HCI_POWER_ON);
return 0;
}

View File

@ -109,9 +109,9 @@ static void dump_advertising_report(advertising_report_t * e){
}
static void dump_characteristic_value(le_characteristic_value_event_t * event){
printf(" * characteristic value of length %d *** ", event->blob_length );
printf_hexdump(event->blob , event->blob_length);
static void dump_characteristic_value(uint8_t * blob, uint16_t blob_length){
printf(" * characteristic value of length %d *** ", blob_length );
printf_hexdump(blob , blob_length);
printf("\n");
}
@ -138,17 +138,41 @@ static void error_code(int status){
break;
}
}
void handle_gatt_client_event(le_event_t * event){
static void extract_service(le_service_t * service, uint8_t * packet){
service->start_group_handle = READ_BT_16(packet, 4);
service->end_group_handle = READ_BT_16(packet, 6);
service->uuid16 = 0;
swap128(&packet[8], service->uuid128);
if (sdp_has_blueooth_base_uuid(service->uuid128)){
service->uuid16 = READ_NET_32(service->uuid128, 0);
}
}
static void extract_characteristic(le_characteristic_t * characteristic, uint8_t * packet){
characteristic->start_handle = READ_BT_16(packet, 4);
characteristic->value_handle = READ_BT_16(packet, 6);
characteristic->end_handle = READ_BT_16(packet, 8);
characteristic->properties = READ_BT_16(packet, 10);
characteristic->uuid16 = 0;
swap128(&packet[12], characteristic->uuid128);
if (sdp_has_blueooth_base_uuid(characteristic->uuid128)){
characteristic->uuid16 = READ_NET_32(characteristic->uuid128, 0);
}
}
void handle_gatt_client_event(int8_t packet_type, uint8_t *packet, uint16_t size){
switch(state){
case TC_W4_SERVICE_RESULT:
switch(event->type){
switch(packet[0]){
case GATT_SERVICE_QUERY_RESULT:
battery_service = ((le_service_event_t *) event)->service;
extract_service(&battery_service, packet);
printf("Battery service found:\n");
dump_service(&battery_service);
break;
case GATT_QUERY_COMPLETE:
if (!((gatt_complete_event_t *) event)->status){
if (!packet[4]){
printf("Battery service not found. Restart scan.\n");
state = TC_W4_SCAN_RESULT;
le_central_start_scan();
@ -164,10 +188,10 @@ void handle_gatt_client_event(le_event_t * event){
break;
case TC_W4_CHARACTERISTIC_RESULT:
switch(event->type){
switch(packet[0]){
case GATT_CHARACTERISTIC_QUERY_RESULT:
printf("Battery level characteristic found:\n");
config_characteristic = ((le_characteristic_event_t *) event)->characteristic;
extract_characteristic(&config_characteristic, packet);
dump_characteristic(&config_characteristic);
break;
case GATT_QUERY_COMPLETE:
@ -180,16 +204,16 @@ void handle_gatt_client_event(le_event_t * event){
}
break;
case TC_W4_BATTERY_DATA:
if (event->type == GATT_QUERY_COMPLETE){
if (((gatt_complete_event_t*)event)->status != 0){
if (packet[0] == GATT_QUERY_COMPLETE){
if (packet[4] != 0){
printf("\nNotification is not possible: ");
error_code(((gatt_complete_event_t*)event)->status);
error_code(packet[4]);
}
break;
}
if (event->type != GATT_NOTIFICATION) break;
if (packet[0] != GATT_NOTIFICATION) break;
printf("\nBattery Data:\n");
dump_characteristic_value((le_characteristic_value_event_t *) event);
dump_characteristic_value(&packet[8], READ_BT_16(packet, 6));
break;
default:

View File

@ -110,7 +110,7 @@ static void handle_hci_event(void * connection, uint8_t packet_type, uint16_t ch
// Handles GATT client query results, sends queries and the
// GAP disconnect command when the querying is done.
void handle_gatt_client_event(le_event_t * event);
void handle_gatt_client_event(uint8_t packet_type, uint8_t *packet, uint16_t size);
static void gatt_client_setup(void){
// Initialize L2CAP and register HCI event handler
@ -236,17 +236,39 @@ static void handle_hci_event(void * connection, uint8_t packet_type, uint16_t ch
/* LISTING_START(GATTBrowserQueryHandler): Handling of the GATT client queries */
static int search_services = 1;
void handle_gatt_client_event(le_event_t * event){
static void extract_service(le_service_t * service, uint8_t * packet){
service->start_group_handle = READ_BT_16(packet, 4);
service->end_group_handle = READ_BT_16(packet, 6);
service->uuid16 = 0;
swap128(&packet[8], service->uuid128);
if (sdp_has_blueooth_base_uuid(service->uuid128)){
service->uuid16 = READ_NET_32(service->uuid128, 0);
}
}
static void extract_characteristic(le_characteristic_t * characteristic, uint8_t * packet){
characteristic->start_handle = READ_BT_16(packet, 4);
characteristic->value_handle = READ_BT_16(packet, 6);
characteristic->end_handle = READ_BT_16(packet, 8);
characteristic->properties = READ_BT_16(packet, 10);
characteristic->uuid16 = 0;
swap128(&packet[12], characteristic->uuid128);
if (sdp_has_blueooth_base_uuid(characteristic->uuid128)){
characteristic->uuid16 = READ_NET_32(characteristic->uuid128, 0);
}
}
void handle_gatt_client_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
le_service_t service;
le_characteristic_t characteristic;
switch(event->type){
case GATT_SERVICE_QUERY_RESULT:
service = ((le_service_event_t *) event)->service;
switch(packet[0]){
case GATT_SERVICE_QUERY_RESULT:\
extract_service(&service, packet);
dump_service(&service);
services[service_count++] = service;
break;
case GATT_CHARACTERISTIC_QUERY_RESULT:
characteristic = ((le_characteristic_event_t *) event)->characteristic;
extract_characteristic(&characteristic, packet);
dump_characteristic(&characteristic);
break;
case GATT_QUERY_COMPLETE:

View File

@ -410,65 +410,93 @@ void use_public_pts_address(void){
current_pts_address_type = public_pts_address_type;
}
void handle_gatt_client_event(le_event_t * event){
le_characteristic_value_event_t * value;
le_characteristic_event_t * characteristic_event;
void extract_service(le_service_t * service, uint8_t * packet){
service->start_group_handle = READ_BT_16(packet, 4);
service->end_group_handle = READ_BT_16(packet, 6);
service->uuid16 = 0;
swap128(&packet[8], service->uuid128);
if (sdp_has_blueooth_base_uuid(service->uuid128)){
service->uuid16 = READ_NET_32(service->uuid128, 0);
}
}
void extract_characteristic(le_characteristic_t * characteristic, uint8_t * packet){
characteristic->start_handle = READ_BT_16(packet, 4);
characteristic->value_handle = READ_BT_16(packet, 6);
characteristic->end_handle = READ_BT_16(packet, 8);
characteristic->properties = READ_BT_16(packet, 10);
characteristic->uuid16 = 0;
swap128(&packet[12], characteristic->uuid128);
if (sdp_has_blueooth_base_uuid(characteristic->uuid128)){
characteristic->uuid16 = READ_NET_32(characteristic->uuid128, 0);
}
}
void handle_gatt_client_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return;
uint8_t address_type;
bd_addr_t flipped_address;
le_service_t * service;
gatt_complete_event_t * complete_event;
le_characteristic_descriptor_event_t * characteristic_descriptor_event;
switch(event->type){
le_service_t service;
le_characteristic_t characteristic;
uint8_t * value;
uint16_t value_handle;
uint16_t value_length;
uint16_t value_offset;
uint8_t status;
switch(packet[0]){
case GATT_SERVICE_QUERY_RESULT:
switch (central_state){
case CENTRAL_W4_PRIMARY_SERVICES:
service = &((le_service_event_t *) event)->service;
extract_service(&service, packet);
printf("Primary Service with UUID ");
printUUID(service->uuid128, service->uuid16);
printf(", start group handle 0x%04x, end group handle 0x%04x\n", service->start_group_handle, service->end_group_handle);
printUUID(service.uuid128, service.uuid16);
printf(", start group handle 0x%04x, end group handle 0x%04x\n", service.start_group_handle, service.end_group_handle);
break;
default:
break;
}
break;
case GATT_INCLUDED_SERVICE_QUERY_RESULT:
service = &((le_service_event_t *) event)->service;
extract_service(&service, packet);
printf("Included Service with UUID ");
printUUID(service->uuid128, service->uuid16);
printf(", start group handle 0x%04x, end group handle 0x%04x\n", service->start_group_handle, service->end_group_handle);
printUUID(service.uuid128, service.uuid16);
printf(", start group handle 0x%04x, end group handle 0x%04x\n", service.start_group_handle, service.end_group_handle);
break;
case GATT_CHARACTERISTIC_QUERY_RESULT:
characteristic_event = ((le_characteristic_event_t *) event);
extract_characteristic(&characteristic, packet);
switch (central_state) {
case CENTRAL_W4_NAME_QUERY_COMPLETE:
gap_name_characteristic = characteristic_event->characteristic;
gap_name_characteristic = characteristic;
printf("GAP Name Characteristic found, value handle: 0x04%x\n", gap_name_characteristic.value_handle);
break;
case CENTRAL_W4_RECONNECTION_ADDRESS_QUERY_COMPLETE:
gap_reconnection_address_characteristic = characteristic_event->characteristic;
gap_reconnection_address_characteristic = characteristic;
printf("GAP Reconnection Address Characteristic found, value handle: 0x04%x\n", gap_reconnection_address_characteristic.value_handle);
break;
case CENTRAL_W4_PERIPHERAL_PRIVACY_FLAG_QUERY_COMPLETE:
gap_peripheral_privacy_flag_characteristic = characteristic_event->characteristic;
gap_peripheral_privacy_flag_characteristic = characteristic;
printf("GAP Peripheral Privacy Flag Characteristic found, value handle: 0x04%x\n", gap_peripheral_privacy_flag_characteristic.value_handle);
break;
case CENTRAL_W4_SIGNED_WRITE_QUERY_COMPLETE:
signed_write_characteristic = characteristic_event->characteristic;
signed_write_characteristic = characteristic;
printf("Characteristic for Signed Write found, value handle: 0x%04x\n", signed_write_characteristic.value_handle);
break;
case CENTRAL_W4_CHARACTERISTICS:
printf("Characteristic found with handle 0x%04x, uuid ", (characteristic_event->characteristic).value_handle);
if ((characteristic_event->characteristic).uuid16){
printf("%04x\n", (characteristic_event->characteristic).uuid16);
printf("Characteristic found with handle 0x%04x, uuid ", characteristic.value_handle);
if (characteristic.uuid16){
printf("%04x\n", characteristic.uuid16);
} else {
printf_hexdump((characteristic_event->characteristic).uuid128, 16);
printf_hexdump(characteristic.uuid128, 16);
}
break;
case CENTRAL_GPA_W4_RESPONSE2:
switch (ui_uuid16){
case GATT_CHARACTERISTIC_PRESENTATION_FORMAT:
case GATT_CHARACTERISTIC_AGGREGATE_FORMAT:
ui_attribute_handle = characteristic_event->characteristic.value_handle;
ui_attribute_handle = characteristic.value_handle;
break;
default:
break;
@ -479,70 +507,72 @@ void handle_gatt_client_event(le_event_t * event){
}
break;
case GATT_CHARACTERISTIC_VALUE_QUERY_RESULT:
value = (le_characteristic_value_event_t *) event;
value_handle = READ_BT_16(packet, 4);
value_length = READ_BT_16(packet, 6);
value = &packet[8];
switch (central_state){
case CENTRAL_W4_NAME_VALUE:
value->blob[value->blob_length] = 0;
printf("GAP Service: Device Name: %s\n", value->blob);
value[value_length] = 0;
printf("GAP Service: Device Name: %s\n", value);
break;
case CENTRAL_W4_READ_CHARACTERISTIC_VALUE_BY_HANDLE:
case CENTRAL_W4_READ_CHARACTERISTIC_VALUE_BY_UUID:
case CENTRAL_W4_READ_MULTIPLE_CHARACTERISTIC_VALUES:
printf("Value: ");
printf_hexdump(value->blob, value->blob_length);
printf_hexdump(value, value_length);
break;
case CENTRAL_GPA_W4_RESPONSE:
switch (ui_uuid16){
case GATT_PRIMARY_SERVICE_UUID:
printf ("Attribute handle 0x%04x, primary service 0x%04x\n", value->value_handle, READ_BT_16(value->blob,0));
printf ("Attribute handle 0x%04x, primary service 0x%04x\n", value_handle, READ_BT_16(value,0));
break;
case GATT_SECONDARY_SERVICE_UUID:
printf ("Attribute handle 0x%04x, secondary service 0x%04x\n", value->value_handle, READ_BT_16(value->blob,0));
printf ("Attribute handle 0x%04x, secondary service 0x%04x\n", value_handle, READ_BT_16(value,0));
break;
case GATT_INCLUDE_SERVICE_UUID:
printf ("Attribute handle 0x%04x, included service attribute handle 0x%04x, end group handle 0x%04x, uuid %04x\n",
value->value_handle, READ_BT_16(value->blob,0), READ_BT_16(value->blob,2), READ_BT_16(value->blob,4));
value_handle, READ_BT_16(value,0), READ_BT_16(value,2), READ_BT_16(value,4));
break;
case GATT_CHARACTERISTICS_UUID:
printf ("Attribute handle 0x%04x, properties 0x%02x, value handle 0x%04x, uuid ",
value->value_handle, value->blob[0], READ_BT_16(value->blob,1));
if (value->blob_length < 19){
printf("%04x\n", READ_BT_16(value->blob, 3));
value_handle, value[0], READ_BT_16(value,1));
if (value_length < 19){
printf("%04x\n", READ_BT_16(value, 3));
} else {
printUUID128(&value->blob[3]);
printUUID128(&value[3]);
printf("\n");
}
break;
case GATT_CHARACTERISTIC_EXTENDED_PROPERTIES:
printf ("Attribute handle 0x%04x, gatt characteristic properties 0x%04x\n", value->value_handle, READ_BT_16(value->blob,0));
printf ("Attribute handle 0x%04x, gatt characteristic properties 0x%04x\n", value_handle, READ_BT_16(value,0));
break;
case GATT_CHARACTERISTIC_USER_DESCRIPTION:
// go the value, but PTS 6.3 requires another request
printf("Read by type request received, store attribute handle for read request\n");
ui_attribute_handle = value->value_handle;
ui_attribute_handle = value_handle;
break;
case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION:
printf ("Attribute handle 0x%04x, gatt client characteristic configuration 0x%04x\n", value->value_handle, READ_BT_16(value->blob,0));
printf ("Attribute handle 0x%04x, gatt client characteristic configuration 0x%04x\n", value_handle, READ_BT_16(value,0));
break;
case GATT_CHARACTERISTIC_AGGREGATE_FORMAT:
ui_handles_count = value->blob_length >> 1;
printf ("Attribute handle 0x%04x, gatt characteristic aggregate format. Handles: ", value->value_handle);
ui_handles_count = value_length >> 1;
printf ("Attribute handle 0x%04x, gatt characteristic aggregate format. Handles: ", value_handle);
for (ui_handles_index = 0; ui_handles_index < ui_handles_count ; ui_handles_index++){
ui_handles[ui_handles_index] = READ_BT_16(value->blob, (ui_handles_index << 1));
ui_handles[ui_handles_index] = READ_BT_16(value, (ui_handles_index << 1));
printf("0x%04x, ", ui_handles[ui_handles_index]);
}
printf("\n");
ui_handles_index = 0;
ui_aggregate_handle = value->value_handle;
ui_aggregate_handle = value_handle;
break;
case GATT_CHARACTERISTIC_PRESENTATION_FORMAT:
printf("Presentation format: ");
printf_hexdump(value->blob, value->blob_length);
memcpy(ui_presentation_format, value->blob, 7);
printf_hexdump(value, value_length);
memcpy(ui_presentation_format, value, 7);
break;
default:
printf("Value: ");
printf_hexdump(value->blob, value->blob_length);
printf_hexdump(value, value_length);
break;
}
break;
@ -550,14 +580,14 @@ void handle_gatt_client_event(le_event_t * event){
switch (ui_uuid16){
case GATT_CHARACTERISTIC_PRESENTATION_FORMAT:
printf("Value: ");
printf_hexdump(value->blob, value->blob_length);
printf_hexdump(value, value_length);
printf("Format 0x%02x, Exponent 0x%02x, Unit 0x%04x\n",
ui_presentation_format[0], ui_presentation_format[1], READ_BT_16(ui_presentation_format, 2));
break;
case GATT_CHARACTERISTIC_AGGREGATE_FORMAT:
printf("Aggregated value: ");
printf_hexdump(value->blob, value->blob_length);
memcpy(ui_value_data, value->blob, value->blob_length);
printf_hexdump(value, value_length);
memcpy(ui_value_data, value, value_length);
ui_value_pos = 0;
central_state = CENTRAL_GPA_W4_RESPONSE4;
default:
@ -569,20 +599,23 @@ void handle_gatt_client_event(le_event_t * event){
}
break;
case GATT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
value = (le_characteristic_value_event_t *) event;
value = &packet[10];
value_offset = READ_BT_16(packet, 6);
value_length = READ_BT_16(packet, 8);
central_state = CENTRAL_IDLE;
printf("Value (offset %02u): ", value->value_offset);
printf_hexdump(value->blob, value->blob_length);
printf("Value (offset %02u): ", value_offset);
printf_hexdump(value, value_length);
break;
case GATT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
characteristic_descriptor_event = (le_characteristic_descriptor_event_t *) event;
value_handle = READ_BT_16(packet, 4);
value_length = READ_BT_16(packet, 6);
value = &packet[8];
switch (central_state){
case CENTRAL_GPA_W4_RESPONSE2:
switch (ui_uuid16){
case GATT_CHARACTERISTIC_USER_DESCRIPTION:
characteristic_descriptor_event->value[characteristic_descriptor_event->value_length] = 0;
printf ("Attribute handle 0x%04x, characteristic user descriptor: %s\n",
characteristic_descriptor_event->handle, characteristic_descriptor_event->value);
value[value_length] = 0;
printf ("Attribute handle 0x%04x, characteristic user descriptor: %s\n", value_handle, value);
break;
default:
break;
@ -591,30 +624,31 @@ void handle_gatt_client_event(le_event_t * event){
case CENTRAL_GPA_W4_RESPONSE4:
// only characteristic aggregate format
printf("Value: ");
printf_hexdump(&ui_value_data[ui_value_pos], gpa_format_type_len[characteristic_descriptor_event->value[0]]);
ui_value_pos += gpa_format_type_len[characteristic_descriptor_event->value[0]];
printf_hexdump(&ui_value_data[ui_value_pos], gpa_format_type_len[value[0]]);
ui_value_pos += gpa_format_type_len[value[0]];
printf("Format 0x%02x, Exponent 0x%02x, Unit 0x%04x\n",
characteristic_descriptor_event->value[0], characteristic_descriptor_event->value[1],
READ_BT_16(characteristic_descriptor_event->value, 2));
value[0], value[1], READ_BT_16(value, 2));
break;
default:
printf("Value: ");
printf_hexdump(characteristic_descriptor_event->value, characteristic_descriptor_event->value_length);
printf_hexdump(value, value_length);
break;
}
break;
case GATT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
characteristic_descriptor_event = (le_characteristic_descriptor_event_t *) event;
printf("Value (offset %02u): ", characteristic_descriptor_event->value_offset);
printf_hexdump(characteristic_descriptor_event->value, characteristic_descriptor_event->value_length);
value = &packet[10];
value_offset = READ_BT_16(packet, 6);
value_length = READ_BT_16(packet, 8);
printf("Value (offset %02u): ", value_offset);
printf_hexdump(value, value_length);
break;
case GATT_QUERY_COMPLETE:
complete_event = (gatt_complete_event_t*) event;
if (complete_event->status){
status = packet[4];
if (status){
central_state = CENTRAL_IDLE;
printf("GATT_QUERY_COMPLETE: %s 0x%02x\n",
att_error_string_for_code(complete_event->status), complete_event->status);
att_error_string_for_code(status), status);
break;
}
switch (central_state){
@ -713,14 +747,18 @@ void handle_gatt_client_event(le_event_t * event){
}
break;
case GATT_NOTIFICATION:
value = (le_characteristic_value_event_t *) event;
printf("Notification handle 0x%04x, value: ", value->value_handle);
printf_hexdump(value->blob, value->blob_length);
value_handle = READ_BT_16(packet, 4);
value_length = READ_BT_16(packet, 6);
value = &packet[8];
printf("Notification handle 0x%04x, value: ", value_handle);
printf_hexdump(value, value_length);
break;
case GATT_INDICATION:
value = (le_characteristic_value_event_t *) event;
printf("Indication handle 0x%04x, value: ", value->value_handle);
printf_hexdump(value->blob, value->blob_length);
value_handle = READ_BT_16(packet, 4);
value_length = READ_BT_16(packet, 6);
value = &packet[8];
printf("Indication handle 0x%04x, value: ", value_handle);
printf_hexdump(value, value_length);
break;
default:
break;