ble client: unit test discover primary service by uuid

This commit is contained in:
mila@ringwald.ch 2014-04-11 09:31:45 +00:00
parent 4d890b2d0d
commit c032b69fa6
2 changed files with 98 additions and 29 deletions

View File

@ -661,7 +661,7 @@ le_command_status_t le_central_discover_primary_services_by_uuid16(le_peripheral
peripheral->end_group_handle = 0xffff;
peripheral->state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
peripheral->uuid16 = uuid16;
sdp_normalize_uuid((uint8_t*) &(peripheral->uuid128), peripheral->uuid16);
gatt_client_run();
return BLE_PERIPHERAL_OK;
}
@ -672,6 +672,7 @@ le_command_status_t le_central_discover_primary_services_by_uuid128(le_periphera
peripheral->end_group_handle = 0xffff;
peripheral->uuid16 = 0;
memcpy(peripheral->uuid128, uuid128, 16);
peripheral->state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
gatt_client_run();
@ -1420,7 +1421,7 @@ static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pa
service.start_group_handle = READ_BT_16(packet,i);
service.end_group_handle = READ_BT_16(packet,i+2);
memcpy(service.uuid128, peripheral->uuid128, 16);
service.uuid16 = peripheral->uuid16;
event.service = service;
(*le_central_callback)((le_central_event_t*)&event);
}

View File

@ -24,13 +24,15 @@
static bd_addr_t test_device_addr = {0x34, 0xb1, 0xf7, 0xd1, 0x77, 0x9b};
static le_peripheral_t test_device;
uint8_t service_uuid128[] = {0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
uint16_t service_uuid16 = 0xF000;
static int result_index;
static le_service_t services[50];
static uint8_t advertisement_received;
static uint8_t connected;
static uint8_t primary_services_found;
static uint8_t result_found;
void mock_simulate_hci_state_working();
void mock_simulate_command_complete(const hci_cmd_t *cmd);
@ -39,6 +41,60 @@ void mock_simulate_connected();
void mock_simulate_exchange_mtu();
void mock_simulate_discover_primary_services_response();
static void printUUID(uint8_t * uuid128, uint16_t uuid16){
printf(", uuid ");
if (uuid16){
printf(" 0x%02x",uuid16);
} else {
printUUID128(uuid128);
}
printf("\n");
}
void printUUID1(uint8_t *uuid) {
printf("0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
}
static void hexdump2(void const *data, int size){
int i;
for (i=0; i<size;i++){
printf("%02X ", ((uint8_t *)data)[i]);
}
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);
printUUID1(characteristic->uuid128);
}
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);
hexdump2( 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_characteristic_descriptor_t * descriptor){
printf(" *** descriptor *** handle 0x%02x, value ", descriptor->handle);
hexdump2(descriptor->value, descriptor->value_length);
printUUID1(descriptor->uuid128);
}
static void dump_characteristic_value(le_characteristic_value_event_t * event){
printf(" *** characteristic value of length %d *** ", event->characteristic_value_blob_length);
hexdump2(event->characteristic_value, event->characteristic_value_blob_length);
printf("\n");
}
void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
int i;
@ -56,8 +112,16 @@ static void verify_advertisement(ad_event_t * e){
}
static void verify_primary_services(){
if (result_index == 1){
if (services[0].uuid16){
CHECK_EQUAL(service_uuid16, services[0].uuid16);
} else {
CHECK_EQUAL_ARRAY(service_uuid128, services[0].uuid128, 16);
}
return;
}
CHECK_EQUAL(6, result_index);
uint8_t uuids[6][16] = {
{0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb},
{0x00, 0x00, 0x18, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb},
@ -87,8 +151,8 @@ static void handle_le_central_event(le_central_event_t * event){
services[result_index++] = ((le_service_event_t *) event)->service;
break;
case GATT_SERVICE_QUERY_COMPLETE:
verify_primary_services();
primary_services_found = 1;
verify_primary_services();
result_found = 1;
break;
default:
printf("handle_le_central_event");
@ -100,44 +164,48 @@ TEST_GROUP(GATTClient){
int acl_buffer_size;
uint8_t acl_buffer[27];
void connect(){
le_central_connect(&test_device, 1, test_device_addr);
mock_simulate_connected();
mock_simulate_exchange_mtu();
CHECK(connected);
}
void setup(){
advertisement_received = 0;
connected = 0;
primary_services_found = 0;
result_found = 0;
result_index = 0;
le_central_init();
le_central_register_handler(handle_le_central_event);
mock_simulate_hci_state_working();
att_set_db(profile_data);
connect();
}
};
// 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);
// }
TEST(GATTClient, TestDiscoverPrimaryServices){
le_central_connect(&test_device, 1, test_device_addr);
mock_simulate_connected();
mock_simulate_exchange_mtu();
CHECK(connected);
le_central_discover_primary_services(&test_device);
CHECK(primary_services_found);
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, TestDiscoverPrimaryServices){
le_central_discover_primary_services(&test_device);
CHECK(result_found);
}
TEST(GATTClient, TestDiscoverPrimaryServicesByUUID16){
le_central_discover_primary_services_by_uuid16(&test_device, service_uuid16);
CHECK(result_found);
}
TEST(GATTClient, TestDiscoverPrimaryServicesByUUID128){
le_central_discover_primary_services_by_uuid128(&test_device, service_uuid128);
CHECK(result_found);
}
int main (int argc, const char * argv[]){