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 232fa56ea6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 242fa56ea6SMatthias Ringwald * RINGWALD 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 3815de8206SMilanka Ringwald #define __BTSTACK_FILE__ "hid_keyboard_demo.c" 392fa56ea6SMatthias Ringwald 402fa56ea6SMatthias Ringwald // ***************************************************************************** 4115de8206SMilanka Ringwald /* EXAMPLE_START(hid_keyboard_demo): HID Keyboard (Server) Demo 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 4615de8206SMilanka Ringwald * 4715de8206SMilanka Ringwald * @text Status: Basic implementation. HID Request from Host are not answered yet. Works with iOS. 482fa56ea6SMatthias Ringwald */ 492fa56ea6SMatthias Ringwald // ***************************************************************************** 502fa56ea6SMatthias Ringwald 512fa56ea6SMatthias Ringwald 522fa56ea6SMatthias Ringwald #include <stdint.h> 532fa56ea6SMatthias Ringwald #include <stdio.h> 542fa56ea6SMatthias Ringwald #include <stdlib.h> 552fa56ea6SMatthias Ringwald #include <string.h> 560316aa6fSMatthias Ringwald #include <inttypes.h> 572fa56ea6SMatthias Ringwald 582fa56ea6SMatthias Ringwald #include "btstack.h" 596518788aSMatthias Ringwald 607ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 617ea7688aSMatthias Ringwald #include "btstack_stdin.h" 622fa56ea6SMatthias Ringwald #endif 632fa56ea6SMatthias Ringwald 648eb8d463SMatthias Ringwald // to enable demo text on POSIX systems 657ea7688aSMatthias Ringwald // #undef HAVE_BTSTACK_STDIN 662fa56ea6SMatthias Ringwald 672fa56ea6SMatthias Ringwald // from USB HID Specification 1.1, Appendix B.1 682fa56ea6SMatthias Ringwald const uint8_t hid_descriptor_keyboard_boot_mode[] = { 692fa56ea6SMatthias Ringwald 702fa56ea6SMatthias Ringwald 0x05, 0x01, // Usage Page (Generic Desktop) 712fa56ea6SMatthias Ringwald 0x09, 0x06, // Usage (Keyboard) 722fa56ea6SMatthias Ringwald 0xa1, 0x01, // Collection (Application) 732fa56ea6SMatthias Ringwald 742fa56ea6SMatthias Ringwald // Modifier byte 752fa56ea6SMatthias Ringwald 762fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 772fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 782fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 792fa56ea6SMatthias Ringwald 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 802fa56ea6SMatthias Ringwald 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI) 812fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 822fa56ea6SMatthias Ringwald 0x25, 0x01, // Logical Maximum (1) 832fa56ea6SMatthias Ringwald 0x81, 0x02, // Input (Data, Variable, Absolute) 842fa56ea6SMatthias Ringwald 852fa56ea6SMatthias Ringwald // Reserved byte 862fa56ea6SMatthias Ringwald 872fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 882fa56ea6SMatthias Ringwald 0x95, 0x08, // Report Count (8) 892fa56ea6SMatthias Ringwald 0x81, 0x03, // Input (Constant, Variable, Absolute) 902fa56ea6SMatthias Ringwald 914cf72855SMatthias Ringwald // LED report + padding 922fa56ea6SMatthias Ringwald 932fa56ea6SMatthias Ringwald 0x95, 0x05, // Report Count (5) 942fa56ea6SMatthias Ringwald 0x75, 0x01, // Report Size (1) 952fa56ea6SMatthias Ringwald 0x05, 0x08, // Usage Page (LEDs) 962fa56ea6SMatthias Ringwald 0x19, 0x01, // Usage Minimum (Num Lock) 972fa56ea6SMatthias Ringwald 0x29, 0x05, // Usage Maxium (Kana) 982fa56ea6SMatthias Ringwald 0x91, 0x02, // Output (Data, Variable, Absolute) 992fa56ea6SMatthias Ringwald 1002fa56ea6SMatthias Ringwald 0x95, 0x01, // Report Count (1) 1012fa56ea6SMatthias Ringwald 0x75, 0x03, // Report Size (3) 1022fa56ea6SMatthias Ringwald 0x91, 0x03, // Output (Constant, Variable, Absolute) 1032fa56ea6SMatthias Ringwald 1042fa56ea6SMatthias Ringwald // Keycodes 1052fa56ea6SMatthias Ringwald 1062fa56ea6SMatthias Ringwald 0x95, 0x06, // Report Count (6) 1072fa56ea6SMatthias Ringwald 0x75, 0x08, // Report Size (8) 1082fa56ea6SMatthias Ringwald 0x15, 0x00, // Logical Minimum (0) 1092fa56ea6SMatthias Ringwald 0x25, 0xff, // Logical Maximum (1) 1102fa56ea6SMatthias Ringwald 0x05, 0x07, // Usage Page (Key codes) 1112fa56ea6SMatthias Ringwald 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 1122fa56ea6SMatthias Ringwald 0x29, 0xff, // Usage Maxium (Reserved) 1132fa56ea6SMatthias Ringwald 0x81, 0x00, // Input (Data, Array) 1142fa56ea6SMatthias Ringwald 1152fa56ea6SMatthias Ringwald 0xc0, // End collection 1162fa56ea6SMatthias Ringwald }; 1172fa56ea6SMatthias Ringwald 118ca44a803SMatthias Ringwald // 119ca44a803SMatthias Ringwald #define CHAR_ILLEGAL 0xff 120ca44a803SMatthias Ringwald #define CHAR_RETURN '\n' 121ca44a803SMatthias Ringwald #define CHAR_ESCAPE 27 122ca44a803SMatthias Ringwald #define CHAR_TAB '\t' 123ca44a803SMatthias Ringwald #define CHAR_BACKSPACE 0x7f 124ca44a803SMatthias Ringwald 125ca44a803SMatthias Ringwald // Simplified US Keyboard with Shift modifier 126ca44a803SMatthias Ringwald 127ca44a803SMatthias Ringwald /** 128ca44a803SMatthias Ringwald * English (US) 129ca44a803SMatthias Ringwald */ 130ca44a803SMatthias Ringwald static const uint8_t keytable_us_none [] = { 131ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 132ca44a803SMatthias Ringwald 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 133ca44a803SMatthias Ringwald 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 134ca44a803SMatthias Ringwald 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 135ca44a803SMatthias Ringwald '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 136ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 137ca44a803SMatthias Ringwald '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 138ca44a803SMatthias Ringwald '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 139ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 140ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 141ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 142ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 143ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 144ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 145ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 146ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 147ca44a803SMatthias Ringwald }; 148ca44a803SMatthias Ringwald 149ca44a803SMatthias Ringwald static const uint8_t keytable_us_shift[] = { 150ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 151ca44a803SMatthias Ringwald 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 152ca44a803SMatthias Ringwald 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 153ca44a803SMatthias Ringwald 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 154ca44a803SMatthias Ringwald '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 155ca44a803SMatthias Ringwald CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 156ca44a803SMatthias Ringwald '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 157ca44a803SMatthias Ringwald '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 158ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 159ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 160ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 161ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 162ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 163ca44a803SMatthias Ringwald CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 164ca44a803SMatthias Ringwald '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 165ca44a803SMatthias Ringwald '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 166ca44a803SMatthias Ringwald }; 167ca44a803SMatthias Ringwald 1684e43ca9fSMatthias Ringwald // STATE 1694e43ca9fSMatthias Ringwald 1704e43ca9fSMatthias Ringwald static uint8_t hid_service_buffer[250]; 1714e43ca9fSMatthias Ringwald static uint8_t device_id_sdp_service_buffer[100]; 1724e43ca9fSMatthias Ringwald static const char hid_device_name[] = "BTstack HID Keyboard"; 1734e43ca9fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 1744e43ca9fSMatthias Ringwald static uint16_t hid_cid; 1754e43ca9fSMatthias Ringwald static bd_addr_t device_addr; 1764e43ca9fSMatthias Ringwald 1774e43ca9fSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 1784e43ca9fSMatthias Ringwald static const char * device_addr_string = "BC:EC:5D:E6:15:03"; 1794e43ca9fSMatthias Ringwald #endif 1804e43ca9fSMatthias Ringwald 1814e43ca9fSMatthias Ringwald static enum { 1824e43ca9fSMatthias Ringwald APP_BOOTING, 1834e43ca9fSMatthias Ringwald APP_NOT_CONNECTED, 1844e43ca9fSMatthias Ringwald APP_CONNECTING, 1854e43ca9fSMatthias Ringwald APP_CONNECTED 1864e43ca9fSMatthias Ringwald } app_state = APP_BOOTING; 1874e43ca9fSMatthias Ringwald 1884e43ca9fSMatthias Ringwald 189ca44a803SMatthias Ringwald // HID Keyboard lookup 190ca44a803SMatthias Ringwald static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){ 191ca44a803SMatthias Ringwald int i; 192ca44a803SMatthias Ringwald for (i=0;i<size;i++){ 193ca44a803SMatthias Ringwald if (table[i] != character) continue; 194ca44a803SMatthias Ringwald *keycode = i; 195ca44a803SMatthias Ringwald return 1; 196ca44a803SMatthias Ringwald } 197ca44a803SMatthias Ringwald return 0; 198ca44a803SMatthias Ringwald } 199ca44a803SMatthias Ringwald 200ca44a803SMatthias Ringwald static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){ 201ca44a803SMatthias Ringwald int found; 202ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode); 203ca44a803SMatthias Ringwald if (found) { 204ca44a803SMatthias Ringwald *modifier = 0; // none 205ca44a803SMatthias Ringwald return 1; 206ca44a803SMatthias Ringwald } 207ca44a803SMatthias Ringwald found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode); 208ca44a803SMatthias Ringwald if (found) { 209ca44a803SMatthias Ringwald *modifier = 2; // shift 210ca44a803SMatthias Ringwald return 1; 211ca44a803SMatthias Ringwald } 212ca44a803SMatthias Ringwald return 0; 213ca44a803SMatthias Ringwald } 214ca44a803SMatthias Ringwald 215ca44a803SMatthias Ringwald // HID Report sending 2164cf72855SMatthias Ringwald static int send_keycode; 2174cf72855SMatthias Ringwald static int send_modifier; 2184cf72855SMatthias Ringwald 2194cf72855SMatthias Ringwald static void send_key(int modifier, int keycode){ 2204cf72855SMatthias Ringwald send_keycode = keycode; 2214cf72855SMatthias Ringwald send_modifier = modifier; 2228eb8d463SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 2234cf72855SMatthias Ringwald } 2244cf72855SMatthias Ringwald 2254cf72855SMatthias Ringwald static void send_report(int modifier, int keycode){ 2264cf72855SMatthias Ringwald uint8_t report[] = { 0xa1, modifier, 0, 0, keycode, 0, 0, 0, 0, 0}; 2278eb8d463SMatthias Ringwald hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report)); 2284cf72855SMatthias Ringwald } 2294cf72855SMatthias Ringwald 2308eb8d463SMatthias Ringwald // Demo Application 231ca44a803SMatthias Ringwald 2327ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 2332fa56ea6SMatthias Ringwald 2348eb8d463SMatthias Ringwald // On systems with STDIN, we can directly type on the console 2352fa56ea6SMatthias Ringwald 23695a8ee01SMatthias Ringwald static void stdin_process(char character){ 237ca44a803SMatthias Ringwald uint8_t modifier; 238ca44a803SMatthias Ringwald uint8_t keycode; 2394e43ca9fSMatthias Ringwald int found; 2404e43ca9fSMatthias Ringwald 2414e43ca9fSMatthias Ringwald switch (app_state){ 2424e43ca9fSMatthias Ringwald case APP_BOOTING: 2434e43ca9fSMatthias Ringwald case APP_CONNECTING: 2444e43ca9fSMatthias Ringwald // ignore 2454e43ca9fSMatthias Ringwald break; 2464e43ca9fSMatthias Ringwald 2474e43ca9fSMatthias Ringwald case APP_CONNECTED: 2484e43ca9fSMatthias Ringwald // send keyu 2494e43ca9fSMatthias Ringwald found = keycode_and_modifer_us_for_character(character, &keycode, &modifier); 250ca44a803SMatthias Ringwald if (found){ 251ca44a803SMatthias Ringwald send_key(modifier, keycode); 2524cf72855SMatthias Ringwald return; 2532fa56ea6SMatthias Ringwald } 2544e43ca9fSMatthias Ringwald break; 2554e43ca9fSMatthias Ringwald case APP_NOT_CONNECTED: 2564e43ca9fSMatthias Ringwald printf("Connecting to %s...\n", bd_addr_to_str(device_addr)); 2574e43ca9fSMatthias Ringwald hid_device_connect(device_addr, &hid_cid); 2584e43ca9fSMatthias Ringwald break; 2594e43ca9fSMatthias Ringwald } 2602fa56ea6SMatthias Ringwald } 2616518788aSMatthias Ringwald #else 2622fa56ea6SMatthias Ringwald 2636518788aSMatthias Ringwald // On embedded systems, send constant demo text with fixed period 2646518788aSMatthias Ringwald 2656518788aSMatthias Ringwald #define TYPING_PERIOD_MS 100 2666518788aSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n"; 2676518788aSMatthias Ringwald 2686518788aSMatthias Ringwald static int demo_pos; 2696518788aSMatthias Ringwald static btstack_timer_source_t typing_timer; 2706518788aSMatthias Ringwald 2716518788aSMatthias Ringwald static void typing_timer_handler(btstack_timer_source_t * ts){ 2726518788aSMatthias Ringwald 2736518788aSMatthias Ringwald // abort if not connected 2748eb8d463SMatthias Ringwald if (!hid_cid) return; 2756518788aSMatthias Ringwald 2766518788aSMatthias Ringwald // get next character 2776518788aSMatthias Ringwald uint8_t character = demo_text[demo_pos++]; 2786518788aSMatthias Ringwald if (demo_text[demo_pos] == 0){ 2796518788aSMatthias Ringwald demo_pos = 0; 2800316aa6fSMatthias Ringwald } 2810316aa6fSMatthias Ringwald 2826518788aSMatthias Ringwald // get keycodeand send 2836518788aSMatthias Ringwald uint8_t modifier; 2846518788aSMatthias Ringwald uint8_t keycode; 2856518788aSMatthias Ringwald int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier); 2866518788aSMatthias Ringwald if (found){ 2876518788aSMatthias Ringwald send_key(modifier, keycode); 2886518788aSMatthias Ringwald } 2896518788aSMatthias Ringwald 2906518788aSMatthias Ringwald // set next timer 2916518788aSMatthias Ringwald btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS); 2926518788aSMatthias Ringwald btstack_run_loop_add_timer(ts); 2936518788aSMatthias Ringwald } 2946518788aSMatthias Ringwald 2958eb8d463SMatthias Ringwald static void hid_embedded_start_typing(void){ 2966518788aSMatthias Ringwald demo_pos = 0; 2976518788aSMatthias Ringwald // set one-shot timer 2986518788aSMatthias Ringwald typing_timer.process = &typing_timer_handler; 2996518788aSMatthias Ringwald btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS); 3006518788aSMatthias Ringwald btstack_run_loop_add_timer(&typing_timer); 3016518788aSMatthias Ringwald } 3026518788aSMatthias Ringwald 3036518788aSMatthias Ringwald #endif 3046518788aSMatthias Ringwald 3050316aa6fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ 3062fa56ea6SMatthias Ringwald UNUSED(channel); 3070316aa6fSMatthias Ringwald UNUSED(packet_size); 3084e43ca9fSMatthias Ringwald uint8_t status; 3092fa56ea6SMatthias Ringwald switch (packet_type){ 3102fa56ea6SMatthias Ringwald case HCI_EVENT_PACKET: 3110316aa6fSMatthias Ringwald switch (packet[0]){ 3124e43ca9fSMatthias Ringwald case BTSTACK_EVENT_STATE: 3134e43ca9fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 3144e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3154e43ca9fSMatthias Ringwald break; 3164e43ca9fSMatthias Ringwald 3170316aa6fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 3180316aa6fSMatthias Ringwald // ssp: inform about user confirmation request 3190316aa6fSMatthias Ringwald log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet)); 3200316aa6fSMatthias Ringwald log_info("SSP User Confirmation Auto accept\n"); 3210316aa6fSMatthias Ringwald break; 3220316aa6fSMatthias Ringwald 3238eb8d463SMatthias Ringwald case HCI_EVENT_HID_META: 3248eb8d463SMatthias Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 3258eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 3264e43ca9fSMatthias Ringwald status = hid_subevent_connection_opened_get_status(packet); 3274e43ca9fSMatthias Ringwald if (status) { 3284e43ca9fSMatthias Ringwald // outgoing connection failed 3294e43ca9fSMatthias Ringwald printf("Connection failed, status 0x%x\n", status); 3304e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3314e43ca9fSMatthias Ringwald hid_cid = 0; 3324e43ca9fSMatthias Ringwald return; 3334e43ca9fSMatthias Ringwald } 3344e43ca9fSMatthias Ringwald app_state = APP_CONNECTED; 3358eb8d463SMatthias Ringwald hid_cid = hid_subevent_connection_opened_get_hid_cid(packet); 3367ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3378eb8d463SMatthias Ringwald printf("HID Connected, please start typing...\n"); 3388eb8d463SMatthias Ringwald #else 3398eb8d463SMatthias Ringwald printf("HID Connected, sending demo text...\n"); 3408eb8d463SMatthias Ringwald hid_embedded_start_typing(); 3416518788aSMatthias Ringwald #endif 3420316aa6fSMatthias Ringwald break; 3438eb8d463SMatthias Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 3440316aa6fSMatthias Ringwald printf("HID Disconnected\n"); 3454e43ca9fSMatthias Ringwald app_state = APP_NOT_CONNECTED; 3468eb8d463SMatthias Ringwald hid_cid = 0; 3470316aa6fSMatthias Ringwald break; 3488eb8d463SMatthias Ringwald case HID_SUBEVENT_CAN_SEND_NOW: 3494cf72855SMatthias Ringwald if (send_keycode){ 3504cf72855SMatthias Ringwald send_report(send_modifier, send_keycode); 3514cf72855SMatthias Ringwald send_keycode = 0; 3524cf72855SMatthias Ringwald send_modifier = 0; 3538eb8d463SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 3544cf72855SMatthias Ringwald } else { 3554cf72855SMatthias Ringwald send_report(0, 0); 3564cf72855SMatthias Ringwald } 3578eb8d463SMatthias Ringwald break; 3588eb8d463SMatthias Ringwald default: 3598eb8d463SMatthias Ringwald break; 3608eb8d463SMatthias Ringwald } 3618eb8d463SMatthias Ringwald break; 3622fa56ea6SMatthias Ringwald default: 3632fa56ea6SMatthias Ringwald break; 3642fa56ea6SMatthias Ringwald } 3652fa56ea6SMatthias Ringwald break; 3662fa56ea6SMatthias Ringwald default: 3672fa56ea6SMatthias Ringwald break; 3682fa56ea6SMatthias Ringwald } 3692fa56ea6SMatthias Ringwald } 3702fa56ea6SMatthias Ringwald 3712fa56ea6SMatthias Ringwald /* @section Main Application Setup 3722fa56ea6SMatthias Ringwald * 3732fa56ea6SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 3742fa56ea6SMatthias Ringwald * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. 3752fa56ea6SMatthias Ringwald * At the end the Bluetooth stack is started. 3762fa56ea6SMatthias Ringwald */ 3772fa56ea6SMatthias Ringwald 3782fa56ea6SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HID Device */ 3792fa56ea6SMatthias Ringwald 3802fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 3812fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 3822fa56ea6SMatthias Ringwald (void)argc; 3832fa56ea6SMatthias Ringwald (void)argv; 3842fa56ea6SMatthias Ringwald 3852fa56ea6SMatthias Ringwald gap_discoverable_control(1); 3862fa56ea6SMatthias Ringwald gap_set_class_of_device(0x2540); 3870c2b8870SMatthias Ringwald gap_set_local_name("HID Keyboard Demo 00:00:00:00:00:00"); 3882fa56ea6SMatthias Ringwald 3892fa56ea6SMatthias Ringwald // L2CAP 3902fa56ea6SMatthias Ringwald l2cap_init(); 3912fa56ea6SMatthias Ringwald 3922fa56ea6SMatthias Ringwald // SDP Server 3932fa56ea6SMatthias Ringwald sdp_init(); 3942fa56ea6SMatthias Ringwald memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); 3952fa56ea6SMatthias Ringwald // hid sevice subclass 2540 Keyboard, hid counntry code 33 US, hid virtual cable off, hid reconnect initiate off, hid boot device off 3962fa56ea6SMatthias Ringwald hid_create_sdp_record(hid_service_buffer, 0x10001, 0x2540, 33, 0, 0, 0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode), hid_device_name); 39747f85edbSMatthias Ringwald printf("HID service record size: %u\n", de_get_len( hid_service_buffer)); 3982fa56ea6SMatthias Ringwald sdp_register_service(hid_service_buffer); 3992fa56ea6SMatthias Ringwald 40047f85edbSMatthias 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 40147f85edbSMatthias Ringwald // device info: BlueKitchen GmbH, product 1, version 1 40247f85edbSMatthias 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); 40347f85edbSMatthias Ringwald printf("Device ID SDP service record size: %u\n", de_get_len((uint8_t*)device_id_sdp_service_buffer)); 40447f85edbSMatthias Ringwald sdp_register_service(device_id_sdp_service_buffer); 40547f85edbSMatthias Ringwald 4068eb8d463SMatthias Ringwald // HID Device 4078eb8d463SMatthias Ringwald hid_device_init(); 408*a4fe6467SMatthias Ringwald 409*a4fe6467SMatthias Ringwald // register for HCI events 410*a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 411*a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 412*a4fe6467SMatthias Ringwald 413*a4fe6467SMatthias Ringwald // register for HID events 4148eb8d463SMatthias Ringwald hid_device_register_packet_handler(&packet_handler); 4158eb8d463SMatthias Ringwald 4167ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 4174e43ca9fSMatthias Ringwald sscanf_bd_addr(device_addr_string, device_addr); 4182fa56ea6SMatthias Ringwald btstack_stdin_setup(stdin_process); 4192fa56ea6SMatthias Ringwald #endif 4202fa56ea6SMatthias Ringwald // turn on! 4212fa56ea6SMatthias Ringwald hci_power_control(HCI_POWER_ON); 4222fa56ea6SMatthias Ringwald return 0; 4232fa56ea6SMatthias Ringwald } 4242fa56ea6SMatthias Ringwald /* LISTING_END */ 4252fa56ea6SMatthias Ringwald /* EXAMPLE_END */ 426