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