xref: /btstack/src/ble/gatt-service/hids_device.c (revision 1b9864ea2acef64d69fcf42d50d9a3bf75f3d6c3)
1 /*
2  * Copyright (C) 2014 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_device.c"
39 
40 /**
41  * Implementation of the GATT HIDS Device
42  * To use with your application, add '#import <hids.gatt>' to your .gatt file
43  */
44 
45 #include "hids_device.h"
46 
47 #include "ble/att_db.h"
48 #include "ble/att_server.h"
49 #include "bluetooth_gatt.h"
50 #include "btstack_util.h"
51 #include "btstack_debug.h"
52 #include "btstack_hid_parser.h"
53 
54 #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS    0x80
55 
56 // storage for 'generic' HID Device with single Input, Output, and Feature Report
57 static hids_device_report_t hid_reports_generic_storage[3];
58 
59 typedef struct{
60     uint16_t        con_handle;
61 
62     uint8_t         hid_country_code;
63     const uint8_t * hid_descriptor;
64     uint16_t        hid_descriptor_size;
65 
66     uint16_t        hid_report_map_handle;
67     uint8_t         hid_protocol_mode;
68     uint16_t        hid_protocol_mode_value_handle;
69 
70     uint16_t        hid_boot_mouse_input_value_handle;
71     uint16_t        hid_boot_mouse_input_client_configuration_handle;
72     uint16_t        hid_boot_mouse_input_client_configuration_value;
73 
74     uint16_t        hid_boot_keyboard_input_value_handle;
75     uint16_t        hid_boot_keyboard_input_client_configuration_handle;
76     uint16_t        hid_boot_keyboard_input_client_configuration_value;
77 
78     hids_device_report_t * hid_reports;
79 
80     uint8_t         hid_input_reports_num;
81     uint8_t         hid_output_reports_num;
82     uint8_t         hid_feature_reports_num;
83 
84     uint16_t        hid_control_point_value_handle;
85     uint8_t         hid_control_point_suspend;
86 
87     btstack_context_callback_registration_t  can_send_now_callback;
88 } hids_device_t;
89 
90 static hids_device_t hids_device;
91 
92 static btstack_packet_handler_t packet_handler;
93 static att_service_handler_t hid_service;
94 static void (*hids_device_get_report_callback)(hci_con_handle_t hid_cid, hid_report_type_t report_type, uint16_t report_id, uint16_t max_report_size, uint8_t * out_report);
95 
96 // TODO: store hids device connection into list
97 static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
98     UNUSED(con_handle);
99     return &hids_device;
100 }
101 
102 static hids_device_t * hids_device_create_instance(void){
103     memset(&hids_device, 0, sizeof(hids_device_t));
104     return &hids_device;
105 }
106 
107 static hids_device_report_t * hids_device_get_report_for_value_handle(hids_device_t * device, uint16_t client_configuration_handle){
108     uint8_t pos;
109     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
110     for (pos = 0 ; pos < total_reports ; pos++){
111         if (device->hid_reports[pos].value_handle == client_configuration_handle){
112             return &device->hid_reports[pos];
113         }
114     }
115     return NULL;
116 }
117 
118 static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){
119     uint8_t pos;
120     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
121     for (pos = 0 ; pos < total_reports ; pos++){
122         if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){
123             return &device->hid_reports[pos];
124         }
125     }
126     return NULL;
127 }
128 
129 static hids_device_report_t *
130 hids_device_get_report_for_id_and_type(hids_device_t *device, uint16_t report_id, hid_report_type_t type) {
131     uint8_t pos;
132     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
133     for (pos = 0 ; pos < total_reports ; pos++){
134         if ((device->hid_reports[pos].type == type) && (device->hid_reports[pos].id == report_id)){
135             return &device->hid_reports[pos];
136         }
137     }
138     return NULL;
139 }
140 
141 static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
142     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
143     if (!instance){
144         log_error("no instance for handle 0x%02x", con_handle);
145         return;
146     }
147 
148     if (!packet_handler) return;
149     uint8_t buffer[6];
150     buffer[0] = HCI_EVENT_HIDS_META;
151     buffer[1] = 4;
152     buffer[2] = event;
153     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
154     buffer[5] = value;
155     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
156 }
157 
158 static void hids_device_emit_event_with_uint8_uint8_t(uint8_t event, hci_con_handle_t con_handle, uint8_t value_1, uint8_t value_2){
159     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
160     if (!instance){
161         log_error("no instance for handle 0x%02x", con_handle);
162         return;
163     }
164 
165     if (!packet_handler) return;
166     uint8_t buffer[7];
167     buffer[0] = HCI_EVENT_HIDS_META;
168     buffer[1] = 4;
169     buffer[2] = event;
170     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
171     buffer[5] = value_1;
172     buffer[6] = value_2;
173     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
174 }
175 
176 static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
177     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
178     if (!instance){
179         log_error("no instance for handle 0x%02x", con_handle);
180         return;
181     }
182 
183     if (!packet_handler) return;
184     uint8_t buffer[5];
185     buffer[0] = HCI_EVENT_HIDS_META;
186     buffer[1] = 4;
187     buffer[2] = event;
188     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
189     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
190 }
191 
192 static void hids_device_can_send_now(void * context){
193     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
194     // notify client
195     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
196     if (!instance){
197         log_error("no instance for handle 0x%02x", con_handle);
198         return;
199     }
200 
201     if (!packet_handler) return;
202     uint8_t buffer[5];
203     buffer[0] = HCI_EVENT_HIDS_META;
204     buffer[1] = 3;
205     buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
206     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
207     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
208 }
209 
210 // ATT Client Read Callback for Dynamic Data
211 // - if buffer == NULL, don't copy data, just return size of value
212 // - if buffer != NULL, copy data and return number bytes copied
213 static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
214     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
215     if (!instance){
216         log_error("no instance for handle 0x%02x", con_handle);
217         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
218     }
219 
220     if (att_handle == instance->hid_protocol_mode_value_handle){
221         log_info("Read protocol mode");
222         return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
223     }
224 
225     if (att_handle == instance->hid_report_map_handle){
226         log_info("Read report map");
227         return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
228     }
229 
230     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
231         return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
232     }
233 
234     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
235         return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
236     }
237 
238     if (att_handle == instance->hid_control_point_value_handle){
239         if (buffer && (buffer_size >= 1u)){
240             buffer[0] = instance->hid_control_point_suspend;
241         }
242         return 1;
243     }
244 
245     uint8_t boot_report_size = 0;
246     if (att_handle == instance->hid_boot_mouse_input_value_handle){
247         boot_report_size = 3;
248     }
249     if (att_handle == instance->hid_boot_keyboard_input_value_handle){
250         boot_report_size = 8;
251     }
252     if (boot_report_size != 0){
253         // no callback, no report
254         if (hids_device_get_report_callback == NULL){
255             return 0;
256         }
257         // answer length request by ATT Server
258         if (buffer == NULL){
259             return boot_report_size;
260         } else {
261             // Report ID 0, Type Input
262             (*hids_device_get_report_callback)(con_handle, HID_REPORT_TYPE_INPUT, 0, boot_report_size, buffer);
263             return boot_report_size;
264         }
265     }
266 
267     hids_device_report_t * report;
268     report = hids_device_get_report_for_value_handle(instance, att_handle);
269     if (report != NULL){
270         // no callback, no report
271         if (hids_device_get_report_callback == NULL){
272             return 0;
273         }
274         // answer length request by ATT Server
275         if (buffer == NULL){
276             return report->size;
277         } else {
278             uint16_t max_size = btstack_min(report->size, buffer_size);
279             (*hids_device_get_report_callback)(con_handle, report->type, report->id, max_size, buffer);
280             return report->size;
281         }
282     }
283 
284     report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
285     if (report != NULL){
286         return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size);
287     }
288     return 0;
289 }
290 
291 static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
292     UNUSED(buffer_size);
293     UNUSED(offset);
294 
295     if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
296         return 0;
297     }
298 
299     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
300     if (!instance){
301         log_error("no instance for handle 0x%02x", con_handle);
302         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
303     }
304 
305     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
306         uint16_t new_value = little_endian_read_16(buffer, 0);
307         instance->hid_boot_mouse_input_client_configuration_value = new_value;
308         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
309     }
310     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
311         uint16_t new_value = little_endian_read_16(buffer, 0);
312         instance->hid_boot_keyboard_input_client_configuration_value = new_value;
313         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
314     }
315 
316     if (att_handle == instance->hid_protocol_mode_value_handle){
317         instance->hid_protocol_mode = buffer[0];
318         log_info("Set protocol mode: %u", instance->hid_protocol_mode);
319         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
320     }
321 
322     if (att_handle == instance->hid_control_point_value_handle){
323         if (buffer_size < 1u){
324             return ATT_ERROR_INVALID_OFFSET;
325         }
326         instance->hid_control_point_suspend = buffer[0];
327         instance->con_handle = con_handle;
328         log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
329         if (instance->hid_control_point_suspend == 0u){
330             hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
331         } else if (instance->hid_control_point_suspend == 1u){
332             hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
333         }
334     }
335 
336     hids_device_report_t * report;
337     report = hids_device_get_report_for_value_handle(instance, att_handle);
338     if (report != NULL){
339         // assemble event in buffer
340         uint8_t event[257];
341         uint16_t pos = 0;
342         event[pos++] = HCI_EVENT_HIDS_META;
343         // skip length
344         pos++;
345         event[pos++] = HIDS_SUBEVENT_SET_REPORT;
346         little_endian_store_16(event, pos, con_handle);
347         pos += 2;
348         event[pos++] = report->id;
349         event[pos++] = (uint8_t) report->type;
350         uint8_t length_to_copy = btstack_min(buffer_size, 250);
351         event[pos++] = length_to_copy;
352         memcpy(&event[pos], buffer, length_to_copy);
353         pos += length_to_copy;
354         // set event length
355         event[1] = pos - 2;
356         (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
357         return 0;
358     }
359 
360     report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
361     if (report != NULL){
362         uint16_t new_value = little_endian_read_16(buffer, 0);
363         report->client_configuration_value = new_value;
364         log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value);
365 
366         switch (report->type){
367             case HID_REPORT_TYPE_INPUT:
368                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
369                 break;
370             case HID_REPORT_TYPE_OUTPUT:
371                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
372                 break;
373             case HID_REPORT_TYPE_FEATURE:
374                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
375                 break;
376             default:
377                 btstack_unreachable();
378                 break;
379         }
380     }
381     return 0;
382 }
383 
384 void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size,
385     uint16_t num_reports, hids_device_report_t * report_storage){
386 
387     hids_device_t * instance = hids_device_create_instance();
388 
389     btstack_assert(num_reports > 0);
390     btstack_assert(report_storage != NULL);
391 
392     instance->hid_country_code    = hid_country_code;
393     instance->hid_descriptor      = hid_descriptor;
394     instance->hid_descriptor_size = hid_descriptor_size;
395 
396     // default
397     instance->hid_protocol_mode   = 1;
398 
399     // get service handle range
400     uint16_t start_handle = 0;
401     uint16_t end_handle   = 0xffff;
402     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
403     btstack_assert(service_found != 0);
404     UNUSED(service_found);
405 
406     // get report map handle
407     instance->hid_report_map_handle                               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
408 
409     // get report map handle
410     instance->hid_protocol_mode_value_handle                      = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE);
411 
412     // get value and client configuration handles for boot mouse input, boot keyboard input and report input
413     instance->hid_boot_mouse_input_value_handle                   = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT);
414     instance->hid_boot_mouse_input_client_configuration_handle    = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT);
415 
416     instance->hid_boot_keyboard_input_value_handle                = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT);
417     instance->hid_boot_keyboard_input_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT);
418 
419     instance->hid_control_point_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT);
420 
421     log_info("hid_report_map_handle                               0x%02x", instance->hid_report_map_handle);
422     log_info("hid_protocol_mode_value_handle                      0x%02x", instance->hid_protocol_mode_value_handle);
423     log_info("hid_boot_mouse_input_value_handle                   0x%02x", instance->hid_boot_mouse_input_value_handle);
424     log_info("hid_boot_mouse_input_client_configuration_handle    0x%02x", instance->hid_boot_mouse_input_client_configuration_handle);
425     log_info("hid_boot_keyboard_input_value_handle                0x%02x", instance->hid_boot_keyboard_input_value_handle);
426     log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle);
427     log_info("hid_control_point_value_handle                      0x%02x", instance->hid_control_point_value_handle);
428 
429     instance->hid_reports = report_storage;
430 
431     uint16_t hid_reports_num = num_reports;
432     uint16_t assigned_reports_num = 0;
433     uint16_t start_chr_handle = start_handle;
434 
435     while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) {
436         // mandatory
437         uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
438         if (chr_value_handle == 0){
439             break;
440         }
441 
442         // optional
443         uint16_t chr_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
444 
445         // mandatory
446         uint16_t report_reference_handle = gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT, ORG_BLUETOOTH_DESCRIPTOR_REPORT_REFERENCE);
447         if (report_reference_handle == 0){
448             break;
449         }
450 
451         // get report id and type from report reference
452         uint16_t report_reference_value_len;
453         const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len);
454         if (report_reference_value == NULL){
455             break;
456         }
457         if (report_reference_value_len != 2){
458             break;
459         }
460         uint8_t report_id = report_reference_value[0];
461         hid_report_type_t report_type = (hid_report_type_t) report_reference_value[1];
462 
463         // store report info
464         hids_device_report_t * report = &report_storage[assigned_reports_num];
465         report->value_handle = chr_value_handle;
466         report->client_configuration_handle = chr_client_configuration_handle;
467         report->client_configuration_value = 0;
468         report->id   = report_id;
469         report->type = report_type;
470         report->size = btstack_hid_get_report_size_for_id(report_id, report_type, hid_descriptor_size, hid_descriptor);
471 
472         switch (report->type){
473             case HID_REPORT_TYPE_INPUT:
474                 instance->hid_input_reports_num++;
475                 break;
476             case HID_REPORT_TYPE_OUTPUT:
477                 instance->hid_output_reports_num++;
478                 break;
479             case HID_REPORT_TYPE_FEATURE:
480                 instance->hid_feature_reports_num++;
481                 break;
482             default:
483                 btstack_unreachable();
484                 return;
485         }
486         log_info("hid_report_value_handle                       0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type);
487         if (report->client_configuration_handle != 0){
488             log_info("hid_report_client_configuration_handle        0x%02x", report->client_configuration_handle);
489         }
490 
491         assigned_reports_num++;
492         start_chr_handle = report_reference_handle + 1;
493     }
494 
495     // register service with ATT Server
496     hid_service.start_handle   = start_handle;
497     hid_service.end_handle     = end_handle;
498     hid_service.read_callback  = &att_read_callback;
499     hid_service.write_callback = &att_write_callback;
500     att_server_register_service_handler(&hid_service);
501 }
502 
503 /**
504  * @brief Set up HIDS Device
505  */
506 void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
507     uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t);
508     hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage);
509 }
510 
511 /**
512  * @brief Register callback for the HIDS Device client.
513  * @param callback
514  */
515 void hids_device_register_packet_handler(btstack_packet_handler_t callback){
516     packet_handler = callback;
517 }
518 
519 void hids_device_register_get_report_callback(void (*callback)(hci_con_handle_t con_handle, hid_report_type_t report_type, uint16_t report_id, uint16_t max_report_size, uint8_t * out_report)){
520     hids_device_get_report_callback = callback;
521 }
522 
523 /**
524  * @brief Request can send now event to send HID Report
525  * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
526  * @param hid_cid
527  */
528 void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
529     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
530     if (!instance){
531         log_error("no instance for handle 0x%02x", con_handle);
532         return;
533     }
534 
535     instance->can_send_now_callback.callback = &hids_device_can_send_now;
536     instance->can_send_now_callback.context  = (void*) (uintptr_t) con_handle;
537     att_server_register_can_send_now_callback(&instance->can_send_now_callback, con_handle);
538 }
539 
540 uint8_t hids_device_send_input_report_for_id(hci_con_handle_t con_handle, uint16_t report_id, const uint8_t * report, uint16_t report_len){
541     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
542     if (!instance){
543         log_error("no instance for handle 0x%02x", con_handle);
544         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
545     }
546 
547     hids_device_report_t * report_storage = hids_device_get_report_for_id_and_type(instance, report_id,
548                                                                                    HID_REPORT_TYPE_INPUT);
549     if (report_storage == NULL){
550         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
551     }
552 
553     return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
554 }
555 
556 uint8_t hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
557     hids_device_t * device = hids_device_get_instance_for_con_handle(con_handle);
558     if (!device){
559         log_error("no instance for handle 0x%02x", con_handle);
560         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
561     }
562 
563     uint8_t pos;
564     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
565     for (pos = 0 ; pos < total_reports ; pos++){
566         hids_device_report_t * report_storage = &device->hid_reports[pos];
567         if (report_storage->type == HID_REPORT_TYPE_INPUT){
568             return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
569         }
570     }
571     return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
572 }
573 
574 /**
575  * @brief Send HID Boot Mouse Input Report
576  */
577 uint8_t hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
578     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
579     if (!instance){
580         log_error("no instance for handle 0x%02x", con_handle);
581         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
582     }
583     return att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
584 }
585 
586 /**
587  * @brief Send HID Boot Mouse Input Report
588  */
589 uint8_t hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
590     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
591     if (!instance){
592         log_error("no instance for handle 0x%02x", con_handle);
593         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
594     }
595     return att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
596 }
597