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 * 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 58*05439aa6SMilanka Ringwald // MBP 2016 static const char * remote_addr_string = "F4-0F-24-3B-1B-E1"; 592d05904dSMatthias Ringwald // iMpulse static const char * remote_addr_string = "64:6E:6C:C1:AA:B5"; 60*05439aa6SMilanka Ringwald // Logitec 61*05439aa6SMilanka Ringwald static const char * remote_addr_string = "00:1F:20:86:DF:52"; 622d05904dSMatthias Ringwald 632d05904dSMatthias Ringwald static bd_addr_t remote_addr; 642d05904dSMatthias Ringwald 652d05904dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 662d05904dSMatthias Ringwald 671d5191c9SMatthias Ringwald // Simplified US Keyboard with Shift modifier 681d5191c9SMatthias Ringwald 691d5191c9SMatthias Ringwald #define CHAR_ILLEGAL 0xff 701d5191c9SMatthias Ringwald #define CHAR_RETURN '\n' 711d5191c9SMatthias Ringwald #define CHAR_ESCAPE 27 721d5191c9SMatthias Ringwald #define CHAR_TAB '\t' 731d5191c9SMatthias Ringwald #define CHAR_BACKSPACE 0x7f 741d5191c9SMatthias Ringwald 751d5191c9SMatthias Ringwald /** 761d5191c9SMatthias Ringwald * English (US) 771d5191c9SMatthias Ringwald */ 781d5191c9SMatthias Ringwald static const uint8_t keytable_us_none [] = { 791d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 801d5191c9SMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 811d5191c9SMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 821d5191c9SMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 831d5191c9SMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 841d5191c9SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 851d5191c9SMatthias Ringwald '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 861d5191c9SMatthias Ringwald '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 871d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 881d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 891d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 901d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 911d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 921d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 931d5191c9SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 941d5191c9SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 951d5191c9SMatthias Ringwald }; 961d5191c9SMatthias Ringwald 971d5191c9SMatthias Ringwald static const uint8_t keytable_us_shift[] = { 981d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 991d5191c9SMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 1001d5191c9SMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 1011d5191c9SMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 1021d5191c9SMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 1031d5191c9SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 1041d5191c9SMatthias Ringwald '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 1051d5191c9SMatthias Ringwald '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 1061d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 1071d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 1081d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 1091d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 1101d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 1111d5191c9SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 1121d5191c9SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 1131d5191c9SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 1141d5191c9SMatthias Ringwald }; 1151d5191c9SMatthias Ringwald 116*05439aa6SMilanka Ringwald // SDP 117*05439aa6SMilanka Ringwald static uint8_t hid_descriptor[MAX_ATTRIBUTE_VALUE_SIZE]; 1182d05904dSMatthias Ringwald 119*05439aa6SMilanka Ringwald // App 120*05439aa6SMilanka Ringwald static enum { 121*05439aa6SMilanka Ringwald APP_IDLE, 122*05439aa6SMilanka Ringwald APP_CONNECTED 123*05439aa6SMilanka Ringwald } app_state = APP_IDLE; 124*05439aa6SMilanka Ringwald 125*05439aa6SMilanka Ringwald static uint16_t hid_host_cid = 0; 126*05439aa6SMilanka Ringwald static bool hid_host_descriptor_available = false; 1272d05904dSMatthias Ringwald /* @section Main application configuration 1282d05904dSMatthias Ringwald * 1292d05904dSMatthias Ringwald * @text In the application configuration, L2CAP is initialized 1302d05904dSMatthias Ringwald */ 1312d05904dSMatthias Ringwald 1322d05904dSMatthias Ringwald /* LISTING_START(PanuSetup): Panu setup */ 1332d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1342d05904dSMatthias Ringwald 1352d05904dSMatthias Ringwald static void hid_host_setup(void){ 1362d05904dSMatthias Ringwald 137a4fe6467SMatthias Ringwald // Initialize L2CAP 138a4fe6467SMatthias Ringwald l2cap_init(); 139a4fe6467SMatthias Ringwald 140*05439aa6SMilanka Ringwald // Initialize HID Host 141*05439aa6SMilanka Ringwald hid_host_init(hid_descriptor, sizeof(hid_descriptor)); 142*05439aa6SMilanka Ringwald hid_host_register_packet_handler(packet_handler); 1436e1e5689SMatthias Ringwald 1448e0c4421SMatthias Ringwald // Allow sniff mode requests by HID device and support role switch 1458e0c4421SMatthias Ringwald gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH); 1468e0c4421SMatthias Ringwald 1478e0c4421SMatthias Ringwald // try to become master on incoming connections 1488e0c4421SMatthias Ringwald hci_set_master_slave_policy(HCI_ROLE_MASTER); 1496e1e5689SMatthias Ringwald 1502d05904dSMatthias Ringwald // register for HCI events 1512d05904dSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1522d05904dSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1532d05904dSMatthias Ringwald 1541d5191c9SMatthias Ringwald // Disable stdout buffering 1551d5191c9SMatthias Ringwald setbuf(stdout, NULL); 1562d05904dSMatthias Ringwald } 1572d05904dSMatthias Ringwald /* LISTING_END */ 1582d05904dSMatthias Ringwald 1592d05904dSMatthias Ringwald /* 1601d5191c9SMatthias Ringwald * @section HID Report Handler 1611d5191c9SMatthias Ringwald * 1621d5191c9SMatthias Ringwald * @text Use BTstack's compact HID Parser to process incoming HID Report 1631d5191c9SMatthias Ringwald * Iterate over all fields and process fields with usage page = 0x07 / Keyboard 1641d5191c9SMatthias Ringwald * Check if SHIFT is down and process first character (don't handle multiple key presses) 1651d5191c9SMatthias Ringwald * 1661d5191c9SMatthias Ringwald */ 1671d5191c9SMatthias Ringwald #define NUM_KEYS 6 1681d5191c9SMatthias Ringwald static uint8_t last_keys[NUM_KEYS]; 1691d5191c9SMatthias Ringwald static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){ 1701d5191c9SMatthias Ringwald // check if HID Input Report 1711d5191c9SMatthias Ringwald if (report_len < 1) return; 1721d5191c9SMatthias Ringwald if (*report != 0xa1) return; 173*05439aa6SMilanka Ringwald 1741d5191c9SMatthias Ringwald report++; 1751d5191c9SMatthias Ringwald report_len--; 176*05439aa6SMilanka Ringwald 1771d5191c9SMatthias Ringwald btstack_hid_parser_t parser; 178*05439aa6SMilanka Ringwald btstack_hid_parser_init(&parser, 179*05439aa6SMilanka Ringwald hid_descriptor_storage_get_descriptor_data(hid_host_cid), 180*05439aa6SMilanka Ringwald hid_descriptor_storage_get_descriptor_len(hid_host_cid), 181*05439aa6SMilanka Ringwald HID_REPORT_TYPE_INPUT, report, report_len); 182*05439aa6SMilanka Ringwald 1831d5191c9SMatthias Ringwald int shift = 0; 1841d5191c9SMatthias Ringwald uint8_t new_keys[NUM_KEYS]; 1851d5191c9SMatthias Ringwald memset(new_keys, 0, sizeof(new_keys)); 1861d5191c9SMatthias Ringwald int new_keys_count = 0; 1871d5191c9SMatthias Ringwald while (btstack_hid_parser_has_more(&parser)){ 1881d5191c9SMatthias Ringwald uint16_t usage_page; 1891d5191c9SMatthias Ringwald uint16_t usage; 1901d5191c9SMatthias Ringwald int32_t value; 1911d5191c9SMatthias Ringwald btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value); 1921d5191c9SMatthias Ringwald if (usage_page != 0x07) continue; 1931d5191c9SMatthias Ringwald switch (usage){ 1941d5191c9SMatthias Ringwald case 0xe1: 1951d5191c9SMatthias Ringwald case 0xe6: 1961d5191c9SMatthias Ringwald if (value){ 1971d5191c9SMatthias Ringwald shift = 1; 1981d5191c9SMatthias Ringwald } 1991d5191c9SMatthias Ringwald continue; 2001d5191c9SMatthias Ringwald case 0x00: 2011d5191c9SMatthias Ringwald continue; 2021d5191c9SMatthias Ringwald default: 2031d5191c9SMatthias Ringwald break; 2041d5191c9SMatthias Ringwald } 2051d5191c9SMatthias Ringwald if (usage >= sizeof(keytable_us_none)) continue; 2061d5191c9SMatthias Ringwald 2071d5191c9SMatthias Ringwald // store new keys 2081d5191c9SMatthias Ringwald new_keys[new_keys_count++] = usage; 2091d5191c9SMatthias Ringwald 2101d5191c9SMatthias Ringwald // check if usage was used last time (and ignore in that case) 2111d5191c9SMatthias Ringwald int i; 2121d5191c9SMatthias Ringwald for (i=0;i<NUM_KEYS;i++){ 2131d5191c9SMatthias Ringwald if (usage == last_keys[i]){ 2141d5191c9SMatthias Ringwald usage = 0; 2151d5191c9SMatthias Ringwald } 2161d5191c9SMatthias Ringwald } 2171d5191c9SMatthias Ringwald if (usage == 0) continue; 2181d5191c9SMatthias Ringwald 2191d5191c9SMatthias Ringwald uint8_t key; 2201d5191c9SMatthias Ringwald if (shift){ 2211d5191c9SMatthias Ringwald key = keytable_us_shift[usage]; 2221d5191c9SMatthias Ringwald } else { 2231d5191c9SMatthias Ringwald key = keytable_us_none[usage]; 2241d5191c9SMatthias Ringwald } 2251d5191c9SMatthias Ringwald if (key == CHAR_ILLEGAL) continue; 2261d5191c9SMatthias Ringwald if (key == CHAR_BACKSPACE){ 2271d5191c9SMatthias Ringwald printf("\b \b"); // go back one char, print space, go back one char again 2281d5191c9SMatthias Ringwald continue; 2291d5191c9SMatthias Ringwald } 2301d5191c9SMatthias Ringwald printf("%c", key); 2311d5191c9SMatthias Ringwald } 2321d5191c9SMatthias Ringwald memcpy(last_keys, new_keys, NUM_KEYS); 2331d5191c9SMatthias Ringwald } 2341d5191c9SMatthias Ringwald 2351d5191c9SMatthias Ringwald /* 2362d05904dSMatthias Ringwald * @section Packet Handler 2372d05904dSMatthias Ringwald * 2382d05904dSMatthias Ringwald * @text The packet handler responds to various HCI Events. 2392d05904dSMatthias Ringwald */ 2402d05904dSMatthias Ringwald 2412d05904dSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 2422d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 2432d05904dSMatthias Ringwald { 2442d05904dSMatthias Ringwald /* LISTING_PAUSE */ 245*05439aa6SMilanka Ringwald UNUSED(channel); 246*05439aa6SMilanka Ringwald UNUSED(size); 247*05439aa6SMilanka Ringwald 2482d05904dSMatthias Ringwald uint8_t event; 2492d05904dSMatthias Ringwald bd_addr_t event_addr; 250e85416c1SMatthias Ringwald uint8_t status; 2512d05904dSMatthias Ringwald 2522d05904dSMatthias Ringwald /* LISTING_RESUME */ 2532d05904dSMatthias Ringwald switch (packet_type) { 2542d05904dSMatthias Ringwald case HCI_EVENT_PACKET: 2552d05904dSMatthias Ringwald event = hci_event_packet_get_type(packet); 256*05439aa6SMilanka Ringwald 2572d05904dSMatthias Ringwald switch (event) { 258*05439aa6SMilanka Ringwald #ifndef HAVE_BTSTACK_STDIN 2592d05904dSMatthias Ringwald /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING 2602d05904dSMatthias Ringwald * is received and the example is started in client mode, the remote SDP HID query is started. 2612d05904dSMatthias Ringwald */ 2622d05904dSMatthias Ringwald case BTSTACK_EVENT_STATE: 2632d05904dSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 264*05439aa6SMilanka Ringwald status = hid_host_connect(remote_addr, HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT, &hid_host_cid); 265*05439aa6SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 266*05439aa6SMilanka Ringwald printf("HID host connect failed, status 0x%02x\n", status); 267*05439aa6SMilanka Ringwald } 2682d05904dSMatthias Ringwald } 2692d05904dSMatthias Ringwald break; 270*05439aa6SMilanka Ringwald #endif 2712d05904dSMatthias Ringwald /* LISTING_PAUSE */ 2722d05904dSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 2732d05904dSMatthias Ringwald // inform about pin code request 2742d05904dSMatthias Ringwald printf("Pin code request - using '0000'\n"); 2752d05904dSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 2762d05904dSMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 2772d05904dSMatthias Ringwald break; 2782d05904dSMatthias Ringwald 2792d05904dSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 2802d05904dSMatthias Ringwald // inform about user confirmation request 281173fff9bSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8)); 2822d05904dSMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 2832d05904dSMatthias Ringwald break; 2842d05904dSMatthias Ringwald 2852d05904dSMatthias Ringwald /* LISTING_RESUME */ 286*05439aa6SMilanka Ringwald case HCI_EVENT_HID_META: 287*05439aa6SMilanka Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 288*05439aa6SMilanka Ringwald case HID_SUBEVENT_INCOMING_CONNECTION: 289*05439aa6SMilanka Ringwald hid_host_accept_connection(hid_subevent_incoming_connection_get_hid_cid(packet), HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT); 2906e1e5689SMatthias Ringwald break; 2916e1e5689SMatthias Ringwald 292*05439aa6SMilanka Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 293*05439aa6SMilanka Ringwald status = hid_subevent_connection_opened_get_status(packet); 294*05439aa6SMilanka Ringwald if (status) { 295*05439aa6SMilanka Ringwald // outgoing connection failed 296*05439aa6SMilanka Ringwald printf("Connection failed, status 0x%x\n", status); 297*05439aa6SMilanka Ringwald app_state = APP_IDLE; 298*05439aa6SMilanka Ringwald hid_host_cid = 0; 299*05439aa6SMilanka Ringwald return; 300e85416c1SMatthias Ringwald } 301*05439aa6SMilanka Ringwald app_state = APP_CONNECTED; 302*05439aa6SMilanka Ringwald hid_host_descriptor_available = false; 303*05439aa6SMilanka Ringwald hid_host_cid = hid_subevent_connection_opened_get_hid_cid(packet); 304*05439aa6SMilanka Ringwald printf("HID Host connected...\n"); 305*05439aa6SMilanka Ringwald break; 306*05439aa6SMilanka Ringwald 307*05439aa6SMilanka Ringwald case HID_SUBEVENT_DESCRIPTOR_AVAILABLE: 308*05439aa6SMilanka Ringwald status = hid_subevent_descriptor_available_get_status(packet); 309*05439aa6SMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 310*05439aa6SMilanka Ringwald hid_host_descriptor_available = true; 3116e1e5689SMatthias Ringwald } 312e85416c1SMatthias Ringwald break; 313*05439aa6SMilanka Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 314*05439aa6SMilanka Ringwald hid_host_cid = 0; 315*05439aa6SMilanka Ringwald hid_host_descriptor_available = false; 316*05439aa6SMilanka Ringwald printf("HID Host disconnected..\n"); 317*05439aa6SMilanka Ringwald break; 318*05439aa6SMilanka Ringwald 319*05439aa6SMilanka Ringwald case HID_SUBEVENT_GET_REPORT_RESPONSE: 320*05439aa6SMilanka Ringwald status = hid_subevent_get_report_response_get_handshake_status(packet); 321*05439aa6SMilanka Ringwald if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 322*05439aa6SMilanka Ringwald printf("Error get report, status 0x%02x\n", status); 323*05439aa6SMilanka Ringwald break; 3246e1e5689SMatthias Ringwald } 325*05439aa6SMilanka Ringwald printf("Received report[%d]: ", hid_subevent_get_report_response_get_report_len(packet)); 326*05439aa6SMilanka Ringwald printf_hexdump(hid_subevent_get_report_response_get_report(packet), hid_subevent_get_report_response_get_report_len(packet)); 327*05439aa6SMilanka Ringwald printf("\n"); 328*05439aa6SMilanka Ringwald break; 329*05439aa6SMilanka Ringwald 330*05439aa6SMilanka Ringwald case HID_SUBEVENT_SET_REPORT_RESPONSE: 331*05439aa6SMilanka Ringwald status = hid_subevent_set_report_response_get_handshake_status(packet); 332*05439aa6SMilanka Ringwald if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 333*05439aa6SMilanka Ringwald printf("Error set report, status 0x%02x\n", status); 334*05439aa6SMilanka Ringwald break; 3356e1e5689SMatthias Ringwald } 336*05439aa6SMilanka Ringwald printf("Report set.\n"); 337*05439aa6SMilanka Ringwald break; 338*05439aa6SMilanka Ringwald 339*05439aa6SMilanka Ringwald case HID_SUBEVENT_GET_PROTOCOL_RESPONSE: 340*05439aa6SMilanka Ringwald status = hid_subevent_get_protocol_response_get_handshake_status(packet); 341*05439aa6SMilanka Ringwald if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 342*05439aa6SMilanka Ringwald printf("Error get report, status 0x%02x\n", status); 343*05439aa6SMilanka Ringwald break; 3446e1e5689SMatthias Ringwald } 345*05439aa6SMilanka Ringwald switch ((hid_protocol_mode_t)hid_subevent_get_protocol_response_get_protocol_mode(packet)){ 346*05439aa6SMilanka Ringwald case HID_PROTOCOL_MODE_BOOT: 347*05439aa6SMilanka Ringwald printf("HID device is in BOOT mode.\n"); 348*05439aa6SMilanka Ringwald break; 349*05439aa6SMilanka Ringwald case HID_PROTOCOL_MODE_REPORT: 350*05439aa6SMilanka Ringwald printf("HID device is in REPORT mode.\n"); 351*05439aa6SMilanka Ringwald break; 3522d05904dSMatthias Ringwald default: 3532d05904dSMatthias Ringwald break; 3542d05904dSMatthias Ringwald } 355e85416c1SMatthias Ringwald break; 356*05439aa6SMilanka Ringwald 357*05439aa6SMilanka Ringwald case HID_SUBEVENT_SET_PROTOCOL_RESPONSE: 358*05439aa6SMilanka Ringwald status = hid_subevent_set_report_response_get_handshake_status(packet); 359*05439aa6SMilanka Ringwald if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){ 360*05439aa6SMilanka Ringwald printf("Error set protocol, status 0x%02x\n", status); 361e85416c1SMatthias Ringwald break; 362e85416c1SMatthias Ringwald } 363*05439aa6SMilanka Ringwald printf("Protocol set.\n"); 364*05439aa6SMilanka Ringwald break; 365*05439aa6SMilanka Ringwald 366*05439aa6SMilanka Ringwald case HID_SUBEVENT_REPORT: 367*05439aa6SMilanka Ringwald printf("Received input report[%d]: \n", hid_subevent_report_get_report_len(packet)); 368*05439aa6SMilanka Ringwald if (hid_host_descriptor_available){ 369*05439aa6SMilanka Ringwald hid_host_handle_interrupt_report(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet)); 370*05439aa6SMilanka Ringwald printf("\n"); 371*05439aa6SMilanka Ringwald } else { 372*05439aa6SMilanka Ringwald printf("Cannot handle input report, HID Descriptor is not available\n"); 373*05439aa6SMilanka Ringwald printf_hexdump(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet)); 374*05439aa6SMilanka Ringwald } 375*05439aa6SMilanka Ringwald break; 376*05439aa6SMilanka Ringwald 377*05439aa6SMilanka Ringwald default: 378*05439aa6SMilanka Ringwald break; 379*05439aa6SMilanka Ringwald } 380*05439aa6SMilanka Ringwald break; 381*05439aa6SMilanka Ringwald default: 382*05439aa6SMilanka Ringwald break; 383*05439aa6SMilanka Ringwald } 384*05439aa6SMilanka Ringwald break; 3852d05904dSMatthias Ringwald default: 3862d05904dSMatthias Ringwald break; 3872d05904dSMatthias Ringwald } 3882d05904dSMatthias Ringwald } 3892d05904dSMatthias Ringwald /* LISTING_END */ 3902d05904dSMatthias Ringwald 391*05439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN 392*05439aa6SMilanka Ringwald static void show_usage(void){ 393*05439aa6SMilanka Ringwald bd_addr_t iut_address; 394*05439aa6SMilanka Ringwald gap_local_bd_addr(iut_address); 395*05439aa6SMilanka Ringwald printf("\n--- Bluetooth HID Host Test Console %s ---\n", bd_addr_to_str(iut_address)); 396*05439aa6SMilanka Ringwald printf("c - Connect to %s in report mode, with fallback to BOOT mode.\n", remote_addr_string); 397*05439aa6SMilanka Ringwald printf("C - Disconnect from %s\n", remote_addr_string); 398*05439aa6SMilanka Ringwald 399*05439aa6SMilanka Ringwald printf("\n"); 400*05439aa6SMilanka Ringwald printf("Ctrl-c - exit\n"); 401*05439aa6SMilanka Ringwald printf("---\n"); 402*05439aa6SMilanka Ringwald } 403*05439aa6SMilanka Ringwald 404*05439aa6SMilanka Ringwald static void stdin_process(char cmd){ 405*05439aa6SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 406*05439aa6SMilanka Ringwald switch (cmd){ 407*05439aa6SMilanka Ringwald case 'c': 408*05439aa6SMilanka Ringwald printf("Connect to %s in report mode, with fallback to BOOT mode.\n", remote_addr_string); 409*05439aa6SMilanka Ringwald status = hid_host_connect(remote_addr, HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT, &hid_host_cid); 410*05439aa6SMilanka Ringwald break; 411*05439aa6SMilanka Ringwald 412*05439aa6SMilanka Ringwald case 'C': 413*05439aa6SMilanka Ringwald printf("Disconnect from %s...\n", remote_addr_string); 414*05439aa6SMilanka Ringwald hid_host_disconnect(hid_host_cid); 415*05439aa6SMilanka Ringwald break; 416*05439aa6SMilanka Ringwald case '\n': 417*05439aa6SMilanka Ringwald case '\r': 418*05439aa6SMilanka Ringwald break; 419*05439aa6SMilanka Ringwald default: 420*05439aa6SMilanka Ringwald show_usage(); 421*05439aa6SMilanka Ringwald break; 422*05439aa6SMilanka Ringwald } 423*05439aa6SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 424*05439aa6SMilanka Ringwald printf("HID host cmd \'%c\' failed, status 0x%02x\n", cmd, status); 425*05439aa6SMilanka Ringwald } 426*05439aa6SMilanka Ringwald } 427*05439aa6SMilanka Ringwald #endif 428*05439aa6SMilanka 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 440*05439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN 441*05439aa6SMilanka Ringwald btstack_stdin_setup(stdin_process); 442*05439aa6SMilanka Ringwald #endif 443*05439aa6SMilanka 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