mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-23 19:20:51 +00:00
ble client: unit test connect
This commit is contained in:
parent
ac4c738e79
commit
c442c7c23f
@ -20,9 +20,17 @@
|
||||
#include "hci.h"
|
||||
#include "ble_client.h"
|
||||
|
||||
static bd_addr_t test_device_addr = {0x61, 0x48, 0x80, 0xe6, 0xa1, 0x71};
|
||||
static bd_addr_t test_device_addr = {0x34, 0xb1, 0xf7, 0xd1, 0x77, 0x9b};
|
||||
static le_peripheral_t test_device;
|
||||
|
||||
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 mock_simulate_exchange_mtu();
|
||||
|
||||
void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
|
||||
int i;
|
||||
@ -33,31 +41,35 @@ void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
|
||||
|
||||
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(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);
|
||||
}
|
||||
|
||||
uint8_t advertisement_received;
|
||||
|
||||
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:
|
||||
printf("GATT_CONNECTION_COMPLETE\n");
|
||||
connected = 1;
|
||||
break;
|
||||
default:
|
||||
printf("handle_le_central_event");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_GROUP(GATTClient){
|
||||
void setup(){
|
||||
advertisement_received = 0;
|
||||
connected = 0;
|
||||
le_central_init();
|
||||
le_central_register_handler(handle_le_central_event);
|
||||
// le_central_register_packet_handler(handle_hci_event);
|
||||
mock_simulate_hci_state_working();
|
||||
}
|
||||
};
|
||||
@ -65,9 +77,18 @@ TEST_GROUP(GATTClient){
|
||||
|
||||
TEST(GATTClient, TestScanning){
|
||||
le_central_start_scan();
|
||||
mock_simulate_command_complete(&hci_le_set_scan_enable);
|
||||
mock_simulate_scan_response();
|
||||
CHECK(advertisement_received);
|
||||
}
|
||||
|
||||
TEST(GATTClient, TestConnecting){
|
||||
le_central_connect(&test_device, 1, test_device_addr);
|
||||
mock_simulate_connected();
|
||||
mock_simulate_exchange_mtu();
|
||||
CHECK(connected);
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, const char * argv[]){
|
||||
return CommandLineTestRunner::RunAllTests(argc, argv);
|
||||
|
@ -12,8 +12,8 @@
|
||||
static btstack_packet_handler_t att_packet_handler;
|
||||
static void (*gatt_central_packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
|
||||
|
||||
static uint8_t l2cap_stack_buffer[20];
|
||||
static uint16_t max_l2cap_data_packet_length = 20;
|
||||
static const uint16_t max_mtu = 23;
|
||||
static uint8_t l2cap_stack_buffer[max_mtu];
|
||||
|
||||
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};
|
||||
@ -25,11 +25,23 @@ void mock_simulate_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};
|
||||
void mock_simulate_connected(){
|
||||
uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05};
|
||||
gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
|
||||
}
|
||||
|
||||
|
||||
void mock_simulate_scan_response(){
|
||||
uint8_t packet[] = {0x3E, 0x0F, 0x02, 0x01, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x03, 0x02, 0x01, 0x05, 0xBC};
|
||||
gatt_central_packet_handler(NULL, HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
|
||||
}
|
||||
|
||||
void mock_simulate_exchange_mtu(){
|
||||
printf("mock_simulate_exchange_mtu\n");
|
||||
uint8_t packet[] = {0x03, 0x17, 0x00};
|
||||
att_packet_handler(ATT_DATA_PACKET, 0x40, (uint8_t *)&packet, sizeof(packet));
|
||||
}
|
||||
|
||||
int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
|
||||
return 1;
|
||||
}
|
||||
@ -39,25 +51,19 @@ int l2cap_can_send_connectionless_packet_now(void){
|
||||
}
|
||||
|
||||
int hci_send_cmd(const hci_cmd_t *cmd, ...){
|
||||
// printf("hci_send_cmd \n");
|
||||
|
||||
if (cmd == &hci_le_set_scan_enable){
|
||||
mock_simulate_command_complete(cmd);
|
||||
mock_simulate_scan_response();
|
||||
}
|
||||
printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
uint8_t *l2cap_get_outgoing_buffer(void){
|
||||
printf("l2cap_get_outgoing_buffer \n");
|
||||
printf("l2cap_get_outgoing_buffer\n");
|
||||
return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
|
||||
}
|
||||
|
||||
|
||||
uint16_t l2cap_max_mtu(void){
|
||||
printf("l2cap_max_mtu \n");
|
||||
return max_l2cap_data_packet_length;
|
||||
printf("l2cap_max_mtu\n");
|
||||
return max_mtu;
|
||||
}
|
||||
|
||||
|
||||
@ -69,18 +75,13 @@ void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t pa
|
||||
gatt_central_packet_handler = handler;
|
||||
}
|
||||
|
||||
int hci_reserve_packet_buffer(void){
|
||||
printf("hci_reserve_packet_buffer \n");
|
||||
int l2cap_reserve_packet_buffer(void){
|
||||
printf("l2cap_reserve_packet_buffer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l2cap_reserve_packet_buffer(void){
|
||||
printf("l2cap_reserve_packet_buffer \n");
|
||||
return hci_reserve_packet_buffer();
|
||||
}
|
||||
|
||||
int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
|
||||
printf("l2cap_send_prepared_connectionless \n");
|
||||
printf("l2cap_send_prepared_connectionless\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user