xref: /btstack/src/ble/gatt-service/hids_device.c (revision bbf45f896fd139c6f30b084f4679e6de4ef829d8)
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"
521b9864eaSMatthias 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 
78*bbf45f89SMilanka Ringwald     uint16_t        hid_boot_keyboard_output_value_handle;
79*bbf45f89SMilanka Ringwald 
80f4f6c196SMilanka Ringwald     hids_device_report_t * hid_reports;
810235c9e5SMilanka Ringwald 
82f4f6c196SMilanka Ringwald     uint8_t         hid_input_reports_num;
83f4f6c196SMilanka Ringwald     uint8_t         hid_output_reports_num;
84f4f6c196SMilanka Ringwald     uint8_t         hid_feature_reports_num;
850235c9e5SMilanka Ringwald 
860235c9e5SMilanka Ringwald     uint16_t        hid_control_point_value_handle;
870235c9e5SMilanka Ringwald     uint8_t         hid_control_point_suspend;
880235c9e5SMilanka Ringwald 
89086ea75fSMatthias Ringwald     btstack_context_callback_registration_t  can_send_now_callback;
900235c9e5SMilanka Ringwald } hids_device_t;
910235c9e5SMilanka Ringwald 
920235c9e5SMilanka Ringwald static hids_device_t hids_device;
930235c9e5SMilanka Ringwald 
94a4bfc4feSMatthias Ringwald static btstack_packet_handler_t packet_handler;
95a4bfc4feSMatthias Ringwald static att_service_handler_t hid_service;
961b9864eaSMatthias 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);
97a4bfc4feSMatthias Ringwald 
980235c9e5SMilanka Ringwald // TODO: store hids device connection into list
990235c9e5SMilanka Ringwald static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
1000235c9e5SMilanka Ringwald     UNUSED(con_handle);
1010235c9e5SMilanka Ringwald     return &hids_device;
1020235c9e5SMilanka Ringwald }
1030235c9e5SMilanka Ringwald 
1040235c9e5SMilanka Ringwald static hids_device_t * hids_device_create_instance(void){
105f4f6c196SMilanka Ringwald     memset(&hids_device, 0, sizeof(hids_device_t));
1060235c9e5SMilanka Ringwald     return &hids_device;
1070235c9e5SMilanka Ringwald }
1080235c9e5SMilanka Ringwald 
1098af7e88bSMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_value_handle(hids_device_t * device, uint16_t value_handle){
110aa3c9a66SMatthias Ringwald     uint8_t pos;
111aa3c9a66SMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
112aa3c9a66SMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
1138af7e88bSMilanka Ringwald         if (device->hid_reports[pos].value_handle == value_handle){
114aa3c9a66SMatthias Ringwald             return &device->hid_reports[pos];
115aa3c9a66SMatthias Ringwald         }
116aa3c9a66SMatthias Ringwald     }
117aa3c9a66SMatthias Ringwald     return NULL;
118aa3c9a66SMatthias Ringwald }
119aa3c9a66SMatthias Ringwald 
120f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){
121d75a985bSMatthias Ringwald     uint8_t pos;
122d75a985bSMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
123d75a985bSMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
124f4f6c196SMilanka Ringwald         if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){
125f4f6c196SMilanka Ringwald             return &device->hid_reports[pos];
126f4f6c196SMilanka Ringwald         }
127f4f6c196SMilanka Ringwald     }
128f4f6c196SMilanka Ringwald     return NULL;
129f4f6c196SMilanka Ringwald }
130f4f6c196SMilanka Ringwald 
131a3c1c955SMatthias Ringwald static hids_device_report_t *
132a3c1c955SMatthias Ringwald hids_device_get_report_for_id_and_type(hids_device_t *device, uint16_t report_id, hid_report_type_t type) {
13345569c34SMatthias Ringwald     uint8_t pos;
13445569c34SMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
13545569c34SMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
13645569c34SMatthias Ringwald         if ((device->hid_reports[pos].type == type) && (device->hid_reports[pos].id == report_id)){
13745569c34SMatthias Ringwald             return &device->hid_reports[pos];
13845569c34SMatthias Ringwald         }
13945569c34SMatthias Ringwald     }
14045569c34SMatthias Ringwald     return NULL;
14145569c34SMatthias Ringwald }
14245569c34SMatthias Ringwald 
143a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
1440235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1450235c9e5SMilanka Ringwald     if (!instance){
1460235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1470235c9e5SMilanka Ringwald         return;
1480235c9e5SMilanka Ringwald     }
1490235c9e5SMilanka Ringwald 
150a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
151a4bfc4feSMatthias Ringwald     uint8_t buffer[6];
152a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
153a4bfc4feSMatthias Ringwald     buffer[1] = 4;
154a4bfc4feSMatthias Ringwald     buffer[2] = event;
155a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
156a4bfc4feSMatthias Ringwald     buffer[5] = value;
157a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
158a4bfc4feSMatthias Ringwald }
159a4bfc4feSMatthias Ringwald 
160f00767dfSMatthias 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){
161f00767dfSMatthias Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
162f00767dfSMatthias Ringwald     if (!instance){
163f00767dfSMatthias Ringwald         log_error("no instance for handle 0x%02x", con_handle);
164f00767dfSMatthias Ringwald         return;
165f00767dfSMatthias Ringwald     }
166f00767dfSMatthias Ringwald 
167f00767dfSMatthias Ringwald     if (!packet_handler) return;
168f00767dfSMatthias Ringwald     uint8_t buffer[7];
169f00767dfSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
170f00767dfSMatthias Ringwald     buffer[1] = 4;
171f00767dfSMatthias Ringwald     buffer[2] = event;
172f00767dfSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
173f00767dfSMatthias Ringwald     buffer[5] = value_1;
174f00767dfSMatthias Ringwald     buffer[6] = value_2;
175f00767dfSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
176f00767dfSMatthias Ringwald }
177f00767dfSMatthias Ringwald 
1780235c9e5SMilanka Ringwald static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
1790235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1800235c9e5SMilanka Ringwald     if (!instance){
1810235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1820235c9e5SMilanka Ringwald         return;
1830235c9e5SMilanka Ringwald     }
1840235c9e5SMilanka Ringwald 
1850235c9e5SMilanka Ringwald     if (!packet_handler) return;
1860235c9e5SMilanka Ringwald     uint8_t buffer[5];
1870235c9e5SMilanka Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
1880235c9e5SMilanka Ringwald     buffer[1] = 4;
1890235c9e5SMilanka Ringwald     buffer[2] = event;
1900235c9e5SMilanka Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
1910235c9e5SMilanka Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
1920235c9e5SMilanka Ringwald }
1930235c9e5SMilanka Ringwald 
194a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){
195a4bfc4feSMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
196a4bfc4feSMatthias Ringwald     // notify client
1970235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1980235c9e5SMilanka Ringwald     if (!instance){
1990235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
2000235c9e5SMilanka Ringwald         return;
2010235c9e5SMilanka Ringwald     }
2020235c9e5SMilanka Ringwald 
203a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
204a4bfc4feSMatthias Ringwald     uint8_t buffer[5];
205a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
206a4bfc4feSMatthias Ringwald     buffer[1] = 3;
207a4bfc4feSMatthias Ringwald     buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
208a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
209a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
210a4bfc4feSMatthias Ringwald }
211a4bfc4feSMatthias Ringwald 
212a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data
213a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
214a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
2150235c9e5SMilanka 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){
2160235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
2170235c9e5SMilanka Ringwald     if (!instance){
2180235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
2190235c9e5SMilanka Ringwald         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
220a4bfc4feSMatthias Ringwald     }
2210235c9e5SMilanka Ringwald 
2220235c9e5SMilanka Ringwald     if (att_handle == instance->hid_protocol_mode_value_handle){
2230235c9e5SMilanka Ringwald         log_info("Read protocol mode");
2240235c9e5SMilanka Ringwald         return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
2250235c9e5SMilanka Ringwald     }
226c9d24807SMilanka Ringwald 
2270235c9e5SMilanka Ringwald     if (att_handle == instance->hid_report_map_handle){
228523edd21SMatthias Ringwald         log_info("Read report map");
2290235c9e5SMilanka Ringwald         return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
230a4bfc4feSMatthias Ringwald     }
231c9d24807SMilanka Ringwald 
2320235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
2330235c9e5SMilanka Ringwald         return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
234a4bfc4feSMatthias Ringwald     }
235c9d24807SMilanka Ringwald 
2360235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
2370235c9e5SMilanka Ringwald         return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
238a4bfc4feSMatthias Ringwald     }
239c9d24807SMilanka Ringwald 
2400235c9e5SMilanka Ringwald     if (att_handle == instance->hid_control_point_value_handle){
2414ea43905SMatthias Ringwald         if (buffer && (buffer_size >= 1u)){
2420235c9e5SMilanka Ringwald             buffer[0] = instance->hid_control_point_suspend;
2430235c9e5SMilanka Ringwald         }
2440235c9e5SMilanka Ringwald         return 1;
245a4bfc4feSMatthias Ringwald     }
246f4f6c196SMilanka Ringwald 
247*bbf45f89SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_output_value_handle){
248*bbf45f89SMilanka Ringwald         // TODO
249*bbf45f89SMilanka Ringwald         return 0;
250*bbf45f89SMilanka Ringwald     }
251*bbf45f89SMilanka Ringwald 
2521b9864eaSMatthias Ringwald     uint8_t boot_report_size = 0;
2531b9864eaSMatthias Ringwald     if (att_handle == instance->hid_boot_mouse_input_value_handle){
2541b9864eaSMatthias Ringwald         boot_report_size = 3;
2551b9864eaSMatthias Ringwald     }
2561b9864eaSMatthias Ringwald     if (att_handle == instance->hid_boot_keyboard_input_value_handle){
2571b9864eaSMatthias Ringwald         boot_report_size = 8;
2581b9864eaSMatthias Ringwald     }
2591b9864eaSMatthias Ringwald     if (boot_report_size != 0){
2601b9864eaSMatthias Ringwald         // no callback, no report
2611b9864eaSMatthias Ringwald         if (hids_device_get_report_callback == NULL){
2621b9864eaSMatthias Ringwald             return 0;
2631b9864eaSMatthias Ringwald         }
2641b9864eaSMatthias Ringwald         // answer length request by ATT Server
2651b9864eaSMatthias Ringwald         if (buffer == NULL){
2661b9864eaSMatthias Ringwald             return boot_report_size;
2671b9864eaSMatthias Ringwald         } else {
2681b9864eaSMatthias Ringwald             // Report ID 0, Type Input
2691b9864eaSMatthias Ringwald             (*hids_device_get_report_callback)(con_handle, HID_REPORT_TYPE_INPUT, 0, boot_report_size, buffer);
2701b9864eaSMatthias Ringwald             return boot_report_size;
2711b9864eaSMatthias Ringwald         }
2721b9864eaSMatthias Ringwald     }
2731b9864eaSMatthias Ringwald 
2741b9864eaSMatthias Ringwald     hids_device_report_t * report;
2751b9864eaSMatthias Ringwald     report = hids_device_get_report_for_value_handle(instance, att_handle);
2761b9864eaSMatthias Ringwald     if (report != NULL){
2771b9864eaSMatthias Ringwald         // no callback, no report
2781b9864eaSMatthias Ringwald         if (hids_device_get_report_callback == NULL){
2791b9864eaSMatthias Ringwald             return 0;
2801b9864eaSMatthias Ringwald         }
2811b9864eaSMatthias Ringwald         // answer length request by ATT Server
2821b9864eaSMatthias Ringwald         if (buffer == NULL){
2831b9864eaSMatthias Ringwald             return report->size;
2841b9864eaSMatthias Ringwald         } else {
2851b9864eaSMatthias Ringwald             uint16_t max_size = btstack_min(report->size, buffer_size);
2861b9864eaSMatthias Ringwald             (*hids_device_get_report_callback)(con_handle, report->type, report->id, max_size, buffer);
2871b9864eaSMatthias Ringwald             return report->size;
2881b9864eaSMatthias Ringwald         }
2891b9864eaSMatthias Ringwald     }
2901b9864eaSMatthias Ringwald 
2911b9864eaSMatthias Ringwald     report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
292f4f6c196SMilanka Ringwald     if (report != NULL){
293f4f6c196SMilanka Ringwald         return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size);
294f4f6c196SMilanka Ringwald     }
295a4bfc4feSMatthias Ringwald     return 0;
296a4bfc4feSMatthias Ringwald }
297a4bfc4feSMatthias Ringwald 
298a4bfc4feSMatthias 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){
299a4bfc4feSMatthias Ringwald     UNUSED(buffer_size);
300a4bfc4feSMatthias Ringwald     UNUSED(offset);
301a4bfc4feSMatthias Ringwald 
302689e7fb3SMatthias Ringwald     if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
303689e7fb3SMatthias Ringwald         return 0;
304689e7fb3SMatthias Ringwald     }
305689e7fb3SMatthias Ringwald 
3060235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
3070235c9e5SMilanka Ringwald     if (!instance){
3080235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
3090235c9e5SMilanka Ringwald         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
3100235c9e5SMilanka Ringwald     }
3110235c9e5SMilanka Ringwald 
3120235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
31385828295SMatthias Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
3140235c9e5SMilanka Ringwald         instance->hid_boot_mouse_input_client_configuration_value = new_value;
315c1dbba9dSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
316b3b57560SMilanka Ringwald         return 0;
317a4bfc4feSMatthias Ringwald     }
3180235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
31985828295SMatthias Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
3200235c9e5SMilanka Ringwald         instance->hid_boot_keyboard_input_client_configuration_value = new_value;
321c1dbba9dSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
322b3b57560SMilanka Ringwald         return 0;
323a4bfc4feSMatthias Ringwald     }
3240235c9e5SMilanka Ringwald 
3250235c9e5SMilanka Ringwald     if (att_handle == instance->hid_protocol_mode_value_handle){
3260235c9e5SMilanka Ringwald         instance->hid_protocol_mode = buffer[0];
3270235c9e5SMilanka Ringwald         log_info("Set protocol mode: %u", instance->hid_protocol_mode);
3280235c9e5SMilanka Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
329b3b57560SMilanka Ringwald         return 0;
3300235c9e5SMilanka Ringwald     }
3310235c9e5SMilanka Ringwald 
3320235c9e5SMilanka Ringwald     if (att_handle == instance->hid_control_point_value_handle){
3334ea43905SMatthias Ringwald         if (buffer_size < 1u){
3340235c9e5SMilanka Ringwald             return ATT_ERROR_INVALID_OFFSET;
3350235c9e5SMilanka Ringwald         }
3360235c9e5SMilanka Ringwald         instance->hid_control_point_suspend = buffer[0];
3370235c9e5SMilanka Ringwald         instance->con_handle = con_handle;
3380235c9e5SMilanka Ringwald         log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
3394ea43905SMatthias Ringwald         if (instance->hid_control_point_suspend == 0u){
3400235c9e5SMilanka Ringwald             hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
3414ea43905SMatthias Ringwald         } else if (instance->hid_control_point_suspend == 1u){
3420235c9e5SMilanka Ringwald             hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
3430235c9e5SMilanka Ringwald         }
344b3b57560SMilanka Ringwald         return 0;
345b3b57560SMilanka Ringwald     }
346*bbf45f89SMilanka Ringwald 
347*bbf45f89SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_output_value_handle){
348*bbf45f89SMilanka Ringwald         // TODO
349*bbf45f89SMilanka Ringwald         return 0;
3500235c9e5SMilanka Ringwald     }
351f4f6c196SMilanka Ringwald 
352aa3c9a66SMatthias Ringwald     hids_device_report_t * report;
353aa3c9a66SMatthias Ringwald     report = hids_device_get_report_for_value_handle(instance, att_handle);
354aa3c9a66SMatthias Ringwald     if (report != NULL){
355aa3c9a66SMatthias Ringwald         // assemble event in buffer
356aa3c9a66SMatthias Ringwald         uint8_t event[257];
357aa3c9a66SMatthias Ringwald         uint16_t pos = 0;
358aa3c9a66SMatthias Ringwald         event[pos++] = HCI_EVENT_HIDS_META;
359aa3c9a66SMatthias Ringwald         // skip length
360aa3c9a66SMatthias Ringwald         pos++;
361aa3c9a66SMatthias Ringwald         event[pos++] = HIDS_SUBEVENT_SET_REPORT;
362aa3c9a66SMatthias Ringwald         little_endian_store_16(event, pos, con_handle);
363aa3c9a66SMatthias Ringwald         pos += 2;
364aa3c9a66SMatthias Ringwald         event[pos++] = report->id;
365aa3c9a66SMatthias Ringwald         event[pos++] = (uint8_t) report->type;
366aa3c9a66SMatthias Ringwald         uint8_t length_to_copy = btstack_min(buffer_size, 250);
367aa3c9a66SMatthias Ringwald         event[pos++] = length_to_copy;
368aa3c9a66SMatthias Ringwald         memcpy(&event[pos], buffer, length_to_copy);
369aa3c9a66SMatthias Ringwald         pos += length_to_copy;
370aa3c9a66SMatthias Ringwald         // set event length
371aa3c9a66SMatthias Ringwald         event[1] = pos - 2;
372aa3c9a66SMatthias Ringwald         (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
373aa3c9a66SMatthias Ringwald         return 0;
374aa3c9a66SMatthias Ringwald     }
375aa3c9a66SMatthias Ringwald 
376aa3c9a66SMatthias Ringwald     report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
377f4f6c196SMilanka Ringwald     if (report != NULL){
378f4f6c196SMilanka Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
379f4f6c196SMilanka Ringwald         report->client_configuration_value = new_value;
380f4f6c196SMilanka Ringwald         log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value);
381f4f6c196SMilanka Ringwald 
382f4f6c196SMilanka Ringwald         switch (report->type){
383f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_INPUT:
384f00767dfSMatthias Ringwald                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
385f4f6c196SMilanka Ringwald                 break;
386f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_OUTPUT:
387f00767dfSMatthias Ringwald                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
388f4f6c196SMilanka Ringwald                 break;
389f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_FEATURE:
390f00767dfSMatthias Ringwald                 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
391f4f6c196SMilanka Ringwald                 break;
392f4f6c196SMilanka Ringwald             default:
393f4f6c196SMilanka Ringwald                 btstack_unreachable();
394f4f6c196SMilanka Ringwald                 break;
395f4f6c196SMilanka Ringwald         }
396f4f6c196SMilanka Ringwald     }
397a4bfc4feSMatthias Ringwald     return 0;
398a4bfc4feSMatthias Ringwald }
399a4bfc4feSMatthias Ringwald 
400f823745aSMilanka Ringwald void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size,
401f823745aSMilanka Ringwald     uint16_t num_reports, hids_device_report_t * report_storage){
402f823745aSMilanka Ringwald 
4030235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_create_instance();
404a4bfc4feSMatthias Ringwald 
405f823745aSMilanka Ringwald     btstack_assert(num_reports > 0);
406f823745aSMilanka Ringwald     btstack_assert(report_storage != NULL);
407f823745aSMilanka Ringwald 
408f823745aSMilanka Ringwald     instance->hid_country_code    = hid_country_code;
409f823745aSMilanka Ringwald     instance->hid_descriptor      = hid_descriptor;
410f823745aSMilanka Ringwald     instance->hid_descriptor_size = hid_descriptor_size;
411a4bfc4feSMatthias Ringwald 
412a4bfc4feSMatthias Ringwald     // default
4130235c9e5SMilanka Ringwald     instance->hid_protocol_mode   = 1;
414a4bfc4feSMatthias Ringwald 
415a4bfc4feSMatthias Ringwald     // get service handle range
416a4bfc4feSMatthias Ringwald     uint16_t start_handle = 0;
417843359edSMatthias Ringwald     uint16_t end_handle   = 0xffff;
418c436b760SMilanka Ringwald     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
419a2489f29SMatthias Ringwald     btstack_assert(service_found != 0);
420a2489f29SMatthias Ringwald     UNUSED(service_found);
421a4bfc4feSMatthias Ringwald 
422a4bfc4feSMatthias Ringwald     // get report map handle
4230235c9e5SMilanka Ringwald     instance->hid_report_map_handle                               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
424a4bfc4feSMatthias Ringwald 
425a4bfc4feSMatthias Ringwald     // get report map handle
4260235c9e5SMilanka 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);
427a4bfc4feSMatthias Ringwald 
428a4bfc4feSMatthias Ringwald     // get value and client configuration handles for boot mouse input, boot keyboard input and report input
4290235c9e5SMilanka 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);
4300235c9e5SMilanka 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);
431a4bfc4feSMatthias Ringwald 
4320235c9e5SMilanka 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);
4330235c9e5SMilanka 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);
434a4bfc4feSMatthias Ringwald 
435*bbf45f89SMilanka Ringwald     instance->hid_boot_keyboard_output_value_handle               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT);
436*bbf45f89SMilanka Ringwald 
4370235c9e5SMilanka 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);
4380235c9e5SMilanka Ringwald 
439c9d24807SMilanka Ringwald     log_info("hid_report_map_handle                               0x%02x", instance->hid_report_map_handle);
440c9d24807SMilanka Ringwald     log_info("hid_protocol_mode_value_handle                      0x%02x", instance->hid_protocol_mode_value_handle);
441c9d24807SMilanka Ringwald     log_info("hid_boot_mouse_input_value_handle                   0x%02x", instance->hid_boot_mouse_input_value_handle);
442c9d24807SMilanka Ringwald     log_info("hid_boot_mouse_input_client_configuration_handle    0x%02x", instance->hid_boot_mouse_input_client_configuration_handle);
443c9d24807SMilanka Ringwald     log_info("hid_boot_keyboard_input_value_handle                0x%02x", instance->hid_boot_keyboard_input_value_handle);
444*bbf45f89SMilanka Ringwald     log_info("hid_boot_keyboard_output_value_handle               0x%02x", instance->hid_boot_keyboard_output_value_handle);
445c9d24807SMilanka Ringwald     log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle);
446c9d24807SMilanka Ringwald     log_info("hid_control_point_value_handle                      0x%02x", instance->hid_control_point_value_handle);
447a4bfc4feSMatthias Ringwald 
448f823745aSMilanka Ringwald     instance->hid_reports = report_storage;
449f4f6c196SMilanka Ringwald 
450f823745aSMilanka Ringwald     uint16_t hid_reports_num = num_reports;
451f4f6c196SMilanka Ringwald     uint16_t assigned_reports_num = 0;
452f4f6c196SMilanka Ringwald     uint16_t start_chr_handle = start_handle;
453f4f6c196SMilanka Ringwald 
454f4f6c196SMilanka Ringwald     while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) {
4552c84eaa3SMatthias Ringwald         // mandatory
456f4f6c196SMilanka Ringwald         uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
457f4f6c196SMilanka Ringwald         if (chr_value_handle == 0){
458f4f6c196SMilanka Ringwald             break;
459f4f6c196SMilanka Ringwald         }
460f4f6c196SMilanka Ringwald 
4612c84eaa3SMatthias Ringwald         // optional
462f4f6c196SMilanka 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);
4632c84eaa3SMatthias Ringwald 
4642c84eaa3SMatthias Ringwald         // mandatory
465f4f6c196SMilanka 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);
4662c84eaa3SMatthias Ringwald         if (report_reference_handle == 0){
4672c84eaa3SMatthias Ringwald             break;
4682c84eaa3SMatthias Ringwald         }
4692c84eaa3SMatthias Ringwald 
4702c84eaa3SMatthias Ringwald         // get report id and type from report reference
471f4f6c196SMilanka Ringwald         uint16_t report_reference_value_len;
472f4f6c196SMilanka Ringwald         const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len);
473f4f6c196SMilanka Ringwald         if (report_reference_value == NULL){
474f4f6c196SMilanka Ringwald             break;
475f4f6c196SMilanka Ringwald         }
476f4f6c196SMilanka Ringwald         if (report_reference_value_len != 2){
477f4f6c196SMilanka Ringwald             break;
478f4f6c196SMilanka Ringwald         }
4792c84eaa3SMatthias Ringwald         uint8_t report_id = report_reference_value[0];
4802c84eaa3SMatthias Ringwald         hid_report_type_t report_type = (hid_report_type_t) report_reference_value[1];
481f4f6c196SMilanka Ringwald 
4822c84eaa3SMatthias Ringwald         // store report info
483160eac51SMatthias Ringwald         hids_device_report_t * report = &report_storage[assigned_reports_num];
484f4f6c196SMilanka Ringwald         report->value_handle = chr_value_handle;
485f4f6c196SMilanka Ringwald         report->client_configuration_handle = chr_client_configuration_handle;
486f4f6c196SMilanka Ringwald         report->client_configuration_value = 0;
4872c84eaa3SMatthias Ringwald         report->id   = report_id;
4882c84eaa3SMatthias Ringwald         report->type = report_type;
4891b9864eaSMatthias Ringwald         report->size = btstack_hid_get_report_size_for_id(report_id, report_type, hid_descriptor_size, hid_descriptor);
490f4f6c196SMilanka Ringwald 
491f4f6c196SMilanka Ringwald         switch (report->type){
492f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_INPUT:
493f4f6c196SMilanka Ringwald                 instance->hid_input_reports_num++;
494f4f6c196SMilanka Ringwald                 break;
495f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_OUTPUT:
496f4f6c196SMilanka Ringwald                 instance->hid_output_reports_num++;
497f4f6c196SMilanka Ringwald                 break;
498f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_FEATURE:
499f4f6c196SMilanka Ringwald                 instance->hid_feature_reports_num++;
500f4f6c196SMilanka Ringwald                 break;
501f4f6c196SMilanka Ringwald             default:
502f4f6c196SMilanka Ringwald                 btstack_unreachable();
503f4f6c196SMilanka Ringwald                 return;
504f4f6c196SMilanka Ringwald         }
505f4f6c196SMilanka Ringwald         log_info("hid_report_value_handle                       0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type);
5062c84eaa3SMatthias Ringwald         if (report->client_configuration_handle != 0){
507f4f6c196SMilanka Ringwald             log_info("hid_report_client_configuration_handle        0x%02x", report->client_configuration_handle);
5082c84eaa3SMatthias Ringwald         }
509f4f6c196SMilanka Ringwald 
510f4f6c196SMilanka Ringwald         assigned_reports_num++;
5112c84eaa3SMatthias Ringwald         start_chr_handle = report_reference_handle + 1;
512f4f6c196SMilanka Ringwald     }
513f4f6c196SMilanka Ringwald 
5143d71c7a4SMatthias Ringwald     // register service with ATT Server
515a4bfc4feSMatthias Ringwald     hid_service.start_handle   = start_handle;
516a4bfc4feSMatthias Ringwald     hid_service.end_handle     = end_handle;
517a4bfc4feSMatthias Ringwald     hid_service.read_callback  = &att_read_callback;
518a4bfc4feSMatthias Ringwald     hid_service.write_callback = &att_write_callback;
5193d71c7a4SMatthias Ringwald     att_server_register_service_handler(&hid_service);
520a4bfc4feSMatthias Ringwald }
521a4bfc4feSMatthias Ringwald 
522a4bfc4feSMatthias Ringwald /**
523f823745aSMilanka Ringwald  * @brief Set up HIDS Device
524f823745aSMilanka Ringwald  */
525f823745aSMilanka Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
526160eac51SMatthias Ringwald     uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t);
527160eac51SMatthias Ringwald     hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage);
528f823745aSMilanka Ringwald }
529f823745aSMilanka Ringwald 
530f823745aSMilanka Ringwald /**
531a4bfc4feSMatthias Ringwald  * @brief Register callback for the HIDS Device client.
532a4bfc4feSMatthias Ringwald  * @param callback
533a4bfc4feSMatthias Ringwald  */
534a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){
535a4bfc4feSMatthias Ringwald     packet_handler = callback;
536a4bfc4feSMatthias Ringwald }
537a4bfc4feSMatthias Ringwald 
5381b9864eaSMatthias 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)){
5391b9864eaSMatthias Ringwald     hids_device_get_report_callback = callback;
5401b9864eaSMatthias Ringwald }
5411b9864eaSMatthias Ringwald 
542a4bfc4feSMatthias Ringwald /**
543a4bfc4feSMatthias Ringwald  * @brief Request can send now event to send HID Report
544a4bfc4feSMatthias Ringwald  * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
545a4bfc4feSMatthias Ringwald  * @param hid_cid
546a4bfc4feSMatthias Ringwald  */
547a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
5480235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
5490235c9e5SMilanka Ringwald     if (!instance){
5500235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
5510235c9e5SMilanka Ringwald         return;
5520235c9e5SMilanka Ringwald     }
5530235c9e5SMilanka Ringwald 
554086ea75fSMatthias Ringwald     instance->can_send_now_callback.callback = &hids_device_can_send_now;
555086ea75fSMatthias Ringwald     instance->can_send_now_callback.context  = (void*) (uintptr_t) con_handle;
556086ea75fSMatthias Ringwald     att_server_register_can_send_now_callback(&instance->can_send_now_callback, con_handle);
557a4bfc4feSMatthias Ringwald }
558a4bfc4feSMatthias Ringwald 
55945569c34SMatthias 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){
5600235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
5610235c9e5SMilanka Ringwald     if (!instance){
5620235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
563f4f6c196SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
5640235c9e5SMilanka Ringwald     }
565f4f6c196SMilanka Ringwald 
566a3c1c955SMatthias Ringwald     hids_device_report_t * report_storage = hids_device_get_report_for_id_and_type(instance, report_id,
567a3c1c955SMatthias Ringwald                                                                                    HID_REPORT_TYPE_INPUT);
568f4f6c196SMilanka Ringwald     if (report_storage == NULL){
569f4f6c196SMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
570f4f6c196SMilanka Ringwald     }
571f4f6c196SMilanka Ringwald 
572f4f6c196SMilanka Ringwald     return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
573f4f6c196SMilanka Ringwald }
574f4f6c196SMilanka Ringwald 
575d38dfb7fSMatthias Ringwald uint8_t hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
576a90492ddSMatthias Ringwald     hids_device_t * device = hids_device_get_instance_for_con_handle(con_handle);
577a90492ddSMatthias Ringwald     if (!device){
578f4f6c196SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
579f4f6c196SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
580f4f6c196SMilanka Ringwald     }
581f4f6c196SMilanka Ringwald 
582a90492ddSMatthias Ringwald     uint8_t pos;
583a90492ddSMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
584a90492ddSMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
585a90492ddSMatthias Ringwald         hids_device_report_t * report_storage = &device->hid_reports[pos];
586d38dfb7fSMatthias Ringwald         if (report_storage->type == HID_REPORT_TYPE_INPUT){
587f4f6c196SMilanka Ringwald             return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
588f4f6c196SMilanka Ringwald         }
589f4f6c196SMilanka Ringwald     }
590f4f6c196SMilanka Ringwald     return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
591f4f6c196SMilanka Ringwald }
592f4f6c196SMilanka Ringwald 
593d38dfb7fSMatthias Ringwald /**
594d38dfb7fSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
595d38dfb7fSMatthias Ringwald  */
596d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
597d38dfb7fSMatthias Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
598d38dfb7fSMatthias Ringwald     if (!instance){
599d38dfb7fSMatthias Ringwald         log_error("no instance for handle 0x%02x", con_handle);
600d38dfb7fSMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
601d38dfb7fSMatthias Ringwald     }
602d38dfb7fSMatthias Ringwald     return att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
6030235c9e5SMilanka Ringwald }
6040235c9e5SMilanka Ringwald 
6050235c9e5SMilanka Ringwald /**
606a4bfc4feSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
607a4bfc4feSMatthias Ringwald  */
608d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
6090235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
6100235c9e5SMilanka Ringwald     if (!instance){
6110235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
612d38dfb7fSMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
6130235c9e5SMilanka Ringwald     }
614d38dfb7fSMatthias Ringwald     return att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
615a4bfc4feSMatthias Ringwald }
616