xref: /btstack/example/hog_keyboard_demo.c (revision 4cf49978783c0210e70100601dff72ad36d7de21)
1a4bfc4feSMatthias Ringwald /*
2a4bfc4feSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3a4bfc4feSMatthias Ringwald  *
4a4bfc4feSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5a4bfc4feSMatthias Ringwald  * modification, are permitted provided that the following conditions
6a4bfc4feSMatthias Ringwald  * are met:
7a4bfc4feSMatthias Ringwald  *
8a4bfc4feSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9a4bfc4feSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10a4bfc4feSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11a4bfc4feSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12a4bfc4feSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13a4bfc4feSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14a4bfc4feSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15a4bfc4feSMatthias Ringwald  *    from this software without specific prior written permission.
16a4bfc4feSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17a4bfc4feSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18a4bfc4feSMatthias Ringwald  *    monetary gain.
19a4bfc4feSMatthias Ringwald  *
20a4bfc4feSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21a4bfc4feSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a4bfc4feSMatthias 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,
25a4bfc4feSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26a4bfc4feSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27a4bfc4feSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28a4bfc4feSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29a4bfc4feSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30a4bfc4feSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a4bfc4feSMatthias Ringwald  * SUCH DAMAGE.
32a4bfc4feSMatthias Ringwald  *
33a4bfc4feSMatthias Ringwald  * Please inquire about commercial licensing options at
34a4bfc4feSMatthias Ringwald  * [email protected]
35a4bfc4feSMatthias Ringwald  *
36a4bfc4feSMatthias Ringwald  */
37a4bfc4feSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hog_keyboard_demo.c"
39a4bfc4feSMatthias Ringwald 
40a4bfc4feSMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(hog_keyboard_demo): HID Keyboard LE
42a4bfc4feSMatthias Ringwald  */
43a4bfc4feSMatthias Ringwald // *****************************************************************************
44a4bfc4feSMatthias Ringwald 
45a4bfc4feSMatthias Ringwald #include <stdint.h>
46a4bfc4feSMatthias Ringwald #include <stdio.h>
47a4bfc4feSMatthias Ringwald #include <stdlib.h>
48a4bfc4feSMatthias Ringwald #include <string.h>
49a4bfc4feSMatthias Ringwald #include <inttypes.h>
50a4bfc4feSMatthias Ringwald 
51a4bfc4feSMatthias Ringwald #include "hog_keyboard_demo.h"
52a4bfc4feSMatthias Ringwald 
53a4bfc4feSMatthias Ringwald #include "btstack.h"
54a4bfc4feSMatthias Ringwald 
55a4bfc4feSMatthias Ringwald #include "ble/gatt-service/battery_service_server.h"
56a4bfc4feSMatthias Ringwald #include "ble/gatt-service/device_information_service_server.h"
57a4bfc4feSMatthias Ringwald #include "ble/gatt-service/hids_device.h"
58a4bfc4feSMatthias Ringwald 
59a4bfc4feSMatthias Ringwald // from USB HID Specification 1.1, Appendix B.1
60a4bfc4feSMatthias Ringwald const uint8_t hid_descriptor_keyboard_boot_mode[] = {
61a4bfc4feSMatthias Ringwald 
62a4bfc4feSMatthias Ringwald     0x05, 0x01,                    // Usage Page (Generic Desktop)
63a4bfc4feSMatthias Ringwald     0x09, 0x06,                    // Usage (Keyboard)
64a4bfc4feSMatthias Ringwald     0xa1, 0x01,                    // Collection (Application)
65a4bfc4feSMatthias Ringwald 
66a3ace439SMatthias Ringwald     0x85,  0x01,                   // Report ID 1
67a3ace439SMatthias Ringwald 
68a4bfc4feSMatthias Ringwald     // Modifier byte
69a4bfc4feSMatthias Ringwald 
70a4bfc4feSMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
71a4bfc4feSMatthias Ringwald     0x95, 0x08,                    //   Report Count (8)
72a4bfc4feSMatthias Ringwald     0x05, 0x07,                    //   Usage Page (Key codes)
73a4bfc4feSMatthias Ringwald     0x19, 0xe0,                    //   Usage Minimum (Keyboard LeftControl)
74a4bfc4feSMatthias Ringwald     0x29, 0xe7,                    //   Usage Maxium (Keyboard Right GUI)
75a4bfc4feSMatthias Ringwald     0x15, 0x00,                    //   Logical Minimum (0)
76a4bfc4feSMatthias Ringwald     0x25, 0x01,                    //   Logical Maximum (1)
77a4bfc4feSMatthias Ringwald     0x81, 0x02,                    //   Input (Data, Variable, Absolute)
78a4bfc4feSMatthias Ringwald 
79a4bfc4feSMatthias Ringwald     // Reserved byte
80a4bfc4feSMatthias Ringwald 
81a4bfc4feSMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
82a4bfc4feSMatthias Ringwald     0x95, 0x08,                    //   Report Count (8)
83a4bfc4feSMatthias Ringwald     0x81, 0x03,                    //   Input (Constant, Variable, Absolute)
84a4bfc4feSMatthias Ringwald 
85a4bfc4feSMatthias Ringwald     // LED report + padding
86a4bfc4feSMatthias Ringwald 
87a4bfc4feSMatthias Ringwald     0x95, 0x05,                    //   Report Count (5)
88a4bfc4feSMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
89a4bfc4feSMatthias Ringwald     0x05, 0x08,                    //   Usage Page (LEDs)
90a4bfc4feSMatthias Ringwald     0x19, 0x01,                    //   Usage Minimum (Num Lock)
91a4bfc4feSMatthias Ringwald     0x29, 0x05,                    //   Usage Maxium (Kana)
92a4bfc4feSMatthias Ringwald     0x91, 0x02,                    //   Output (Data, Variable, Absolute)
93a4bfc4feSMatthias Ringwald 
94a4bfc4feSMatthias Ringwald     0x95, 0x01,                    //   Report Count (1)
95a4bfc4feSMatthias Ringwald     0x75, 0x03,                    //   Report Size (3)
96a4bfc4feSMatthias Ringwald     0x91, 0x03,                    //   Output (Constant, Variable, Absolute)
97a4bfc4feSMatthias Ringwald 
98a4bfc4feSMatthias Ringwald     // Keycodes
99a4bfc4feSMatthias Ringwald 
100a4bfc4feSMatthias Ringwald     0x95, 0x06,                    //   Report Count (6)
101a4bfc4feSMatthias Ringwald     0x75, 0x08,                    //   Report Size (8)
102a4bfc4feSMatthias Ringwald     0x15, 0x00,                    //   Logical Minimum (0)
103a4bfc4feSMatthias Ringwald     0x25, 0xff,                    //   Logical Maximum (1)
104a4bfc4feSMatthias Ringwald     0x05, 0x07,                    //   Usage Page (Key codes)
105a4bfc4feSMatthias Ringwald     0x19, 0x00,                    //   Usage Minimum (Reserved (no event indicated))
106a4bfc4feSMatthias Ringwald     0x29, 0xff,                    //   Usage Maxium (Reserved)
107a4bfc4feSMatthias Ringwald     0x81, 0x00,                    //   Input (Data, Array)
108a4bfc4feSMatthias Ringwald 
109a4bfc4feSMatthias Ringwald     0xc0,                          // End collection
110a4bfc4feSMatthias Ringwald };
111a4bfc4feSMatthias Ringwald 
112a4bfc4feSMatthias Ringwald 
113a4bfc4feSMatthias Ringwald 
114a4bfc4feSMatthias Ringwald //
115a4bfc4feSMatthias Ringwald #define CHAR_ILLEGAL     0xff
116a4bfc4feSMatthias Ringwald #define CHAR_RETURN     '\n'
117a4bfc4feSMatthias Ringwald #define CHAR_ESCAPE      27
118a4bfc4feSMatthias Ringwald #define CHAR_TAB         '\t'
119a4bfc4feSMatthias Ringwald #define CHAR_BACKSPACE   0x7f
120a4bfc4feSMatthias Ringwald 
121a4bfc4feSMatthias Ringwald // Simplified US Keyboard with Shift modifier
122a4bfc4feSMatthias Ringwald 
123a4bfc4feSMatthias Ringwald /**
124a4bfc4feSMatthias Ringwald  * English (US)
125a4bfc4feSMatthias Ringwald  */
126a4bfc4feSMatthias Ringwald static const uint8_t keytable_us_none [] = {
127a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*   0-3 */
128a4bfc4feSMatthias Ringwald     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',                   /*  4-13 */
129a4bfc4feSMatthias Ringwald     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',                   /* 14-23 */
130a4bfc4feSMatthias Ringwald     'u', 'v', 'w', 'x', 'y', 'z',                                       /* 24-29 */
131a4bfc4feSMatthias Ringwald     '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',                   /* 30-39 */
132a4bfc4feSMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
133a4bfc4feSMatthias Ringwald     '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',',       /* 45-54 */
134a4bfc4feSMatthias Ringwald     '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
135a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
136a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
137a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
138a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
139a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
140a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
141a4bfc4feSMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
142a4bfc4feSMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xa7,                                 /* 97-100 */
143a4bfc4feSMatthias Ringwald };
144a4bfc4feSMatthias Ringwald 
145a4bfc4feSMatthias Ringwald static const uint8_t keytable_us_shift[] = {
146a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*  0-3  */
147a4bfc4feSMatthias Ringwald     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',                   /*  4-13 */
148a4bfc4feSMatthias Ringwald     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',                   /* 14-23 */
149a4bfc4feSMatthias Ringwald     'U', 'V', 'W', 'X', 'Y', 'Z',                                       /* 24-29 */
150a4bfc4feSMatthias Ringwald     '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',                   /* 30-39 */
151a4bfc4feSMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
152a4bfc4feSMatthias Ringwald     '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<',         /* 45-54 */
153a4bfc4feSMatthias Ringwald     '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
154a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
155a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
156a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
157a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
158a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
159a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
160a4bfc4feSMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
161a4bfc4feSMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xb1,                                 /* 97-100 */
162a4bfc4feSMatthias Ringwald };
163a4bfc4feSMatthias Ringwald 
164a4bfc4feSMatthias Ringwald // static btstack_timer_source_t heartbeat;
165a4bfc4feSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
166a4bfc4feSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
167a4bfc4feSMatthias Ringwald static uint8_t battery = 100;
168381856a5SMatthias Ringwald static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
16950304921SMatthias Ringwald static uint8_t protocol_mode = 1;
170a4bfc4feSMatthias Ringwald 
171a4bfc4feSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
172a4bfc4feSMatthias Ringwald 
173a4bfc4feSMatthias Ringwald const uint8_t adv_data[] = {
174a4bfc4feSMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
175a4bfc4feSMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
176a4bfc4feSMatthias Ringwald     // Name
177a4bfc4feSMatthias Ringwald     0x0d, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'H', 'I', 'D', ' ', 'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd',
178a4bfc4feSMatthias Ringwald     // 16-bit Service UUIDs
179a4bfc4feSMatthias Ringwald     0x03, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE & 0xff, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE >> 8,
180ae856536SMatthias Ringwald     // Appearance HID - Keyboard (Category 15, Sub-Category 1)
181ae856536SMatthias Ringwald     0x03, BLUETOOTH_DATA_TYPE_APPEARANCE, 0xC1, 0x03,
182a4bfc4feSMatthias Ringwald };
183a4bfc4feSMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
184a4bfc4feSMatthias Ringwald 
le_keyboard_setup(void)185a4bfc4feSMatthias Ringwald static void le_keyboard_setup(void){
186a4bfc4feSMatthias Ringwald 
187a4bfc4feSMatthias Ringwald     l2cap_init();
188a4bfc4feSMatthias Ringwald 
189a4bfc4feSMatthias Ringwald     // setup SM: Display only
190a4bfc4feSMatthias Ringwald     sm_init();
191*4cf49978SMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
192a4bfc4feSMatthias Ringwald     sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
193a4bfc4feSMatthias Ringwald 
194a4bfc4feSMatthias Ringwald     // setup ATT server
195a4bfc4feSMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
196a4bfc4feSMatthias Ringwald 
197a4bfc4feSMatthias Ringwald     // setup battery service
198a4bfc4feSMatthias Ringwald     battery_service_server_init(battery);
199a4bfc4feSMatthias Ringwald 
200a4bfc4feSMatthias Ringwald     // setup device information service
201a4bfc4feSMatthias Ringwald     device_information_service_server_init();
202a4bfc4feSMatthias Ringwald 
203a4bfc4feSMatthias Ringwald     // setup HID Device service
204a4bfc4feSMatthias Ringwald     hids_device_init(0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode));
205a4bfc4feSMatthias Ringwald 
206a4bfc4feSMatthias Ringwald     // setup advertisements
207a4bfc4feSMatthias Ringwald     uint16_t adv_int_min = 0x0030;
208a4bfc4feSMatthias Ringwald     uint16_t adv_int_max = 0x0030;
209a4bfc4feSMatthias Ringwald     uint8_t adv_type = 0;
210a4bfc4feSMatthias Ringwald     bd_addr_t null_addr;
211a4bfc4feSMatthias Ringwald     memset(null_addr, 0, 6);
212a4bfc4feSMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
213a4bfc4feSMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
214a4bfc4feSMatthias Ringwald     gap_advertisements_enable(1);
215a4fe6467SMatthias Ringwald 
216a4fe6467SMatthias Ringwald     // register for HCI events
217a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
218a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
219a4fe6467SMatthias Ringwald 
220a4fe6467SMatthias Ringwald     // register for SM events
221a4fe6467SMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
222a4fe6467SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
223a4fe6467SMatthias Ringwald 
224a4fe6467SMatthias Ringwald     // register for HIDS
225a4fe6467SMatthias Ringwald     hids_device_register_packet_handler(packet_handler);
226a4bfc4feSMatthias Ringwald }
227a4bfc4feSMatthias Ringwald 
228a4bfc4feSMatthias Ringwald // HID Keyboard lookup
lookup_keycode(uint8_t character,const uint8_t * table,int size,uint8_t * keycode)229a4bfc4feSMatthias Ringwald static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){
230a4bfc4feSMatthias Ringwald     int i;
231a4bfc4feSMatthias Ringwald     for (i=0;i<size;i++){
232a4bfc4feSMatthias Ringwald         if (table[i] != character) continue;
233a4bfc4feSMatthias Ringwald         *keycode = i;
234a4bfc4feSMatthias Ringwald         return 1;
235a4bfc4feSMatthias Ringwald     }
236a4bfc4feSMatthias Ringwald     return 0;
237a4bfc4feSMatthias Ringwald }
238a4bfc4feSMatthias Ringwald 
keycode_and_modifer_us_for_character(uint8_t character,uint8_t * keycode,uint8_t * modifier)239a4bfc4feSMatthias Ringwald static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){
240a4bfc4feSMatthias Ringwald     int found;
241a4bfc4feSMatthias Ringwald     found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode);
242a4bfc4feSMatthias Ringwald     if (found) {
243a4bfc4feSMatthias Ringwald         *modifier = 0;  // none
244a4bfc4feSMatthias Ringwald         return 1;
245a4bfc4feSMatthias Ringwald     }
246a4bfc4feSMatthias Ringwald     found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode);
247a4bfc4feSMatthias Ringwald     if (found) {
248a4bfc4feSMatthias Ringwald         *modifier = 2;  // shift
249a4bfc4feSMatthias Ringwald         return 1;
250a4bfc4feSMatthias Ringwald     }
251a4bfc4feSMatthias Ringwald     return 0;
252a4bfc4feSMatthias Ringwald }
253a4bfc4feSMatthias Ringwald 
254a4bfc4feSMatthias Ringwald // HID Report sending
send_report(int modifier,int keycode)255a4bfc4feSMatthias Ringwald static void send_report(int modifier, int keycode){
256cf5ed549SMatthias Ringwald     uint8_t report[] = {  modifier, 0, keycode, 0, 0, 0, 0, 0};
25750304921SMatthias Ringwald     switch (protocol_mode){
25850304921SMatthias Ringwald         case 0:
25950304921SMatthias Ringwald             hids_device_send_boot_keyboard_input_report(con_handle, report, sizeof(report));
26050304921SMatthias Ringwald             break;
26150304921SMatthias Ringwald         case 1:
262a4bfc4feSMatthias Ringwald            hids_device_send_input_report(con_handle, report, sizeof(report));
26350304921SMatthias Ringwald            break;
26450304921SMatthias Ringwald         default:
26550304921SMatthias Ringwald             break;
26650304921SMatthias Ringwald     }
267a4bfc4feSMatthias Ringwald }
268a4bfc4feSMatthias Ringwald 
269a4bfc4feSMatthias Ringwald // Demo Application
270a4bfc4feSMatthias Ringwald 
271a4bfc4feSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
272a4bfc4feSMatthias Ringwald 
273a4bfc4feSMatthias Ringwald // On systems with STDIN, we can directly type on the console
274a4bfc4feSMatthias Ringwald static enum {
275a4bfc4feSMatthias Ringwald     W4_INPUT,
276a4bfc4feSMatthias Ringwald     W4_CAN_SEND_FROM_BUFFER,
277a4bfc4feSMatthias Ringwald     W4_CAN_SEND_KEY_UP,
278a4bfc4feSMatthias Ringwald } state;
279a4bfc4feSMatthias Ringwald 
280a4bfc4feSMatthias Ringwald // Buffer for 20 characters
281a4bfc4feSMatthias Ringwald static uint8_t ascii_input_storage[20];
282a4bfc4feSMatthias Ringwald static btstack_ring_buffer_t ascii_input_buffer;
283a4bfc4feSMatthias Ringwald 
typing_can_send_now(void)284a4bfc4feSMatthias Ringwald static void typing_can_send_now(void){
285a4bfc4feSMatthias Ringwald     switch (state){
286a4bfc4feSMatthias Ringwald         case W4_CAN_SEND_FROM_BUFFER:
287a4bfc4feSMatthias Ringwald             while (1){
288a4bfc4feSMatthias Ringwald                 uint8_t c;
289a4bfc4feSMatthias Ringwald                 uint32_t num_bytes_read;
290a4bfc4feSMatthias Ringwald 
291a4bfc4feSMatthias Ringwald                 btstack_ring_buffer_read(&ascii_input_buffer, &c, 1, &num_bytes_read);
292a4bfc4feSMatthias Ringwald                 if (num_bytes_read == 0){
293a4bfc4feSMatthias Ringwald                     state = W4_INPUT;
294a4bfc4feSMatthias Ringwald                     break;
295a4bfc4feSMatthias Ringwald                 }
296a4bfc4feSMatthias Ringwald 
297a4bfc4feSMatthias Ringwald                 uint8_t modifier;
298a4bfc4feSMatthias Ringwald                 uint8_t keycode;
299a4bfc4feSMatthias Ringwald                 int found = keycode_and_modifer_us_for_character(c, &keycode, &modifier);
300a4bfc4feSMatthias Ringwald                 if (!found) continue;
301a4bfc4feSMatthias Ringwald 
302381856a5SMatthias Ringwald                 printf("sending: %c\n", c);
303381856a5SMatthias Ringwald 
304a4bfc4feSMatthias Ringwald                 send_report(modifier, keycode);
305a4bfc4feSMatthias Ringwald                 state = W4_CAN_SEND_KEY_UP;
306a4bfc4feSMatthias Ringwald                 hids_device_request_can_send_now_event(con_handle);
307a4bfc4feSMatthias Ringwald                 break;
308a4bfc4feSMatthias Ringwald             }
309a4bfc4feSMatthias Ringwald             break;
310a4bfc4feSMatthias Ringwald         case W4_CAN_SEND_KEY_UP:
311a4bfc4feSMatthias Ringwald             send_report(0, 0);
312a4bfc4feSMatthias Ringwald             if (btstack_ring_buffer_bytes_available(&ascii_input_buffer)){
313a4bfc4feSMatthias Ringwald                 state = W4_CAN_SEND_FROM_BUFFER;
314a4bfc4feSMatthias Ringwald                 hids_device_request_can_send_now_event(con_handle);
315a4bfc4feSMatthias Ringwald             } else {
316a4bfc4feSMatthias Ringwald                 state = W4_INPUT;
317a4bfc4feSMatthias Ringwald             }
318a4bfc4feSMatthias Ringwald             break;
319a4bfc4feSMatthias Ringwald         default:
320a4bfc4feSMatthias Ringwald             break;
321a4bfc4feSMatthias Ringwald     }
322a4bfc4feSMatthias Ringwald }
323a4bfc4feSMatthias Ringwald 
stdin_process(char character)324a4bfc4feSMatthias Ringwald static void stdin_process(char character){
325a4bfc4feSMatthias Ringwald     uint8_t c = character;
326a4bfc4feSMatthias Ringwald     btstack_ring_buffer_write(&ascii_input_buffer, &c, 1);
327a4bfc4feSMatthias Ringwald     // start sending
328381856a5SMatthias Ringwald     if (state == W4_INPUT && con_handle != HCI_CON_HANDLE_INVALID){
329a4bfc4feSMatthias Ringwald         state = W4_CAN_SEND_FROM_BUFFER;
330a4bfc4feSMatthias Ringwald         hids_device_request_can_send_now_event(con_handle);
331a4bfc4feSMatthias Ringwald     }
332a4bfc4feSMatthias Ringwald }
333a4bfc4feSMatthias Ringwald 
334a4bfc4feSMatthias Ringwald #else
335a4bfc4feSMatthias Ringwald 
336a4bfc4feSMatthias Ringwald // On embedded systems, send constant demo text with fixed period
337a4bfc4feSMatthias Ringwald 
338a4bfc4feSMatthias Ringwald #define TYPING_PERIOD_MS 50
339a4bfc4feSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n";
340a4bfc4feSMatthias Ringwald 
341a4bfc4feSMatthias Ringwald static int demo_pos;
342a4bfc4feSMatthias Ringwald static btstack_timer_source_t typing_timer;
343a4bfc4feSMatthias Ringwald 
344a4bfc4feSMatthias Ringwald static int send_keycode;
345a4bfc4feSMatthias Ringwald static int send_modifier;
346a4bfc4feSMatthias Ringwald static int send_keyup;
347a4bfc4feSMatthias Ringwald 
send_key(int modifier,int keycode)348a4bfc4feSMatthias Ringwald static void send_key(int modifier, int keycode){
349a4bfc4feSMatthias Ringwald     send_keycode = keycode;
350a4bfc4feSMatthias Ringwald     send_modifier = modifier;
351a4bfc4feSMatthias Ringwald     hids_device_request_can_send_now_event(con_handle);
352a4bfc4feSMatthias Ringwald }
353a4bfc4feSMatthias Ringwald 
typing_can_send_now(void)354a4bfc4feSMatthias Ringwald static void typing_can_send_now(void){
355a4bfc4feSMatthias Ringwald    send_report(send_modifier, send_keycode);
356a4bfc4feSMatthias Ringwald }
357a4bfc4feSMatthias Ringwald 
typing_timer_handler(btstack_timer_source_t * ts)358a4bfc4feSMatthias Ringwald static void typing_timer_handler(btstack_timer_source_t * ts){
359a4bfc4feSMatthias Ringwald 
360a4bfc4feSMatthias Ringwald     if (send_keyup){
361a4bfc4feSMatthias Ringwald         // just send key up
362a4bfc4feSMatthias Ringwald         send_keyup = 0;
363a4bfc4feSMatthias Ringwald         send_key(0, 0);
364a4bfc4feSMatthias Ringwald     } else {
365a4bfc4feSMatthias Ringwald         // get next character
366a4bfc4feSMatthias Ringwald         uint8_t character = demo_text[demo_pos++];
367a4bfc4feSMatthias Ringwald         if (demo_text[demo_pos] == 0){
368a4bfc4feSMatthias Ringwald             demo_pos = 0;
369a4bfc4feSMatthias Ringwald         }
370a4bfc4feSMatthias Ringwald 
371a4bfc4feSMatthias Ringwald         // get keycode and send
372a4bfc4feSMatthias Ringwald         uint8_t modifier;
373a4bfc4feSMatthias Ringwald         uint8_t keycode;
374a4bfc4feSMatthias Ringwald         int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
375a4bfc4feSMatthias Ringwald         if (found){
376a4bfc4feSMatthias Ringwald             printf("%c\n", character);
377a4bfc4feSMatthias Ringwald             send_key(modifier, keycode);
378a4bfc4feSMatthias Ringwald             send_keyup = 1;
379a4bfc4feSMatthias Ringwald         }
380a4bfc4feSMatthias Ringwald     }
381a4bfc4feSMatthias Ringwald 
382a4bfc4feSMatthias Ringwald     // set next timer
383a4bfc4feSMatthias Ringwald     btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS);
384a4bfc4feSMatthias Ringwald     btstack_run_loop_add_timer(ts);
385a4bfc4feSMatthias Ringwald }
386a4bfc4feSMatthias Ringwald 
hid_embedded_start_typing(void)387a4bfc4feSMatthias Ringwald static void hid_embedded_start_typing(void){
388a4bfc4feSMatthias Ringwald     printf("Start typing..\n");
389a4bfc4feSMatthias Ringwald 
390a4bfc4feSMatthias Ringwald     demo_pos = 0;
391a4bfc4feSMatthias Ringwald     // set one-shot timer
392a4bfc4feSMatthias Ringwald     typing_timer.process = &typing_timer_handler;
393a4bfc4feSMatthias Ringwald     btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS);
394a4bfc4feSMatthias Ringwald     btstack_run_loop_add_timer(&typing_timer);
395a4bfc4feSMatthias Ringwald }
396a4bfc4feSMatthias Ringwald 
397a4bfc4feSMatthias Ringwald #endif
398a4bfc4feSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)399a4bfc4feSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
400a4bfc4feSMatthias Ringwald     UNUSED(channel);
401a4bfc4feSMatthias Ringwald     UNUSED(size);
402a4bfc4feSMatthias Ringwald 
4037bbeb3adSMilanka Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
4047bbeb3adSMilanka Ringwald 
405a4bfc4feSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
406a4bfc4feSMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
407d241e50bSMatthias Ringwald             con_handle = HCI_CON_HANDLE_INVALID;
408a4bfc4feSMatthias Ringwald             printf("Disconnected\n");
409a4bfc4feSMatthias Ringwald             break;
410a4bfc4feSMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
411a4bfc4feSMatthias Ringwald             printf("Just Works requested\n");
412a4bfc4feSMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
413a4bfc4feSMatthias Ringwald             break;
414a4bfc4feSMatthias Ringwald         case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
415a4bfc4feSMatthias Ringwald             printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
416a4bfc4feSMatthias Ringwald             sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
417a4bfc4feSMatthias Ringwald             break;
418a4bfc4feSMatthias Ringwald         case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
419a4bfc4feSMatthias Ringwald             printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
420a4bfc4feSMatthias Ringwald             break;
421a4bfc4feSMatthias Ringwald         case HCI_EVENT_HIDS_META:
422a4bfc4feSMatthias Ringwald             switch (hci_event_hids_meta_get_subevent_code(packet)){
423a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_INPUT_REPORT_ENABLE:
424a4bfc4feSMatthias Ringwald                     con_handle = hids_subevent_input_report_enable_get_con_handle(packet);
425a4bfc4feSMatthias Ringwald                     printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet));
426a4bfc4feSMatthias Ringwald #ifndef HAVE_BTSTACK_STDIN
427a4bfc4feSMatthias Ringwald                     hid_embedded_start_typing();
428a4bfc4feSMatthias Ringwald #endif
429a4bfc4feSMatthias Ringwald                     break;
430a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE:
43131eaaa99SMatthias Ringwald                     con_handle = hids_subevent_boot_keyboard_input_report_enable_get_con_handle(packet);
432a4bfc4feSMatthias Ringwald                     printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet));
433a4bfc4feSMatthias Ringwald                     break;
434a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_PROTOCOL_MODE:
43550304921SMatthias Ringwald                     protocol_mode = hids_subevent_protocol_mode_get_protocol_mode(packet);
436a4bfc4feSMatthias Ringwald                     printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot");
437a4bfc4feSMatthias Ringwald                     break;
438a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_CAN_SEND_NOW:
439a4bfc4feSMatthias Ringwald                     typing_can_send_now();
440a4bfc4feSMatthias Ringwald                     break;
441a4bfc4feSMatthias Ringwald                 default:
442a4bfc4feSMatthias Ringwald                     break;
443a4bfc4feSMatthias Ringwald             }
4447bbeb3adSMilanka Ringwald             break;
4457bbeb3adSMilanka Ringwald 
4467bbeb3adSMilanka Ringwald         default:
447a4bfc4feSMatthias Ringwald             break;
448a4bfc4feSMatthias Ringwald     }
449a4bfc4feSMatthias Ringwald }
450a4bfc4feSMatthias Ringwald 
451a4bfc4feSMatthias Ringwald int btstack_main(void);
btstack_main(void)452a4bfc4feSMatthias Ringwald int btstack_main(void)
453a4bfc4feSMatthias Ringwald {
454a4bfc4feSMatthias Ringwald     le_keyboard_setup();
455a4bfc4feSMatthias Ringwald 
456a4bfc4feSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
457a4bfc4feSMatthias Ringwald     btstack_ring_buffer_init(&ascii_input_buffer, ascii_input_storage, sizeof(ascii_input_storage));
458a4bfc4feSMatthias Ringwald     btstack_stdin_setup(stdin_process);
459a4bfc4feSMatthias Ringwald #endif
460a4bfc4feSMatthias Ringwald 
461a4bfc4feSMatthias Ringwald     // turn on!
462a4bfc4feSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
463a4bfc4feSMatthias Ringwald 
464a4bfc4feSMatthias Ringwald     return 0;
465a4bfc4feSMatthias Ringwald }
4664dc98401SMilanka Ringwald /* EXAMPLE_END */
467