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 23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24*2fca4dadSMilanka 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 65574d351dSMatthias Ringwald // When not set to 0xffff, sniff and sniff subrating are enabled 66574d351dSMatthias Ringwald static uint16_t host_max_latency = 1600; 67574d351dSMatthias Ringwald static uint16_t host_min_timeout = 3200; 68574d351dSMatthias Ringwald 69912af117SMatthias Ringwald #define REPORT_ID 0x01 70912af117SMatthias Ringwald 71912af117SMatthias Ringwald // close to USB HID Specification 1.1, Appendix B.1 72912af117SMatthias Ringwald const uint8_t hid_descriptor_keyboard[] = { 732fa56ea6SMatthias Ringwald 742fa56ea6SMatthias Ringwald 0x05, 0x01, // Usage Page (Generic Desktop) 752fa56ea6SMatthias Ringwald 0x09, 0x06, // Usage (Keyboard) 762fa56ea6SMatthias Ringwald 0xa1, 0x01, // Collection (Application) 772fa56ea6SMatthias Ringwald 78912af117SMatthias Ringwald // Report ID 792fa56ea6SMatthias Ringwald 80912af117SMatthias Ringwald 0x85, REPORT_ID, // Report ID 81912af117SMatthias Ringwald 82912af117SMatthias Ringwald // Modifier byte (input) 83912af117SMatthias Ringwald 84912af117SMatthias Ringwald 0x05, 0x07, // Usage Page (Key Codes) 852fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 862fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 872fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 882fa56ea6SMatthias Ringwald 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 892fa56ea6SMatthias Ringwald 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI) 902fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 912fa56ea6SMatthias Ringwald 0x25, 0x01, // Logical Maximum (1) 922fa56ea6SMatthias Ringwald 0x81, 0x02, // Input (Data, Variable, Absolute) 932fa56ea6SMatthias Ringwald 94912af117SMatthias Ringwald // Reserved byte (input) 952fa56ea6SMatthias Ringwald 962fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 972fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 982fa56ea6SMatthias Ringwald 0x81, 0x03, // Input (Constant, Variable, Absolute) 992fa56ea6SMatthias Ringwald 100912af117SMatthias Ringwald // LED report + padding (output) 1012fa56ea6SMatthias Ringwald 1022fa56ea6SMatthias Ringwald 0x95, 0x05, // Report Count (5) 1032fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 1042fa56ea6SMatthias Ringwald 0x05, 0x08, // Usage Page (LEDs) 1052fa56ea6SMatthias Ringwald 0x19, 0x01, // Usage Minimum (Num Lock) 1062fa56ea6SMatthias Ringwald 0x29, 0x05, // Usage Maxium (Kana) 1072fa56ea6SMatthias Ringwald 0x91, 0x02, // Output (Data, Variable, Absolute) 1082fa56ea6SMatthias Ringwald 1092fa56ea6SMatthias Ringwald 0x95, 0x01, // Report Count (1) 1102fa56ea6SMatthias Ringwald 0x75, 0x03, // Report Size (3) 1112fa56ea6SMatthias Ringwald 0x91, 0x03, // Output (Constant, Variable, Absolute) 1122fa56ea6SMatthias Ringwald 113912af117SMatthias Ringwald // Keycodes (input) 1142fa56ea6SMatthias Ringwald 1152fa56ea6SMatthias Ringwald 0x95, 0x06, // Report Count (6) 1162fa56ea6SMatthias Ringwald 0x75, 0x08, // Report Size (8) 1172fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 1182fa56ea6SMatthias Ringwald 0x25, 0xff, // Logical Maximum (1) 1192fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 1202fa56ea6SMatthias Ringwald 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 1212fa56ea6SMatthias Ringwald 0x29, 0xff, // Usage Maxium (Reserved) 1222fa56ea6SMatthias Ringwald 0x81, 0x00, // Input (Data, Array) 1232fa56ea6SMatthias Ringwald 1242fa56ea6SMatthias Ringwald 0xc0, // End collection 1252fa56ea6SMatthias Ringwald }; 1262fa56ea6SMatthias Ringwald 127ca44a803SMatthias Ringwald // 128ca44a803SMatthias Ringwald #define CHAR_ILLEGAL 0xff 129ca44a803SMatthias Ringwald #define CHAR_RETURN '\n' 130ca44a803SMatthias Ringwald #define CHAR_ESCAPE 27 131ca44a803SMatthias Ringwald #define CHAR_TAB '\t' 132ca44a803SMatthias Ringwald #define CHAR_BACKSPACE 0x7f 133ca44a803SMatthias Ringwald 134ca44a803SMatthias Ringwald // Simplified US Keyboard with Shift modifier 135ca44a803SMatthias Ringwald 136ca44a803SMatthias Ringwald /** 137ca44a803SMatthias Ringwald * English (US) 138ca44a803SMatthias Ringwald */ 139ca44a803SMatthias Ringwald static const uint8_t keytable_us_none [] = { 140ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 141ca44a803SMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 142ca44a803SMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 143ca44a803SMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 144ca44a803SMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 145ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 146ca44a803SMatthias Ringwald '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 147ca44a803SMatthias Ringwald '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 148ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 149ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 150ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 151ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 152ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 153ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 154ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 155ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 156ca44a803SMatthias Ringwald }; 157ca44a803SMatthias Ringwald 158ca44a803SMatthias Ringwald static const uint8_t keytable_us_shift[] = { 159ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 160ca44a803SMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 161ca44a803SMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 162ca44a803SMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 163ca44a803SMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 164ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 165ca44a803SMatthias Ringwald '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 166ca44a803SMatthias Ringwald '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 167ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 168ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 169ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 170ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 171ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 172ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 173ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 174ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 175ca44a803SMatthias Ringwald }; 176ca44a803SMatthias Ringwald 1774e43ca9fSMatthias Ringwald // STATE 1784e43ca9fSMatthias Ringwald 179574d351dSMatthias Ringwald static uint8_t hid_service_buffer[300]; 1804e43ca9fSMatthias Ringwald static uint8_t device_id_sdp_service_buffer[100]; 1814e43ca9fSMatthias Ringwald static const char hid_device_name[] = "BTstack HID Keyboard"; 1824e43ca9fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 1834e43ca9fSMatthias Ringwald static uint16_t hid_cid; 1844e43ca9fSMatthias Ringwald static bd_addr_t device_addr; 185b274c7b3SMilanka Ringwald static uint8_t hid_boot_device = 0; 1864e43ca9fSMatthias Ringwald 1874e43ca9fSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 1884e43ca9fSMatthias Ringwald static const char * device_addr_string = "BC:EC:5D:E6:15:03"; 1894e43ca9fSMatthias Ringwald #endif 1904e43ca9fSMatthias Ringwald 1914e43ca9fSMatthias Ringwald static enum { 1924e43ca9fSMatthias Ringwald APP_BOOTING, 1934e43ca9fSMatthias Ringwald APP_NOT_CONNECTED, 1944e43ca9fSMatthias Ringwald APP_CONNECTING, 1954e43ca9fSMatthias Ringwald APP_CONNECTED 1964e43ca9fSMatthias Ringwald } app_state = APP_BOOTING; 1974e43ca9fSMatthias Ringwald 198ca44a803SMatthias Ringwald // HID Keyboard lookup 199ca44a803SMatthias Ringwald static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){ 200ca44a803SMatthias Ringwald int i; 201ca44a803SMatthias Ringwald for (i=0;i<size;i++){ 202ca44a803SMatthias Ringwald if (table[i] != character) continue; 203ca44a803SMatthias Ringwald *keycode = i; 204ca44a803SMatthias Ringwald return 1; 205ca44a803SMatthias Ringwald } 206ca44a803SMatthias Ringwald return 0; 207ca44a803SMatthias Ringwald } 208ca44a803SMatthias Ringwald 209ca44a803SMatthias Ringwald static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){ 210ca44a803SMatthias Ringwald int found; 211ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode); 212ca44a803SMatthias Ringwald if (found) { 213ca44a803SMatthias Ringwald *modifier = 0; // none 214ca44a803SMatthias Ringwald return 1; 215ca44a803SMatthias Ringwald } 216ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode); 217ca44a803SMatthias Ringwald if (found) { 218ca44a803SMatthias Ringwald *modifier = 2; // shift 219ca44a803SMatthias Ringwald return 1; 220ca44a803SMatthias Ringwald } 221ca44a803SMatthias Ringwald return 0; 222ca44a803SMatthias Ringwald } 223ca44a803SMatthias Ringwald 224ca44a803SMatthias Ringwald // HID Report sending 2254cf72855SMatthias Ringwald static int send_keycode; 2264cf72855SMatthias Ringwald static int send_modifier; 2274cf72855SMatthias Ringwald 2284cf72855SMatthias Ringwald static void send_key(int modifier, int keycode){ 2294cf72855SMatthias Ringwald send_keycode = keycode; 2304cf72855SMatthias Ringwald send_modifier = modifier; 2318eb8d463SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 2324cf72855SMatthias Ringwald } 2334cf72855SMatthias Ringwald 2344cf72855SMatthias Ringwald static void send_report(int modifier, int keycode){ 235912af117SMatthias Ringwald uint8_t report[] = { 0xa1, REPORT_ID, modifier, 0, keycode, 0, 0, 0, 0, 0}; 2368eb8d463SMatthias Ringwald hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report)); 2374cf72855SMatthias Ringwald } 2384cf72855SMatthias Ringwald 2398eb8d463SMatthias Ringwald // Demo Application 240ca44a803SMatthias Ringwald 2417ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 2422fa56ea6SMatthias Ringwald 2438eb8d463SMatthias Ringwald // On systems with STDIN, we can directly type on the console 2442fa56ea6SMatthias Ringwald 24595a8ee01SMatthias Ringwald static void stdin_process(char character){ 246ca44a803SMatthias Ringwald uint8_t modifier; 247ca44a803SMatthias Ringwald uint8_t keycode; 2484e43ca9fSMatthias Ringwald int found; 2494e43ca9fSMatthias Ringwald 2504e43ca9fSMatthias Ringwald switch (app_state){ 2514e43ca9fSMatthias Ringwald case APP_BOOTING: 2524e43ca9fSMatthias Ringwald case APP_CONNECTING: 2534e43ca9fSMatthias Ringwald // ignore 2544e43ca9fSMatthias Ringwald break; 2554e43ca9fSMatthias Ringwald 2564e43ca9fSMatthias Ringwald case APP_CONNECTED: 2574e43ca9fSMatthias Ringwald // send keyu 2584e43ca9fSMatthias Ringwald found = keycode_and_modifer_us_for_character(character, &keycode, &modifier); 259ca44a803SMatthias Ringwald if (found){ 260ca44a803SMatthias Ringwald send_key(modifier, keycode); 2614cf72855SMatthias Ringwald return; 2622fa56ea6SMatthias Ringwald } 2634e43ca9fSMatthias Ringwald break; 2644e43ca9fSMatthias Ringwald case APP_NOT_CONNECTED: 2654e43ca9fSMatthias Ringwald printf("Connecting to %s...\n", bd_addr_to_str(device_addr)); 2664e43ca9fSMatthias Ringwald hid_device_connect(device_addr, &hid_cid); 2674e43ca9fSMatthias Ringwald break; 2687bbeb3adSMilanka Ringwald default: 2697bbeb3adSMilanka Ringwald btstack_assert(false); 2707bbeb3adSMilanka Ringwald break; 2714e43ca9fSMatthias Ringwald } 2722fa56ea6SMatthias Ringwald } 2736518788aSMatthias Ringwald #else 2742fa56ea6SMatthias Ringwald 2756518788aSMatthias Ringwald // On embedded systems, send constant demo text with fixed period 2766518788aSMatthias Ringwald 2776518788aSMatthias Ringwald #define TYPING_PERIOD_MS 100 2786518788aSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n"; 2796518788aSMatthias Ringwald 2806518788aSMatthias Ringwald static int demo_pos; 2816518788aSMatthias Ringwald static btstack_timer_source_t typing_timer; 2826518788aSMatthias Ringwald 2836518788aSMatthias Ringwald static void typing_timer_handler(btstack_timer_source_t * ts){ 2846518788aSMatthias Ringwald 2856518788aSMatthias Ringwald // abort if not connected 2868eb8d463SMatthias Ringwald if (!hid_cid) return; 2876518788aSMatthias Ringwald 2886518788aSMatthias Ringwald // get next character 2896518788aSMatthias Ringwald uint8_t character = demo_text[demo_pos++]; 2906518788aSMatthias Ringwald if (demo_text[demo_pos] == 0){ 2916518788aSMatthias Ringwald demo_pos = 0; 2920316aa6fSMatthias Ringwald } 2930316aa6fSMatthias Ringwald 2946518788aSMatthias Ringwald // get keycodeand send 2956518788aSMatthias Ringwald uint8_t modifier; 2966518788aSMatthias Ringwald uint8_t keycode; 2976518788aSMatthias Ringwald int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier); 2986518788aSMatthias Ringwald if (found){ 2996518788aSMatthias Ringwald send_key(modifier, keycode); 3006518788aSMatthias Ringwald } 3016518788aSMatthias Ringwald 3026518788aSMatthias Ringwald // set next timer 3036518788aSMatthias Ringwald btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS); 3046518788aSMatthias Ringwald btstack_run_loop_add_timer(ts); 3056518788aSMatthias Ringwald } 3066518788aSMatthias Ringwald 3078eb8d463SMatthias Ringwald static void hid_embedded_start_typing(void){ 3086518788aSMatthias Ringwald demo_pos = 0; 3096518788aSMatthias Ringwald // set one-shot timer 3106518788aSMatthias Ringwald typing_timer.process = &typing_timer_handler; 3116518788aSMatthias Ringwald btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS); 3126518788aSMatthias Ringwald btstack_run_loop_add_timer(&typing_timer); 3136518788aSMatthias Ringwald } 3146518788aSMatthias Ringwald 3156518788aSMatthias Ringwald #endif 3166518788aSMatthias Ringwald 3170316aa6fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ 3182fa56ea6SMatthias Ringwald UNUSED(channel); 3190316aa6fSMatthias Ringwald UNUSED(packet_size); 3204e43ca9fSMatthias Ringwald uint8_t status; 3212fa56ea6SMatthias Ringwald switch (packet_type){ 3222fa56ea6SMatthias Ringwald case HCI_EVENT_PACKET: 3236058cb0dSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 3244e43ca9fSMatthias Ringwald case BTSTACK_EVENT_STATE: 3254e43ca9fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 3264e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3274e43ca9fSMatthias Ringwald break; 3284e43ca9fSMatthias Ringwald 3290316aa6fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 3300316aa6fSMatthias Ringwald // ssp: inform about user confirmation request 3310316aa6fSMatthias Ringwald log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet)); 3320316aa6fSMatthias Ringwald log_info("SSP User Confirmation Auto accept\n"); 3330316aa6fSMatthias Ringwald break; 3340316aa6fSMatthias Ringwald 3358eb8d463SMatthias Ringwald case HCI_EVENT_HID_META: 3368eb8d463SMatthias Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 3378eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 3384e43ca9fSMatthias Ringwald status = hid_subevent_connection_opened_get_status(packet); 3396058cb0dSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 3404e43ca9fSMatthias Ringwald // outgoing connection failed 3414e43ca9fSMatthias Ringwald printf("Connection failed, status 0x%x\n", status); 3424e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3434e43ca9fSMatthias Ringwald hid_cid = 0; 3444e43ca9fSMatthias Ringwald return; 3454e43ca9fSMatthias Ringwald } 3464e43ca9fSMatthias Ringwald app_state = APP_CONNECTED; 3478eb8d463SMatthias Ringwald hid_cid = hid_subevent_connection_opened_get_hid_cid(packet); 3487ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3498eb8d463SMatthias Ringwald printf("HID Connected, please start typing...\n"); 3508eb8d463SMatthias Ringwald #else 3518eb8d463SMatthias Ringwald printf("HID Connected, sending demo text...\n"); 3528eb8d463SMatthias Ringwald hid_embedded_start_typing(); 3536518788aSMatthias Ringwald #endif 3540316aa6fSMatthias Ringwald break; 3558eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 3560316aa6fSMatthias Ringwald printf("HID Disconnected\n"); 3574e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3588eb8d463SMatthias Ringwald hid_cid = 0; 3590316aa6fSMatthias Ringwald break; 3608eb8d463SMatthias Ringwald case HID_SUBEVENT_CAN_SEND_NOW: 3614cf72855SMatthias Ringwald if (send_keycode){ 3624cf72855SMatthias Ringwald send_report(send_modifier, send_keycode); 3634cf72855SMatthias Ringwald send_keycode = 0; 3644cf72855SMatthias Ringwald send_modifier = 0; 3658eb8d463SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 3664cf72855SMatthias Ringwald } else { 3674cf72855SMatthias Ringwald send_report(0, 0); 3684cf72855SMatthias Ringwald } 3698eb8d463SMatthias Ringwald break; 3708eb8d463SMatthias Ringwald default: 3718eb8d463SMatthias Ringwald break; 3728eb8d463SMatthias Ringwald } 3738eb8d463SMatthias Ringwald break; 3742fa56ea6SMatthias Ringwald default: 3752fa56ea6SMatthias Ringwald break; 3762fa56ea6SMatthias Ringwald } 3772fa56ea6SMatthias Ringwald break; 3782fa56ea6SMatthias Ringwald default: 3792fa56ea6SMatthias Ringwald break; 3802fa56ea6SMatthias Ringwald } 3812fa56ea6SMatthias Ringwald } 3822fa56ea6SMatthias Ringwald 3832fa56ea6SMatthias Ringwald /* @section Main Application Setup 3842fa56ea6SMatthias Ringwald * 3852fa56ea6SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 3862fa56ea6SMatthias Ringwald * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. 3872fa56ea6SMatthias Ringwald * At the end the Bluetooth stack is started. 3882fa56ea6SMatthias Ringwald */ 3892fa56ea6SMatthias Ringwald 3902fa56ea6SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HID Device */ 3912fa56ea6SMatthias Ringwald 3922fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 3932fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 3942fa56ea6SMatthias Ringwald (void)argc; 3952fa56ea6SMatthias Ringwald (void)argv; 3962fa56ea6SMatthias Ringwald 39745a92abaSMatthias Ringwald // allow to get found by inquiry 3982fa56ea6SMatthias Ringwald gap_discoverable_control(1); 39945a92abaSMatthias Ringwald // use Limited Discoverable Mode; Peripheral; Keyboard as CoD 4002fa56ea6SMatthias Ringwald gap_set_class_of_device(0x2540); 40145a92abaSMatthias Ringwald // set local name to be identified - zeroes will be replaced by actual BD ADDR 4020c2b8870SMatthias Ringwald gap_set_local_name("HID Keyboard Demo 00:00:00:00:00:00"); 40345a92abaSMatthias Ringwald // allow for role switch in general and sniff mode 40445a92abaSMatthias Ringwald gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE ); 40545a92abaSMatthias Ringwald // allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it 40645a92abaSMatthias Ringwald gap_set_allow_role_switch(true); 4072fa56ea6SMatthias Ringwald 4082fa56ea6SMatthias Ringwald // L2CAP 4092fa56ea6SMatthias Ringwald l2cap_init(); 4102fa56ea6SMatthias Ringwald 4112fa56ea6SMatthias Ringwald // SDP Server 4122fa56ea6SMatthias Ringwald sdp_init(); 4132fa56ea6SMatthias Ringwald memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); 4141ad99f3bSMilanka Ringwald 4151ad99f3bSMilanka Ringwald uint8_t hid_virtual_cable = 0; 4161ad99f3bSMilanka Ringwald uint8_t hid_remote_wake = 1; 4171ad99f3bSMilanka Ringwald uint8_t hid_reconnect_initiate = 1; 418ffe3f1a1SMilanka Ringwald uint8_t hid_normally_connectable = 1; 4191ad99f3bSMilanka Ringwald 42080d9d5d4SMilanka Ringwald hid_sdp_record_t hid_params = { 42180d9d5d4SMilanka Ringwald // hid sevice subclass 2540 Keyboard, hid counntry code 33 US 42280d9d5d4SMilanka Ringwald 0x2540, 33, 42380d9d5d4SMilanka Ringwald hid_virtual_cable, hid_remote_wake, 42480d9d5d4SMilanka Ringwald hid_reconnect_initiate, hid_normally_connectable, 42580d9d5d4SMilanka Ringwald hid_boot_device, 426574d351dSMatthias Ringwald host_max_latency, host_min_timeout, 427574d351dSMatthias Ringwald 3200, 428912af117SMatthias Ringwald hid_descriptor_keyboard, 429912af117SMatthias Ringwald sizeof(hid_descriptor_keyboard), 43080d9d5d4SMilanka Ringwald hid_device_name 43180d9d5d4SMilanka Ringwald }; 43280d9d5d4SMilanka Ringwald 43380d9d5d4SMilanka Ringwald hid_create_sdp_record(hid_service_buffer, 0x10001, &hid_params); 43480d9d5d4SMilanka Ringwald 43547f85edbSMatthias Ringwald printf("HID service record size: %u\n", de_get_len( hid_service_buffer)); 4362fa56ea6SMatthias Ringwald sdp_register_service(hid_service_buffer); 4372fa56ea6SMatthias Ringwald 43847f85edbSMatthias 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 43947f85edbSMatthias Ringwald // device info: BlueKitchen GmbH, product 1, version 1 44047f85edbSMatthias 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); 44147f85edbSMatthias Ringwald printf("Device ID SDP service record size: %u\n", de_get_len((uint8_t*)device_id_sdp_service_buffer)); 44247f85edbSMatthias Ringwald sdp_register_service(device_id_sdp_service_buffer); 44347f85edbSMatthias Ringwald 4448eb8d463SMatthias Ringwald // HID Device 445912af117SMatthias Ringwald hid_device_init(hid_boot_device, sizeof(hid_descriptor_keyboard), hid_descriptor_keyboard); 446a4fe6467SMatthias Ringwald 447a4fe6467SMatthias Ringwald // register for HCI events 448a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 449a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 450a4fe6467SMatthias Ringwald 451a4fe6467SMatthias Ringwald // register for HID events 4528eb8d463SMatthias Ringwald hid_device_register_packet_handler(&packet_handler); 4538eb8d463SMatthias Ringwald 4547ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 4554e43ca9fSMatthias Ringwald sscanf_bd_addr(device_addr_string, device_addr); 4562fa56ea6SMatthias Ringwald btstack_stdin_setup(stdin_process); 4572fa56ea6SMatthias Ringwald #endif 4582fa56ea6SMatthias Ringwald // turn on! 4592fa56ea6SMatthias Ringwald hci_power_control(HCI_POWER_ON); 4602fa56ea6SMatthias Ringwald return 0; 4612fa56ea6SMatthias Ringwald } 4622fa56ea6SMatthias Ringwald /* LISTING_END */ 4632fa56ea6SMatthias Ringwald /* EXAMPLE_END */ 464