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 1442d05904dSMatthias Ringwald // register for HCI events 1452d05904dSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1462d05904dSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1472d05904dSMatthias Ringwald 148*6e82c1f8SMatthias Ringwald // Allow sniff mode requests by HID device 149*6e82c1f8SMatthias Ringwald gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE); 150*6e82c1f8SMatthias Ringwald 1511d5191c9SMatthias Ringwald // Disable stdout buffering 1521d5191c9SMatthias Ringwald setbuf(stdout, NULL); 1532d05904dSMatthias Ringwald } 1542d05904dSMatthias Ringwald /* LISTING_END */ 1552d05904dSMatthias Ringwald 1562d05904dSMatthias Ringwald /* @section SDP parser callback 1572d05904dSMatthias Ringwald * 1582d05904dSMatthias Ringwald * @text The SDP parsers retrieves the BNEP PAN UUID as explained in 1592d05904dSMatthias Ringwald * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}. 1602d05904dSMatthias Ringwald */ 1612d05904dSMatthias Ringwald 1622d05904dSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 1632d05904dSMatthias Ringwald 1642d05904dSMatthias Ringwald UNUSED(packet_type); 1652d05904dSMatthias Ringwald UNUSED(channel); 1662d05904dSMatthias Ringwald UNUSED(size); 1672d05904dSMatthias Ringwald 1682d05904dSMatthias Ringwald des_iterator_t attribute_list_it; 1692d05904dSMatthias Ringwald des_iterator_t additional_des_it; 1702d05904dSMatthias Ringwald des_iterator_t prot_it; 1712d05904dSMatthias Ringwald uint8_t *des_element; 1722d05904dSMatthias Ringwald uint8_t *element; 1732d05904dSMatthias Ringwald uint32_t uuid; 174e85416c1SMatthias Ringwald uint8_t status; 1752d05904dSMatthias Ringwald 1762d05904dSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 1772d05904dSMatthias Ringwald case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 1782d05904dSMatthias Ringwald if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 1792d05904dSMatthias Ringwald attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 1802d05904dSMatthias Ringwald if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 1812d05904dSMatthias Ringwald switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 1822d05904dSMatthias Ringwald case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 1832d05904dSMatthias Ringwald for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) { 1842d05904dSMatthias Ringwald if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue; 1852d05904dSMatthias Ringwald des_element = des_iterator_get_element(&attribute_list_it); 1862d05904dSMatthias Ringwald des_iterator_init(&prot_it, des_element); 1872d05904dSMatthias Ringwald element = des_iterator_get_element(&prot_it); 18814fd128cSMatthias Ringwald if (!element) continue; 1892d05904dSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 1902d05904dSMatthias Ringwald uuid = de_get_uuid32(element); 19114fd128cSMatthias Ringwald des_iterator_next(&prot_it); 1922d05904dSMatthias Ringwald switch (uuid){ 1932d05904dSMatthias Ringwald case BLUETOOTH_PROTOCOL_L2CAP: 1942d05904dSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 1952d05904dSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_control_psm); 1968d995dffSMatthias Ringwald printf("HID Control PSM: 0x%04x\n", (int) hid_control_psm); 1972d05904dSMatthias Ringwald break; 1982d05904dSMatthias Ringwald default: 1992d05904dSMatthias Ringwald break; 2002d05904dSMatthias Ringwald } 2012d05904dSMatthias Ringwald } 2022d05904dSMatthias Ringwald break; 2032d05904dSMatthias Ringwald case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: 2042d05904dSMatthias Ringwald for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) { 2052d05904dSMatthias Ringwald if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue; 2062d05904dSMatthias Ringwald des_element = des_iterator_get_element(&attribute_list_it); 2072d05904dSMatthias Ringwald for (des_iterator_init(&additional_des_it, des_element); des_iterator_has_more(&additional_des_it); des_iterator_next(&additional_des_it)) { 2082d05904dSMatthias Ringwald if (des_iterator_get_type(&additional_des_it) != DE_DES) continue; 2092d05904dSMatthias Ringwald des_element = des_iterator_get_element(&additional_des_it); 2102d05904dSMatthias Ringwald des_iterator_init(&prot_it, des_element); 2112d05904dSMatthias Ringwald element = des_iterator_get_element(&prot_it); 21214fd128cSMatthias Ringwald if (!element) continue; 2132d05904dSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 2142d05904dSMatthias Ringwald uuid = de_get_uuid32(element); 21514fd128cSMatthias Ringwald des_iterator_next(&prot_it); 2162d05904dSMatthias Ringwald switch (uuid){ 2172d05904dSMatthias Ringwald case BLUETOOTH_PROTOCOL_L2CAP: 2182d05904dSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 2192d05904dSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_interrupt_psm); 2208d995dffSMatthias Ringwald printf("HID Interrupt PSM: 0x%04x\n", (int) hid_interrupt_psm); 2212d05904dSMatthias Ringwald break; 2222d05904dSMatthias Ringwald default: 2232d05904dSMatthias Ringwald break; 2242d05904dSMatthias Ringwald } 2252d05904dSMatthias Ringwald } 2262d05904dSMatthias Ringwald } 2272d05904dSMatthias Ringwald break; 2282d05904dSMatthias Ringwald case BLUETOOTH_ATTRIBUTE_HID_DESCRIPTOR_LIST: 2291d5191c9SMatthias Ringwald for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) { 2301d5191c9SMatthias Ringwald if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue; 2311d5191c9SMatthias Ringwald des_element = des_iterator_get_element(&attribute_list_it); 2321d5191c9SMatthias Ringwald for (des_iterator_init(&additional_des_it, des_element); des_iterator_has_more(&additional_des_it); des_iterator_next(&additional_des_it)) { 2331d5191c9SMatthias Ringwald if (des_iterator_get_type(&additional_des_it) != DE_STRING) continue; 2341d5191c9SMatthias Ringwald element = des_iterator_get_element(&additional_des_it); 2351d5191c9SMatthias Ringwald const uint8_t * descriptor = de_get_string(element); 2361d5191c9SMatthias Ringwald hid_descriptor_len = de_get_data_size(element); 2371d5191c9SMatthias Ringwald memcpy(hid_descriptor, descriptor, hid_descriptor_len); 2382d05904dSMatthias Ringwald printf("HID Descriptor:\n"); 2392d05904dSMatthias Ringwald printf_hexdump(hid_descriptor, hid_descriptor_len); 2401d5191c9SMatthias Ringwald } 2411d5191c9SMatthias Ringwald } 2422d05904dSMatthias Ringwald break; 2432d05904dSMatthias Ringwald default: 2442d05904dSMatthias Ringwald break; 2452d05904dSMatthias Ringwald } 2462d05904dSMatthias Ringwald } 2472d05904dSMatthias Ringwald } else { 2482d05904dSMatthias 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)); 2492d05904dSMatthias Ringwald } 2502d05904dSMatthias Ringwald break; 2512d05904dSMatthias Ringwald 2522d05904dSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 2532d05904dSMatthias Ringwald if (!hid_control_psm) { 2542d05904dSMatthias Ringwald printf("HID Control PSM missing\n"); 2552d05904dSMatthias Ringwald break; 2562d05904dSMatthias Ringwald } 2572d05904dSMatthias Ringwald if (!hid_interrupt_psm) { 2582d05904dSMatthias Ringwald printf("HID Interrupt PSM missing\n"); 2592d05904dSMatthias Ringwald break; 2602d05904dSMatthias Ringwald } 2612d05904dSMatthias Ringwald printf("Setup HID\n"); 262e85416c1SMatthias Ringwald status = l2cap_create_channel(packet_handler, remote_addr, hid_control_psm, 48, &l2cap_hid_control_cid); 263e85416c1SMatthias Ringwald if (status){ 264e85416c1SMatthias Ringwald printf("Connecting to HID Control failed: 0x%02x\n", status); 265e85416c1SMatthias Ringwald } 2662d05904dSMatthias Ringwald break; 2672d05904dSMatthias Ringwald } 2682d05904dSMatthias Ringwald } 2692d05904dSMatthias Ringwald 2702d05904dSMatthias Ringwald /* 2711d5191c9SMatthias Ringwald * @section HID Report Handler 2721d5191c9SMatthias Ringwald * 2731d5191c9SMatthias Ringwald * @text Use BTstack's compact HID Parser to process incoming HID Report 2741d5191c9SMatthias Ringwald * Iterate over all fields and process fields with usage page = 0x07 / Keyboard 2751d5191c9SMatthias Ringwald * Check if SHIFT is down and process first character (don't handle multiple key presses) 2761d5191c9SMatthias Ringwald * 2771d5191c9SMatthias Ringwald */ 2781d5191c9SMatthias Ringwald #define NUM_KEYS 6 2791d5191c9SMatthias Ringwald static uint8_t last_keys[NUM_KEYS]; 2801d5191c9SMatthias Ringwald static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){ 2811d5191c9SMatthias Ringwald // check if HID Input Report 2821d5191c9SMatthias Ringwald if (report_len < 1) return; 2831d5191c9SMatthias Ringwald if (*report != 0xa1) return; 2841d5191c9SMatthias Ringwald report++; 2851d5191c9SMatthias Ringwald report_len--; 2861d5191c9SMatthias Ringwald btstack_hid_parser_t parser; 287a796c06fSMilanka Ringwald btstack_hid_parser_init(&parser, hid_descriptor, hid_descriptor_len, HID_REPORT_TYPE_INPUT, report, report_len); 2881d5191c9SMatthias Ringwald int shift = 0; 2891d5191c9SMatthias Ringwald uint8_t new_keys[NUM_KEYS]; 2901d5191c9SMatthias Ringwald memset(new_keys, 0, sizeof(new_keys)); 2911d5191c9SMatthias Ringwald int new_keys_count = 0; 2921d5191c9SMatthias Ringwald while (btstack_hid_parser_has_more(&parser)){ 2931d5191c9SMatthias Ringwald uint16_t usage_page; 2941d5191c9SMatthias Ringwald uint16_t usage; 2951d5191c9SMatthias Ringwald int32_t value; 2961d5191c9SMatthias Ringwald btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value); 2971d5191c9SMatthias Ringwald if (usage_page != 0x07) continue; 2981d5191c9SMatthias Ringwald switch (usage){ 2991d5191c9SMatthias Ringwald case 0xe1: 3001d5191c9SMatthias Ringwald case 0xe6: 3011d5191c9SMatthias Ringwald if (value){ 3021d5191c9SMatthias Ringwald shift = 1; 3031d5191c9SMatthias Ringwald } 3041d5191c9SMatthias Ringwald continue; 3051d5191c9SMatthias Ringwald case 0x00: 3061d5191c9SMatthias Ringwald continue; 3071d5191c9SMatthias Ringwald default: 3081d5191c9SMatthias Ringwald break; 3091d5191c9SMatthias Ringwald } 3101d5191c9SMatthias Ringwald if (usage >= sizeof(keytable_us_none)) continue; 3111d5191c9SMatthias Ringwald 3121d5191c9SMatthias Ringwald // store new keys 3131d5191c9SMatthias Ringwald new_keys[new_keys_count++] = usage; 3141d5191c9SMatthias Ringwald 3151d5191c9SMatthias Ringwald // check if usage was used last time (and ignore in that case) 3161d5191c9SMatthias Ringwald int i; 3171d5191c9SMatthias Ringwald for (i=0;i<NUM_KEYS;i++){ 3181d5191c9SMatthias Ringwald if (usage == last_keys[i]){ 3191d5191c9SMatthias Ringwald usage = 0; 3201d5191c9SMatthias Ringwald } 3211d5191c9SMatthias Ringwald } 3221d5191c9SMatthias Ringwald if (usage == 0) continue; 3231d5191c9SMatthias Ringwald 3241d5191c9SMatthias Ringwald uint8_t key; 3251d5191c9SMatthias Ringwald if (shift){ 3261d5191c9SMatthias Ringwald key = keytable_us_shift[usage]; 3271d5191c9SMatthias Ringwald } else { 3281d5191c9SMatthias Ringwald key = keytable_us_none[usage]; 3291d5191c9SMatthias Ringwald } 3301d5191c9SMatthias Ringwald if (key == CHAR_ILLEGAL) continue; 3311d5191c9SMatthias Ringwald if (key == CHAR_BACKSPACE){ 3321d5191c9SMatthias Ringwald printf("\b \b"); // go back one char, print space, go back one char again 3331d5191c9SMatthias Ringwald continue; 3341d5191c9SMatthias Ringwald } 3351d5191c9SMatthias Ringwald printf("%c", key); 3361d5191c9SMatthias Ringwald } 3371d5191c9SMatthias Ringwald memcpy(last_keys, new_keys, NUM_KEYS); 3381d5191c9SMatthias Ringwald } 3391d5191c9SMatthias Ringwald 3401d5191c9SMatthias Ringwald /* 3412d05904dSMatthias Ringwald * @section Packet Handler 3422d05904dSMatthias Ringwald * 3432d05904dSMatthias Ringwald * @text The packet handler responds to various HCI Events. 3442d05904dSMatthias Ringwald */ 3452d05904dSMatthias Ringwald 3462d05904dSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 3472d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 3482d05904dSMatthias Ringwald { 3492d05904dSMatthias Ringwald /* LISTING_PAUSE */ 3502d05904dSMatthias Ringwald uint8_t event; 3512d05904dSMatthias Ringwald bd_addr_t event_addr; 352e85416c1SMatthias Ringwald uint8_t status; 353e85416c1SMatthias Ringwald uint16_t l2cap_cid; 3542d05904dSMatthias Ringwald 3552d05904dSMatthias Ringwald /* LISTING_RESUME */ 3562d05904dSMatthias Ringwald switch (packet_type) { 3572d05904dSMatthias Ringwald case HCI_EVENT_PACKET: 3582d05904dSMatthias Ringwald event = hci_event_packet_get_type(packet); 3592d05904dSMatthias Ringwald switch (event) { 3602d05904dSMatthias Ringwald /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING 3612d05904dSMatthias Ringwald * is received and the example is started in client mode, the remote SDP HID query is started. 3622d05904dSMatthias Ringwald */ 3632d05904dSMatthias Ringwald case BTSTACK_EVENT_STATE: 3642d05904dSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 3652d05904dSMatthias Ringwald printf("Start SDP HID query for remote HID Device.\n"); 3662d05904dSMatthias Ringwald sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); 3672d05904dSMatthias Ringwald } 3682d05904dSMatthias Ringwald break; 3692d05904dSMatthias Ringwald 3702d05904dSMatthias Ringwald /* LISTING_PAUSE */ 3712d05904dSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 3722d05904dSMatthias Ringwald // inform about pin code request 3732d05904dSMatthias Ringwald printf("Pin code request - using '0000'\n"); 3742d05904dSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 3752d05904dSMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 3762d05904dSMatthias Ringwald break; 3772d05904dSMatthias Ringwald 3782d05904dSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 3792d05904dSMatthias Ringwald // inform about user confirmation request 380173fff9bSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8)); 3812d05904dSMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 3822d05904dSMatthias Ringwald break; 3832d05904dSMatthias Ringwald 3842d05904dSMatthias Ringwald /* LISTING_RESUME */ 385e85416c1SMatthias Ringwald 386e85416c1SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 387e85416c1SMatthias Ringwald status = packet[2]; 388e85416c1SMatthias Ringwald if (status){ 389e85416c1SMatthias Ringwald printf("L2CAP Connection failed: 0x%02x\n", status); 390e85416c1SMatthias Ringwald break; 391e85416c1SMatthias Ringwald } 392e85416c1SMatthias Ringwald l2cap_cid = little_endian_read_16(packet, 13); 393e85416c1SMatthias Ringwald if (!l2cap_cid) break; 394e85416c1SMatthias Ringwald if (l2cap_cid == l2cap_hid_control_cid){ 395e85416c1SMatthias Ringwald status = l2cap_create_channel(packet_handler, remote_addr, hid_interrupt_psm, 48, &l2cap_hid_interrupt_cid); 396e85416c1SMatthias Ringwald if (status){ 397e85416c1SMatthias Ringwald printf("Connecting to HID Control failed: 0x%02x\n", status); 398e85416c1SMatthias Ringwald break; 399e85416c1SMatthias Ringwald } 400e85416c1SMatthias Ringwald } 401e85416c1SMatthias Ringwald if (l2cap_cid == l2cap_hid_interrupt_cid){ 402e85416c1SMatthias Ringwald printf("HID Connection established\n"); 403e85416c1SMatthias Ringwald } 404e85416c1SMatthias Ringwald break; 4052d05904dSMatthias Ringwald default: 4062d05904dSMatthias Ringwald break; 4072d05904dSMatthias Ringwald } 408e85416c1SMatthias Ringwald break; 409e85416c1SMatthias Ringwald case L2CAP_DATA_PACKET: 410e85416c1SMatthias Ringwald // for now, just dump incoming data 411e85416c1SMatthias Ringwald if (channel == l2cap_hid_interrupt_cid){ 4121d5191c9SMatthias Ringwald hid_host_handle_interrupt_report(packet, size); 413e85416c1SMatthias Ringwald } else if (channel == l2cap_hid_control_cid){ 414e85416c1SMatthias Ringwald printf("HID Control: "); 4151d5191c9SMatthias Ringwald printf_hexdump(packet, size); 416e85416c1SMatthias Ringwald } else { 417e85416c1SMatthias Ringwald break; 418e85416c1SMatthias Ringwald } 4192d05904dSMatthias Ringwald default: 4202d05904dSMatthias Ringwald break; 4212d05904dSMatthias Ringwald } 4222d05904dSMatthias Ringwald } 4232d05904dSMatthias Ringwald /* LISTING_END */ 4242d05904dSMatthias Ringwald 4252d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 4262d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 4272d05904dSMatthias Ringwald 4282d05904dSMatthias Ringwald (void)argc; 4292d05904dSMatthias Ringwald (void)argv; 4302d05904dSMatthias Ringwald 4312d05904dSMatthias Ringwald hid_host_setup(); 4322d05904dSMatthias Ringwald 4332d05904dSMatthias Ringwald // parse human readable Bluetooth address 4342d05904dSMatthias Ringwald sscanf_bd_addr(remote_addr_string, remote_addr); 4352d05904dSMatthias Ringwald 4362d05904dSMatthias Ringwald // Turn on the device 4372d05904dSMatthias Ringwald hci_power_control(HCI_POWER_ON); 4382d05904dSMatthias Ringwald return 0; 4392d05904dSMatthias Ringwald } 4402d05904dSMatthias Ringwald 4412d05904dSMatthias Ringwald /* EXAMPLE_END */ 442