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