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