xref: /btstack/src/ble/gatt-service/hids_device.c (revision d75a985bdf68b6fa48d644ad1e285518663fa987)
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"
52a4bfc4feSMatthias Ringwald 
530235c9e5SMilanka Ringwald #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS    0x80
540235c9e5SMilanka Ringwald 
55160eac51SMatthias Ringwald // storage for 'generic' HID Device with single Input, Output, and Feature Report
56160eac51SMatthias Ringwald static hids_device_report_t hid_reports_generic_storage[3];
57f4f6c196SMilanka Ringwald 
580235c9e5SMilanka Ringwald typedef struct{
590235c9e5SMilanka Ringwald     uint16_t        con_handle;
600235c9e5SMilanka Ringwald 
610235c9e5SMilanka Ringwald     uint8_t         hid_country_code;
620235c9e5SMilanka Ringwald     const uint8_t * hid_descriptor;
630235c9e5SMilanka Ringwald     uint16_t        hid_descriptor_size;
640235c9e5SMilanka Ringwald 
650235c9e5SMilanka Ringwald     uint16_t        hid_report_map_handle;
66c1dbba9dSMatthias Ringwald     uint8_t         hid_protocol_mode;
670235c9e5SMilanka Ringwald     uint16_t        hid_protocol_mode_value_handle;
680235c9e5SMilanka Ringwald 
690235c9e5SMilanka Ringwald     uint16_t        hid_boot_mouse_input_value_handle;
700235c9e5SMilanka Ringwald     uint16_t        hid_boot_mouse_input_client_configuration_handle;
710235c9e5SMilanka Ringwald     uint16_t        hid_boot_mouse_input_client_configuration_value;
720235c9e5SMilanka Ringwald 
730235c9e5SMilanka Ringwald     uint16_t        hid_boot_keyboard_input_value_handle;
740235c9e5SMilanka Ringwald     uint16_t        hid_boot_keyboard_input_client_configuration_handle;
750235c9e5SMilanka Ringwald     uint16_t        hid_boot_keyboard_input_client_configuration_value;
760235c9e5SMilanka Ringwald 
77f4f6c196SMilanka Ringwald     hids_device_report_t * hid_reports;
780235c9e5SMilanka Ringwald 
79f4f6c196SMilanka Ringwald     uint8_t         hid_input_reports_num;
80f4f6c196SMilanka Ringwald     uint8_t         hid_output_reports_num;
81f4f6c196SMilanka Ringwald     uint8_t         hid_feature_reports_num;
820235c9e5SMilanka Ringwald 
830235c9e5SMilanka Ringwald     uint16_t        hid_control_point_value_handle;
840235c9e5SMilanka Ringwald     uint8_t         hid_control_point_suspend;
850235c9e5SMilanka Ringwald 
860235c9e5SMilanka Ringwald     btstack_context_callback_registration_t  battery_callback;
870235c9e5SMilanka Ringwald } hids_device_t;
880235c9e5SMilanka Ringwald 
890235c9e5SMilanka Ringwald static hids_device_t hids_device;
900235c9e5SMilanka Ringwald 
91a4bfc4feSMatthias Ringwald static btstack_packet_handler_t packet_handler;
92a4bfc4feSMatthias Ringwald static att_service_handler_t hid_service;
93a4bfc4feSMatthias Ringwald 
940235c9e5SMilanka Ringwald // TODO: store hids device connection into list
950235c9e5SMilanka Ringwald static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
960235c9e5SMilanka Ringwald     UNUSED(con_handle);
970235c9e5SMilanka Ringwald     return &hids_device;
980235c9e5SMilanka Ringwald }
990235c9e5SMilanka Ringwald 
1000235c9e5SMilanka Ringwald static hids_device_t * hids_device_create_instance(void){
101f4f6c196SMilanka Ringwald     memset(&hids_device, 0, sizeof(hids_device_t));
1020235c9e5SMilanka Ringwald     return &hids_device;
1030235c9e5SMilanka Ringwald }
1040235c9e5SMilanka Ringwald 
105f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){
106*d75a985bSMatthias Ringwald     uint8_t pos;
107*d75a985bSMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
108*d75a985bSMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
109f4f6c196SMilanka Ringwald         if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){
110f4f6c196SMilanka Ringwald             return &device->hid_reports[pos];
111f4f6c196SMilanka Ringwald         }
112f4f6c196SMilanka Ringwald     }
113f4f6c196SMilanka Ringwald     return NULL;
114f4f6c196SMilanka Ringwald }
115f4f6c196SMilanka Ringwald 
116f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_id(hids_device_t * device, uint16_t report_id){
117*d75a985bSMatthias Ringwald     uint8_t pos;
118*d75a985bSMatthias Ringwald     uint8_t total_reports =  device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
119*d75a985bSMatthias Ringwald     for (pos = 0 ; pos < total_reports ; pos++){
120f4f6c196SMilanka Ringwald         if (device->hid_reports[pos].id == report_id){
121f4f6c196SMilanka Ringwald             return &device->hid_reports[pos];
122f4f6c196SMilanka Ringwald         }
123f4f6c196SMilanka Ringwald     }
124f4f6c196SMilanka Ringwald     return NULL;
125f4f6c196SMilanka Ringwald }
1260235c9e5SMilanka Ringwald 
127a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
1280235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1290235c9e5SMilanka Ringwald     if (!instance){
1300235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1310235c9e5SMilanka Ringwald         return;
1320235c9e5SMilanka Ringwald     }
1330235c9e5SMilanka Ringwald 
134a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
135a4bfc4feSMatthias Ringwald     uint8_t buffer[6];
136a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
137a4bfc4feSMatthias Ringwald     buffer[1] = 4;
138a4bfc4feSMatthias Ringwald     buffer[2] = event;
139a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
140a4bfc4feSMatthias Ringwald     buffer[5] = value;
141a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
142a4bfc4feSMatthias Ringwald }
143a4bfc4feSMatthias Ringwald 
1440235c9e5SMilanka Ringwald static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
1450235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1460235c9e5SMilanka Ringwald     if (!instance){
1470235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1480235c9e5SMilanka Ringwald         return;
1490235c9e5SMilanka Ringwald     }
1500235c9e5SMilanka Ringwald 
1510235c9e5SMilanka Ringwald     if (!packet_handler) return;
1520235c9e5SMilanka Ringwald     uint8_t buffer[5];
1530235c9e5SMilanka Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
1540235c9e5SMilanka Ringwald     buffer[1] = 4;
1550235c9e5SMilanka Ringwald     buffer[2] = event;
1560235c9e5SMilanka Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
1570235c9e5SMilanka Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
1580235c9e5SMilanka Ringwald }
1590235c9e5SMilanka Ringwald 
160a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){
161a4bfc4feSMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
162a4bfc4feSMatthias Ringwald     // notify client
1630235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1640235c9e5SMilanka Ringwald     if (!instance){
1650235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1660235c9e5SMilanka Ringwald         return;
1670235c9e5SMilanka Ringwald     }
1680235c9e5SMilanka Ringwald 
169a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
170a4bfc4feSMatthias Ringwald     uint8_t buffer[5];
171a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
172a4bfc4feSMatthias Ringwald     buffer[1] = 3;
173a4bfc4feSMatthias Ringwald     buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
174a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
175a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
176a4bfc4feSMatthias Ringwald }
177a4bfc4feSMatthias Ringwald 
178a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data
179a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
180a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
1810235c9e5SMilanka 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){
1820235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1830235c9e5SMilanka Ringwald     if (!instance){
1840235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
1850235c9e5SMilanka Ringwald         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
186a4bfc4feSMatthias Ringwald     }
1870235c9e5SMilanka Ringwald 
1880235c9e5SMilanka Ringwald     if (att_handle == instance->hid_protocol_mode_value_handle){
1890235c9e5SMilanka Ringwald         log_info("Read protocol mode");
1900235c9e5SMilanka Ringwald         return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
1910235c9e5SMilanka Ringwald     }
192c9d24807SMilanka Ringwald 
1930235c9e5SMilanka Ringwald     if (att_handle == instance->hid_report_map_handle){
194523edd21SMatthias Ringwald         log_info("Read report map");
1950235c9e5SMilanka Ringwald         return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
196a4bfc4feSMatthias Ringwald     }
197c9d24807SMilanka Ringwald 
1980235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
1990235c9e5SMilanka Ringwald         return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
200a4bfc4feSMatthias Ringwald     }
201c9d24807SMilanka Ringwald 
2020235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
2030235c9e5SMilanka Ringwald         return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
204a4bfc4feSMatthias Ringwald     }
205c9d24807SMilanka Ringwald 
2060235c9e5SMilanka Ringwald     if (att_handle == instance->hid_control_point_value_handle){
2074ea43905SMatthias Ringwald         if (buffer && (buffer_size >= 1u)){
2080235c9e5SMilanka Ringwald             buffer[0] = instance->hid_control_point_suspend;
2090235c9e5SMilanka Ringwald         }
2100235c9e5SMilanka Ringwald         return 1;
211a4bfc4feSMatthias Ringwald     }
212f4f6c196SMilanka Ringwald 
213f4f6c196SMilanka Ringwald     hids_device_report_t * report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
214f4f6c196SMilanka Ringwald     if (report != NULL){
215f4f6c196SMilanka Ringwald         return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size);
216f4f6c196SMilanka Ringwald     }
217a4bfc4feSMatthias Ringwald     return 0;
218a4bfc4feSMatthias Ringwald }
219a4bfc4feSMatthias Ringwald 
220a4bfc4feSMatthias 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){
221a4bfc4feSMatthias Ringwald     UNUSED(transaction_mode);
222a4bfc4feSMatthias Ringwald     UNUSED(buffer_size);
223a4bfc4feSMatthias Ringwald     UNUSED(offset);
224a4bfc4feSMatthias Ringwald 
2250235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
2260235c9e5SMilanka Ringwald     if (!instance){
2270235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
2280235c9e5SMilanka Ringwald         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
2290235c9e5SMilanka Ringwald     }
2300235c9e5SMilanka Ringwald 
2310235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
23285828295SMatthias Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
2330235c9e5SMilanka Ringwald         instance->hid_boot_mouse_input_client_configuration_value = new_value;
234c1dbba9dSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
235a4bfc4feSMatthias Ringwald     }
2360235c9e5SMilanka Ringwald     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
23785828295SMatthias Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
2380235c9e5SMilanka Ringwald         instance->hid_boot_keyboard_input_client_configuration_value = new_value;
239c1dbba9dSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
240a4bfc4feSMatthias Ringwald     }
2410235c9e5SMilanka Ringwald 
2420235c9e5SMilanka Ringwald     if (att_handle == instance->hid_protocol_mode_value_handle){
2430235c9e5SMilanka Ringwald         instance->hid_protocol_mode = buffer[0];
2440235c9e5SMilanka Ringwald         log_info("Set protocol mode: %u", instance->hid_protocol_mode);
2450235c9e5SMilanka Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
2460235c9e5SMilanka Ringwald     }
2470235c9e5SMilanka Ringwald 
2480235c9e5SMilanka Ringwald     if (att_handle == instance->hid_control_point_value_handle){
2494ea43905SMatthias Ringwald         if (buffer_size < 1u){
2500235c9e5SMilanka Ringwald             return ATT_ERROR_INVALID_OFFSET;
2510235c9e5SMilanka Ringwald         }
2520235c9e5SMilanka Ringwald         instance->hid_control_point_suspend = buffer[0];
2530235c9e5SMilanka Ringwald         instance->con_handle = con_handle;
2540235c9e5SMilanka Ringwald         log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
2554ea43905SMatthias Ringwald         if (instance->hid_control_point_suspend == 0u){
2560235c9e5SMilanka Ringwald             hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
2574ea43905SMatthias Ringwald         } else if (instance->hid_control_point_suspend == 1u){
2580235c9e5SMilanka Ringwald             hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
2590235c9e5SMilanka Ringwald         }
2600235c9e5SMilanka Ringwald     }
261f4f6c196SMilanka Ringwald 
262f4f6c196SMilanka Ringwald     hids_device_report_t * report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
263f4f6c196SMilanka Ringwald     if (report != NULL){
264f4f6c196SMilanka Ringwald         uint16_t new_value = little_endian_read_16(buffer, 0);
265f4f6c196SMilanka Ringwald         report->client_configuration_value = new_value;
266f4f6c196SMilanka Ringwald         log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value);
267f4f6c196SMilanka Ringwald 
268f4f6c196SMilanka Ringwald         switch (report->type){
269f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_INPUT:
270f4f6c196SMilanka Ringwald                 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
271f4f6c196SMilanka Ringwald                 break;
272f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_OUTPUT:
273f4f6c196SMilanka Ringwald                 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
274f4f6c196SMilanka Ringwald                 break;
275f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_FEATURE:
276f4f6c196SMilanka Ringwald                 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, (uint8_t) new_value);
277f4f6c196SMilanka Ringwald                 break;
278f4f6c196SMilanka Ringwald             default:
279f4f6c196SMilanka Ringwald                 btstack_unreachable();
280f4f6c196SMilanka Ringwald                 break;
281f4f6c196SMilanka Ringwald         }
282f4f6c196SMilanka Ringwald     }
283a4bfc4feSMatthias Ringwald     return 0;
284a4bfc4feSMatthias Ringwald }
285a4bfc4feSMatthias Ringwald 
286f823745aSMilanka Ringwald void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size,
287f823745aSMilanka Ringwald     uint16_t num_reports, hids_device_report_t * report_storage){
288f823745aSMilanka Ringwald 
2890235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_create_instance();
290a4bfc4feSMatthias Ringwald 
291f823745aSMilanka Ringwald     btstack_assert(num_reports > 0);
292f823745aSMilanka Ringwald     btstack_assert(report_storage != NULL);
293f823745aSMilanka Ringwald 
294f823745aSMilanka Ringwald     instance->hid_country_code    = hid_country_code;
295f823745aSMilanka Ringwald     instance->hid_descriptor      = hid_descriptor;
296f823745aSMilanka Ringwald     instance->hid_descriptor_size = hid_descriptor_size;
297a4bfc4feSMatthias Ringwald 
298a4bfc4feSMatthias Ringwald     // default
2990235c9e5SMilanka Ringwald     instance->hid_protocol_mode   = 1;
300a4bfc4feSMatthias Ringwald 
301a4bfc4feSMatthias Ringwald     // get service handle range
302a4bfc4feSMatthias Ringwald     uint16_t start_handle = 0;
303843359edSMatthias Ringwald     uint16_t end_handle   = 0xffff;
304c436b760SMilanka Ringwald     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
305a2489f29SMatthias Ringwald     btstack_assert(service_found != 0);
306a2489f29SMatthias Ringwald     UNUSED(service_found);
307a4bfc4feSMatthias Ringwald 
308a4bfc4feSMatthias Ringwald     // get report map handle
3090235c9e5SMilanka Ringwald     instance->hid_report_map_handle                               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
310a4bfc4feSMatthias Ringwald 
311a4bfc4feSMatthias Ringwald     // get report map handle
3120235c9e5SMilanka 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);
313a4bfc4feSMatthias Ringwald 
314a4bfc4feSMatthias Ringwald     // get value and client configuration handles for boot mouse input, boot keyboard input and report input
3150235c9e5SMilanka 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);
3160235c9e5SMilanka 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);
317a4bfc4feSMatthias Ringwald 
3180235c9e5SMilanka 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);
3190235c9e5SMilanka 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);
320a4bfc4feSMatthias Ringwald 
3210235c9e5SMilanka 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);
3220235c9e5SMilanka Ringwald 
323c9d24807SMilanka Ringwald     log_info("hid_report_map_handle                               0x%02x", instance->hid_report_map_handle);
324c9d24807SMilanka Ringwald     log_info("hid_protocol_mode_value_handle                      0x%02x", instance->hid_protocol_mode_value_handle);
325c9d24807SMilanka Ringwald     log_info("hid_boot_mouse_input_value_handle                   0x%02x", instance->hid_boot_mouse_input_value_handle);
326c9d24807SMilanka Ringwald     log_info("hid_boot_mouse_input_client_configuration_handle    0x%02x", instance->hid_boot_mouse_input_client_configuration_handle);
327c9d24807SMilanka Ringwald     log_info("hid_boot_keyboard_input_value_handle                0x%02x", instance->hid_boot_keyboard_input_value_handle);
328c9d24807SMilanka Ringwald     log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle);
329c9d24807SMilanka Ringwald     log_info("hid_control_point_value_handle                      0x%02x", instance->hid_control_point_value_handle);
330a4bfc4feSMatthias Ringwald 
331f823745aSMilanka Ringwald     instance->hid_reports = report_storage;
332f4f6c196SMilanka Ringwald 
333f823745aSMilanka Ringwald     uint16_t hid_reports_num = num_reports;
334f4f6c196SMilanka Ringwald     uint16_t assigned_reports_num = 0;
335f4f6c196SMilanka Ringwald     uint16_t start_chr_handle = start_handle;
336f4f6c196SMilanka Ringwald 
337f4f6c196SMilanka Ringwald     while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) {
338f4f6c196SMilanka Ringwald         uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
339f4f6c196SMilanka Ringwald         if (chr_value_handle == 0){
340f4f6c196SMilanka Ringwald             break;
341f4f6c196SMilanka Ringwald         }
342f4f6c196SMilanka Ringwald 
343f4f6c196SMilanka 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);
344f4f6c196SMilanka 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);
345f4f6c196SMilanka Ringwald         uint16_t report_reference_value_len;
346f4f6c196SMilanka Ringwald         const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len);
347f4f6c196SMilanka Ringwald 
348f4f6c196SMilanka Ringwald         if (report_reference_value == NULL){
349f4f6c196SMilanka Ringwald             break;
350f4f6c196SMilanka Ringwald         }
351f4f6c196SMilanka Ringwald         if (report_reference_value_len != 2){
352f4f6c196SMilanka Ringwald             break;
353f4f6c196SMilanka Ringwald         }
354f4f6c196SMilanka Ringwald 
355160eac51SMatthias Ringwald         hids_device_report_t * report = &report_storage[assigned_reports_num];
356f4f6c196SMilanka Ringwald         report->value_handle = chr_value_handle;
357f4f6c196SMilanka Ringwald         report->client_configuration_handle = chr_client_configuration_handle;
358f4f6c196SMilanka Ringwald         report->client_configuration_value = 0;
359f4f6c196SMilanka Ringwald 
360f4f6c196SMilanka Ringwald         report->id = report_reference_value[0];
361f4f6c196SMilanka Ringwald         report->type = (hid_report_type_t)report_reference_value[1];
362f4f6c196SMilanka Ringwald 
363f4f6c196SMilanka Ringwald         switch (report->type){
364f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_INPUT:
365f4f6c196SMilanka Ringwald                 instance->hid_input_reports_num++;
366f4f6c196SMilanka Ringwald                 break;
367f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_OUTPUT:
368f4f6c196SMilanka Ringwald                 instance->hid_output_reports_num++;
369f4f6c196SMilanka Ringwald                 break;
370f4f6c196SMilanka Ringwald             case HID_REPORT_TYPE_FEATURE:
371f4f6c196SMilanka Ringwald                 instance->hid_feature_reports_num++;
372f4f6c196SMilanka Ringwald                 break;
373f4f6c196SMilanka Ringwald             default:
374f4f6c196SMilanka Ringwald                 btstack_unreachable();
375f4f6c196SMilanka Ringwald                 return;
376f4f6c196SMilanka Ringwald         }
377f4f6c196SMilanka Ringwald         log_info("hid_report_value_handle                       0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type);
378f4f6c196SMilanka Ringwald         log_info("hid_report_client_configuration_handle        0x%02x", report->client_configuration_handle);
379f4f6c196SMilanka Ringwald 
380f4f6c196SMilanka Ringwald         assigned_reports_num++;
381f4f6c196SMilanka Ringwald         start_chr_handle = chr_client_configuration_handle + 1;
382f4f6c196SMilanka Ringwald     }
383f4f6c196SMilanka Ringwald 
3843d71c7a4SMatthias Ringwald     // register service with ATT Server
385a4bfc4feSMatthias Ringwald     hid_service.start_handle   = start_handle;
386a4bfc4feSMatthias Ringwald     hid_service.end_handle     = end_handle;
387a4bfc4feSMatthias Ringwald     hid_service.read_callback  = &att_read_callback;
388a4bfc4feSMatthias Ringwald     hid_service.write_callback = &att_write_callback;
3893d71c7a4SMatthias Ringwald     att_server_register_service_handler(&hid_service);
390a4bfc4feSMatthias Ringwald }
391a4bfc4feSMatthias Ringwald 
392a4bfc4feSMatthias Ringwald /**
393f823745aSMilanka Ringwald  * @brief Set up HIDS Device
394f823745aSMilanka Ringwald  */
395f823745aSMilanka Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
396160eac51SMatthias Ringwald     uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t);
397160eac51SMatthias Ringwald     hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage);
398f823745aSMilanka Ringwald }
399f823745aSMilanka Ringwald 
400f823745aSMilanka Ringwald /**
401a4bfc4feSMatthias Ringwald  * @brief Register callback for the HIDS Device client.
402a4bfc4feSMatthias Ringwald  * @param callback
403a4bfc4feSMatthias Ringwald  */
404a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){
405a4bfc4feSMatthias Ringwald     packet_handler = callback;
406a4bfc4feSMatthias Ringwald }
407a4bfc4feSMatthias Ringwald 
408a4bfc4feSMatthias Ringwald /**
409a4bfc4feSMatthias Ringwald  * @brief Request can send now event to send HID Report
410a4bfc4feSMatthias Ringwald  * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
411a4bfc4feSMatthias Ringwald  * @param hid_cid
412a4bfc4feSMatthias Ringwald  */
413a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
4140235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
4150235c9e5SMilanka Ringwald     if (!instance){
4160235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
4170235c9e5SMilanka Ringwald         return;
4180235c9e5SMilanka Ringwald     }
4190235c9e5SMilanka Ringwald 
4200235c9e5SMilanka Ringwald     instance->battery_callback.callback = &hids_device_can_send_now;
4210235c9e5SMilanka Ringwald     instance->battery_callback.context  = (void*) (uintptr_t) con_handle;
4220235c9e5SMilanka Ringwald     att_server_register_can_send_now_callback(&instance->battery_callback, con_handle);
423a4bfc4feSMatthias Ringwald }
424a4bfc4feSMatthias Ringwald 
425a4bfc4feSMatthias Ringwald /**
426a4bfc4feSMatthias Ringwald  * @brief Send HID Report: Input
427a4bfc4feSMatthias Ringwald  */
428f4f6c196SMilanka Ringwald 
429f4f6c196SMilanka Ringwald uint8_t hids_device_send_report_with_id(hci_con_handle_t con_handle, uint16_t report_id, const uint8_t * report, uint16_t report_len){
4300235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
4310235c9e5SMilanka Ringwald     if (!instance){
4320235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
433f4f6c196SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
4340235c9e5SMilanka Ringwald     }
435f4f6c196SMilanka Ringwald 
436f4f6c196SMilanka Ringwald     hids_device_report_t * report_storage = hids_device_get_report_for_id(instance, report_id);
437f4f6c196SMilanka Ringwald     if (report_storage == NULL){
438f4f6c196SMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
439f4f6c196SMilanka Ringwald     }
440f4f6c196SMilanka Ringwald 
441f4f6c196SMilanka Ringwald     return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
442f4f6c196SMilanka Ringwald }
443f4f6c196SMilanka Ringwald 
444f4f6c196SMilanka Ringwald static uint8_t hids_device_send_report_with_type(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len, hid_report_type_t report_type){
445f4f6c196SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
446f4f6c196SMilanka Ringwald     if (!instance){
447f4f6c196SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
448f4f6c196SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
449f4f6c196SMilanka Ringwald     }
450f4f6c196SMilanka Ringwald 
451f4f6c196SMilanka Ringwald     uint8_t pos = 0;
452f4f6c196SMilanka Ringwald     while (pos < (instance->hid_input_reports_num + instance->hid_output_reports_num + instance->hid_feature_reports_num)){
453f4f6c196SMilanka Ringwald         hids_device_report_t * report_storage = &instance->hid_reports[pos];
454f4f6c196SMilanka Ringwald 
455f4f6c196SMilanka Ringwald         if (report_storage->type == report_type){
456f4f6c196SMilanka Ringwald             return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
457f4f6c196SMilanka Ringwald         }
458f4f6c196SMilanka Ringwald     }
459f4f6c196SMilanka Ringwald     return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
460f4f6c196SMilanka Ringwald }
461f4f6c196SMilanka Ringwald 
462f4f6c196SMilanka Ringwald void hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
463f4f6c196SMilanka Ringwald     (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_INPUT);
4640235c9e5SMilanka Ringwald }
4650235c9e5SMilanka Ringwald 
4660235c9e5SMilanka Ringwald /**
4670235c9e5SMilanka Ringwald  * @brief Send HID Report: Output
4680235c9e5SMilanka Ringwald  */
4690235c9e5SMilanka Ringwald void hids_device_send_output_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
470f4f6c196SMilanka Ringwald     (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_OUTPUT);
4710235c9e5SMilanka Ringwald }
4720235c9e5SMilanka Ringwald 
4730235c9e5SMilanka Ringwald /**
4740235c9e5SMilanka Ringwald  * @brief Send HID Report: Feature
4750235c9e5SMilanka Ringwald  */
4760235c9e5SMilanka Ringwald void hids_device_send_feature_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
477f4f6c196SMilanka Ringwald     (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_FEATURE);
478a4bfc4feSMatthias Ringwald }
479a4bfc4feSMatthias Ringwald 
480a4bfc4feSMatthias Ringwald /**
481a4bfc4feSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
482a4bfc4feSMatthias Ringwald  */
483a4bfc4feSMatthias Ringwald void hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
4840235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
4850235c9e5SMilanka Ringwald     if (!instance){
4860235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
4870235c9e5SMilanka Ringwald         return;
4880235c9e5SMilanka Ringwald     }
489842f9974SMilanka Ringwald     att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
490a4bfc4feSMatthias Ringwald }
491a4bfc4feSMatthias Ringwald 
492a4bfc4feSMatthias Ringwald /**
493a4bfc4feSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
494a4bfc4feSMatthias Ringwald  */
495a4bfc4feSMatthias Ringwald void hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
4960235c9e5SMilanka Ringwald     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
4970235c9e5SMilanka Ringwald     if (!instance){
4980235c9e5SMilanka Ringwald         log_error("no instance for handle 0x%02x", con_handle);
4990235c9e5SMilanka Ringwald         return;
5000235c9e5SMilanka Ringwald     }
501842f9974SMilanka Ringwald     att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
502a4bfc4feSMatthias Ringwald }
503