xref: /btstack/test/gatt_client/gatt_client_test.cpp (revision 46d6c6044a82d8cc98f35a2ca93e895adb50ca82)
1 
2 // *****************************************************************************
3 //
4 // test rfcomm query tests
5 //
6 // *****************************************************************************
7 
8 
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "CppUTest/TestHarness.h"
15 #include "CppUTest/CommandLineTestRunner.h"
16 
17 #include "hci_cmd.h"
18 
19 #include "btstack_memory.h"
20 #include "hci.h"
21 #include "hci_dump.h"
22 #include "ble/gatt_client.h"
23 #include "ble/att_db.h"
24 #include "profile.h"
25 #include "expected_results.h"
26 
27 static uint16_t gatt_client_handle = 0x40;
28 static int gatt_query_complete = 0;
29 
30 typedef enum {
31 	IDLE,
32 	DISCOVER_PRIMARY_SERVICES,
33     DISCOVER_PRIMARY_SERVICE_WITH_UUID16,
34     DISCOVER_PRIMARY_SERVICE_WITH_UUID128,
35 
36     DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID16,
37     DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID128,
38 
39     DISCOVER_CHARACTERISTICS_FOR_SERVICE_WITH_UUID16,
40     DISCOVER_CHARACTERISTICS_FOR_SERVICE_WITH_UUID128,
41     DISCOVER_CHARACTERISTICS_BY_UUID16,
42     DISCOVER_CHARACTERISTICS_BY_UUID128,
43 	DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID,
44 
45 	READ_CHARACTERISTIC_VALUE,
46 	READ_LONG_CHARACTERISTIC_VALUE,
47 	WRITE_CHARACTERISTIC_VALUE,
48 	WRITE_LONG_CHARACTERISTIC_VALUE,
49 
50     DISCOVER_CHARACTERISTIC_DESCRIPTORS,
51     READ_CHARACTERISTIC_DESCRIPTOR,
52     WRITE_CHARACTERISTIC_DESCRIPTOR,
53     WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION,
54     READ_LONG_CHARACTERISTIC_DESCRIPTOR,
55     WRITE_LONG_CHARACTERISTIC_DESCRIPTOR,
56     WRITE_RELIABLE_LONG_CHARACTERISTIC_VALUE,
57     WRITE_CHARACTERISTIC_VALUE_WITHOUT_RESPONSE
58 } current_test_t;
59 
60 current_test_t test = IDLE;
61 
62 uint8_t  characteristic_uuid128[] = {0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
63 uint16_t characteristic_uuid16 = 0xF000;
64 
65 static int result_index;
66 static uint8_t result_counter;
67 
68 static gatt_client_service_t services[50];
69 static gatt_client_service_t included_services[50];
70 
71 static gatt_client_characteristic_t characteristics[50];
72 static gatt_client_characteristic_descriptor_t descriptors[50];
73 
74 void mock_simulate_discover_primary_services_response(void);
75 void mock_simulate_att_exchange_mtu_response(void);
76 
77 void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){
78 	for (int i=0; i<size; i++){
79 		BYTES_EQUAL(expected[i], actual[i]);
80 	}
81 }
82 
83 void pUUID128(const uint8_t *uuid) {
84     printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
85            uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
86            uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
87 }
88 
89 //static int counter = 0;
90 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){
91 	CHECK_EQUAL_ARRAY(exp_uuid, uuid, 16);
92 	if (!exp_handles) return;
93 	CHECK_EQUAL(exp_handles[0], start_handle);
94  	CHECK_EQUAL(exp_handles[1], end_handle);
95 }
96 
97 // -----------------------------------------------------
98 
99 static void verify_primary_services_with_uuid16(void){
100 	CHECK_EQUAL(1, result_index);
101 	CHECK_EQUAL_GATT_ATTRIBUTE(primary_service_uuid16,  primary_service_uuid16_handles, services[0].uuid128, services[0].start_group_handle, services[0].end_group_handle);
102 }
103 
104 
105 static void verify_primary_services_with_uuid128(void){
106 	CHECK_EQUAL(1, result_index);
107 	CHECK_EQUAL_GATT_ATTRIBUTE(primary_service_uuid128, primary_service_uuid128_handles, services[0].uuid128, services[0].start_group_handle, services[0].end_group_handle);
108 }
109 
110 static void verify_primary_services(void){
111 	CHECK_EQUAL(6, result_index);
112 	for (int i=0; i<result_index; i++){
113 		CHECK_EQUAL_GATT_ATTRIBUTE(primary_service_uuids[i], NULL, services[i].uuid128, services[i].start_group_handle, services[i].end_group_handle);
114 	}
115 }
116 
117 static void verify_included_services_uuid16(void){
118 	CHECK_EQUAL(1, result_index);
119 	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);
120 }
121 
122 static void verify_included_services_uuid128(void){
123 	CHECK_EQUAL(2, result_index);
124 	for (int i=0; i<result_index; i++){
125 		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);
126 	}
127 }
128 
129 static void verify_charasteristics(void){
130 	CHECK_EQUAL(15, result_index);
131 	for (int i=0; i<result_index; i++){
132 		CHECK_EQUAL_GATT_ATTRIBUTE(characteristic_uuids[i], characteristic_handles[i], characteristics[i].uuid128, characteristics[i].start_handle, characteristics[i].end_handle);
133     }
134 }
135 
136 static void verify_blob(uint16_t value_length, uint16_t value_offset, uint8_t * value){
137 	uint8_t * expected_value = (uint8_t*)&long_value[value_offset];
138     CHECK(value_length);
139 	CHECK_EQUAL_ARRAY(expected_value, value, value_length);
140     if (value_offset + value_length != sizeof(long_value)) return;
141     result_counter++;
142 }
143 
144 static void handle_ble_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
145 	if (packet_type != HCI_EVENT_PACKET) return;
146 	uint8_t status;
147 	gatt_client_service_t service;
148 	gatt_client_characteristic_t characteristic;
149 	gatt_client_characteristic_descriptor_t descriptor;
150 	switch (packet[0]){
151 		case GATT_EVENT_QUERY_COMPLETE:
152 			status = packet[4];
153             gatt_query_complete = 1;
154             if (status){
155                 gatt_query_complete = 0;
156                 printf("GATT_EVENT_QUERY_COMPLETE failed with status 0x%02X\n", status);
157             }
158             break;
159 		case GATT_EVENT_SERVICE_QUERY_RESULT:
160 			service.start_group_handle = little_endian_read_16(packet, 4);
161 			service.end_group_handle   = little_endian_read_16(packet, 6);
162 			service.uuid16 = 0;
163 			reverse_128(&packet[8], service.uuid128);
164 			if (uuid_has_bluetooth_prefix(service.uuid128)){
165 				service.uuid16 = big_endian_read_32(service.uuid128, 0);
166 			}
167 			services[result_index++] = service;
168 			result_counter++;
169             break;
170         case GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT:
171 			service.start_group_handle = little_endian_read_16(packet, 6);
172 			service.end_group_handle   = little_endian_read_16(packet, 8);
173 			service.uuid16 = 0;
174 			reverse_128(&packet[10], service.uuid128);
175 			if (uuid_has_bluetooth_prefix(service.uuid128)){
176 				service.uuid16 = big_endian_read_32(service.uuid128, 0);
177 			}
178             included_services[result_index++] = service;
179             result_counter++;
180             break;
181         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
182         	characteristic.start_handle = little_endian_read_16(packet, 4);
183         	characteristic.value_handle = little_endian_read_16(packet, 6);
184         	characteristic.end_handle =   little_endian_read_16(packet, 8);
185         	characteristic.properties =   little_endian_read_16(packet, 10);
186 			characteristic.uuid16 = 0;
187 			reverse_128(&packet[12], characteristic.uuid128);
188 			if (uuid_has_bluetooth_prefix(characteristic.uuid128)){
189 				characteristic.uuid16 = big_endian_read_32(characteristic.uuid128, 0);
190 			}
191         	characteristics[result_index++] = characteristic;
192         	result_counter++;
193             break;
194         case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
195         	descriptor.handle = little_endian_read_16(packet, 4);
196 			reverse_128(&packet[6], descriptor.uuid128);
197 			if (uuid_has_bluetooth_prefix(descriptor.uuid128)){
198 				descriptor.uuid16 = big_endian_read_32(descriptor.uuid128, 0);
199 			}
200         	descriptors[result_index++] = descriptor;
201         	result_counter++;
202         	break;
203         case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
204         case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
205         	CHECK_EQUAL(short_value_length, little_endian_read_16(packet, 6));
206         	CHECK_EQUAL_ARRAY((uint8_t*)short_value, &packet[8], short_value_length);
207         	result_counter++;
208         	break;
209         case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
210         case GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
211         	verify_blob(little_endian_read_16(packet, 8), little_endian_read_16(packet, 6), &packet[10]);
212         	result_counter++;
213         	break;
214 	}
215 }
216 
217 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){
218 	switch(test){
219 		case WRITE_CHARACTERISTIC_DESCRIPTOR:
220 		case WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
221 			CHECK_EQUAL(ATT_TRANSACTION_MODE_NONE, transaction_mode);
222 			CHECK_EQUAL(0, offset);
223 			CHECK_EQUAL_ARRAY(indication, buffer, 2);
224 			result_counter++;
225 			break;
226 		case WRITE_CHARACTERISTIC_VALUE:
227 			CHECK_EQUAL(ATT_TRANSACTION_MODE_NONE, transaction_mode);
228 			CHECK_EQUAL(0, offset);
229 			CHECK_EQUAL_ARRAY((uint8_t *)short_value, buffer, short_value_length);
230     		result_counter++;
231 			break;
232 		case WRITE_LONG_CHARACTERISTIC_DESCRIPTOR:
233 		case WRITE_LONG_CHARACTERISTIC_VALUE:
234 		case WRITE_RELIABLE_LONG_CHARACTERISTIC_VALUE:
235 			if (transaction_mode == ATT_TRANSACTION_MODE_VALIDATE) break;
236 			if (transaction_mode == ATT_TRANSACTION_MODE_EXECUTE) break;
237 			CHECK_EQUAL(ATT_TRANSACTION_MODE_ACTIVE, transaction_mode);
238 			CHECK_EQUAL_ARRAY((uint8_t *)&long_value[offset], buffer, buffer_size);
239 			if (offset + buffer_size != sizeof(long_value)) break;
240 			result_counter++;
241 			break;
242 		default:
243 			break;
244 	}
245 	return 0;
246 }
247 
248 int copy_bytes(uint8_t * value, uint16_t value_length, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
249 	int blob_length = value_length - offset;
250 	if (blob_length >= buffer_size) blob_length = buffer_size;
251 
252 	memcpy(buffer, &value[offset], blob_length);
253 	return blob_length;
254 }
255 
256 extern "C" uint16_t att_read_callback(uint16_t handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
257 	//printf("gatt client test, att_read_callback_t handle 0x%04x, offset %u, buffer %p, buffer_size %u\n", handle, offset, buffer, buffer_size);
258 	switch(test){
259 		case READ_CHARACTERISTIC_DESCRIPTOR:
260 		case READ_CHARACTERISTIC_VALUE:
261 			result_counter++;
262 			if (buffer){
263 				return copy_bytes((uint8_t *)short_value, short_value_length, offset, buffer, buffer_size);
264 			}
265 			return short_value_length;
266 		case READ_LONG_CHARACTERISTIC_DESCRIPTOR:
267 		case READ_LONG_CHARACTERISTIC_VALUE:
268 			result_counter++;
269 			if (buffer) {
270 				return copy_bytes((uint8_t *)long_value, long_value_length, offset, buffer, buffer_size);
271 			}
272 			return long_value_length;
273 		default:
274 			break;
275 	}
276 	return 0;
277 }
278 
279 // static const char * decode_status(uint8_t status){
280 // 	switch (status){
281 // 		case 0: return "0";
282 //     	case GATT_CLIENT_IN_WRONG_STATE: return "GATT_CLIENT_IN_WRONG_STATE";
283 //     	case GATT_CLIENT_DIFFERENT_CONTEXT_FOR_ADDRESS_ALREADY_EXISTS: return "GATT_CLIENT_DIFFERENT_CONTEXT_FOR_ADDRESS_ALREADY_EXISTS";
284 //     	case GATT_CLIENT_NOT_CONNECTED: return "GATT_CLIENT_NOT_CONNECTED";
285 //     	case GATT_CLIENT_VALUE_TOO_LONG: return "GATT_CLIENT_VALUE_TOO_LONG";
286 // 	    case GATT_CLIENT_BUSY: return "GATT_CLIENT_BUSY";
287 // 	    case GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED: return "GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED";
288 // 	    case GATT_CLIENTCHARACTERISTIC_INDICATION_NOT_SUPPORTED: return "GATT_CLIENTCHARACTERISTIC_INDICATION_NOT_SUPPORTED";
289 // 	}
290 // }
291 
292 TEST_GROUP(GATTClient){
293 	int acl_buffer_size;
294     uint8_t acl_buffer[27];
295     uint8_t status;
296 
297 	void setup(void){
298 		result_counter = 0;
299 		result_index = 0;
300 		test = IDLE;
301 	}
302 
303 	void reset_query_state(void){
304 		gatt_query_complete = 0;
305 		result_counter = 0;
306 		result_index = 0;
307 	}
308 };
309 
310 
311 TEST(GATTClient, TestDiscoverPrimaryServices){
312 	test = DISCOVER_PRIMARY_SERVICES;
313 	reset_query_state();
314 	status = gatt_client_discover_primary_services(handle_ble_client_event, gatt_client_handle);
315 	CHECK_EQUAL(0, status);
316 	CHECK_EQUAL(1, gatt_query_complete);
317 	verify_primary_services();
318 	CHECK_EQUAL(1, gatt_query_complete);
319 }
320 
321 TEST(GATTClient, TestDiscoverPrimaryServicesByUUID16){
322 	test = DISCOVER_PRIMARY_SERVICE_WITH_UUID16;
323 	reset_query_state();
324 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
325 	CHECK_EQUAL(0, status);
326 	CHECK_EQUAL(1, result_counter);
327 	verify_primary_services_with_uuid16();
328 	CHECK_EQUAL(1, gatt_query_complete);
329 }
330 
331 TEST(GATTClient, TestDiscoverPrimaryServicesByUUID128){
332 	test = DISCOVER_PRIMARY_SERVICE_WITH_UUID128;
333 	status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128);
334 	CHECK_EQUAL(0, status);
335 	CHECK_EQUAL(1, result_counter);
336 	verify_primary_services_with_uuid128();
337 	CHECK_EQUAL(1, gatt_query_complete);
338 }
339 
340 TEST(GATTClient, TestFindIncludedServicesForServiceWithUUID16){
341 	test = DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID16;
342 	reset_query_state();
343 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
344 	CHECK_EQUAL(0, status);
345 	CHECK_EQUAL(1, gatt_query_complete);
346 
347 	reset_query_state();
348 	status = gatt_client_find_included_services_for_service(handle_ble_client_event, gatt_client_handle, &services[0]);
349 	CHECK_EQUAL(0, status);
350 	CHECK_EQUAL(1, gatt_query_complete);
351 	verify_included_services_uuid16();
352 }
353 
354 TEST(GATTClient, TestFindIncludedServicesForServiceWithUUID128){
355 	test = DISCOVER_INCLUDED_SERVICE_FOR_SERVICE_WITH_UUID128;
356 	reset_query_state();
357 	status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128);
358 	CHECK_EQUAL(0, status);
359 	CHECK_EQUAL(1, gatt_query_complete);
360 
361 	reset_query_state();
362 	status = gatt_client_find_included_services_for_service(handle_ble_client_event, gatt_client_handle, &services[0]);
363 	CHECK_EQUAL(0, status);
364 	CHECK_EQUAL(1, gatt_query_complete);
365 	verify_included_services_uuid128();
366 }
367 
368 TEST(GATTClient, TestDiscoverCharacteristicsForService){
369 	test = DISCOVER_CHARACTERISTICS_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_discover_characteristics_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_charasteristics();
380 }
381 
382 TEST(GATTClient, TestDiscoverCharacteristicsByUUID16){
383 	test = DISCOVER_CHARACTERISTICS_BY_UUID16;
384 	reset_query_state();
385 	status = gatt_client_discover_characteristics_for_handle_range_by_uuid16(handle_ble_client_event, gatt_client_handle, 0x30, 0x32, 0xF102);
386 	CHECK_EQUAL(0, status);
387 	CHECK_EQUAL(1, gatt_query_complete);
388 	CHECK_EQUAL(1, result_counter);
389 }
390 
391 TEST(GATTClient, TestDiscoverCharacteristicsByUUID128){
392 	test = DISCOVER_CHARACTERISTICS_BY_UUID128;
393 	reset_query_state();
394 	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]);
395 	CHECK_EQUAL(0, status);
396 	CHECK_EQUAL(1, gatt_query_complete);
397 	CHECK_EQUAL(1, result_counter);
398 }
399 
400 TEST(GATTClient, TestDiscoverCharacteristics4ServiceByUUID128){
401 	test = DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID;
402 	reset_query_state();
403 	status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128);
404 	CHECK_EQUAL(0, status);
405 	CHECK_EQUAL(1, gatt_query_complete);
406 	CHECK_EQUAL(1, result_counter);
407 
408 	reset_query_state();
409 	uint8_t characteristic_uuid[] = {0x00, 0x00, 0xF2, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
410 	status = gatt_client_discover_characteristics_for_service_by_uuid128(handle_ble_client_event, gatt_client_handle, &services[0], characteristic_uuid);
411 	CHECK_EQUAL(0, status);
412 	CHECK_EQUAL(1, gatt_query_complete);
413 	CHECK_EQUAL(1, result_counter);
414 
415 	reset_query_state();
416 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF200);
417 	CHECK_EQUAL(0, status);
418 	CHECK_EQUAL(1, gatt_query_complete);
419 	CHECK_EQUAL(1, result_counter);
420 }
421 
422 TEST(GATTClient, TestDiscoverCharacteristics4ServiceByUUID16){
423 	test = DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID;
424 	reset_query_state();
425 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
426 	CHECK_EQUAL(0, status);
427 	CHECK_EQUAL(1, gatt_query_complete);
428 	CHECK_EQUAL(1, result_counter);
429 
430 	reset_query_state();
431 	uint8_t characteristic_uuid[]= { 0x00, 0x00, 0xF1, 0x05, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
432 	status = gatt_client_discover_characteristics_for_service_by_uuid128(handle_ble_client_event, gatt_client_handle, &services[0], characteristic_uuid);
433 	CHECK_EQUAL(0, status);
434 	CHECK_EQUAL(1, gatt_query_complete);
435 	CHECK_EQUAL(1, result_counter);
436 
437 	reset_query_state();
438 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
439 	CHECK_EQUAL(0, status);
440 	CHECK_EQUAL(1, gatt_query_complete);
441 	CHECK_EQUAL(1, result_counter);
442 }
443 
444 TEST(GATTClient, TestDiscoverCharacteristicDescriptor){
445 	test = DISCOVER_CHARACTERISTIC_DESCRIPTORS;
446 	reset_query_state();
447 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
448 	CHECK_EQUAL(0, status);
449 	CHECK_EQUAL(1, gatt_query_complete);
450 	CHECK_EQUAL(1, result_counter);
451 
452 	reset_query_state();
453 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
454 	CHECK_EQUAL(0, status);
455 	CHECK_EQUAL(1, gatt_query_complete);
456 	CHECK_EQUAL(1, result_counter);
457 
458 	reset_query_state();
459 	status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]);
460 	CHECK_EQUAL(0, status);
461 	CHECK_EQUAL(1, gatt_query_complete);
462 	CHECK(result_counter);
463 	CHECK_EQUAL(3, result_index);
464 	CHECK_EQUAL(0x2902, descriptors[0].uuid16);
465 	CHECK_EQUAL(0x2900, descriptors[1].uuid16);
466 	CHECK_EQUAL(0x2901, descriptors[2].uuid16);
467 }
468 
469 TEST(GATTClient, TestWriteClientCharacteristicConfiguration){
470 	test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
471 	reset_query_state();
472 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
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], 0xF100);
479 	CHECK_EQUAL(0, status);
480 	CHECK_EQUAL(1, gatt_query_complete);
481 	CHECK_EQUAL(1, result_counter);
482 
483 	reset_query_state();
484 	status = gatt_client_write_client_characteristic_configuration(handle_ble_client_event, gatt_client_handle, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
485  	CHECK_EQUAL(0, status);
486  	CHECK_EQUAL(1, gatt_query_complete);
487  	CHECK_EQUAL(1, result_counter);
488 }
489 
490 TEST(GATTClient, TestReadCharacteristicDescriptor){
491 	test = READ_CHARACTERISTIC_DESCRIPTOR;
492 	reset_query_state();
493 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
494 	CHECK_EQUAL(0, status);
495 	CHECK_EQUAL(1, gatt_query_complete);
496 	CHECK_EQUAL(1, result_counter);
497 
498 	reset_query_state();
499 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
500 	CHECK_EQUAL(0, status);
501 	CHECK_EQUAL(1, gatt_query_complete);
502 	CHECK_EQUAL(1, result_counter);
503 
504 	reset_query_state();
505 	status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]);
506 	CHECK_EQUAL(0, status);
507 	CHECK_EQUAL(1, gatt_query_complete);
508 	CHECK_EQUAL(3, result_counter);
509 
510 	reset_query_state();
511 	status = gatt_client_read_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0]);
512 	CHECK_EQUAL(0, status);
513 	CHECK_EQUAL(1, gatt_query_complete);
514 	CHECK_EQUAL(3, result_counter);
515 }
516 
517 TEST(GATTClient, TestReadCharacteristicValue){
518 	test = READ_CHARACTERISTIC_VALUE;
519 	reset_query_state();
520 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
521 	CHECK_EQUAL(0, status);
522 	CHECK_EQUAL(1, gatt_query_complete);
523 	CHECK_EQUAL(1, result_counter);
524 
525 	reset_query_state();
526 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
527 	CHECK_EQUAL(0, status);
528 	CHECK_EQUAL(1, gatt_query_complete);
529 	CHECK_EQUAL(1, result_counter);
530 
531 	reset_query_state();
532 	status = gatt_client_read_value_of_characteristic(handle_ble_client_event, gatt_client_handle, &characteristics[0]);
533 	CHECK_EQUAL(0, status);
534 	CHECK_EQUAL(1, gatt_query_complete);
535 	CHECK_EQUAL(3, result_counter);
536 }
537 
538 TEST(GATTClient, TestWriteCharacteristicValue){
539     test = WRITE_CHARACTERISTIC_VALUE;
540 	reset_query_state();
541 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
542 	CHECK_EQUAL(0, status);
543 	CHECK_EQUAL(1, gatt_query_complete);
544 	CHECK_EQUAL(1, result_counter);
545 
546 	reset_query_state();
547 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
548 	CHECK_EQUAL(0, status);
549 	CHECK_EQUAL(1, gatt_query_complete);
550 	CHECK_EQUAL(1, result_counter);
551 
552 	reset_query_state();
553 	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);
554 	CHECK_EQUAL(0, status);
555 	CHECK_EQUAL(1, gatt_query_complete);
556 }
557 
558 TEST(GATTClient, TestWriteCharacteristicDescriptor){
559 	test = WRITE_CHARACTERISTIC_DESCRIPTOR;
560 	reset_query_state();
561 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
562 	CHECK_EQUAL(0, status);
563 	CHECK_EQUAL(1, gatt_query_complete);
564 	CHECK_EQUAL(1, result_counter);
565 
566 	reset_query_state();
567 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
568 	CHECK_EQUAL(0, status);
569 	CHECK_EQUAL(1, gatt_query_complete);
570 	CHECK_EQUAL(1, result_counter);
571 
572 	reset_query_state();
573 	status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]);
574 	CHECK_EQUAL(0, status);
575 	CHECK_EQUAL(1, gatt_query_complete);
576 	CHECK_EQUAL(3, result_counter);
577 
578 	reset_query_state();
579 	status = gatt_client_write_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0], sizeof(indication), indication);
580 	CHECK_EQUAL(0, status);
581 	CHECK_EQUAL(1, gatt_query_complete);
582 }
583 
584 TEST(GATTClient, TestReadLongCharacteristicValue){
585 	test = READ_LONG_CHARACTERISTIC_VALUE;
586 	reset_query_state();
587 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
588 	CHECK_EQUAL(0, status);
589 	CHECK_EQUAL(1, gatt_query_complete);
590 	CHECK_EQUAL(1, result_counter);
591 
592 	reset_query_state();
593 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
594 	CHECK_EQUAL(0, status);
595 	CHECK_EQUAL(1, gatt_query_complete);
596 	CHECK_EQUAL(1, result_counter);
597 
598 	reset_query_state();
599 	status = gatt_client_read_long_value_of_characteristic(handle_ble_client_event, gatt_client_handle, &characteristics[0]);
600 	CHECK_EQUAL(0, status);
601 	CHECK_EQUAL(1, gatt_query_complete);
602 	CHECK_EQUAL(7, result_counter);
603 }
604 
605 TEST(GATTClient, TestReadLongCharacteristicDescriptor){
606 	test = READ_LONG_CHARACTERISTIC_DESCRIPTOR;
607 	reset_query_state();
608 	status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128);
609 	CHECK_EQUAL(0, status);
610 	CHECK_EQUAL(1, gatt_query_complete);
611 	CHECK_EQUAL(1, result_counter);
612 
613 	reset_query_state();
614 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF200);
615 	CHECK_EQUAL(0, status);
616 	CHECK_EQUAL(1, gatt_query_complete);
617 	CHECK_EQUAL(1, result_counter);
618 
619 	reset_query_state();
620 	status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]);
621 	CHECK_EQUAL(0, status);
622 	CHECK_EQUAL(1, gatt_query_complete);
623 	CHECK_EQUAL(3, result_counter);
624 
625 	reset_query_state();
626 	result_counter = 0;
627 	status = gatt_client_read_long_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0]);
628 	CHECK_EQUAL(0, status);
629 	CHECK_EQUAL(1, gatt_query_complete);
630 	CHECK_EQUAL(7, result_counter);
631 }
632 
633 
634 TEST(GATTClient, TestWriteLongCharacteristicDescriptor){
635 	test = WRITE_LONG_CHARACTERISTIC_DESCRIPTOR;
636 	reset_query_state();
637 	status = gatt_client_discover_primary_services_by_uuid128(handle_ble_client_event, gatt_client_handle, primary_service_uuid128);
638 	CHECK_EQUAL(0, status);
639 	CHECK_EQUAL(1, gatt_query_complete);
640 	CHECK_EQUAL(1, result_counter);
641 
642 	reset_query_state();
643 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF200);
644 	CHECK_EQUAL(0, status);
645 	CHECK_EQUAL(1, gatt_query_complete);
646 	CHECK_EQUAL(1, result_counter);
647 
648 	reset_query_state();
649 	status = gatt_client_discover_characteristic_descriptors(handle_ble_client_event, gatt_client_handle, &characteristics[0]);
650 	CHECK_EQUAL(0, status);
651 	CHECK_EQUAL(1, gatt_query_complete);
652 	CHECK_EQUAL(3, result_counter);
653 
654 	result_counter = 0;
655 	status = gatt_client_write_long_characteristic_descriptor(handle_ble_client_event, gatt_client_handle, &descriptors[0], sizeof(long_value), (uint8_t *)long_value);
656 	CHECK_EQUAL(0, status);
657 	CHECK_EQUAL(1, gatt_query_complete);
658 	CHECK_EQUAL(1, result_counter);
659 }
660 
661 TEST(GATTClient, TestWriteLongCharacteristicValue){
662 	test = WRITE_LONG_CHARACTERISTIC_VALUE;
663 	reset_query_state();
664 	status = gatt_client_discover_primary_services_by_uuid16(handle_ble_client_event, gatt_client_handle, service_uuid16);
665 	CHECK_EQUAL(0, status);
666 	CHECK_EQUAL(1, gatt_query_complete);
667 	CHECK_EQUAL(1, result_counter);
668 
669 	reset_query_state();
670 	status = gatt_client_discover_characteristics_for_service_by_uuid16(handle_ble_client_event, gatt_client_handle, &services[0], 0xF100);
671 	CHECK_EQUAL(0, status);
672 	CHECK_EQUAL(1, gatt_query_complete);
673 	CHECK_EQUAL(1, result_counter);
674 
675 
676 	reset_query_state();
677 	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);
678 	CHECK_EQUAL(0, status);
679 	CHECK_EQUAL(1, gatt_query_complete);
680 }
681 
682 TEST(GATTClient, TestWriteReliableLongCharacteristicValue){
683 	test = WRITE_RELIABLE_LONG_CHARACTERISTIC_VALUE;
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 	reset_query_state();
697 	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);
698 	CHECK_EQUAL(0, status);
699 	CHECK_EQUAL(1, gatt_query_complete);
700 }
701 
702 
703 int main (int argc, const char * argv[]){
704 	att_set_db(profile_data);
705 	att_set_write_callback(&att_write_callback);
706 	att_set_read_callback(&att_read_callback);
707 
708 	gatt_client_init();
709 
710     return CommandLineTestRunner::RunAllTests(argc, argv);
711 }
712