xref: /btstack/src/ble/gatt-service/hids_device.c (revision 1b9864ea2acef64d69fcf42d50d9a3bf75f3d6c3)
1a4bfc4feSMatthias Ringwald /*
2a4bfc4feSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3a4bfc4feSMatthias Ringwald  *
4a4bfc4feSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5a4bfc4feSMatthias Ringwald  * modification, are permitted provided that the following conditions
6a4bfc4feSMatthias Ringwald  * are met:
7a4bfc4feSMatthias Ringwald  *
8a4bfc4feSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9a4bfc4feSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10a4bfc4feSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11a4bfc4feSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12a4bfc4feSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13a4bfc4feSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14a4bfc4feSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15a4bfc4feSMatthias Ringwald  *    from this software without specific prior written permission.
16a4bfc4feSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17a4bfc4feSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18a4bfc4feSMatthias Ringwald  *    monetary gain.
19a4bfc4feSMatthias Ringwald  *
20a4bfc4feSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21a4bfc4feSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a4bfc4feSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25a4bfc4feSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26a4bfc4feSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27a4bfc4feSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28a4bfc4feSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29a4bfc4feSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30a4bfc4feSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a4bfc4feSMatthias Ringwald  * SUCH DAMAGE.
32a4bfc4feSMatthias Ringwald  *
33a4bfc4feSMatthias Ringwald  * Please inquire about commercial licensing options at
34a4bfc4feSMatthias Ringwald  * [email protected]
35a4bfc4feSMatthias Ringwald  *
36a4bfc4feSMatthias Ringwald  */
37a4bfc4feSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hids_device.c"
39523edd21SMatthias Ringwald 
40a4bfc4feSMatthias Ringwald /**
41a4bfc4feSMatthias Ringwald  * Implementation of the GATT HIDS Device
42a4bfc4feSMatthias Ringwald  * To use with your application, add '#import <hids.gatt>' to your .gatt file
43a4bfc4feSMatthias Ringwald  */
44a4bfc4feSMatthias Ringwald 
45a4bfc4feSMatthias Ringwald #include "hids_device.h"
46a4bfc4feSMatthias Ringwald 
4735b80bb0SMatthias Ringwald #include "ble/att_db.h"
4835b80bb0SMatthias Ringwald #include "ble/att_server.h"
49a4bfc4feSMatthias Ringwald #include "bluetooth_gatt.h"
50a4bfc4feSMatthias Ringwald #include "btstack_util.h"
51523edd21SMatthias Ringwald #include "btstack_debug.h"
52*1b9864eaSMatthias Ringwald #include "btstack_hid_parser.h"
53a4bfc4feSMatthias Ringwald 
540235c9e5SMilanka Ringwald #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS    0x80
550235c9e5SMilanka Ringwald 
56160eac51SMatthias Ringwald // storage for 'generic' HID Device with single Input, Output, and Feature Report
57160eac51SMatthias Ringwald static hids_device_report_t hid_reports_generic_storage[3];
58f4f6c196SMilanka Ringwald 
590235c9e5SMilanka Ringwald typedef struct{
600235c9e5SMilanka Ringwald     uint16_t        con_handle;
610235c9e5SMilanka Ringwald 
620235c9e5SMilanka Ringwald     uint8_t         hid_country_code;
630235c9e5SMilanka Ringwald     const uint8_t * hid_descriptor;
640235c9e5SMilanka Ringwald     uint16_t        hid_descriptor_size;
650235c9e5SMilanka Ringwald 
660235c9e5SMilanka Ringwald     uint16_t        hid_report_map_handle;
67c1dbba9dSMatthias Ringwald     uint8_t         hid_protocol_mode;
680235c9e5SMilanka Ringwald     uint16_t        hid_protocol_mode_value_handle;
690235c9e5SMilanka Ringwald 
700235c9e5SMilanka Ringwald     uint16_t        hid_boot_mouse_input_value_handle;
710235c9e5SMilanka Ringwald     uint16_t        hid_boot_mouse_input_client_configuration_handle;
720235c9e5SMilanka Ringwald     uint16_t        hid_boot_mouse_input_client_configuration_value;
730235c9e5SMilanka Ringwald 
740235c9e5SMilanka Ringwald     uint16_t        hid_boot_keyboard_input_value_handle;
750235c9e5SMilanka Ringwald     uint16_t        hid_boot_keyboard_input_client_configuration_handle;
760235c9e5SMilanka Ringwald     uint16_t        hid_boot_keyboard_input_client_configuration_value;
770235c9e5SMilanka Ringwald 
78f4f6c196SMilanka Ringwald     hids_device_report_t * hid_reports;
790235c9e5SMilanka Ringwald 
80f4f6c196SMilanka Ringwald     uint8_t         hid_input_reports_num;
81f4f6c196SMilanka Ringwald     uint8_t         hid_output_reports_num;
82f4f6c196SMilanka Ringwald     uint8_t         hid_feature_reports_num;
830235c9e5SMilanka Ringwald 
840235c9e5SMilanka Ringwald     uint16_t        hid_control_point_value_handle;
850235c9e5SMilanka Ringwald     uint8_t         hid_control_point_suspend;
860235c9e5SMilanka Ringwald 
87086ea75fSMatthias Ringwald     btstack_context_callback_registration_t  can_send_now_callback;
880235c9e5SMilanka Ringwald } hids_device_t;
890235c9e5SMilanka Ringwald 
900235c9e5SMilanka Ringwald static hids_device_t hids_device;
910235c9e5SMilanka Ringwald 
92a4bfc4feSMatthias Ringwald static btstack_packet_handler_t packet_handler;
93a4bfc4feSMatthias Ringwald static att_service_handler_t hid_service;
94*1b9864eaSMatthias Ringwald 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);
95a4bfc4feSMatthias Ringwald 
960235c9e5SMilanka Ringwald // TODO: store hids device connection into list
970235c9e5SMilanka Ringwald static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
980235c9e5SMilanka Ringwald     UNUSED(con_handle);
990235c9e5SMilanka Ringwald     return &hids_device;
1000235c9e5SMilanka Ringwald }
1010235c9e5SMilanka Ringwald 
1020235c9e5SMilanka Ringwald static hids_device_t * hids_device_create_instance(void){
103f4f6c196SMilanka Ringwald     memset(&hids_device, 0, sizeof(hids_device_t));
1040235c9e5SMilanka Ringwald     return &hids_device;
1050235c9e5SMilanka Ringwald }
1060235c9e5SMilanka Ringwald 
107aa3c9a66SMatthias Ringwald static hids_device_report_t * hids_device_get_report_for_value_handle(hids_device_t * device, uint16_t client_configuration_handle){
108aa3c9a66SMatthias Ringwald     uint8_t pos;
109aa3c9a66SMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
110aa3c9a66SMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
111aa3c9a66SMatthias Ringwald         if (device->hid_reports[pos].value_handle == client_configuration_handle){
112aa3c9a66SMatthias Ringwald             return &device->hid_reports[pos];
113aa3c9a66SMatthias Ringwald         }
114aa3c9a66SMatthias Ringwald     }
115aa3c9a66SMatthias Ringwald     return NULL;
116aa3c9a66SMatthias Ringwald }
117aa3c9a66SMatthias Ringwald 
118f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){
119d75a985bSMatthias Ringwald     uint8_t pos;
120d75a985bSMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
121d75a985bSMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
122f4f6c196SMilanka Ringwald         if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){
123f4f6c196SMilanka Ringwald             return &device->hid_reports[pos];
124f4f6c196SMilanka Ringwald         }
125f4f6c196SMilanka Ringwald     }
126f4f6c196SMilanka Ringwald     return NULL;
127f4f6c196SMilanka Ringwald }
128f4f6c196SMilanka Ringwald 
129a3c1c955SMatthias Ringwald static hids_device_report_t *
130a3c1c955SMatthias Ringwald hids_device_get_report_for_id_and_type(hids_device_t *device, uint16_t report_id, hid_report_type_t type) {
13145569c34SMatthias Ringwald     uint8_t pos;
13245569c34SMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
13345569c34SMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
13445569c34SMatthias Ringwald         if ((device->hid_reports[pos].type == type) && (device->hid_reports[pos].id == report_id)){
13545569c34SMatthias Ringwald             return &device->hid_reports[pos];
13645569c34SMatthias Ringwald         }
13745569c34SMatthias Ringwald     }
13845569c34SMatthias Ringwald     return NULL;
13945569c34SMatthias Ringwald }
14045569c34SMatthias Ringwald 
141a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
1420235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1430235c9e5SMilanka Ringwald     if (!instance){
1440235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1450235c9e5SMilanka Ringwald         return;
1460235c9e5SMilanka Ringwald     }
1470235c9e5SMilanka Ringwald 
148a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
149a4bfc4feSMatthias Ringwald     uint8_t buffer[6];
150a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
151a4bfc4feSMatthias Ringwald     buffer[1] = 4;
152a4bfc4feSMatthias Ringwald     buffer[2] = event;
153a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
154a4bfc4feSMatthias Ringwald     buffer[5] = value;
155a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
156a4bfc4feSMatthias Ringwald }
157a4bfc4feSMatthias Ringwald 
158f00767dfSMatthias Ringwald 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){
159f00767dfSMatthias Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
160f00767dfSMatthias Ringwald     if (!instance){
161f00767dfSMatthias Ringwald         log_error("no instance for handle 0x%02x", con_handle);
162f00767dfSMatthias Ringwald         return;
163f00767dfSMatthias Ringwald     }
164f00767dfSMatthias Ringwald 
165f00767dfSMatthias Ringwald     if (!packet_handler) return;
166f00767dfSMatthias Ringwald     uint8_t buffer[7];
167f00767dfSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
168f00767dfSMatthias Ringwald     buffer[1] = 4;
169f00767dfSMatthias Ringwald     buffer[2] = event;
170f00767dfSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
171f00767dfSMatthias Ringwald     buffer[5] = value_1;
172f00767dfSMatthias Ringwald     buffer[6] = value_2;
173f00767dfSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
174f00767dfSMatthias Ringwald }
175f00767dfSMatthias Ringwald 
1760235c9e5SMilanka Ringwald static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
1770235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1780235c9e5SMilanka Ringwald     if (!instance){
1790235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1800235c9e5SMilanka Ringwald         return;
1810235c9e5SMilanka Ringwald     }
1820235c9e5SMilanka Ringwald 
1830235c9e5SMilanka Ringwald     if (!packet_handler) return;
1840235c9e5SMilanka Ringwald     uint8_t buffer[5];
1850235c9e5SMilanka Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
1860235c9e5SMilanka Ringwald     buffer[1] = 4;
1870235c9e5SMilanka Ringwald     buffer[2] = event;
1880235c9e5SMilanka Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
1890235c9e5SMilanka Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
1900235c9e5SMilanka Ringwald }
1910235c9e5SMilanka Ringwald 
192a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){
193a4bfc4feSMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
194a4bfc4feSMatthias Ringwald     // notify client
1950235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1960235c9e5SMilanka Ringwald     if (!instance){
1970235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1980235c9e5SMilanka Ringwald         return;
1990235c9e5SMilanka Ringwald     }
2000235c9e5SMilanka Ringwald 
201a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
202a4bfc4feSMatthias Ringwald     uint8_t buffer[5];
203a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
204a4bfc4feSMatthias Ringwald     buffer[1] = 3;
205a4bfc4feSMatthias Ringwald     buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
206a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
207a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
208a4bfc4feSMatthias Ringwald }
209a4bfc4feSMatthias Ringwald 
210a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data
211a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
212a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
2130235c9e5SMilanka Ringwald 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){
2140235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
2150235c9e5SMilanka Ringwald     if (!instance){
2160235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
2170235c9e5SMilanka Ringwald         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
218a4bfc4feSMatthias Ringwald     }
2190235c9e5SMilanka Ringwald 
2200235c9e5SMilanka Ringwald     if (att_handle == instance->hid_protocol_mode_value_handle){
2210235c9e5SMilanka Ringwald         log_info("Read protocol mode");
2220235c9e5SMilanka Ringwald         return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
2230235c9e5SMilanka Ringwald     }
224c9d24807SMilanka Ringwald 
2250235c9e5SMilanka Ringwald     if (att_handle == instance->hid_report_map_handle){
226523edd21SMatthias Ringwald         log_info("Read report map");
2270235c9e5SMilanka Ringwald         return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
228a4bfc4feSMatthias Ringwald     }
229c9d24807SMilanka Ringwald 
2300235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
2310235c9e5SMilanka Ringwald         return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
232a4bfc4feSMatthias Ringwald     }
233c9d24807SMilanka Ringwald 
2340235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
2350235c9e5SMilanka Ringwald         return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
236a4bfc4feSMatthias Ringwald     }
237c9d24807SMilanka Ringwald 
2380235c9e5SMilanka Ringwald     if (att_handle == instance->hid_control_point_value_handle){
2394ea43905SMatthias Ringwald         if (buffer && (buffer_size >= 1u)){
2400235c9e5SMilanka Ringwald             buffer[0] = instance->hid_control_point_suspend;
2410235c9e5SMilanka Ringwald         }
2420235c9e5SMilanka Ringwald         return 1;
243a4bfc4feSMatthias Ringwald     }
244f4f6c196SMilanka Ringwald 
245*1b9864eaSMatthias Ringwald     uint8_t boot_report_size = 0;
246*1b9864eaSMatthias Ringwald     if (att_handle == instance->hid_boot_mouse_input_value_handle){
247*1b9864eaSMatthias Ringwald         boot_report_size = 3;
248*1b9864eaSMatthias Ringwald     }
249*1b9864eaSMatthias Ringwald     if (att_handle == instance->hid_boot_keyboard_input_value_handle){
250*1b9864eaSMatthias Ringwald         boot_report_size = 8;
251*1b9864eaSMatthias Ringwald     }
252*1b9864eaSMatthias Ringwald     if (boot_report_size != 0){
253*1b9864eaSMatthias Ringwald         // no callback, no report
254*1b9864eaSMatthias Ringwald         if (hids_device_get_report_callback == NULL){
255*1b9864eaSMatthias Ringwald             return 0;
256*1b9864eaSMatthias Ringwald         }
257*1b9864eaSMatthias Ringwald         // answer length request by ATT Server
258*1b9864eaSMatthias Ringwald         if (buffer == NULL){
259*1b9864eaSMatthias Ringwald             return boot_report_size;
260*1b9864eaSMatthias Ringwald         } else {
261*1b9864eaSMatthias Ringwald             // Report ID 0, Type Input
262*1b9864eaSMatthias Ringwald             (*hids_device_get_report_callback)(con_handle, HID_REPORT_TYPE_INPUT, 0, boot_report_size, buffer);
263*1b9864eaSMatthias Ringwald             return boot_report_size;
264*1b9864eaSMatthias Ringwald         }
265*1b9864eaSMatthias Ringwald     }
266*1b9864eaSMatthias Ringwald 
267*1b9864eaSMatthias Ringwald     hids_device_report_t * report;
268*1b9864eaSMatthias Ringwald     report = hids_device_get_report_for_value_handle(instance, att_handle);
269*1b9864eaSMatthias Ringwald     if (report != NULL){
270*1b9864eaSMatthias Ringwald         // no callback, no report
271*1b9864eaSMatthias Ringwald         if (hids_device_get_report_callback == NULL){
272*1b9864eaSMatthias Ringwald             return 0;
273*1b9864eaSMatthias Ringwald         }
274*1b9864eaSMatthias Ringwald         // answer length request by ATT Server
275*1b9864eaSMatthias Ringwald         if (buffer == NULL){
276*1b9864eaSMatthias Ringwald             return report->size;
277*1b9864eaSMatthias Ringwald         } else {
278*1b9864eaSMatthias Ringwald             uint16_t max_size = btstack_min(report->size, buffer_size);
279*1b9864eaSMatthias Ringwald             (*hids_device_get_report_callback)(con_handle, report->type, report->id, max_size, buffer);
280*1b9864eaSMatthias Ringwald             return report->size;
281*1b9864eaSMatthias Ringwald         }
282*1b9864eaSMatthias Ringwald     }
283*1b9864eaSMatthias Ringwald 
284*1b9864eaSMatthias Ringwald     report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
285f4f6c196SMilanka Ringwald     if (report != NULL){
286f4f6c196SMilanka Ringwald         return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size);
287f4f6c196SMilanka Ringwald     }
288a4bfc4feSMatthias Ringwald     return 0;
289a4bfc4feSMatthias Ringwald }
290a4bfc4feSMatthias Ringwald 
291a4bfc4feSMatthias Ringwald 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){
292a4bfc4feSMatthias Ringwald     UNUSED(buffer_size);
293a4bfc4feSMatthias Ringwald     UNUSED(offset);
294a4bfc4feSMatthias Ringwald 
295689e7fb3SMatthias Ringwald     if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
296689e7fb3SMatthias Ringwald         return 0;
297689e7fb3SMatthias Ringwald     }
298689e7fb3SMatthias Ringwald 
2990235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
3000235c9e5SMilanka Ringwald     if (!instance){
3010235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
3020235c9e5SMilanka Ringwald         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
3030235c9e5SMilanka Ringwald     }
3040235c9e5SMilanka Ringwald 
3050235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
30685828295SMatthias Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
3070235c9e5SMilanka Ringwald         instance->hid_boot_mouse_input_client_configuration_value = new_value;
308c1dbba9dSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
309a4bfc4feSMatthias Ringwald     }
3100235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
31185828295SMatthias Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
3120235c9e5SMilanka Ringwald         instance->hid_boot_keyboard_input_client_configuration_value = new_value;
313c1dbba9dSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
314a4bfc4feSMatthias Ringwald     }
3150235c9e5SMilanka Ringwald 
3160235c9e5SMilanka Ringwald     if (att_handle == instance->hid_protocol_mode_value_handle){
3170235c9e5SMilanka Ringwald         instance->hid_protocol_mode = buffer[0];
3180235c9e5SMilanka Ringwald         log_info("Set protocol mode: %u", instance->hid_protocol_mode);
3190235c9e5SMilanka Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
3200235c9e5SMilanka Ringwald     }
3210235c9e5SMilanka Ringwald 
3220235c9e5SMilanka Ringwald     if (att_handle == instance->hid_control_point_value_handle){
3234ea43905SMatthias Ringwald         if (buffer_size < 1u){
3240235c9e5SMilanka Ringwald             return ATT_ERROR_INVALID_OFFSET;
3250235c9e5SMilanka Ringwald         }
3260235c9e5SMilanka Ringwald         instance->hid_control_point_suspend = buffer[0];
3270235c9e5SMilanka Ringwald         instance->con_handle = con_handle;
3280235c9e5SMilanka Ringwald         log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
3294ea43905SMatthias Ringwald         if (instance->hid_control_point_suspend == 0u){
3300235c9e5SMilanka Ringwald             hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
3314ea43905SMatthias Ringwald         } else if (instance->hid_control_point_suspend == 1u){
3320235c9e5SMilanka Ringwald             hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
3330235c9e5SMilanka Ringwald         }
3340235c9e5SMilanka Ringwald     }
335f4f6c196SMilanka Ringwald 
336aa3c9a66SMatthias Ringwald     hids_device_report_t * report;
337aa3c9a66SMatthias Ringwald     report = hids_device_get_report_for_value_handle(instance, att_handle);
338aa3c9a66SMatthias Ringwald     if (report != NULL){
339aa3c9a66SMatthias Ringwald         // assemble event in buffer
340aa3c9a66SMatthias Ringwald         uint8_t event[257];
341aa3c9a66SMatthias Ringwald         uint16_t pos = 0;
342aa3c9a66SMatthias Ringwald         event[pos++] = HCI_EVENT_HIDS_META;
343aa3c9a66SMatthias Ringwald         // skip length
344aa3c9a66SMatthias Ringwald         pos++;
345aa3c9a66SMatthias Ringwald         event[pos++] = HIDS_SUBEVENT_SET_REPORT;
346aa3c9a66SMatthias Ringwald         little_endian_store_16(event, pos, con_handle);
347aa3c9a66SMatthias Ringwald         pos += 2;
348aa3c9a66SMatthias Ringwald         event[pos++] = report->id;
349aa3c9a66SMatthias Ringwald         event[pos++] = (uint8_t) report->type;
350aa3c9a66SMatthias Ringwald         uint8_t length_to_copy = btstack_min(buffer_size, 250);
351aa3c9a66SMatthias Ringwald         event[pos++] = length_to_copy;
352aa3c9a66SMatthias Ringwald         memcpy(&event[pos], buffer, length_to_copy);
353aa3c9a66SMatthias Ringwald         pos += length_to_copy;
354aa3c9a66SMatthias Ringwald         // set event length
355aa3c9a66SMatthias Ringwald         event[1] = pos - 2;
356aa3c9a66SMatthias Ringwald         (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
357aa3c9a66SMatthias Ringwald         return 0;
358aa3c9a66SMatthias Ringwald     }
359aa3c9a66SMatthias Ringwald 
360aa3c9a66SMatthias Ringwald     report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
361f4f6c196SMilanka Ringwald     if (report != NULL){
362f4f6c196SMilanka Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
363f4f6c196SMilanka Ringwald         report->client_configuration_value = new_value;
364f4f6c196SMilanka Ringwald         log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value);
365f4f6c196SMilanka Ringwald 
366f4f6c196SMilanka Ringwald         switch (report->type){
367f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_INPUT:
368f00767dfSMatthias Ringwald                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
369f4f6c196SMilanka Ringwald                 break;
370f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_OUTPUT:
371f00767dfSMatthias Ringwald                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
372f4f6c196SMilanka Ringwald                 break;
373f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_FEATURE:
374f00767dfSMatthias Ringwald                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
375f4f6c196SMilanka Ringwald                 break;
376f4f6c196SMilanka Ringwald             default:
377f4f6c196SMilanka Ringwald                 btstack_unreachable();
378f4f6c196SMilanka Ringwald                 break;
379f4f6c196SMilanka Ringwald         }
380f4f6c196SMilanka Ringwald     }
381a4bfc4feSMatthias Ringwald     return 0;
382a4bfc4feSMatthias Ringwald }
383a4bfc4feSMatthias Ringwald 
384f823745aSMilanka Ringwald void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size,
385f823745aSMilanka Ringwald     uint16_t num_reports, hids_device_report_t * report_storage){
386f823745aSMilanka Ringwald 
3870235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_create_instance();
388a4bfc4feSMatthias Ringwald 
389f823745aSMilanka Ringwald     btstack_assert(num_reports > 0);
390f823745aSMilanka Ringwald     btstack_assert(report_storage != NULL);
391f823745aSMilanka Ringwald 
392f823745aSMilanka Ringwald     instance->hid_country_code    = hid_country_code;
393f823745aSMilanka Ringwald     instance->hid_descriptor      = hid_descriptor;
394f823745aSMilanka Ringwald     instance->hid_descriptor_size = hid_descriptor_size;
395a4bfc4feSMatthias Ringwald 
396a4bfc4feSMatthias Ringwald     // default
3970235c9e5SMilanka Ringwald     instance->hid_protocol_mode   = 1;
398a4bfc4feSMatthias Ringwald 
399a4bfc4feSMatthias Ringwald     // get service handle range
400a4bfc4feSMatthias Ringwald     uint16_t start_handle = 0;
401843359edSMatthias Ringwald     uint16_t end_handle   = 0xffff;
402c436b760SMilanka Ringwald     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
403a2489f29SMatthias Ringwald     btstack_assert(service_found != 0);
404a2489f29SMatthias Ringwald     UNUSED(service_found);
405a4bfc4feSMatthias Ringwald 
406a4bfc4feSMatthias Ringwald     // get report map handle
4070235c9e5SMilanka Ringwald     instance->hid_report_map_handle                               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
408a4bfc4feSMatthias Ringwald 
409a4bfc4feSMatthias Ringwald     // get report map handle
4100235c9e5SMilanka Ringwald     instance->hid_protocol_mode_value_handle                      = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE);
411a4bfc4feSMatthias Ringwald 
412a4bfc4feSMatthias Ringwald     // get value and client configuration handles for boot mouse input, boot keyboard input and report input
4130235c9e5SMilanka Ringwald     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);
4140235c9e5SMilanka Ringwald     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);
415a4bfc4feSMatthias Ringwald 
4160235c9e5SMilanka Ringwald     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);
4170235c9e5SMilanka Ringwald     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);
418a4bfc4feSMatthias Ringwald 
4190235c9e5SMilanka Ringwald     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);
4200235c9e5SMilanka Ringwald 
421c9d24807SMilanka Ringwald     log_info("hid_report_map_handle                               0x%02x", instance->hid_report_map_handle);
422c9d24807SMilanka Ringwald     log_info("hid_protocol_mode_value_handle                      0x%02x", instance->hid_protocol_mode_value_handle);
423c9d24807SMilanka Ringwald     log_info("hid_boot_mouse_input_value_handle                   0x%02x", instance->hid_boot_mouse_input_value_handle);
424c9d24807SMilanka Ringwald     log_info("hid_boot_mouse_input_client_configuration_handle    0x%02x", instance->hid_boot_mouse_input_client_configuration_handle);
425c9d24807SMilanka Ringwald     log_info("hid_boot_keyboard_input_value_handle                0x%02x", instance->hid_boot_keyboard_input_value_handle);
426c9d24807SMilanka Ringwald     log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle);
427c9d24807SMilanka Ringwald     log_info("hid_control_point_value_handle                      0x%02x", instance->hid_control_point_value_handle);
428a4bfc4feSMatthias Ringwald 
429f823745aSMilanka Ringwald     instance->hid_reports = report_storage;
430f4f6c196SMilanka Ringwald 
431f823745aSMilanka Ringwald     uint16_t hid_reports_num = num_reports;
432f4f6c196SMilanka Ringwald     uint16_t assigned_reports_num = 0;
433f4f6c196SMilanka Ringwald     uint16_t start_chr_handle = start_handle;
434f4f6c196SMilanka Ringwald 
435f4f6c196SMilanka Ringwald     while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) {
4362c84eaa3SMatthias Ringwald         // mandatory
437f4f6c196SMilanka Ringwald         uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
438f4f6c196SMilanka Ringwald         if (chr_value_handle == 0){
439f4f6c196SMilanka Ringwald             break;
440f4f6c196SMilanka Ringwald         }
441f4f6c196SMilanka Ringwald 
4422c84eaa3SMatthias Ringwald         // optional
443f4f6c196SMilanka Ringwald         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);
4442c84eaa3SMatthias Ringwald 
4452c84eaa3SMatthias Ringwald         // mandatory
446f4f6c196SMilanka Ringwald         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);
4472c84eaa3SMatthias Ringwald         if (report_reference_handle == 0){
4482c84eaa3SMatthias Ringwald             break;
4492c84eaa3SMatthias Ringwald         }
4502c84eaa3SMatthias Ringwald 
4512c84eaa3SMatthias Ringwald         // get report id and type from report reference
452f4f6c196SMilanka Ringwald         uint16_t report_reference_value_len;
453f4f6c196SMilanka Ringwald         const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len);
454f4f6c196SMilanka Ringwald         if (report_reference_value == NULL){
455f4f6c196SMilanka Ringwald             break;
456f4f6c196SMilanka Ringwald         }
457f4f6c196SMilanka Ringwald         if (report_reference_value_len != 2){
458f4f6c196SMilanka Ringwald             break;
459f4f6c196SMilanka Ringwald         }
4602c84eaa3SMatthias Ringwald         uint8_t report_id = report_reference_value[0];
4612c84eaa3SMatthias Ringwald         hid_report_type_t report_type = (hid_report_type_t) report_reference_value[1];
462f4f6c196SMilanka Ringwald 
4632c84eaa3SMatthias Ringwald         // store report info
464160eac51SMatthias Ringwald         hids_device_report_t * report = &report_storage[assigned_reports_num];
465f4f6c196SMilanka Ringwald         report->value_handle = chr_value_handle;
466f4f6c196SMilanka Ringwald         report->client_configuration_handle = chr_client_configuration_handle;
467f4f6c196SMilanka Ringwald         report->client_configuration_value = 0;
4682c84eaa3SMatthias Ringwald         report->id   = report_id;
4692c84eaa3SMatthias Ringwald         report->type = report_type;
470*1b9864eaSMatthias Ringwald         report->size = btstack_hid_get_report_size_for_id(report_id, report_type, hid_descriptor_size, hid_descriptor);
471f4f6c196SMilanka Ringwald 
472f4f6c196SMilanka Ringwald         switch (report->type){
473f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_INPUT:
474f4f6c196SMilanka Ringwald                 instance->hid_input_reports_num++;
475f4f6c196SMilanka Ringwald                 break;
476f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_OUTPUT:
477f4f6c196SMilanka Ringwald                 instance->hid_output_reports_num++;
478f4f6c196SMilanka Ringwald                 break;
479f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_FEATURE:
480f4f6c196SMilanka Ringwald                 instance->hid_feature_reports_num++;
481f4f6c196SMilanka Ringwald                 break;
482f4f6c196SMilanka Ringwald             default:
483f4f6c196SMilanka Ringwald                 btstack_unreachable();
484f4f6c196SMilanka Ringwald                 return;
485f4f6c196SMilanka Ringwald         }
486f4f6c196SMilanka Ringwald         log_info("hid_report_value_handle                       0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type);
4872c84eaa3SMatthias Ringwald         if (report->client_configuration_handle != 0){
488f4f6c196SMilanka Ringwald             log_info("hid_report_client_configuration_handle        0x%02x", report->client_configuration_handle);
4892c84eaa3SMatthias Ringwald         }
490f4f6c196SMilanka Ringwald 
491f4f6c196SMilanka Ringwald         assigned_reports_num++;
4922c84eaa3SMatthias Ringwald         start_chr_handle = report_reference_handle + 1;
493f4f6c196SMilanka Ringwald     }
494f4f6c196SMilanka Ringwald 
4953d71c7a4SMatthias Ringwald     // register service with ATT Server
496a4bfc4feSMatthias Ringwald     hid_service.start_handle   = start_handle;
497a4bfc4feSMatthias Ringwald     hid_service.end_handle     = end_handle;
498a4bfc4feSMatthias Ringwald     hid_service.read_callback  = &att_read_callback;
499a4bfc4feSMatthias Ringwald     hid_service.write_callback = &att_write_callback;
5003d71c7a4SMatthias Ringwald     att_server_register_service_handler(&hid_service);
501a4bfc4feSMatthias Ringwald }
502a4bfc4feSMatthias Ringwald 
503a4bfc4feSMatthias Ringwald /**
504f823745aSMilanka Ringwald  * @brief Set up HIDS Device
505f823745aSMilanka Ringwald  */
506f823745aSMilanka Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
507160eac51SMatthias Ringwald     uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t);
508160eac51SMatthias Ringwald     hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage);
509f823745aSMilanka Ringwald }
510f823745aSMilanka Ringwald 
511f823745aSMilanka Ringwald /**
512a4bfc4feSMatthias Ringwald  * @brief Register callback for the HIDS Device client.
513a4bfc4feSMatthias Ringwald  * @param callback
514a4bfc4feSMatthias Ringwald  */
515a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){
516a4bfc4feSMatthias Ringwald     packet_handler = callback;
517a4bfc4feSMatthias Ringwald }
518a4bfc4feSMatthias Ringwald 
519*1b9864eaSMatthias Ringwald 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*1b9864eaSMatthias Ringwald     hids_device_get_report_callback = callback;
521*1b9864eaSMatthias Ringwald }
522*1b9864eaSMatthias Ringwald 
523a4bfc4feSMatthias Ringwald /**
524a4bfc4feSMatthias Ringwald  * @brief Request can send now event to send HID Report
525a4bfc4feSMatthias Ringwald  * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
526a4bfc4feSMatthias Ringwald  * @param hid_cid
527a4bfc4feSMatthias Ringwald  */
528a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
5290235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
5300235c9e5SMilanka Ringwald     if (!instance){
5310235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
5320235c9e5SMilanka Ringwald         return;
5330235c9e5SMilanka Ringwald     }
5340235c9e5SMilanka Ringwald 
535086ea75fSMatthias Ringwald     instance->can_send_now_callback.callback = &hids_device_can_send_now;
536086ea75fSMatthias Ringwald     instance->can_send_now_callback.context  = (void*) (uintptr_t) con_handle;
537086ea75fSMatthias Ringwald     att_server_register_can_send_now_callback(&instance->can_send_now_callback, con_handle);
538a4bfc4feSMatthias Ringwald }
539a4bfc4feSMatthias Ringwald 
54045569c34SMatthias Ringwald 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){
5410235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
5420235c9e5SMilanka Ringwald     if (!instance){
5430235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
544f4f6c196SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
5450235c9e5SMilanka Ringwald     }
546f4f6c196SMilanka Ringwald 
547a3c1c955SMatthias Ringwald     hids_device_report_t * report_storage = hids_device_get_report_for_id_and_type(instance, report_id,
548a3c1c955SMatthias Ringwald                                                                                    HID_REPORT_TYPE_INPUT);
549f4f6c196SMilanka Ringwald     if (report_storage == NULL){
550f4f6c196SMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
551f4f6c196SMilanka Ringwald     }
552f4f6c196SMilanka Ringwald 
553f4f6c196SMilanka Ringwald     return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
554f4f6c196SMilanka Ringwald }
555f4f6c196SMilanka Ringwald 
556d38dfb7fSMatthias Ringwald uint8_t hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
557a90492ddSMatthias Ringwald     hids_device_t * device = hids_device_get_instance_for_con_handle(con_handle);
558a90492ddSMatthias Ringwald     if (!device){
559f4f6c196SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
560f4f6c196SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
561f4f6c196SMilanka Ringwald     }
562f4f6c196SMilanka Ringwald 
563a90492ddSMatthias Ringwald     uint8_t pos;
564a90492ddSMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
565a90492ddSMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
566a90492ddSMatthias Ringwald         hids_device_report_t * report_storage = &device->hid_reports[pos];
567d38dfb7fSMatthias Ringwald         if (report_storage->type == HID_REPORT_TYPE_INPUT){
568f4f6c196SMilanka Ringwald             return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
569f4f6c196SMilanka Ringwald         }
570f4f6c196SMilanka Ringwald     }
571f4f6c196SMilanka Ringwald     return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
572f4f6c196SMilanka Ringwald }
573f4f6c196SMilanka Ringwald 
574d38dfb7fSMatthias Ringwald /**
575d38dfb7fSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
576d38dfb7fSMatthias Ringwald  */
577d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
578d38dfb7fSMatthias Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
579d38dfb7fSMatthias Ringwald     if (!instance){
580d38dfb7fSMatthias Ringwald         log_error("no instance for handle 0x%02x", con_handle);
581d38dfb7fSMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
582d38dfb7fSMatthias Ringwald     }
583d38dfb7fSMatthias Ringwald     return att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
5840235c9e5SMilanka Ringwald }
5850235c9e5SMilanka Ringwald 
5860235c9e5SMilanka Ringwald /**
587a4bfc4feSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
588a4bfc4feSMatthias Ringwald  */
589d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
5900235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
5910235c9e5SMilanka Ringwald     if (!instance){
5920235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
593d38dfb7fSMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
5940235c9e5SMilanka Ringwald     }
595d38dfb7fSMatthias Ringwald     return att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
596a4bfc4feSMatthias Ringwald }
597