12d05904dSMatthias Ringwald /* 22d05904dSMatthias Ringwald * Copyright (C) 2017 BlueKitchen GmbH 32d05904dSMatthias Ringwald * 42d05904dSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 52d05904dSMatthias Ringwald * modification, are permitted provided that the following conditions 62d05904dSMatthias Ringwald * are met: 72d05904dSMatthias Ringwald * 82d05904dSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 92d05904dSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 102d05904dSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 112d05904dSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 122d05904dSMatthias Ringwald * documentation and/or other materials provided with the distribution. 132d05904dSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 142d05904dSMatthias Ringwald * contributors may be used to endorse or promote products derived 152d05904dSMatthias Ringwald * from this software without specific prior written permission. 162d05904dSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 172d05904dSMatthias Ringwald * personal benefit and not for any commercial purpose or for 182d05904dSMatthias Ringwald * monetary gain. 192d05904dSMatthias Ringwald * 202d05904dSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 212d05904dSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 222d05904dSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232d05904dSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 242d05904dSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 252d05904dSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 262d05904dSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 272d05904dSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 282d05904dSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 292d05904dSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 302d05904dSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312d05904dSMatthias Ringwald * SUCH DAMAGE. 322d05904dSMatthias Ringwald * 332d05904dSMatthias Ringwald * Please inquire about commercial licensing options at 342d05904dSMatthias Ringwald * [email protected] 352d05904dSMatthias Ringwald * 362d05904dSMatthias Ringwald */ 372d05904dSMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hid_host_demo.c" 392d05904dSMatthias Ringwald 402d05904dSMatthias Ringwald /* 412d05904dSMatthias Ringwald * hid_host_demo.c 422d05904dSMatthias Ringwald */ 432d05904dSMatthias Ringwald 442d05904dSMatthias Ringwald /* EXAMPLE_START(hid_host_demo): HID Host Demo 452d05904dSMatthias Ringwald * 46e85416c1SMatthias Ringwald * @text This example implements an HID Host. For now, it connnects to a fixed device, queries the HID SDP 47e85416c1SMatthias Ringwald * record and opens the HID Control + Interrupt channels 482d05904dSMatthias Ringwald */ 492d05904dSMatthias Ringwald 50173fff9bSMatthias Ringwald #include <inttypes.h> 512d05904dSMatthias Ringwald #include <stdio.h> 522d05904dSMatthias Ringwald 532d05904dSMatthias Ringwald #include "btstack_config.h" 542d05904dSMatthias Ringwald #include "btstack.h" 552d05904dSMatthias Ringwald 5667c74d26SMatthias Ringwald #define MAX_ATTRIBUTE_VALUE_SIZE 300 572d05904dSMatthias Ringwald 58e85416c1SMatthias Ringwald // SDP 592d05904dSMatthias Ringwald static uint8_t hid_descriptor[MAX_ATTRIBUTE_VALUE_SIZE]; 602d05904dSMatthias Ringwald static uint16_t hid_descriptor_len; 612d05904dSMatthias Ringwald 622d05904dSMatthias Ringwald static uint16_t hid_control_psm; 632d05904dSMatthias Ringwald static uint16_t hid_interrupt_psm; 642d05904dSMatthias Ringwald 652d05904dSMatthias Ringwald static uint8_t attribute_value[MAX_ATTRIBUTE_VALUE_SIZE]; 662d05904dSMatthias Ringwald static const unsigned int attribute_value_buffer_size = MAX_ATTRIBUTE_VALUE_SIZE; 672d05904dSMatthias Ringwald 68e85416c1SMatthias Ringwald // L2CAP 69e85416c1SMatthias Ringwald static uint16_t l2cap_hid_control_cid; 70e85416c1SMatthias Ringwald static uint16_t l2cap_hid_interrupt_cid; 71e85416c1SMatthias Ringwald 722d05904dSMatthias Ringwald // MBP 2016 732d05904dSMatthias Ringwald static const char * remote_addr_string = "F4-0F-24-3B-1B-E1"; 742d05904dSMatthias Ringwald // iMpulse static const char * remote_addr_string = "64:6E:6C:C1:AA:B5"; 752d05904dSMatthias Ringwald 762d05904dSMatthias Ringwald static bd_addr_t remote_addr; 772d05904dSMatthias Ringwald 782d05904dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 792d05904dSMatthias Ringwald 801d5191c9SMatthias Ringwald // Simplified US Keyboard with Shift modifier 811d5191c9SMatthias Ringwald 821d5191c9SMatthias Ringwald #define CHAR_ILLEGAL 0xff 831d5191c9SMatthias Ringwald #define CHAR_RETURN '\n' 841d5191c9SMatthias Ringwald #define CHAR_ESCAPE 27 851d5191c9SMatthias Ringwald #define CHAR_TAB '\t' 861d5191c9SMatthias Ringwald #define CHAR_BACKSPACE 0x7f 871d5191c9SMatthias Ringwald 881d5191c9SMatthias Ringwald /** 891d5191c9SMatthias Ringwald * English (US) 901d5191c9SMatthias Ringwald */ 911d5191c9SMatthias Ringwald static const uint8_t keytable_us_none [] = { 921d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 931d5191c9SMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 941d5191c9SMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 951d5191c9SMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 961d5191c9SMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 971d5191c9SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 981d5191c9SMatthias Ringwald '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 991d5191c9SMatthias Ringwald '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 1001d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 1011d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 1021d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 1031d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 1041d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 1051d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 1061d5191c9SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 1071d5191c9SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 1081d5191c9SMatthias Ringwald }; 1091d5191c9SMatthias Ringwald 1101d5191c9SMatthias Ringwald static const uint8_t keytable_us_shift[] = { 1111d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 1121d5191c9SMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 1131d5191c9SMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 1141d5191c9SMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 1151d5191c9SMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 1161d5191c9SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 1171d5191c9SMatthias Ringwald '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 1181d5191c9SMatthias Ringwald '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 1191d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 1201d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 1211d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 1221d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 1231d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 1241d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 1251d5191c9SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 1261d5191c9SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 1271d5191c9SMatthias Ringwald }; 1281d5191c9SMatthias Ringwald 1292d05904dSMatthias Ringwald 1302d05904dSMatthias Ringwald /* @section Main application configuration 1312d05904dSMatthias Ringwald * 1322d05904dSMatthias Ringwald * @text In the application configuration, L2CAP is initialized 1332d05904dSMatthias Ringwald */ 1342d05904dSMatthias Ringwald 1352d05904dSMatthias Ringwald /* LISTING_START(PanuSetup): Panu setup */ 1362d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1372d05904dSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1382d05904dSMatthias Ringwald 1392d05904dSMatthias Ringwald static void hid_host_setup(void){ 1402d05904dSMatthias Ringwald 141a4fe6467SMatthias Ringwald // Initialize L2CAP 142a4fe6467SMatthias Ringwald l2cap_init(); 143a4fe6467SMatthias Ringwald 144*6e1e5689SMatthias Ringwald // register L2CAP Services for reconnections 145*6e1e5689SMatthias Ringwald l2cap_register_service(packet_handler, PSM_HID_INTERRUPT, 0xffff, gap_get_security_level()); 146*6e1e5689SMatthias Ringwald l2cap_register_service(packet_handler, PSM_HID_CONTROL, 0xffff, gap_get_security_level()); 147*6e1e5689SMatthias Ringwald 148*6e1e5689SMatthias Ringwald // Allow sniff mode requests by HID device 149*6e1e5689SMatthias Ringwald gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE); 150*6e1e5689SMatthias Ringwald 1512d05904dSMatthias Ringwald // register for HCI events 1522d05904dSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1532d05904dSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1542d05904dSMatthias Ringwald 1556e82c1f8SMatthias Ringwald // Allow sniff mode requests by HID device 1566e82c1f8SMatthias Ringwald gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE); 1576e82c1f8SMatthias Ringwald 1581d5191c9SMatthias Ringwald // Disable stdout buffering 1591d5191c9SMatthias Ringwald setbuf(stdout, NULL); 1602d05904dSMatthias Ringwald } 1612d05904dSMatthias Ringwald /* LISTING_END */ 1622d05904dSMatthias Ringwald 1632d05904dSMatthias Ringwald /* @section SDP parser callback 1642d05904dSMatthias Ringwald * 1652d05904dSMatthias Ringwald * @text The SDP parsers retrieves the BNEP PAN UUID as explained in 1662d05904dSMatthias Ringwald * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}. 1672d05904dSMatthias Ringwald */ 1682d05904dSMatthias Ringwald 1692d05904dSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 1702d05904dSMatthias Ringwald 1712d05904dSMatthias Ringwald UNUSED(packet_type); 1722d05904dSMatthias Ringwald UNUSED(channel); 1732d05904dSMatthias Ringwald UNUSED(size); 1742d05904dSMatthias Ringwald 1752d05904dSMatthias Ringwald des_iterator_t attribute_list_it; 1762d05904dSMatthias Ringwald des_iterator_t additional_des_it; 1772d05904dSMatthias Ringwald des_iterator_t prot_it; 1782d05904dSMatthias Ringwald uint8_t *des_element; 1792d05904dSMatthias Ringwald uint8_t *element; 1802d05904dSMatthias Ringwald uint32_t uuid; 181e85416c1SMatthias Ringwald uint8_t status; 1822d05904dSMatthias Ringwald 1832d05904dSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 1842d05904dSMatthias Ringwald case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 1852d05904dSMatthias Ringwald if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 1862d05904dSMatthias Ringwald attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 1872d05904dSMatthias Ringwald if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 1882d05904dSMatthias Ringwald switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 1892d05904dSMatthias Ringwald case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 1902d05904dSMatthias Ringwald for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) { 1912d05904dSMatthias Ringwald if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue; 1922d05904dSMatthias Ringwald des_element = des_iterator_get_element(&attribute_list_it); 1932d05904dSMatthias Ringwald des_iterator_init(&prot_it, des_element); 1942d05904dSMatthias Ringwald element = des_iterator_get_element(&prot_it); 19514fd128cSMatthias Ringwald if (!element) continue; 1962d05904dSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 1972d05904dSMatthias Ringwald uuid = de_get_uuid32(element); 19814fd128cSMatthias Ringwald des_iterator_next(&prot_it); 1992d05904dSMatthias Ringwald switch (uuid){ 2002d05904dSMatthias Ringwald case BLUETOOTH_PROTOCOL_L2CAP: 2012d05904dSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 2022d05904dSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_control_psm); 2038d995dffSMatthias Ringwald printf("HID Control PSM: 0x%04x\n", (int) hid_control_psm); 2042d05904dSMatthias Ringwald break; 2052d05904dSMatthias Ringwald default: 2062d05904dSMatthias Ringwald break; 2072d05904dSMatthias Ringwald } 2082d05904dSMatthias Ringwald } 2092d05904dSMatthias Ringwald break; 2102d05904dSMatthias Ringwald case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: 2112d05904dSMatthias Ringwald for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) { 2122d05904dSMatthias Ringwald if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue; 2132d05904dSMatthias Ringwald des_element = des_iterator_get_element(&attribute_list_it); 2142d05904dSMatthias Ringwald for (des_iterator_init(&additional_des_it, des_element); des_iterator_has_more(&additional_des_it); des_iterator_next(&additional_des_it)) { 2152d05904dSMatthias Ringwald if (des_iterator_get_type(&additional_des_it) != DE_DES) continue; 2162d05904dSMatthias Ringwald des_element = des_iterator_get_element(&additional_des_it); 2172d05904dSMatthias Ringwald des_iterator_init(&prot_it, des_element); 2182d05904dSMatthias Ringwald element = des_iterator_get_element(&prot_it); 21914fd128cSMatthias Ringwald if (!element) continue; 2202d05904dSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 2212d05904dSMatthias Ringwald uuid = de_get_uuid32(element); 22214fd128cSMatthias Ringwald des_iterator_next(&prot_it); 2232d05904dSMatthias Ringwald switch (uuid){ 2242d05904dSMatthias Ringwald case BLUETOOTH_PROTOCOL_L2CAP: 2252d05904dSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 2262d05904dSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_interrupt_psm); 2278d995dffSMatthias Ringwald printf("HID Interrupt PSM: 0x%04x\n", (int) hid_interrupt_psm); 2282d05904dSMatthias Ringwald break; 2292d05904dSMatthias Ringwald default: 2302d05904dSMatthias Ringwald break; 2312d05904dSMatthias Ringwald } 2322d05904dSMatthias Ringwald } 2332d05904dSMatthias Ringwald } 2342d05904dSMatthias Ringwald break; 2352d05904dSMatthias Ringwald case BLUETOOTH_ATTRIBUTE_HID_DESCRIPTOR_LIST: 2361d5191c9SMatthias Ringwald for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) { 2371d5191c9SMatthias Ringwald if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue; 2381d5191c9SMatthias Ringwald des_element = des_iterator_get_element(&attribute_list_it); 2391d5191c9SMatthias Ringwald for (des_iterator_init(&additional_des_it, des_element); des_iterator_has_more(&additional_des_it); des_iterator_next(&additional_des_it)) { 2401d5191c9SMatthias Ringwald if (des_iterator_get_type(&additional_des_it) != DE_STRING) continue; 2411d5191c9SMatthias Ringwald element = des_iterator_get_element(&additional_des_it); 2421d5191c9SMatthias Ringwald const uint8_t * descriptor = de_get_string(element); 2431d5191c9SMatthias Ringwald hid_descriptor_len = de_get_data_size(element); 2441d5191c9SMatthias Ringwald memcpy(hid_descriptor, descriptor, hid_descriptor_len); 2452d05904dSMatthias Ringwald printf("HID Descriptor:\n"); 2462d05904dSMatthias Ringwald printf_hexdump(hid_descriptor, hid_descriptor_len); 2471d5191c9SMatthias Ringwald } 2481d5191c9SMatthias Ringwald } 2492d05904dSMatthias Ringwald break; 2502d05904dSMatthias Ringwald default: 2512d05904dSMatthias Ringwald break; 2522d05904dSMatthias Ringwald } 2532d05904dSMatthias Ringwald } 2542d05904dSMatthias Ringwald } else { 2552d05904dSMatthias Ringwald fprintf(stderr, "SDP attribute value buffer size exceeded: available %d, required %d\n", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); 2562d05904dSMatthias Ringwald } 2572d05904dSMatthias Ringwald break; 2582d05904dSMatthias Ringwald 2592d05904dSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 260*6e1e5689SMatthias Ringwald if (sdp_event_query_complete_get_status(packet) != ERROR_CODE_SUCCESS){ 261*6e1e5689SMatthias Ringwald printf("SDP Query failed\n"); 262*6e1e5689SMatthias Ringwald break; 263*6e1e5689SMatthias Ringwald } 264*6e1e5689SMatthias Ringwald if ((l2cap_hid_interrupt_cid != 0) && (l2cap_hid_control_cid != 0)){ 265*6e1e5689SMatthias Ringwald printf("HID device re-connected\n"); 266*6e1e5689SMatthias Ringwald break; 267*6e1e5689SMatthias Ringwald } 2682d05904dSMatthias Ringwald if (!hid_control_psm) { 269b2bb8038SMatthias Ringwald hid_control_psm = BLUETOOTH_PSM_HID_CONTROL; 270b2bb8038SMatthias Ringwald printf("HID Control PSM missing, using default 0x%04x\n", hid_control_psm); 2712d05904dSMatthias Ringwald } 2722d05904dSMatthias Ringwald if (!hid_interrupt_psm) { 273b2bb8038SMatthias Ringwald hid_interrupt_psm = BLUETOOTH_PSM_HID_INTERRUPT; 274b2bb8038SMatthias Ringwald printf("HID Interrupt PSM missing, using default 0x%04x\n", hid_interrupt_psm); 2752d05904dSMatthias Ringwald break; 2762d05904dSMatthias Ringwald } 2772d05904dSMatthias Ringwald printf("Setup HID\n"); 278e85416c1SMatthias Ringwald status = l2cap_create_channel(packet_handler, remote_addr, hid_control_psm, 48, &l2cap_hid_control_cid); 279e85416c1SMatthias Ringwald if (status){ 280e85416c1SMatthias Ringwald printf("Connecting to HID Control failed: 0x%02x\n", status); 281e85416c1SMatthias Ringwald } 2822d05904dSMatthias Ringwald break; 2832d05904dSMatthias Ringwald } 2842d05904dSMatthias Ringwald } 2852d05904dSMatthias Ringwald 2862d05904dSMatthias Ringwald /* 2871d5191c9SMatthias Ringwald * @section HID Report Handler 2881d5191c9SMatthias Ringwald * 2891d5191c9SMatthias Ringwald * @text Use BTstack's compact HID Parser to process incoming HID Report 2901d5191c9SMatthias Ringwald * Iterate over all fields and process fields with usage page = 0x07 / Keyboard 2911d5191c9SMatthias Ringwald * Check if SHIFT is down and process first character (don't handle multiple key presses) 2921d5191c9SMatthias Ringwald * 2931d5191c9SMatthias Ringwald */ 2941d5191c9SMatthias Ringwald #define NUM_KEYS 6 2951d5191c9SMatthias Ringwald static uint8_t last_keys[NUM_KEYS]; 2961d5191c9SMatthias Ringwald static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){ 2971d5191c9SMatthias Ringwald // check if HID Input Report 2981d5191c9SMatthias Ringwald if (report_len < 1) return; 2991d5191c9SMatthias Ringwald if (*report != 0xa1) return; 3001d5191c9SMatthias Ringwald report++; 3011d5191c9SMatthias Ringwald report_len--; 3021d5191c9SMatthias Ringwald btstack_hid_parser_t parser; 303a796c06fSMilanka Ringwald btstack_hid_parser_init(&parser, hid_descriptor, hid_descriptor_len, HID_REPORT_TYPE_INPUT, report, report_len); 3041d5191c9SMatthias Ringwald int shift = 0; 3051d5191c9SMatthias Ringwald uint8_t new_keys[NUM_KEYS]; 3061d5191c9SMatthias Ringwald memset(new_keys, 0, sizeof(new_keys)); 3071d5191c9SMatthias Ringwald int new_keys_count = 0; 3081d5191c9SMatthias Ringwald while (btstack_hid_parser_has_more(&parser)){ 3091d5191c9SMatthias Ringwald uint16_t usage_page; 3101d5191c9SMatthias Ringwald uint16_t usage; 3111d5191c9SMatthias Ringwald int32_t value; 3121d5191c9SMatthias Ringwald btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value); 3131d5191c9SMatthias Ringwald if (usage_page != 0x07) continue; 3141d5191c9SMatthias Ringwald switch (usage){ 3151d5191c9SMatthias Ringwald case 0xe1: 3161d5191c9SMatthias Ringwald case 0xe6: 3171d5191c9SMatthias Ringwald if (value){ 3181d5191c9SMatthias Ringwald shift = 1; 3191d5191c9SMatthias Ringwald } 3201d5191c9SMatthias Ringwald continue; 3211d5191c9SMatthias Ringwald case 0x00: 3221d5191c9SMatthias Ringwald continue; 3231d5191c9SMatthias Ringwald default: 3241d5191c9SMatthias Ringwald break; 3251d5191c9SMatthias Ringwald } 3261d5191c9SMatthias Ringwald if (usage >= sizeof(keytable_us_none)) continue; 3271d5191c9SMatthias Ringwald 3281d5191c9SMatthias Ringwald // store new keys 3291d5191c9SMatthias Ringwald new_keys[new_keys_count++] = usage; 3301d5191c9SMatthias Ringwald 3311d5191c9SMatthias Ringwald // check if usage was used last time (and ignore in that case) 3321d5191c9SMatthias Ringwald int i; 3331d5191c9SMatthias Ringwald for (i=0;i<NUM_KEYS;i++){ 3341d5191c9SMatthias Ringwald if (usage == last_keys[i]){ 3351d5191c9SMatthias Ringwald usage = 0; 3361d5191c9SMatthias Ringwald } 3371d5191c9SMatthias Ringwald } 3381d5191c9SMatthias Ringwald if (usage == 0) continue; 3391d5191c9SMatthias Ringwald 3401d5191c9SMatthias Ringwald uint8_t key; 3411d5191c9SMatthias Ringwald if (shift){ 3421d5191c9SMatthias Ringwald key = keytable_us_shift[usage]; 3431d5191c9SMatthias Ringwald } else { 3441d5191c9SMatthias Ringwald key = keytable_us_none[usage]; 3451d5191c9SMatthias Ringwald } 3461d5191c9SMatthias Ringwald if (key == CHAR_ILLEGAL) continue; 3471d5191c9SMatthias Ringwald if (key == CHAR_BACKSPACE){ 3481d5191c9SMatthias Ringwald printf("\b \b"); // go back one char, print space, go back one char again 3491d5191c9SMatthias Ringwald continue; 3501d5191c9SMatthias Ringwald } 3511d5191c9SMatthias Ringwald printf("%c", key); 3521d5191c9SMatthias Ringwald } 3531d5191c9SMatthias Ringwald memcpy(last_keys, new_keys, NUM_KEYS); 3541d5191c9SMatthias Ringwald } 3551d5191c9SMatthias Ringwald 3561d5191c9SMatthias Ringwald /* 3572d05904dSMatthias Ringwald * @section Packet Handler 3582d05904dSMatthias Ringwald * 3592d05904dSMatthias Ringwald * @text The packet handler responds to various HCI Events. 3602d05904dSMatthias Ringwald */ 3612d05904dSMatthias Ringwald 3622d05904dSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 3632d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 3642d05904dSMatthias Ringwald { 3652d05904dSMatthias Ringwald /* LISTING_PAUSE */ 3662d05904dSMatthias Ringwald uint8_t event; 3672d05904dSMatthias Ringwald bd_addr_t event_addr; 368e85416c1SMatthias Ringwald uint8_t status; 369e85416c1SMatthias Ringwald uint16_t l2cap_cid; 3702d05904dSMatthias Ringwald 3712d05904dSMatthias Ringwald /* LISTING_RESUME */ 3722d05904dSMatthias Ringwald switch (packet_type) { 3732d05904dSMatthias Ringwald case HCI_EVENT_PACKET: 3742d05904dSMatthias Ringwald event = hci_event_packet_get_type(packet); 3752d05904dSMatthias Ringwald switch (event) { 3762d05904dSMatthias Ringwald /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING 3772d05904dSMatthias Ringwald * is received and the example is started in client mode, the remote SDP HID query is started. 3782d05904dSMatthias Ringwald */ 3792d05904dSMatthias Ringwald case BTSTACK_EVENT_STATE: 3802d05904dSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 381*6e1e5689SMatthias Ringwald printf("Start SDP HID query for remote HID Device %s.\n", bd_addr_to_str(remote_addr)); 3822d05904dSMatthias Ringwald sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); 3832d05904dSMatthias Ringwald } 3842d05904dSMatthias Ringwald break; 3852d05904dSMatthias Ringwald 3862d05904dSMatthias Ringwald /* LISTING_PAUSE */ 3872d05904dSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 3882d05904dSMatthias Ringwald // inform about pin code request 3892d05904dSMatthias Ringwald printf("Pin code request - using '0000'\n"); 3902d05904dSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 3912d05904dSMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 3922d05904dSMatthias Ringwald break; 3932d05904dSMatthias Ringwald 3942d05904dSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 3952d05904dSMatthias Ringwald // inform about user confirmation request 396173fff9bSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8)); 3972d05904dSMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 3982d05904dSMatthias Ringwald break; 3992d05904dSMatthias Ringwald 4002d05904dSMatthias Ringwald /* LISTING_RESUME */ 401*6e1e5689SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 402*6e1e5689SMatthias Ringwald l2cap_cid = l2cap_event_incoming_connection_get_local_cid(packet); 403*6e1e5689SMatthias Ringwald switch (l2cap_event_incoming_connection_get_psm(packet)){ 404*6e1e5689SMatthias Ringwald case PSM_HID_CONTROL: 405*6e1e5689SMatthias Ringwald case PSM_HID_INTERRUPT: 406*6e1e5689SMatthias Ringwald l2cap_accept_connection(l2cap_cid); 407*6e1e5689SMatthias Ringwald break; 408*6e1e5689SMatthias Ringwald default: 409*6e1e5689SMatthias Ringwald l2cap_decline_connection(l2cap_cid); 410*6e1e5689SMatthias Ringwald break; 411*6e1e5689SMatthias Ringwald } 412*6e1e5689SMatthias Ringwald break; 413e85416c1SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 414e85416c1SMatthias Ringwald status = packet[2]; 415e85416c1SMatthias Ringwald if (status){ 416e85416c1SMatthias Ringwald printf("L2CAP Connection failed: 0x%02x\n", status); 417e85416c1SMatthias Ringwald break; 418e85416c1SMatthias Ringwald } 419*6e1e5689SMatthias Ringwald l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 420*6e1e5689SMatthias Ringwald switch (l2cap_event_channel_opened_get_psm(packet)){ 421*6e1e5689SMatthias Ringwald case PSM_HID_CONTROL: 422*6e1e5689SMatthias Ringwald if (l2cap_event_channel_opened_get_incoming(packet) == 0) { 423e85416c1SMatthias Ringwald status = l2cap_create_channel(packet_handler, remote_addr, hid_interrupt_psm, 48, &l2cap_hid_interrupt_cid); 424e85416c1SMatthias Ringwald if (status){ 425*6e1e5689SMatthias Ringwald printf("Connecting to HID Interrupt failed: 0x%02x\n", status); 426e85416c1SMatthias Ringwald break; 427e85416c1SMatthias Ringwald } 428e85416c1SMatthias Ringwald } 429*6e1e5689SMatthias Ringwald l2cap_hid_control_cid = l2cap_cid; 430*6e1e5689SMatthias Ringwald break; 431*6e1e5689SMatthias Ringwald case PSM_HID_INTERRUPT: 432*6e1e5689SMatthias Ringwald l2cap_hid_interrupt_cid = l2cap_cid; 433*6e1e5689SMatthias Ringwald break; 434*6e1e5689SMatthias Ringwald default: 435*6e1e5689SMatthias Ringwald break; 436*6e1e5689SMatthias Ringwald } 437*6e1e5689SMatthias Ringwald 438*6e1e5689SMatthias Ringwald if ((l2cap_hid_control_cid != 0) && (l2cap_hid_interrupt_cid != 0)){ 439*6e1e5689SMatthias Ringwald if (hid_descriptor_len == 0){ 440*6e1e5689SMatthias Ringwald printf("Start SDP HID query to get HID Descriptor\n"); 441*6e1e5689SMatthias Ringwald sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); 442*6e1e5689SMatthias Ringwald } else { 443e85416c1SMatthias Ringwald printf("HID Connection established\n"); 444e85416c1SMatthias Ringwald } 445*6e1e5689SMatthias Ringwald } 446e85416c1SMatthias Ringwald break; 447*6e1e5689SMatthias Ringwald case L2CAP_EVENT_CHANNEL_CLOSED: 448*6e1e5689SMatthias Ringwald if ((l2cap_hid_control_cid != 0) && (l2cap_hid_interrupt_cid != 0)){ 449*6e1e5689SMatthias Ringwald printf("HID Connection closed\n"); 450*6e1e5689SMatthias Ringwald hid_descriptor_len = 0; 451*6e1e5689SMatthias Ringwald } 452*6e1e5689SMatthias Ringwald l2cap_cid = l2cap_event_channel_closed_get_local_cid(packet); 453*6e1e5689SMatthias Ringwald if (l2cap_cid == l2cap_hid_control_cid){ 454*6e1e5689SMatthias Ringwald l2cap_hid_control_cid = 0; 455*6e1e5689SMatthias Ringwald } 456*6e1e5689SMatthias Ringwald if (l2cap_cid == l2cap_hid_interrupt_cid){ 457*6e1e5689SMatthias Ringwald l2cap_hid_interrupt_cid = 0; 458*6e1e5689SMatthias Ringwald } 4592d05904dSMatthias Ringwald default: 4602d05904dSMatthias Ringwald break; 4612d05904dSMatthias Ringwald } 462e85416c1SMatthias Ringwald break; 463e85416c1SMatthias Ringwald case L2CAP_DATA_PACKET: 464e85416c1SMatthias Ringwald // for now, just dump incoming data 465e85416c1SMatthias Ringwald if (channel == l2cap_hid_interrupt_cid){ 4661d5191c9SMatthias Ringwald hid_host_handle_interrupt_report(packet, size); 467e85416c1SMatthias Ringwald } else if (channel == l2cap_hid_control_cid){ 468e85416c1SMatthias Ringwald printf("HID Control: "); 4691d5191c9SMatthias Ringwald printf_hexdump(packet, size); 470e85416c1SMatthias Ringwald } else { 471e85416c1SMatthias Ringwald break; 472e85416c1SMatthias Ringwald } 4732d05904dSMatthias Ringwald default: 4742d05904dSMatthias Ringwald break; 4752d05904dSMatthias Ringwald } 4762d05904dSMatthias Ringwald } 4772d05904dSMatthias Ringwald /* LISTING_END */ 4782d05904dSMatthias Ringwald 4792d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 4802d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 4812d05904dSMatthias Ringwald 4822d05904dSMatthias Ringwald (void)argc; 4832d05904dSMatthias Ringwald (void)argv; 4842d05904dSMatthias Ringwald 4852d05904dSMatthias Ringwald hid_host_setup(); 4862d05904dSMatthias Ringwald 4872d05904dSMatthias Ringwald // parse human readable Bluetooth address 4882d05904dSMatthias Ringwald sscanf_bd_addr(remote_addr_string, remote_addr); 4892d05904dSMatthias Ringwald 4902d05904dSMatthias Ringwald // Turn on the device 4912d05904dSMatthias Ringwald hci_power_control(HCI_POWER_ON); 4922d05904dSMatthias Ringwald return 0; 4932d05904dSMatthias Ringwald } 4942d05904dSMatthias Ringwald 4952d05904dSMatthias Ringwald /* EXAMPLE_END */ 496