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 86086ea75fSMatthias Ringwald btstack_context_callback_registration_t can_send_now_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 105*aa3c9a66SMatthias Ringwald static hids_device_report_t * hids_device_get_report_for_value_handle(hids_device_t * device, uint16_t client_configuration_handle){ 106*aa3c9a66SMatthias Ringwald uint8_t pos; 107*aa3c9a66SMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 108*aa3c9a66SMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){ 109*aa3c9a66SMatthias Ringwald if (device->hid_reports[pos].value_handle == client_configuration_handle){ 110*aa3c9a66SMatthias Ringwald return &device->hid_reports[pos]; 111*aa3c9a66SMatthias Ringwald } 112*aa3c9a66SMatthias Ringwald } 113*aa3c9a66SMatthias Ringwald return NULL; 114*aa3c9a66SMatthias Ringwald } 115*aa3c9a66SMatthias Ringwald 116f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){ 117d75a985bSMatthias Ringwald uint8_t pos; 118d75a985bSMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 119d75a985bSMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){ 120f4f6c196SMilanka Ringwald if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){ 121f4f6c196SMilanka Ringwald return &device->hid_reports[pos]; 122f4f6c196SMilanka Ringwald } 123f4f6c196SMilanka Ringwald } 124f4f6c196SMilanka Ringwald return NULL; 125f4f6c196SMilanka Ringwald } 126f4f6c196SMilanka Ringwald 127a3c1c955SMatthias Ringwald static hids_device_report_t * 128a3c1c955SMatthias Ringwald hids_device_get_report_for_id_and_type(hids_device_t *device, uint16_t report_id, hid_report_type_t type) { 12945569c34SMatthias Ringwald uint8_t pos; 13045569c34SMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 13145569c34SMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){ 13245569c34SMatthias Ringwald if ((device->hid_reports[pos].type == type) && (device->hid_reports[pos].id == report_id)){ 13345569c34SMatthias Ringwald return &device->hid_reports[pos]; 13445569c34SMatthias Ringwald } 13545569c34SMatthias Ringwald } 13645569c34SMatthias Ringwald return NULL; 13745569c34SMatthias Ringwald } 13845569c34SMatthias Ringwald 139a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){ 1400235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1410235c9e5SMilanka Ringwald if (!instance){ 1420235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1430235c9e5SMilanka Ringwald return; 1440235c9e5SMilanka Ringwald } 1450235c9e5SMilanka Ringwald 146a4bfc4feSMatthias Ringwald if (!packet_handler) return; 147a4bfc4feSMatthias Ringwald uint8_t buffer[6]; 148a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 149a4bfc4feSMatthias Ringwald buffer[1] = 4; 150a4bfc4feSMatthias Ringwald buffer[2] = event; 151a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 152a4bfc4feSMatthias Ringwald buffer[5] = value; 153a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 154a4bfc4feSMatthias Ringwald } 155a4bfc4feSMatthias Ringwald 156f00767dfSMatthias 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){ 157f00767dfSMatthias Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 158f00767dfSMatthias Ringwald if (!instance){ 159f00767dfSMatthias Ringwald log_error("no instance for handle 0x%02x", con_handle); 160f00767dfSMatthias Ringwald return; 161f00767dfSMatthias Ringwald } 162f00767dfSMatthias Ringwald 163f00767dfSMatthias Ringwald if (!packet_handler) return; 164f00767dfSMatthias Ringwald uint8_t buffer[7]; 165f00767dfSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 166f00767dfSMatthias Ringwald buffer[1] = 4; 167f00767dfSMatthias Ringwald buffer[2] = event; 168f00767dfSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 169f00767dfSMatthias Ringwald buffer[5] = value_1; 170f00767dfSMatthias Ringwald buffer[6] = value_2; 171f00767dfSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 172f00767dfSMatthias Ringwald } 173f00767dfSMatthias Ringwald 1740235c9e5SMilanka Ringwald static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){ 1750235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1760235c9e5SMilanka Ringwald if (!instance){ 1770235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1780235c9e5SMilanka Ringwald return; 1790235c9e5SMilanka Ringwald } 1800235c9e5SMilanka Ringwald 1810235c9e5SMilanka Ringwald if (!packet_handler) return; 1820235c9e5SMilanka Ringwald uint8_t buffer[5]; 1830235c9e5SMilanka Ringwald buffer[0] = HCI_EVENT_HIDS_META; 1840235c9e5SMilanka Ringwald buffer[1] = 4; 1850235c9e5SMilanka Ringwald buffer[2] = event; 1860235c9e5SMilanka Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 1870235c9e5SMilanka Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 1880235c9e5SMilanka Ringwald } 1890235c9e5SMilanka Ringwald 190a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){ 191a4bfc4feSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 192a4bfc4feSMatthias Ringwald // notify client 1930235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1940235c9e5SMilanka Ringwald if (!instance){ 1950235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1960235c9e5SMilanka Ringwald return; 1970235c9e5SMilanka Ringwald } 1980235c9e5SMilanka Ringwald 199a4bfc4feSMatthias Ringwald if (!packet_handler) return; 200a4bfc4feSMatthias Ringwald uint8_t buffer[5]; 201a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 202a4bfc4feSMatthias Ringwald buffer[1] = 3; 203a4bfc4feSMatthias Ringwald buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW; 204a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 205a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 206a4bfc4feSMatthias Ringwald } 207a4bfc4feSMatthias Ringwald 208a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data 209a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value 210a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied 2110235c9e5SMilanka 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){ 2120235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 2130235c9e5SMilanka Ringwald if (!instance){ 2140235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 2150235c9e5SMilanka Ringwald return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 216a4bfc4feSMatthias Ringwald } 2170235c9e5SMilanka Ringwald 2180235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){ 2190235c9e5SMilanka Ringwald log_info("Read protocol mode"); 2200235c9e5SMilanka Ringwald return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size); 2210235c9e5SMilanka Ringwald } 222c9d24807SMilanka Ringwald 2230235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_map_handle){ 224523edd21SMatthias Ringwald log_info("Read report map"); 2250235c9e5SMilanka Ringwald return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size); 226a4bfc4feSMatthias Ringwald } 227c9d24807SMilanka Ringwald 2280235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 2290235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size); 230a4bfc4feSMatthias Ringwald } 231c9d24807SMilanka Ringwald 2320235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 2330235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size); 234a4bfc4feSMatthias Ringwald } 235c9d24807SMilanka Ringwald 2360235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){ 2374ea43905SMatthias Ringwald if (buffer && (buffer_size >= 1u)){ 2380235c9e5SMilanka Ringwald buffer[0] = instance->hid_control_point_suspend; 2390235c9e5SMilanka Ringwald } 2400235c9e5SMilanka Ringwald return 1; 241a4bfc4feSMatthias Ringwald } 242f4f6c196SMilanka Ringwald 243f4f6c196SMilanka Ringwald hids_device_report_t * report = hids_device_get_report_for_client_configuration_handle(instance, att_handle); 244f4f6c196SMilanka Ringwald if (report != NULL){ 245f4f6c196SMilanka Ringwald return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size); 246f4f6c196SMilanka Ringwald } 247a4bfc4feSMatthias Ringwald return 0; 248a4bfc4feSMatthias Ringwald } 249a4bfc4feSMatthias Ringwald 250a4bfc4feSMatthias 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){ 251a4bfc4feSMatthias Ringwald UNUSED(buffer_size); 252a4bfc4feSMatthias Ringwald UNUSED(offset); 253a4bfc4feSMatthias Ringwald 254689e7fb3SMatthias Ringwald if (transaction_mode != ATT_TRANSACTION_MODE_NONE){ 255689e7fb3SMatthias Ringwald return 0; 256689e7fb3SMatthias Ringwald } 257689e7fb3SMatthias Ringwald 2580235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 2590235c9e5SMilanka Ringwald if (!instance){ 2600235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 2610235c9e5SMilanka Ringwald return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 2620235c9e5SMilanka Ringwald } 2630235c9e5SMilanka Ringwald 2640235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 26585828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2660235c9e5SMilanka Ringwald instance->hid_boot_mouse_input_client_configuration_value = new_value; 267c1dbba9dSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 268a4bfc4feSMatthias Ringwald } 2690235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 27085828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2710235c9e5SMilanka Ringwald instance->hid_boot_keyboard_input_client_configuration_value = new_value; 272c1dbba9dSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 273a4bfc4feSMatthias Ringwald } 2740235c9e5SMilanka Ringwald 2750235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){ 2760235c9e5SMilanka Ringwald instance->hid_protocol_mode = buffer[0]; 2770235c9e5SMilanka Ringwald log_info("Set protocol mode: %u", instance->hid_protocol_mode); 2780235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode); 2790235c9e5SMilanka Ringwald } 2800235c9e5SMilanka Ringwald 2810235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){ 2824ea43905SMatthias Ringwald if (buffer_size < 1u){ 2830235c9e5SMilanka Ringwald return ATT_ERROR_INVALID_OFFSET; 2840235c9e5SMilanka Ringwald } 2850235c9e5SMilanka Ringwald instance->hid_control_point_suspend = buffer[0]; 2860235c9e5SMilanka Ringwald instance->con_handle = con_handle; 2870235c9e5SMilanka Ringwald log_info("Set suspend tp: %u", instance->hid_control_point_suspend ); 2884ea43905SMatthias Ringwald if (instance->hid_control_point_suspend == 0u){ 2890235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle); 2904ea43905SMatthias Ringwald } else if (instance->hid_control_point_suspend == 1u){ 2910235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle); 2920235c9e5SMilanka Ringwald } 2930235c9e5SMilanka Ringwald } 294f4f6c196SMilanka Ringwald 295*aa3c9a66SMatthias Ringwald hids_device_report_t * report; 296*aa3c9a66SMatthias Ringwald report = hids_device_get_report_for_value_handle(instance, att_handle); 297*aa3c9a66SMatthias Ringwald if (report != NULL){ 298*aa3c9a66SMatthias Ringwald // assemble event in buffer 299*aa3c9a66SMatthias Ringwald uint8_t event[257]; 300*aa3c9a66SMatthias Ringwald uint16_t pos = 0; 301*aa3c9a66SMatthias Ringwald event[pos++] = HCI_EVENT_HIDS_META; 302*aa3c9a66SMatthias Ringwald // skip length 303*aa3c9a66SMatthias Ringwald pos++; 304*aa3c9a66SMatthias Ringwald event[pos++] = HIDS_SUBEVENT_SET_REPORT; 305*aa3c9a66SMatthias Ringwald little_endian_store_16(event, pos, con_handle); 306*aa3c9a66SMatthias Ringwald pos += 2; 307*aa3c9a66SMatthias Ringwald event[pos++] = report->id; 308*aa3c9a66SMatthias Ringwald event[pos++] = (uint8_t) report->type; 309*aa3c9a66SMatthias Ringwald uint8_t length_to_copy = btstack_min(buffer_size, 250); 310*aa3c9a66SMatthias Ringwald event[pos++] = length_to_copy; 311*aa3c9a66SMatthias Ringwald memcpy(&event[pos], buffer, length_to_copy); 312*aa3c9a66SMatthias Ringwald pos += length_to_copy; 313*aa3c9a66SMatthias Ringwald // set event length 314*aa3c9a66SMatthias Ringwald event[1] = pos - 2; 315*aa3c9a66SMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 316*aa3c9a66SMatthias Ringwald return 0; 317*aa3c9a66SMatthias Ringwald } 318*aa3c9a66SMatthias Ringwald 319*aa3c9a66SMatthias Ringwald report = hids_device_get_report_for_client_configuration_handle(instance, att_handle); 320f4f6c196SMilanka Ringwald if (report != NULL){ 321f4f6c196SMilanka Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 322f4f6c196SMilanka Ringwald report->client_configuration_value = new_value; 323f4f6c196SMilanka Ringwald log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value); 324f4f6c196SMilanka Ringwald 325f4f6c196SMilanka Ringwald switch (report->type){ 326f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_INPUT: 327f00767dfSMatthias Ringwald hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value); 328f4f6c196SMilanka Ringwald break; 329f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_OUTPUT: 330f00767dfSMatthias Ringwald hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value); 331f4f6c196SMilanka Ringwald break; 332f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_FEATURE: 333f00767dfSMatthias Ringwald hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value); 334f4f6c196SMilanka Ringwald break; 335f4f6c196SMilanka Ringwald default: 336f4f6c196SMilanka Ringwald btstack_unreachable(); 337f4f6c196SMilanka Ringwald break; 338f4f6c196SMilanka Ringwald } 339f4f6c196SMilanka Ringwald } 340a4bfc4feSMatthias Ringwald return 0; 341a4bfc4feSMatthias Ringwald } 342a4bfc4feSMatthias Ringwald 343f823745aSMilanka Ringwald void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size, 344f823745aSMilanka Ringwald uint16_t num_reports, hids_device_report_t * report_storage){ 345f823745aSMilanka Ringwald 3460235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_create_instance(); 347a4bfc4feSMatthias Ringwald 348f823745aSMilanka Ringwald btstack_assert(num_reports > 0); 349f823745aSMilanka Ringwald btstack_assert(report_storage != NULL); 350f823745aSMilanka Ringwald 351f823745aSMilanka Ringwald instance->hid_country_code = hid_country_code; 352f823745aSMilanka Ringwald instance->hid_descriptor = hid_descriptor; 353f823745aSMilanka Ringwald instance->hid_descriptor_size = hid_descriptor_size; 354a4bfc4feSMatthias Ringwald 355a4bfc4feSMatthias Ringwald // default 3560235c9e5SMilanka Ringwald instance->hid_protocol_mode = 1; 357a4bfc4feSMatthias Ringwald 358a4bfc4feSMatthias Ringwald // get service handle range 359a4bfc4feSMatthias Ringwald uint16_t start_handle = 0; 360843359edSMatthias Ringwald uint16_t end_handle = 0xffff; 361c436b760SMilanka Ringwald int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle); 362a2489f29SMatthias Ringwald btstack_assert(service_found != 0); 363a2489f29SMatthias Ringwald UNUSED(service_found); 364a4bfc4feSMatthias Ringwald 365a4bfc4feSMatthias Ringwald // get report map handle 3660235c9e5SMilanka Ringwald instance->hid_report_map_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP); 367a4bfc4feSMatthias Ringwald 368a4bfc4feSMatthias Ringwald // get report map handle 3690235c9e5SMilanka 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); 370a4bfc4feSMatthias Ringwald 371a4bfc4feSMatthias Ringwald // get value and client configuration handles for boot mouse input, boot keyboard input and report input 3720235c9e5SMilanka 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); 3730235c9e5SMilanka 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); 374a4bfc4feSMatthias Ringwald 3750235c9e5SMilanka 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); 3760235c9e5SMilanka 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); 377a4bfc4feSMatthias Ringwald 3780235c9e5SMilanka 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); 3790235c9e5SMilanka Ringwald 380c9d24807SMilanka Ringwald log_info("hid_report_map_handle 0x%02x", instance->hid_report_map_handle); 381c9d24807SMilanka Ringwald log_info("hid_protocol_mode_value_handle 0x%02x", instance->hid_protocol_mode_value_handle); 382c9d24807SMilanka Ringwald log_info("hid_boot_mouse_input_value_handle 0x%02x", instance->hid_boot_mouse_input_value_handle); 383c9d24807SMilanka Ringwald log_info("hid_boot_mouse_input_client_configuration_handle 0x%02x", instance->hid_boot_mouse_input_client_configuration_handle); 384c9d24807SMilanka Ringwald log_info("hid_boot_keyboard_input_value_handle 0x%02x", instance->hid_boot_keyboard_input_value_handle); 385c9d24807SMilanka Ringwald log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle); 386c9d24807SMilanka Ringwald log_info("hid_control_point_value_handle 0x%02x", instance->hid_control_point_value_handle); 387a4bfc4feSMatthias Ringwald 388f823745aSMilanka Ringwald instance->hid_reports = report_storage; 389f4f6c196SMilanka Ringwald 390f823745aSMilanka Ringwald uint16_t hid_reports_num = num_reports; 391f4f6c196SMilanka Ringwald uint16_t assigned_reports_num = 0; 392f4f6c196SMilanka Ringwald uint16_t start_chr_handle = start_handle; 393f4f6c196SMilanka Ringwald 394f4f6c196SMilanka Ringwald while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) { 3952c84eaa3SMatthias Ringwald // mandatory 396f4f6c196SMilanka Ringwald uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 397f4f6c196SMilanka Ringwald if (chr_value_handle == 0){ 398f4f6c196SMilanka Ringwald break; 399f4f6c196SMilanka Ringwald } 400f4f6c196SMilanka Ringwald 4012c84eaa3SMatthias Ringwald // optional 402f4f6c196SMilanka 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); 4032c84eaa3SMatthias Ringwald 4042c84eaa3SMatthias Ringwald // mandatory 405f4f6c196SMilanka 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); 4062c84eaa3SMatthias Ringwald if (report_reference_handle == 0){ 4072c84eaa3SMatthias Ringwald break; 4082c84eaa3SMatthias Ringwald } 4092c84eaa3SMatthias Ringwald 4102c84eaa3SMatthias Ringwald // get report id and type from report reference 411f4f6c196SMilanka Ringwald uint16_t report_reference_value_len; 412f4f6c196SMilanka Ringwald const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len); 413f4f6c196SMilanka Ringwald if (report_reference_value == NULL){ 414f4f6c196SMilanka Ringwald break; 415f4f6c196SMilanka Ringwald } 416f4f6c196SMilanka Ringwald if (report_reference_value_len != 2){ 417f4f6c196SMilanka Ringwald break; 418f4f6c196SMilanka Ringwald } 4192c84eaa3SMatthias Ringwald uint8_t report_id = report_reference_value[0]; 4202c84eaa3SMatthias Ringwald hid_report_type_t report_type = (hid_report_type_t) report_reference_value[1]; 421f4f6c196SMilanka Ringwald 4222c84eaa3SMatthias Ringwald // store report info 423160eac51SMatthias Ringwald hids_device_report_t * report = &report_storage[assigned_reports_num]; 424f4f6c196SMilanka Ringwald report->value_handle = chr_value_handle; 425f4f6c196SMilanka Ringwald report->client_configuration_handle = chr_client_configuration_handle; 426f4f6c196SMilanka Ringwald report->client_configuration_value = 0; 4272c84eaa3SMatthias Ringwald report->id = report_id; 4282c84eaa3SMatthias Ringwald report->type = report_type; 429f4f6c196SMilanka Ringwald 430f4f6c196SMilanka Ringwald switch (report->type){ 431f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_INPUT: 432f4f6c196SMilanka Ringwald instance->hid_input_reports_num++; 433f4f6c196SMilanka Ringwald break; 434f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_OUTPUT: 435f4f6c196SMilanka Ringwald instance->hid_output_reports_num++; 436f4f6c196SMilanka Ringwald break; 437f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_FEATURE: 438f4f6c196SMilanka Ringwald instance->hid_feature_reports_num++; 439f4f6c196SMilanka Ringwald break; 440f4f6c196SMilanka Ringwald default: 441f4f6c196SMilanka Ringwald btstack_unreachable(); 442f4f6c196SMilanka Ringwald return; 443f4f6c196SMilanka Ringwald } 444f4f6c196SMilanka Ringwald log_info("hid_report_value_handle 0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type); 4452c84eaa3SMatthias Ringwald if (report->client_configuration_handle != 0){ 446f4f6c196SMilanka Ringwald log_info("hid_report_client_configuration_handle 0x%02x", report->client_configuration_handle); 4472c84eaa3SMatthias Ringwald } 448f4f6c196SMilanka Ringwald 449f4f6c196SMilanka Ringwald assigned_reports_num++; 4502c84eaa3SMatthias Ringwald start_chr_handle = report_reference_handle + 1; 451f4f6c196SMilanka Ringwald } 452f4f6c196SMilanka Ringwald 4533d71c7a4SMatthias Ringwald // register service with ATT Server 454a4bfc4feSMatthias Ringwald hid_service.start_handle = start_handle; 455a4bfc4feSMatthias Ringwald hid_service.end_handle = end_handle; 456a4bfc4feSMatthias Ringwald hid_service.read_callback = &att_read_callback; 457a4bfc4feSMatthias Ringwald hid_service.write_callback = &att_write_callback; 4583d71c7a4SMatthias Ringwald att_server_register_service_handler(&hid_service); 459a4bfc4feSMatthias Ringwald } 460a4bfc4feSMatthias Ringwald 461a4bfc4feSMatthias Ringwald /** 462f823745aSMilanka Ringwald * @brief Set up HIDS Device 463f823745aSMilanka Ringwald */ 464f823745aSMilanka Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){ 465160eac51SMatthias Ringwald uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t); 466160eac51SMatthias Ringwald hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage); 467f823745aSMilanka Ringwald } 468f823745aSMilanka Ringwald 469f823745aSMilanka Ringwald /** 470a4bfc4feSMatthias Ringwald * @brief Register callback for the HIDS Device client. 471a4bfc4feSMatthias Ringwald * @param callback 472a4bfc4feSMatthias Ringwald */ 473a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){ 474a4bfc4feSMatthias Ringwald packet_handler = callback; 475a4bfc4feSMatthias Ringwald } 476a4bfc4feSMatthias Ringwald 477a4bfc4feSMatthias Ringwald /** 478a4bfc4feSMatthias Ringwald * @brief Request can send now event to send HID Report 479a4bfc4feSMatthias Ringwald * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent 480a4bfc4feSMatthias Ringwald * @param hid_cid 481a4bfc4feSMatthias Ringwald */ 482a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){ 4830235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 4840235c9e5SMilanka Ringwald if (!instance){ 4850235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 4860235c9e5SMilanka Ringwald return; 4870235c9e5SMilanka Ringwald } 4880235c9e5SMilanka Ringwald 489086ea75fSMatthias Ringwald instance->can_send_now_callback.callback = &hids_device_can_send_now; 490086ea75fSMatthias Ringwald instance->can_send_now_callback.context = (void*) (uintptr_t) con_handle; 491086ea75fSMatthias Ringwald att_server_register_can_send_now_callback(&instance->can_send_now_callback, con_handle); 492a4bfc4feSMatthias Ringwald } 493a4bfc4feSMatthias Ringwald 49445569c34SMatthias 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){ 4950235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 4960235c9e5SMilanka Ringwald if (!instance){ 4970235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 498f4f6c196SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 4990235c9e5SMilanka Ringwald } 500f4f6c196SMilanka Ringwald 501a3c1c955SMatthias Ringwald hids_device_report_t * report_storage = hids_device_get_report_for_id_and_type(instance, report_id, 502a3c1c955SMatthias Ringwald HID_REPORT_TYPE_INPUT); 503f4f6c196SMilanka Ringwald if (report_storage == NULL){ 504f4f6c196SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 505f4f6c196SMilanka Ringwald } 506f4f6c196SMilanka Ringwald 507f4f6c196SMilanka Ringwald return att_server_notify(con_handle, report_storage->value_handle, report, report_len); 508f4f6c196SMilanka Ringwald } 509f4f6c196SMilanka Ringwald 510d38dfb7fSMatthias Ringwald uint8_t hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 511a90492ddSMatthias Ringwald hids_device_t * device = hids_device_get_instance_for_con_handle(con_handle); 512a90492ddSMatthias Ringwald if (!device){ 513f4f6c196SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 514f4f6c196SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 515f4f6c196SMilanka Ringwald } 516f4f6c196SMilanka Ringwald 517a90492ddSMatthias Ringwald uint8_t pos; 518a90492ddSMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 519a90492ddSMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){ 520a90492ddSMatthias Ringwald hids_device_report_t * report_storage = &device->hid_reports[pos]; 521d38dfb7fSMatthias Ringwald if (report_storage->type == HID_REPORT_TYPE_INPUT){ 522f4f6c196SMilanka Ringwald return att_server_notify(con_handle, report_storage->value_handle, report, report_len); 523f4f6c196SMilanka Ringwald } 524f4f6c196SMilanka Ringwald } 525f4f6c196SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 526f4f6c196SMilanka Ringwald } 527f4f6c196SMilanka Ringwald 528d38dfb7fSMatthias Ringwald /** 529d38dfb7fSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 530d38dfb7fSMatthias Ringwald */ 531d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 532d38dfb7fSMatthias Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 533d38dfb7fSMatthias Ringwald if (!instance){ 534d38dfb7fSMatthias Ringwald log_error("no instance for handle 0x%02x", con_handle); 535d38dfb7fSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 536d38dfb7fSMatthias Ringwald } 537d38dfb7fSMatthias Ringwald return att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len); 5380235c9e5SMilanka Ringwald } 5390235c9e5SMilanka Ringwald 5400235c9e5SMilanka Ringwald /** 541a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 542a4bfc4feSMatthias Ringwald */ 543d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 5440235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 5450235c9e5SMilanka Ringwald if (!instance){ 5460235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 547d38dfb7fSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 5480235c9e5SMilanka Ringwald } 549d38dfb7fSMatthias Ringwald return att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len); 550a4bfc4feSMatthias Ringwald } 551