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