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 65*1884c638SMatthias Ringwald // timing of keypresses 66*1884c638SMatthias Ringwald #define TYPING_KEYDOWN_MS 20 67*1884c638SMatthias Ringwald #define TYPING_DELAY_MS 20 68*1884c638SMatthias 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 88912af117SMatthias Ringwald 0x05, 0x07, // Usage Page (Key Codes) 892fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 902fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 912fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 922fa56ea6SMatthias Ringwald 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 932fa56ea6SMatthias Ringwald 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI) 942fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 952fa56ea6SMatthias Ringwald 0x25, 0x01, // Logical Maximum (1) 962fa56ea6SMatthias Ringwald 0x81, 0x02, // Input (Data, Variable, Absolute) 972fa56ea6SMatthias Ringwald 98912af117SMatthias Ringwald // Reserved byte (input) 992fa56ea6SMatthias Ringwald 1002fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 1012fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 1022fa56ea6SMatthias Ringwald 0x81, 0x03, // Input (Constant, Variable, Absolute) 1032fa56ea6SMatthias Ringwald 104912af117SMatthias Ringwald // LED report + padding (output) 1052fa56ea6SMatthias Ringwald 1062fa56ea6SMatthias Ringwald 0x95, 0x05, // Report Count (5) 1072fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 1082fa56ea6SMatthias Ringwald 0x05, 0x08, // Usage Page (LEDs) 1092fa56ea6SMatthias Ringwald 0x19, 0x01, // Usage Minimum (Num Lock) 1102fa56ea6SMatthias Ringwald 0x29, 0x05, // Usage Maxium (Kana) 1112fa56ea6SMatthias Ringwald 0x91, 0x02, // Output (Data, Variable, Absolute) 1122fa56ea6SMatthias Ringwald 1132fa56ea6SMatthias Ringwald 0x95, 0x01, // Report Count (1) 1142fa56ea6SMatthias Ringwald 0x75, 0x03, // Report Size (3) 1152fa56ea6SMatthias Ringwald 0x91, 0x03, // Output (Constant, Variable, Absolute) 1162fa56ea6SMatthias Ringwald 117912af117SMatthias Ringwald // Keycodes (input) 1182fa56ea6SMatthias Ringwald 1192fa56ea6SMatthias Ringwald 0x95, 0x06, // Report Count (6) 1202fa56ea6SMatthias Ringwald 0x75, 0x08, // Report Size (8) 1212fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 1222fa56ea6SMatthias Ringwald 0x25, 0xff, // Logical Maximum (1) 1232fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 1242fa56ea6SMatthias Ringwald 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 1252fa56ea6SMatthias Ringwald 0x29, 0xff, // Usage Maxium (Reserved) 1262fa56ea6SMatthias Ringwald 0x81, 0x00, // Input (Data, Array) 1272fa56ea6SMatthias Ringwald 1282fa56ea6SMatthias Ringwald 0xc0, // End collection 1292fa56ea6SMatthias Ringwald }; 1302fa56ea6SMatthias Ringwald 131ca44a803SMatthias Ringwald // 132ca44a803SMatthias Ringwald #define CHAR_ILLEGAL 0xff 133ca44a803SMatthias Ringwald #define CHAR_RETURN '\n' 134ca44a803SMatthias Ringwald #define CHAR_ESCAPE 27 135ca44a803SMatthias Ringwald #define CHAR_TAB '\t' 136ca44a803SMatthias Ringwald #define CHAR_BACKSPACE 0x7f 137ca44a803SMatthias Ringwald 138ca44a803SMatthias Ringwald // Simplified US Keyboard with Shift modifier 139ca44a803SMatthias Ringwald 140ca44a803SMatthias Ringwald /** 141ca44a803SMatthias Ringwald * English (US) 142ca44a803SMatthias Ringwald */ 143ca44a803SMatthias Ringwald static const uint8_t keytable_us_none [] = { 144ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 145ca44a803SMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 146ca44a803SMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 147ca44a803SMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 148ca44a803SMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 149ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 150ca44a803SMatthias Ringwald '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 151ca44a803SMatthias Ringwald '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 152ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 153ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 154ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 155ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 156ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 157ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 158ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 159ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 160ca44a803SMatthias Ringwald }; 161ca44a803SMatthias Ringwald 162ca44a803SMatthias Ringwald static const uint8_t keytable_us_shift[] = { 163ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 164ca44a803SMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 165ca44a803SMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 166ca44a803SMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 167ca44a803SMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 168ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 169ca44a803SMatthias Ringwald '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 170ca44a803SMatthias Ringwald '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 171ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 172ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 173ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 174ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 175ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 176ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 177ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 178ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 179ca44a803SMatthias Ringwald }; 180ca44a803SMatthias Ringwald 1814e43ca9fSMatthias Ringwald // STATE 1824e43ca9fSMatthias Ringwald 183574d351dSMatthias Ringwald static uint8_t hid_service_buffer[300]; 1844e43ca9fSMatthias Ringwald static uint8_t device_id_sdp_service_buffer[100]; 1854e43ca9fSMatthias Ringwald static const char hid_device_name[] = "BTstack HID Keyboard"; 1864e43ca9fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 1874e43ca9fSMatthias Ringwald static uint16_t hid_cid; 188b274c7b3SMilanka Ringwald static uint8_t hid_boot_device = 0; 1894e43ca9fSMatthias Ringwald 190*1884c638SMatthias Ringwald // HID Report sending 191*1884c638SMatthias Ringwald static uint8_t send_buffer_storage[16]; 192*1884c638SMatthias Ringwald static btstack_ring_buffer_t send_buffer; 193*1884c638SMatthias Ringwald static btstack_timer_source_t send_timer; 194*1884c638SMatthias Ringwald static uint8_t send_modifier; 195*1884c638SMatthias Ringwald static uint8_t send_keycode; 196*1884c638SMatthias Ringwald static bool send_active; 197*1884c638SMatthias Ringwald 1984e43ca9fSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 199*1884c638SMatthias Ringwald static bd_addr_t device_addr; 2004e43ca9fSMatthias Ringwald static const char * device_addr_string = "BC:EC:5D:E6:15:03"; 2014e43ca9fSMatthias Ringwald #endif 2024e43ca9fSMatthias Ringwald 2034e43ca9fSMatthias Ringwald static enum { 2044e43ca9fSMatthias Ringwald APP_BOOTING, 2054e43ca9fSMatthias Ringwald APP_NOT_CONNECTED, 2064e43ca9fSMatthias Ringwald APP_CONNECTING, 2074e43ca9fSMatthias Ringwald APP_CONNECTED 2084e43ca9fSMatthias Ringwald } app_state = APP_BOOTING; 2094e43ca9fSMatthias Ringwald 210ca44a803SMatthias Ringwald // HID Keyboard lookup 211*1884c638SMatthias Ringwald static bool lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){ 212ca44a803SMatthias Ringwald int i; 213ca44a803SMatthias Ringwald for (i=0;i<size;i++){ 214ca44a803SMatthias Ringwald if (table[i] != character) continue; 215ca44a803SMatthias Ringwald *keycode = i; 216*1884c638SMatthias Ringwald return true; 217ca44a803SMatthias Ringwald } 218*1884c638SMatthias Ringwald return false; 219ca44a803SMatthias Ringwald } 220ca44a803SMatthias Ringwald 221*1884c638SMatthias Ringwald static bool keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){ 222*1884c638SMatthias Ringwald bool found; 223ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode); 224ca44a803SMatthias Ringwald if (found) { 225ca44a803SMatthias Ringwald *modifier = 0; // none 226*1884c638SMatthias Ringwald return true; 227ca44a803SMatthias Ringwald } 228ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode); 229ca44a803SMatthias Ringwald if (found) { 230ca44a803SMatthias Ringwald *modifier = 2; // shift 231*1884c638SMatthias Ringwald return true; 232ca44a803SMatthias Ringwald } 233*1884c638SMatthias Ringwald return false; 2344cf72855SMatthias Ringwald } 2354cf72855SMatthias Ringwald 2364cf72855SMatthias Ringwald static void send_report(int modifier, int keycode){ 237912af117SMatthias Ringwald uint8_t report[] = { 0xa1, REPORT_ID, modifier, 0, keycode, 0, 0, 0, 0, 0}; 2388eb8d463SMatthias Ringwald hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report)); 2394cf72855SMatthias Ringwald } 2404cf72855SMatthias Ringwald 241*1884c638SMatthias Ringwald static void trigger_key_up(btstack_timer_source_t * ts){ 242*1884c638SMatthias Ringwald UNUSED(ts); 243*1884c638SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 244*1884c638SMatthias Ringwald } 245*1884c638SMatthias Ringwald 246*1884c638SMatthias Ringwald static void send_next(btstack_timer_source_t * ts) { 247*1884c638SMatthias Ringwald // get next key from buffer 248*1884c638SMatthias Ringwald uint8_t character; 249*1884c638SMatthias Ringwald uint32_t num_bytes_read = 0; 250*1884c638SMatthias Ringwald btstack_ring_buffer_read(&send_buffer, &character, 1, &num_bytes_read); 251*1884c638SMatthias Ringwald if (num_bytes_read == 0) { 252*1884c638SMatthias Ringwald // buffer empty, nothing to send 253*1884c638SMatthias Ringwald send_active = false; 254*1884c638SMatthias Ringwald } else { 255*1884c638SMatthias Ringwald send_active = true; 256*1884c638SMatthias Ringwald // lookup keycode and modifier using US layout 257*1884c638SMatthias Ringwald bool found = keycode_and_modifer_us_for_character(character, &send_keycode, &send_modifier); 258*1884c638SMatthias Ringwald if (found) { 259*1884c638SMatthias Ringwald // request can send now 260*1884c638SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 261*1884c638SMatthias Ringwald } else { 262*1884c638SMatthias Ringwald // restart timer for next character 263*1884c638SMatthias Ringwald btstack_run_loop_set_timer(ts, TYPING_DELAY_MS); 264*1884c638SMatthias Ringwald btstack_run_loop_add_timer(ts); 265*1884c638SMatthias Ringwald } 266*1884c638SMatthias Ringwald } 267*1884c638SMatthias Ringwald } 268*1884c638SMatthias Ringwald 269*1884c638SMatthias Ringwald static void queue_character(char character){ 270*1884c638SMatthias Ringwald btstack_ring_buffer_write(&send_buffer, (uint8_t *) &character, 1); 271*1884c638SMatthias Ringwald if (send_active == false) { 272*1884c638SMatthias Ringwald send_next(&send_timer); 273*1884c638SMatthias Ringwald } 274*1884c638SMatthias Ringwald } 275*1884c638SMatthias Ringwald 2768eb8d463SMatthias Ringwald // Demo Application 277ca44a803SMatthias Ringwald 2787ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 2792fa56ea6SMatthias Ringwald 2808eb8d463SMatthias Ringwald // On systems with STDIN, we can directly type on the console 2812fa56ea6SMatthias Ringwald 28295a8ee01SMatthias Ringwald static void stdin_process(char character){ 2834e43ca9fSMatthias Ringwald switch (app_state){ 2844e43ca9fSMatthias Ringwald case APP_BOOTING: 2854e43ca9fSMatthias Ringwald case APP_CONNECTING: 2864e43ca9fSMatthias Ringwald // ignore 2874e43ca9fSMatthias Ringwald break; 2884e43ca9fSMatthias Ringwald case APP_CONNECTED: 289*1884c638SMatthias Ringwald // add char to send buffer 290*1884c638SMatthias Ringwald queue_character(character); 2914e43ca9fSMatthias Ringwald break; 2924e43ca9fSMatthias Ringwald case APP_NOT_CONNECTED: 2934e43ca9fSMatthias Ringwald printf("Connecting to %s...\n", bd_addr_to_str(device_addr)); 2944e43ca9fSMatthias Ringwald hid_device_connect(device_addr, &hid_cid); 2954e43ca9fSMatthias Ringwald break; 2967bbeb3adSMilanka Ringwald default: 2977bbeb3adSMilanka Ringwald btstack_assert(false); 2987bbeb3adSMilanka Ringwald break; 2994e43ca9fSMatthias Ringwald } 3002fa56ea6SMatthias Ringwald } 3016518788aSMatthias Ringwald #else 3022fa56ea6SMatthias Ringwald 303*1884c638SMatthias Ringwald // On embedded systems, send constant demo text 3046518788aSMatthias Ringwald 305*1884c638SMatthias Ringwald #define TYPING_DEMO_PERIOD_MS 100 306*1884c638SMatthias Ringwald 3076518788aSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n"; 3086518788aSMatthias Ringwald static int demo_pos; 309*1884c638SMatthias Ringwald static btstack_timer_source_t demo_text_timer; 3106518788aSMatthias Ringwald 311*1884c638SMatthias Ringwald static void demo_text_timer_handler(btstack_timer_source_t * ts){ 312*1884c638SMatthias Ringwald UNUSED(ts); 3136518788aSMatthias Ringwald 314*1884c638SMatthias Ringwald // queue character 3156518788aSMatthias Ringwald uint8_t character = demo_text[demo_pos++]; 3166518788aSMatthias Ringwald if (demo_text[demo_pos] == 0){ 3176518788aSMatthias Ringwald demo_pos = 0; 3180316aa6fSMatthias Ringwald } 319*1884c638SMatthias Ringwald queue_character(character); 3200316aa6fSMatthias Ringwald 321*1884c638SMatthias Ringwald // set timer for next character 322*1884c638SMatthias Ringwald btstack_run_loop_set_timer_handler(&demo_text_timer, demo_text_timer_handler); 323*1884c638SMatthias Ringwald btstack_run_loop_set_timer(&demo_text_timer, TYPING_DEMO_PERIOD_MS); 324*1884c638SMatthias Ringwald btstack_run_loop_add_timer(&demo_text_timer); 3256518788aSMatthias Ringwald } 3266518788aSMatthias Ringwald 3276518788aSMatthias Ringwald #endif 3286518788aSMatthias Ringwald 3290316aa6fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ 3302fa56ea6SMatthias Ringwald UNUSED(channel); 3310316aa6fSMatthias Ringwald UNUSED(packet_size); 3324e43ca9fSMatthias Ringwald uint8_t status; 3332fa56ea6SMatthias Ringwald switch (packet_type){ 3342fa56ea6SMatthias Ringwald case HCI_EVENT_PACKET: 3356058cb0dSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 3364e43ca9fSMatthias Ringwald case BTSTACK_EVENT_STATE: 3374e43ca9fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 3384e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3394e43ca9fSMatthias Ringwald break; 3404e43ca9fSMatthias Ringwald 3410316aa6fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 3420316aa6fSMatthias Ringwald // ssp: inform about user confirmation request 3430316aa6fSMatthias Ringwald log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet)); 3440316aa6fSMatthias Ringwald log_info("SSP User Confirmation Auto accept\n"); 3450316aa6fSMatthias Ringwald break; 3460316aa6fSMatthias Ringwald 3478eb8d463SMatthias Ringwald case HCI_EVENT_HID_META: 3488eb8d463SMatthias Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 3498eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 3504e43ca9fSMatthias Ringwald status = hid_subevent_connection_opened_get_status(packet); 3516058cb0dSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 3524e43ca9fSMatthias Ringwald // outgoing connection failed 3534e43ca9fSMatthias Ringwald printf("Connection failed, status 0x%x\n", status); 3544e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3554e43ca9fSMatthias Ringwald hid_cid = 0; 3564e43ca9fSMatthias Ringwald return; 3574e43ca9fSMatthias Ringwald } 3584e43ca9fSMatthias Ringwald app_state = APP_CONNECTED; 3598eb8d463SMatthias Ringwald hid_cid = hid_subevent_connection_opened_get_hid_cid(packet); 3607ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3618eb8d463SMatthias Ringwald printf("HID Connected, please start typing...\n"); 3628eb8d463SMatthias Ringwald #else 3638eb8d463SMatthias Ringwald printf("HID Connected, sending demo text...\n"); 364*1884c638SMatthias Ringwald demo_text_timer_handler(NULL); 3656518788aSMatthias Ringwald #endif 3660316aa6fSMatthias Ringwald break; 3678eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 368*1884c638SMatthias Ringwald btstack_run_loop_remove_timer(&send_timer); 3690316aa6fSMatthias Ringwald printf("HID Disconnected\n"); 3704e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3718eb8d463SMatthias Ringwald hid_cid = 0; 3720316aa6fSMatthias Ringwald break; 3738eb8d463SMatthias Ringwald case HID_SUBEVENT_CAN_SEND_NOW: 3744cf72855SMatthias Ringwald if (send_keycode){ 3754cf72855SMatthias Ringwald send_report(send_modifier, send_keycode); 376*1884c638SMatthias Ringwald // schedule key up 3774cf72855SMatthias Ringwald send_keycode = 0; 3784cf72855SMatthias Ringwald send_modifier = 0; 379*1884c638SMatthias Ringwald btstack_run_loop_set_timer_handler(&send_timer, trigger_key_up); 380*1884c638SMatthias Ringwald btstack_run_loop_set_timer(&send_timer, TYPING_KEYDOWN_MS); 3814cf72855SMatthias Ringwald } else { 3824cf72855SMatthias Ringwald send_report(0, 0); 383*1884c638SMatthias Ringwald // schedule next key down 384*1884c638SMatthias Ringwald btstack_run_loop_set_timer_handler(&send_timer, send_next); 385*1884c638SMatthias Ringwald btstack_run_loop_set_timer(&send_timer, TYPING_DELAY_MS); 3864cf72855SMatthias Ringwald } 387*1884c638SMatthias Ringwald btstack_run_loop_add_timer(&send_timer); 3888eb8d463SMatthias Ringwald break; 3898eb8d463SMatthias Ringwald default: 3908eb8d463SMatthias Ringwald break; 3918eb8d463SMatthias Ringwald } 3928eb8d463SMatthias Ringwald break; 3932fa56ea6SMatthias Ringwald default: 3942fa56ea6SMatthias Ringwald break; 3952fa56ea6SMatthias Ringwald } 3962fa56ea6SMatthias Ringwald break; 3972fa56ea6SMatthias Ringwald default: 3982fa56ea6SMatthias Ringwald break; 3992fa56ea6SMatthias Ringwald } 4002fa56ea6SMatthias Ringwald } 4012fa56ea6SMatthias Ringwald 4022fa56ea6SMatthias Ringwald /* @section Main Application Setup 4032fa56ea6SMatthias Ringwald * 4042fa56ea6SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 4052fa56ea6SMatthias Ringwald * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. 4062fa56ea6SMatthias Ringwald * At the end the Bluetooth stack is started. 4072fa56ea6SMatthias Ringwald */ 4082fa56ea6SMatthias Ringwald 4092fa56ea6SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HID Device */ 4102fa56ea6SMatthias Ringwald 4112fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 4122fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 4132fa56ea6SMatthias Ringwald (void)argc; 4142fa56ea6SMatthias Ringwald (void)argv; 4152fa56ea6SMatthias Ringwald 41645a92abaSMatthias Ringwald // allow to get found by inquiry 4172fa56ea6SMatthias Ringwald gap_discoverable_control(1); 41845a92abaSMatthias Ringwald // use Limited Discoverable Mode; Peripheral; Keyboard as CoD 4192fa56ea6SMatthias Ringwald gap_set_class_of_device(0x2540); 42045a92abaSMatthias Ringwald // set local name to be identified - zeroes will be replaced by actual BD ADDR 4210c2b8870SMatthias Ringwald gap_set_local_name("HID Keyboard Demo 00:00:00:00:00:00"); 42245a92abaSMatthias Ringwald // allow for role switch in general and sniff mode 42345a92abaSMatthias Ringwald gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE ); 42445a92abaSMatthias Ringwald // allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it 42545a92abaSMatthias Ringwald gap_set_allow_role_switch(true); 4262fa56ea6SMatthias Ringwald 4272fa56ea6SMatthias Ringwald // L2CAP 4282fa56ea6SMatthias Ringwald l2cap_init(); 4292fa56ea6SMatthias Ringwald 4308c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE 4318c9bb29eSMatthias Ringwald // Initialize LE Security Manager. Needed for cross-transport key derivation 4328c9bb29eSMatthias Ringwald sm_init(); 4338c9bb29eSMatthias Ringwald #endif 4348c9bb29eSMatthias Ringwald 4352fa56ea6SMatthias Ringwald // SDP Server 4362fa56ea6SMatthias Ringwald sdp_init(); 4372fa56ea6SMatthias Ringwald memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); 4381ad99f3bSMilanka Ringwald 4391ad99f3bSMilanka Ringwald uint8_t hid_virtual_cable = 0; 4401ad99f3bSMilanka Ringwald uint8_t hid_remote_wake = 1; 4411ad99f3bSMilanka Ringwald uint8_t hid_reconnect_initiate = 1; 442ffe3f1a1SMilanka Ringwald uint8_t hid_normally_connectable = 1; 4431ad99f3bSMilanka Ringwald 44480d9d5d4SMilanka Ringwald hid_sdp_record_t hid_params = { 44580d9d5d4SMilanka Ringwald // hid sevice subclass 2540 Keyboard, hid counntry code 33 US 44680d9d5d4SMilanka Ringwald 0x2540, 33, 44780d9d5d4SMilanka Ringwald hid_virtual_cable, hid_remote_wake, 44880d9d5d4SMilanka Ringwald hid_reconnect_initiate, hid_normally_connectable, 44980d9d5d4SMilanka Ringwald hid_boot_device, 450574d351dSMatthias Ringwald host_max_latency, host_min_timeout, 451574d351dSMatthias Ringwald 3200, 452912af117SMatthias Ringwald hid_descriptor_keyboard, 453912af117SMatthias Ringwald sizeof(hid_descriptor_keyboard), 45480d9d5d4SMilanka Ringwald hid_device_name 45580d9d5d4SMilanka Ringwald }; 45680d9d5d4SMilanka Ringwald 45780d9d5d4SMilanka Ringwald hid_create_sdp_record(hid_service_buffer, 0x10001, &hid_params); 45880d9d5d4SMilanka Ringwald 45947f85edbSMatthias Ringwald printf("HID service record size: %u\n", de_get_len( hid_service_buffer)); 4602fa56ea6SMatthias Ringwald sdp_register_service(hid_service_buffer); 4612fa56ea6SMatthias Ringwald 46247f85edbSMatthias 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 46347f85edbSMatthias Ringwald // device info: BlueKitchen GmbH, product 1, version 1 46447f85edbSMatthias Ringwald device_id_create_sdp_record(device_id_sdp_service_buffer, 0x10003, DEVICE_ID_VENDOR_ID_SOURCE_BLUETOOTH, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1, 1); 46547f85edbSMatthias Ringwald printf("Device ID SDP service record size: %u\n", de_get_len((uint8_t*)device_id_sdp_service_buffer)); 46647f85edbSMatthias Ringwald sdp_register_service(device_id_sdp_service_buffer); 46747f85edbSMatthias Ringwald 4688eb8d463SMatthias Ringwald // HID Device 469912af117SMatthias Ringwald hid_device_init(hid_boot_device, sizeof(hid_descriptor_keyboard), hid_descriptor_keyboard); 470a4fe6467SMatthias Ringwald 471a4fe6467SMatthias Ringwald // register for HCI events 472a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 473a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 474a4fe6467SMatthias Ringwald 475a4fe6467SMatthias Ringwald // register for HID events 4768eb8d463SMatthias Ringwald hid_device_register_packet_handler(&packet_handler); 4778eb8d463SMatthias Ringwald 4787ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 4794e43ca9fSMatthias Ringwald sscanf_bd_addr(device_addr_string, device_addr); 4802fa56ea6SMatthias Ringwald btstack_stdin_setup(stdin_process); 4812fa56ea6SMatthias Ringwald #endif 482*1884c638SMatthias Ringwald 483*1884c638SMatthias Ringwald btstack_ring_buffer_init(&send_buffer, send_buffer_storage, sizeof(send_buffer_storage)); 484*1884c638SMatthias Ringwald 4852fa56ea6SMatthias Ringwald // turn on! 4862fa56ea6SMatthias Ringwald hci_power_control(HCI_POWER_ON); 4872fa56ea6SMatthias Ringwald return 0; 4882fa56ea6SMatthias Ringwald } 4892fa56ea6SMatthias Ringwald /* LISTING_END */ 4902fa56ea6SMatthias Ringwald /* EXAMPLE_END */ 491