1 2 // ***************************************************************************** 3 // 4 // test rfcomm query tests 5 // 6 // ***************************************************************************** 7 8 9 #include <stdint.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #include "CppUTest/TestHarness.h" 15 #include "CppUTest/CommandLineTestRunner.h" 16 17 #include "hci_cmd.h" 18 19 #include "btstack_memory.h" 20 #include "hci.h" 21 #include "hci_dump.h" 22 #include "ble/gatt_client.h" 23 #include "ble/att_db.h" 24 #include "profile.h" 25 #include "expected_results.h" 26 27 extern "C" void hci_setup_le_connection(uint16_t con_handle); 28 29 static uint16_t gatt_client_handle = 0x40; 30 static int gatt_query_complete = 0; 31 32 typedef enum { 33 IDLE, 34 DISCOVER_PRIMARY_SERVICES, 35 DISCOVER_PRIMARY_SERVICE_WITH_UUID16, 36 DISCOVER_PRIMARY_SERVICE_WITH_UUID128, 37 38 DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID16, 39 DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID128, 40 41 DISCOVER_CHARACTERISTICS_FOR_SERVICE_WITH_UUID16, 42 DISCOVER_CHARACTERISTICS_FOR_SERVICE_WITH_UUID128, 43 DISCOVER_CHARACTERISTICS_BY_UUID16, 44 DISCOVER_CHARACTERISTICS_BY_UUID128, 45 DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID, 46 47 READ_CHARACTERISTIC_VALUE, 48 READ_LONG_CHARACTERISTIC_VALUE, 49 WRITE_CHARACTERISTIC_VALUE, 50 WRITE_LONG_CHARACTERISTIC_VALUE, 51 52 DISCOVER_CHARACTERISTIC_DESCRIPTORS, 53 READ_CHARACTERISTIC_DESCRIPTOR, 54 WRITE_CHARACTERISTIC_DESCRIPTOR, 55 WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION, 56 READ_LONG_CHARACTERISTIC_DESCRIPTOR, 57 WRITE_LONG_CHARACTERISTIC_DESCRIPTOR, 58 WRITE_RELIABLE_LONG_CHARACTERISTIC_VALUE, 59 WRITE_CHARACTERISTIC_VALUE_WITHOUT_RESPONSE 60 } current_test_t; 61 62 current_test_t test = IDLE; 63 64 uint8_t characteristic_uuid128[] = {0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}; 65 uint16_t characteristic_uuid16 = 0xF000; 66 67 static int result_index; 68 static uint8_t result_counter; 69 70 static gatt_client_service_t services[50]; 71 static gatt_client_service_t included_services[50]; 72 73 static gatt_client_characteristic_t characteristics[50]; 74 static gatt_client_characteristic_descriptor_t descriptors[50]; 75 76 void mock_simulate_discover_primary_services_response(void); 77 void mock_simulate_att_exchange_mtu_response(void); 78 79 void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){ 80 for (int i=0; i<size; i++){ 81 BYTES_EQUAL(expected[i], actual[i]); 82 } 83 } 84 85 void pUUID128(const uint8_t *uuid) { 86 printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", 87 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], 88 uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]); 89 } 90 91 //static int counter = 0; 92 void CHECK_EQUAL_GATT_ATTRIBUTE(const uint8_t * exp_uuid, const uint8_t * exp_handles, uint8_t * uuid, uint16_t start_handle, uint16_t end_handle){ 93 CHECK_EQUAL_ARRAY(exp_uuid, uuid, 16); 94 if (!exp_handles) return; 95 CHECK_EQUAL(exp_handles[0], start_handle); 96 CHECK_EQUAL(exp_handles[1], end_handle); 97 } 98 99 // ----------------------------------------------------- 100 101 static void verify_primary_services_with_uuid16(void){ 102 CHECK_EQUAL(1, result_index); 103 CHECK_EQUAL_GATT_ATTRIBUTE(primary_service_uuid16, primary_service_uuid16_handles, services[0].uuid128, services[0].start_group_handle, services[0].end_group_handle); 104 } 105 106 107 static void verify_primary_services_with_uuid128(void){ 108 CHECK_EQUAL(1, result_index); 109 CHECK_EQUAL_GATT_ATTRIBUTE(primary_service_uuid128, primary_service_uuid128_handles, services[0].uuid128, services[0].start_group_handle, services[0].end_group_handle); 110 } 111 112 static void verify_primary_services(void){ 113 CHECK_EQUAL(6, result_index); 114 for (int i=0; i<result_index; i++){ 115 CHECK_EQUAL_GATT_ATTRIBUTE(primary_service_uuids[i], NULL, services[i].uuid128, services[i].start_group_handle, services[i].end_group_handle); 116 } 117 } 118 119 static void verify_included_services_uuid16(void){ 120 CHECK_EQUAL(1, result_index); 121 CHECK_EQUAL_GATT_ATTRIBUTE(included_services_uuid16, included_services_uuid16_handles, included_services[0].uuid128, included_services[0].start_group_handle, included_services[0].end_group_handle); 122 } 123 124 static void verify_included_services_uuid128(void){ 125 CHECK_EQUAL(2, result_index); 126 for (int i=0; i<result_index; i++){ 127 CHECK_EQUAL_GATT_ATTRIBUTE(included_services_uuid128[i], included_services_uuid128_handles[i], included_services[i].uuid128, included_services[i].start_group_handle, included_services[i].end_group_handle); 128 } 129 } 130 131 static void verify_charasteristics(void){ 132 CHECK_EQUAL(15, result_index); 133 for (int i=0; i<result_index; i++){ 134 CHECK_EQUAL_GATT_ATTRIBUTE(characteristic_uuids[i], characteristic_handles[i], characteristics[i].uuid128, characteristics[i].start_handle, characteristics[i].end_handle); 135 } 136 } 137 138 static void verify_blob(uint16_t value_length, uint16_t value_offset, uint8_t * value){ 139 uint8_t * expected_value = (uint8_t*)&long_value[value_offset]; 140 CHECK(value_length); 141 CHECK_EQUAL_ARRAY(expected_value, value, value_length); 142 if (value_offset + value_length != sizeof(long_value)) return; 143 result_counter++; 144 } 145 146 static void handle_ble_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 147 if (packet_type != HCI_EVENT_PACKET) return; 148 uint8_t status; 149 gatt_client_service_t service; 150 gatt_client_characteristic_t characteristic; 151 gatt_client_characteristic_descriptor_t descriptor; 152 switch (packet[0]){ 153 case GATT_EVENT_QUERY_COMPLETE: 154 status = packet[4]; 155 gatt_query_complete = 1; 156 if (status){ 157 gatt_query_complete = 0; 158 printf("GATT_EVENT_QUERY_COMPLETE failed with status 0x%02X\n", status); 159 } 160 break; 161 case GATT_EVENT_SERVICE_QUERY_RESULT: 162 service.start_group_handle = little_endian_read_16(packet, 4); 163 service.end_group_handle = little_endian_read_16(packet, 6); 164 service.uuid16 = 0; 165 reverse_128(&packet[8], service.uuid128); 166 if (uuid_has_bluetooth_prefix(service.uuid128)){ 167 service.uuid16 = big_endian_read_32(service.uuid128, 0); 168 } 169 services[result_index++] = service; 170 result_counter++; 171 break; 172 case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT: 173 service.start_group_handle = little_endian_read_16(packet, 6); 174 service.end_group_handle = little_endian_read_16(packet, 8); 175 service.uuid16 = 0; 176 reverse_128(&packet[10], service.uuid128); 177 if (uuid_has_bluetooth_prefix(service.uuid128)){ 178 service.uuid16 = big_endian_read_32(service.uuid128, 0); 179 } 180 included_services[result_index++] = service; 181 result_counter++; 182 break; 183 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 184 characteristic.start_handle = little_endian_read_16(packet, 4); 185 characteristic.value_handle = little_endian_read_16(packet, 6); 186 characteristic.end_handle = little_endian_read_16(packet, 8); 187 characteristic.properties = little_endian_read_16(packet, 10); 188 characteristic.uuid16 = 0; 189 reverse_128(&packet[12], characteristic.uuid128); 190 if (uuid_has_bluetooth_prefix(characteristic.uuid128)){ 191 characteristic.uuid16 = big_endian_read_32(characteristic.uuid128, 0); 192 } 193 characteristics[result_index++] = characteristic; 194 result_counter++; 195 break; 196 case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 197 descriptor.handle = little_endian_read_16(packet, 4); 198 reverse_128(&packet[6], descriptor.uuid128); 199 if (uuid_has_bluetooth_prefix(descriptor.uuid128)){ 200 descriptor.uuid16 = big_endian_read_32(descriptor.uuid128, 0); 201 } 202 descriptors[result_index++] = descriptor; 203 result_counter++; 204 break; 205 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 206 case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT: 207 CHECK_EQUAL(short_value_length, little_endian_read_16(packet, 6)); 208 CHECK_EQUAL_ARRAY((uint8_t*)short_value, &packet[8], short_value_length); 209 result_counter++; 210 break; 211 case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT: 212 case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT: 213 verify_blob(little_endian_read_16(packet, 8), little_endian_read_16(packet, 6), &packet[10]); 214 result_counter++; 215 break; 216 } 217 } 218 219 extern "C" int att_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ 220 switch(test){ 221 case WRITE_CHARACTERISTIC_DESCRIPTOR: 222 case WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION: 223 CHECK_EQUAL(ATT_TRANSACTION_MODE_NONE, transaction_mode); 224 CHECK_EQUAL(0, offset); 225 CHECK_EQUAL_ARRAY(indication, buffer, 2); 226 result_counter++; 227 break; 228 case WRITE_CHARACTERISTIC_VALUE: 229 CHECK_EQUAL(ATT_TRANSACTION_MODE_NONE, transaction_mode); 230 CHECK_EQUAL(0, offset); 231 CHECK_EQUAL_ARRAY((uint8_t *)short_value, buffer, short_value_length); 232 result_counter++; 233 break; 234 case WRITE_LONG_CHARACTERISTIC_DESCRIPTOR: 235 case WRITE_LONG_CHARACTERISTIC_VALUE: 236 case WRITE_RELIABLE_LONG_CHARACTERISTIC_VALUE: 237 if (transaction_mode == ATT_TRANSACTION_MODE_VALIDATE) break; 238 if (transaction_mode == ATT_TRANSACTION_MODE_EXECUTE) break; 239 CHECK_EQUAL(ATT_TRANSACTION_MODE_ACTIVE, transaction_mode); 240 CHECK_EQUAL_ARRAY((uint8_t *)&long_value[offset], buffer, buffer_size); 241 if (offset + buffer_size != sizeof(long_value)) break; 242 result_counter++; 243 break; 244 default: 245 break; 246 } 247 return 0; 248 } 249 250 int copy_bytes(uint8_t * value, uint16_t value_length, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 251 int blob_length = value_length - offset; 252 if (blob_length >= buffer_size) blob_length = buffer_size; 253 254 memcpy(buffer, &value[offset], blob_length); 255 return blob_length; 256 } 257 258 extern "C" uint16_t att_read_callback(uint16_t handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 259 //printf("gatt client test, att_read_callback_t handle 0x%04x, offset %u, buffer %p, buffer_size %u\n", handle, offset, buffer, buffer_size); 260 switch(test){ 261 case READ_CHARACTERISTIC_DESCRIPTOR: 262 case READ_CHARACTERISTIC_VALUE: 263 result_counter++; 264 if (buffer){ 265 return copy_bytes((uint8_t *)short_value, short_value_length, offset, buffer, buffer_size); 266 } 267 return short_value_length; 268 case READ_LONG_CHARACTERISTIC_DESCRIPTOR: 269 case READ_LONG_CHARACTERISTIC_VALUE: 270 result_counter++; 271 if (buffer) { 272 return copy_bytes((uint8_t *)long_value, long_value_length, offset, buffer, buffer_size); 273 } 274 return long_value_length; 275 default: 276 break; 277 } 278 return 0; 279 } 280 281 // static const char * decode_status(uint8_t status){ 282 // switch (status){ 283 // case 0: return "0"; 284 // case GATT_CLIENT_IN_WRONG_STATE: return "GATT_CLIENT_IN_WRONG_STATE"; 285 // case GATT_CLIENT_DIFFERENT_CONTEXT_FOR_ADDRESS_ALREADY_EXISTS: return "GATT_CLIENT_DIFFERENT_CONTEXT_FOR_ADDRESS_ALREADY_EXISTS"; 286 // case GATT_CLIENT_NOT_CONNECTED: return "GATT_CLIENT_NOT_CONNECTED"; 287 // case GATT_CLIENT_VALUE_TOO_LONG: return "GATT_CLIENT_VALUE_TOO_LONG"; 288 // case GATT_CLIENT_BUSY: return "GATT_CLIENT_BUSY"; 289 // case GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED: return "GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED"; 290 // case GATT_CLIENTCHARACTERISTIC_INDICATION_NOT_SUPPORTED: return "GATT_CLIENTCHARACTERISTIC_INDICATION_NOT_SUPPORTED"; 291 // } 292 // } 293 294 TEST_GROUP(GATTClient){ 295 int acl_buffer_size; 296 uint8_t acl_buffer[27]; 297 uint8_t status; 298 299 void setup(void){ 300 result_counter = 0; 301 result_index = 0; 302 test = IDLE; 303 hci_setup_le_connection(gatt_client_handle); 304 } 305 306 void reset_query_state(void){ 307 gatt_client_t * gatt_client = gatt_client_get_client(gatt_client_handle); 308 gatt_client->gatt_client_state = P_READY; 309 gatt_client_set_required_security_level(LEVEL_0); 310 gatt_client_mtu_enable_auto_negotiation(1); 311 312 gatt_query_complete = 0; 313 result_counter = 0; 314 result_index = 0; 315 } 316 317 void set_wrong_gatt_client_state(void){ 318 gatt_client_t * gatt_client = gatt_client_get_client(gatt_client_handle); 319 CHECK_TRUE(gatt_client != NULL); 320 gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY; 321 } 322 }; 323 324 TEST(GATTClient, gatt_client_setters){ 325 gatt_client_set_required_security_level(LEVEL_4); 326 gatt_client_mtu_enable_auto_negotiation(0); 327 } 328 329 TEST(GATTClient, gatt_client_is_ready_mtu_exchange_disabled){ 330 gatt_client_mtu_enable_auto_negotiation(0); 331 int status = gatt_client_is_ready(gatt_client_handle); 332 CHECK_EQUAL(1, status); 333 } 334 335 TEST(GATTClient, TestDiscoverPrimaryServices){ 336 test = DISCOVER_PRIMARY_SERVICES; 337 status = gatt_client_discover_primary_services(handle_ble_client_event, HCI_CON_HANDLE_INVALID); 338 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 339 340 set_wrong_gatt_client_state(); 341 status = gatt_client_discover_primary_services(handle_ble_client_event, gatt_client_handle); 342 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 343 344 reset_query_state(); 345 status = gatt_client_discover_primary_services(handle_ble_client_event, gatt_client_handle); 346 CHECK_EQUAL(0, status); 347 CHECK_EQUAL(1, gatt_query_complete); 348 verify_primary_services(); 349 CHECK_EQUAL(1, gatt_query_complete); 350 } 351 352 TEST(GATTClient, TestDiscoverPrimaryServicesByUUID16){ 353 test = DISCOVER_PRIMARY_SERVICE_WITH_UUID16; 354 reset_query_state(); 355 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 356 CHECK_EQUAL(0, status); 357 CHECK_EQUAL(1, result_counter); 358 verify_primary_services_with_uuid16(); 359 CHECK_EQUAL(1, gatt_query_complete); 360 } 361 362 TEST(GATTClient, TestDiscoverPrimaryServicesByUUID128){ 363 test = DISCOVER_PRIMARY_SERVICE_WITH_UUID128; 364 status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128); 365 CHECK_EQUAL(0, status); 366 CHECK_EQUAL(1, result_counter); 367 verify_primary_services_with_uuid128(); 368 CHECK_EQUAL(1, gatt_query_complete); 369 370 // invalid con handle 371 status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, HCI_CON_HANDLE_INVALID, primary_service_uuid128); 372 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 373 374 set_wrong_gatt_client_state(); 375 status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128); 376 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 377 } 378 379 TEST(GATTClient, TestFindIncludedServicesForServiceWithUUID16){ 380 test = DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID16; 381 reset_query_state(); 382 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 383 CHECK_EQUAL(0, status); 384 CHECK_EQUAL(1, gatt_query_complete); 385 386 reset_query_state(); 387 status = gatt_client_find_included_services_for_service(handle_ble_client_event, gatt_client_handle, &services[0]); 388 CHECK_EQUAL(0, status); 389 CHECK_EQUAL(1, gatt_query_complete); 390 verify_included_services_uuid16(); 391 } 392 393 TEST(GATTClient, TestFindIncludedServicesForServiceWithUUID128){ 394 test = DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID128; 395 reset_query_state(); 396 status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128); 397 CHECK_EQUAL(0, status); 398 CHECK_EQUAL(1, gatt_query_complete); 399 400 reset_query_state(); 401 status = gatt_client_find_included_services_for_service(handle_ble_client_event, gatt_client_handle, &services[0]); 402 CHECK_EQUAL(0, status); 403 CHECK_EQUAL(1, gatt_query_complete); 404 verify_included_services_uuid128(); 405 406 // invalid con handle 407 status = gatt_client_find_included_services_for_service(handle_ble_client_event, HCI_CON_HANDLE_INVALID, &services[0]); 408 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 409 410 set_wrong_gatt_client_state(); 411 status = gatt_client_find_included_services_for_service(handle_ble_client_event, gatt_client_handle, &services[0]); 412 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 413 } 414 415 TEST(GATTClient, TestDiscoverCharacteristicsForService){ 416 test = DISCOVER_CHARACTERISTICS_FOR_SERVICE_WITH_UUID16; 417 reset_query_state(); 418 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 419 CHECK_EQUAL(0, status); 420 CHECK_EQUAL(1, gatt_query_complete); 421 422 reset_query_state(); 423 status = gatt_client_discover_characteristics_for_service(handle_ble_client_event, gatt_client_handle, &services[0]); 424 CHECK_EQUAL(0, status); 425 CHECK_EQUAL(1, gatt_query_complete); 426 verify_charasteristics(); 427 428 // invalid con handle 429 status = gatt_client_discover_characteristics_for_service(handle_ble_client_event, HCI_CON_HANDLE_INVALID, &services[0]); 430 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 431 432 set_wrong_gatt_client_state(); 433 status = gatt_client_discover_characteristics_for_service(handle_ble_client_event, gatt_client_handle, &services[0]); 434 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 435 } 436 437 TEST(GATTClient, TestDiscoverCharacteristicsByUUID16){ 438 test = DISCOVER_CHARACTERISTICS_BY_UUID16; 439 reset_query_state(); 440 status = gatt_client_discover_characteristics_for_handle_range_by_uuid16(handle_ble_client_event, gatt_client_handle, 0x30, 0x32, 0xF102); 441 CHECK_EQUAL(0, status); 442 CHECK_EQUAL(1, gatt_query_complete); 443 CHECK_EQUAL(1, result_counter); 444 445 // invalid con handle 446 status = gatt_client_discover_characteristics_for_handle_range_by_uuid16(handle_ble_client_event, HCI_CON_HANDLE_INVALID, 0x30, 0x32, 0xF102); 447 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 448 449 set_wrong_gatt_client_state(); 450 status = gatt_client_discover_characteristics_for_handle_range_by_uuid16(handle_ble_client_event, gatt_client_handle, 0x30, 0x32, 0xF102); 451 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 452 } 453 454 TEST(GATTClient, TestDiscoverCharacteristicsByUUID128){ 455 test = DISCOVER_CHARACTERISTICS_BY_UUID128; 456 reset_query_state(); 457 status = gatt_client_discover_characteristics_for_handle_range_by_uuid128(handle_ble_client_event, gatt_client_handle, characteristic_handles[1][0], characteristic_handles[1][1], characteristic_uuids[1]); 458 CHECK_EQUAL(0, status); 459 CHECK_EQUAL(1, gatt_query_complete); 460 CHECK_EQUAL(1, result_counter); 461 462 // invalid con handle 463 status = gatt_client_discover_characteristics_for_handle_range_by_uuid128(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristic_handles[1][0], characteristic_handles[1][1], characteristic_uuids[1]); 464 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 465 466 set_wrong_gatt_client_state(); 467 status = gatt_client_discover_characteristics_for_handle_range_by_uuid128(handle_ble_client_event, gatt_client_handle, characteristic_handles[1][0], characteristic_handles[1][1], characteristic_uuids[1]); 468 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 469 } 470 471 TEST(GATTClient, TestDiscoverCharacteristics4ServiceByUUID128){ 472 test = DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID; 473 reset_query_state(); 474 status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128); 475 CHECK_EQUAL(0, status); 476 CHECK_EQUAL(1, gatt_query_complete); 477 CHECK_EQUAL(1, result_counter); 478 479 reset_query_state(); 480 uint8_t characteristic_uuid[] = {0x00, 0x00, 0xF2, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; 481 status = gatt_client_discover_characteristics_for_service_by_uuid128(handle_ble_client_event, gatt_client_handle, &services[0], characteristic_uuid); 482 CHECK_EQUAL(0, status); 483 CHECK_EQUAL(1, gatt_query_complete); 484 CHECK_EQUAL(1, result_counter); 485 486 reset_query_state(); 487 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF200); 488 CHECK_EQUAL(0, status); 489 CHECK_EQUAL(1, gatt_query_complete); 490 CHECK_EQUAL(1, result_counter); 491 } 492 493 TEST(GATTClient, TestDiscoverCharacteristics4ServiceByUUID16){ 494 test = DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID; 495 reset_query_state(); 496 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 497 CHECK_EQUAL(0, status); 498 CHECK_EQUAL(1, gatt_query_complete); 499 CHECK_EQUAL(1, result_counter); 500 501 reset_query_state(); 502 uint8_t characteristic_uuid[]= { 0x00, 0x00, 0xF1, 0x05, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; 503 status = gatt_client_discover_characteristics_for_service_by_uuid128(handle_ble_client_event, gatt_client_handle, &services[0], characteristic_uuid); 504 CHECK_EQUAL(0, status); 505 CHECK_EQUAL(1, gatt_query_complete); 506 CHECK_EQUAL(1, result_counter); 507 508 reset_query_state(); 509 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 510 CHECK_EQUAL(0, status); 511 CHECK_EQUAL(1, gatt_query_complete); 512 CHECK_EQUAL(1, result_counter); 513 } 514 515 TEST(GATTClient, TestDiscoverCharacteristicDescriptor){ 516 test = DISCOVER_CHARACTERISTIC_DESCRIPTORS; 517 518 reset_query_state(); 519 // invalid con handle 520 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, HCI_CON_HANDLE_INVALID, service_uuid16); 521 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 522 523 set_wrong_gatt_client_state(); 524 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 525 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 526 527 reset_query_state(); 528 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 529 CHECK_EQUAL(0, status); 530 CHECK_EQUAL(1, gatt_query_complete); 531 CHECK_EQUAL(1, result_counter); 532 533 reset_query_state(); 534 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 535 CHECK_EQUAL(0, status); 536 CHECK_EQUAL(1, gatt_query_complete); 537 CHECK_EQUAL(1, result_counter); 538 539 reset_query_state(); 540 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 541 CHECK_EQUAL(0, status); 542 CHECK_EQUAL(1, gatt_query_complete); 543 CHECK(result_counter); 544 CHECK_EQUAL(3, result_index); 545 CHECK_EQUAL(0x2902, descriptors[0].uuid16); 546 CHECK_EQUAL(0x2900, descriptors[1].uuid16); 547 CHECK_EQUAL(0x2901, descriptors[2].uuid16); 548 } 549 550 TEST(GATTClient, TestWriteClientCharacteristicConfiguration){ 551 test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 552 reset_query_state(); 553 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 554 CHECK_EQUAL(0, status); 555 CHECK_EQUAL(1, gatt_query_complete); 556 CHECK_EQUAL(1, result_counter); 557 558 reset_query_state(); 559 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 560 CHECK_EQUAL(0, status); 561 CHECK_EQUAL(1, gatt_query_complete); 562 CHECK_EQUAL(1, result_counter); 563 564 // invalid con handle 565 status = gatt_client_write_client_characteristic_configuration(handle_ble_client_event, HCI_CON_HANDLE_INVALID, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 566 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 567 568 set_wrong_gatt_client_state(); 569 status = gatt_client_write_client_characteristic_configuration(handle_ble_client_event, gatt_client_handle, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 570 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 571 572 reset_query_state(); 573 status = gatt_client_write_client_characteristic_configuration(handle_ble_client_event, gatt_client_handle, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 574 CHECK_EQUAL(0, status); 575 CHECK_EQUAL(1, gatt_query_complete); 576 CHECK_EQUAL(1, result_counter); 577 578 reset_query_state(); 579 characteristics->properties = 0; 580 status = gatt_client_write_client_characteristic_configuration(handle_ble_client_event, gatt_client_handle, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 581 CHECK_EQUAL(GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED, status); 582 583 reset_query_state(); 584 characteristics->properties = 0; 585 status = gatt_client_write_client_characteristic_configuration(handle_ble_client_event, gatt_client_handle, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION); 586 CHECK_EQUAL(GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED, status); 587 } 588 589 TEST(GATTClient, TestReadCharacteristicDescriptor){ 590 test = READ_CHARACTERISTIC_DESCRIPTOR; 591 reset_query_state(); 592 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 593 CHECK_EQUAL(0, status); 594 CHECK_EQUAL(1, gatt_query_complete); 595 CHECK_EQUAL(1, result_counter); 596 597 reset_query_state(); 598 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 599 CHECK_EQUAL(0, status); 600 CHECK_EQUAL(1, gatt_query_complete); 601 CHECK_EQUAL(1, result_counter); 602 603 reset_query_state(); 604 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 605 CHECK_EQUAL(0, status); 606 CHECK_EQUAL(1, gatt_query_complete); 607 CHECK_EQUAL(3, result_counter); 608 609 // invalid con handle 610 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, HCI_CON_HANDLE_INVALID, &characteristics[0]); 611 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 612 613 set_wrong_gatt_client_state(); 614 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 615 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 616 617 reset_query_state(); 618 uint16_t end_handle = characteristics[0].end_handle; 619 characteristics[0].end_handle = characteristics[0].value_handle; 620 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 621 CHECK_EQUAL(0, status); 622 characteristics[0].end_handle = end_handle; 623 624 reset_query_state(); 625 status = gatt_client_read_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0]); 626 CHECK_EQUAL(0, status); 627 CHECK_EQUAL(1, gatt_query_complete); 628 CHECK_EQUAL(3, result_counter); 629 630 // invalid con handle 631 status = gatt_client_read_characteristic_descriptor(handle_ble_client_event, HCI_CON_HANDLE_INVALID, &descriptors[0]); 632 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 633 634 set_wrong_gatt_client_state(); 635 status = gatt_client_read_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0]); 636 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 637 } 638 639 TEST(GATTClient, gatt_client_read_value_of_characteristic_using_value_handle){ 640 test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 641 reset_query_state(); 642 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 643 CHECK_EQUAL(0, status); 644 CHECK_EQUAL(1, gatt_query_complete); 645 CHECK_EQUAL(1, result_counter); 646 647 reset_query_state(); 648 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 649 CHECK_EQUAL(0, status); 650 CHECK_EQUAL(1, gatt_query_complete); 651 CHECK_EQUAL(1, result_counter); 652 653 // invalid con handle 654 status = gatt_client_read_value_of_characteristic_using_value_handle(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].value_handle); 655 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 656 657 set_wrong_gatt_client_state(); 658 status = gatt_client_read_value_of_characteristic_using_value_handle(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle); 659 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 660 reset_query_state(); 661 } 662 663 TEST(GATTClient, gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset){ 664 test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 665 reset_query_state(); 666 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 667 CHECK_EQUAL(0, status); 668 CHECK_EQUAL(1, gatt_query_complete); 669 CHECK_EQUAL(1, result_counter); 670 671 reset_query_state(); 672 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 673 CHECK_EQUAL(0, status); 674 CHECK_EQUAL(1, gatt_query_complete); 675 CHECK_EQUAL(1, result_counter); 676 677 // invalid con handle 678 status = gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].value_handle, 0); 679 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 680 681 set_wrong_gatt_client_state(); 682 status = gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, 0); 683 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 684 reset_query_state(); 685 } 686 687 688 TEST(GATTClient, gatt_client_read_value_of_characteristics_by_uuid16){ 689 test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 690 reset_query_state(); 691 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 692 CHECK_EQUAL(0, status); 693 CHECK_EQUAL(1, gatt_query_complete); 694 CHECK_EQUAL(1, result_counter); 695 696 reset_query_state(); 697 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 698 CHECK_EQUAL(0, status); 699 CHECK_EQUAL(1, gatt_query_complete); 700 CHECK_EQUAL(1, result_counter); 701 702 reset_query_state(); 703 status = gatt_client_read_value_of_characteristics_by_uuid16(handle_ble_client_event, gatt_client_handle, characteristics[0].start_handle, characteristics[0].end_handle, characteristics[0].uuid16); 704 CHECK_EQUAL(0, status); 705 CHECK_EQUAL(1, gatt_query_complete); 706 707 // invalid con handle 708 status = gatt_client_read_value_of_characteristics_by_uuid16(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].start_handle, characteristics[0].end_handle, characteristics[0].uuid16); 709 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 710 711 set_wrong_gatt_client_state(); 712 status = gatt_client_read_value_of_characteristics_by_uuid16(handle_ble_client_event, gatt_client_handle, characteristics[0].start_handle, characteristics[0].end_handle, characteristics[0].uuid16); 713 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 714 reset_query_state(); 715 } 716 717 TEST(GATTClient, gatt_client_read_value_of_characteristics_by_uuid128){ 718 test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 719 reset_query_state(); 720 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 721 CHECK_EQUAL(0, status); 722 CHECK_EQUAL(1, gatt_query_complete); 723 CHECK_EQUAL(1, result_counter); 724 725 reset_query_state(); 726 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 727 CHECK_EQUAL(0, status); 728 CHECK_EQUAL(1, gatt_query_complete); 729 CHECK_EQUAL(1, result_counter); 730 731 reset_query_state(); 732 status = gatt_client_read_value_of_characteristics_by_uuid128(handle_ble_client_event, gatt_client_handle, characteristics[0].start_handle, characteristics[0].end_handle, characteristics[0].uuid128); 733 CHECK_EQUAL(0, status); 734 CHECK_EQUAL(1, gatt_query_complete); 735 736 // invalid con handle 737 status = gatt_client_read_value_of_characteristics_by_uuid128(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].start_handle, characteristics[0].end_handle, characteristics[0].uuid128); 738 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 739 740 set_wrong_gatt_client_state(); 741 status = gatt_client_read_value_of_characteristics_by_uuid128(handle_ble_client_event, gatt_client_handle, characteristics[0].start_handle, characteristics[0].end_handle, characteristics[0].uuid128); 742 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 743 reset_query_state(); 744 } 745 746 TEST(GATTClient, gatt_client_write_characteristic_descriptor_using_descriptor_handle){ 747 test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; 748 reset_query_state(); 749 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 750 CHECK_EQUAL(0, status); 751 CHECK_EQUAL(1, gatt_query_complete); 752 CHECK_EQUAL(1, result_counter); 753 754 reset_query_state(); 755 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 756 CHECK_EQUAL(0, status); 757 CHECK_EQUAL(1, gatt_query_complete); 758 CHECK_EQUAL(1, result_counter); 759 760 761 reset_query_state(); 762 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 763 CHECK_EQUAL(0, status); 764 CHECK_EQUAL(1, gatt_query_complete); 765 CHECK_EQUAL(3, result_counter); 766 767 reset_query_state(); 768 status = gatt_client_write_characteristic_descriptor_using_descriptor_handle(handle_ble_client_event, gatt_client_handle, descriptors[0].handle, characteristics[0].end_handle, characteristics[0].uuid128); 769 CHECK_EQUAL(0, status); 770 771 // invalid con handle 772 status = gatt_client_write_characteristic_descriptor_using_descriptor_handle(handle_ble_client_event, HCI_CON_HANDLE_INVALID, descriptors[0].handle, characteristics[0].end_handle, characteristics[0].uuid128); 773 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 774 775 set_wrong_gatt_client_state(); 776 status = gatt_client_write_characteristic_descriptor_using_descriptor_handle(handle_ble_client_event, gatt_client_handle, descriptors[0].handle, characteristics[0].end_handle, characteristics[0].uuid128); 777 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 778 reset_query_state(); 779 } 780 781 TEST(GATTClient, gatt_client_prepare_write){ 782 reset_query_state(); 783 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 784 CHECK_EQUAL(0, status); 785 CHECK_EQUAL(1, gatt_query_complete); 786 CHECK_EQUAL(1, result_counter); 787 788 reset_query_state(); 789 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 790 CHECK_EQUAL(0, status); 791 CHECK_EQUAL(1, gatt_query_complete); 792 CHECK_EQUAL(1, result_counter); 793 794 reset_query_state(); 795 status = gatt_client_prepare_write(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, 0, short_value_length, (uint8_t*)short_value); 796 CHECK_EQUAL(0, status); 797 798 // invalid con handle 799 status = gatt_client_prepare_write(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].value_handle, 0, short_value_length, (uint8_t*)short_value); 800 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 801 802 set_wrong_gatt_client_state(); 803 status = gatt_client_prepare_write(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, 0, short_value_length, (uint8_t*)short_value); 804 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 805 reset_query_state(); 806 } 807 808 TEST(GATTClient, gatt_client_execute_write){ 809 reset_query_state(); 810 status = gatt_client_execute_write(handle_ble_client_event, gatt_client_handle); 811 CHECK_EQUAL(0, status); 812 813 // invalid con handle 814 status = gatt_client_execute_write(handle_ble_client_event, HCI_CON_HANDLE_INVALID); 815 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 816 817 set_wrong_gatt_client_state(); 818 status = gatt_client_execute_write(handle_ble_client_event, gatt_client_handle); 819 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 820 reset_query_state(); 821 } 822 823 TEST(GATTClient, gatt_client_cancel_write){ 824 reset_query_state(); 825 status = gatt_client_cancel_write(handle_ble_client_event, gatt_client_handle); 826 CHECK_EQUAL(0, status); 827 828 // invalid con handle 829 status = gatt_client_cancel_write(handle_ble_client_event, HCI_CON_HANDLE_INVALID); 830 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 831 832 set_wrong_gatt_client_state(); 833 status = gatt_client_cancel_write(handle_ble_client_event, gatt_client_handle); 834 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 835 reset_query_state(); 836 } 837 838 TEST(GATTClient, TestReadCharacteristicValue){ 839 test = READ_CHARACTERISTIC_VALUE; 840 reset_query_state(); 841 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 842 CHECK_EQUAL(0, status); 843 CHECK_EQUAL(1, gatt_query_complete); 844 CHECK_EQUAL(1, result_counter); 845 846 reset_query_state(); 847 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 848 CHECK_EQUAL(0, status); 849 CHECK_EQUAL(1, gatt_query_complete); 850 CHECK_EQUAL(1, result_counter); 851 852 reset_query_state(); 853 status = gatt_client_read_value_of_characteristic(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 854 CHECK_EQUAL(0, status); 855 CHECK_EQUAL(1, gatt_query_complete); 856 CHECK_EQUAL(3, result_counter); 857 } 858 859 TEST(GATTClient, TestWriteCharacteristicValue){ 860 test = WRITE_CHARACTERISTIC_VALUE; 861 reset_query_state(); 862 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 863 CHECK_EQUAL(0, status); 864 CHECK_EQUAL(1, gatt_query_complete); 865 CHECK_EQUAL(1, result_counter); 866 867 reset_query_state(); 868 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 869 CHECK_EQUAL(0, status); 870 CHECK_EQUAL(1, gatt_query_complete); 871 CHECK_EQUAL(1, result_counter); 872 873 // invalid con handle 874 status = gatt_client_write_value_of_characteristic(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].value_handle, short_value_length, (uint8_t*)short_value); 875 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 876 877 set_wrong_gatt_client_state(); 878 status = gatt_client_write_value_of_characteristic(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, short_value_length, (uint8_t*)short_value); 879 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 880 881 882 reset_query_state(); 883 status = gatt_client_write_value_of_characteristic(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, short_value_length, (uint8_t*)short_value); 884 CHECK_EQUAL(0, status); 885 CHECK_EQUAL(1, gatt_query_complete); 886 } 887 888 TEST(GATTClient, TestWriteCharacteristicDescriptor){ 889 test = WRITE_CHARACTERISTIC_DESCRIPTOR; 890 reset_query_state(); 891 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 892 CHECK_EQUAL(0, status); 893 CHECK_EQUAL(1, gatt_query_complete); 894 CHECK_EQUAL(1, result_counter); 895 896 reset_query_state(); 897 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 898 CHECK_EQUAL(0, status); 899 CHECK_EQUAL(1, gatt_query_complete); 900 CHECK_EQUAL(1, result_counter); 901 902 reset_query_state(); 903 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 904 CHECK_EQUAL(0, status); 905 CHECK_EQUAL(1, gatt_query_complete); 906 CHECK_EQUAL(3, result_counter); 907 908 reset_query_state(); 909 status = gatt_client_write_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0], sizeof(indication), indication); 910 CHECK_EQUAL(0, status); 911 CHECK_EQUAL(1, gatt_query_complete); 912 } 913 914 TEST(GATTClient, TestReadLongCharacteristicValue){ 915 test = READ_LONG_CHARACTERISTIC_VALUE; 916 reset_query_state(); 917 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 918 CHECK_EQUAL(0, status); 919 CHECK_EQUAL(1, gatt_query_complete); 920 CHECK_EQUAL(1, result_counter); 921 922 reset_query_state(); 923 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 924 CHECK_EQUAL(0, status); 925 CHECK_EQUAL(1, gatt_query_complete); 926 CHECK_EQUAL(1, result_counter); 927 928 reset_query_state(); 929 status = gatt_client_read_long_value_of_characteristic(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 930 CHECK_EQUAL(0, status); 931 CHECK_EQUAL(1, gatt_query_complete); 932 CHECK_EQUAL(4, result_counter); 933 } 934 935 TEST(GATTClient, TestReadLongCharacteristicDescriptor){ 936 test = READ_LONG_CHARACTERISTIC_DESCRIPTOR; 937 reset_query_state(); 938 status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128); 939 CHECK_EQUAL(0, status); 940 CHECK_EQUAL(1, gatt_query_complete); 941 CHECK_EQUAL(1, result_counter); 942 943 reset_query_state(); 944 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF200); 945 CHECK_EQUAL(0, status); 946 CHECK_EQUAL(1, gatt_query_complete); 947 CHECK_EQUAL(1, result_counter); 948 949 reset_query_state(); 950 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 951 CHECK_EQUAL(0, status); 952 CHECK_EQUAL(1, gatt_query_complete); 953 CHECK_EQUAL(3, result_counter); 954 955 reset_query_state(); 956 result_counter = 0; 957 status = gatt_client_read_long_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0]); 958 CHECK_EQUAL(0, status); 959 CHECK_EQUAL(1, gatt_query_complete); 960 CHECK_EQUAL(4, result_counter); 961 } 962 963 964 TEST(GATTClient, TestWriteLongCharacteristicDescriptor){ 965 test = WRITE_LONG_CHARACTERISTIC_DESCRIPTOR; 966 reset_query_state(); 967 status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128); 968 CHECK_EQUAL(0, status); 969 CHECK_EQUAL(1, gatt_query_complete); 970 CHECK_EQUAL(1, result_counter); 971 972 reset_query_state(); 973 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF200); 974 CHECK_EQUAL(0, status); 975 CHECK_EQUAL(1, gatt_query_complete); 976 CHECK_EQUAL(1, result_counter); 977 978 reset_query_state(); 979 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 980 CHECK_EQUAL(0, status); 981 CHECK_EQUAL(1, gatt_query_complete); 982 CHECK_EQUAL(3, result_counter); 983 984 result_counter = 0; 985 status = gatt_client_write_long_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0], sizeof(long_value), (uint8_t *)long_value); 986 CHECK_EQUAL(0, status); 987 CHECK_EQUAL(1, gatt_query_complete); 988 CHECK_EQUAL(1, result_counter); 989 } 990 991 TEST(GATTClient, TestWriteLongCharacteristicValue){ 992 test = WRITE_LONG_CHARACTERISTIC_VALUE; 993 reset_query_state(); 994 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 995 CHECK_EQUAL(0, status); 996 CHECK_EQUAL(1, gatt_query_complete); 997 CHECK_EQUAL(1, result_counter); 998 999 reset_query_state(); 1000 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 1001 CHECK_EQUAL(0, status); 1002 CHECK_EQUAL(1, gatt_query_complete); 1003 CHECK_EQUAL(1, result_counter); 1004 1005 1006 reset_query_state(); 1007 status = gatt_client_write_long_value_of_characteristic(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1008 CHECK_EQUAL(0, status); 1009 CHECK_EQUAL(1, gatt_query_complete); 1010 } 1011 1012 TEST(GATTClient, TestWriteReliableLongCharacteristicValue){ 1013 test = WRITE_RELIABLE_LONG_CHARACTERISTIC_VALUE; 1014 reset_query_state(); 1015 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 1016 CHECK_EQUAL(0, status); 1017 CHECK_EQUAL(1, gatt_query_complete); 1018 CHECK_EQUAL(1, result_counter); 1019 1020 reset_query_state(); 1021 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 1022 CHECK_EQUAL(0, status); 1023 CHECK_EQUAL(1, gatt_query_complete); 1024 CHECK_EQUAL(1, result_counter); 1025 1026 // invalid con handle 1027 status = gatt_client_reliable_write_long_value_of_characteristic(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1028 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1029 1030 set_wrong_gatt_client_state(); 1031 status = gatt_client_reliable_write_long_value_of_characteristic(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1032 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1033 1034 reset_query_state(); 1035 status = gatt_client_reliable_write_long_value_of_characteristic(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1036 CHECK_EQUAL(0, status); 1037 CHECK_EQUAL(1, gatt_query_complete); 1038 } 1039 1040 TEST(GATTClient, gatt_client_write_long_value_of_characteristic_with_offset){ 1041 reset_query_state(); 1042 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 1043 CHECK_EQUAL(0, status); 1044 CHECK_EQUAL(1, gatt_query_complete); 1045 CHECK_EQUAL(1, result_counter); 1046 1047 reset_query_state(); 1048 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 1049 CHECK_EQUAL(0, status); 1050 CHECK_EQUAL(1, gatt_query_complete); 1051 CHECK_EQUAL(1, result_counter); 1052 1053 reset_query_state(); 1054 status = gatt_client_write_long_value_of_characteristic_with_offset(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, 0, long_value_length, (uint8_t*)long_value); 1055 CHECK_EQUAL(0, status); 1056 1057 reset_query_state(); 1058 // invalid con handle 1059 status = gatt_client_write_long_value_of_characteristic_with_offset(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].value_handle, 0, long_value_length, (uint8_t*)long_value); 1060 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1061 1062 reset_query_state(); 1063 set_wrong_gatt_client_state(); 1064 status = gatt_client_write_long_value_of_characteristic_with_offset(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, 0, long_value_length, (uint8_t*)long_value); 1065 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1066 } 1067 1068 TEST(GATTClient, gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset){ 1069 reset_query_state(); 1070 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 1071 CHECK_EQUAL(0, status); 1072 CHECK_EQUAL(1, gatt_query_complete); 1073 CHECK_EQUAL(1, result_counter); 1074 1075 reset_query_state(); 1076 status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]); 1077 CHECK_EQUAL(0, status); 1078 CHECK_EQUAL(1, gatt_query_complete); 1079 CHECK(result_counter); 1080 CHECK_EQUAL(3, result_index); 1081 CHECK_EQUAL(0x2902, descriptors[0].uuid16); 1082 CHECK_EQUAL(0x2900, descriptors[1].uuid16); 1083 CHECK_EQUAL(0x2901, descriptors[2].uuid16); 1084 1085 reset_query_state(); 1086 status = gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(handle_ble_client_event, gatt_client_handle, descriptors[0].handle, 0); 1087 CHECK_EQUAL(0, status); 1088 1089 reset_query_state(); 1090 // invalid con handle 1091 status = gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(handle_ble_client_event, HCI_CON_HANDLE_INVALID, descriptors[0].handle, 0); 1092 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1093 1094 reset_query_state(); 1095 set_wrong_gatt_client_state(); 1096 status = gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(handle_ble_client_event, gatt_client_handle, descriptors[0].handle, 0); 1097 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1098 } 1099 1100 TEST(GATTClient, gatt_client_read_multiple_characteristic_values){ 1101 reset_query_state(); 1102 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 1103 CHECK_EQUAL(0, status); 1104 CHECK_EQUAL(1, gatt_query_complete); 1105 CHECK_EQUAL(1, result_counter); 1106 1107 reset_query_state(); 1108 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 1109 CHECK_EQUAL(0, status); 1110 CHECK_EQUAL(1, gatt_query_complete); 1111 CHECK_EQUAL(1, result_counter); 1112 1113 uint16_t value_handles[] = {characteristics[0].value_handle}; 1114 1115 reset_query_state(); 1116 status = gatt_client_read_multiple_characteristic_values(handle_ble_client_event, gatt_client_handle, 1, value_handles); 1117 CHECK_EQUAL(0, status); 1118 1119 reset_query_state(); 1120 // invalid con handle 1121 status = gatt_client_read_multiple_characteristic_values(handle_ble_client_event, HCI_CON_HANDLE_INVALID, 1, value_handles); 1122 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1123 1124 reset_query_state(); 1125 set_wrong_gatt_client_state(); 1126 status = gatt_client_read_multiple_characteristic_values(handle_ble_client_event, gatt_client_handle, 1, value_handles); 1127 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1128 } 1129 1130 TEST(GATTClient, gatt_client_write_value_of_characteristic_without_response){ 1131 reset_query_state(); 1132 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 1133 CHECK_EQUAL(0, status); 1134 CHECK_EQUAL(1, gatt_query_complete); 1135 CHECK_EQUAL(1, result_counter); 1136 1137 reset_query_state(); 1138 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 1139 CHECK_EQUAL(0, status); 1140 CHECK_EQUAL(1, gatt_query_complete); 1141 CHECK_EQUAL(1, result_counter); 1142 1143 uint16_t value_handles[] = {characteristics[0].value_handle}; 1144 1145 reset_query_state(); 1146 // invalid con handle 1147 status = gatt_client_write_value_of_characteristic_without_response(HCI_CON_HANDLE_INVALID, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1148 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1149 1150 reset_query_state(); 1151 set_wrong_gatt_client_state(); 1152 status = gatt_client_write_value_of_characteristic_without_response(gatt_client_handle, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1153 CHECK_EQUAL(GATT_CLIENT_VALUE_TOO_LONG, status); 1154 1155 reset_query_state(); 1156 1157 status = gatt_client_write_value_of_characteristic_without_response(gatt_client_handle, characteristics[0].value_handle, 19, (uint8_t*)long_value); 1158 CHECK_EQUAL(0, status); 1159 } 1160 1161 TEST(GATTClient, gatt_client_is_ready){ 1162 int status = gatt_client_is_ready(HCI_CON_HANDLE_INVALID); 1163 CHECK_EQUAL(0, status); 1164 1165 status = gatt_client_is_ready(gatt_client_handle); 1166 CHECK_EQUAL(1, status); 1167 } 1168 1169 1170 TEST(GATTClient, register_for_notification){ 1171 reset_query_state(); 1172 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 1173 CHECK_EQUAL(0, status); 1174 CHECK_EQUAL(1, gatt_query_complete); 1175 CHECK_EQUAL(1, result_counter); 1176 1177 reset_query_state(); 1178 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 1179 CHECK_EQUAL(0, status); 1180 CHECK_EQUAL(1, gatt_query_complete); 1181 CHECK_EQUAL(1, result_counter); 1182 1183 gatt_client_notification_t notification; 1184 1185 gatt_client_listen_for_characteristic_value_updates(¬ification, handle_ble_client_event, gatt_client_handle, &characteristics[0]); 1186 gatt_client_stop_listening_for_characteristic_value_updates(¬ification); 1187 1188 gatt_client_listen_for_characteristic_value_updates(¬ification, handle_ble_client_event, gatt_client_handle, NULL); 1189 gatt_client_stop_listening_for_characteristic_value_updates(¬ification); 1190 } 1191 1192 TEST(GATTClient, gatt_client_signed_write_without_response){ 1193 reset_query_state(); 1194 status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16); 1195 CHECK_EQUAL(0, status); 1196 CHECK_EQUAL(1, gatt_query_complete); 1197 CHECK_EQUAL(1, result_counter); 1198 1199 reset_query_state(); 1200 status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100); 1201 CHECK_EQUAL(0, status); 1202 CHECK_EQUAL(1, gatt_query_complete); 1203 CHECK_EQUAL(1, result_counter); 1204 1205 reset_query_state(); 1206 // invalid con handle 1207 status = gatt_client_signed_write_without_response(handle_ble_client_event, HCI_CON_HANDLE_INVALID, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1208 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1209 1210 reset_query_state(); 1211 set_wrong_gatt_client_state(); 1212 status = gatt_client_signed_write_without_response(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1213 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1214 1215 reset_query_state(); 1216 1217 status = gatt_client_signed_write_without_response(handle_ble_client_event, gatt_client_handle, characteristics[0].value_handle, long_value_length, (uint8_t*)long_value); 1218 CHECK_EQUAL(0, status); 1219 } 1220 1221 TEST(GATTClient, gatt_client_discover_secondary_services){ 1222 reset_query_state(); 1223 // invalid con handle 1224 status = gatt_client_discover_secondary_services(handle_ble_client_event, HCI_CON_HANDLE_INVALID); 1225 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1226 1227 reset_query_state(); 1228 set_wrong_gatt_client_state(); 1229 status = gatt_client_discover_secondary_services(handle_ble_client_event, gatt_client_handle); 1230 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1231 1232 reset_query_state(); 1233 1234 status = gatt_client_discover_secondary_services(handle_ble_client_event, gatt_client_handle); 1235 CHECK_EQUAL(0, status); 1236 } 1237 1238 TEST(GATTClient, gatt_client_request_can_write_without_response_event){ 1239 int status = gatt_client_request_can_write_without_response_event(handle_ble_client_event, HCI_CON_HANDLE_INVALID); 1240 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1241 1242 gatt_client_t * gatt_client = gatt_client_get_client(gatt_client_handle); 1243 CHECK_TRUE(gatt_client != NULL); 1244 btstack_packet_handler_t write_without_response_callback; 1245 gatt_client->write_without_response_callback = write_without_response_callback; 1246 status = gatt_client_request_can_write_without_response_event(handle_ble_client_event, gatt_client_handle); 1247 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1248 1249 gatt_client->write_without_response_callback = NULL; 1250 status = gatt_client_request_can_write_without_response_event(handle_ble_client_event, gatt_client_handle); 1251 CHECK_EQUAL(ERROR_CODE_SUCCESS, status); 1252 } 1253 1254 TEST(GATTClient, gatt_client_send_mtu_negotiation){ 1255 gatt_client_send_mtu_negotiation(handle_ble_client_event, HCI_CON_HANDLE_INVALID); 1256 1257 gatt_client_send_mtu_negotiation(handle_ble_client_event, gatt_client_handle); 1258 1259 gatt_client_t * gatt_client = gatt_client_get_client(gatt_client_handle); 1260 CHECK_TRUE(gatt_client != NULL); 1261 gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 1262 gatt_client_send_mtu_negotiation(handle_ble_client_event, gatt_client_handle); 1263 1264 gatt_client->mtu_state = SEND_MTU_EXCHANGE; 1265 } 1266 1267 TEST(GATTClient, gatt_client_get_mtu){ 1268 reset_query_state(); 1269 uint16_t mtu; 1270 int status = gatt_client_get_mtu(HCI_CON_HANDLE_INVALID, &mtu); 1271 CHECK_EQUAL(BTSTACK_MEMORY_ALLOC_FAILED, status); 1272 1273 status = gatt_client_get_mtu(gatt_client_handle, &mtu); 1274 CHECK_EQUAL(GATT_CLIENT_IN_WRONG_STATE, status); 1275 CHECK_EQUAL(ATT_DEFAULT_MTU, mtu); 1276 1277 gatt_client_t * gatt_client = gatt_client_get_client(gatt_client_handle); 1278 CHECK_TRUE(gatt_client != NULL); 1279 gatt_client->mtu = 30; 1280 1281 gatt_client->mtu_state = MTU_EXCHANGED; 1282 status = gatt_client_get_mtu(gatt_client_handle, &mtu); 1283 CHECK_EQUAL(0, status); 1284 CHECK_EQUAL(gatt_client->mtu, mtu); 1285 1286 gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED; 1287 status = gatt_client_get_mtu(gatt_client_handle, &mtu); 1288 CHECK_EQUAL(0, status); 1289 CHECK_EQUAL(gatt_client->mtu, mtu); 1290 1291 gatt_client->mtu_state = SEND_MTU_EXCHANGE; 1292 } 1293 1294 1295 int main (int argc, const char * argv[]){ 1296 att_set_db(profile_data); 1297 att_set_write_callback(&att_write_callback); 1298 att_set_read_callback(&att_read_callback); 1299 1300 gatt_client_init(); 1301 1302 return CommandLineTestRunner::RunAllTests(argc, argv); 1303 } 1304