xref: /btstack/src/ble/gatt-service/hids_client.c (revision eb4dccece89f38c3fa8fb13197f36188f58d954a)
1 /*
2  * Copyright (C) 2021 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hids_client.c"
39 
40 #include "btstack_config.h"
41 
42 #ifdef ENABLE_TESTING_SUPPORT
43 #include <stdio.h>
44 #endif
45 
46 #include <stdint.h>
47 #include <string.h>
48 
49 #include "ble/gatt-service/hids_client.h"
50 
51 #include "btstack_memory.h"
52 #include "ble/att_db.h"
53 #include "ble/core.h"
54 #include "ble/gatt_client.h"
55 #include "bluetooth_gatt.h"
56 #include "btstack_debug.h"
57 #include "btstack_event.h"
58 #include "btstack_run_loop.h"
59 #include "gap.h"
60 
61 #define HID_REPORT_MODE_REPORT_ID               3
62 #define HID_REPORT_MODE_REPORT_MAP_ID           4
63 #define HID_REPORT_MODE_HID_INFORMATION_ID      5
64 #define HID_REPORT_MODE_HID_CONTROL_POINT_ID    6
65 
66 static btstack_linked_list_t clients;
67 static uint16_t hids_cid_counter = 0;
68 
69 static uint8_t * hids_client_descriptor_storage;
70 static uint16_t  hids_client_descriptor_storage_len;
71 
72 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
73 
74 #ifdef ENABLE_TESTING_SUPPORT
75 static char * hid_characteristic_name(uint16_t uuid){
76     switch (uuid){
77         case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE:
78             return "PROTOCOL_MODE";
79 
80         case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT:
81             return "BOOT_KEYBOARD_INPUT_REPORT";
82 
83         case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT:
84             return "BOOT_MOUSE_INPUT_REPORT";
85 
86         case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT:
87             return "BOOT_KEYBOARD_OUTPUT_REPORT";
88 
89         case ORG_BLUETOOTH_CHARACTERISTIC_REPORT:
90             return "REPORT";
91 
92         case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP:
93             return "REPORT_MAP";
94 
95         case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION:
96             return "HID_INFORMATION";
97 
98         case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT:
99             return "HID_CONTROL_POINT";
100         default:
101             return "UKNOWN";
102     }
103 }
104 #endif
105 
106 static hids_client_t * hids_get_client_for_con_handle(hci_con_handle_t con_handle){
107     btstack_linked_list_iterator_t it;
108     btstack_linked_list_iterator_init(&it, &clients);
109     while (btstack_linked_list_iterator_has_next(&it)){
110         hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
111         if (client->con_handle != con_handle) continue;
112         return client;
113     }
114     return NULL;
115 }
116 
117 static hids_client_t * hids_get_client_for_cid(uint16_t hids_cid){
118     btstack_linked_list_iterator_t it;
119     btstack_linked_list_iterator_init(&it, &clients);
120     while (btstack_linked_list_iterator_has_next(&it)){
121         hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
122         if (client->cid != hids_cid) continue;
123         return client;
124     }
125     return NULL;
126 }
127 
128 
129 // START Descriptor Storage Util
130 
131 static uint16_t hids_client_descriptor_storage_get_available_space(void){
132     // assumes all descriptors are back to back
133     uint16_t free_space = hids_client_descriptor_storage_len;
134     uint8_t i;
135 
136     btstack_linked_list_iterator_t it;
137     btstack_linked_list_iterator_init(&it, &clients);
138     while (btstack_linked_list_iterator_has_next(&it)){
139         hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
140         for (i = 0; i < client->num_instances; i++){
141             free_space -= client->services[i].hid_descriptor_len;
142         }
143     }
144     return free_space;
145 }
146 
147 static void hids_client_descriptor_storage_init(hids_client_t * client, uint8_t service_index){
148     client->services[service_index].hid_descriptor_len = 0;
149     client->services[service_index].hid_descriptor_max_len = hids_client_descriptor_storage_get_available_space();
150     client->services[service_index].hid_descriptor_offset = hids_client_descriptor_storage_len - client->services[service_index].hid_descriptor_max_len;
151 }
152 
153 static bool hids_client_descriptor_storage_store(hids_client_t * client, uint8_t service_index, uint8_t byte){
154     if (client->services[service_index].hid_descriptor_len >= client->services[service_index].hid_descriptor_max_len) return false;
155 
156     hids_client_descriptor_storage[client->services[service_index].hid_descriptor_offset + client->services[service_index].hid_descriptor_len] = byte;
157     client->services[service_index].hid_descriptor_len++;
158     return true;
159 }
160 
161 static void hids_client_descriptor_storage_delete(hids_client_t * client){
162     uint8_t service_index = 0;
163     uint16_t next_offset = 0;
164 
165     for (service_index = 0; service_index < client->num_instances; service_index++){
166         next_offset += client->services[service_index].hid_descriptor_offset + client->services[service_index].hid_descriptor_len;
167         client->services[service_index].hid_descriptor_len = 0;
168         client->services[service_index].hid_descriptor_offset = 0;
169     }
170 
171     memmove(&hids_client_descriptor_storage[client->services[0].hid_descriptor_offset],
172             &hids_client_descriptor_storage[next_offset],
173             hids_client_descriptor_storage_len - next_offset);
174 
175     uint8_t i;
176     btstack_linked_list_iterator_t it;
177     btstack_linked_list_iterator_init(&it, &clients);
178     while (btstack_linked_list_iterator_has_next(&it)){
179         hids_client_t * conn = (hids_client_t *)btstack_linked_list_iterator_next(&it);
180         for (i = 0; i < client->num_instances; i++){
181             if (conn->services[i].hid_descriptor_offset >= next_offset){
182                 conn->services[i].hid_descriptor_offset = next_offset;
183                 next_offset += conn->services[service_index].hid_descriptor_len;
184             }
185         }
186     }
187 }
188 
189 const uint8_t * hids_client_descriptor_storage_get_descriptor_data(uint16_t hids_cid, uint8_t service_index){
190     hids_client_t * client = hids_get_client_for_cid(hids_cid);
191     if (client == NULL){
192         return NULL;
193     }
194     if (service_index >= client->num_instances){
195         return NULL;
196     }
197     return &hids_client_descriptor_storage[client->services[service_index].hid_descriptor_offset];
198 }
199 
200 uint16_t hids_client_descriptor_storage_get_descriptor_len(uint16_t hids_cid, uint8_t service_index){
201     hids_client_t * client = hids_get_client_for_cid(hids_cid);
202     if (client == NULL){
203         return 0;
204     }
205     if (service_index >= client->num_instances){
206         return 0;
207     }
208     return client->services[service_index].hid_descriptor_len;
209 }
210 
211 // END Descriptor Storage Util
212 
213 static uint16_t hids_get_next_cid(void){
214     if (hids_cid_counter == 0xffff) {
215         hids_cid_counter = 1;
216     } else {
217         hids_cid_counter++;
218     }
219     return hids_cid_counter;
220 }
221 
222 static uint8_t find_report_index_for_value_handle(hids_client_t * client, uint16_t value_handle){
223     uint8_t i;
224     for (i = 0; i < client->num_reports; i++){
225         if (client->reports[i].value_handle == value_handle){
226             return i;
227         }
228     }
229     return HIDS_CLIENT_INVALID_REPORT_INDEX;
230 }
231 
232 static uint8_t find_external_report_index_for_value_handle(hids_client_t * client, uint16_t value_handle){
233     uint8_t i;
234     for (i = 0; i < client->num_external_reports; i++){
235         if (client->external_reports[i].value_handle == value_handle){
236             return i;
237         }
238     }
239     return HIDS_CLIENT_INVALID_REPORT_INDEX;
240 }
241 
242 static bool external_report_index_for_uuid_exists(hids_client_t * client, uint16_t uuid16){
243     uint8_t i;
244     for (i = 0; i < client->num_external_reports; i++){
245         if (client->external_reports[i].external_report_reference_uuid == uuid16){
246             return true;
247         }
248     }
249     return false;
250 }
251 
252 static uint8_t find_report_index_for_report_id_and_report_type(hids_client_t * client, uint8_t report_id, hid_report_type_t report_type){
253     uint8_t i;
254 
255     for (i = 0; i < client->num_reports; i++){
256         hids_client_report_t report = client->reports[i];
257         hid_protocol_mode_t  protocol_mode = client->services[report.service_index].protocol_mode;
258 
259         if (protocol_mode == HID_PROTOCOL_MODE_BOOT){
260             if (!client->reports[i].boot_report){
261                 continue;
262             }
263         } else if (protocol_mode == HID_PROTOCOL_MODE_REPORT){
264             if (client->reports[i].boot_report){
265                 continue;
266             }
267         }
268 
269         if (report.report_id == report_id && report.report_type == report_type){
270             return i;
271         }
272     }
273     return HIDS_CLIENT_INVALID_REPORT_INDEX;
274 }
275 
276 static uint8_t hids_client_add_characteristic(hids_client_t * client, gatt_client_characteristic_t * characteristic, uint8_t report_id, hid_report_type_t report_type, bool boot_report){
277 
278     uint8_t report_index = find_external_report_index_for_value_handle(client, characteristic->value_handle);
279     if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
280         return report_index;
281     }
282     report_index = client->num_reports;
283 
284     if (report_index < HIDS_CLIENT_NUM_REPORTS) {
285         client->reports[report_index].value_handle = characteristic->value_handle;
286         client->reports[report_index].end_handle = characteristic->end_handle;
287         client->reports[report_index].properties = characteristic->properties;
288 
289         client->reports[report_index].service_index = client->service_index;
290         client->reports[report_index].report_id = report_id;
291         client->reports[report_index].report_type = report_type;
292         client->reports[report_index].boot_report = boot_report;
293 
294         log_info("add index %d, id %d, type %d, value handle 0x%02x, properties 0x%02x", report_index, report_id, report_type, characteristic->value_handle, characteristic->properties);
295         client->num_reports++;
296         return report_index;
297     } else {
298         log_info("not enough storage, increase HIDS_CLIENT_NUM_REPORTS");
299         return HIDS_CLIENT_INVALID_REPORT_INDEX;
300     }
301 }
302 
303 static uint8_t hids_client_add_external_report(hids_client_t * client, gatt_client_characteristic_descriptor_t * characteristic_descriptor){
304     uint8_t report_index = client->num_external_reports;
305 
306     if (report_index < HIDS_CLIENT_NUM_REPORTS) {
307         client->external_reports[report_index].value_handle = characteristic_descriptor->handle;
308         client->external_reports[report_index].service_index = client->service_index;
309 
310         client->num_external_reports++;
311         log_info("add external index %d [%d], value handle 0x%02x", report_index, client->num_external_reports, characteristic_descriptor->handle);
312         return report_index;
313     } else {
314         log_info("not enough storage, increase HIDS_CLIENT_NUM_REPORTS");
315         return HIDS_CLIENT_INVALID_REPORT_INDEX;
316     }
317 }
318 
319 static uint8_t hids_client_get_next_active_report_map_index(hids_client_t * client){
320     uint8_t i;
321     for (i = client->service_index; i < client->num_instances; i++){
322         if (client->services[i].report_map_value_handle != 0){
323             return i;
324         }
325     }
326     client->service_index = HIDS_CLIENT_INVALID_REPORT_INDEX;
327     return HIDS_CLIENT_INVALID_REPORT_INDEX;
328 }
329 
330 static bool hids_client_report_query_next_report_map(hids_client_t * client){
331     client->service_index++;
332     if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
333         client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR;
334         return true;
335     }
336     return false;
337 }
338 
339 static bool hids_client_report_map_query_init(hids_client_t * client){
340     client->service_index = 0;
341 
342     if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
343         client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR;
344         return true;
345     }
346     return false;
347 }
348 
349 static bool hids_client_report_query_next_report_map_uuid(hids_client_t * client){
350     client->report_index++;
351     if (client->report_index < client->num_external_reports){
352         client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID;
353         return true;
354     }
355     return false;
356 }
357 
358 static bool hids_client_report_map_uuid_query_init(hids_client_t * client){
359     client->report_index = 0;
360     if (client->num_external_reports > 0){
361         client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID;
362         return true;
363     }
364     return false;
365 }
366 
367 static uint8_t hids_client_get_next_report_index(hids_client_t * client){
368     uint8_t i;
369     uint8_t index = HIDS_CLIENT_INVALID_REPORT_INDEX;
370 
371     for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++){
372         hids_client_report_t report = client->reports[i];
373         if (!report.boot_report){
374             if (report.report_type == HID_REPORT_TYPE_RESERVED && report.report_id == HID_REPORT_MODE_REPORT_ID){
375                 index = i;
376                 client->service_index = report.service_index;
377                 break;
378             }
379         }
380     }
381     client->report_index = index;
382     return index;
383 }
384 
385 static bool hids_client_report_query_next_report(hids_client_t * client){
386     client->report_index++;
387     if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
388         client->state = HIDS_CLIENT_STATE_W2_FIND_REPORT;
389         return true;
390     }
391     return false;
392 }
393 
394 static bool hids_client_report_query_init(hids_client_t * client){
395     client->report_index = 0;
396 
397     if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
398         client->state = HIDS_CLIENT_STATE_W2_FIND_REPORT;
399         return true;
400     }
401     return false;
402 }
403 
404 static uint8_t hids_client_get_next_notification_report_index(hids_client_t * client){
405     uint8_t i;
406     uint8_t index = HIDS_CLIENT_INVALID_REPORT_INDEX;
407 
408     for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++){
409         hids_client_report_t report = client->reports[i];
410         hid_protocol_mode_t  protocol_mode = client->services[report.service_index].protocol_mode;
411 
412         if (protocol_mode == HID_PROTOCOL_MODE_BOOT){
413             if (!client->reports[i].boot_report){
414                 continue;
415             }
416         } else if (protocol_mode == HID_PROTOCOL_MODE_REPORT){
417             if (client->reports[i].boot_report){
418                 continue;
419             }
420         }
421         if (report.report_type == HID_REPORT_TYPE_INPUT){
422             index = i;
423         }
424     }
425     client->report_index = index;
426     return index;
427 }
428 
429 static bool hids_client_report_next_notification_report_index(hids_client_t * client){
430     client->report_index++;
431     if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
432         client->state = HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS;
433         return true;
434     }
435     return false;
436 }
437 
438 static bool hids_client_report_notifications_init(hids_client_t * client){
439 #ifdef ENABLE_TESTING_SUPPORT
440     printf("\nRegister for Notifications: \n");
441 #endif
442     client->report_index = 0;
443 
444     if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
445         client->state = HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS;
446         return true;
447     }
448     return false;
449 }
450 
451 static bool hids_client_report_next_notifications_configuration_report_index(hids_client_t * client){
452     client->report_index++;
453     if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
454         client->state = HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS;
455         return true;
456     }
457     return false;
458 }
459 
460 static bool hids_client_notifications_configuration_init(hids_client_t * client){
461 #ifdef ENABLE_TESTING_SUPPORT
462     printf("\nConfigure for Notifications: \n");
463 #endif
464     client->report_index = 0;
465 
466     if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
467         client->state = HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS;
468         return true;
469     }
470     return false;
471 }
472 
473 static hids_client_t * hids_create_client(hci_con_handle_t con_handle, uint16_t cid){
474     hids_client_t * client = btstack_memory_hids_client_get();
475     if (!client){
476         log_error("Not enough memory to create client");
477         return NULL;
478     }
479     client->state = HIDS_CLIENT_STATE_IDLE;
480     client->cid = cid;
481     client->con_handle = con_handle;
482 
483     btstack_linked_list_add(&clients, (btstack_linked_item_t *) client);
484     return client;
485 }
486 
487 static void hids_finalize_client(hids_client_t * client){
488     // stop listening
489     uint8_t i;
490     for (i = 0; i < client->num_reports; i++){
491         gatt_client_stop_listening_for_characteristic_value_updates(&client->reports[i].notification_listener);
492     }
493 
494     hids_client_descriptor_storage_delete(client);
495     btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client);
496     btstack_memory_hids_client_free(client);
497 }
498 
499 
500 static void hids_emit_connection_established(hids_client_t * client, uint8_t status){
501     uint8_t event[8];
502     int pos = 0;
503     event[pos++] = HCI_EVENT_GATTSERVICE_META;
504     event[pos++] = sizeof(event) - 2;
505     event[pos++] = GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED;
506     little_endian_store_16(event, pos, client->cid);
507     pos += 2;
508     event[pos++] = status;
509     event[pos++] = client->services[0].protocol_mode;
510     event[pos++] = client->num_instances;
511     (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
512 }
513 
514 static void hids_emit_notifications_configuration(hids_client_t * client){
515     uint8_t event[6];
516     int pos = 0;
517     event[pos++] = HCI_EVENT_GATTSERVICE_META;
518     event[pos++] = sizeof(event) - 2;
519     event[pos++] = GATTSERVICE_SUBEVENT_HID_SERVICE_REPORTS_NOTIFICATION;
520     little_endian_store_16(event, pos, client->cid);
521     pos += 2;
522     event[pos++] = client->value;
523     (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
524 }
525 
526 static void hids_client_setup_report_event(hids_client_t * client, uint8_t report_index, uint8_t *buffer, uint16_t report_len){
527     uint16_t pos = 0;
528     buffer[pos++] = HCI_EVENT_GATTSERVICE_META;
529     pos++;  // skip len
530     buffer[pos++] = GATTSERVICE_SUBEVENT_HID_REPORT;
531     little_endian_store_16(buffer, pos, client->cid);
532     pos += 2;
533     buffer[pos++] = client->reports[report_index].service_index;
534     buffer[pos++] = client->reports[report_index].report_id;
535     little_endian_store_16(buffer, pos, report_len + 1);
536     pos += 2;
537     buffer[pos++] = client->reports[report_index].report_id;
538     buffer[1] = pos + (report_len + 1) - 2;
539 
540 }
541 
542 static void hids_client_emit_hid_information_event(hids_client_t * client, const uint8_t *value, uint16_t value_len){
543     if (value_len != 4) return;
544 
545     uint8_t event[11];
546     int pos = 0;
547     event[pos++] = HCI_EVENT_GATTSERVICE_META;
548     event[pos++] = sizeof(event) - 2;
549     event[pos++] = GATTSERVICE_SUBEVENT_HID_INFORMATION;
550     little_endian_store_16(event, pos, client->cid);
551     pos += 2;
552     event[pos++] = client->service_index;
553 
554     memcpy(event+pos, value, 3);
555     pos += 3;
556     event[pos++] = (value[3] & 0x02) >> 1;
557     event[pos++] = value[3] & 0x01;
558 
559     (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
560 }
561 
562 static void hids_client_emit_protocol_mode_event(hids_client_t * client, const uint8_t *value, uint16_t value_len){
563     if (value_len != 1) return;
564 
565     uint8_t event[11];
566     int pos = 0;
567     event[pos++] = HCI_EVENT_GATTSERVICE_META;
568     event[pos++] = sizeof(event) - 2;
569     event[pos++] = GATTSERVICE_SUBEVENT_HID_PROTOCOL_MODE;
570     little_endian_store_16(event, pos, client->cid);
571     pos += 2;
572     event[pos++] = client->service_index;
573     event[pos++] = value[0];
574     (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
575 }
576 
577 static void handle_notification_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
578     UNUSED(packet_type);
579     UNUSED(channel);
580 
581     if (hci_event_packet_get_type(packet) != GATT_EVENT_NOTIFICATION) return;
582 
583     hids_client_t * client = hids_get_client_for_con_handle(gatt_event_notification_get_handle(packet));
584     btstack_assert(client != NULL);
585 
586     uint8_t report_index = find_report_index_for_value_handle(client, gatt_event_notification_get_value_handle(packet));
587     if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
588         return;
589     }
590 
591     uint8_t * in_place_event = &packet[-2];
592     hids_client_setup_report_event(client, report_index, in_place_event, gatt_event_notification_get_value_length(packet));
593     (*client->client_handler)(HCI_EVENT_GATTSERVICE_META, client->cid, in_place_event, size + 2);
594 }
595 
596 static void handle_report_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
597     UNUSED(packet_type);
598     UNUSED(channel);
599 
600     if (hci_event_packet_get_type(packet) != GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) return;
601 
602     hids_client_t * client = hids_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet));
603     btstack_assert(client != NULL);
604 
605     if (client->state != HIDS_CLIENT_W4_GET_REPORT_RESULT){
606         return;
607     }
608     client->state = HIDS_CLIENT_STATE_CONNECTED;
609 
610     uint8_t report_index = find_report_index_for_value_handle(client, gatt_event_characteristic_value_query_result_get_value_handle(packet));
611     if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
612         return;
613     }
614 
615     uint8_t * in_place_event = &packet[-2];
616     hids_client_setup_report_event(client, report_index, in_place_event, gatt_event_characteristic_value_query_result_get_value_length(packet));
617     (*client->client_handler)(HCI_EVENT_GATTSERVICE_META, client->cid, in_place_event, size + 2);
618 }
619 
620 static void hids_run_for_client(hids_client_t * client){
621     uint8_t att_status;
622     gatt_client_service_t service;
623     gatt_client_characteristic_t characteristic;
624 
625     switch (client->state){
626         case HIDS_CLIENT_STATE_W2_QUERY_SERVICE:
627 #ifdef ENABLE_TESTING_SUPPORT
628             printf("\n\nQuery Services:\n");
629 #endif
630             client->state = HIDS_CLIENT_STATE_W4_SERVICE_RESULT;
631 
632             // result in GATT_EVENT_SERVICE_QUERY_RESULT
633             att_status = gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, client->con_handle, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE);
634             UNUSED(att_status);
635             break;
636 
637         case HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC:
638 #ifdef ENABLE_TESTING_SUPPORT
639             printf("\n\nQuery Characteristics of service %d:\n", client->service_index);
640 #endif
641             client->state = HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT;
642 
643             service.start_group_handle = client->services[client->service_index].start_handle;
644             service.end_group_handle = client->services[client->service_index].end_handle;
645 
646             // result in GATT_EVENT_CHARACTERISTIC_QUERY_RESULT
647             att_status = gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, client->con_handle, &service);
648 
649             UNUSED(att_status);
650             break;
651 
652         case HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR:
653 #ifdef ENABLE_TESTING_SUPPORT
654             printf("\n\nRead REPORT_MAP (Handle 0x%04X) HID Descriptor of service %d:\n", client->services[client->service_index].report_map_value_handle, client->service_index);
655 #endif
656             client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR;
657 
658             // result in GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT
659             att_status = gatt_client_read_long_value_of_characteristic_using_value_handle(&handle_gatt_client_event, client->con_handle, client->services[client->service_index].report_map_value_handle);
660             UNUSED(att_status);
661             break;
662 
663         case HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS:
664 #ifdef ENABLE_TESTING_SUPPORT
665             printf("\nDiscover REPORT_MAP (Handle 0x%04X) Characteristic Descriptors of service %d:\n", client->services[client->service_index].report_map_value_handle, client->service_index);
666 #endif
667             client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT;
668 
669             characteristic.value_handle = client->services[client->service_index].report_map_value_handle;
670             characteristic.end_handle = client->services[client->service_index].report_map_end_handle;
671 
672             // result in GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT
673             att_status = gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic);
674             UNUSED(att_status);
675             break;
676 
677         case HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID:
678 #ifdef ENABLE_TESTING_SUPPORT
679             printf("\nRead external chr UUID (Handle 0x%04X) Characteristic Descriptors, service index %d:\n", client->external_reports[client->report_index].value_handle, client->service_index);
680 #endif
681             client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID;
682 
683             // result in GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT
684             att_status = gatt_client_read_characteristic_descriptor_using_descriptor_handle(&handle_gatt_client_event, client->con_handle, client->external_reports[client->report_index].value_handle);
685             UNUSED(att_status);
686             break;
687 
688         case HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC:
689  #ifdef ENABLE_TESTING_SUPPORT
690             printf("\nDiscover External Report Characteristic:\n");
691 #endif
692             client->state = HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT;
693 
694             service.start_group_handle = 0x0001;
695             service.end_group_handle = 0xffff;
696 
697             // Result in GATT_EVENT_CHARACTERISTIC_QUERY_RESULT
698             att_status = gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, client->con_handle, &service);
699             UNUSED(att_status);
700             break;
701 
702         case HIDS_CLIENT_STATE_W2_FIND_REPORT:
703 #ifdef ENABLE_TESTING_SUPPORT
704             printf("\nQuery Report Characteristic Descriptors [%d, %d, 0x%04X]:\n",
705                 client->report_index,
706                 client->reports[client->report_index].service_index,
707                 client->reports[client->report_index].value_handle);
708 #endif
709             client->state = HIDS_CLIENT_STATE_W4_REPORT_FOUND;
710             client->handle = 0;
711 
712             characteristic.value_handle = client->reports[client->report_index].value_handle;
713             characteristic.end_handle = client->reports[client->report_index].end_handle;
714             characteristic.properties = client->reports[client->report_index].properties;
715 
716             // result in GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT
717             att_status = gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic);
718             UNUSED(att_status);
719             break;
720 
721         case HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE:
722             client->state = HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE;
723 
724             // result in GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT
725             att_status = gatt_client_read_characteristic_descriptor_using_descriptor_handle(&handle_gatt_client_event, client->con_handle, client->handle);
726             client->handle = 0;
727             UNUSED(att_status);
728             break;
729 
730         case HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS:
731 #ifdef ENABLE_TESTING_SUPPORT
732             if (client->value > 0){
733                 printf("    Notification configuration enable ");
734             } else {
735                 printf("    Notification configuration disable ");
736             }
737             printf("[%d, %d, 0x%04X]:\n",
738                 client->report_index,
739                 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);
740 #endif
741 
742             client->state = HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED;
743 
744             characteristic.value_handle = client->reports[client->report_index].value_handle;
745             characteristic.end_handle = client->reports[client->report_index].end_handle;
746             characteristic.properties = client->reports[client->report_index].properties;
747 
748             // end of write marked in GATT_EVENT_QUERY_COMPLETE
749 
750             att_status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, client->con_handle, &characteristic, client->value);
751 
752             if (att_status == ERROR_CODE_SUCCESS){
753                 switch(client->value){
754                     case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION:
755                         gatt_client_listen_for_characteristic_value_updates(
756                             &client->reports[client->report_index].notification_listener,
757                             &handle_notification_event, client->con_handle, &characteristic);
758                         break;
759                     default:
760                         gatt_client_stop_listening_for_characteristic_value_updates(&client->reports[client->report_index].notification_listener);
761                         break;
762                 }
763             } else {
764                 if (hids_client_report_next_notifications_configuration_report_index(client)){
765                     hids_run_for_client(client);
766                     break;
767                 }
768                 client->state = HIDS_CLIENT_STATE_CONNECTED;
769             }
770             break;
771 
772         case HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS:
773 #ifdef ENABLE_TESTING_SUPPORT
774             printf("    Notification enable [%d, %d, 0x%04X]:\n",
775                 client->report_index,
776                 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);
777 #endif
778             client->state = HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED;
779 
780             characteristic.value_handle = client->reports[client->report_index].value_handle;
781             characteristic.end_handle = client->reports[client->report_index].end_handle;
782             characteristic.properties = client->reports[client->report_index].properties;
783 
784             // end of write marked in GATT_EVENT_QUERY_COMPLETE
785             att_status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, client->con_handle, &characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
786 
787             if (att_status == ERROR_CODE_SUCCESS){
788                 gatt_client_listen_for_characteristic_value_updates(
789                     &client->reports[client->report_index].notification_listener,
790                     &handle_notification_event, client->con_handle, &characteristic);
791             } else {
792                 if (hids_client_report_next_notification_report_index(client)){
793                     hids_run_for_client(client);
794                     break;
795                 }
796                 client->state = HIDS_CLIENT_STATE_CONNECTED;
797                 hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
798             }
799             break;
800 
801 
802         case HIDS_CLIENT_W2_SEND_WRITE_REPORT:
803 #ifdef ENABLE_TESTING_SUPPORT
804             printf("    Write report [%d, %d, 0x%04X]:\n",
805                 client->report_index,
806                 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);
807 #endif
808 
809             client->state = HIDS_CLIENT_W4_WRITE_REPORT_DONE;
810 
811             // see GATT_EVENT_QUERY_COMPLETE for end of write
812             att_status = gatt_client_write_value_of_characteristic(
813                 &handle_report_event, client->con_handle,
814                 client->reports[client->report_index].value_handle,
815                 client->report_len, (uint8_t *)client->report);
816             UNUSED(att_status);
817             break;
818 
819         case HIDS_CLIENT_W2_SEND_GET_REPORT:
820 #ifdef ENABLE_TESTING_SUPPORT
821             printf("    Get report [index %d, ID %d, Service %d, handle 0x%04X]:\n",
822                 client->report_index,
823                 client->reports[client->report_index].report_id,
824                 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);
825 #endif
826 
827             client->state = HIDS_CLIENT_W4_GET_REPORT_RESULT;
828             // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT
829             att_status = gatt_client_read_value_of_characteristic_using_value_handle(
830                 &handle_report_event,
831                 client->con_handle,
832                 client->reports[client->report_index].value_handle);
833             UNUSED(att_status);
834             break;
835 
836 #ifdef ENABLE_TESTING_SUPPORT
837         case HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION:
838             client->state = HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT;
839 
840             // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT
841             att_status = gatt_client_read_value_of_characteristic_using_value_handle(
842                 &handle_gatt_client_event,
843                 client->con_handle,
844                 client->reports[client->report_index].ccc_handle);
845 
846             break;
847 #endif
848         case HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC:
849             client->state = HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT;
850 
851             // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT
852             att_status = gatt_client_read_value_of_characteristic_using_value_handle(
853                 &handle_gatt_client_event,
854                 client->con_handle,
855                 client->handle);
856             break;
857 
858         case HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE:
859         case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
860             (void) gatt_client_request_can_write_without_response_event(&handle_gatt_client_event, client->con_handle);
861             break;
862 
863         default:
864             break;
865     }
866 }
867 
868 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
869     UNUSED(packet_type);
870     UNUSED(channel);
871     UNUSED(size);
872 
873     hids_client_t * client = NULL;
874     uint8_t att_status;
875     gatt_client_service_t service;
876     gatt_client_characteristic_t characteristic;
877     gatt_client_characteristic_descriptor_t characteristic_descriptor;
878 
879     // hids_client_report_t * boot_keyboard_report;
880     // hids_client_report_t * boot_mouse_report;
881     const uint8_t * characteristic_descriptor_value;
882     uint8_t i;
883     uint8_t report_index;
884 
885     const uint8_t * value;
886     uint16_t value_len;
887 
888     switch(hci_event_packet_get_type(packet)){
889         case GATT_EVENT_SERVICE_QUERY_RESULT:
890             client = hids_get_client_for_con_handle(gatt_event_service_query_result_get_handle(packet));
891             btstack_assert(client != NULL);
892 
893             if (client->num_instances < MAX_NUM_HID_SERVICES){
894                 uint8_t index = client->num_instances;
895                 gatt_event_service_query_result_get_service(packet, &service);
896                 client->services[index].start_handle = service.start_group_handle;
897                 client->services[index].end_handle = service.end_group_handle;
898                 client->num_instances++;
899 
900 #ifdef ENABLE_TESTING_SUPPORT
901                 printf("HID Service: start handle 0x%04X, end handle 0x%04X\n", client->services[index].start_handle, client->services[index].end_handle);
902 #endif
903                 hids_client_descriptor_storage_init(client, index);
904             }  else {
905                 log_info("%d hid services found, only first %d can be stored, increase MAX_NUM_HID_SERVICES", client->num_instances + 1, MAX_NUM_HID_SERVICES);
906             }
907             break;
908 
909         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
910             client = hids_get_client_for_con_handle(gatt_event_characteristic_query_result_get_handle(packet));
911             btstack_assert(client != NULL);
912             gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
913 
914             report_index = HIDS_CLIENT_INVALID_REPORT_INDEX;
915             if (client->state == HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT){
916                 if (!external_report_index_for_uuid_exists(client, characteristic.uuid16)){
917                     break;
918                 }
919             }
920 
921             switch (characteristic.uuid16){
922                 case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE:
923                     client->services[client->service_index].protocol_mode_value_handle = characteristic.value_handle;
924                     break;
925 
926                 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT:
927                     report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_KEYBOARD_ID, HID_REPORT_TYPE_INPUT, true);
928                     break;
929 
930                 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT:
931                     report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_MOUSE_ID, HID_REPORT_TYPE_INPUT, true);
932                     break;
933 
934                 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT:
935                     report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_KEYBOARD_ID, HID_REPORT_TYPE_OUTPUT, true);
936                     break;
937 
938                 case ORG_BLUETOOTH_CHARACTERISTIC_REPORT:
939                     report_index = hids_client_add_characteristic(client, &characteristic, HID_REPORT_MODE_REPORT_ID, HID_REPORT_TYPE_RESERVED, false);
940                     break;
941 
942                 case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP:
943                     client->services[client->service_index].report_map_value_handle = characteristic.value_handle;
944                     client->services[client->service_index].report_map_end_handle = characteristic.end_handle;
945                     break;
946 
947                 case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION:
948                     client->services[client->service_index].hid_information_value_handle = characteristic.value_handle;
949                     break;
950 
951                 case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT:
952                     client->services[client->service_index].control_point_value_handle = characteristic.value_handle;
953                     break;
954 
955                 default:
956 #ifdef ENABLE_TESTING_SUPPORT
957                     printf("    TODO: Found external characteristic 0x%04X\n", characteristic.uuid16);
958 #endif
959                     return;
960             }
961 
962 #ifdef ENABLE_TESTING_SUPPORT
963             printf("HID Characteristic %s:  \n    Attribute Handle 0x%04X, Properties 0x%02X, Handle 0x%04X, UUID 0x%04X, service %d",
964                 hid_characteristic_name(characteristic.uuid16),
965                 characteristic.start_handle,
966                 characteristic.properties,
967                 characteristic.value_handle, characteristic.uuid16,
968                 client->service_index);
969 
970             if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
971                 printf(", report index 0x%02X", report_index);
972             }
973             printf("\n");
974 #endif
975             break;
976 
977         case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
978             // Map Report characteristic value == HID Descriptor
979             client = hids_get_client_for_con_handle(gatt_event_long_characteristic_value_query_result_get_handle(packet));
980             btstack_assert(client != NULL);
981 
982             value = gatt_event_long_characteristic_value_query_result_get_value(packet);
983             value_len = gatt_event_long_characteristic_value_query_result_get_value_length(packet);
984 
985 #ifdef ENABLE_TESTING_SUPPORT
986             // printf("Report Map HID Desc [%d] for service %d\n", descriptor_len, client->service_index);
987             printf_hexdump(value, value_len);
988 #endif
989             for (i = 0; i < value_len; i++){
990                 bool stored = hids_client_descriptor_storage_store(client, client->service_index, value[i]);
991                 if (!stored){
992                     client->services[client->service_index].hid_descriptor_status = ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
993                     break;
994                 }
995             }
996             break;
997 
998         case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
999             client = hids_get_client_for_con_handle(gatt_event_all_characteristic_descriptors_query_result_get_handle(packet));
1000             btstack_assert(client != NULL);
1001             gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &characteristic_descriptor);
1002 
1003             switch (client->state) {
1004                 case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT:
1005                     // setup for descriptor value query
1006                     if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_EXTERNAL_REPORT_REFERENCE){
1007                         report_index = hids_client_add_external_report(client, &characteristic_descriptor);
1008 
1009 #ifdef ENABLE_TESTING_SUPPORT
1010                         if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
1011                             printf("    External Report Reference Characteristic Descriptor: Handle 0x%04X, UUID 0x%04X, service %d, report index 0x%02X\n",
1012                                 characteristic_descriptor.handle,
1013                                 characteristic_descriptor.uuid16,
1014                                 client->service_index, report_index);
1015                         }
1016 #endif
1017                     }
1018                     break;
1019                 case HIDS_CLIENT_STATE_W4_REPORT_FOUND:
1020                     // setup for descriptor value query
1021                     if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_REPORT_REFERENCE){
1022                         client->handle = characteristic_descriptor.handle;
1023 #ifdef ENABLE_TESTING_SUPPORT
1024                         printf("    Report Characteristic Report Reference Characteristic Descriptor:  Handle 0x%04X, UUID 0x%04X\n",
1025                             characteristic_descriptor.handle,
1026                             characteristic_descriptor.uuid16);
1027 #endif
1028                     }
1029 
1030 #ifdef ENABLE_TESTING_SUPPORT
1031                     if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION){
1032                         client->reports[client->report_index].ccc_handle = characteristic_descriptor.handle;
1033                         printf("    Report Client Characteristic Configuration Descriptor: Handle 0x%04X, UUID 0x%04X\n",
1034                             characteristic_descriptor.handle,
1035                             characteristic_descriptor.uuid16);
1036                     }
1037 #endif
1038                     break;
1039 
1040                 default:
1041                     break;
1042             }
1043             break;
1044 
1045         case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
1046             client = hids_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet));
1047             btstack_assert(client != NULL);
1048 
1049             value = gatt_event_characteristic_value_query_result_get_value(packet);
1050             value_len = gatt_event_characteristic_value_query_result_get_value_length(packet);
1051 
1052 
1053             switch (client->state){
1054 #ifdef ENABLE_TESTING_SUPPORT
1055                 case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
1056                     printf("    Received CCC value: ");
1057                     printf_hexdump(value,  value_len);
1058                     break;
1059 #endif
1060                 case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT:{
1061                     uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet);
1062                     if (value_handle == client->services[client->service_index].hid_information_value_handle){
1063                         hids_client_emit_hid_information_event(client, value, value_len);
1064                         break;
1065                     }
1066                     if (value_handle == client->services[client->service_index].protocol_mode_value_handle){
1067                         hids_client_emit_protocol_mode_event(client, value, value_len);
1068                         break;
1069                     }
1070                     break;
1071                 }
1072                 default:
1073                     break;
1074             }
1075 
1076             break;
1077 
1078         case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
1079             client = hids_get_client_for_con_handle(gatt_event_characteristic_descriptor_query_result_get_handle(packet));
1080             btstack_assert(client != NULL);
1081 
1082             if (gatt_event_characteristic_descriptor_query_result_get_descriptor_length(packet) != 2){
1083                 break;
1084             }
1085 
1086             characteristic_descriptor_value = gatt_event_characteristic_descriptor_query_result_get_descriptor(packet);
1087             switch (client->state) {
1088                 case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID:
1089                     // get external report characteristic uuid
1090                     report_index = find_external_report_index_for_value_handle(client, gatt_event_characteristic_descriptor_query_result_get_descriptor_handle(packet));
1091                     if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
1092                         client->external_reports[report_index].external_report_reference_uuid = little_endian_read_16(characteristic_descriptor_value, 0);
1093 #ifdef ENABLE_TESTING_SUPPORT
1094                         printf("    Update external_report_reference_uuid of report index 0x%02X, service index 0x%02X, UUID 0x%02X\n",
1095                             report_index, client->service_index, client->external_reports[report_index].external_report_reference_uuid);
1096 #endif
1097                     }
1098                     break;
1099 
1100                 case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE:
1101 
1102                     if (client->report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
1103                         client->reports[client->report_index].report_id = characteristic_descriptor_value[0];
1104                         client->reports[client->report_index].report_type = (hid_report_type_t)characteristic_descriptor_value[1];
1105     #ifdef ENABLE_TESTING_SUPPORT
1106                         printf("    Update report ID and type [%d, %d] of report index 0x%02X, service index 0x%02X\n",
1107                             client->reports[client->report_index].report_id,
1108                             client->reports[client->report_index].report_type,
1109                             client->report_index, client->service_index);
1110     #endif
1111                     }
1112                     break;
1113 
1114                 default:
1115                     break;
1116             }
1117             break;
1118 
1119         case GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE:
1120             client = hids_get_client_for_con_handle(gatt_event_can_write_without_response_get_handle(packet));
1121             btstack_assert(client != NULL);
1122 
1123             switch (client->state){
1124                 case HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE:
1125                     att_status = gatt_client_write_value_of_characteristic_without_response(
1126                         client->con_handle,
1127                         client->services[client->service_index].protocol_mode_value_handle, 1, (uint8_t *)&client->required_protocol_mode);
1128 
1129 #ifdef ENABLE_TESTING_SUPPORT
1130                     printf("\n\nSet report mode %d of service %d, status 0x%02x", client->required_protocol_mode, client->service_index, att_status);
1131 #endif
1132 
1133                     if (att_status == ATT_ERROR_SUCCESS){
1134                         client->services[client->service_index].protocol_mode = client->required_protocol_mode;
1135                         if ((client->service_index + 1) < client->num_instances){
1136                             client->service_index++;
1137                             hids_run_for_client(client);
1138                             break;
1139                         }
1140                     }
1141 
1142                     // read UUIDS for external characteristics
1143                     if (hids_client_report_map_uuid_query_init(client)){
1144                         hids_run_for_client(client);
1145                         break;
1146                     }
1147 
1148                     // discover characteristic descriptor for all Report characteristics,
1149                     // then read value of characteristic descriptor to get Report ID
1150                     if (hids_client_report_query_init(client)){
1151                         hids_run_for_client(client);
1152                         break;
1153                     }
1154 
1155                     client->state = HIDS_CLIENT_STATE_CONNECTED;
1156                     hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
1157                     break;
1158 
1159                 case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
1160 #ifdef ENABLE_TESTING_SUPPORT
1161                     printf("    Write characteristic [service %d, handle 0x%04X]:\n", client->service_index, client->handle);
1162 #endif
1163                     client->state = HIDS_CLIENT_STATE_CONNECTED;
1164                     (void) gatt_client_write_value_of_characteristic_without_response(client->con_handle, client->handle, 1, (uint8_t *) &client->value);
1165                     break;
1166 
1167                 default:
1168                     break;
1169             }
1170 
1171             break;
1172 
1173         case GATT_EVENT_QUERY_COMPLETE:
1174             client = hids_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet));
1175             btstack_assert(client != NULL);
1176 
1177             att_status = gatt_event_query_complete_get_att_status(packet);
1178 
1179             switch (client->state){
1180                 case HIDS_CLIENT_STATE_W4_SERVICE_RESULT:
1181                     if (att_status != ATT_ERROR_SUCCESS){
1182                         hids_emit_connection_established(client, att_status);
1183                         hids_finalize_client(client);
1184                         break;
1185                     }
1186 
1187                     if (client->num_instances == 0){
1188                         hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
1189                         hids_finalize_client(client);
1190                         break;
1191                     }
1192 
1193                     client->service_index = 0;
1194                     client->state = HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC;
1195                     break;
1196 
1197                 case HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT:
1198                     if (att_status != ATT_ERROR_SUCCESS){
1199                         hids_emit_connection_established(client, att_status);
1200                         hids_finalize_client(client);
1201                         break;
1202                     }
1203 
1204                     if ((client->service_index + 1) < client->num_instances){
1205                         // discover characteristics of next service
1206                         client->service_index++;
1207                         client->state = HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC;
1208                         break;
1209                     }
1210 
1211                     btstack_assert(client->required_protocol_mode != HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT);
1212 
1213                     switch (client->required_protocol_mode){
1214                         case HID_PROTOCOL_MODE_REPORT:
1215                             for (i = 0; i < client->num_instances; i++){
1216                                 client->services[i].protocol_mode = HID_PROTOCOL_MODE_REPORT;
1217                             }
1218                             // 1. we need to get HID Descriptor and
1219                             // 2. get external Report characteristics if referenced from Report Map
1220                             if (hids_client_report_map_query_init(client)){
1221                                 break;
1222                             }
1223                             hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
1224                             hids_finalize_client(client);
1225                             return;
1226 
1227                         default:
1228                             // set boot mode
1229                             client->service_index = 0;
1230                             client->state = HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE;
1231                             break;
1232                     }
1233                     break;
1234 
1235 
1236                 // HID descriptor found
1237                 case HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR:
1238                     if (att_status != ATT_ERROR_SUCCESS){
1239                         hids_emit_connection_established(client, att_status);
1240                         hids_finalize_client(client);
1241                         break;
1242                     }
1243                     client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS;
1244                     break;
1245 
1246                 // found all descriptors, check if there is one with EXTERNAL_REPORT_REFERENCE
1247                 case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT:
1248                     // go for next report map
1249                     if (hids_client_report_query_next_report_map(client)){
1250                         break;
1251                     }
1252 
1253                     // read UUIDS for external characteristics
1254                     if (hids_client_report_map_uuid_query_init(client)){
1255                         break;
1256                     }
1257 
1258                     // discover characteristic descriptor for all Report characteristics,
1259                     // then read value of characteristic descriptor to get Report ID
1260                     if (hids_client_report_query_init(client)){
1261                         break;
1262                     }
1263 
1264                     hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
1265                     hids_finalize_client(client);
1266                     break;
1267 
1268                 case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID:
1269                     // go for next map report
1270                     if (hids_client_report_query_next_report_map_uuid(client)){
1271                         break;
1272                     }
1273 
1274                     // update external characteristics with correct value handle and end handle
1275                     client->state = HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC;
1276                     break;
1277 
1278                 case HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT:
1279                     // discover characteristic descriptor for all Report characteristics,
1280                     // then read value of characteristic descriptor to get Report ID
1281                     if (hids_client_report_query_init(client)){
1282                         break;
1283                     }
1284 
1285                     hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
1286                     hids_finalize_client(client);
1287                     break;
1288 
1289                 case HIDS_CLIENT_STATE_W4_REPORT_FOUND:
1290                     if (client->handle != 0){
1291                         client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE;
1292                         break;
1293                     }
1294                     // go for next report
1295                     if (hids_client_report_query_next_report(client)){
1296                         break;
1297                     }
1298                     client->state = HIDS_CLIENT_STATE_CONNECTED;
1299                     hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
1300                     break;
1301 
1302                 case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE:
1303                     // go for next report
1304                     if (hids_client_report_query_next_report(client)){
1305                         break;
1306                     }
1307                     if (hids_client_report_notifications_init(client)){
1308                         break;
1309                     }
1310                     client->state = HIDS_CLIENT_STATE_CONNECTED;
1311                     hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
1312                     break;
1313 
1314                 case HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED:
1315                     if (hids_client_report_next_notification_report_index(client)){
1316                         break;
1317                     }
1318                     client->state = HIDS_CLIENT_STATE_CONNECTED;
1319                     hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
1320                     break;
1321 
1322                 case HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED:
1323                     if (hids_client_report_next_notifications_configuration_report_index(client)){
1324                         break;
1325                     }
1326                     hids_emit_notifications_configuration(client);
1327                     client->state = HIDS_CLIENT_STATE_CONNECTED;
1328                     break;
1329 
1330 #ifdef ENABLE_TESTING_SUPPORT
1331                 case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
1332                     client->state = HIDS_CLIENT_W2_SEND_GET_REPORT;
1333                     break;
1334 #endif
1335 
1336                 case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT:
1337                 case HIDS_CLIENT_W4_WRITE_REPORT_DONE:
1338                     client->state = HIDS_CLIENT_STATE_CONNECTED;
1339                     break;
1340 
1341 
1342                 default:
1343                     break;
1344             }
1345             break;
1346 
1347         default:
1348             break;
1349     }
1350 
1351     if (client != NULL){
1352         hids_run_for_client(client);
1353     }
1354 }
1355 
1356 uint8_t hids_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, hid_protocol_mode_t protocol_mode, uint16_t * hids_cid){
1357     btstack_assert(packet_handler != NULL);
1358 
1359     hids_client_t * client = hids_get_client_for_con_handle(con_handle);
1360     if (client != NULL){
1361         return ERROR_CODE_COMMAND_DISALLOWED;
1362     }
1363 
1364     uint16_t cid = hids_get_next_cid();
1365     if (hids_cid != NULL) {
1366         *hids_cid = cid;
1367     }
1368 
1369     client = hids_create_client(con_handle, cid);
1370     if (client == NULL) {
1371         return BTSTACK_MEMORY_ALLOC_FAILED;
1372     }
1373 
1374     client->required_protocol_mode = protocol_mode;
1375     client->client_handler = packet_handler;
1376     client->state = HIDS_CLIENT_STATE_W2_QUERY_SERVICE;
1377 
1378     hids_run_for_client(client);
1379     return ERROR_CODE_SUCCESS;
1380 }
1381 
1382 uint8_t hids_client_disconnect(uint16_t hids_cid){
1383     hids_client_t * client = hids_get_client_for_cid(hids_cid);
1384     if (client == NULL){
1385         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1386     }
1387     // finalize connection
1388     hids_finalize_client(client);
1389     return ERROR_CODE_SUCCESS;
1390 }
1391 
1392 uint8_t hids_client_send_write_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type, const uint8_t * report, uint8_t report_len){
1393     hids_client_t * client = hids_get_client_for_cid(hids_cid);
1394     if (client == NULL){
1395         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1396     }
1397 
1398     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1399         return ERROR_CODE_COMMAND_DISALLOWED;
1400     }
1401 
1402     uint8_t report_index = find_report_index_for_report_id_and_report_type(client, report_id, report_type);
1403 
1404     if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
1405         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1406     }
1407 
1408     uint16_t mtu;
1409     uint8_t status = gatt_client_get_mtu(client->con_handle, &mtu);
1410 
1411     if (status != ERROR_CODE_SUCCESS){
1412         return status;
1413     }
1414 
1415     if (mtu - 2 < report_len){
1416         return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
1417     }
1418 
1419     client->state = HIDS_CLIENT_W2_SEND_WRITE_REPORT;
1420     client->report_index = report_index;
1421     client->report = report;
1422     client->report_len = report_len;
1423 
1424     hids_run_for_client(client);
1425     return ERROR_CODE_SUCCESS;
1426 }
1427 
1428 uint8_t hids_client_send_get_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type){
1429     hids_client_t * client = hids_get_client_for_cid(hids_cid);
1430     if (client == NULL){
1431         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1432     }
1433 
1434     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1435         return ERROR_CODE_COMMAND_DISALLOWED;
1436     }
1437 
1438     uint8_t report_index = find_report_index_for_report_id_and_report_type(client, report_id, report_type);
1439     if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
1440         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1441     }
1442 
1443     client->report_index = report_index;
1444 
1445 #ifdef ENABLE_TESTING_SUPPORT
1446     client->state = HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION;
1447 #else
1448     client->state = HIDS_CLIENT_W2_SEND_GET_REPORT;
1449 #endif
1450     hids_run_for_client(client);
1451     return ERROR_CODE_SUCCESS;
1452 }
1453 
1454 
1455 uint8_t hids_client_get_hid_information(uint16_t hids_cid, uint8_t service_index){
1456     hids_client_t * client = hids_get_client_for_cid(hids_cid);
1457     if (client == NULL){
1458         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1459     }
1460 
1461     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1462         return ERROR_CODE_COMMAND_DISALLOWED;
1463     }
1464 
1465     if (service_index >= client->num_instances){
1466         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1467     }
1468 
1469     client->service_index = service_index;
1470     client->handle = client->services[client->service_index].hid_information_value_handle;
1471 
1472     client->state = HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC;
1473     hids_run_for_client(client);
1474     return ERROR_CODE_SUCCESS;
1475 }
1476 
1477 uint8_t hids_client_get_protocol_mode(uint16_t hids_cid, uint8_t service_index){
1478     hids_client_t * client = hids_get_client_for_cid(hids_cid);
1479     if (client == NULL){
1480         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1481     }
1482 
1483     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1484         return ERROR_CODE_COMMAND_DISALLOWED;
1485     }
1486 
1487     if (service_index >= client->num_instances){
1488         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1489     }
1490 
1491     client->service_index = service_index;
1492     client->handle = client->services[client->service_index].protocol_mode_value_handle;
1493 
1494     client->state = HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC;
1495     hids_run_for_client(client);
1496     return ERROR_CODE_SUCCESS;
1497 }
1498 
1499 uint8_t hids_client_send_set_protocol_mode(uint16_t hids_cid, uint8_t service_index, hid_protocol_mode_t protocol_mode){
1500     hids_client_t * client = hids_get_client_for_cid(hids_cid);
1501     if (client == NULL){
1502         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1503     }
1504 
1505     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1506         return ERROR_CODE_COMMAND_DISALLOWED;
1507     }
1508 
1509     if (service_index >= client->num_instances){
1510         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1511     }
1512 
1513     client->service_index = service_index;
1514     client->handle = client->services[client->service_index].protocol_mode_value_handle;
1515     client->value = (uint8_t)protocol_mode;
1516 
1517     client->state = HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE;
1518     hids_run_for_client(client);
1519     return ERROR_CODE_SUCCESS;
1520 }
1521 
1522 
1523 static uint8_t hids_client_send_control_point_cmd(uint16_t hids_cid, uint8_t service_index, uint8_t value){
1524     hids_client_t * client = hids_get_client_for_cid(hids_cid);
1525     if (client == NULL){
1526         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1527     }
1528 
1529     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1530         return ERROR_CODE_COMMAND_DISALLOWED;
1531     }
1532 
1533     if (service_index >= client->num_instances){
1534         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1535     }
1536 
1537     client->service_index = service_index;
1538     client->handle = client->services[client->service_index].control_point_value_handle;
1539     client->value = value;
1540 
1541     client->state = HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE;
1542     hids_run_for_client(client);
1543     return ERROR_CODE_SUCCESS;
1544 }
1545 
1546 uint8_t hids_client_send_suspend(uint16_t hids_cid, uint8_t service_index){
1547     return hids_client_send_control_point_cmd(hids_cid, service_index, 0);
1548 }
1549 
1550 uint8_t hids_client_send_exit_suspend(uint16_t hids_cid, uint8_t service_index){
1551     return hids_client_send_control_point_cmd(hids_cid, service_index, 1);
1552 }
1553 
1554 uint8_t hids_client_enable_notifications(uint16_t hids_cid){
1555      hids_client_t * client = hids_get_client_for_cid(hids_cid);
1556     if (client == NULL){
1557         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1558     }
1559 
1560     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1561         return ERROR_CODE_COMMAND_DISALLOWED;
1562     }
1563     client->value = GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
1564     if (hids_client_notifications_configuration_init(client)){
1565         hids_run_for_client(client);
1566         return ERROR_CODE_SUCCESS;
1567     }
1568     hids_emit_notifications_configuration(client);
1569     return ERROR_CODE_SUCCESS;
1570 }
1571 
1572 uint8_t hids_client_disable_notifications(uint16_t hids_cid){
1573          hids_client_t * client = hids_get_client_for_cid(hids_cid);
1574     if (client == NULL){
1575         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1576     }
1577 
1578     if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
1579         return ERROR_CODE_COMMAND_DISALLOWED;
1580     }
1581 
1582     client->value = GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NONE;
1583     if (hids_client_notifications_configuration_init(client)){
1584         hids_run_for_client(client);
1585         return ERROR_CODE_SUCCESS;
1586     }
1587     hids_emit_notifications_configuration(client);
1588     return ERROR_CODE_SUCCESS;
1589 }
1590 
1591 void hids_client_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len){
1592     hids_client_descriptor_storage = hid_descriptor_storage;
1593     hids_client_descriptor_storage_len = hid_descriptor_storage_len;
1594 }
1595 
1596 void hids_client_deinit(void){}
1597