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