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 44ec8ae085SMilanka Ringwald /* EXAMPLE_START(hid_host_demo): HID Host Classic 452d05904dSMatthias Ringwald * 46*d93f9014SMilanka Ringwald * @text This example implements a HID Host. For now, it connnects to a fixed device. 47*d93f9014SMilanka Ringwald * It will connect in Report protocol mode if this mode is suported by the HID Device, 48*d93f9014SMilanka Ringwald * otherwise it will fall back to BOOT protocol mode. 492d05904dSMatthias Ringwald */ 502d05904dSMatthias Ringwald 51173fff9bSMatthias Ringwald #include <inttypes.h> 522d05904dSMatthias Ringwald #include <stdio.h> 532d05904dSMatthias Ringwald 542d05904dSMatthias Ringwald #include "btstack_config.h" 552d05904dSMatthias Ringwald #include "btstack.h" 562d05904dSMatthias Ringwald 5767c74d26SMatthias Ringwald #define MAX_ATTRIBUTE_VALUE_SIZE 300 582d05904dSMatthias Ringwald 5905439aa6SMilanka Ringwald // MBP 2016 static const char * remote_addr_string = "F4-0F-24-3B-1B-E1"; 602d05904dSMatthias Ringwald // iMpulse static const char * remote_addr_string = "64:6E:6C:C1:AA:B5"; 6105439aa6SMilanka Ringwald // Logitec 6205439aa6SMilanka Ringwald static const char * remote_addr_string = "00:1F:20:86:DF:52"; 632d05904dSMatthias Ringwald 642d05904dSMatthias Ringwald static bd_addr_t remote_addr; 652d05904dSMatthias Ringwald 662d05904dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 672d05904dSMatthias Ringwald 681d5191c9SMatthias Ringwald // Simplified US Keyboard with Shift modifier 691d5191c9SMatthias Ringwald 701d5191c9SMatthias Ringwald #define CHAR_ILLEGAL 0xff 711d5191c9SMatthias Ringwald #define CHAR_RETURN '\n' 721d5191c9SMatthias Ringwald #define CHAR_ESCAPE 27 731d5191c9SMatthias Ringwald #define CHAR_TAB '\t' 741d5191c9SMatthias Ringwald #define CHAR_BACKSPACE 0x7f 751d5191c9SMatthias Ringwald 761d5191c9SMatthias Ringwald /** 771d5191c9SMatthias Ringwald * English (US) 781d5191c9SMatthias Ringwald */ 791d5191c9SMatthias Ringwald static const uint8_t keytable_us_none [] = { 801d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 811d5191c9SMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 821d5191c9SMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 831d5191c9SMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 841d5191c9SMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 851d5191c9SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 861d5191c9SMatthias Ringwald '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 871d5191c9SMatthias Ringwald '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 881d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 891d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 901d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 911d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 921d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 931d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 941d5191c9SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 951d5191c9SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 961d5191c9SMatthias Ringwald }; 971d5191c9SMatthias Ringwald 981d5191c9SMatthias Ringwald static const uint8_t keytable_us_shift[] = { 991d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 1001d5191c9SMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 1011d5191c9SMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 1021d5191c9SMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 1031d5191c9SMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 1041d5191c9SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 1051d5191c9SMatthias Ringwald '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 1061d5191c9SMatthias Ringwald '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 1071d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 1081d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 1091d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 1101d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 1111d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 1121d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 1131d5191c9SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 1141d5191c9SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 1151d5191c9SMatthias Ringwald }; 1161d5191c9SMatthias Ringwald 11705439aa6SMilanka Ringwald // SDP 118*d93f9014SMilanka Ringwald static uint8_t hid_descriptor_storage[MAX_ATTRIBUTE_VALUE_SIZE]; 1192d05904dSMatthias Ringwald 12005439aa6SMilanka Ringwald // App 12105439aa6SMilanka Ringwald static enum { 12205439aa6SMilanka Ringwald APP_IDLE, 12305439aa6SMilanka Ringwald APP_CONNECTED 12405439aa6SMilanka Ringwald } app_state = APP_IDLE; 12505439aa6SMilanka Ringwald 12605439aa6SMilanka Ringwald static uint16_t hid_host_cid = 0; 12705439aa6SMilanka Ringwald static bool hid_host_descriptor_available = false; 128e615b0ecSMilanka Ringwald static hid_protocol_mode_t hid_host_report_mode = HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT; 129e615b0ecSMilanka Ringwald 1302d05904dSMatthias Ringwald /* @section Main application configuration 1312d05904dSMatthias Ringwald * 132*d93f9014SMilanka Ringwald * @text In the application configuration, L2CAP and HID host are initialized, and the link policies 133*d93f9014SMilanka Ringwald * are set to allow sniff mode and role change. 1342d05904dSMatthias Ringwald */ 1352d05904dSMatthias Ringwald 1362d05904dSMatthias Ringwald /* LISTING_START(PanuSetup): Panu setup */ 1372d05904dSMatthias Ringwald static void packet_handler (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 14405439aa6SMilanka Ringwald // Initialize HID Host 145*d93f9014SMilanka Ringwald hid_host_init(hid_descriptor_storage, sizeof(hid_descriptor_storage)); 14605439aa6SMilanka Ringwald hid_host_register_packet_handler(packet_handler); 1476e1e5689SMatthias Ringwald 1488e0c4421SMatthias Ringwald // Allow sniff mode requests by HID device and support role switch 1498e0c4421SMatthias Ringwald gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH); 1508e0c4421SMatthias Ringwald 1518e0c4421SMatthias Ringwald // try to become master on incoming connections 1528e0c4421SMatthias Ringwald hci_set_master_slave_policy(HCI_ROLE_MASTER); 1536e1e5689SMatthias Ringwald 1542d05904dSMatthias Ringwald // register for HCI events 1552d05904dSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1562d05904dSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1572d05904dSMatthias Ringwald 1581d5191c9SMatthias Ringwald // Disable stdout buffering 1591d5191c9SMatthias Ringwald setbuf(stdout, NULL); 1602d05904dSMatthias Ringwald } 1612d05904dSMatthias Ringwald /* LISTING_END */ 1622d05904dSMatthias Ringwald 1632d05904dSMatthias Ringwald /* 1641d5191c9SMatthias Ringwald * @section HID Report Handler 1651d5191c9SMatthias Ringwald * 166*d93f9014SMilanka Ringwald * @text Use BTstack's compact HID Parser to process incoming HID Report in Report protocol mode. 1671d5191c9SMatthias Ringwald * Iterate over all fields and process fields with usage page = 0x07 / Keyboard 1681d5191c9SMatthias Ringwald * Check if SHIFT is down and process first character (don't handle multiple key presses) 1691d5191c9SMatthias Ringwald * 1701d5191c9SMatthias Ringwald */ 171*d93f9014SMilanka Ringwald 1721d5191c9SMatthias Ringwald #define NUM_KEYS 6 1731d5191c9SMatthias Ringwald static uint8_t last_keys[NUM_KEYS]; 1741d5191c9SMatthias Ringwald static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){ 1751d5191c9SMatthias Ringwald // check if HID Input Report 1761d5191c9SMatthias Ringwald if (report_len < 1) return; 1771d5191c9SMatthias Ringwald if (*report != 0xa1) return; 17805439aa6SMilanka Ringwald 1791d5191c9SMatthias Ringwald report++; 1801d5191c9SMatthias Ringwald report_len--; 18105439aa6SMilanka Ringwald 1821d5191c9SMatthias Ringwald btstack_hid_parser_t parser; 18305439aa6SMilanka Ringwald btstack_hid_parser_init(&parser, 18405439aa6SMilanka Ringwald hid_descriptor_storage_get_descriptor_data(hid_host_cid), 18505439aa6SMilanka Ringwald hid_descriptor_storage_get_descriptor_len(hid_host_cid), 18605439aa6SMilanka Ringwald HID_REPORT_TYPE_INPUT, report, report_len); 18705439aa6SMilanka Ringwald 1881d5191c9SMatthias Ringwald int shift = 0; 1891d5191c9SMatthias Ringwald uint8_t new_keys[NUM_KEYS]; 1901d5191c9SMatthias Ringwald memset(new_keys, 0, sizeof(new_keys)); 1911d5191c9SMatthias Ringwald int new_keys_count = 0; 1921d5191c9SMatthias Ringwald while (btstack_hid_parser_has_more(&parser)){ 1931d5191c9SMatthias Ringwald uint16_t usage_page; 1941d5191c9SMatthias Ringwald uint16_t usage; 1951d5191c9SMatthias Ringwald int32_t value; 1961d5191c9SMatthias Ringwald btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value); 1971d5191c9SMatthias Ringwald if (usage_page != 0x07) continue; 1981d5191c9SMatthias Ringwald switch (usage){ 1991d5191c9SMatthias Ringwald case 0xe1: 2001d5191c9SMatthias Ringwald case 0xe6: 2011d5191c9SMatthias Ringwald if (value){ 2021d5191c9SMatthias Ringwald shift = 1; 2031d5191c9SMatthias Ringwald } 2041d5191c9SMatthias Ringwald continue; 2051d5191c9SMatthias Ringwald case 0x00: 2061d5191c9SMatthias Ringwald continue; 2071d5191c9SMatthias Ringwald default: 2081d5191c9SMatthias Ringwald break; 2091d5191c9SMatthias Ringwald } 2101d5191c9SMatthias Ringwald if (usage >= sizeof(keytable_us_none)) continue; 2111d5191c9SMatthias Ringwald 2121d5191c9SMatthias Ringwald // store new keys 2131d5191c9SMatthias Ringwald new_keys[new_keys_count++] = usage; 2141d5191c9SMatthias Ringwald 2151d5191c9SMatthias Ringwald // check if usage was used last time (and ignore in that case) 2161d5191c9SMatthias Ringwald int i; 2171d5191c9SMatthias Ringwald for (i=0;i<NUM_KEYS;i++){ 2181d5191c9SMatthias Ringwald if (usage == last_keys[i]){ 2191d5191c9SMatthias Ringwald usage = 0; 2201d5191c9SMatthias Ringwald } 2211d5191c9SMatthias Ringwald } 2221d5191c9SMatthias Ringwald if (usage == 0) continue; 2231d5191c9SMatthias Ringwald 2241d5191c9SMatthias Ringwald uint8_t key; 2251d5191c9SMatthias Ringwald if (shift){ 2261d5191c9SMatthias Ringwald key = keytable_us_shift[usage]; 2271d5191c9SMatthias Ringwald } else { 2281d5191c9SMatthias Ringwald key = keytable_us_none[usage]; 2291d5191c9SMatthias Ringwald } 2301d5191c9SMatthias Ringwald if (key == CHAR_ILLEGAL) continue; 2311d5191c9SMatthias Ringwald if (key == CHAR_BACKSPACE){ 2321d5191c9SMatthias Ringwald printf("\b \b"); // go back one char, print space, go back one char again 2331d5191c9SMatthias Ringwald continue; 2341d5191c9SMatthias Ringwald } 2351d5191c9SMatthias Ringwald printf("%c", key); 2361d5191c9SMatthias Ringwald } 2371d5191c9SMatthias Ringwald memcpy(last_keys, new_keys, NUM_KEYS); 2381d5191c9SMatthias Ringwald } 2391d5191c9SMatthias Ringwald 2401d5191c9SMatthias Ringwald /* 2412d05904dSMatthias Ringwald * @section Packet Handler 2422d05904dSMatthias Ringwald * 243*d93f9014SMilanka Ringwald * @text The packet handler responds to various HID events. 2442d05904dSMatthias Ringwald */ 2452d05904dSMatthias Ringwald 2462d05904dSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 2472d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 2482d05904dSMatthias Ringwald { 2492d05904dSMatthias Ringwald /* LISTING_PAUSE */ 25005439aa6SMilanka Ringwald UNUSED(channel); 25105439aa6SMilanka Ringwald UNUSED(size); 25205439aa6SMilanka Ringwald 2532d05904dSMatthias Ringwald uint8_t event; 2542d05904dSMatthias Ringwald bd_addr_t event_addr; 255e85416c1SMatthias Ringwald uint8_t status; 2562d05904dSMatthias Ringwald 2572d05904dSMatthias Ringwald /* LISTING_RESUME */ 2582d05904dSMatthias Ringwald switch (packet_type) { 2592d05904dSMatthias Ringwald case HCI_EVENT_PACKET: 2602d05904dSMatthias Ringwald event = hci_event_packet_get_type(packet); 26105439aa6SMilanka Ringwald 2622d05904dSMatthias Ringwald switch (event) { 26305439aa6SMilanka Ringwald #ifndef HAVE_BTSTACK_STDIN 2642d05904dSMatthias Ringwald /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING 2652d05904dSMatthias Ringwald * is received and the example is started in client mode, the remote SDP HID query is started. 2662d05904dSMatthias Ringwald */ 2672d05904dSMatthias Ringwald case BTSTACK_EVENT_STATE: 2682d05904dSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 269e615b0ecSMilanka Ringwald status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid); 27005439aa6SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 271*d93f9014SMilanka Ringwald printf("HID host connect failed, status 0x%02x.\n", status); 27205439aa6SMilanka Ringwald } 2732d05904dSMatthias Ringwald } 2742d05904dSMatthias Ringwald break; 27505439aa6SMilanka Ringwald #endif 2762d05904dSMatthias Ringwald /* LISTING_PAUSE */ 2772d05904dSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 2782d05904dSMatthias Ringwald // inform about pin code request 2792d05904dSMatthias Ringwald printf("Pin code request - using '0000'\n"); 2802d05904dSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 2812d05904dSMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 2822d05904dSMatthias Ringwald break; 2832d05904dSMatthias Ringwald 2842d05904dSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 2852d05904dSMatthias Ringwald // inform about user confirmation request 286173fff9bSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8)); 2872d05904dSMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 2882d05904dSMatthias Ringwald break; 2892d05904dSMatthias Ringwald 2902d05904dSMatthias Ringwald /* LISTING_RESUME */ 29105439aa6SMilanka Ringwald case HCI_EVENT_HID_META: 29205439aa6SMilanka Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 293*d93f9014SMilanka Ringwald 29405439aa6SMilanka Ringwald case HID_SUBEVENT_INCOMING_CONNECTION: 295*d93f9014SMilanka Ringwald // There is an incoming connection: we can accept it or decline it. 296*d93f9014SMilanka Ringwald // The hid_host_report_mode in the hid_host_accept_connection function 297*d93f9014SMilanka Ringwald // allows the application to request a protocol mode. 298*d93f9014SMilanka Ringwald // For available protocol modes, see hid_protocol_mode_t in hid.h file. 299e615b0ecSMilanka Ringwald hid_host_accept_connection(hid_subevent_incoming_connection_get_hid_cid(packet), hid_host_report_mode); 3006e1e5689SMatthias Ringwald break; 3016e1e5689SMatthias Ringwald 30205439aa6SMilanka Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 303*d93f9014SMilanka Ringwald // The status field of this event indicates if the control and interrupt 304*d93f9014SMilanka Ringwald // connections were opened successfully. 30505439aa6SMilanka Ringwald status = hid_subevent_connection_opened_get_status(packet); 306*d93f9014SMilanka Ringwald if (status != ERROR_CODE_SUCCESS) { 30705439aa6SMilanka Ringwald printf("Connection failed, status 0x%x\n", status); 30805439aa6SMilanka Ringwald app_state = APP_IDLE; 30905439aa6SMilanka Ringwald hid_host_cid = 0; 31005439aa6SMilanka Ringwald return; 311e85416c1SMatthias Ringwald } 31205439aa6SMilanka Ringwald app_state = APP_CONNECTED; 31305439aa6SMilanka Ringwald hid_host_descriptor_available = false; 31405439aa6SMilanka Ringwald hid_host_cid = hid_subevent_connection_opened_get_hid_cid(packet); 315*d93f9014SMilanka Ringwald printf("HID Host connected.\n"); 31605439aa6SMilanka Ringwald break; 31705439aa6SMilanka Ringwald 31805439aa6SMilanka Ringwald case HID_SUBEVENT_DESCRIPTOR_AVAILABLE: 319*d93f9014SMilanka Ringwald // This event will follows HID_SUBEVENT_CONNECTION_OPENED event. 320*d93f9014SMilanka Ringwald // For incoming connections, i.e. HID Device initiating the connection, 321*d93f9014SMilanka Ringwald // the HID_SUBEVENT_DESCRIPTOR_AVAILABLE is delayed, and some HID 322*d93f9014SMilanka Ringwald // reports may be received via HID_SUBEVENT_REPORT event. It is up to 323*d93f9014SMilanka Ringwald // the application if these reports should be buffered or ignored until 324*d93f9014SMilanka Ringwald // the HID descriptor is available. 32505439aa6SMilanka Ringwald status = hid_subevent_descriptor_available_get_status(packet); 32605439aa6SMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 32705439aa6SMilanka Ringwald hid_host_descriptor_available = true; 328*d93f9014SMilanka Ringwald printf("HID Descriptor available, please start typing.\n"); 329e615b0ecSMilanka Ringwald } else { 330e615b0ecSMilanka Ringwald printf("Cannot handle input report, HID Descriptor is not available.\n"); 3316e1e5689SMatthias Ringwald } 332e85416c1SMatthias Ringwald break; 33305439aa6SMilanka Ringwald 334*d93f9014SMilanka Ringwald case HID_SUBEVENT_REPORT: 335*d93f9014SMilanka Ringwald // Handle input report. 336*d93f9014SMilanka Ringwald if (hid_host_descriptor_available){ 337*d93f9014SMilanka Ringwald hid_host_handle_interrupt_report(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet)); 338*d93f9014SMilanka Ringwald } else { 339*d93f9014SMilanka Ringwald printf_hexdump(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet)); 3402d05904dSMatthias Ringwald } 341e85416c1SMatthias Ringwald break; 34205439aa6SMilanka Ringwald 34305439aa6SMilanka Ringwald case HID_SUBEVENT_SET_PROTOCOL_RESPONSE: 344*d93f9014SMilanka Ringwald // For incoming connections, the library will set the protocol mode of the 345*d93f9014SMilanka Ringwald // HID Device as requested in the call to hid_host_accept_connection. The event 346*d93f9014SMilanka Ringwald // reports the result. For connections initiated by calling hid_host_connect, 347*d93f9014SMilanka Ringwald // this event will occur only if the established report mode is boot mode. 348f1aa1cdcSMilanka Ringwald status = hid_subevent_set_protocol_response_get_handshake_status(packet); 34905439aa6SMilanka Ringwald if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 35005439aa6SMilanka Ringwald printf("Error set protocol, status 0x%02x\n", status); 351e85416c1SMatthias Ringwald break; 352e85416c1SMatthias Ringwald } 353f1aa1cdcSMilanka Ringwald switch ((hid_protocol_mode_t)hid_subevent_set_protocol_response_get_protocol_mode(packet)){ 354f1aa1cdcSMilanka Ringwald case HID_PROTOCOL_MODE_BOOT: 355e615b0ecSMilanka Ringwald printf("Protocol mode set: BOOT.\n"); 356f1aa1cdcSMilanka Ringwald break; 357f1aa1cdcSMilanka Ringwald case HID_PROTOCOL_MODE_REPORT: 358e615b0ecSMilanka Ringwald printf("Protocol mode set: REPORT.\n"); 359f1aa1cdcSMilanka Ringwald break; 360f1aa1cdcSMilanka Ringwald default: 361f1aa1cdcSMilanka Ringwald printf("Unknown protocol mode.\n"); 362f1aa1cdcSMilanka Ringwald break; 363f1aa1cdcSMilanka Ringwald } 36405439aa6SMilanka Ringwald break; 36505439aa6SMilanka Ringwald 366*d93f9014SMilanka Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 367*d93f9014SMilanka Ringwald // The connection was closed. 368*d93f9014SMilanka Ringwald hid_host_cid = 0; 369*d93f9014SMilanka Ringwald hid_host_descriptor_available = false; 370*d93f9014SMilanka Ringwald printf("HID Host disconnected.\n"); 37105439aa6SMilanka Ringwald break; 37205439aa6SMilanka Ringwald 37305439aa6SMilanka Ringwald default: 37405439aa6SMilanka Ringwald break; 37505439aa6SMilanka Ringwald } 37605439aa6SMilanka Ringwald break; 37705439aa6SMilanka Ringwald default: 37805439aa6SMilanka Ringwald break; 37905439aa6SMilanka Ringwald } 38005439aa6SMilanka Ringwald break; 3812d05904dSMatthias Ringwald default: 3822d05904dSMatthias Ringwald break; 3832d05904dSMatthias Ringwald } 3842d05904dSMatthias Ringwald } 3852d05904dSMatthias Ringwald /* LISTING_END */ 3862d05904dSMatthias Ringwald 38705439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN 38805439aa6SMilanka Ringwald static void show_usage(void){ 38905439aa6SMilanka Ringwald bd_addr_t iut_address; 39005439aa6SMilanka Ringwald gap_local_bd_addr(iut_address); 391*d93f9014SMilanka Ringwald printf("\n--- Bluetooth HID Host Console %s ---\n", bd_addr_to_str(iut_address)); 392*d93f9014SMilanka Ringwald printf("c - Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string); 393*d93f9014SMilanka Ringwald printf("C - Disconnect\n"); 39405439aa6SMilanka Ringwald 39505439aa6SMilanka Ringwald printf("\n"); 39605439aa6SMilanka Ringwald printf("Ctrl-c - exit\n"); 39705439aa6SMilanka Ringwald printf("---\n"); 39805439aa6SMilanka Ringwald } 39905439aa6SMilanka Ringwald 40005439aa6SMilanka Ringwald static void stdin_process(char cmd){ 40105439aa6SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 40205439aa6SMilanka Ringwald switch (cmd){ 40305439aa6SMilanka Ringwald case 'c': 404*d93f9014SMilanka Ringwald printf("Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string); 405e615b0ecSMilanka Ringwald status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid); 40605439aa6SMilanka Ringwald break; 40705439aa6SMilanka Ringwald case 'C': 408*d93f9014SMilanka Ringwald printf("Disconnect...\n"); 40905439aa6SMilanka Ringwald hid_host_disconnect(hid_host_cid); 41005439aa6SMilanka Ringwald break; 41105439aa6SMilanka Ringwald case '\n': 41205439aa6SMilanka Ringwald case '\r': 41305439aa6SMilanka Ringwald break; 41405439aa6SMilanka Ringwald default: 41505439aa6SMilanka Ringwald show_usage(); 41605439aa6SMilanka Ringwald break; 41705439aa6SMilanka Ringwald } 41805439aa6SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 41905439aa6SMilanka Ringwald printf("HID host cmd \'%c\' failed, status 0x%02x\n", cmd, status); 42005439aa6SMilanka Ringwald } 42105439aa6SMilanka Ringwald } 42205439aa6SMilanka Ringwald #endif 42305439aa6SMilanka Ringwald 4242d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 4252d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 4262d05904dSMatthias Ringwald 4272d05904dSMatthias Ringwald (void)argc; 4282d05904dSMatthias Ringwald (void)argv; 4292d05904dSMatthias Ringwald 4302d05904dSMatthias Ringwald hid_host_setup(); 4312d05904dSMatthias Ringwald 4322d05904dSMatthias Ringwald // parse human readable Bluetooth address 4332d05904dSMatthias Ringwald sscanf_bd_addr(remote_addr_string, remote_addr); 4342d05904dSMatthias Ringwald 43505439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN 43605439aa6SMilanka Ringwald btstack_stdin_setup(stdin_process); 43705439aa6SMilanka Ringwald #endif 43805439aa6SMilanka Ringwald 4392d05904dSMatthias Ringwald // Turn on the device 4402d05904dSMatthias Ringwald hci_power_control(HCI_POWER_ON); 4412d05904dSMatthias Ringwald return 0; 4422d05904dSMatthias Ringwald } 4432d05904dSMatthias Ringwald 4442d05904dSMatthias Ringwald /* EXAMPLE_END */ 445