xref: /btstack/example/hid_host_demo.c (revision 9cf717e80dcdece8067e45ab8d41dc6e37b58b1e)
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 
163*9cf717e8SMatthias Ringwald     // make discoverable to allow HID device to initiate connection
164*9cf717e8SMatthias Ringwald     gap_discoverable_control(1);
165*9cf717e8SMatthias Ringwald 
1661d5191c9SMatthias Ringwald     // Disable stdout buffering
1679d010406SMatthias Ringwald     setvbuf(stdin, NULL, _IONBF, 0);
1682d05904dSMatthias Ringwald }
1692d05904dSMatthias Ringwald /* LISTING_END */
1702d05904dSMatthias Ringwald 
1712d05904dSMatthias Ringwald /*
1721d5191c9SMatthias Ringwald  * @section HID Report Handler
1731d5191c9SMatthias Ringwald  *
174d93f9014SMilanka Ringwald  * @text Use BTstack's compact HID Parser to process incoming HID Report in Report protocol mode.
1751d5191c9SMatthias Ringwald  * Iterate over all fields and process fields with usage page = 0x07 / Keyboard
1761d5191c9SMatthias Ringwald  * Check if SHIFT is down and process first character (don't handle multiple key presses)
1771d5191c9SMatthias Ringwald  *
1781d5191c9SMatthias Ringwald  */
179d93f9014SMilanka Ringwald 
1801d5191c9SMatthias Ringwald #define NUM_KEYS 6
1811d5191c9SMatthias Ringwald static uint8_t last_keys[NUM_KEYS];
1821d5191c9SMatthias Ringwald static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){
1831d5191c9SMatthias Ringwald     // check if HID Input Report
1841d5191c9SMatthias Ringwald     if (report_len < 1) return;
1851d5191c9SMatthias Ringwald     if (*report != 0xa1) return;
18605439aa6SMilanka Ringwald 
1871d5191c9SMatthias Ringwald     report++;
1881d5191c9SMatthias Ringwald     report_len--;
18905439aa6SMilanka Ringwald 
1901d5191c9SMatthias Ringwald     btstack_hid_parser_t parser;
19105439aa6SMilanka Ringwald     btstack_hid_parser_init(&parser,
19205439aa6SMilanka Ringwald         hid_descriptor_storage_get_descriptor_data(hid_host_cid),
19305439aa6SMilanka Ringwald         hid_descriptor_storage_get_descriptor_len(hid_host_cid),
19405439aa6SMilanka Ringwald         HID_REPORT_TYPE_INPUT, report, report_len);
19505439aa6SMilanka Ringwald 
1961d5191c9SMatthias Ringwald     int shift = 0;
1971d5191c9SMatthias Ringwald     uint8_t new_keys[NUM_KEYS];
1981d5191c9SMatthias Ringwald     memset(new_keys, 0, sizeof(new_keys));
1991d5191c9SMatthias Ringwald     int     new_keys_count = 0;
2001d5191c9SMatthias Ringwald     while (btstack_hid_parser_has_more(&parser)){
2011d5191c9SMatthias Ringwald         uint16_t usage_page;
2021d5191c9SMatthias Ringwald         uint16_t usage;
2031d5191c9SMatthias Ringwald         int32_t  value;
2041d5191c9SMatthias Ringwald         btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value);
2051d5191c9SMatthias Ringwald         if (usage_page != 0x07) continue;
2061d5191c9SMatthias Ringwald         switch (usage){
2071d5191c9SMatthias Ringwald             case 0xe1:
2081d5191c9SMatthias Ringwald             case 0xe6:
2091d5191c9SMatthias Ringwald                 if (value){
2101d5191c9SMatthias Ringwald                     shift = 1;
2111d5191c9SMatthias Ringwald                 }
2121d5191c9SMatthias Ringwald                 continue;
2131d5191c9SMatthias Ringwald             case 0x00:
2141d5191c9SMatthias Ringwald                 continue;
2151d5191c9SMatthias Ringwald             default:
2161d5191c9SMatthias Ringwald                 break;
2171d5191c9SMatthias Ringwald         }
2181d5191c9SMatthias Ringwald         if (usage >= sizeof(keytable_us_none)) continue;
2191d5191c9SMatthias Ringwald 
2201d5191c9SMatthias Ringwald         // store new keys
2219d010406SMatthias Ringwald         new_keys[new_keys_count++] = (uint8_t) usage;
2221d5191c9SMatthias Ringwald 
2231d5191c9SMatthias Ringwald         // check if usage was used last time (and ignore in that case)
2241d5191c9SMatthias Ringwald         int i;
2251d5191c9SMatthias Ringwald         for (i=0;i<NUM_KEYS;i++){
2261d5191c9SMatthias Ringwald             if (usage == last_keys[i]){
2271d5191c9SMatthias Ringwald                 usage = 0;
2281d5191c9SMatthias Ringwald             }
2291d5191c9SMatthias Ringwald         }
2301d5191c9SMatthias Ringwald         if (usage == 0) continue;
2311d5191c9SMatthias Ringwald 
2321d5191c9SMatthias Ringwald         uint8_t key;
2331d5191c9SMatthias Ringwald         if (shift){
2341d5191c9SMatthias Ringwald             key = keytable_us_shift[usage];
2351d5191c9SMatthias Ringwald         } else {
2361d5191c9SMatthias Ringwald             key = keytable_us_none[usage];
2371d5191c9SMatthias Ringwald         }
2381d5191c9SMatthias Ringwald         if (key == CHAR_ILLEGAL) continue;
2391d5191c9SMatthias Ringwald         if (key == CHAR_BACKSPACE){
2401d5191c9SMatthias Ringwald             printf("\b \b");    // go back one char, print space, go back one char again
2411d5191c9SMatthias Ringwald             continue;
2421d5191c9SMatthias Ringwald         }
2431d5191c9SMatthias Ringwald         printf("%c", key);
2441d5191c9SMatthias Ringwald     }
2451d5191c9SMatthias Ringwald     memcpy(last_keys, new_keys, NUM_KEYS);
2461d5191c9SMatthias Ringwald }
2471d5191c9SMatthias Ringwald 
2481d5191c9SMatthias Ringwald /*
2492d05904dSMatthias Ringwald  * @section Packet Handler
2502d05904dSMatthias Ringwald  *
251d93f9014SMilanka Ringwald  * @text The packet handler responds to various HID events.
2522d05904dSMatthias Ringwald  */
2532d05904dSMatthias Ringwald 
2542d05904dSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */
2552d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
2562d05904dSMatthias Ringwald {
2572d05904dSMatthias Ringwald     /* LISTING_PAUSE */
25805439aa6SMilanka Ringwald     UNUSED(channel);
25905439aa6SMilanka Ringwald     UNUSED(size);
26005439aa6SMilanka Ringwald 
2612d05904dSMatthias Ringwald     uint8_t   event;
2622d05904dSMatthias Ringwald     bd_addr_t event_addr;
263e85416c1SMatthias Ringwald     uint8_t   status;
2642d05904dSMatthias Ringwald 
2652d05904dSMatthias Ringwald     /* LISTING_RESUME */
2662d05904dSMatthias Ringwald     switch (packet_type) {
2672d05904dSMatthias Ringwald 		case HCI_EVENT_PACKET:
2682d05904dSMatthias Ringwald             event = hci_event_packet_get_type(packet);
26905439aa6SMilanka Ringwald 
2702d05904dSMatthias Ringwald             switch (event) {
27105439aa6SMilanka Ringwald #ifndef HAVE_BTSTACK_STDIN
2722d05904dSMatthias Ringwald                 /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING
2732d05904dSMatthias Ringwald                  * is received and the example is started in client mode, the remote SDP HID query is started.
2742d05904dSMatthias Ringwald                  */
2752d05904dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
2762d05904dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
277e615b0ecSMilanka Ringwald                         status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid);
27805439aa6SMilanka Ringwald                         if (status != ERROR_CODE_SUCCESS){
279d93f9014SMilanka Ringwald                             printf("HID host connect failed, status 0x%02x.\n", status);
28005439aa6SMilanka Ringwald                         }
2812d05904dSMatthias Ringwald                     }
2822d05904dSMatthias Ringwald                     break;
28305439aa6SMilanka Ringwald #endif
2842d05904dSMatthias Ringwald                 /* LISTING_PAUSE */
2852d05904dSMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
2862d05904dSMatthias Ringwald 					// inform about pin code request
2872d05904dSMatthias Ringwald                     printf("Pin code request - using '0000'\n");
2882d05904dSMatthias Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
2892d05904dSMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
2902d05904dSMatthias Ringwald 					break;
2912d05904dSMatthias Ringwald 
2922d05904dSMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
2932d05904dSMatthias Ringwald                     // inform about user confirmation request
294173fff9bSMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8));
2952d05904dSMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
2962d05904dSMatthias Ringwald                     break;
2972d05904dSMatthias Ringwald 
2982d05904dSMatthias Ringwald                 /* LISTING_RESUME */
29905439aa6SMilanka Ringwald                 case HCI_EVENT_HID_META:
30005439aa6SMilanka Ringwald                     switch (hci_event_hid_meta_get_subevent_code(packet)){
301d93f9014SMilanka Ringwald 
30205439aa6SMilanka Ringwald                         case HID_SUBEVENT_INCOMING_CONNECTION:
303d93f9014SMilanka Ringwald                             // There is an incoming connection: we can accept it or decline it.
304d93f9014SMilanka Ringwald                             // The hid_host_report_mode in the hid_host_accept_connection function
305d93f9014SMilanka Ringwald                             // allows the application to request a protocol mode.
3063cbedd43SMatthias Ringwald                             // For available protocol modes, see hid_protocol_mode_t in btstack_hid.h file.
307e615b0ecSMilanka Ringwald                             hid_host_accept_connection(hid_subevent_incoming_connection_get_hid_cid(packet), hid_host_report_mode);
3086e1e5689SMatthias Ringwald                             break;
3096e1e5689SMatthias Ringwald 
31005439aa6SMilanka Ringwald                         case HID_SUBEVENT_CONNECTION_OPENED:
311d93f9014SMilanka Ringwald                             // The status field of this event indicates if the control and interrupt
312d93f9014SMilanka Ringwald                             // connections were opened successfully.
31305439aa6SMilanka Ringwald                             status = hid_subevent_connection_opened_get_status(packet);
314d93f9014SMilanka Ringwald                             if (status != ERROR_CODE_SUCCESS) {
315fcd55a0bSMilanka Ringwald                                 printf("Connection failed, status 0x%02x\n", status);
31605439aa6SMilanka Ringwald                                 app_state = APP_IDLE;
31705439aa6SMilanka Ringwald                                 hid_host_cid = 0;
31805439aa6SMilanka Ringwald                                 return;
319e85416c1SMatthias Ringwald                             }
32005439aa6SMilanka Ringwald                             app_state = APP_CONNECTED;
32105439aa6SMilanka Ringwald                             hid_host_descriptor_available = false;
32205439aa6SMilanka Ringwald                             hid_host_cid = hid_subevent_connection_opened_get_hid_cid(packet);
323d93f9014SMilanka Ringwald                             printf("HID Host connected.\n");
32405439aa6SMilanka Ringwald                             break;
32505439aa6SMilanka Ringwald 
32605439aa6SMilanka Ringwald                         case HID_SUBEVENT_DESCRIPTOR_AVAILABLE:
327d93f9014SMilanka Ringwald                             // This event will follows HID_SUBEVENT_CONNECTION_OPENED event.
328d93f9014SMilanka Ringwald                             // For incoming connections, i.e. HID Device initiating the connection,
329d93f9014SMilanka Ringwald                             // the HID_SUBEVENT_DESCRIPTOR_AVAILABLE is delayed, and some HID
330d93f9014SMilanka Ringwald                             // reports may be received via HID_SUBEVENT_REPORT event. It is up to
331d93f9014SMilanka Ringwald                             // the application if these reports should be buffered or ignored until
332d93f9014SMilanka Ringwald                             // the HID descriptor is available.
33305439aa6SMilanka Ringwald                             status = hid_subevent_descriptor_available_get_status(packet);
33405439aa6SMilanka Ringwald                             if (status == ERROR_CODE_SUCCESS){
33505439aa6SMilanka Ringwald                                 hid_host_descriptor_available = true;
336d93f9014SMilanka Ringwald                                 printf("HID Descriptor available, please start typing.\n");
337e615b0ecSMilanka Ringwald                             } else {
338fcd55a0bSMilanka Ringwald                                 printf("Cannot handle input report, HID Descriptor is not available, status 0x%02x\n", status);
3396e1e5689SMatthias Ringwald                             }
340e85416c1SMatthias Ringwald                             break;
34105439aa6SMilanka Ringwald 
342d93f9014SMilanka Ringwald                         case HID_SUBEVENT_REPORT:
343d93f9014SMilanka Ringwald                             // Handle input report.
344d93f9014SMilanka Ringwald                             if (hid_host_descriptor_available){
345d93f9014SMilanka Ringwald                                 hid_host_handle_interrupt_report(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet));
346d93f9014SMilanka Ringwald                             } else {
347d93f9014SMilanka Ringwald                                 printf_hexdump(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet));
3482d05904dSMatthias Ringwald                             }
349e85416c1SMatthias Ringwald                             break;
35005439aa6SMilanka Ringwald 
35105439aa6SMilanka Ringwald                         case HID_SUBEVENT_SET_PROTOCOL_RESPONSE:
352d93f9014SMilanka Ringwald                             // For incoming connections, the library will set the protocol mode of the
353d93f9014SMilanka Ringwald                             // HID Device as requested in the call to hid_host_accept_connection. The event
354d93f9014SMilanka Ringwald                             // reports the result. For connections initiated by calling hid_host_connect,
355d93f9014SMilanka Ringwald                             // this event will occur only if the established report mode is boot mode.
356f1aa1cdcSMilanka Ringwald                             status = hid_subevent_set_protocol_response_get_handshake_status(packet);
35705439aa6SMilanka Ringwald                             if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){
35805439aa6SMilanka Ringwald                                 printf("Error set protocol, status 0x%02x\n", status);
359e85416c1SMatthias Ringwald                                 break;
360e85416c1SMatthias Ringwald                             }
361f1aa1cdcSMilanka Ringwald                             switch ((hid_protocol_mode_t)hid_subevent_set_protocol_response_get_protocol_mode(packet)){
362f1aa1cdcSMilanka Ringwald                                 case HID_PROTOCOL_MODE_BOOT:
363e615b0ecSMilanka Ringwald                                     printf("Protocol mode set: BOOT.\n");
364f1aa1cdcSMilanka Ringwald                                     break;
365f1aa1cdcSMilanka Ringwald                                 case HID_PROTOCOL_MODE_REPORT:
366e615b0ecSMilanka Ringwald                                     printf("Protocol mode set: REPORT.\n");
367f1aa1cdcSMilanka Ringwald                                     break;
368f1aa1cdcSMilanka Ringwald                                 default:
369f1aa1cdcSMilanka Ringwald                                     printf("Unknown protocol mode.\n");
370f1aa1cdcSMilanka Ringwald                                     break;
371f1aa1cdcSMilanka Ringwald                             }
37205439aa6SMilanka Ringwald                             break;
37305439aa6SMilanka Ringwald 
374d93f9014SMilanka Ringwald                         case HID_SUBEVENT_CONNECTION_CLOSED:
375d93f9014SMilanka Ringwald                             // The connection was closed.
376d93f9014SMilanka Ringwald                             hid_host_cid = 0;
377d93f9014SMilanka Ringwald                             hid_host_descriptor_available = false;
378d93f9014SMilanka Ringwald                             printf("HID Host disconnected.\n");
37905439aa6SMilanka Ringwald                             break;
38005439aa6SMilanka Ringwald 
38105439aa6SMilanka Ringwald                         default:
38205439aa6SMilanka Ringwald                             break;
38305439aa6SMilanka Ringwald                     }
38405439aa6SMilanka Ringwald                     break;
38505439aa6SMilanka Ringwald                 default:
38605439aa6SMilanka Ringwald                     break;
38705439aa6SMilanka Ringwald             }
38805439aa6SMilanka Ringwald             break;
3892d05904dSMatthias Ringwald         default:
3902d05904dSMatthias Ringwald             break;
3912d05904dSMatthias Ringwald     }
3922d05904dSMatthias Ringwald }
3932d05904dSMatthias Ringwald /* LISTING_END */
3942d05904dSMatthias Ringwald 
39505439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN
39605439aa6SMilanka Ringwald static void show_usage(void){
39705439aa6SMilanka Ringwald     bd_addr_t      iut_address;
39805439aa6SMilanka Ringwald     gap_local_bd_addr(iut_address);
399d93f9014SMilanka Ringwald     printf("\n--- Bluetooth HID Host Console %s ---\n", bd_addr_to_str(iut_address));
400d93f9014SMilanka Ringwald     printf("c      - Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string);
401d93f9014SMilanka Ringwald     printf("C      - Disconnect\n");
40205439aa6SMilanka Ringwald 
40305439aa6SMilanka Ringwald     printf("\n");
40405439aa6SMilanka Ringwald     printf("Ctrl-c - exit\n");
40505439aa6SMilanka Ringwald     printf("---\n");
40605439aa6SMilanka Ringwald }
40705439aa6SMilanka Ringwald 
40805439aa6SMilanka Ringwald static void stdin_process(char cmd){
40905439aa6SMilanka Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
41005439aa6SMilanka Ringwald     switch (cmd){
41105439aa6SMilanka Ringwald         case 'c':
412d93f9014SMilanka Ringwald             printf("Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string);
413e615b0ecSMilanka Ringwald             status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid);
41405439aa6SMilanka Ringwald             break;
41505439aa6SMilanka Ringwald         case 'C':
416d93f9014SMilanka Ringwald             printf("Disconnect...\n");
41705439aa6SMilanka Ringwald             hid_host_disconnect(hid_host_cid);
41805439aa6SMilanka Ringwald             break;
41905439aa6SMilanka Ringwald         case '\n':
42005439aa6SMilanka Ringwald         case '\r':
42105439aa6SMilanka Ringwald             break;
42205439aa6SMilanka Ringwald         default:
42305439aa6SMilanka Ringwald             show_usage();
42405439aa6SMilanka Ringwald             break;
42505439aa6SMilanka Ringwald     }
42605439aa6SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
42705439aa6SMilanka Ringwald         printf("HID host cmd \'%c\' failed, status 0x%02x\n", cmd, status);
42805439aa6SMilanka Ringwald     }
42905439aa6SMilanka Ringwald }
43005439aa6SMilanka Ringwald #endif
43105439aa6SMilanka Ringwald 
4322d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
4332d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
4342d05904dSMatthias Ringwald 
4352d05904dSMatthias Ringwald     (void)argc;
4362d05904dSMatthias Ringwald     (void)argv;
4372d05904dSMatthias Ringwald 
4382d05904dSMatthias Ringwald     hid_host_setup();
4392d05904dSMatthias Ringwald 
4402d05904dSMatthias Ringwald     // parse human readable Bluetooth address
4412d05904dSMatthias Ringwald     sscanf_bd_addr(remote_addr_string, remote_addr);
4422d05904dSMatthias Ringwald 
44305439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN
44405439aa6SMilanka Ringwald     btstack_stdin_setup(stdin_process);
44505439aa6SMilanka Ringwald #endif
44605439aa6SMilanka Ringwald 
4472d05904dSMatthias Ringwald     // Turn on the device
4482d05904dSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
4492d05904dSMatthias Ringwald     return 0;
4502d05904dSMatthias Ringwald }
4512d05904dSMatthias Ringwald 
4522d05904dSMatthias Ringwald /* EXAMPLE_END */
453