ble client: split le central and gatt tests

This commit is contained in:
mila@ringwald.ch 2014-04-28 21:09:35 +00:00
parent 73b0d685f1
commit ee988ca983
6 changed files with 168 additions and 100 deletions

View File

@ -84,18 +84,16 @@ static linked_list_t gatt_client_connections = NULL;
static uint16_t att_client_start_handle = 0x0001;
void (*le_central_callback_old)(le_central_event_t * event);
void (*le_central_packet_handler_old)(uint8_t packet_type, uint8_t *packet, uint16_t size) = NULL;
static void packet_handler_old(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
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 (*le_central_callback)(le_central_event_t * event);
static void le_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size);
void (*le_central_callback)(le_central_event_t * event);
void (*le_central_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = NULL;
static void le_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
void (*gatt_client_callback)(le_central_event_t * event);
void (*gatt_client_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = NULL;
static void gatt_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void gatt_client_run();
@ -122,26 +120,42 @@ void le_central_init(){
}
void ble_client_init(){
// le_connections = NULL;
le_central_init();
gatt_client_init();
l2cap_register_packet_handler(packet_handler_old);
l2cap_register_packet_handler(ble_packet_handler);
}
static void dummy_notify(le_central_event_t* event){}
void gatt_client_register_handler(void (*gatt_callback)(le_central_event_t* event)){
if (gatt_callback == NULL){
gatt_callback = &dummy_notify;
}
gatt_client_callback = gatt_callback;
}
void le_central_register_handler(void (*le_callback)(le_central_event_t* event)){
le_central_callback_old = dummy_notify;
if (le_callback != NULL){
le_central_callback_old = le_callback;
}
if (le_callback == NULL){
le_callback = dummy_notify;
}
le_central_callback = le_callback;
}
void ble_client_register_packet_handler(void (*le_callback)(le_central_event_t* event)){
gatt_client_register_handler(le_callback);
le_central_register_handler(le_callback);
/*
ble_client_callback = dummy_notify;
if (le_callback != NULL){
ble_client_callback = le_callback;
}
*/
}
void le_central_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
le_central_packet_handler_old = handler;
ble_client_packet_handler = handler;
}
@ -354,7 +368,7 @@ static inline void send_le_central_connection_complete_event(le_central_t * peri
event.type = type;
event.device = peripheral;
event.status = status;
(*le_central_callback_old)((le_central_event_t*)&event);
(*le_central_callback)((le_central_event_t*)&event);
}
static inline void send_gatt_complete_event(gatt_client_t * peripheral, uint8_t type, uint8_t status){
@ -362,7 +376,7 @@ static inline void send_gatt_complete_event(gatt_client_t * peripheral, uint8_t
event.type = type;
event.device = peripheral;
event.status = status;
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
}
static gatt_client_t * get_gatt_client_context_for_handle(uint16_t handle){
@ -453,7 +467,7 @@ static void handle_advertising_packet(uint8_t *packet, int size){
advertisement_event.rssi = packet[4+num_reports*9+total_data_length + i];
(*le_central_callback_old)((le_central_event_t*)&advertisement_event);
(*le_central_callback)((le_central_event_t*)&advertisement_event);
}
}
@ -1108,8 +1122,7 @@ static void gatt_packet_handler(void * connection, uint8_t packet_type, uint16_t
gatt_client_run();
}
static void packet_handler_old(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
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;
le_packet_handler(connection, packet_type, channel, packet, size);
@ -1122,11 +1135,11 @@ static void packet_handler_old(void * connection, uint8_t packet_type, uint16_t
// printf("HCI_EVENT_LE_META 0x%02x, ADVERTISING_REPORT 0x%02x, scanning %d\n", HCI_EVENT_LE_META, HCI_SUBEVENT_LE_ADVERTISING_REPORT, SCANNING);
gatt_client_run();
//gatt_client_run();
// forward to app
if (!le_central_packet_handler_old) return;
le_central_packet_handler_old(packet_type, packet, size);
if (!ble_client_packet_handler) return;
ble_client_packet_handler(packet_type, packet, size);
}
static char * att_errors[] = {
@ -1197,7 +1210,7 @@ static void report_gatt_services(gatt_client_t * peripheral, uint8_t * packet,
event.service = service;
// printf(" report_gatt_services 0x%02x : 0x%02x-0x%02x\n", service.uuid16, service.start_group_handle, service.end_group_handle);
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
}
// printf("report_gatt_services for %02X done\n", peripheral->handle);
}
@ -1238,7 +1251,7 @@ static void characteristic_end_found(gatt_client_t * peripheral, uint16_t end_ha
event.characteristic.properties = peripheral->characteristic_properties;
event.characteristic.uuid16 = peripheral->uuid16;
memcpy(event.characteristic.uuid128, peripheral->uuid128, 16);
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
peripheral->characteristic_start_handle = 0;
}
@ -1273,7 +1286,7 @@ static void report_gatt_included_service(gatt_client_t * peripheral, uint8_t *uu
le_service_event_t event;
event.type = GATT_INCLUDED_SERVICE_QUERY_RESULT;
event.service = service;
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
}
static void send_characteristic_value_event(gatt_client_t * peripheral, uint16_t handle, uint8_t * value, uint16_t length, uint16_t offset, uint8_t event_type){
@ -1283,7 +1296,7 @@ static void send_characteristic_value_event(gatt_client_t * peripheral, uint16_t
event.characteristic_value_blob_length = length;
event.characteristic_value = value;
event.characteristic_value_offset = offset;
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
}
static void report_gatt_long_characteristic_value_blob(gatt_client_t * peripheral, uint8_t * value, uint16_t blob_length, int value_offset){
@ -1317,7 +1330,7 @@ static void report_gatt_characteristic_descriptor(gatt_client_t * peripheral, ui
descriptor.value = value;
descriptor.value_length = value_length;
event.characteristic_descriptor = descriptor;
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
}
static void report_gatt_all_characteristic_descriptors(gatt_client_t * peripheral, uint8_t * packet, uint16_t size, uint16_t pair_size){
@ -1338,7 +1351,7 @@ static void report_gatt_all_characteristic_descriptors(gatt_client_t * periphera
descriptor.value_length = 0;
event.characteristic_descriptor = descriptor;
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
}
}
@ -1414,8 +1427,10 @@ static int is_value_valid(gatt_client_t *peripheral, uint8_t *packet, uint16_t s
static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
if (packet_type != ATT_DATA_PACKET) return;
gatt_client_t * peripheral = get_gatt_client_context_for_handle(handle);
if (!peripheral) return;
switch (packet[0]){
case ATT_EXCHANGE_MTU_RESPONSE:
{
@ -1526,7 +1541,7 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
memcpy(service.uuid128, peripheral->uuid128, 16);
service.uuid16 = peripheral->uuid16;
event.service = service;
(*le_central_callback_old)((le_central_event_t*)&event);
(*gatt_client_callback)((le_central_event_t*)&event);
}
trigger_next_service_by_uuid_query(peripheral, service.end_group_handle);
@ -2287,7 +2302,7 @@ static void handle_disconnect(le_central_event_t * event){
printf(" DONE\n");
}
static void handle_le_central_event(le_central_event_t * event){
static void handle_ble_client_event(le_central_event_t * event){
handle_scan_and_connect(event);
handle_disconnect(event);
@ -2433,7 +2448,7 @@ void setup(void){
hci_init(transport, &config, control, remote_db);
l2cap_init();
ble_client_init();
le_central_register_handler(handle_le_central_event);
ble_client_register_packet_handler(handle_ble_client_event);
le_central_register_packet_handler(handle_hci_event);
}

View File

@ -274,7 +274,12 @@ typedef struct le_characteristic_value_event{
void ble_client_init();
void gatt_client_init();
void le_central_init();
void gatt_client_register_handler(void (*le_callback)(le_central_event_t * event));
void le_central_register_handler(void (*le_callback)(le_central_event_t * event));
void ble_client_register_packet_handler(void (*le_callback)(le_central_event_t * event));
le_command_status_t le_central_start_scan();
// creates one event per found peripheral device

View File

@ -26,7 +26,7 @@ COMMON = \
COMMON_OBJ = $(COMMON:.c=.o)
all: gatt_client
all: gatt_client le_central
# compile .ble description
profile.h: profile.gatt
@ -35,8 +35,11 @@ profile.h: profile.gatt
gatt_client: ${CORE_OBJ} ${COMMON_OBJ} gatt_client.c profile.h expected_results.h
${CC} ${CORE_OBJ} ${COMMON_OBJ} gatt_client.c ${CFLAGS} ${LDFLAGS} -o $@
le_central: ${CORE_OBJ} ${COMMON_OBJ} le_central.c
${CC} ${CORE_OBJ} ${COMMON_OBJ} le_central.c ${CFLAGS} ${LDFLAGS} -o $@
clean:
rm -f gatt_client
rm -f gatt_client le_central
rm -f *.o ${BTSTACK_ROOT}/src/*.o
rm -rf *.dSYM

View File

@ -23,13 +23,10 @@
#include "profile.h"
#include "expected_results.h"
static bd_addr_t test_device_addr = {0x34, 0xb1, 0xf7, 0xd1, 0x77, 0x9b};
static gatt_client_t test_device;
static le_central_t test_le_central_context;
typedef enum {
IDLE2,
IDLE,
DISCOVER_PRIMARY_SERVICES,
DISCOVER_PRIMARY_SERVICE_WITH_UUID16,
DISCOVER_PRIMARY_SERVICE_WITH_UUID128,
@ -58,28 +55,21 @@ typedef enum {
WRITE_CHARACTERISTIC_VALUE_WITHOUT_RESPONSE
} current_test_t;
current_test_t test = IDLE2;
current_test_t test = IDLE;
uint8_t characteristic_uuid128[] = {0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
uint16_t characteristic_uuid16 = 0xF000;
static int result_index;
static uint8_t result_found;
static uint8_t result_complete;
static le_service_t services[50];
static le_service_t included_services[50];
static le_characteristic_t characteristics[50];
static le_characteristic_descriptor_t descriptors[50];
static uint8_t advertisement_received;
static uint8_t connected;
static uint8_t result_found;
static uint8_t result_complete;
void mock_simulate_hci_state_working();
void mock_simulate_command_complete(const hci_cmd_t *cmd);
void mock_simulate_scan_response();
void mock_simulate_connected();
void mock_simulate_exchange_mtu();
void mock_simulate_discover_primary_services_response();
@ -98,14 +88,6 @@ void CHECK_EQUAL_GATT_ATTRIBUTE(const uint8_t * exp_uuid, const uint8_t * exp_ha
// -----------------------------------------------------
static void verify_advertisement(ad_event_t * e){
CHECK_EQUAL(0, e->event_type);
CHECK_EQUAL(0, e->address_type);
CHECK_EQUAL(188, e->rssi);
CHECK_EQUAL(3, e->length);
CHECK_EQUAL_ARRAY((uint8_t *)test_device_addr, (uint8_t *)e->address, 6);
}
static void verify_primary_services_with_uuid16(){
CHECK_EQUAL(1, result_index);
CHECK_EQUAL_GATT_ATTRIBUTE(primary_service_uuid16, primary_service_uuid16_handles, services[0].uuid128, services[0].start_group_handle, services[0].end_group_handle);
@ -152,18 +134,6 @@ static void verify_blob(uint16_t value_length, uint16_t value_offset, uint8_t *
static void handle_le_central_event(le_central_event_t * event){
switch(event->type){
case GATT_ADVERTISEMENT:
advertisement_received = 1;
verify_advertisement((ad_event_t *) event);
break;
case GATT_CONNECTION_COMPLETE: {
connected = 1;
le_central_connection_complete_event_t * peripheral_event = (le_central_connection_complete_event_t *) event;
uint16_t handle = peripheral_event->device->handle;
gatt_client_start(&test_device, handle);
break;
}
case GATT_SERVICE_QUERY_RESULT:
services[result_index++] = ((le_service_event_t *) event)->service;
break;
@ -374,37 +344,22 @@ TEST_GROUP(GATTClient){
int acl_buffer_size;
uint8_t acl_buffer[27];
void connect(){
le_central_connect(&test_le_central_context, 1, test_device_addr);
mock_simulate_connected();
CHECK(connected);
mock_simulate_exchange_mtu();
}
void setup(){
advertisement_received = 0;
connected = 0;
result_found = 0;
result_index = 0;
result_complete = 0;
test = IDLE2;
test = IDLE;
ble_client_init();
le_central_register_handler(handle_le_central_event);
mock_simulate_hci_state_working();
// setup remote db
att_set_db(profile_data);
att_set_write_callback(&att_write_callback);
att_set_read_callback(&att_read_callback);
connect();
}
};
TEST(GATTClient, TestScanning){
le_central_start_scan();
mock_simulate_command_complete(&hci_le_set_scan_enable);
mock_simulate_scan_response();
CHECK(advertisement_received);
}
gatt_client_init();
gatt_client_register_handler(handle_le_central_event);
gatt_client_start(&test_device, 0x40);
}
};
TEST(GATTClient, TestDiscoverPrimaryServices){
test = DISCOVER_PRIMARY_SERVICES;

View File

@ -0,0 +1,94 @@
//*****************************************************************************
//
// test rfcomm query tests
//
//*****************************************************************************
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include <btstack/hci_cmds.h>
#include "btstack_memory.h"
#include "hci.h"
#include "ble_client.h"
static bd_addr_t test_device_addr = {0x34, 0xb1, 0xf7, 0xd1, 0x77, 0x9b};
static le_central_t test_le_central_context;
static uint8_t advertisement_received;
static uint8_t connected;
void mock_simulate_hci_state_working();
void mock_simulate_command_complete(const hci_cmd_t *cmd);
void mock_simulate_scan_response();
void mock_simulate_connected();
void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){
for (int i=0; i<size; i++){
BYTES_EQUAL(expected[i], actual[i]);
}
}
// -----------------------------------------------------
static void verify_advertisement(ad_event_t * e){
CHECK_EQUAL(0, e->event_type);
CHECK_EQUAL(0, e->address_type);
CHECK_EQUAL(188, e->rssi);
CHECK_EQUAL(3, e->length);
CHECK_EQUAL_ARRAY((uint8_t *)test_device_addr, (uint8_t *)e->address, 6);
}
static void handle_ble_client_event(le_central_event_t * event){
switch(event->type){
case GATT_ADVERTISEMENT:
advertisement_received = 1;
verify_advertisement((ad_event_t *) event);
break;
case GATT_CONNECTION_COMPLETE: {
connected = 1;
break;
}
default:
printf("handle_le_central_event");
break;
}
}
TEST_GROUP(LECentral){
void connect(){
le_central_connect(&test_le_central_context, 1, test_device_addr);
mock_simulate_connected();
CHECK(connected);
}
void setup(){
advertisement_received = 0;
connected = 0;
ble_client_init();
ble_client_register_packet_handler(handle_ble_client_event);
mock_simulate_hci_state_working();
connect();
}
};
TEST(LECentral, TestScanning){
le_central_start_scan();
mock_simulate_command_complete(&hci_le_set_scan_enable);
mock_simulate_scan_response();
CHECK(advertisement_received);
}
int main (int argc, const char * argv[]){
return CommandLineTestRunner::RunAllTests(argc, argv);
}

View File

@ -15,6 +15,7 @@ static void (*gatt_central_packet_handler) (void * connection, uint8_t packet_ty
static const uint16_t max_mtu = 23;
static uint8_t l2cap_stack_buffer[max_mtu];
uint16_t gatt_client_handle = 0x40;
void mock_simulate_command_complete(const hci_cmd_t *cmd){
uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, cmd->opcode & 0xff, cmd->opcode >> 8, 0};
@ -37,11 +38,6 @@ void mock_simulate_scan_response(){
gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
}
void mock_simulate_exchange_mtu(){
// uint8_t packet[] = {0x03, 0x17, 0x00};
// att_packet_handler(ATT_DATA_PACKET, 0x40, (uint8_t *)&packet, sizeof(packet));
}
static void att_init_connection(att_connection_t * att_connection){
att_connection->mtu = 23;
att_connection->encryption_key_size = 0;
@ -75,6 +71,7 @@ uint16_t l2cap_max_mtu(void){
void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
// printf("mock: l2cap_register_fixed_channel, use att_packet_handler\n");
att_packet_handler = packet_handler;
}
@ -88,7 +85,6 @@ int l2cap_reserve_packet_buffer(void){
}
int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
// printf("l2cap_send_prepared_connectionless\n");
// assert handle = current connection
// assert cid = att
@ -97,7 +93,7 @@ int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t l
uint8_t response[max_mtu];
uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
if (response_len){
att_packet_handler(ATT_DATA_PACKET, 0x40, &response[0], response_len);
att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
}
return 0;
}