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