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 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH 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 * 469e8a7569SMatthias Ringwald * @text This example implements a HID Host. For now, it connects to a fixed device. 479e8a7569SMatthias Ringwald * It will connect in Report protocol mode if this mode is supported by the HID Device, 48d93f9014SMilanka 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 118d93f9014SMilanka 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 * 132d93f9014SMilanka Ringwald * @text In the application configuration, L2CAP and HID host are initialized, and the link policies 133d93f9014SMilanka 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 1448c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE 1458c9bb29eSMatthias Ringwald // Initialize LE Security Manager. Needed for cross-transport key derivation 1468c9bb29eSMatthias Ringwald sm_init(); 1478c9bb29eSMatthias Ringwald #endif 1488c9bb29eSMatthias Ringwald 14905439aa6SMilanka Ringwald // Initialize HID Host 150d93f9014SMilanka Ringwald hid_host_init(hid_descriptor_storage, sizeof(hid_descriptor_storage)); 15105439aa6SMilanka Ringwald hid_host_register_packet_handler(packet_handler); 1526e1e5689SMatthias Ringwald 1538e0c4421SMatthias Ringwald // Allow sniff mode requests by HID device and support role switch 1548e0c4421SMatthias Ringwald gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH); 1558e0c4421SMatthias Ringwald 1568e0c4421SMatthias Ringwald // try to become master on incoming connections 1578e0c4421SMatthias Ringwald hci_set_master_slave_policy(HCI_ROLE_MASTER); 1586e1e5689SMatthias Ringwald 1592d05904dSMatthias Ringwald // register for HCI events 1602d05904dSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1612d05904dSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1622d05904dSMatthias Ringwald 1631d5191c9SMatthias Ringwald // Disable stdout buffering 164*9d010406SMatthias Ringwald setvbuf(stdin, NULL, _IONBF, 0); 1652d05904dSMatthias Ringwald } 1662d05904dSMatthias Ringwald /* LISTING_END */ 1672d05904dSMatthias Ringwald 1682d05904dSMatthias Ringwald /* 1691d5191c9SMatthias Ringwald * @section HID Report Handler 1701d5191c9SMatthias Ringwald * 171d93f9014SMilanka Ringwald * @text Use BTstack's compact HID Parser to process incoming HID Report in Report protocol mode. 1721d5191c9SMatthias Ringwald * Iterate over all fields and process fields with usage page = 0x07 / Keyboard 1731d5191c9SMatthias Ringwald * Check if SHIFT is down and process first character (don't handle multiple key presses) 1741d5191c9SMatthias Ringwald * 1751d5191c9SMatthias Ringwald */ 176d93f9014SMilanka Ringwald 1771d5191c9SMatthias Ringwald #define NUM_KEYS 6 1781d5191c9SMatthias Ringwald static uint8_t last_keys[NUM_KEYS]; 1791d5191c9SMatthias Ringwald static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){ 1801d5191c9SMatthias Ringwald // check if HID Input Report 1811d5191c9SMatthias Ringwald if (report_len < 1) return; 1821d5191c9SMatthias Ringwald if (*report != 0xa1) return; 18305439aa6SMilanka Ringwald 1841d5191c9SMatthias Ringwald report++; 1851d5191c9SMatthias Ringwald report_len--; 18605439aa6SMilanka Ringwald 1871d5191c9SMatthias Ringwald btstack_hid_parser_t parser; 18805439aa6SMilanka Ringwald btstack_hid_parser_init(&parser, 18905439aa6SMilanka Ringwald hid_descriptor_storage_get_descriptor_data(hid_host_cid), 19005439aa6SMilanka Ringwald hid_descriptor_storage_get_descriptor_len(hid_host_cid), 19105439aa6SMilanka Ringwald HID_REPORT_TYPE_INPUT, report, report_len); 19205439aa6SMilanka Ringwald 1931d5191c9SMatthias Ringwald int shift = 0; 1941d5191c9SMatthias Ringwald uint8_t new_keys[NUM_KEYS]; 1951d5191c9SMatthias Ringwald memset(new_keys, 0, sizeof(new_keys)); 1961d5191c9SMatthias Ringwald int new_keys_count = 0; 1971d5191c9SMatthias Ringwald while (btstack_hid_parser_has_more(&parser)){ 1981d5191c9SMatthias Ringwald uint16_t usage_page; 1991d5191c9SMatthias Ringwald uint16_t usage; 2001d5191c9SMatthias Ringwald int32_t value; 2011d5191c9SMatthias Ringwald btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value); 2021d5191c9SMatthias Ringwald if (usage_page != 0x07) continue; 2031d5191c9SMatthias Ringwald switch (usage){ 2041d5191c9SMatthias Ringwald case 0xe1: 2051d5191c9SMatthias Ringwald case 0xe6: 2061d5191c9SMatthias Ringwald if (value){ 2071d5191c9SMatthias Ringwald shift = 1; 2081d5191c9SMatthias Ringwald } 2091d5191c9SMatthias Ringwald continue; 2101d5191c9SMatthias Ringwald case 0x00: 2111d5191c9SMatthias Ringwald continue; 2121d5191c9SMatthias Ringwald default: 2131d5191c9SMatthias Ringwald break; 2141d5191c9SMatthias Ringwald } 2151d5191c9SMatthias Ringwald if (usage >= sizeof(keytable_us_none)) continue; 2161d5191c9SMatthias Ringwald 2171d5191c9SMatthias Ringwald // store new keys 218*9d010406SMatthias Ringwald new_keys[new_keys_count++] = (uint8_t) usage; 2191d5191c9SMatthias Ringwald 2201d5191c9SMatthias Ringwald // check if usage was used last time (and ignore in that case) 2211d5191c9SMatthias Ringwald int i; 2221d5191c9SMatthias Ringwald for (i=0;i<NUM_KEYS;i++){ 2231d5191c9SMatthias Ringwald if (usage == last_keys[i]){ 2241d5191c9SMatthias Ringwald usage = 0; 2251d5191c9SMatthias Ringwald } 2261d5191c9SMatthias Ringwald } 2271d5191c9SMatthias Ringwald if (usage == 0) continue; 2281d5191c9SMatthias Ringwald 2291d5191c9SMatthias Ringwald uint8_t key; 2301d5191c9SMatthias Ringwald if (shift){ 2311d5191c9SMatthias Ringwald key = keytable_us_shift[usage]; 2321d5191c9SMatthias Ringwald } else { 2331d5191c9SMatthias Ringwald key = keytable_us_none[usage]; 2341d5191c9SMatthias Ringwald } 2351d5191c9SMatthias Ringwald if (key == CHAR_ILLEGAL) continue; 2361d5191c9SMatthias Ringwald if (key == CHAR_BACKSPACE){ 2371d5191c9SMatthias Ringwald printf("\b \b"); // go back one char, print space, go back one char again 2381d5191c9SMatthias Ringwald continue; 2391d5191c9SMatthias Ringwald } 2401d5191c9SMatthias Ringwald printf("%c", key); 2411d5191c9SMatthias Ringwald } 2421d5191c9SMatthias Ringwald memcpy(last_keys, new_keys, NUM_KEYS); 2431d5191c9SMatthias Ringwald } 2441d5191c9SMatthias Ringwald 2451d5191c9SMatthias Ringwald /* 2462d05904dSMatthias Ringwald * @section Packet Handler 2472d05904dSMatthias Ringwald * 248d93f9014SMilanka Ringwald * @text The packet handler responds to various HID events. 2492d05904dSMatthias Ringwald */ 2502d05904dSMatthias Ringwald 2512d05904dSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 2522d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 2532d05904dSMatthias Ringwald { 2542d05904dSMatthias Ringwald /* LISTING_PAUSE */ 25505439aa6SMilanka Ringwald UNUSED(channel); 25605439aa6SMilanka Ringwald UNUSED(size); 25705439aa6SMilanka Ringwald 2582d05904dSMatthias Ringwald uint8_t event; 2592d05904dSMatthias Ringwald bd_addr_t event_addr; 260e85416c1SMatthias Ringwald uint8_t status; 2612d05904dSMatthias Ringwald 2622d05904dSMatthias Ringwald /* LISTING_RESUME */ 2632d05904dSMatthias Ringwald switch (packet_type) { 2642d05904dSMatthias Ringwald case HCI_EVENT_PACKET: 2652d05904dSMatthias Ringwald event = hci_event_packet_get_type(packet); 26605439aa6SMilanka Ringwald 2672d05904dSMatthias Ringwald switch (event) { 26805439aa6SMilanka Ringwald #ifndef HAVE_BTSTACK_STDIN 2692d05904dSMatthias Ringwald /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING 2702d05904dSMatthias Ringwald * is received and the example is started in client mode, the remote SDP HID query is started. 2712d05904dSMatthias Ringwald */ 2722d05904dSMatthias Ringwald case BTSTACK_EVENT_STATE: 2732d05904dSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 274e615b0ecSMilanka Ringwald status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid); 27505439aa6SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 276d93f9014SMilanka Ringwald printf("HID host connect failed, status 0x%02x.\n", status); 27705439aa6SMilanka Ringwald } 2782d05904dSMatthias Ringwald } 2792d05904dSMatthias Ringwald break; 28005439aa6SMilanka Ringwald #endif 2812d05904dSMatthias Ringwald /* LISTING_PAUSE */ 2822d05904dSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 2832d05904dSMatthias Ringwald // inform about pin code request 2842d05904dSMatthias Ringwald printf("Pin code request - using '0000'\n"); 2852d05904dSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 2862d05904dSMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 2872d05904dSMatthias Ringwald break; 2882d05904dSMatthias Ringwald 2892d05904dSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 2902d05904dSMatthias Ringwald // inform about user confirmation request 291173fff9bSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8)); 2922d05904dSMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 2932d05904dSMatthias Ringwald break; 2942d05904dSMatthias Ringwald 2952d05904dSMatthias Ringwald /* LISTING_RESUME */ 29605439aa6SMilanka Ringwald case HCI_EVENT_HID_META: 29705439aa6SMilanka Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 298d93f9014SMilanka Ringwald 29905439aa6SMilanka Ringwald case HID_SUBEVENT_INCOMING_CONNECTION: 300d93f9014SMilanka Ringwald // There is an incoming connection: we can accept it or decline it. 301d93f9014SMilanka Ringwald // The hid_host_report_mode in the hid_host_accept_connection function 302d93f9014SMilanka Ringwald // allows the application to request a protocol mode. 3033cbedd43SMatthias Ringwald // For available protocol modes, see hid_protocol_mode_t in btstack_hid.h file. 304e615b0ecSMilanka Ringwald hid_host_accept_connection(hid_subevent_incoming_connection_get_hid_cid(packet), hid_host_report_mode); 3056e1e5689SMatthias Ringwald break; 3066e1e5689SMatthias Ringwald 30705439aa6SMilanka Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 308d93f9014SMilanka Ringwald // The status field of this event indicates if the control and interrupt 309d93f9014SMilanka Ringwald // connections were opened successfully. 31005439aa6SMilanka Ringwald status = hid_subevent_connection_opened_get_status(packet); 311d93f9014SMilanka Ringwald if (status != ERROR_CODE_SUCCESS) { 31205439aa6SMilanka Ringwald printf("Connection failed, status 0x%x\n", status); 31305439aa6SMilanka Ringwald app_state = APP_IDLE; 31405439aa6SMilanka Ringwald hid_host_cid = 0; 31505439aa6SMilanka Ringwald return; 316e85416c1SMatthias Ringwald } 31705439aa6SMilanka Ringwald app_state = APP_CONNECTED; 31805439aa6SMilanka Ringwald hid_host_descriptor_available = false; 31905439aa6SMilanka Ringwald hid_host_cid = hid_subevent_connection_opened_get_hid_cid(packet); 320d93f9014SMilanka Ringwald printf("HID Host connected.\n"); 32105439aa6SMilanka Ringwald break; 32205439aa6SMilanka Ringwald 32305439aa6SMilanka Ringwald case HID_SUBEVENT_DESCRIPTOR_AVAILABLE: 324d93f9014SMilanka Ringwald // This event will follows HID_SUBEVENT_CONNECTION_OPENED event. 325d93f9014SMilanka Ringwald // For incoming connections, i.e. HID Device initiating the connection, 326d93f9014SMilanka Ringwald // the HID_SUBEVENT_DESCRIPTOR_AVAILABLE is delayed, and some HID 327d93f9014SMilanka Ringwald // reports may be received via HID_SUBEVENT_REPORT event. It is up to 328d93f9014SMilanka Ringwald // the application if these reports should be buffered or ignored until 329d93f9014SMilanka Ringwald // the HID descriptor is available. 33005439aa6SMilanka Ringwald status = hid_subevent_descriptor_available_get_status(packet); 33105439aa6SMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 33205439aa6SMilanka Ringwald hid_host_descriptor_available = true; 333d93f9014SMilanka Ringwald printf("HID Descriptor available, please start typing.\n"); 334e615b0ecSMilanka Ringwald } else { 335e615b0ecSMilanka Ringwald printf("Cannot handle input report, HID Descriptor is not available.\n"); 3366e1e5689SMatthias Ringwald } 337e85416c1SMatthias Ringwald break; 33805439aa6SMilanka Ringwald 339d93f9014SMilanka Ringwald case HID_SUBEVENT_REPORT: 340d93f9014SMilanka Ringwald // Handle input report. 341d93f9014SMilanka Ringwald if (hid_host_descriptor_available){ 342d93f9014SMilanka Ringwald hid_host_handle_interrupt_report(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet)); 343d93f9014SMilanka Ringwald } else { 344d93f9014SMilanka Ringwald printf_hexdump(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet)); 3452d05904dSMatthias Ringwald } 346e85416c1SMatthias Ringwald break; 34705439aa6SMilanka Ringwald 34805439aa6SMilanka Ringwald case HID_SUBEVENT_SET_PROTOCOL_RESPONSE: 349d93f9014SMilanka Ringwald // For incoming connections, the library will set the protocol mode of the 350d93f9014SMilanka Ringwald // HID Device as requested in the call to hid_host_accept_connection. The event 351d93f9014SMilanka Ringwald // reports the result. For connections initiated by calling hid_host_connect, 352d93f9014SMilanka Ringwald // this event will occur only if the established report mode is boot mode. 353f1aa1cdcSMilanka Ringwald status = hid_subevent_set_protocol_response_get_handshake_status(packet); 35405439aa6SMilanka Ringwald if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 35505439aa6SMilanka Ringwald printf("Error set protocol, status 0x%02x\n", status); 356e85416c1SMatthias Ringwald break; 357e85416c1SMatthias Ringwald } 358f1aa1cdcSMilanka Ringwald switch ((hid_protocol_mode_t)hid_subevent_set_protocol_response_get_protocol_mode(packet)){ 359f1aa1cdcSMilanka Ringwald case HID_PROTOCOL_MODE_BOOT: 360e615b0ecSMilanka Ringwald printf("Protocol mode set: BOOT.\n"); 361f1aa1cdcSMilanka Ringwald break; 362f1aa1cdcSMilanka Ringwald case HID_PROTOCOL_MODE_REPORT: 363e615b0ecSMilanka Ringwald printf("Protocol mode set: REPORT.\n"); 364f1aa1cdcSMilanka Ringwald break; 365f1aa1cdcSMilanka Ringwald default: 366f1aa1cdcSMilanka Ringwald printf("Unknown protocol mode.\n"); 367f1aa1cdcSMilanka Ringwald break; 368f1aa1cdcSMilanka Ringwald } 36905439aa6SMilanka Ringwald break; 37005439aa6SMilanka Ringwald 371d93f9014SMilanka Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 372d93f9014SMilanka Ringwald // The connection was closed. 373d93f9014SMilanka Ringwald hid_host_cid = 0; 374d93f9014SMilanka Ringwald hid_host_descriptor_available = false; 375d93f9014SMilanka Ringwald printf("HID Host disconnected.\n"); 37605439aa6SMilanka Ringwald break; 37705439aa6SMilanka Ringwald 37805439aa6SMilanka Ringwald default: 37905439aa6SMilanka Ringwald break; 38005439aa6SMilanka Ringwald } 38105439aa6SMilanka Ringwald break; 38205439aa6SMilanka Ringwald default: 38305439aa6SMilanka Ringwald break; 38405439aa6SMilanka Ringwald } 38505439aa6SMilanka Ringwald break; 3862d05904dSMatthias Ringwald default: 3872d05904dSMatthias Ringwald break; 3882d05904dSMatthias Ringwald } 3892d05904dSMatthias Ringwald } 3902d05904dSMatthias Ringwald /* LISTING_END */ 3912d05904dSMatthias Ringwald 39205439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN 39305439aa6SMilanka Ringwald static void show_usage(void){ 39405439aa6SMilanka Ringwald bd_addr_t iut_address; 39505439aa6SMilanka Ringwald gap_local_bd_addr(iut_address); 396d93f9014SMilanka Ringwald printf("\n--- Bluetooth HID Host Console %s ---\n", bd_addr_to_str(iut_address)); 397d93f9014SMilanka Ringwald printf("c - Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string); 398d93f9014SMilanka Ringwald printf("C - Disconnect\n"); 39905439aa6SMilanka Ringwald 40005439aa6SMilanka Ringwald printf("\n"); 40105439aa6SMilanka Ringwald printf("Ctrl-c - exit\n"); 40205439aa6SMilanka Ringwald printf("---\n"); 40305439aa6SMilanka Ringwald } 40405439aa6SMilanka Ringwald 40505439aa6SMilanka Ringwald static void stdin_process(char cmd){ 40605439aa6SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 40705439aa6SMilanka Ringwald switch (cmd){ 40805439aa6SMilanka Ringwald case 'c': 409d93f9014SMilanka Ringwald printf("Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string); 410e615b0ecSMilanka Ringwald status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid); 41105439aa6SMilanka Ringwald break; 41205439aa6SMilanka Ringwald case 'C': 413d93f9014SMilanka Ringwald printf("Disconnect...\n"); 41405439aa6SMilanka Ringwald hid_host_disconnect(hid_host_cid); 41505439aa6SMilanka Ringwald break; 41605439aa6SMilanka Ringwald case '\n': 41705439aa6SMilanka Ringwald case '\r': 41805439aa6SMilanka Ringwald break; 41905439aa6SMilanka Ringwald default: 42005439aa6SMilanka Ringwald show_usage(); 42105439aa6SMilanka Ringwald break; 42205439aa6SMilanka Ringwald } 42305439aa6SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 42405439aa6SMilanka Ringwald printf("HID host cmd \'%c\' failed, status 0x%02x\n", cmd, status); 42505439aa6SMilanka Ringwald } 42605439aa6SMilanka Ringwald } 42705439aa6SMilanka Ringwald #endif 42805439aa6SMilanka Ringwald 4292d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 4302d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 4312d05904dSMatthias Ringwald 4322d05904dSMatthias Ringwald (void)argc; 4332d05904dSMatthias Ringwald (void)argv; 4342d05904dSMatthias Ringwald 4352d05904dSMatthias Ringwald hid_host_setup(); 4362d05904dSMatthias Ringwald 4372d05904dSMatthias Ringwald // parse human readable Bluetooth address 4382d05904dSMatthias Ringwald sscanf_bd_addr(remote_addr_string, remote_addr); 4392d05904dSMatthias Ringwald 44005439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN 44105439aa6SMilanka Ringwald btstack_stdin_setup(stdin_process); 44205439aa6SMilanka Ringwald #endif 44305439aa6SMilanka Ringwald 4442d05904dSMatthias Ringwald // Turn on the device 4452d05904dSMatthias Ringwald hci_power_control(HCI_POWER_ON); 4462d05904dSMatthias Ringwald return 0; 4472d05904dSMatthias Ringwald } 4482d05904dSMatthias Ringwald 4492d05904dSMatthias Ringwald /* EXAMPLE_END */ 450