1d40c9ac6SMatthias Ringwald /* 2d40c9ac6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3d40c9ac6SMatthias Ringwald * 4d40c9ac6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5d40c9ac6SMatthias Ringwald * modification, are permitted provided that the following conditions 6d40c9ac6SMatthias Ringwald * are met: 7d40c9ac6SMatthias Ringwald * 8d40c9ac6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9d40c9ac6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10d40c9ac6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11d40c9ac6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12d40c9ac6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13d40c9ac6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14d40c9ac6SMatthias Ringwald * contributors may be used to endorse or promote products derived 15d40c9ac6SMatthias Ringwald * from this software without specific prior written permission. 16d40c9ac6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17d40c9ac6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18d40c9ac6SMatthias Ringwald * monetary gain. 19d40c9ac6SMatthias Ringwald * 20d40c9ac6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21d40c9ac6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22d40c9ac6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23d40c9ac6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24d40c9ac6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25d40c9ac6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26d40c9ac6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27d40c9ac6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28d40c9ac6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29d40c9ac6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30d40c9ac6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31d40c9ac6SMatthias Ringwald * SUCH DAMAGE. 32d40c9ac6SMatthias Ringwald * 33d40c9ac6SMatthias Ringwald * Please inquire about commercial licensing options at 34d40c9ac6SMatthias Ringwald * [email protected] 35d40c9ac6SMatthias Ringwald * 36d40c9ac6SMatthias Ringwald */ 37d40c9ac6SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hid_device.c" 39d40c9ac6SMatthias Ringwald 40d40c9ac6SMatthias Ringwald #include <string.h> 41d40c9ac6SMatthias Ringwald 4284e3541eSMilanka Ringwald #include "bluetooth.h" 4384e3541eSMilanka Ringwald #include "bluetooth_psm.h" 4484e3541eSMilanka Ringwald #include "bluetooth_sdp.h" 4584e3541eSMilanka Ringwald #include "btstack_debug.h" 4684e3541eSMilanka Ringwald #include "btstack_event.h" 4784e3541eSMilanka Ringwald #include "btstack_hid_parser.h" 48d40c9ac6SMatthias Ringwald #include "classic/hid_device.h" 49d40c9ac6SMatthias Ringwald #include "classic/sdp_util.h" 508eb8d463SMatthias Ringwald #include "l2cap.h" 518eb8d463SMatthias Ringwald 52c12110e4SMilanka Ringwald typedef enum { 53c12110e4SMilanka Ringwald HID_DEVICE_IDLE, 54c12110e4SMilanka Ringwald HID_DEVICE_CONNECTED, 55ba9a58feSMilanka Ringwald HID_DEVICE_W2_GET_REPORT, 56ba9a58feSMilanka Ringwald HID_DEVICE_W2_SET_REPORT, 57ba9a58feSMilanka Ringwald HID_DEVICE_W2_GET_PROTOCOL, 58ba9a58feSMilanka Ringwald HID_DEVICE_W2_SET_PROTOCOL, 59ba9a58feSMilanka Ringwald HID_DEVICE_W2_ANSWER_SET_PROTOCOL, 60ba9a58feSMilanka Ringwald HID_DEVICE_W2_SEND_UNSUPPORTED_REQUEST, 61c12110e4SMilanka Ringwald } hid_device_state_t; 62c12110e4SMilanka Ringwald 638eb8d463SMatthias Ringwald // hid device state 648eb8d463SMatthias Ringwald typedef struct hid_device { 658eb8d463SMatthias Ringwald uint16_t cid; 668eb8d463SMatthias Ringwald bd_addr_t bd_addr; 678eb8d463SMatthias Ringwald hci_con_handle_t con_handle; 688eb8d463SMatthias Ringwald uint16_t control_cid; 698eb8d463SMatthias Ringwald uint16_t interrupt_cid; 708eb8d463SMatthias Ringwald uint8_t incoming; 7157c643eeSMatthias Ringwald uint8_t connected; 72c12110e4SMilanka Ringwald hid_device_state_t state; 73c12110e4SMilanka Ringwald hid_report_type_t report_type; 74c12110e4SMilanka Ringwald uint16_t report_id; 75a1118d11SMilanka Ringwald uint16_t expected_report_size; 76a1118d11SMilanka Ringwald uint16_t report_size; 77e99e41f0SMatthias Ringwald uint8_t user_request_can_send_now; 78ba9a58feSMilanka Ringwald 79bc967e00SMilanka Ringwald hid_handshake_param_type_t report_status; 80ba9a58feSMilanka Ringwald hid_protocol_mode_t protocol_mode; 818eb8d463SMatthias Ringwald } hid_device_t; 828eb8d463SMatthias Ringwald 838eb8d463SMatthias Ringwald static hid_device_t _hid_device; 84ba9a58feSMilanka Ringwald static uint8_t hid_boot_protocol_mode_supported; 85662cddc2SMilanka Ringwald static const uint8_t * hid_descriptor; 86662cddc2SMilanka Ringwald static uint16_t hid_descriptor_len; 87c12110e4SMilanka Ringwald 88a1118d11SMilanka Ringwald static int dummy_write_report(uint16_t hid_cid, hid_report_type_t report_type, uint16_t report_id, int * out_report_size, uint8_t * out_report){ 89c12110e4SMilanka Ringwald UNUSED(hid_cid); 90c12110e4SMilanka Ringwald UNUSED(report_type); 91c12110e4SMilanka Ringwald UNUSED(report_id); 92c12110e4SMilanka Ringwald UNUSED(out_report_size); 93c12110e4SMilanka Ringwald UNUSED(out_report); 94a1118d11SMilanka Ringwald return -1; 95c12110e4SMilanka Ringwald } 964dfe6a8bSMilanka Ringwald 974dfe6a8bSMilanka Ringwald static void dummy_set_report(uint16_t hid_cid, hid_report_type_t report_type, int report_size, uint8_t * report){ 98ba9a58feSMilanka Ringwald UNUSED(hid_cid); 99ba9a58feSMilanka Ringwald UNUSED(report_type); 100ba9a58feSMilanka Ringwald UNUSED(report_size); 101ba9a58feSMilanka Ringwald UNUSED(report); 102ba9a58feSMilanka Ringwald } 1034dfe6a8bSMilanka Ringwald 1047d26fe66SMilanka Ringwald static void dummy_report_data(uint16_t hid_cid, hid_report_type_t report_type, uint16_t report_id, int report_size, uint8_t * report){ 1057d26fe66SMilanka Ringwald UNUSED(hid_cid); 1067d26fe66SMilanka Ringwald UNUSED(report_type); 1077d26fe66SMilanka Ringwald UNUSED(report_id); 1087d26fe66SMilanka Ringwald UNUSED(report_size); 1097d26fe66SMilanka Ringwald UNUSED(report); 1107d26fe66SMilanka Ringwald } 1117d26fe66SMilanka Ringwald 112a1118d11SMilanka Ringwald static int (*hci_device_get_report) (uint16_t hid_cid, hid_report_type_t report_type, uint16_t report_id, int * out_report_size, uint8_t * out_report) = dummy_write_report; 1134dfe6a8bSMilanka Ringwald static void (*hci_device_set_report) (uint16_t hid_cid, hid_report_type_t report_type, int report_size, uint8_t * report) = dummy_set_report; 1147d26fe66SMilanka Ringwald static void (*hci_device_report_data) (uint16_t hid_cid, hid_report_type_t report_type, uint16_t report_id, int report_size, uint8_t * report) = dummy_report_data; 1158eb8d463SMatthias Ringwald 1168eb8d463SMatthias Ringwald static btstack_packet_handler_t hid_callback; 117d40c9ac6SMatthias Ringwald 118c12110e4SMilanka Ringwald static uint16_t hid_device_cid = 0; 119c12110e4SMilanka Ringwald 120c12110e4SMilanka Ringwald static uint16_t hid_device_get_next_cid(void){ 121c12110e4SMilanka Ringwald hid_device_cid++; 122c12110e4SMilanka Ringwald if (!hid_device_cid){ 123c12110e4SMilanka Ringwald hid_device_cid = 1; 124c12110e4SMilanka Ringwald } 125c12110e4SMilanka Ringwald return hid_device_cid; 126c12110e4SMilanka Ringwald } 127c12110e4SMilanka Ringwald 128c12110e4SMilanka Ringwald // TODO: store hid device connection into list 1294176cbc0SMatthias Ringwald static hid_device_t * hid_device_get_instance_for_l2cap_cid(uint16_t cid){ 130c1ab6cc1SMatthias Ringwald if ((_hid_device.control_cid == cid) || (_hid_device.interrupt_cid == cid)){ 1314176cbc0SMatthias Ringwald return &_hid_device; 1324176cbc0SMatthias Ringwald } 1334176cbc0SMatthias Ringwald return NULL; 1344176cbc0SMatthias Ringwald } 1354176cbc0SMatthias Ringwald 1364176cbc0SMatthias Ringwald static hid_device_t * hid_device_get_instance_for_hid_cid(uint16_t hid_cid){ 1374176cbc0SMatthias Ringwald if (_hid_device.cid == hid_cid){ 138c12110e4SMilanka Ringwald return &_hid_device; 139c12110e4SMilanka Ringwald } 140c12110e4SMilanka Ringwald return NULL; 141c12110e4SMilanka Ringwald } 142c12110e4SMilanka Ringwald 14372e3f392SMatthias Ringwald static hid_device_t * hid_device_provide_instance_for_bd_addr(bd_addr_t bd_addr){ 1446510739bSMilanka Ringwald if (!_hid_device.cid){ 1456535961aSMatthias Ringwald (void)memcpy(_hid_device.bd_addr, bd_addr, 6); 1466510739bSMilanka Ringwald _hid_device.cid = hid_device_get_next_cid(); 147ba9a58feSMilanka Ringwald _hid_device.protocol_mode = HID_PROTOCOL_MODE_REPORT; 14872e3f392SMatthias Ringwald _hid_device.con_handle = HCI_CON_HANDLE_INVALID; 1496510739bSMilanka Ringwald } 150c12110e4SMilanka Ringwald return &_hid_device; 151c12110e4SMilanka Ringwald } 152c12110e4SMilanka Ringwald 153c12110e4SMilanka Ringwald static hid_device_t * hid_device_create_instance(void){ 154ba9a58feSMilanka Ringwald 155c12110e4SMilanka Ringwald return &_hid_device; 156c12110e4SMilanka Ringwald } 157c12110e4SMilanka Ringwald 158d40c9ac6SMatthias Ringwald void hid_create_sdp_record( 159d40c9ac6SMatthias Ringwald uint8_t *service, 160d40c9ac6SMatthias Ringwald uint32_t service_record_handle, 161d40c9ac6SMatthias Ringwald uint16_t hid_device_subclass, 162d40c9ac6SMatthias Ringwald uint8_t hid_country_code, 163d40c9ac6SMatthias Ringwald uint8_t hid_virtual_cable, 164*1ad99f3bSMilanka Ringwald uint8_t hid_remote_wake, 165d40c9ac6SMatthias Ringwald uint8_t hid_reconnect_initiate, 166d40c9ac6SMatthias Ringwald uint8_t hid_boot_device, 167662cddc2SMilanka Ringwald const uint8_t * descriptor, uint16_t descriptor_size, 168d40c9ac6SMatthias Ringwald const char *device_name){ 169d40c9ac6SMatthias Ringwald 170d40c9ac6SMatthias Ringwald uint8_t * attribute; 171d40c9ac6SMatthias Ringwald de_create_sequence(service); 172d40c9ac6SMatthias Ringwald 173d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 174d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 175d40c9ac6SMatthias Ringwald 176d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 177d40c9ac6SMatthias Ringwald attribute = de_push_sequence(service); 178d40c9ac6SMatthias Ringwald { 179d40c9ac6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); 180d40c9ac6SMatthias Ringwald } 181d40c9ac6SMatthias Ringwald de_pop_sequence(service, attribute); 182d40c9ac6SMatthias Ringwald 183d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 184d40c9ac6SMatthias Ringwald attribute = de_push_sequence(service); 185d40c9ac6SMatthias Ringwald { 186d40c9ac6SMatthias Ringwald uint8_t * l2cpProtocol = de_push_sequence(attribute); 187d40c9ac6SMatthias Ringwald { 188d40c9ac6SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 18984e3541eSMilanka Ringwald de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_HID_CONTROL); 190d40c9ac6SMatthias Ringwald } 191d40c9ac6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 192d40c9ac6SMatthias Ringwald 193d40c9ac6SMatthias Ringwald uint8_t * hidProtocol = de_push_sequence(attribute); 194d40c9ac6SMatthias Ringwald { 195d40c9ac6SMatthias Ringwald de_add_number(hidProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_HIDP); 196d40c9ac6SMatthias Ringwald } 197d40c9ac6SMatthias Ringwald de_pop_sequence(attribute, hidProtocol); 198d40c9ac6SMatthias Ringwald } 199d40c9ac6SMatthias Ringwald de_pop_sequence(service, attribute); 200d40c9ac6SMatthias Ringwald 201d40c9ac6SMatthias Ringwald // TODO? 202d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_LANGUAGE_BASE_ATTRIBUTE_ID_LIST); 203d40c9ac6SMatthias Ringwald attribute = de_push_sequence(service); 204d40c9ac6SMatthias Ringwald { 205d40c9ac6SMatthias Ringwald de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e); 206d40c9ac6SMatthias Ringwald de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a); 207d40c9ac6SMatthias Ringwald de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100); 208d40c9ac6SMatthias Ringwald } 209d40c9ac6SMatthias Ringwald de_pop_sequence(service, attribute); 210d40c9ac6SMatthias Ringwald 211d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS); 212d40c9ac6SMatthias Ringwald attribute = de_push_sequence(service); 213d40c9ac6SMatthias Ringwald { 214d40c9ac6SMatthias Ringwald uint8_t * additionalDescriptorAttribute = de_push_sequence(attribute); 215d40c9ac6SMatthias Ringwald { 216d40c9ac6SMatthias Ringwald uint8_t * l2cpProtocol = de_push_sequence(additionalDescriptorAttribute); 217d40c9ac6SMatthias Ringwald { 218d40c9ac6SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 21984e3541eSMilanka Ringwald de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_HID_INTERRUPT); 220d40c9ac6SMatthias Ringwald } 221d40c9ac6SMatthias Ringwald de_pop_sequence(additionalDescriptorAttribute, l2cpProtocol); 222d40c9ac6SMatthias Ringwald 2239fa0921fSMatthias Ringwald uint8_t * hidProtocol = de_push_sequence(additionalDescriptorAttribute); 224d40c9ac6SMatthias Ringwald { 225d40c9ac6SMatthias Ringwald de_add_number(hidProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_HIDP); 226d40c9ac6SMatthias Ringwald } 2279fa0921fSMatthias Ringwald de_pop_sequence(additionalDescriptorAttribute, hidProtocol); 228d40c9ac6SMatthias Ringwald } 229d40c9ac6SMatthias Ringwald de_pop_sequence(attribute, additionalDescriptorAttribute); 230d40c9ac6SMatthias Ringwald } 231d40c9ac6SMatthias Ringwald de_pop_sequence(service, attribute); 232d40c9ac6SMatthias Ringwald 233d40c9ac6SMatthias Ringwald // 0x0100 "ServiceName" 234d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 235d40c9ac6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(device_name), (uint8_t *) device_name); 236d40c9ac6SMatthias Ringwald 237d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 238d40c9ac6SMatthias Ringwald attribute = de_push_sequence(service); 239d40c9ac6SMatthias Ringwald { 240d40c9ac6SMatthias Ringwald uint8_t * hidProfile = de_push_sequence(attribute); 241d40c9ac6SMatthias Ringwald { 242d40c9ac6SMatthias Ringwald de_add_number(hidProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); 243d40c9ac6SMatthias Ringwald de_add_number(hidProfile, DE_UINT, DE_SIZE_16, 0x0101); // Version 1.1 244d40c9ac6SMatthias Ringwald } 245d40c9ac6SMatthias Ringwald de_pop_sequence(attribute, hidProfile); 246d40c9ac6SMatthias Ringwald } 247d40c9ac6SMatthias Ringwald de_pop_sequence(service, attribute); 248d40c9ac6SMatthias Ringwald 249d40c9ac6SMatthias Ringwald // Deprecated in v1.1.1 250d40c9ac6SMatthias Ringwald // de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DEVICE_RELEASE_NUMBER); 251d40c9ac6SMatthias Ringwald // de_add_number(service, DE_UINT, DE_SIZE_16, 0x0101); 252d40c9ac6SMatthias Ringwald 253d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_PARSER_VERSION); 254d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0111); // v1.1.1 255d40c9ac6SMatthias Ringwald 256d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DEVICE_SUBCLASS); 2579679ea81SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, hid_device_subclass); 258d40c9ac6SMatthias Ringwald 259d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_COUNTRY_CODE); 2609679ea81SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, hid_country_code); 261d40c9ac6SMatthias Ringwald 262d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_VIRTUAL_CABLE); 263d40c9ac6SMatthias Ringwald de_add_number(service, DE_BOOL, DE_SIZE_8, hid_virtual_cable); 264d40c9ac6SMatthias Ringwald 265d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_RECONNECT_INITIATE); 266d40c9ac6SMatthias Ringwald de_add_number(service, DE_BOOL, DE_SIZE_8, hid_reconnect_initiate); 267d40c9ac6SMatthias Ringwald 268d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DESCRIPTOR_LIST); 269d40c9ac6SMatthias Ringwald attribute = de_push_sequence(service); 270d40c9ac6SMatthias Ringwald { 271d40c9ac6SMatthias Ringwald uint8_t* hidDescriptor = de_push_sequence(attribute); 272d40c9ac6SMatthias Ringwald { 273d40c9ac6SMatthias Ringwald de_add_number(hidDescriptor, DE_UINT, DE_SIZE_8, 0x22); // Report Descriptor 274662cddc2SMilanka Ringwald de_add_data(hidDescriptor, DE_STRING, descriptor_size, (uint8_t *) descriptor); 275d40c9ac6SMatthias Ringwald } 276d40c9ac6SMatthias Ringwald de_pop_sequence(attribute, hidDescriptor); 277d40c9ac6SMatthias Ringwald } 278d40c9ac6SMatthias Ringwald de_pop_sequence(service, attribute); 279d40c9ac6SMatthias Ringwald 2809679ea81SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HIDLANGID_BASE_LIST); 2819679ea81SMatthias Ringwald attribute = de_push_sequence(service); 2829679ea81SMatthias Ringwald { 2839679ea81SMatthias Ringwald uint8_t* hig_lang_base = de_push_sequence(attribute); 2849679ea81SMatthias Ringwald { 2859679ea81SMatthias Ringwald // see: http://www.usb.org/developers/docs/USB_LANGIDs.pdf 2869679ea81SMatthias Ringwald de_add_number(hig_lang_base, DE_UINT, DE_SIZE_16, 0x0409); // HIDLANGID = English (US) 2879679ea81SMatthias Ringwald de_add_number(hig_lang_base, DE_UINT, DE_SIZE_16, 0x0100); // HIDLanguageBase = 0x0100 default 2889679ea81SMatthias Ringwald } 2899679ea81SMatthias Ringwald de_pop_sequence(attribute, hig_lang_base); 2909679ea81SMatthias Ringwald } 2919679ea81SMatthias Ringwald de_pop_sequence(service, attribute); 2929679ea81SMatthias Ringwald 2936510739bSMilanka Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_REMOTE_WAKE); 2946510739bSMilanka Ringwald de_add_number(service, DE_BOOL, DE_SIZE_8, hid_remote_wake); 2956510739bSMilanka Ringwald 296d40c9ac6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_BOOT_DEVICE); 297d40c9ac6SMatthias Ringwald de_add_number(service, DE_BOOL, DE_SIZE_8, hid_boot_device); 298d40c9ac6SMatthias Ringwald } 2998eb8d463SMatthias Ringwald 3008eb8d463SMatthias Ringwald static inline void hid_device_emit_connected_event(hid_device_t * context, uint8_t status){ 3018eb8d463SMatthias Ringwald uint8_t event[15]; 3028eb8d463SMatthias Ringwald int pos = 0; 3038eb8d463SMatthias Ringwald event[pos++] = HCI_EVENT_HID_META; 3048eb8d463SMatthias Ringwald pos++; // skip len 3058eb8d463SMatthias Ringwald event[pos++] = HID_SUBEVENT_CONNECTION_OPENED; 3068eb8d463SMatthias Ringwald little_endian_store_16(event,pos,context->cid); 3078eb8d463SMatthias Ringwald pos+=2; 3088eb8d463SMatthias Ringwald event[pos++] = status; 3096510739bSMilanka Ringwald reverse_bd_addr(context->bd_addr, &event[pos]); 3108eb8d463SMatthias Ringwald pos += 6; 3118eb8d463SMatthias Ringwald little_endian_store_16(event,pos,context->con_handle); 3128eb8d463SMatthias Ringwald pos += 2; 3138eb8d463SMatthias Ringwald event[pos++] = context->incoming; 3148eb8d463SMatthias Ringwald event[1] = pos - 2; 3158eb8d463SMatthias Ringwald hid_callback(HCI_EVENT_PACKET, context->cid, &event[0], pos); 3168eb8d463SMatthias Ringwald } 3178eb8d463SMatthias Ringwald 3186510739bSMilanka Ringwald static inline void hid_device_emit_event(hid_device_t * context, uint8_t subevent_type){ 319517089d1SMilanka Ringwald uint8_t event[5]; 3206510739bSMilanka Ringwald int pos = 0; 3216510739bSMilanka Ringwald event[pos++] = HCI_EVENT_HID_META; 3226510739bSMilanka Ringwald pos++; // skip len 3236510739bSMilanka Ringwald event[pos++] = subevent_type; 3246510739bSMilanka Ringwald little_endian_store_16(event,pos,context->cid); 3256510739bSMilanka Ringwald pos+=2; 3266510739bSMilanka Ringwald event[1] = pos - 2; 3276510739bSMilanka Ringwald hid_callback(HCI_EVENT_PACKET, context->cid, &event[0], pos); 3286510739bSMilanka Ringwald } 3296510739bSMilanka Ringwald 330c8cb324eSMilanka Ringwald static int hid_report_size_valid(uint16_t cid, int report_id, hid_report_type_t report_type, int report_size){ 331c8cb324eSMilanka Ringwald if (!report_size) return 0; 332c8cb324eSMilanka Ringwald if (hid_device_in_boot_protocol_mode(cid)){ 333c8cb324eSMilanka Ringwald switch (report_id){ 334c8cb324eSMilanka Ringwald case HID_BOOT_MODE_KEYBOARD_ID: 335c8cb324eSMilanka Ringwald if (report_size < 8) return 0; 336c8cb324eSMilanka Ringwald break; 337c8cb324eSMilanka Ringwald case HID_BOOT_MODE_MOUSE_ID: 338c8cb324eSMilanka Ringwald if (report_size < 1) return 0; 339c8cb324eSMilanka Ringwald break; 340c8cb324eSMilanka Ringwald default: 341c8cb324eSMilanka Ringwald return 0; 342c8cb324eSMilanka Ringwald } 343c8cb324eSMilanka Ringwald } else { 344c8cb324eSMilanka Ringwald int size = btstack_hid_get_report_size_for_id(report_id, report_type, hid_descriptor_len, hid_descriptor); 345c1ab6cc1SMatthias Ringwald if ((size == 0) || (size != report_size)) return 0; 346c8cb324eSMilanka Ringwald } 347c8cb324eSMilanka Ringwald return 1; 348c8cb324eSMilanka Ringwald } 349c8cb324eSMilanka Ringwald 350a796c06fSMilanka Ringwald static int hid_get_report_size_for_id(uint16_t cid, int report_id, hid_report_type_t report_type, uint16_t descriptor_len, const uint8_t * descriptor){ 351c8cb324eSMilanka Ringwald if (hid_device_in_boot_protocol_mode(cid)){ 352c8cb324eSMilanka Ringwald switch (report_id){ 353c8cb324eSMilanka Ringwald case HID_BOOT_MODE_KEYBOARD_ID: 354c8cb324eSMilanka Ringwald return 8; 355c8cb324eSMilanka Ringwald case HID_BOOT_MODE_MOUSE_ID: 356c8cb324eSMilanka Ringwald return 3; 357c8cb324eSMilanka Ringwald default: 358c8cb324eSMilanka Ringwald return 0; 359c8cb324eSMilanka Ringwald } 360c8cb324eSMilanka Ringwald } else { 361a796c06fSMilanka Ringwald return btstack_hid_get_report_size_for_id(report_id, report_type, descriptor_len, descriptor); 362c8cb324eSMilanka Ringwald } 363c8cb324eSMilanka Ringwald } 364c8cb324eSMilanka Ringwald 365c8cb324eSMilanka Ringwald static hid_report_id_status_t hid_report_id_status(uint16_t cid, uint16_t report_id){ 366c8cb324eSMilanka Ringwald if (hid_device_in_boot_protocol_mode(cid)){ 367c8cb324eSMilanka Ringwald switch (report_id){ 368c8cb324eSMilanka Ringwald case HID_BOOT_MODE_KEYBOARD_ID: 369c8cb324eSMilanka Ringwald case HID_BOOT_MODE_MOUSE_ID: 370c8cb324eSMilanka Ringwald return HID_REPORT_ID_VALID; 371c8cb324eSMilanka Ringwald default: 372c8cb324eSMilanka Ringwald return HID_REPORT_ID_INVALID; 373c8cb324eSMilanka Ringwald } 374c8cb324eSMilanka Ringwald } else { 375c8cb324eSMilanka Ringwald return btstack_hid_id_valid(report_id, hid_descriptor_len, hid_descriptor); 376c8cb324eSMilanka Ringwald } 377c8cb324eSMilanka Ringwald } 378c8cb324eSMilanka Ringwald 3794dfe6a8bSMilanka Ringwald static hid_handshake_param_type_t hid_device_set_report_cmd_is_valid(uint16_t cid, hid_report_type_t report_type, int report_size, uint8_t * report){ 3804dfe6a8bSMilanka Ringwald int pos = 0; 381c8cb324eSMilanka Ringwald int report_id = 0; 382c8cb324eSMilanka Ringwald 383c8cb324eSMilanka Ringwald if (btstack_hid_report_id_declared(hid_descriptor_len, hid_descriptor)){ 384c8cb324eSMilanka Ringwald report_id = report[pos++]; 3854dfe6a8bSMilanka Ringwald hid_report_id_status_t report_id_status = hid_report_id_status(cid, report_id); 3864dfe6a8bSMilanka Ringwald switch (report_id_status){ 3874dfe6a8bSMilanka Ringwald case HID_REPORT_ID_INVALID: 3884dfe6a8bSMilanka Ringwald return HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_REPORT_ID; 3894dfe6a8bSMilanka Ringwald default: 3904dfe6a8bSMilanka Ringwald break; 3914dfe6a8bSMilanka Ringwald } 392c8cb324eSMilanka Ringwald } 3934dfe6a8bSMilanka Ringwald 3944dfe6a8bSMilanka Ringwald if (!hid_report_size_valid(cid, report_id, report_type, report_size-pos)){ 395c8cb324eSMilanka Ringwald // TODO clarify DCT/BI-03c 3964dfe6a8bSMilanka Ringwald return HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 3974dfe6a8bSMilanka Ringwald } 3984dfe6a8bSMilanka Ringwald return HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL; 3994dfe6a8bSMilanka Ringwald } 4004dfe6a8bSMilanka Ringwald 4018eb8d463SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ 4028eb8d463SMatthias Ringwald UNUSED(channel); 4038eb8d463SMatthias Ringwald UNUSED(packet_size); 4048eb8d463SMatthias Ringwald int connected_before; 40557c643eeSMatthias Ringwald uint16_t psm; 40657c643eeSMatthias Ringwald uint8_t status; 407c12110e4SMilanka Ringwald hid_device_t * device = NULL; 4086510739bSMilanka Ringwald uint8_t param; 4096510739bSMilanka Ringwald bd_addr_t address; 4107d26fe66SMilanka Ringwald uint16_t local_cid; 411a1118d11SMilanka Ringwald int pos = 0; 412ba9a58feSMilanka Ringwald int report_size; 413a1118d11SMilanka Ringwald uint8_t report[48]; 414ca3f868cSMilanka Ringwald hid_message_type_t message_type; 415c12110e4SMilanka Ringwald 4168eb8d463SMatthias Ringwald switch (packet_type){ 417c12110e4SMilanka Ringwald case L2CAP_DATA_PACKET: 4184176cbc0SMatthias Ringwald device = hid_device_get_instance_for_l2cap_cid(channel); 419c12110e4SMilanka Ringwald if (!device) { 420c12110e4SMilanka Ringwald log_error("no device with cid 0x%02x", channel); 421c12110e4SMilanka Ringwald return; 422c12110e4SMilanka Ringwald } 423ca3f868cSMilanka Ringwald message_type = (hid_message_type_t)(packet[0] >> 4); 424c12110e4SMilanka Ringwald switch (message_type){ 425c12110e4SMilanka Ringwald case HID_MESSAGE_TYPE_GET_REPORT: 42607678896SMilanka Ringwald 427a1118d11SMilanka Ringwald pos = 0; 428ca3f868cSMilanka Ringwald device->report_type = (hid_report_type_t)(packet[pos++] & 0x03); 429c12110e4SMilanka Ringwald device->report_id = 0; 4307d26fe66SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL; 431a1118d11SMilanka Ringwald device->state = HID_DEVICE_W2_GET_REPORT; 4327d26fe66SMilanka Ringwald 4337d26fe66SMilanka Ringwald switch (device->protocol_mode){ 4347d26fe66SMilanka Ringwald case HID_PROTOCOL_MODE_BOOT: 435c8cb324eSMilanka Ringwald if (packet_size < 2){ 436ba9a58feSMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 437ba9a58feSMilanka Ringwald break; 438ba9a58feSMilanka Ringwald } 439a1118d11SMilanka Ringwald device->report_id = packet[pos++]; 4407d26fe66SMilanka Ringwald break; 4417d26fe66SMilanka Ringwald case HID_PROTOCOL_MODE_REPORT: 442a1118d11SMilanka Ringwald if (!btstack_hid_report_id_declared(hid_descriptor_len, hid_descriptor)) { 443c8cb324eSMilanka Ringwald if (packet_size < 2) break; 444c8cb324eSMilanka Ringwald if (packet[0] & 0x08){ 445c8cb324eSMilanka Ringwald if (packet_size > 2) { 446c8cb324eSMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_REPORT_ID; 447c8cb324eSMilanka Ringwald } 448c8cb324eSMilanka Ringwald } else { 449c8cb324eSMilanka Ringwald if (packet_size > 1) { 450c8cb324eSMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_REPORT_ID; 451c8cb324eSMilanka Ringwald } 452c8cb324eSMilanka Ringwald } 453a1118d11SMilanka Ringwald break; 454a1118d11SMilanka Ringwald } 4557d26fe66SMilanka Ringwald if (packet_size < 2){ 4567d26fe66SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 4577d26fe66SMilanka Ringwald break; 4587d26fe66SMilanka Ringwald } 459a1118d11SMilanka Ringwald device->report_id = packet[pos++]; 460c8cb324eSMilanka Ringwald break; 4617bbeb3adSMilanka Ringwald default: 4627bbeb3adSMilanka Ringwald btstack_assert(false); 4637bbeb3adSMilanka Ringwald break; 464c8cb324eSMilanka Ringwald } 465c8cb324eSMilanka Ringwald if (device->report_status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 466e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event(device->control_cid); 467c8cb324eSMilanka Ringwald break; 468c8cb324eSMilanka Ringwald } 469ca3f868cSMilanka Ringwald switch (hid_report_id_status(device->cid, device->report_id)){ 470a1118d11SMilanka Ringwald case HID_REPORT_ID_INVALID: 471a1118d11SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_REPORT_ID; 472a1118d11SMilanka Ringwald break; 473a1118d11SMilanka Ringwald default: 4747d26fe66SMilanka Ringwald break; 4757d26fe66SMilanka Ringwald } 476c8cb324eSMilanka Ringwald 477c8cb324eSMilanka Ringwald device->expected_report_size = hid_get_report_size_for_id(device->cid, device->report_id, device->report_type, hid_descriptor_len, hid_descriptor); 478a1118d11SMilanka Ringwald report_size = device->expected_report_size + pos; // add 1 for header size and report id 479a1118d11SMilanka Ringwald 480a1df452eSMatthias Ringwald if ((packet[0] & 0x08) && (packet_size >= (pos + 1))){ 481a1118d11SMilanka Ringwald device->report_size = btstack_min(btstack_min(little_endian_read_16(packet, pos), report_size), sizeof(report)); 482a1118d11SMilanka Ringwald } else { 483a1118d11SMilanka Ringwald device->report_size = btstack_min(btstack_min(l2cap_max_mtu(), report_size), sizeof(report)); 484a1118d11SMilanka Ringwald } 485a1118d11SMilanka Ringwald 486e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event(device->control_cid); 4877d26fe66SMilanka Ringwald break; 4887d26fe66SMilanka Ringwald 4897d26fe66SMilanka Ringwald case HID_MESSAGE_TYPE_SET_REPORT: 4907d26fe66SMilanka Ringwald device->state = HID_DEVICE_W2_SET_REPORT; 491a1118d11SMilanka Ringwald device->report_size = l2cap_max_mtu(); 492ca3f868cSMilanka Ringwald device->report_type = (hid_report_type_t)(packet[0] & 0x03); 4937d26fe66SMilanka Ringwald if (packet_size < 1){ 4947d26fe66SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 4957d26fe66SMilanka Ringwald break; 4967d26fe66SMilanka Ringwald } 4977d26fe66SMilanka Ringwald 4987d26fe66SMilanka Ringwald switch (device->protocol_mode){ 4997d26fe66SMilanka Ringwald case HID_PROTOCOL_MODE_BOOT: 5007d26fe66SMilanka Ringwald if (packet_size < 3){ 5017d26fe66SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 5027d26fe66SMilanka Ringwald break; 5037d26fe66SMilanka Ringwald } 5047d26fe66SMilanka Ringwald device->report_id = packet[1]; 5054dfe6a8bSMilanka Ringwald device->report_status = hid_device_set_report_cmd_is_valid(device->cid, device->report_type, packet_size - 1, &packet[1]); 5064dfe6a8bSMilanka Ringwald if (device->report_status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL) break; 5074dfe6a8bSMilanka Ringwald (*hci_device_set_report)(device->cid, device->report_type, packet_size-1, &packet[1]); 5087d26fe66SMilanka Ringwald break; 5097d26fe66SMilanka Ringwald case HID_PROTOCOL_MODE_REPORT: 5104dfe6a8bSMilanka Ringwald device->report_status = hid_device_set_report_cmd_is_valid(device->cid, device->report_type, packet_size - 1, &packet[1]); 5114dfe6a8bSMilanka Ringwald if (device->report_status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL) break; 5124dfe6a8bSMilanka Ringwald 5137d26fe66SMilanka Ringwald if (packet_size >= 2){ 5144dfe6a8bSMilanka Ringwald (*hci_device_set_report)(device->cid, device->report_type, packet_size-1, &packet[1]); 5157d26fe66SMilanka Ringwald } else { 5167d26fe66SMilanka Ringwald uint8_t payload[] = {0}; 5174dfe6a8bSMilanka Ringwald (*hci_device_set_report)(device->cid, device->report_type, 1, payload); 5187d26fe66SMilanka Ringwald } 5197d26fe66SMilanka Ringwald break; 5207bbeb3adSMilanka Ringwald default: 5217bbeb3adSMilanka Ringwald btstack_assert(false); 5227bbeb3adSMilanka Ringwald break; 5237d26fe66SMilanka Ringwald } 524ca3f868cSMilanka Ringwald device->report_type = (hid_report_type_t)(packet[0] & 0x03); 525e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event(device->control_cid); 526ba9a58feSMilanka Ringwald break; 527ba9a58feSMilanka Ringwald case HID_MESSAGE_TYPE_GET_PROTOCOL: 528ba9a58feSMilanka Ringwald device->state = HID_DEVICE_W2_GET_PROTOCOL; 529ba9a58feSMilanka Ringwald if (packet_size != 1) { 530ba9a58feSMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 531ba9a58feSMilanka Ringwald break; 532ba9a58feSMilanka Ringwald } 533ba9a58feSMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL; 534df3cc7a0SMilanka Ringwald // hid_device_request_can_send_now_event(channel); 535df3cc7a0SMilanka Ringwald l2cap_request_can_send_now_event(device->control_cid); 5367d26fe66SMilanka Ringwald break; 537ba9a58feSMilanka Ringwald 538ba9a58feSMilanka Ringwald case HID_MESSAGE_TYPE_SET_PROTOCOL: 539ba9a58feSMilanka Ringwald device->state = HID_DEVICE_W2_SET_PROTOCOL; 540ba9a58feSMilanka Ringwald if (packet_size != 1) { 541ba9a58feSMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 542ba9a58feSMilanka Ringwald break; 543ba9a58feSMilanka Ringwald } 544ba9a58feSMilanka Ringwald param = packet[0] & 0x01; 545c1ab6cc1SMatthias Ringwald if ((param == HID_PROTOCOL_MODE_BOOT) && !hid_boot_protocol_mode_supported){ 546ba9a58feSMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 547ba9a58feSMilanka Ringwald break; 548ba9a58feSMilanka Ringwald } 549ca3f868cSMilanka Ringwald device->protocol_mode = (hid_protocol_mode_t) param; 5507d26fe66SMilanka Ringwald switch (device->protocol_mode){ 5517d26fe66SMilanka Ringwald case HID_PROTOCOL_MODE_BOOT: 552ba9a58feSMilanka Ringwald break; 5537d26fe66SMilanka Ringwald case HID_PROTOCOL_MODE_REPORT: 5547d26fe66SMilanka Ringwald break; 5557bbeb3adSMilanka Ringwald default: 5567bbeb3adSMilanka Ringwald btstack_assert(false); 5577bbeb3adSMilanka Ringwald break; 5587d26fe66SMilanka Ringwald } 5597d26fe66SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL; 560e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event(device->control_cid); 5617d26fe66SMilanka Ringwald break; 5627d26fe66SMilanka Ringwald 5636510739bSMilanka Ringwald case HID_MESSAGE_TYPE_HID_CONTROL: 5646510739bSMilanka Ringwald param = packet[0] & 0x0F; 5656510739bSMilanka Ringwald switch (param){ 5666510739bSMilanka Ringwald case HID_CONTROL_PARAM_SUSPEND: 5676510739bSMilanka Ringwald hid_device_emit_event(device, HID_SUBEVENT_SUSPEND); 5686510739bSMilanka Ringwald break; 5696510739bSMilanka Ringwald case HID_CONTROL_PARAM_EXIT_SUSPEND: 5706510739bSMilanka Ringwald hid_device_emit_event(device, HID_SUBEVENT_EXIT_SUSPEND); 5716510739bSMilanka Ringwald break; 5726510739bSMilanka Ringwald default: 5736510739bSMilanka Ringwald device->state = HID_DEVICE_W2_SEND_UNSUPPORTED_REQUEST; 574e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event(device->control_cid); 5756510739bSMilanka Ringwald break; 5766510739bSMilanka Ringwald } 5776510739bSMilanka Ringwald break; 5787d26fe66SMilanka Ringwald 5797d26fe66SMilanka Ringwald case HID_MESSAGE_TYPE_DATA: 5807d26fe66SMilanka Ringwald if (packet_size < 2) { 5817d26fe66SMilanka Ringwald break; 5827d26fe66SMilanka Ringwald } 58307678896SMilanka Ringwald pos = 0; 584ca3f868cSMilanka Ringwald device->report_type = (hid_report_type_t)(packet[pos++] & 0x03); 58507678896SMilanka Ringwald device->report_id = 0; 58607678896SMilanka Ringwald if (btstack_hid_report_id_declared(hid_descriptor_len, hid_descriptor)){ 58707678896SMilanka Ringwald device->report_id = packet[pos++]; 588738c9391SMilanka Ringwald } 58907678896SMilanka Ringwald 59007678896SMilanka Ringwald if (hid_report_id_status(device->cid, device->report_id) == HID_REPORT_ID_INVALID){ 59107678896SMilanka Ringwald log_info("Ignore invalid report data packet"); 59207678896SMilanka Ringwald break; 59307678896SMilanka Ringwald } 59407678896SMilanka Ringwald if (!hid_report_size_valid(device->cid, device->report_id, device->report_type, packet_size - pos)){ 59507678896SMilanka Ringwald log_info("Ignore invalid report data packet, invalid size"); 59607678896SMilanka Ringwald break; 59707678896SMilanka Ringwald } 59807678896SMilanka Ringwald (*hci_device_report_data)(device->cid, device->report_type, device->report_id, packet_size - pos, &packet[pos]); 5997d26fe66SMilanka Ringwald break; 600c12110e4SMilanka Ringwald default: 601c12110e4SMilanka Ringwald device->state = HID_DEVICE_W2_SEND_UNSUPPORTED_REQUEST; 602e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event(device->control_cid); 603c12110e4SMilanka Ringwald break; 604c12110e4SMilanka Ringwald } 605c12110e4SMilanka Ringwald break; 6068eb8d463SMatthias Ringwald case HCI_EVENT_PACKET: 6078eb8d463SMatthias Ringwald switch (packet[0]){ 6088eb8d463SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 6098eb8d463SMatthias Ringwald switch (l2cap_event_incoming_connection_get_psm(packet)){ 6108eb8d463SMatthias Ringwald case PSM_HID_CONTROL: 6118eb8d463SMatthias Ringwald case PSM_HID_INTERRUPT: 6126510739bSMilanka Ringwald l2cap_event_incoming_connection_get_address(packet, address); 61372e3f392SMatthias Ringwald device = hid_device_provide_instance_for_bd_addr(address); 614c12110e4SMilanka Ringwald if (!device) { 6153da09877SMatthias Ringwald log_error("L2CAP_EVENT_INCOMING_CONNECTION, cannot create instance for %s", bd_addr_to_str(address)); 616c12110e4SMilanka Ringwald l2cap_decline_connection(channel); 617c12110e4SMilanka Ringwald break; 618c12110e4SMilanka Ringwald } 619c1ab6cc1SMatthias Ringwald if ((device->con_handle == HCI_CON_HANDLE_INVALID) || (l2cap_event_incoming_connection_get_handle(packet) == device->con_handle)){ 620c12110e4SMilanka Ringwald device->con_handle = l2cap_event_incoming_connection_get_handle(packet); 621c12110e4SMilanka Ringwald device->incoming = 1; 6226510739bSMilanka Ringwald l2cap_event_incoming_connection_get_address(packet, device->bd_addr); 6233da09877SMatthias Ringwald psm = l2cap_event_incoming_connection_get_psm(packet); 6243da09877SMatthias Ringwald switch (psm){ 6253da09877SMatthias Ringwald case PSM_HID_CONTROL: 6263da09877SMatthias Ringwald device->control_cid = l2cap_event_incoming_connection_get_local_cid(packet); 6273da09877SMatthias Ringwald break; 6283da09877SMatthias Ringwald case PSM_HID_INTERRUPT: 6293da09877SMatthias Ringwald device->interrupt_cid = l2cap_event_incoming_connection_get_local_cid(packet); 6303da09877SMatthias Ringwald break; 6313da09877SMatthias Ringwald default: 6323da09877SMatthias Ringwald break; 6333da09877SMatthias Ringwald } 6343da09877SMatthias Ringwald 6358eb8d463SMatthias Ringwald l2cap_accept_connection(channel); 6368eb8d463SMatthias Ringwald } else { 6378eb8d463SMatthias Ringwald l2cap_decline_connection(channel); 6383da09877SMatthias Ringwald log_info("L2CAP_EVENT_INCOMING_CONNECTION, decline connection for %s", bd_addr_to_str(address)); 6398eb8d463SMatthias Ringwald } 6408eb8d463SMatthias Ringwald break; 6418eb8d463SMatthias Ringwald default: 6428eb8d463SMatthias Ringwald l2cap_decline_connection(channel); 6438eb8d463SMatthias Ringwald break; 6448eb8d463SMatthias Ringwald } 6458eb8d463SMatthias Ringwald break; 6468eb8d463SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 6473da09877SMatthias Ringwald device = hid_device_get_instance_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet)); 648c12110e4SMilanka Ringwald if (!device) { 649c12110e4SMilanka Ringwald log_error("L2CAP_EVENT_CHANNEL_OPENED, no hid device for local cid 0x%02x", l2cap_event_channel_opened_get_local_cid(packet)); 650c12110e4SMilanka Ringwald return; 651c12110e4SMilanka Ringwald } 65257c643eeSMatthias Ringwald status = l2cap_event_channel_opened_get_status(packet); 65357c643eeSMatthias Ringwald if (status) { 654c12110e4SMilanka Ringwald if (device->incoming == 0){ 65557c643eeSMatthias Ringwald // report error for outgoing connection 656c12110e4SMilanka Ringwald hid_device_emit_connected_event(device, status); 65757c643eeSMatthias Ringwald } 65857c643eeSMatthias Ringwald return; 65957c643eeSMatthias Ringwald } 660641577b2SMatthias Ringwald 661641577b2SMatthias Ringwald // store con_handle 662641577b2SMatthias Ringwald if (device->con_handle == HCI_CON_HANDLE_INVALID){ 663641577b2SMatthias Ringwald device->con_handle = l2cap_event_channel_opened_get_handle(packet); 664641577b2SMatthias Ringwald } 665641577b2SMatthias Ringwald 666641577b2SMatthias Ringwald // store l2cap cid 66757c643eeSMatthias Ringwald psm = l2cap_event_channel_opened_get_psm(packet); 66857c643eeSMatthias Ringwald switch (psm){ 6698eb8d463SMatthias Ringwald case PSM_HID_CONTROL: 670c12110e4SMilanka Ringwald device->control_cid = l2cap_event_channel_opened_get_local_cid(packet); 6718eb8d463SMatthias Ringwald break; 6728eb8d463SMatthias Ringwald case PSM_HID_INTERRUPT: 673c12110e4SMilanka Ringwald device->interrupt_cid = l2cap_event_channel_opened_get_local_cid(packet); 6748eb8d463SMatthias Ringwald break; 6758eb8d463SMatthias Ringwald default: 6768eb8d463SMatthias Ringwald break; 6778eb8d463SMatthias Ringwald } 6786510739bSMilanka Ringwald 67957c643eeSMatthias Ringwald // connect HID Interrupt for outgoing 680c1ab6cc1SMatthias Ringwald if ((device->incoming == 0) && (psm == PSM_HID_CONTROL)){ 681c12110e4SMilanka Ringwald status = l2cap_create_channel(packet_handler, device->bd_addr, PSM_HID_INTERRUPT, 48, &device->interrupt_cid); 68257c643eeSMatthias Ringwald break; 68357c643eeSMatthias Ringwald } 684641577b2SMatthias Ringwald 685641577b2SMatthias Ringwald // emit connected if both channels are open 686641577b2SMatthias Ringwald connected_before = device->connected; 687c12110e4SMilanka Ringwald if (!connected_before && device->control_cid && device->interrupt_cid){ 688c12110e4SMilanka Ringwald device->connected = 1; 689c12110e4SMilanka Ringwald hid_device_emit_connected_event(device, 0); 6908eb8d463SMatthias Ringwald } 6918eb8d463SMatthias Ringwald break; 6928eb8d463SMatthias Ringwald case L2CAP_EVENT_CHANNEL_CLOSED: 6934176cbc0SMatthias Ringwald device = hid_device_get_instance_for_l2cap_cid(l2cap_event_channel_closed_get_local_cid(packet)); 694c12110e4SMilanka Ringwald if (!device) return; 695c12110e4SMilanka Ringwald 6966510739bSMilanka Ringwald // connected_before = device->connected; 697c12110e4SMilanka Ringwald device->incoming = 0; 698c12110e4SMilanka Ringwald if (l2cap_event_channel_closed_get_local_cid(packet) == device->interrupt_cid){ 699c12110e4SMilanka Ringwald device->interrupt_cid = 0; 70057c18996SMilanka Ringwald } 70157c18996SMilanka Ringwald if (l2cap_event_channel_closed_get_local_cid(packet) == device->control_cid){ 70257c18996SMilanka Ringwald device->control_cid = 0; 7038eb8d463SMatthias Ringwald } 7046510739bSMilanka Ringwald if (!device->interrupt_cid && !device->control_cid){ 7056510739bSMilanka Ringwald device->connected = 0; 7063da09877SMatthias Ringwald device->con_handle = HCI_CON_HANDLE_INVALID; 7076510739bSMilanka Ringwald device->cid = 0; 708caf9a547SMilanka Ringwald hid_device_emit_event(device, HID_SUBEVENT_CONNECTION_CLOSED); 7098eb8d463SMatthias Ringwald } 7108eb8d463SMatthias Ringwald break; 711c12110e4SMilanka Ringwald 71257c18996SMilanka Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 7137d26fe66SMilanka Ringwald local_cid = l2cap_event_can_send_now_get_local_cid(packet); 7144176cbc0SMatthias Ringwald device = hid_device_get_instance_for_l2cap_cid(local_cid); 7157d26fe66SMilanka Ringwald 716c12110e4SMilanka Ringwald if (!device) return; 717c12110e4SMilanka Ringwald switch (device->state){ 718a1118d11SMilanka Ringwald case HID_DEVICE_W2_GET_REPORT:{ 7197d26fe66SMilanka Ringwald if (device->report_status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL) { 7207d26fe66SMilanka Ringwald report[0] = (HID_MESSAGE_TYPE_HANDSHAKE << 4) | device->report_status; 7217d26fe66SMilanka Ringwald hid_device_send_control_message(device->cid, &report[0], 1); 7227d26fe66SMilanka Ringwald break; 7237d26fe66SMilanka Ringwald } 724a1118d11SMilanka Ringwald 725a1118d11SMilanka Ringwald pos = 0; 726a1118d11SMilanka Ringwald report[pos++] = (HID_MESSAGE_TYPE_DATA << 4) | device->report_type; 727a1118d11SMilanka Ringwald if (device->report_id){ 728a1118d11SMilanka Ringwald report[pos++] = device->report_id; 729a1118d11SMilanka Ringwald } 730a1118d11SMilanka Ringwald 7317d26fe66SMilanka Ringwald report_size = 0; 732a1118d11SMilanka Ringwald status = (*hci_device_get_report)(device->cid, device->report_type, device->report_id, &report_size, &report[pos]); 733481c7cdcSMilanka Ringwald 734a1118d11SMilanka Ringwald switch (status){ 735a1118d11SMilanka Ringwald case 0: 736a1118d11SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_NOT_READY; 737a1118d11SMilanka Ringwald break; 738a1118d11SMilanka Ringwald case 1: 739a1118d11SMilanka Ringwald if (report_size == 0){ 740a1118d11SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_UNSUPPORTED_REQUEST; 741a1118d11SMilanka Ringwald break; 742a1118d11SMilanka Ringwald } 743a1118d11SMilanka Ringwald if (device->expected_report_size != report_size){ 744a1118d11SMilanka Ringwald log_error("Expected report size of %d bytes, received %d", device->expected_report_size, report_size); 745a1118d11SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_UNSUPPORTED_REQUEST; 746a1118d11SMilanka Ringwald break; 747a1118d11SMilanka Ringwald } 748a1118d11SMilanka Ringwald break; 749a1118d11SMilanka Ringwald default: 750a1118d11SMilanka Ringwald device->report_status = HID_HANDSHAKE_PARAM_TYPE_ERR_UNSUPPORTED_REQUEST; 751a1118d11SMilanka Ringwald break; 752a1118d11SMilanka Ringwald } 753ba9a58feSMilanka Ringwald if (device->report_status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 754ba9a58feSMilanka Ringwald report[0] = (HID_MESSAGE_TYPE_HANDSHAKE << 4) | device->report_status; 755481c7cdcSMilanka Ringwald hid_device_send_control_message(device->cid, &report[0], 1); 756481c7cdcSMilanka Ringwald break; 757481c7cdcSMilanka Ringwald } 758481c7cdcSMilanka Ringwald 759a1118d11SMilanka Ringwald // if (report_size > l2cap_max_mtu()){ 760a1118d11SMilanka Ringwald // report[0] = (HID_MESSAGE_TYPE_HANDSHAKE << 4) | HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER; 761a1118d11SMilanka Ringwald // hid_device_send_control_message(device->cid, &report[0], 1); 762a1118d11SMilanka Ringwald // break; 763a1118d11SMilanka Ringwald // } 764481c7cdcSMilanka Ringwald 765a1118d11SMilanka Ringwald hid_device_send_control_message(device->cid, &report[0], device->report_size); 766481c7cdcSMilanka Ringwald // device->state = HID_DEVICE_IDLE; 767c12110e4SMilanka Ringwald break; 768a1118d11SMilanka Ringwald } 769ba9a58feSMilanka Ringwald case HID_DEVICE_W2_SET_REPORT: 770ba9a58feSMilanka Ringwald case HID_DEVICE_W2_SET_PROTOCOL: 771ba9a58feSMilanka Ringwald report[0] = (HID_MESSAGE_TYPE_HANDSHAKE << 4) | device->report_status; 7726510739bSMilanka Ringwald hid_device_send_control_message(device->cid, &report[0], 1); 773ba9a58feSMilanka Ringwald break; 774ba9a58feSMilanka Ringwald case HID_DEVICE_W2_GET_PROTOCOL: 775ba9a58feSMilanka Ringwald if (device->report_status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 776ba9a58feSMilanka Ringwald report[0] = (HID_MESSAGE_TYPE_HANDSHAKE << 4) | device->report_status; 777ba9a58feSMilanka Ringwald hid_device_send_control_message(device->cid, &report[0], 1); 778ba9a58feSMilanka Ringwald break; 779ba9a58feSMilanka Ringwald } 7805139b81eSMilanka Ringwald 7815139b81eSMilanka Ringwald report[0] = (HID_MESSAGE_TYPE_DATA << 4); 7825139b81eSMilanka Ringwald report[1] = device->protocol_mode; 7835139b81eSMilanka Ringwald hid_device_send_control_message(device->cid, &report[0], 2); 784ba9a58feSMilanka Ringwald break; 785ba9a58feSMilanka Ringwald 786ba9a58feSMilanka Ringwald 787ba9a58feSMilanka Ringwald case HID_DEVICE_W2_SEND_UNSUPPORTED_REQUEST: 788ba9a58feSMilanka Ringwald report[0] = (HID_MESSAGE_TYPE_HANDSHAKE << 4) | HID_HANDSHAKE_PARAM_TYPE_ERR_UNSUPPORTED_REQUEST; 789ba9a58feSMilanka Ringwald hid_device_send_control_message(device->cid, &report[0], 1); 790c12110e4SMilanka Ringwald break; 791c12110e4SMilanka Ringwald default: 792e99e41f0SMatthias Ringwald if (device->user_request_can_send_now){ 793e99e41f0SMatthias Ringwald device->user_request_can_send_now = 0; 794caf9a547SMilanka Ringwald hid_device_emit_event(device, HID_SUBEVENT_CAN_SEND_NOW); 795e99e41f0SMatthias Ringwald } 796c12110e4SMilanka Ringwald break; 797c12110e4SMilanka Ringwald } 798ba9a58feSMilanka Ringwald device->state = HID_DEVICE_IDLE; 7998eb8d463SMatthias Ringwald break; 8008eb8d463SMatthias Ringwald default: 8018eb8d463SMatthias Ringwald break; 8028eb8d463SMatthias Ringwald } 8038eb8d463SMatthias Ringwald break; 8048eb8d463SMatthias Ringwald default: 8058eb8d463SMatthias Ringwald break; 8068eb8d463SMatthias Ringwald } 8078eb8d463SMatthias Ringwald } 8088eb8d463SMatthias Ringwald 8098eb8d463SMatthias Ringwald /** 8108eb8d463SMatthias Ringwald * @brief Set up HID Device 8118eb8d463SMatthias Ringwald */ 812662cddc2SMilanka Ringwald void hid_device_init(uint8_t boot_protocol_mode_supported, uint16_t descriptor_len, const uint8_t * descriptor){ 813ba9a58feSMilanka Ringwald hid_boot_protocol_mode_supported = boot_protocol_mode_supported; 814662cddc2SMilanka Ringwald hid_descriptor = descriptor; 815662cddc2SMilanka Ringwald hid_descriptor_len = descriptor_len; 816a1118d11SMilanka Ringwald hci_device_get_report = dummy_write_report; 817a1118d11SMilanka Ringwald hci_device_set_report = dummy_set_report; 818a1118d11SMilanka Ringwald hci_device_report_data = dummy_report_data; 819a1118d11SMilanka Ringwald 82078315a58SMatthias Ringwald l2cap_register_service(packet_handler, PSM_HID_INTERRUPT, 100, gap_get_security_level()); 82178315a58SMatthias Ringwald l2cap_register_service(packet_handler, PSM_HID_CONTROL, 100, gap_get_security_level()); 8228eb8d463SMatthias Ringwald } 8238eb8d463SMatthias Ringwald 8242cc1b993SMatthias Ringwald void hid_device_deinit(void){ 8252cc1b993SMatthias Ringwald (void) memset(&_hid_device, 0, sizeof(hid_device_t)); 8262cc1b993SMatthias Ringwald } 8272cc1b993SMatthias Ringwald 8288eb8d463SMatthias Ringwald /** 8298eb8d463SMatthias Ringwald * @brief Register callback for the HID Device client. 8308eb8d463SMatthias Ringwald * @param callback 8318eb8d463SMatthias Ringwald */ 8328eb8d463SMatthias Ringwald void hid_device_register_packet_handler(btstack_packet_handler_t callback){ 8338eb8d463SMatthias Ringwald hid_callback = callback; 8348eb8d463SMatthias Ringwald } 8358eb8d463SMatthias Ringwald 836c12110e4SMilanka Ringwald 837c12110e4SMilanka Ringwald 838a1118d11SMilanka Ringwald void hid_device_register_report_request_callback(int (*callback)(uint16_t hid_cid, hid_report_type_t report_type, uint16_t report_id, int * out_report_size, uint8_t * out_report)){ 839c12110e4SMilanka Ringwald if (callback == NULL){ 840c12110e4SMilanka Ringwald callback = dummy_write_report; 841c12110e4SMilanka Ringwald } 842a1118d11SMilanka Ringwald hci_device_get_report = callback; 843c12110e4SMilanka Ringwald } 844c12110e4SMilanka Ringwald 8454dfe6a8bSMilanka Ringwald void hid_device_register_set_report_callback(void (*callback)(uint16_t hid_cid, hid_report_type_t report_type, int report_size, uint8_t * report)){ 846ba9a58feSMilanka Ringwald if (callback == NULL){ 847ba9a58feSMilanka Ringwald callback = dummy_set_report; 848ba9a58feSMilanka Ringwald } 849ba9a58feSMilanka Ringwald hci_device_set_report = callback; 850ba9a58feSMilanka Ringwald } 851c12110e4SMilanka Ringwald 8527d26fe66SMilanka Ringwald void hid_device_register_report_data_callback(void (*callback)(uint16_t cid, hid_report_type_t report_type, uint16_t report_id, int report_size, uint8_t * report)){ 8537d26fe66SMilanka Ringwald if (callback == NULL){ 8547d26fe66SMilanka Ringwald callback = dummy_report_data; 8557d26fe66SMilanka Ringwald } 8567d26fe66SMilanka Ringwald hci_device_report_data = callback; 8577d26fe66SMilanka Ringwald } 8587d26fe66SMilanka Ringwald 8597d26fe66SMilanka Ringwald 8608eb8d463SMatthias Ringwald /** 8618eb8d463SMatthias Ringwald * @brief Request can send now event to send HID Report 8628eb8d463SMatthias Ringwald * @param hid_cid 8638eb8d463SMatthias Ringwald */ 8648eb8d463SMatthias Ringwald void hid_device_request_can_send_now_event(uint16_t hid_cid){ 8654176cbc0SMatthias Ringwald hid_device_t * hid_device = hid_device_get_instance_for_hid_cid(hid_cid); 866e99e41f0SMatthias Ringwald if (!hid_device || !hid_device->interrupt_cid) return; 867e99e41f0SMatthias Ringwald hid_device->user_request_can_send_now = 1; 868e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event(hid_device->interrupt_cid); 8698eb8d463SMatthias Ringwald } 8708eb8d463SMatthias Ringwald 8718eb8d463SMatthias Ringwald /** 8728eb8d463SMatthias Ringwald * @brief Send HID message on interrupt channel 8738eb8d463SMatthias Ringwald * @param hid_cid 8748eb8d463SMatthias Ringwald */ 8758eb8d463SMatthias Ringwald void hid_device_send_interrupt_message(uint16_t hid_cid, const uint8_t * message, uint16_t message_len){ 8764176cbc0SMatthias Ringwald hid_device_t * hid_device = hid_device_get_instance_for_hid_cid(hid_cid); 877c12110e4SMilanka Ringwald if (!hid_device || !hid_device->interrupt_cid) return; 8788eb8d463SMatthias Ringwald l2cap_send(hid_device->interrupt_cid, (uint8_t*) message, message_len); 879e9392950SMilanka Ringwald if (hid_device->user_request_can_send_now){ 880517089d1SMilanka Ringwald printf("request user can send now because pending\n"); 881e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event((hid_device->interrupt_cid)); 882e99e41f0SMatthias Ringwald } 8838eb8d463SMatthias Ringwald } 8848eb8d463SMatthias Ringwald 8858eb8d463SMatthias Ringwald /** 8868eb8d463SMatthias Ringwald * @brief Send HID message on control channel 8878eb8d463SMatthias Ringwald * @param hid_cid 8888eb8d463SMatthias Ringwald */ 889c12110e4SMilanka Ringwald void hid_device_send_control_message(uint16_t hid_cid, const uint8_t * message, uint16_t message_len){ 8904176cbc0SMatthias Ringwald hid_device_t * hid_device = hid_device_get_instance_for_hid_cid(hid_cid); 891c12110e4SMilanka Ringwald if (!hid_device || !hid_device->control_cid) return; 8928eb8d463SMatthias Ringwald l2cap_send(hid_device->control_cid, (uint8_t*) message, message_len); 893e99e41f0SMatthias Ringwald // request user can send now if pending 894e99e41f0SMatthias Ringwald if (hid_device->user_request_can_send_now){ 895e99e41f0SMatthias Ringwald l2cap_request_can_send_now_event((hid_device->control_cid)); 896e99e41f0SMatthias Ringwald } 8978eb8d463SMatthias Ringwald } 8988eb8d463SMatthias Ringwald 89957c643eeSMatthias Ringwald /* 90057c643eeSMatthias Ringwald * @brief Create HID connection to HID Host 90157c643eeSMatthias Ringwald * @param addr 90257c643eeSMatthias Ringwald * @param hid_cid to use for other commands 90357c643eeSMatthias Ringwald * @result status 90457c643eeSMatthias Ringwald */ 90557c643eeSMatthias Ringwald uint8_t hid_device_connect(bd_addr_t addr, uint16_t * hid_cid){ 906c12110e4SMilanka Ringwald hid_device_t * hid_device = hid_device_create_instance(); 907c12110e4SMilanka Ringwald if (!hid_device){ 908c12110e4SMilanka Ringwald log_error("hid_device_connect: could not create a hid device instace"); 909c12110e4SMilanka Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 910c12110e4SMilanka Ringwald } 91157c643eeSMatthias Ringwald // assign hic_cid 912c12110e4SMilanka Ringwald *hid_cid = hid_device_get_next_cid(); 91372e3f392SMatthias Ringwald 91457c643eeSMatthias Ringwald // store address 9156535961aSMatthias Ringwald (void)memcpy(hid_device->bd_addr, addr, 6); 91657c643eeSMatthias Ringwald 91757c643eeSMatthias Ringwald // reset state 9186510739bSMilanka Ringwald hid_device->cid = *hid_cid; 91957c643eeSMatthias Ringwald hid_device->incoming = 0; 92057c643eeSMatthias Ringwald hid_device->connected = 0; 92157c643eeSMatthias Ringwald hid_device->control_cid = 0; 92257c643eeSMatthias Ringwald hid_device->interrupt_cid = 0; 92372e3f392SMatthias Ringwald hid_device->con_handle = HCI_CON_HANDLE_INVALID; 92472e3f392SMatthias Ringwald 92557c643eeSMatthias Ringwald // create l2cap control using fixed HID L2CAP PSM 92657c643eeSMatthias Ringwald log_info("Create outgoing HID Control"); 92757c643eeSMatthias Ringwald uint8_t status = l2cap_create_channel(packet_handler, hid_device->bd_addr, PSM_HID_CONTROL, 48, &hid_device->control_cid); 92857c643eeSMatthias Ringwald return status; 92957c643eeSMatthias Ringwald } 9306510739bSMilanka Ringwald 9316510739bSMilanka Ringwald /* 9326510739bSMilanka Ringwald * @brief Disconnect from HID Host 9336510739bSMilanka Ringwald * @param hid_cid 9346510739bSMilanka Ringwald * @result status 9356510739bSMilanka Ringwald */ 93657c18996SMilanka Ringwald void hid_device_disconnect_interrupt_channel(uint16_t hid_cid){ 9374176cbc0SMatthias Ringwald hid_device_t * hid_device = hid_device_get_instance_for_hid_cid(hid_cid); 93857c18996SMilanka Ringwald if (!hid_device){ 93957c18996SMilanka Ringwald log_error("hid_device_disconnect_interrupt_channel: could not find hid device instace"); 94057c18996SMilanka Ringwald return; 94157c18996SMilanka Ringwald } 94257c18996SMilanka Ringwald log_info("Disconnect from interrupt channel HID Host"); 94357c18996SMilanka Ringwald if (hid_device->interrupt_cid){ 94457c18996SMilanka Ringwald l2cap_disconnect(hid_device->interrupt_cid, 0); // reason isn't used 94557c18996SMilanka Ringwald } 94657c18996SMilanka Ringwald } 94757c18996SMilanka Ringwald 94857c18996SMilanka Ringwald void hid_device_disconnect_control_channel(uint16_t hid_cid){ 9494176cbc0SMatthias Ringwald hid_device_t * hid_device = hid_device_get_instance_for_hid_cid(hid_cid); 95057c18996SMilanka Ringwald if (!hid_device){ 95157c18996SMilanka Ringwald log_error("hid_device_disconnect_control_channel: could not find hid device instace"); 95257c18996SMilanka Ringwald return; 95357c18996SMilanka Ringwald } 95457c18996SMilanka Ringwald log_info("Disconnect from control channel HID Host"); 95557c18996SMilanka Ringwald if (hid_device->control_cid){ 95657c18996SMilanka Ringwald l2cap_disconnect(hid_device->control_cid, 0); // reason isn't used 95757c18996SMilanka Ringwald } 95857c18996SMilanka Ringwald } 95957c18996SMilanka Ringwald 9606510739bSMilanka Ringwald void hid_device_disconnect(uint16_t hid_cid){ 9614176cbc0SMatthias Ringwald hid_device_t * hid_device = hid_device_get_instance_for_hid_cid(hid_cid); 9626510739bSMilanka Ringwald if (!hid_device){ 9636510739bSMilanka Ringwald log_error("hid_device_disconnect: could not find hid device instace"); 9646510739bSMilanka Ringwald return; 9656510739bSMilanka Ringwald } 9666510739bSMilanka Ringwald log_info("Disconnect from HID Host"); 9676510739bSMilanka Ringwald if (hid_device->interrupt_cid){ 9686510739bSMilanka Ringwald l2cap_disconnect(hid_device->interrupt_cid, 0); // reason isn't used 9696510739bSMilanka Ringwald } 97057c18996SMilanka Ringwald if (hid_device->control_cid){ 97157c18996SMilanka Ringwald l2cap_disconnect(hid_device->control_cid, 0); // reason isn't used 97257c18996SMilanka Ringwald } 9736510739bSMilanka Ringwald } 9747d26fe66SMilanka Ringwald 9757d26fe66SMilanka Ringwald int hid_device_in_boot_protocol_mode(uint16_t hid_cid){ 9764176cbc0SMatthias Ringwald hid_device_t * hid_device = hid_device_get_instance_for_hid_cid(hid_cid); 9777d26fe66SMilanka Ringwald if (!hid_device){ 9787d26fe66SMilanka Ringwald log_error("hid_device_in_boot_protocol_mode: could not find hid device instace"); 9797d26fe66SMilanka Ringwald return 0; 9807d26fe66SMilanka Ringwald } 9817d26fe66SMilanka Ringwald return hid_device->protocol_mode == HID_PROTOCOL_MODE_BOOT; 9827d26fe66SMilanka Ringwald } 983