xref: /btstack/example/hid_host_demo.c (revision 5d4d8cc7b1d35a90bbd6d5ffd2d3050b2bfc861c)
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 
591f790386SMatthias Ringwald static const char * remote_addr_string = "00:1A:7D:DA:71:01";
602d05904dSMatthias Ringwald 
612d05904dSMatthias Ringwald static bd_addr_t remote_addr;
622d05904dSMatthias Ringwald 
632d05904dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
642d05904dSMatthias Ringwald 
651d5191c9SMatthias Ringwald // Simplified US Keyboard with Shift modifier
661d5191c9SMatthias Ringwald 
671d5191c9SMatthias Ringwald #define CHAR_ILLEGAL     0xff
681d5191c9SMatthias Ringwald #define CHAR_RETURN     '\n'
691d5191c9SMatthias Ringwald #define CHAR_ESCAPE      27
701d5191c9SMatthias Ringwald #define CHAR_TAB         '\t'
711d5191c9SMatthias Ringwald #define CHAR_BACKSPACE   0x7f
721d5191c9SMatthias Ringwald 
731d5191c9SMatthias Ringwald /**
741d5191c9SMatthias Ringwald  * English (US)
751d5191c9SMatthias Ringwald  */
761d5191c9SMatthias Ringwald static const uint8_t keytable_us_none [] = {
771d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*   0-3 */
781d5191c9SMatthias Ringwald     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',                   /*  4-13 */
791d5191c9SMatthias Ringwald     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',                   /* 14-23 */
801d5191c9SMatthias Ringwald     'u', 'v', 'w', 'x', 'y', 'z',                                       /* 24-29 */
811d5191c9SMatthias Ringwald     '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',                   /* 30-39 */
821d5191c9SMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
831d5191c9SMatthias Ringwald     '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',',       /* 45-54 */
841d5191c9SMatthias Ringwald     '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
851d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
861d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
871d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
881d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
891d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
901d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
911d5191c9SMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
921d5191c9SMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xa7,                                 /* 97-100 */
931d5191c9SMatthias Ringwald };
941d5191c9SMatthias Ringwald 
951d5191c9SMatthias Ringwald static const uint8_t keytable_us_shift[] = {
961d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*  0-3  */
971d5191c9SMatthias Ringwald     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',                   /*  4-13 */
981d5191c9SMatthias Ringwald     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',                   /* 14-23 */
991d5191c9SMatthias Ringwald     'U', 'V', 'W', 'X', 'Y', 'Z',                                       /* 24-29 */
1001d5191c9SMatthias Ringwald     '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',                   /* 30-39 */
1011d5191c9SMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
1021d5191c9SMatthias Ringwald     '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<',         /* 45-54 */
1031d5191c9SMatthias Ringwald     '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
1041d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
1051d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
1061d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
1071d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
1081d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
1091d5191c9SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
1101d5191c9SMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
1111d5191c9SMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xb1,                                 /* 97-100 */
1121d5191c9SMatthias Ringwald };
1131d5191c9SMatthias Ringwald 
11405439aa6SMilanka Ringwald // SDP
115d93f9014SMilanka Ringwald static uint8_t hid_descriptor_storage[MAX_ATTRIBUTE_VALUE_SIZE];
1162d05904dSMatthias Ringwald 
11705439aa6SMilanka Ringwald // App
11805439aa6SMilanka Ringwald static enum {
11905439aa6SMilanka Ringwald     APP_IDLE,
12005439aa6SMilanka Ringwald     APP_CONNECTED
12105439aa6SMilanka Ringwald } app_state = APP_IDLE;
12205439aa6SMilanka Ringwald 
12305439aa6SMilanka Ringwald static uint16_t hid_host_cid = 0;
12405439aa6SMilanka Ringwald static bool     hid_host_descriptor_available = false;
125e615b0ecSMilanka Ringwald static hid_protocol_mode_t hid_host_report_mode = HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT;
126e615b0ecSMilanka Ringwald 
1272d05904dSMatthias Ringwald /* @section Main application configuration
1282d05904dSMatthias Ringwald  *
129d93f9014SMilanka Ringwald  * @text In the application configuration, L2CAP and HID host are initialized, and the link policies
130d93f9014SMilanka Ringwald  * are set to allow sniff mode and role change.
1312d05904dSMatthias Ringwald  */
1322d05904dSMatthias Ringwald 
1332d05904dSMatthias Ringwald /* LISTING_START(PanuSetup): Panu setup */
1342d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1352d05904dSMatthias Ringwald 
hid_host_setup(void)1362d05904dSMatthias Ringwald static void hid_host_setup(void){
1372d05904dSMatthias Ringwald 
138a4fe6467SMatthias Ringwald     // Initialize L2CAP
139a4fe6467SMatthias Ringwald     l2cap_init();
140a4fe6467SMatthias Ringwald 
1418c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE
1428c9bb29eSMatthias Ringwald     // Initialize LE Security Manager. Needed for cross-transport key derivation
1438c9bb29eSMatthias Ringwald     sm_init();
1448c9bb29eSMatthias Ringwald #endif
1458c9bb29eSMatthias Ringwald 
14605439aa6SMilanka Ringwald     // Initialize HID Host
147d93f9014SMilanka Ringwald     hid_host_init(hid_descriptor_storage, sizeof(hid_descriptor_storage));
14805439aa6SMilanka Ringwald     hid_host_register_packet_handler(packet_handler);
1496e1e5689SMatthias Ringwald 
1508e0c4421SMatthias Ringwald     // Allow sniff mode requests by HID device and support role switch
1518e0c4421SMatthias Ringwald     gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH);
1528e0c4421SMatthias Ringwald 
1538e0c4421SMatthias Ringwald     // try to become master on incoming connections
1548e0c4421SMatthias Ringwald     hci_set_master_slave_policy(HCI_ROLE_MASTER);
1556e1e5689SMatthias Ringwald 
1562d05904dSMatthias Ringwald     // register for HCI events
1572d05904dSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1582d05904dSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1592d05904dSMatthias Ringwald 
1609cf717e8SMatthias Ringwald     // make discoverable to allow HID device to initiate connection
1619cf717e8SMatthias Ringwald     gap_discoverable_control(1);
1629cf717e8SMatthias Ringwald 
1631d5191c9SMatthias Ringwald     // Disable stdout buffering
1649d010406SMatthias 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];
1791f790386SMatthias Ringwald static bool hid_host_caps_lock;
1801f790386SMatthias Ringwald 
1811f790386SMatthias Ringwald static uint16_t hid_host_led_report_id;
1821f790386SMatthias Ringwald static uint8_t  hid_host_led_report_len;
1831f790386SMatthias Ringwald static uint8_t  hid_host_led_caps_lock_bit;
1841f790386SMatthias Ringwald 
hid_host_set_leds(void)1851f790386SMatthias Ringwald static void hid_host_set_leds(void){
1861f790386SMatthias Ringwald     if (hid_host_led_report_len == 0) return;
1871f790386SMatthias Ringwald 
1881f790386SMatthias Ringwald     uint8_t output_report[8];
1891f790386SMatthias Ringwald 
1901f790386SMatthias Ringwald     uint8_t caps_lock_report_offset = hid_host_led_caps_lock_bit >> 3;
1911f790386SMatthias Ringwald     if (caps_lock_report_offset >= sizeof(output_report)) return;
1921f790386SMatthias Ringwald 
1931f790386SMatthias Ringwald     memset(output_report, 0, sizeof(output_report));
1941f790386SMatthias Ringwald     if (hid_host_caps_lock){
1951f790386SMatthias Ringwald         output_report[caps_lock_report_offset] = 1 << (hid_host_led_caps_lock_bit & 0x07);
1961f790386SMatthias Ringwald     }
1971f790386SMatthias Ringwald     hid_host_send_set_report(hid_host_cid, HID_REPORT_TYPE_OUTPUT, hid_host_led_report_id, output_report, hid_host_led_report_len);
1981f790386SMatthias Ringwald }
1991f790386SMatthias Ringwald 
hid_host_demo_lookup_caps_lock_led(void)200b129caddSMatthias Ringwald static void hid_host_demo_lookup_caps_lock_led(void){
201b129caddSMatthias Ringwald     btstack_hid_usage_iterator_t iterator;
202b129caddSMatthias Ringwald     const uint8_t *hid_descriptor = hid_descriptor_storage_get_descriptor_data(hid_host_cid);
203b129caddSMatthias Ringwald     const uint16_t hid_descriptor_len = hid_descriptor_storage_get_descriptor_len(hid_host_cid);
204b129caddSMatthias Ringwald     btstack_hid_usage_iterator_init(&iterator, hid_descriptor, hid_descriptor_len, HID_REPORT_TYPE_OUTPUT);
205b129caddSMatthias Ringwald     while (btstack_hid_usage_iterator_has_more(&iterator)){
206b129caddSMatthias Ringwald         btstack_hid_usage_item_t item;
207b129caddSMatthias Ringwald         btstack_hid_usage_iterator_get_item(&iterator, &item);
208b129caddSMatthias Ringwald         if (item.usage_page == HID_USAGE_PAGE_LED && item.usage == HID_USAGE_LED_CAPS_LOCK){
209b129caddSMatthias Ringwald             hid_host_led_report_id     = item.report_id;
210b129caddSMatthias Ringwald             hid_host_led_report_len    = btstack_hid_get_report_size_for_id(hid_host_led_report_id, HID_REPORT_TYPE_OUTPUT, hid_descriptor, hid_descriptor_len);
211b129caddSMatthias Ringwald             hid_host_led_caps_lock_bit = (uint8_t) item.bit_pos;
212b129caddSMatthias Ringwald             printf("Found CAPS LOCK in Output Report with ID 0x%04x at bit %3u\n", hid_host_led_report_id, hid_host_led_caps_lock_bit);
213b129caddSMatthias Ringwald         }
214b129caddSMatthias Ringwald     }
215b129caddSMatthias Ringwald }
216b129caddSMatthias Ringwald 
hid_host_handle_interrupt_report(const uint8_t * report,uint16_t report_len)2171d5191c9SMatthias Ringwald static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){
2181d5191c9SMatthias Ringwald     // check if HID Input Report
2191d5191c9SMatthias Ringwald     if (report_len < 1) return;
2201d5191c9SMatthias Ringwald     if (*report != 0xa1) return;
22105439aa6SMilanka Ringwald 
2221d5191c9SMatthias Ringwald     report++;
2231d5191c9SMatthias Ringwald     report_len--;
22405439aa6SMilanka Ringwald 
2251d5191c9SMatthias Ringwald     btstack_hid_parser_t parser;
22605439aa6SMilanka Ringwald     btstack_hid_parser_init(&parser,
22705439aa6SMilanka Ringwald         hid_descriptor_storage_get_descriptor_data(hid_host_cid),
22805439aa6SMilanka Ringwald         hid_descriptor_storage_get_descriptor_len(hid_host_cid),
22905439aa6SMilanka Ringwald         HID_REPORT_TYPE_INPUT, report, report_len);
23005439aa6SMilanka Ringwald 
2311f790386SMatthias Ringwald     bool shift = hid_host_caps_lock;
2321d5191c9SMatthias Ringwald     uint8_t new_keys[NUM_KEYS];
2331d5191c9SMatthias Ringwald     memset(new_keys, 0, sizeof(new_keys));
2341d5191c9SMatthias Ringwald     int     new_keys_count = 0;
2351f790386SMatthias Ringwald 
2361d5191c9SMatthias Ringwald     while (btstack_hid_parser_has_more(&parser)){
2371d5191c9SMatthias Ringwald         uint16_t usage_page;
2381d5191c9SMatthias Ringwald         uint16_t usage;
2391d5191c9SMatthias Ringwald         int32_t  value;
2401d5191c9SMatthias Ringwald         btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value);
2411d5191c9SMatthias Ringwald         if (usage_page != 0x07) continue;
2421d5191c9SMatthias Ringwald         switch (usage){
243678408a1SMatthias Ringwald             case HID_USAGE_KEY_KEYBOARD_CAPS_LOCK:
2441f790386SMatthias Ringwald                 // Toggle Caps Lock
2451f790386SMatthias Ringwald                 hid_host_caps_lock = !hid_host_caps_lock;
2461f790386SMatthias Ringwald                 // update LEDs
2471f790386SMatthias Ringwald                 hid_host_set_leds();
2481f790386SMatthias Ringwald                 break;
249678408a1SMatthias Ringwald             case HID_USAGE_KEY_KEYBOARD_LEFTSHIFT:
250678408a1SMatthias Ringwald             case HID_USAGE_KEY_KEYBOARD_RIGHTSHIFT:
2511d5191c9SMatthias Ringwald                 if (value){
2521d5191c9SMatthias Ringwald                     shift = 1;
2531d5191c9SMatthias Ringwald                 }
2541d5191c9SMatthias Ringwald                 continue;
255678408a1SMatthias Ringwald             case HID_USAGE_KEY_RESERVED:
2561d5191c9SMatthias Ringwald                 continue;
2571d5191c9SMatthias Ringwald             default:
2581d5191c9SMatthias Ringwald                 break;
2591d5191c9SMatthias Ringwald         }
2601d5191c9SMatthias Ringwald         if (usage >= sizeof(keytable_us_none)) continue;
2611d5191c9SMatthias Ringwald 
2621d5191c9SMatthias Ringwald         // store new keys
2639d010406SMatthias Ringwald         new_keys[new_keys_count++] = (uint8_t) usage;
2641d5191c9SMatthias Ringwald 
2651d5191c9SMatthias Ringwald         // check if usage was used last time (and ignore in that case)
2661d5191c9SMatthias Ringwald         int i;
2671d5191c9SMatthias Ringwald         for (i=0;i<NUM_KEYS;i++){
2681d5191c9SMatthias Ringwald             if (usage == last_keys[i]){
2691d5191c9SMatthias Ringwald                 usage = 0;
2701d5191c9SMatthias Ringwald             }
2711d5191c9SMatthias Ringwald         }
2721d5191c9SMatthias Ringwald         if (usage == 0) continue;
2731d5191c9SMatthias Ringwald 
2741d5191c9SMatthias Ringwald         uint8_t key;
2751d5191c9SMatthias Ringwald         if (shift){
2761d5191c9SMatthias Ringwald             key = keytable_us_shift[usage];
2771d5191c9SMatthias Ringwald         } else {
2781d5191c9SMatthias Ringwald             key = keytable_us_none[usage];
2791d5191c9SMatthias Ringwald         }
2801d5191c9SMatthias Ringwald         if (key == CHAR_ILLEGAL) continue;
2811d5191c9SMatthias Ringwald         if (key == CHAR_BACKSPACE){
2821d5191c9SMatthias Ringwald             printf("\b \b");    // go back one char, print space, go back one char again
2831d5191c9SMatthias Ringwald             continue;
2841d5191c9SMatthias Ringwald         }
2851d5191c9SMatthias Ringwald         printf("%c", key);
286*5d4d8cc7SMatthias Ringwald         fflush(stdout);
2871d5191c9SMatthias Ringwald     }
2881d5191c9SMatthias Ringwald     memcpy(last_keys, new_keys, NUM_KEYS);
2891d5191c9SMatthias Ringwald }
2901d5191c9SMatthias Ringwald 
2911d5191c9SMatthias Ringwald /*
2922d05904dSMatthias Ringwald  * @section Packet Handler
2932d05904dSMatthias Ringwald  *
294d93f9014SMilanka Ringwald  * @text The packet handler responds to various HID events.
2952d05904dSMatthias Ringwald  */
2962d05904dSMatthias Ringwald 
2972d05904dSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)2982d05904dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
2992d05904dSMatthias Ringwald {
3002d05904dSMatthias Ringwald     /* LISTING_PAUSE */
30105439aa6SMilanka Ringwald     UNUSED(channel);
30205439aa6SMilanka Ringwald     UNUSED(size);
30305439aa6SMilanka Ringwald 
3042d05904dSMatthias Ringwald     uint8_t   event;
3052d05904dSMatthias Ringwald     bd_addr_t event_addr;
306e85416c1SMatthias Ringwald     uint8_t   status;
3072d05904dSMatthias Ringwald 
3082d05904dSMatthias Ringwald     /* LISTING_RESUME */
3092d05904dSMatthias Ringwald     switch (packet_type) {
3102d05904dSMatthias Ringwald 		case HCI_EVENT_PACKET:
3112d05904dSMatthias Ringwald             event = hci_event_packet_get_type(packet);
31205439aa6SMilanka Ringwald 
3132d05904dSMatthias Ringwald             switch (event) {
31405439aa6SMilanka Ringwald #ifndef HAVE_BTSTACK_STDIN
3152d05904dSMatthias Ringwald                 /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING
3162d05904dSMatthias Ringwald                  * is received and the example is started in client mode, the remote SDP HID query is started.
3172d05904dSMatthias Ringwald                  */
3182d05904dSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
3192d05904dSMatthias Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
320e615b0ecSMilanka Ringwald                         status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid);
32105439aa6SMilanka Ringwald                         if (status != ERROR_CODE_SUCCESS){
322d93f9014SMilanka Ringwald                             printf("HID host connect failed, status 0x%02x.\n", status);
32305439aa6SMilanka Ringwald                         }
3242d05904dSMatthias Ringwald                     }
3252d05904dSMatthias Ringwald                     break;
32605439aa6SMilanka Ringwald #endif
3272d05904dSMatthias Ringwald                 /* LISTING_PAUSE */
3282d05904dSMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
3292d05904dSMatthias Ringwald 					// inform about pin code request
3302d05904dSMatthias Ringwald                     printf("Pin code request - using '0000'\n");
3312d05904dSMatthias Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
3322d05904dSMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
3332d05904dSMatthias Ringwald 					break;
3342d05904dSMatthias Ringwald 
3352d05904dSMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
3362d05904dSMatthias Ringwald                     // inform about user confirmation request
337173fff9bSMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8));
3382d05904dSMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
3392d05904dSMatthias Ringwald                     break;
3402d05904dSMatthias Ringwald 
3412d05904dSMatthias Ringwald                 /* LISTING_RESUME */
34205439aa6SMilanka Ringwald                 case HCI_EVENT_HID_META:
34305439aa6SMilanka Ringwald                     switch (hci_event_hid_meta_get_subevent_code(packet)){
344d93f9014SMilanka Ringwald 
34505439aa6SMilanka Ringwald                         case HID_SUBEVENT_INCOMING_CONNECTION:
346d93f9014SMilanka Ringwald                             // There is an incoming connection: we can accept it or decline it.
347d93f9014SMilanka Ringwald                             // The hid_host_report_mode in the hid_host_accept_connection function
348d93f9014SMilanka Ringwald                             // allows the application to request a protocol mode.
3493cbedd43SMatthias Ringwald                             // For available protocol modes, see hid_protocol_mode_t in btstack_hid.h file.
350e615b0ecSMilanka Ringwald                             hid_host_accept_connection(hid_subevent_incoming_connection_get_hid_cid(packet), hid_host_report_mode);
3516e1e5689SMatthias Ringwald                             break;
3526e1e5689SMatthias Ringwald 
35305439aa6SMilanka Ringwald                         case HID_SUBEVENT_CONNECTION_OPENED:
354d93f9014SMilanka Ringwald                             // The status field of this event indicates if the control and interrupt
355d93f9014SMilanka Ringwald                             // connections were opened successfully.
35605439aa6SMilanka Ringwald                             status = hid_subevent_connection_opened_get_status(packet);
357d93f9014SMilanka Ringwald                             if (status != ERROR_CODE_SUCCESS) {
358fcd55a0bSMilanka Ringwald                                 printf("Connection failed, status 0x%02x\n", status);
35905439aa6SMilanka Ringwald                                 app_state = APP_IDLE;
36005439aa6SMilanka Ringwald                                 hid_host_cid = 0;
36105439aa6SMilanka Ringwald                                 return;
362e85416c1SMatthias Ringwald                             }
36305439aa6SMilanka Ringwald                             app_state = APP_CONNECTED;
36405439aa6SMilanka Ringwald                             hid_host_descriptor_available = false;
36505439aa6SMilanka Ringwald                             hid_host_cid = hid_subevent_connection_opened_get_hid_cid(packet);
3661f790386SMatthias Ringwald                             hid_host_caps_lock = false;
3671f790386SMatthias Ringwald                             hid_host_led_report_len = 0;
368d93f9014SMilanka Ringwald                             printf("HID Host connected.\n");
36905439aa6SMilanka Ringwald                             break;
37005439aa6SMilanka Ringwald 
37105439aa6SMilanka Ringwald                         case HID_SUBEVENT_DESCRIPTOR_AVAILABLE:
372d93f9014SMilanka Ringwald                             // This event will follows HID_SUBEVENT_CONNECTION_OPENED event.
373d93f9014SMilanka Ringwald                             // For incoming connections, i.e. HID Device initiating the connection,
374d93f9014SMilanka Ringwald                             // the HID_SUBEVENT_DESCRIPTOR_AVAILABLE is delayed, and some HID
375d93f9014SMilanka Ringwald                             // reports may be received via HID_SUBEVENT_REPORT event. It is up to
376d93f9014SMilanka Ringwald                             // the application if these reports should be buffered or ignored until
377d93f9014SMilanka Ringwald                             // the HID descriptor is available.
37805439aa6SMilanka Ringwald                             status = hid_subevent_descriptor_available_get_status(packet);
37905439aa6SMilanka Ringwald                             if (status == ERROR_CODE_SUCCESS){
38005439aa6SMilanka Ringwald                                 hid_host_descriptor_available = true;
381d93f9014SMilanka Ringwald                                 printf("HID Descriptor available, please start typing.\n");
3821f790386SMatthias Ringwald                                 hid_host_demo_lookup_caps_lock_led();
383e615b0ecSMilanka Ringwald                             } else {
384fcd55a0bSMilanka Ringwald                                 printf("Cannot handle input report, HID Descriptor is not available, status 0x%02x\n", status);
3856e1e5689SMatthias Ringwald                             }
386e85416c1SMatthias Ringwald                             break;
38705439aa6SMilanka Ringwald 
388d93f9014SMilanka Ringwald                         case HID_SUBEVENT_REPORT:
389d93f9014SMilanka Ringwald                             // Handle input report.
390d93f9014SMilanka Ringwald                             if (hid_host_descriptor_available){
391d93f9014SMilanka Ringwald                                 hid_host_handle_interrupt_report(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet));
392d93f9014SMilanka Ringwald                             } else {
393d93f9014SMilanka Ringwald                                 printf_hexdump(hid_subevent_report_get_report(packet), hid_subevent_report_get_report_len(packet));
3942d05904dSMatthias Ringwald                             }
395e85416c1SMatthias Ringwald                             break;
39605439aa6SMilanka Ringwald 
39705439aa6SMilanka Ringwald                         case HID_SUBEVENT_SET_PROTOCOL_RESPONSE:
398d93f9014SMilanka Ringwald                             // For incoming connections, the library will set the protocol mode of the
399d93f9014SMilanka Ringwald                             // HID Device as requested in the call to hid_host_accept_connection. The event
400d93f9014SMilanka Ringwald                             // reports the result. For connections initiated by calling hid_host_connect,
401d93f9014SMilanka Ringwald                             // this event will occur only if the established report mode is boot mode.
402f1aa1cdcSMilanka Ringwald                             status = hid_subevent_set_protocol_response_get_handshake_status(packet);
40305439aa6SMilanka Ringwald                             if (status != HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL){
40405439aa6SMilanka Ringwald                                 printf("Error set protocol, status 0x%02x\n", status);
405e85416c1SMatthias Ringwald                                 break;
406e85416c1SMatthias Ringwald                             }
407f1aa1cdcSMilanka Ringwald                             switch ((hid_protocol_mode_t)hid_subevent_set_protocol_response_get_protocol_mode(packet)){
408f1aa1cdcSMilanka Ringwald                                 case HID_PROTOCOL_MODE_BOOT:
409e615b0ecSMilanka Ringwald                                     printf("Protocol mode set: BOOT.\n");
410f1aa1cdcSMilanka Ringwald                                     break;
411f1aa1cdcSMilanka Ringwald                                 case HID_PROTOCOL_MODE_REPORT:
412e615b0ecSMilanka Ringwald                                     printf("Protocol mode set: REPORT.\n");
413f1aa1cdcSMilanka Ringwald                                     break;
414f1aa1cdcSMilanka Ringwald                                 default:
415f1aa1cdcSMilanka Ringwald                                     printf("Unknown protocol mode.\n");
416f1aa1cdcSMilanka Ringwald                                     break;
417f1aa1cdcSMilanka Ringwald                             }
41805439aa6SMilanka Ringwald                             break;
41905439aa6SMilanka Ringwald 
420d93f9014SMilanka Ringwald                         case HID_SUBEVENT_CONNECTION_CLOSED:
421d93f9014SMilanka Ringwald                             // The connection was closed.
422d93f9014SMilanka Ringwald                             hid_host_cid = 0;
423d93f9014SMilanka Ringwald                             hid_host_descriptor_available = false;
424d93f9014SMilanka Ringwald                             printf("HID Host disconnected.\n");
42505439aa6SMilanka Ringwald                             break;
42605439aa6SMilanka Ringwald 
42705439aa6SMilanka Ringwald                         default:
42805439aa6SMilanka Ringwald                             break;
42905439aa6SMilanka Ringwald                     }
43005439aa6SMilanka Ringwald                     break;
43105439aa6SMilanka Ringwald                 default:
43205439aa6SMilanka Ringwald                     break;
43305439aa6SMilanka Ringwald             }
43405439aa6SMilanka Ringwald             break;
4352d05904dSMatthias Ringwald         default:
4362d05904dSMatthias Ringwald             break;
4372d05904dSMatthias Ringwald     }
4382d05904dSMatthias Ringwald }
4392d05904dSMatthias Ringwald /* LISTING_END */
4402d05904dSMatthias Ringwald 
44105439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN
show_usage(void)44205439aa6SMilanka Ringwald static void show_usage(void){
44305439aa6SMilanka Ringwald     bd_addr_t      iut_address;
44405439aa6SMilanka Ringwald     gap_local_bd_addr(iut_address);
445d93f9014SMilanka Ringwald     printf("\n--- Bluetooth HID Host Console %s ---\n", bd_addr_to_str(iut_address));
446d93f9014SMilanka Ringwald     printf("c      - Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string);
447d93f9014SMilanka Ringwald     printf("C      - Disconnect\n");
44805439aa6SMilanka Ringwald 
44905439aa6SMilanka Ringwald     printf("\n");
45005439aa6SMilanka Ringwald     printf("Ctrl-c - exit\n");
45105439aa6SMilanka Ringwald     printf("---\n");
45205439aa6SMilanka Ringwald }
45305439aa6SMilanka Ringwald 
stdin_process(char cmd)45405439aa6SMilanka Ringwald static void stdin_process(char cmd){
45505439aa6SMilanka Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
45605439aa6SMilanka Ringwald     switch (cmd){
45705439aa6SMilanka Ringwald         case 'c':
458d93f9014SMilanka Ringwald             printf("Connect to %s in report mode, with fallback to boot mode.\n", remote_addr_string);
459e615b0ecSMilanka Ringwald             status = hid_host_connect(remote_addr, hid_host_report_mode, &hid_host_cid);
46005439aa6SMilanka Ringwald             break;
46105439aa6SMilanka Ringwald         case 'C':
462d93f9014SMilanka Ringwald             printf("Disconnect...\n");
46305439aa6SMilanka Ringwald             hid_host_disconnect(hid_host_cid);
46405439aa6SMilanka Ringwald             break;
46505439aa6SMilanka Ringwald         case '\n':
46605439aa6SMilanka Ringwald         case '\r':
46705439aa6SMilanka Ringwald             break;
46805439aa6SMilanka Ringwald         default:
46905439aa6SMilanka Ringwald             show_usage();
47005439aa6SMilanka Ringwald             break;
47105439aa6SMilanka Ringwald     }
47205439aa6SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
47305439aa6SMilanka Ringwald         printf("HID host cmd \'%c\' failed, status 0x%02x\n", cmd, status);
47405439aa6SMilanka Ringwald     }
47505439aa6SMilanka Ringwald }
47605439aa6SMilanka Ringwald #endif
47705439aa6SMilanka Ringwald 
4782d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])4792d05904dSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
4802d05904dSMatthias Ringwald 
4812d05904dSMatthias Ringwald     (void)argc;
4822d05904dSMatthias Ringwald     (void)argv;
4832d05904dSMatthias Ringwald 
4842d05904dSMatthias Ringwald     hid_host_setup();
4852d05904dSMatthias Ringwald 
4862d05904dSMatthias Ringwald     // parse human readable Bluetooth address
4872d05904dSMatthias Ringwald     sscanf_bd_addr(remote_addr_string, remote_addr);
4882d05904dSMatthias Ringwald 
48905439aa6SMilanka Ringwald #ifdef HAVE_BTSTACK_STDIN
49005439aa6SMilanka Ringwald     btstack_stdin_setup(stdin_process);
49105439aa6SMilanka Ringwald #endif
49205439aa6SMilanka Ringwald 
4932d05904dSMatthias Ringwald     // Turn on the device
4942d05904dSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
4952d05904dSMatthias Ringwald     return 0;
4962d05904dSMatthias Ringwald }
4972d05904dSMatthias Ringwald 
4982d05904dSMatthias Ringwald /* EXAMPLE_END */
499