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