xref: /btstack/example/hid_keyboard_demo.c (revision 574d351d38dbf008693250ba89042aafdc3f0208)
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 
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*574d351dSMatthias Ringwald // When not set to 0xffff, sniff and sniff subrating are enabled
66*574d351dSMatthias Ringwald static uint16_t host_max_latency = 1600;
67*574d351dSMatthias Ringwald static uint16_t host_min_timeout = 3200;
68*574d351dSMatthias Ringwald 
692fa56ea6SMatthias Ringwald // from USB HID Specification 1.1, Appendix B.1
702fa56ea6SMatthias Ringwald const uint8_t hid_descriptor_keyboard_boot_mode[] = {
712fa56ea6SMatthias Ringwald 
722fa56ea6SMatthias Ringwald     0x05, 0x01,                    // Usage Page (Generic Desktop)
732fa56ea6SMatthias Ringwald     0x09, 0x06,                    // Usage (Keyboard)
742fa56ea6SMatthias Ringwald     0xa1, 0x01,                    // Collection (Application)
752fa56ea6SMatthias Ringwald 
762fa56ea6SMatthias Ringwald     // Modifier byte
772fa56ea6SMatthias Ringwald 
782fa56ea6SMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
792fa56ea6SMatthias Ringwald     0x95, 0x08,                    //   Report Count (8)
802fa56ea6SMatthias Ringwald     0x05, 0x07,                    //   Usage Page (Key codes)
812fa56ea6SMatthias Ringwald     0x19, 0xe0,                    //   Usage Minimum (Keyboard LeftControl)
822fa56ea6SMatthias Ringwald     0x29, 0xe7,                    //   Usage Maxium (Keyboard Right GUI)
832fa56ea6SMatthias Ringwald     0x15, 0x00,                    //   Logical Minimum (0)
842fa56ea6SMatthias Ringwald     0x25, 0x01,                    //   Logical Maximum (1)
852fa56ea6SMatthias Ringwald     0x81, 0x02,                    //   Input (Data, Variable, Absolute)
862fa56ea6SMatthias Ringwald 
872fa56ea6SMatthias Ringwald     // Reserved byte
882fa56ea6SMatthias Ringwald 
892fa56ea6SMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
902fa56ea6SMatthias Ringwald     0x95, 0x08,                    //   Report Count (8)
912fa56ea6SMatthias Ringwald     0x81, 0x03,                    //   Input (Constant, Variable, Absolute)
922fa56ea6SMatthias Ringwald 
934cf72855SMatthias Ringwald     // LED report + padding
942fa56ea6SMatthias Ringwald 
952fa56ea6SMatthias Ringwald     0x95, 0x05,                    //   Report Count (5)
962fa56ea6SMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
972fa56ea6SMatthias Ringwald     0x05, 0x08,                    //   Usage Page (LEDs)
982fa56ea6SMatthias Ringwald     0x19, 0x01,                    //   Usage Minimum (Num Lock)
992fa56ea6SMatthias Ringwald     0x29, 0x05,                    //   Usage Maxium (Kana)
1002fa56ea6SMatthias Ringwald     0x91, 0x02,                    //   Output (Data, Variable, Absolute)
1012fa56ea6SMatthias Ringwald 
1022fa56ea6SMatthias Ringwald     0x95, 0x01,                    //   Report Count (1)
1032fa56ea6SMatthias Ringwald     0x75, 0x03,                    //   Report Size (3)
1042fa56ea6SMatthias Ringwald     0x91, 0x03,                    //   Output (Constant, Variable, Absolute)
1052fa56ea6SMatthias Ringwald 
1062fa56ea6SMatthias Ringwald     // Keycodes
1072fa56ea6SMatthias Ringwald 
1082fa56ea6SMatthias Ringwald     0x95, 0x06,                    //   Report Count (6)
1092fa56ea6SMatthias Ringwald     0x75, 0x08,                    //   Report Size (8)
1102fa56ea6SMatthias Ringwald     0x15, 0x00,                    //   Logical Minimum (0)
1112fa56ea6SMatthias Ringwald     0x25, 0xff,                    //   Logical Maximum (1)
1122fa56ea6SMatthias Ringwald     0x05, 0x07,                    //   Usage Page (Key codes)
1132fa56ea6SMatthias Ringwald     0x19, 0x00,                    //   Usage Minimum (Reserved (no event indicated))
1142fa56ea6SMatthias Ringwald     0x29, 0xff,                    //   Usage Maxium (Reserved)
1152fa56ea6SMatthias Ringwald     0x81, 0x00,                    //   Input (Data, Array)
1162fa56ea6SMatthias Ringwald 
1172fa56ea6SMatthias Ringwald     0xc0,                          // End collection
1182fa56ea6SMatthias Ringwald };
1192fa56ea6SMatthias Ringwald 
120ca44a803SMatthias Ringwald //
121ca44a803SMatthias Ringwald #define CHAR_ILLEGAL     0xff
122ca44a803SMatthias Ringwald #define CHAR_RETURN     '\n'
123ca44a803SMatthias Ringwald #define CHAR_ESCAPE      27
124ca44a803SMatthias Ringwald #define CHAR_TAB         '\t'
125ca44a803SMatthias Ringwald #define CHAR_BACKSPACE   0x7f
126ca44a803SMatthias Ringwald 
127ca44a803SMatthias Ringwald // Simplified US Keyboard with Shift modifier
128ca44a803SMatthias Ringwald 
129ca44a803SMatthias Ringwald /**
130ca44a803SMatthias Ringwald  * English (US)
131ca44a803SMatthias Ringwald  */
132ca44a803SMatthias Ringwald static const uint8_t keytable_us_none [] = {
133ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*   0-3 */
134ca44a803SMatthias Ringwald     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',                   /*  4-13 */
135ca44a803SMatthias Ringwald     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',                   /* 14-23 */
136ca44a803SMatthias Ringwald     'u', 'v', 'w', 'x', 'y', 'z',                                       /* 24-29 */
137ca44a803SMatthias Ringwald     '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',                   /* 30-39 */
138ca44a803SMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
139ca44a803SMatthias Ringwald     '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',',       /* 45-54 */
140ca44a803SMatthias Ringwald     '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
141ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
142ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
143ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
144ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
145ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
146ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
147ca44a803SMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
148ca44a803SMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xa7,                                 /* 97-100 */
149ca44a803SMatthias Ringwald };
150ca44a803SMatthias Ringwald 
151ca44a803SMatthias Ringwald static const uint8_t keytable_us_shift[] = {
152ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*  0-3  */
153ca44a803SMatthias Ringwald     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',                   /*  4-13 */
154ca44a803SMatthias Ringwald     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',                   /* 14-23 */
155ca44a803SMatthias Ringwald     'U', 'V', 'W', 'X', 'Y', 'Z',                                       /* 24-29 */
156ca44a803SMatthias Ringwald     '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',                   /* 30-39 */
157ca44a803SMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
158ca44a803SMatthias Ringwald     '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<',         /* 45-54 */
159ca44a803SMatthias Ringwald     '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
160ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
161ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
162ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
163ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
164ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
165ca44a803SMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
166ca44a803SMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
167ca44a803SMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xb1,                                 /* 97-100 */
168ca44a803SMatthias Ringwald };
169ca44a803SMatthias Ringwald 
1704e43ca9fSMatthias Ringwald // STATE
1714e43ca9fSMatthias Ringwald 
172*574d351dSMatthias Ringwald static uint8_t hid_service_buffer[300];
1734e43ca9fSMatthias Ringwald static uint8_t device_id_sdp_service_buffer[100];
1744e43ca9fSMatthias Ringwald static const char hid_device_name[] = "BTstack HID Keyboard";
1754e43ca9fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
1764e43ca9fSMatthias Ringwald static uint16_t hid_cid;
1774e43ca9fSMatthias Ringwald static bd_addr_t device_addr;
178b274c7b3SMilanka Ringwald static uint8_t hid_boot_device = 0;
1794e43ca9fSMatthias Ringwald 
1804e43ca9fSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
1814e43ca9fSMatthias Ringwald static const char * device_addr_string = "BC:EC:5D:E6:15:03";
1824e43ca9fSMatthias Ringwald #endif
1834e43ca9fSMatthias Ringwald 
1844e43ca9fSMatthias Ringwald static enum {
1854e43ca9fSMatthias Ringwald     APP_BOOTING,
1864e43ca9fSMatthias Ringwald     APP_NOT_CONNECTED,
1874e43ca9fSMatthias Ringwald     APP_CONNECTING,
1884e43ca9fSMatthias Ringwald     APP_CONNECTED
1894e43ca9fSMatthias Ringwald } app_state = APP_BOOTING;
1904e43ca9fSMatthias Ringwald 
191ca44a803SMatthias Ringwald // HID Keyboard lookup
192ca44a803SMatthias Ringwald static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){
193ca44a803SMatthias Ringwald     int i;
194ca44a803SMatthias Ringwald     for (i=0;i<size;i++){
195ca44a803SMatthias Ringwald         if (table[i] != character) continue;
196ca44a803SMatthias Ringwald         *keycode = i;
197ca44a803SMatthias Ringwald         return 1;
198ca44a803SMatthias Ringwald     }
199ca44a803SMatthias Ringwald     return 0;
200ca44a803SMatthias Ringwald }
201ca44a803SMatthias Ringwald 
202ca44a803SMatthias Ringwald static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){
203ca44a803SMatthias Ringwald     int found;
204ca44a803SMatthias Ringwald     found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode);
205ca44a803SMatthias Ringwald     if (found) {
206ca44a803SMatthias Ringwald         *modifier = 0;  // none
207ca44a803SMatthias Ringwald         return 1;
208ca44a803SMatthias Ringwald     }
209ca44a803SMatthias Ringwald     found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode);
210ca44a803SMatthias Ringwald     if (found) {
211ca44a803SMatthias Ringwald         *modifier = 2;  // shift
212ca44a803SMatthias Ringwald         return 1;
213ca44a803SMatthias Ringwald     }
214ca44a803SMatthias Ringwald     return 0;
215ca44a803SMatthias Ringwald }
216ca44a803SMatthias Ringwald 
217ca44a803SMatthias Ringwald // HID Report sending
2184cf72855SMatthias Ringwald static int send_keycode;
2194cf72855SMatthias Ringwald static int send_modifier;
2204cf72855SMatthias Ringwald 
2214cf72855SMatthias Ringwald static void send_key(int modifier, int keycode){
2224cf72855SMatthias Ringwald     send_keycode = keycode;
2234cf72855SMatthias Ringwald     send_modifier = modifier;
2248eb8d463SMatthias Ringwald     hid_device_request_can_send_now_event(hid_cid);
2254cf72855SMatthias Ringwald }
2264cf72855SMatthias Ringwald 
2274cf72855SMatthias Ringwald static void send_report(int modifier, int keycode){
2284cf72855SMatthias Ringwald     uint8_t report[] = { 0xa1, modifier, 0, 0, keycode, 0, 0, 0, 0, 0};
2298eb8d463SMatthias Ringwald     hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report));
2304cf72855SMatthias Ringwald }
2314cf72855SMatthias Ringwald 
2328eb8d463SMatthias Ringwald // Demo Application
233ca44a803SMatthias Ringwald 
2347ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
2352fa56ea6SMatthias Ringwald 
2368eb8d463SMatthias Ringwald // On systems with STDIN, we can directly type on the console
2372fa56ea6SMatthias Ringwald 
23895a8ee01SMatthias Ringwald static void stdin_process(char character){
239ca44a803SMatthias Ringwald     uint8_t modifier;
240ca44a803SMatthias Ringwald     uint8_t keycode;
2414e43ca9fSMatthias Ringwald     int found;
2424e43ca9fSMatthias Ringwald 
2434e43ca9fSMatthias Ringwald     switch (app_state){
2444e43ca9fSMatthias Ringwald         case APP_BOOTING:
2454e43ca9fSMatthias Ringwald         case APP_CONNECTING:
2464e43ca9fSMatthias Ringwald             // ignore
2474e43ca9fSMatthias Ringwald             break;
2484e43ca9fSMatthias Ringwald 
2494e43ca9fSMatthias Ringwald         case APP_CONNECTED:
2504e43ca9fSMatthias Ringwald             // send keyu
2514e43ca9fSMatthias Ringwald             found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
252ca44a803SMatthias Ringwald             if (found){
253ca44a803SMatthias Ringwald                 send_key(modifier, keycode);
2544cf72855SMatthias Ringwald                 return;
2552fa56ea6SMatthias Ringwald             }
2564e43ca9fSMatthias Ringwald             break;
2574e43ca9fSMatthias Ringwald         case APP_NOT_CONNECTED:
2584e43ca9fSMatthias Ringwald             printf("Connecting to %s...\n", bd_addr_to_str(device_addr));
2594e43ca9fSMatthias Ringwald             hid_device_connect(device_addr, &hid_cid);
2604e43ca9fSMatthias Ringwald             break;
2617bbeb3adSMilanka Ringwald         default:
2627bbeb3adSMilanka Ringwald             btstack_assert(false);
2637bbeb3adSMilanka Ringwald             break;
2644e43ca9fSMatthias Ringwald     }
2652fa56ea6SMatthias Ringwald }
2666518788aSMatthias Ringwald #else
2672fa56ea6SMatthias Ringwald 
2686518788aSMatthias Ringwald // On embedded systems, send constant demo text with fixed period
2696518788aSMatthias Ringwald 
2706518788aSMatthias Ringwald #define TYPING_PERIOD_MS 100
2716518788aSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n";
2726518788aSMatthias Ringwald 
2736518788aSMatthias Ringwald static int demo_pos;
2746518788aSMatthias Ringwald static btstack_timer_source_t typing_timer;
2756518788aSMatthias Ringwald 
2766518788aSMatthias Ringwald static void typing_timer_handler(btstack_timer_source_t * ts){
2776518788aSMatthias Ringwald 
2786518788aSMatthias Ringwald     // abort if not connected
2798eb8d463SMatthias Ringwald     if (!hid_cid) return;
2806518788aSMatthias Ringwald 
2816518788aSMatthias Ringwald     // get next character
2826518788aSMatthias Ringwald     uint8_t character = demo_text[demo_pos++];
2836518788aSMatthias Ringwald     if (demo_text[demo_pos] == 0){
2846518788aSMatthias Ringwald         demo_pos = 0;
2850316aa6fSMatthias Ringwald     }
2860316aa6fSMatthias Ringwald 
2876518788aSMatthias Ringwald     // get keycodeand send
2886518788aSMatthias Ringwald     uint8_t modifier;
2896518788aSMatthias Ringwald     uint8_t keycode;
2906518788aSMatthias Ringwald     int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
2916518788aSMatthias Ringwald     if (found){
2926518788aSMatthias Ringwald         send_key(modifier, keycode);
2936518788aSMatthias Ringwald     }
2946518788aSMatthias Ringwald 
2956518788aSMatthias Ringwald     // set next timer
2966518788aSMatthias Ringwald     btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS);
2976518788aSMatthias Ringwald     btstack_run_loop_add_timer(ts);
2986518788aSMatthias Ringwald }
2996518788aSMatthias Ringwald 
3008eb8d463SMatthias Ringwald static void hid_embedded_start_typing(void){
3016518788aSMatthias Ringwald     demo_pos = 0;
3026518788aSMatthias Ringwald     // set one-shot timer
3036518788aSMatthias Ringwald     typing_timer.process = &typing_timer_handler;
3046518788aSMatthias Ringwald     btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS);
3056518788aSMatthias Ringwald     btstack_run_loop_add_timer(&typing_timer);
3066518788aSMatthias Ringwald }
3076518788aSMatthias Ringwald 
3086518788aSMatthias Ringwald #endif
3096518788aSMatthias Ringwald 
3100316aa6fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){
3112fa56ea6SMatthias Ringwald     UNUSED(channel);
3120316aa6fSMatthias Ringwald     UNUSED(packet_size);
3134e43ca9fSMatthias Ringwald     uint8_t status;
3142fa56ea6SMatthias Ringwald     switch (packet_type){
3152fa56ea6SMatthias Ringwald         case HCI_EVENT_PACKET:
3166058cb0dSMatthias Ringwald             switch (hci_event_packet_get_type(packet)){
3174e43ca9fSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
3184e43ca9fSMatthias Ringwald                     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
3194e43ca9fSMatthias Ringwald                     app_state = APP_NOT_CONNECTED;
3204e43ca9fSMatthias Ringwald                     break;
3214e43ca9fSMatthias Ringwald 
3220316aa6fSMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
3230316aa6fSMatthias Ringwald                     // ssp: inform about user confirmation request
3240316aa6fSMatthias Ringwald                     log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet));
3250316aa6fSMatthias Ringwald                     log_info("SSP User Confirmation Auto accept\n");
3260316aa6fSMatthias Ringwald                     break;
3270316aa6fSMatthias Ringwald 
3288eb8d463SMatthias Ringwald                 case HCI_EVENT_HID_META:
3298eb8d463SMatthias Ringwald                     switch (hci_event_hid_meta_get_subevent_code(packet)){
3308eb8d463SMatthias Ringwald                         case HID_SUBEVENT_CONNECTION_OPENED:
3314e43ca9fSMatthias Ringwald                             status = hid_subevent_connection_opened_get_status(packet);
3326058cb0dSMatthias Ringwald                             if (status != ERROR_CODE_SUCCESS) {
3334e43ca9fSMatthias Ringwald                                 // outgoing connection failed
3344e43ca9fSMatthias Ringwald                                 printf("Connection failed, status 0x%x\n", status);
3354e43ca9fSMatthias Ringwald                                 app_state = APP_NOT_CONNECTED;
3364e43ca9fSMatthias Ringwald                                 hid_cid = 0;
3374e43ca9fSMatthias Ringwald                                 return;
3384e43ca9fSMatthias Ringwald                             }
3394e43ca9fSMatthias Ringwald                             app_state = APP_CONNECTED;
3408eb8d463SMatthias Ringwald                             hid_cid = hid_subevent_connection_opened_get_hid_cid(packet);
3417ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
3428eb8d463SMatthias Ringwald                             printf("HID Connected, please start typing...\n");
3438eb8d463SMatthias Ringwald #else
3448eb8d463SMatthias Ringwald                             printf("HID Connected, sending demo text...\n");
3458eb8d463SMatthias Ringwald                             hid_embedded_start_typing();
3466518788aSMatthias Ringwald #endif
3470316aa6fSMatthias Ringwald                             break;
3488eb8d463SMatthias Ringwald                         case HID_SUBEVENT_CONNECTION_CLOSED:
3490316aa6fSMatthias Ringwald                             printf("HID Disconnected\n");
3504e43ca9fSMatthias Ringwald                             app_state = APP_NOT_CONNECTED;
3518eb8d463SMatthias Ringwald                             hid_cid = 0;
3520316aa6fSMatthias Ringwald                             break;
3538eb8d463SMatthias Ringwald                         case HID_SUBEVENT_CAN_SEND_NOW:
3544cf72855SMatthias Ringwald                             if (send_keycode){
3554cf72855SMatthias Ringwald                                 send_report(send_modifier, send_keycode);
3564cf72855SMatthias Ringwald                                 send_keycode = 0;
3574cf72855SMatthias Ringwald                                 send_modifier = 0;
3588eb8d463SMatthias Ringwald                                 hid_device_request_can_send_now_event(hid_cid);
3594cf72855SMatthias Ringwald                             } else {
3604cf72855SMatthias Ringwald                                 send_report(0, 0);
3614cf72855SMatthias Ringwald                             }
3628eb8d463SMatthias Ringwald                             break;
3638eb8d463SMatthias Ringwald                         default:
3648eb8d463SMatthias Ringwald                             break;
3658eb8d463SMatthias Ringwald                     }
3668eb8d463SMatthias Ringwald                     break;
3672fa56ea6SMatthias Ringwald                 default:
3682fa56ea6SMatthias Ringwald                     break;
3692fa56ea6SMatthias Ringwald             }
3702fa56ea6SMatthias Ringwald             break;
3712fa56ea6SMatthias Ringwald         default:
3722fa56ea6SMatthias Ringwald             break;
3732fa56ea6SMatthias Ringwald     }
3742fa56ea6SMatthias Ringwald }
3752fa56ea6SMatthias Ringwald 
3762fa56ea6SMatthias Ringwald /* @section Main Application Setup
3772fa56ea6SMatthias Ringwald  *
3782fa56ea6SMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
3792fa56ea6SMatthias Ringwald  * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it.
3802fa56ea6SMatthias Ringwald  * At the end the Bluetooth stack is started.
3812fa56ea6SMatthias Ringwald  */
3822fa56ea6SMatthias Ringwald 
3832fa56ea6SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HID Device */
3842fa56ea6SMatthias Ringwald 
3852fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
3862fa56ea6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
3872fa56ea6SMatthias Ringwald     (void)argc;
3882fa56ea6SMatthias Ringwald     (void)argv;
3892fa56ea6SMatthias Ringwald 
39045a92abaSMatthias Ringwald     // allow to get found by inquiry
3912fa56ea6SMatthias Ringwald     gap_discoverable_control(1);
39245a92abaSMatthias Ringwald     // use Limited Discoverable Mode; Peripheral; Keyboard as CoD
3932fa56ea6SMatthias Ringwald     gap_set_class_of_device(0x2540);
39445a92abaSMatthias Ringwald     // set local name to be identified - zeroes will be replaced by actual BD ADDR
3950c2b8870SMatthias Ringwald     gap_set_local_name("HID Keyboard Demo 00:00:00:00:00:00");
39645a92abaSMatthias Ringwald     // allow for role switch in general and sniff mode
39745a92abaSMatthias Ringwald     gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE );
39845a92abaSMatthias Ringwald     // allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it
39945a92abaSMatthias Ringwald     gap_set_allow_role_switch(true);
4002fa56ea6SMatthias Ringwald 
4012fa56ea6SMatthias Ringwald     // L2CAP
4022fa56ea6SMatthias Ringwald     l2cap_init();
4032fa56ea6SMatthias Ringwald 
4042fa56ea6SMatthias Ringwald     // SDP Server
4052fa56ea6SMatthias Ringwald     sdp_init();
4062fa56ea6SMatthias Ringwald     memset(hid_service_buffer, 0, sizeof(hid_service_buffer));
4071ad99f3bSMilanka Ringwald 
4081ad99f3bSMilanka Ringwald     uint8_t hid_virtual_cable = 0;
4091ad99f3bSMilanka Ringwald     uint8_t hid_remote_wake = 1;
4101ad99f3bSMilanka Ringwald     uint8_t hid_reconnect_initiate = 1;
411ffe3f1a1SMilanka Ringwald     uint8_t hid_normally_connectable = 1;
4121ad99f3bSMilanka Ringwald 
41380d9d5d4SMilanka Ringwald     hid_sdp_record_t hid_params = {
41480d9d5d4SMilanka Ringwald         // hid sevice subclass 2540 Keyboard, hid counntry code 33 US
41580d9d5d4SMilanka Ringwald         0x2540, 33,
41680d9d5d4SMilanka Ringwald         hid_virtual_cable, hid_remote_wake,
41780d9d5d4SMilanka Ringwald         hid_reconnect_initiate, hid_normally_connectable,
41880d9d5d4SMilanka Ringwald         hid_boot_device,
419*574d351dSMatthias Ringwald         host_max_latency, host_min_timeout,
420*574d351dSMatthias Ringwald         3200,
42180d9d5d4SMilanka Ringwald         hid_descriptor_keyboard_boot_mode,
42280d9d5d4SMilanka Ringwald         sizeof(hid_descriptor_keyboard_boot_mode),
42380d9d5d4SMilanka Ringwald         hid_device_name
42480d9d5d4SMilanka Ringwald     };
42580d9d5d4SMilanka Ringwald 
42680d9d5d4SMilanka Ringwald     hid_create_sdp_record(hid_service_buffer, 0x10001, &hid_params);
42780d9d5d4SMilanka Ringwald 
42847f85edbSMatthias Ringwald     printf("HID service record size: %u\n", de_get_len( hid_service_buffer));
4292fa56ea6SMatthias Ringwald     sdp_register_service(hid_service_buffer);
4302fa56ea6SMatthias Ringwald 
43147f85edbSMatthias 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
43247f85edbSMatthias Ringwald     // device info: BlueKitchen GmbH, product 1, version 1
43347f85edbSMatthias 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);
43447f85edbSMatthias Ringwald     printf("Device ID SDP service record size: %u\n", de_get_len((uint8_t*)device_id_sdp_service_buffer));
43547f85edbSMatthias Ringwald     sdp_register_service(device_id_sdp_service_buffer);
43647f85edbSMatthias Ringwald 
4378eb8d463SMatthias Ringwald     // HID Device
438a796c06fSMilanka Ringwald     hid_device_init(hid_boot_device, sizeof(hid_descriptor_keyboard_boot_mode), hid_descriptor_keyboard_boot_mode);
439a4fe6467SMatthias Ringwald 
440a4fe6467SMatthias Ringwald     // register for HCI events
441a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
442a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
443a4fe6467SMatthias Ringwald 
444a4fe6467SMatthias Ringwald     // register for HID events
4458eb8d463SMatthias Ringwald     hid_device_register_packet_handler(&packet_handler);
4468eb8d463SMatthias Ringwald 
4477ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
4484e43ca9fSMatthias Ringwald     sscanf_bd_addr(device_addr_string, device_addr);
4492fa56ea6SMatthias Ringwald     btstack_stdin_setup(stdin_process);
4502fa56ea6SMatthias Ringwald #endif
4512fa56ea6SMatthias Ringwald     // turn on!
4522fa56ea6SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
4532fa56ea6SMatthias Ringwald     return 0;
4542fa56ea6SMatthias Ringwald }
4552fa56ea6SMatthias Ringwald /* LISTING_END */
4562fa56ea6SMatthias Ringwald /* EXAMPLE_END */
457