12fa56ea6SMatthias Ringwald /* 22fa56ea6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 32fa56ea6SMatthias Ringwald * 42fa56ea6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 52fa56ea6SMatthias Ringwald * modification, are permitted provided that the following conditions 62fa56ea6SMatthias Ringwald * are met: 72fa56ea6SMatthias Ringwald * 82fa56ea6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 92fa56ea6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 102fa56ea6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 112fa56ea6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 122fa56ea6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 132fa56ea6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 142fa56ea6SMatthias Ringwald * contributors may be used to endorse or promote products derived 152fa56ea6SMatthias Ringwald * from this software without specific prior written permission. 162fa56ea6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 172fa56ea6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 182fa56ea6SMatthias Ringwald * monetary gain. 192fa56ea6SMatthias Ringwald * 202fa56ea6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 212fa56ea6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 222fa56ea6SMatthias 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, 252fa56ea6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 262fa56ea6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 272fa56ea6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 282fa56ea6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 292fa56ea6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 302fa56ea6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312fa56ea6SMatthias Ringwald * SUCH DAMAGE. 322fa56ea6SMatthias Ringwald * 332fa56ea6SMatthias Ringwald * Please inquire about commercial licensing options at 342fa56ea6SMatthias Ringwald * [email protected] 352fa56ea6SMatthias Ringwald * 362fa56ea6SMatthias Ringwald */ 372fa56ea6SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hid_keyboard_demo.c" 392fa56ea6SMatthias Ringwald 402fa56ea6SMatthias Ringwald // ***************************************************************************** 41ec8ae085SMilanka Ringwald /* EXAMPLE_START(hid_keyboard_demo): HID Keyboard Classic 428eb8d463SMatthias Ringwald * 432fa56ea6SMatthias Ringwald * @text This HID Device example demonstrates how to implement 447ea7688aSMatthias Ringwald * an HID keyboard. Without a HAVE_BTSTACK_STDIN, a fixed demo text is sent 457ea7688aSMatthias Ringwald * If HAVE_BTSTACK_STDIN is defined, you can type from the terminal 462fa56ea6SMatthias Ringwald */ 472fa56ea6SMatthias Ringwald // ***************************************************************************** 482fa56ea6SMatthias Ringwald 492fa56ea6SMatthias Ringwald 502fa56ea6SMatthias Ringwald #include <stdint.h> 512fa56ea6SMatthias Ringwald #include <stdio.h> 522fa56ea6SMatthias Ringwald #include <stdlib.h> 532fa56ea6SMatthias Ringwald #include <string.h> 540316aa6fSMatthias Ringwald #include <inttypes.h> 552fa56ea6SMatthias Ringwald 562fa56ea6SMatthias Ringwald #include "btstack.h" 576518788aSMatthias Ringwald 587ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 597ea7688aSMatthias Ringwald #include "btstack_stdin.h" 602fa56ea6SMatthias Ringwald #endif 612fa56ea6SMatthias Ringwald 628eb8d463SMatthias Ringwald // to enable demo text on POSIX systems 637ea7688aSMatthias Ringwald // #undef HAVE_BTSTACK_STDIN 642fa56ea6SMatthias Ringwald 651884c638SMatthias Ringwald // timing of keypresses 661884c638SMatthias Ringwald #define TYPING_KEYDOWN_MS 20 671884c638SMatthias Ringwald #define TYPING_DELAY_MS 20 681884c638SMatthias Ringwald 69574d351dSMatthias Ringwald // When not set to 0xffff, sniff and sniff subrating are enabled 70574d351dSMatthias Ringwald static uint16_t host_max_latency = 1600; 71574d351dSMatthias Ringwald static uint16_t host_min_timeout = 3200; 72574d351dSMatthias Ringwald 73912af117SMatthias Ringwald #define REPORT_ID 0x01 74912af117SMatthias Ringwald 75912af117SMatthias Ringwald // close to USB HID Specification 1.1, Appendix B.1 76912af117SMatthias Ringwald const uint8_t hid_descriptor_keyboard[] = { 772fa56ea6SMatthias Ringwald 782fa56ea6SMatthias Ringwald 0x05, 0x01, // Usage Page (Generic Desktop) 792fa56ea6SMatthias Ringwald 0x09, 0x06, // Usage (Keyboard) 802fa56ea6SMatthias Ringwald 0xa1, 0x01, // Collection (Application) 812fa56ea6SMatthias Ringwald 82912af117SMatthias Ringwald // Report ID 832fa56ea6SMatthias Ringwald 84912af117SMatthias Ringwald 0x85, REPORT_ID, // Report ID 85912af117SMatthias Ringwald 86912af117SMatthias Ringwald // Modifier byte (input) 87912af117SMatthias Ringwald 882fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 892fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 902fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 912fa56ea6SMatthias Ringwald 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 92c56c3b7aSMatthias Ringwald 0x29, 0xe7, // Usage Maximum (Keyboard Right GUI) 932fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 942fa56ea6SMatthias Ringwald 0x25, 0x01, // Logical Maximum (1) 952fa56ea6SMatthias Ringwald 0x81, 0x02, // Input (Data, Variable, Absolute) 962fa56ea6SMatthias Ringwald 97912af117SMatthias Ringwald // Reserved byte (input) 982fa56ea6SMatthias Ringwald 992fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 1002fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 1012fa56ea6SMatthias Ringwald 0x81, 0x03, // Input (Constant, Variable, Absolute) 1022fa56ea6SMatthias Ringwald 103912af117SMatthias Ringwald // LED report + padding (output) 1042fa56ea6SMatthias Ringwald 1052fa56ea6SMatthias Ringwald 0x95, 0x05, // Report Count (5) 1062fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 1072fa56ea6SMatthias Ringwald 0x05, 0x08, // Usage Page (LEDs) 1082fa56ea6SMatthias Ringwald 0x19, 0x01, // Usage Minimum (Num Lock) 109c56c3b7aSMatthias Ringwald 0x29, 0x05, // Usage Maximum (Kana) 1102fa56ea6SMatthias Ringwald 0x91, 0x02, // Output (Data, Variable, Absolute) 1112fa56ea6SMatthias Ringwald 1122fa56ea6SMatthias Ringwald 0x95, 0x01, // Report Count (1) 1132fa56ea6SMatthias Ringwald 0x75, 0x03, // Report Size (3) 1142fa56ea6SMatthias Ringwald 0x91, 0x03, // Output (Constant, Variable, Absolute) 1152fa56ea6SMatthias Ringwald 116912af117SMatthias Ringwald // Keycodes (input) 1172fa56ea6SMatthias Ringwald 1182fa56ea6SMatthias Ringwald 0x95, 0x06, // Report Count (6) 1192fa56ea6SMatthias Ringwald 0x75, 0x08, // Report Size (8) 1202fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 1212fa56ea6SMatthias Ringwald 0x25, 0xff, // Logical Maximum (1) 1222fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 1232fa56ea6SMatthias Ringwald 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 124c56c3b7aSMatthias Ringwald 0x29, 0xff, // Usage Maximum (Reserved) 1252fa56ea6SMatthias Ringwald 0x81, 0x00, // Input (Data, Array) 1262fa56ea6SMatthias Ringwald 1272fa56ea6SMatthias Ringwald 0xc0, // End collection 1282fa56ea6SMatthias Ringwald }; 1292fa56ea6SMatthias Ringwald 130ca44a803SMatthias Ringwald // 131ca44a803SMatthias Ringwald #define CHAR_ILLEGAL 0xff 132ca44a803SMatthias Ringwald #define CHAR_RETURN '\n' 133ca44a803SMatthias Ringwald #define CHAR_ESCAPE 27 134ca44a803SMatthias Ringwald #define CHAR_TAB '\t' 135ca44a803SMatthias Ringwald #define CHAR_BACKSPACE 0x7f 136ca44a803SMatthias Ringwald 137ca44a803SMatthias Ringwald // Simplified US Keyboard with Shift modifier 138ca44a803SMatthias Ringwald 139ca44a803SMatthias Ringwald /** 140ca44a803SMatthias Ringwald * English (US) 141ca44a803SMatthias Ringwald */ 142ca44a803SMatthias Ringwald static const uint8_t keytable_us_none [] = { 143ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 144ca44a803SMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 145ca44a803SMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 146ca44a803SMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 147ca44a803SMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 148ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 149ca44a803SMatthias Ringwald '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 150ca44a803SMatthias Ringwald '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 151ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 152ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 153ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 154ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 155ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 156ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 157ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 158ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 159ca44a803SMatthias Ringwald }; 160ca44a803SMatthias Ringwald 161ca44a803SMatthias Ringwald static const uint8_t keytable_us_shift[] = { 162ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 163ca44a803SMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 164ca44a803SMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 165ca44a803SMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 166ca44a803SMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 167ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 168ca44a803SMatthias Ringwald '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 169ca44a803SMatthias Ringwald '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 170ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 171ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 172ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 173ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 174ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 175ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 176ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 177ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 178ca44a803SMatthias Ringwald }; 179ca44a803SMatthias Ringwald 1804e43ca9fSMatthias Ringwald // STATE 1814e43ca9fSMatthias Ringwald 182574d351dSMatthias Ringwald static uint8_t hid_service_buffer[300]; 1834e43ca9fSMatthias Ringwald static uint8_t device_id_sdp_service_buffer[100]; 1844e43ca9fSMatthias Ringwald static const char hid_device_name[] = "BTstack HID Keyboard"; 1854e43ca9fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 1864e43ca9fSMatthias Ringwald static uint16_t hid_cid; 187b274c7b3SMilanka Ringwald static uint8_t hid_boot_device = 0; 1884e43ca9fSMatthias Ringwald 1891884c638SMatthias Ringwald // HID Report sending 1901884c638SMatthias Ringwald static uint8_t send_buffer_storage[16]; 1911884c638SMatthias Ringwald static btstack_ring_buffer_t send_buffer; 1921884c638SMatthias Ringwald static btstack_timer_source_t send_timer; 1931884c638SMatthias Ringwald static uint8_t send_modifier; 1941884c638SMatthias Ringwald static uint8_t send_keycode; 1951884c638SMatthias Ringwald static bool send_active; 1961884c638SMatthias Ringwald 1974e43ca9fSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 1981884c638SMatthias Ringwald static bd_addr_t device_addr; 1994e43ca9fSMatthias Ringwald static const char * device_addr_string = "BC:EC:5D:E6:15:03"; 2004e43ca9fSMatthias Ringwald #endif 2014e43ca9fSMatthias Ringwald 2024e43ca9fSMatthias Ringwald static enum { 2034e43ca9fSMatthias Ringwald APP_BOOTING, 2044e43ca9fSMatthias Ringwald APP_NOT_CONNECTED, 2054e43ca9fSMatthias Ringwald APP_CONNECTING, 2064e43ca9fSMatthias Ringwald APP_CONNECTED 2074e43ca9fSMatthias Ringwald } app_state = APP_BOOTING; 2084e43ca9fSMatthias Ringwald 209ca44a803SMatthias Ringwald // HID Keyboard lookup 2101884c638SMatthias Ringwald static bool lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){ 211ca44a803SMatthias Ringwald int i; 212ca44a803SMatthias Ringwald for (i=0;i<size;i++){ 213ca44a803SMatthias Ringwald if (table[i] != character) continue; 214ca44a803SMatthias Ringwald *keycode = i; 2151884c638SMatthias Ringwald return true; 216ca44a803SMatthias Ringwald } 2171884c638SMatthias Ringwald return false; 218ca44a803SMatthias Ringwald } 219ca44a803SMatthias Ringwald 2201884c638SMatthias Ringwald static bool keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){ 2211884c638SMatthias Ringwald bool found; 222ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode); 223ca44a803SMatthias Ringwald if (found) { 224ca44a803SMatthias Ringwald *modifier = 0; // none 2251884c638SMatthias Ringwald return true; 226ca44a803SMatthias Ringwald } 227ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode); 228ca44a803SMatthias Ringwald if (found) { 229ca44a803SMatthias Ringwald *modifier = 2; // shift 2301884c638SMatthias Ringwald return true; 231ca44a803SMatthias Ringwald } 2321884c638SMatthias Ringwald return false; 2334cf72855SMatthias Ringwald } 2344cf72855SMatthias Ringwald 2354cf72855SMatthias Ringwald static void send_report(int modifier, int keycode){ 236912af117SMatthias Ringwald uint8_t report[] = { 0xa1, REPORT_ID, modifier, 0, keycode, 0, 0, 0, 0, 0}; 2378eb8d463SMatthias Ringwald hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report)); 2384cf72855SMatthias Ringwald } 2394cf72855SMatthias Ringwald 2401884c638SMatthias Ringwald static void trigger_key_up(btstack_timer_source_t * ts){ 2411884c638SMatthias Ringwald UNUSED(ts); 2421884c638SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 2431884c638SMatthias Ringwald } 2441884c638SMatthias Ringwald 2451884c638SMatthias Ringwald static void send_next(btstack_timer_source_t * ts) { 2461884c638SMatthias Ringwald // get next key from buffer 2471884c638SMatthias Ringwald uint8_t character; 2481884c638SMatthias Ringwald uint32_t num_bytes_read = 0; 2491884c638SMatthias Ringwald btstack_ring_buffer_read(&send_buffer, &character, 1, &num_bytes_read); 2501884c638SMatthias Ringwald if (num_bytes_read == 0) { 2511884c638SMatthias Ringwald // buffer empty, nothing to send 2521884c638SMatthias Ringwald send_active = false; 2531884c638SMatthias Ringwald } else { 2541884c638SMatthias Ringwald send_active = true; 2551884c638SMatthias Ringwald // lookup keycode and modifier using US layout 2561884c638SMatthias Ringwald bool found = keycode_and_modifer_us_for_character(character, &send_keycode, &send_modifier); 2571884c638SMatthias Ringwald if (found) { 2581884c638SMatthias Ringwald // request can send now 2591884c638SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 2601884c638SMatthias Ringwald } else { 2611884c638SMatthias Ringwald // restart timer for next character 2621884c638SMatthias Ringwald btstack_run_loop_set_timer(ts, TYPING_DELAY_MS); 2631884c638SMatthias Ringwald btstack_run_loop_add_timer(ts); 2641884c638SMatthias Ringwald } 2651884c638SMatthias Ringwald } 2661884c638SMatthias Ringwald } 2671884c638SMatthias Ringwald 2681884c638SMatthias Ringwald static void queue_character(char character){ 2691884c638SMatthias Ringwald btstack_ring_buffer_write(&send_buffer, (uint8_t *) &character, 1); 2701884c638SMatthias Ringwald if (send_active == false) { 2711884c638SMatthias Ringwald send_next(&send_timer); 2721884c638SMatthias Ringwald } 2731884c638SMatthias Ringwald } 2741884c638SMatthias Ringwald 2758eb8d463SMatthias Ringwald // Demo Application 276ca44a803SMatthias Ringwald 2777ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 2782fa56ea6SMatthias Ringwald 2798eb8d463SMatthias Ringwald // On systems with STDIN, we can directly type on the console 2802fa56ea6SMatthias Ringwald 28195a8ee01SMatthias Ringwald static void stdin_process(char character){ 2824e43ca9fSMatthias Ringwald switch (app_state){ 2834e43ca9fSMatthias Ringwald case APP_BOOTING: 2844e43ca9fSMatthias Ringwald case APP_CONNECTING: 2854e43ca9fSMatthias Ringwald // ignore 2864e43ca9fSMatthias Ringwald break; 2874e43ca9fSMatthias Ringwald case APP_CONNECTED: 2881884c638SMatthias Ringwald // add char to send buffer 2891884c638SMatthias Ringwald queue_character(character); 2904e43ca9fSMatthias Ringwald break; 2914e43ca9fSMatthias Ringwald case APP_NOT_CONNECTED: 2924e43ca9fSMatthias Ringwald printf("Connecting to %s...\n", bd_addr_to_str(device_addr)); 2934e43ca9fSMatthias Ringwald hid_device_connect(device_addr, &hid_cid); 2944e43ca9fSMatthias Ringwald break; 2957bbeb3adSMilanka Ringwald default: 2967bbeb3adSMilanka Ringwald btstack_assert(false); 2977bbeb3adSMilanka Ringwald break; 2984e43ca9fSMatthias Ringwald } 2992fa56ea6SMatthias Ringwald } 3006518788aSMatthias Ringwald #else 3012fa56ea6SMatthias Ringwald 3021884c638SMatthias Ringwald // On embedded systems, send constant demo text 3036518788aSMatthias Ringwald 3041884c638SMatthias Ringwald #define TYPING_DEMO_PERIOD_MS 100 3051884c638SMatthias Ringwald 3066518788aSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n"; 3076518788aSMatthias Ringwald static int demo_pos; 3081884c638SMatthias Ringwald static btstack_timer_source_t demo_text_timer; 3096518788aSMatthias Ringwald 3101884c638SMatthias Ringwald static void demo_text_timer_handler(btstack_timer_source_t * ts){ 3111884c638SMatthias Ringwald UNUSED(ts); 3126518788aSMatthias Ringwald 3131884c638SMatthias Ringwald // queue character 3146518788aSMatthias Ringwald uint8_t character = demo_text[demo_pos++]; 3156518788aSMatthias Ringwald if (demo_text[demo_pos] == 0){ 3166518788aSMatthias Ringwald demo_pos = 0; 3170316aa6fSMatthias Ringwald } 3181884c638SMatthias Ringwald queue_character(character); 3190316aa6fSMatthias Ringwald 3201884c638SMatthias Ringwald // set timer for next character 3211884c638SMatthias Ringwald btstack_run_loop_set_timer_handler(&demo_text_timer, demo_text_timer_handler); 3221884c638SMatthias Ringwald btstack_run_loop_set_timer(&demo_text_timer, TYPING_DEMO_PERIOD_MS); 3231884c638SMatthias Ringwald btstack_run_loop_add_timer(&demo_text_timer); 3246518788aSMatthias Ringwald } 3256518788aSMatthias Ringwald 3266518788aSMatthias Ringwald #endif 3276518788aSMatthias Ringwald 3280316aa6fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ 3292fa56ea6SMatthias Ringwald UNUSED(channel); 3300316aa6fSMatthias Ringwald UNUSED(packet_size); 3314e43ca9fSMatthias Ringwald uint8_t status; 3322fa56ea6SMatthias Ringwald switch (packet_type){ 3332fa56ea6SMatthias Ringwald case HCI_EVENT_PACKET: 3346058cb0dSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 3354e43ca9fSMatthias Ringwald case BTSTACK_EVENT_STATE: 3364e43ca9fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 3374e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3384e43ca9fSMatthias Ringwald break; 3394e43ca9fSMatthias Ringwald 3400316aa6fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 3410316aa6fSMatthias Ringwald // ssp: inform about user confirmation request 3420316aa6fSMatthias Ringwald log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet)); 3430316aa6fSMatthias Ringwald log_info("SSP User Confirmation Auto accept\n"); 3440316aa6fSMatthias Ringwald break; 3450316aa6fSMatthias Ringwald 3468eb8d463SMatthias Ringwald case HCI_EVENT_HID_META: 3478eb8d463SMatthias Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 3488eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 3494e43ca9fSMatthias Ringwald status = hid_subevent_connection_opened_get_status(packet); 3506058cb0dSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 3514e43ca9fSMatthias Ringwald // outgoing connection failed 3524e43ca9fSMatthias Ringwald printf("Connection failed, status 0x%x\n", status); 3534e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3544e43ca9fSMatthias Ringwald hid_cid = 0; 3554e43ca9fSMatthias Ringwald return; 3564e43ca9fSMatthias Ringwald } 3574e43ca9fSMatthias Ringwald app_state = APP_CONNECTED; 3588eb8d463SMatthias Ringwald hid_cid = hid_subevent_connection_opened_get_hid_cid(packet); 3597ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3608eb8d463SMatthias Ringwald printf("HID Connected, please start typing...\n"); 3618eb8d463SMatthias Ringwald #else 3628eb8d463SMatthias Ringwald printf("HID Connected, sending demo text...\n"); 3631884c638SMatthias Ringwald demo_text_timer_handler(NULL); 3646518788aSMatthias Ringwald #endif 3650316aa6fSMatthias Ringwald break; 3668eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 3671884c638SMatthias Ringwald btstack_run_loop_remove_timer(&send_timer); 3680316aa6fSMatthias Ringwald printf("HID Disconnected\n"); 3694e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3708eb8d463SMatthias Ringwald hid_cid = 0; 3710316aa6fSMatthias Ringwald break; 3728eb8d463SMatthias Ringwald case HID_SUBEVENT_CAN_SEND_NOW: 3734cf72855SMatthias Ringwald if (send_keycode){ 3744cf72855SMatthias Ringwald send_report(send_modifier, send_keycode); 3751884c638SMatthias Ringwald // schedule key up 3764cf72855SMatthias Ringwald send_keycode = 0; 3774cf72855SMatthias Ringwald send_modifier = 0; 3781884c638SMatthias Ringwald btstack_run_loop_set_timer_handler(&send_timer, trigger_key_up); 3791884c638SMatthias Ringwald btstack_run_loop_set_timer(&send_timer, TYPING_KEYDOWN_MS); 3804cf72855SMatthias Ringwald } else { 3814cf72855SMatthias Ringwald send_report(0, 0); 3821884c638SMatthias Ringwald // schedule next key down 3831884c638SMatthias Ringwald btstack_run_loop_set_timer_handler(&send_timer, send_next); 3841884c638SMatthias Ringwald btstack_run_loop_set_timer(&send_timer, TYPING_DELAY_MS); 3854cf72855SMatthias Ringwald } 3861884c638SMatthias Ringwald btstack_run_loop_add_timer(&send_timer); 3878eb8d463SMatthias Ringwald break; 3888eb8d463SMatthias Ringwald default: 3898eb8d463SMatthias Ringwald break; 3908eb8d463SMatthias Ringwald } 3918eb8d463SMatthias Ringwald break; 3922fa56ea6SMatthias Ringwald default: 3932fa56ea6SMatthias Ringwald break; 3942fa56ea6SMatthias Ringwald } 3952fa56ea6SMatthias Ringwald break; 3962fa56ea6SMatthias Ringwald default: 3972fa56ea6SMatthias Ringwald break; 3982fa56ea6SMatthias Ringwald } 3992fa56ea6SMatthias Ringwald } 4002fa56ea6SMatthias Ringwald 4012fa56ea6SMatthias Ringwald /* @section Main Application Setup 4022fa56ea6SMatthias Ringwald * 4032fa56ea6SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 4042fa56ea6SMatthias Ringwald * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. 4052fa56ea6SMatthias Ringwald * At the end the Bluetooth stack is started. 4062fa56ea6SMatthias Ringwald */ 4072fa56ea6SMatthias Ringwald 4082fa56ea6SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HID Device */ 4092fa56ea6SMatthias Ringwald 4102fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 4112fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 4122fa56ea6SMatthias Ringwald (void)argc; 4132fa56ea6SMatthias Ringwald (void)argv; 4142fa56ea6SMatthias Ringwald 41545a92abaSMatthias Ringwald // allow to get found by inquiry 4162fa56ea6SMatthias Ringwald gap_discoverable_control(1); 41745a92abaSMatthias Ringwald // use Limited Discoverable Mode; Peripheral; Keyboard as CoD 4182fa56ea6SMatthias Ringwald gap_set_class_of_device(0x2540); 41945a92abaSMatthias Ringwald // set local name to be identified - zeroes will be replaced by actual BD ADDR 4200c2b8870SMatthias Ringwald gap_set_local_name("HID Keyboard Demo 00:00:00:00:00:00"); 42145a92abaSMatthias Ringwald // allow for role switch in general and sniff mode 42245a92abaSMatthias Ringwald gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE ); 42345a92abaSMatthias Ringwald // allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it 42445a92abaSMatthias Ringwald gap_set_allow_role_switch(true); 4252fa56ea6SMatthias Ringwald 4262fa56ea6SMatthias Ringwald // L2CAP 4272fa56ea6SMatthias Ringwald l2cap_init(); 4282fa56ea6SMatthias Ringwald 4298c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE 4308c9bb29eSMatthias Ringwald // Initialize LE Security Manager. Needed for cross-transport key derivation 4318c9bb29eSMatthias Ringwald sm_init(); 4328c9bb29eSMatthias Ringwald #endif 4338c9bb29eSMatthias Ringwald 4342fa56ea6SMatthias Ringwald // SDP Server 4352fa56ea6SMatthias Ringwald sdp_init(); 4362fa56ea6SMatthias Ringwald memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); 4371ad99f3bSMilanka Ringwald 4381ad99f3bSMilanka Ringwald uint8_t hid_virtual_cable = 0; 4391ad99f3bSMilanka Ringwald uint8_t hid_remote_wake = 1; 4401ad99f3bSMilanka Ringwald uint8_t hid_reconnect_initiate = 1; 441ffe3f1a1SMilanka Ringwald uint8_t hid_normally_connectable = 1; 4421ad99f3bSMilanka Ringwald 44380d9d5d4SMilanka Ringwald hid_sdp_record_t hid_params = { 44480d9d5d4SMilanka Ringwald // hid sevice subclass 2540 Keyboard, hid counntry code 33 US 44580d9d5d4SMilanka Ringwald 0x2540, 33, 44680d9d5d4SMilanka Ringwald hid_virtual_cable, hid_remote_wake, 44780d9d5d4SMilanka Ringwald hid_reconnect_initiate, hid_normally_connectable, 44880d9d5d4SMilanka Ringwald hid_boot_device, 449574d351dSMatthias Ringwald host_max_latency, host_min_timeout, 450574d351dSMatthias Ringwald 3200, 451912af117SMatthias Ringwald hid_descriptor_keyboard, 452912af117SMatthias Ringwald sizeof(hid_descriptor_keyboard), 45380d9d5d4SMilanka Ringwald hid_device_name 45480d9d5d4SMilanka Ringwald }; 45580d9d5d4SMilanka Ringwald 456*762141afSMatthias Ringwald hid_create_sdp_record(hid_service_buffer, sdp_create_service_record_handle(), &hid_params); 457*762141afSMatthias Ringwald btstack_assert(de_get_len( hid_service_buffer) <= sizeof(hid_service_buffer)); 4582fa56ea6SMatthias Ringwald sdp_register_service(hid_service_buffer); 4592fa56ea6SMatthias Ringwald 46047f85edbSMatthias Ringwald // See https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers if you don't have a USB Vendor ID and need a Bluetooth Vendor ID 46147f85edbSMatthias Ringwald // device info: BlueKitchen GmbH, product 1, version 1 462*762141afSMatthias Ringwald device_id_create_sdp_record(device_id_sdp_service_buffer, sdp_create_service_record_handle(), DEVICE_ID_VENDOR_ID_SOURCE_BLUETOOTH, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1, 1); 463*762141afSMatthias Ringwald btstack_assert(de_get_len( device_id_sdp_service_buffer) <= sizeof(device_id_sdp_service_buffer)); 46447f85edbSMatthias Ringwald sdp_register_service(device_id_sdp_service_buffer); 46547f85edbSMatthias Ringwald 4668eb8d463SMatthias Ringwald // HID Device 467912af117SMatthias Ringwald hid_device_init(hid_boot_device, sizeof(hid_descriptor_keyboard), hid_descriptor_keyboard); 468a4fe6467SMatthias Ringwald 469a4fe6467SMatthias Ringwald // register for HCI events 470a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 471a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 472a4fe6467SMatthias Ringwald 473a4fe6467SMatthias Ringwald // register for HID events 4748eb8d463SMatthias Ringwald hid_device_register_packet_handler(&packet_handler); 4758eb8d463SMatthias Ringwald 4767ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 4774e43ca9fSMatthias Ringwald sscanf_bd_addr(device_addr_string, device_addr); 4782fa56ea6SMatthias Ringwald btstack_stdin_setup(stdin_process); 4792fa56ea6SMatthias Ringwald #endif 4801884c638SMatthias Ringwald 4811884c638SMatthias Ringwald btstack_ring_buffer_init(&send_buffer, send_buffer_storage, sizeof(send_buffer_storage)); 4821884c638SMatthias Ringwald 4832fa56ea6SMatthias Ringwald // turn on! 4842fa56ea6SMatthias Ringwald hci_power_control(HCI_POWER_ON); 4852fa56ea6SMatthias Ringwald return 0; 4862fa56ea6SMatthias Ringwald } 4872fa56ea6SMatthias Ringwald /* LISTING_END */ 4882fa56ea6SMatthias Ringwald /* EXAMPLE_END */ 489