ble client: unit test advertisement

This commit is contained in:
mila@ringwald.ch 2014-04-10 21:41:21 +00:00
parent 4f5e860821
commit 9638642b3f
2 changed files with 36 additions and 17 deletions

View File

@ -20,29 +20,45 @@
#include "hci.h"
#include "ble_client.h"
static bd_addr_t test_device_addr = {0x61, 0x48, 0x80, 0xe6, 0xa1, 0x71};
static void handle_scan_and_connect(le_central_event_t * event){
if (event->type != GATT_ADVERTISEMENT) return;
printf("test client - SCAN ACTIVE\n");
ad_event_t * e = (ad_event_t*) event;
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);
// hexdump2( e->data, e->length);
void mock_simulate_hci_state_working();
// dump_ad_event(ad_event);
void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
int i;
for (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(1, e->address_type);
CHECK_EQUAL(183, e->rssi);
CHECK_EQUAL(3, e->length);
CHECK_EQUAL_ARRAY((uint8_t *)test_device_addr, (uint8_t *)e->address, 6);
}
uint8_t advertisement_received;
static void handle_le_central_event(le_central_event_t * event){
handle_scan_and_connect(event);
switch(event->type){
case GATT_ADVERTISEMENT:
advertisement_received = 1;
verify_advertisement((ad_event_t *) event);
break;
default:
break;
}
}
TEST_GROUP(GATTClient){
uint8_t advertisement_received;
TEST_GROUP(GATTClient){
void setup(){
advertisement_received = 0;
le_central_init();
le_central_register_handler(handle_le_central_event);
mock_simulate_hci_state_working();
}
};

View File

@ -15,13 +15,18 @@ static void (*gatt_central_packet_handler) (void * connection, uint8_t packet_ty
static uint8_t l2cap_stack_buffer[20];
static uint16_t max_l2cap_data_packet_length = 20;
void mock_command_complete(const hci_cmd_t *cmd){
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};
gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
}
void mock_simulate_hci_state_working(){
uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3);
}
void mock_simulate_scan_response(){
uint8_t packet[] = { 0x3E, 0x0F, 0x02, 0x01, 0x00, 0x01, 0x71, 0xA1, 0xE6, 0x80, 0x48, 0x61, 0x03, 0x02, 0x01, 0x1A, 0xB7};
uint8_t packet[] = {0x3E, 0x0F, 0x02, 0x01, 0x00, 0x01, 0x71, 0xA1, 0xE6, 0x80, 0x48, 0x61, 0x03, 0x02, 0x01, 0x1A, 0xB7};
gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
}
@ -34,10 +39,10 @@ int l2cap_can_send_connectionless_packet_now(void){
}
int hci_send_cmd(const hci_cmd_t *cmd, ...){
printf("hci_send_cmd \n");
// printf("hci_send_cmd \n");
if (cmd == &hci_le_set_scan_enable){
mock_command_complete(cmd);
mock_simulate_command_complete(cmd);
mock_simulate_scan_response();
}
return 0;
@ -62,8 +67,6 @@ void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint1
void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
gatt_central_packet_handler = handler;
uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3);
}
int hci_reserve_packet_buffer(void){