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