xref: /btstack/example/hog_keyboard_demo.c (revision d241e50b75ebdd93d98579255639368577a99829)
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
23a4bfc4feSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24a4bfc4feSMatthias Ringwald  * RINGWALD 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 
38a4bfc4feSMatthias Ringwald #define __BTSTACK_FILE__ "hog_keyboard_demo.c"
39a4bfc4feSMatthias Ringwald 
40a4bfc4feSMatthias Ringwald // *****************************************************************************
41a4bfc4feSMatthias Ringwald /* EXAMPLE_START(hog_keyboard_demo): HID-over-GATT Keyboard
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 
66a4bfc4feSMatthias Ringwald     // Modifier byte
67a4bfc4feSMatthias Ringwald 
68a4bfc4feSMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
69a4bfc4feSMatthias Ringwald     0x95, 0x08,                    //   Report Count (8)
70a4bfc4feSMatthias Ringwald     0x05, 0x07,                    //   Usage Page (Key codes)
71a4bfc4feSMatthias Ringwald     0x19, 0xe0,                    //   Usage Minimum (Keyboard LeftControl)
72a4bfc4feSMatthias Ringwald     0x29, 0xe7,                    //   Usage Maxium (Keyboard Right GUI)
73a4bfc4feSMatthias Ringwald     0x15, 0x00,                    //   Logical Minimum (0)
74a4bfc4feSMatthias Ringwald     0x25, 0x01,                    //   Logical Maximum (1)
75a4bfc4feSMatthias Ringwald     0x81, 0x02,                    //   Input (Data, Variable, Absolute)
76a4bfc4feSMatthias Ringwald 
77a4bfc4feSMatthias Ringwald     // Reserved byte
78a4bfc4feSMatthias Ringwald 
79a4bfc4feSMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
80a4bfc4feSMatthias Ringwald     0x95, 0x08,                    //   Report Count (8)
81a4bfc4feSMatthias Ringwald     0x81, 0x03,                    //   Input (Constant, Variable, Absolute)
82a4bfc4feSMatthias Ringwald 
83a4bfc4feSMatthias Ringwald     // LED report + padding
84a4bfc4feSMatthias Ringwald 
85a4bfc4feSMatthias Ringwald     0x95, 0x05,                    //   Report Count (5)
86a4bfc4feSMatthias Ringwald     0x75, 0x01,                    //   Report Size (1)
87a4bfc4feSMatthias Ringwald     0x05, 0x08,                    //   Usage Page (LEDs)
88a4bfc4feSMatthias Ringwald     0x19, 0x01,                    //   Usage Minimum (Num Lock)
89a4bfc4feSMatthias Ringwald     0x29, 0x05,                    //   Usage Maxium (Kana)
90a4bfc4feSMatthias Ringwald     0x91, 0x02,                    //   Output (Data, Variable, Absolute)
91a4bfc4feSMatthias Ringwald 
92a4bfc4feSMatthias Ringwald     0x95, 0x01,                    //   Report Count (1)
93a4bfc4feSMatthias Ringwald     0x75, 0x03,                    //   Report Size (3)
94a4bfc4feSMatthias Ringwald     0x91, 0x03,                    //   Output (Constant, Variable, Absolute)
95a4bfc4feSMatthias Ringwald 
96a4bfc4feSMatthias Ringwald     // Keycodes
97a4bfc4feSMatthias Ringwald 
98a4bfc4feSMatthias Ringwald     0x95, 0x06,                    //   Report Count (6)
99a4bfc4feSMatthias Ringwald     0x75, 0x08,                    //   Report Size (8)
100a4bfc4feSMatthias Ringwald     0x15, 0x00,                    //   Logical Minimum (0)
101a4bfc4feSMatthias Ringwald     0x25, 0xff,                    //   Logical Maximum (1)
102a4bfc4feSMatthias Ringwald     0x05, 0x07,                    //   Usage Page (Key codes)
103a4bfc4feSMatthias Ringwald     0x19, 0x00,                    //   Usage Minimum (Reserved (no event indicated))
104a4bfc4feSMatthias Ringwald     0x29, 0xff,                    //   Usage Maxium (Reserved)
105a4bfc4feSMatthias Ringwald     0x81, 0x00,                    //   Input (Data, Array)
106a4bfc4feSMatthias Ringwald 
107a4bfc4feSMatthias Ringwald     0xc0,                          // End collection
108a4bfc4feSMatthias Ringwald };
109a4bfc4feSMatthias Ringwald 
110a4bfc4feSMatthias Ringwald 
111a4bfc4feSMatthias Ringwald 
112a4bfc4feSMatthias Ringwald //
113a4bfc4feSMatthias Ringwald #define CHAR_ILLEGAL     0xff
114a4bfc4feSMatthias Ringwald #define CHAR_RETURN     '\n'
115a4bfc4feSMatthias Ringwald #define CHAR_ESCAPE      27
116a4bfc4feSMatthias Ringwald #define CHAR_TAB         '\t'
117a4bfc4feSMatthias Ringwald #define CHAR_BACKSPACE   0x7f
118a4bfc4feSMatthias Ringwald 
119a4bfc4feSMatthias Ringwald // Simplified US Keyboard with Shift modifier
120a4bfc4feSMatthias Ringwald 
121a4bfc4feSMatthias Ringwald /**
122a4bfc4feSMatthias Ringwald  * English (US)
123a4bfc4feSMatthias Ringwald  */
124a4bfc4feSMatthias Ringwald static const uint8_t keytable_us_none [] = {
125a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*   0-3 */
126a4bfc4feSMatthias Ringwald     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',                   /*  4-13 */
127a4bfc4feSMatthias Ringwald     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',                   /* 14-23 */
128a4bfc4feSMatthias Ringwald     'u', 'v', 'w', 'x', 'y', 'z',                                       /* 24-29 */
129a4bfc4feSMatthias Ringwald     '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',                   /* 30-39 */
130a4bfc4feSMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
131a4bfc4feSMatthias Ringwald     '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',',       /* 45-54 */
132a4bfc4feSMatthias Ringwald     '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
133a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
134a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
135a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
136a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
137a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
138a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
139a4bfc4feSMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
140a4bfc4feSMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xa7,                                 /* 97-100 */
141a4bfc4feSMatthias Ringwald };
142a4bfc4feSMatthias Ringwald 
143a4bfc4feSMatthias Ringwald static const uint8_t keytable_us_shift[] = {
144a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*  0-3  */
145a4bfc4feSMatthias Ringwald     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',                   /*  4-13 */
146a4bfc4feSMatthias Ringwald     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',                   /* 14-23 */
147a4bfc4feSMatthias Ringwald     'U', 'V', 'W', 'X', 'Y', 'Z',                                       /* 24-29 */
148a4bfc4feSMatthias Ringwald     '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',                   /* 30-39 */
149a4bfc4feSMatthias Ringwald     CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
150a4bfc4feSMatthias Ringwald     '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<',         /* 45-54 */
151a4bfc4feSMatthias Ringwald     '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
152a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
153a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
154a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
155a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
156a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
157a4bfc4feSMatthias Ringwald     CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
158a4bfc4feSMatthias Ringwald     '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
159a4bfc4feSMatthias Ringwald     '6', '7', '8', '9', '0', '.', 0xb1,                                 /* 97-100 */
160a4bfc4feSMatthias Ringwald };
161a4bfc4feSMatthias Ringwald 
162a4bfc4feSMatthias Ringwald // static btstack_timer_source_t heartbeat;
163a4bfc4feSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
164a4bfc4feSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
165a4bfc4feSMatthias Ringwald static uint8_t battery = 100;
166381856a5SMatthias Ringwald static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
167a4bfc4feSMatthias Ringwald 
168a4bfc4feSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
169a4bfc4feSMatthias Ringwald 
170a4bfc4feSMatthias Ringwald const uint8_t adv_data[] = {
171a4bfc4feSMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
172a4bfc4feSMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
173a4bfc4feSMatthias Ringwald     // Name
174a4bfc4feSMatthias Ringwald     0x0d, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'H', 'I', 'D', ' ', 'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd',
175a4bfc4feSMatthias Ringwald     // 16-bit Service UUIDs
176a4bfc4feSMatthias 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,
177a4bfc4feSMatthias Ringwald };
178a4bfc4feSMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
179a4bfc4feSMatthias Ringwald 
180a4bfc4feSMatthias Ringwald static void le_keyboard_setup(void){
181a4bfc4feSMatthias Ringwald 
182a4bfc4feSMatthias Ringwald     // register for HCI events
183a4bfc4feSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
184a4bfc4feSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
185a4bfc4feSMatthias Ringwald 
186a4bfc4feSMatthias Ringwald     l2cap_init();
187a4bfc4feSMatthias Ringwald 
188a4bfc4feSMatthias Ringwald     // setup le device db
189a4bfc4feSMatthias Ringwald     le_device_db_init();
190a4bfc4feSMatthias Ringwald 
191a4bfc4feSMatthias Ringwald     // setup SM: Display only
192a4bfc4feSMatthias Ringwald     sm_init();
193a4bfc4feSMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
194a4bfc4feSMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
195a4bfc4feSMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
196a4bfc4feSMatthias Ringwald     sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
197a4bfc4feSMatthias Ringwald 
198a4bfc4feSMatthias Ringwald     // setup ATT server
199a4bfc4feSMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
200a4bfc4feSMatthias Ringwald 
201a4bfc4feSMatthias Ringwald     // setup battery service
202a4bfc4feSMatthias Ringwald     battery_service_server_init(battery);
203a4bfc4feSMatthias Ringwald 
204a4bfc4feSMatthias Ringwald     // setup device information service
205a4bfc4feSMatthias Ringwald     device_information_service_server_init();
206a4bfc4feSMatthias Ringwald 
207a4bfc4feSMatthias Ringwald     // setup HID Device service
208a4bfc4feSMatthias Ringwald     hids_device_init(0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode));
209a4bfc4feSMatthias Ringwald     hids_device_register_packet_handler(packet_handler);
210a4bfc4feSMatthias Ringwald 
211a4bfc4feSMatthias Ringwald     // setup advertisements
212a4bfc4feSMatthias Ringwald     uint16_t adv_int_min = 0x0030;
213a4bfc4feSMatthias Ringwald     uint16_t adv_int_max = 0x0030;
214a4bfc4feSMatthias Ringwald     uint8_t adv_type = 0;
215a4bfc4feSMatthias Ringwald     bd_addr_t null_addr;
216a4bfc4feSMatthias Ringwald     memset(null_addr, 0, 6);
217a4bfc4feSMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
218a4bfc4feSMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
219a4bfc4feSMatthias Ringwald     gap_advertisements_enable(1);
220a4bfc4feSMatthias Ringwald }
221a4bfc4feSMatthias Ringwald 
222a4bfc4feSMatthias Ringwald // HID Keyboard lookup
223a4bfc4feSMatthias Ringwald static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){
224a4bfc4feSMatthias Ringwald     int i;
225a4bfc4feSMatthias Ringwald     for (i=0;i<size;i++){
226a4bfc4feSMatthias Ringwald         if (table[i] != character) continue;
227a4bfc4feSMatthias Ringwald         *keycode = i;
228a4bfc4feSMatthias Ringwald         return 1;
229a4bfc4feSMatthias Ringwald     }
230a4bfc4feSMatthias Ringwald     return 0;
231a4bfc4feSMatthias Ringwald }
232a4bfc4feSMatthias Ringwald 
233a4bfc4feSMatthias Ringwald static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){
234a4bfc4feSMatthias Ringwald     int found;
235a4bfc4feSMatthias Ringwald     found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode);
236a4bfc4feSMatthias Ringwald     if (found) {
237a4bfc4feSMatthias Ringwald         *modifier = 0;  // none
238a4bfc4feSMatthias Ringwald         return 1;
239a4bfc4feSMatthias Ringwald     }
240a4bfc4feSMatthias Ringwald     found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode);
241a4bfc4feSMatthias Ringwald     if (found) {
242a4bfc4feSMatthias Ringwald         *modifier = 2;  // shift
243a4bfc4feSMatthias Ringwald         return 1;
244a4bfc4feSMatthias Ringwald     }
245a4bfc4feSMatthias Ringwald     return 0;
246a4bfc4feSMatthias Ringwald }
247a4bfc4feSMatthias Ringwald 
248a4bfc4feSMatthias Ringwald // HID Report sending
249a4bfc4feSMatthias Ringwald 
250a4bfc4feSMatthias Ringwald static void send_report(int modifier, int keycode){
251a4bfc4feSMatthias Ringwald     uint8_t report[] = { /* 0xa1, */ modifier, 0, 0, keycode, 0, 0, 0, 0, 0};
252a4bfc4feSMatthias Ringwald     hids_device_send_input_report(con_handle, report, sizeof(report));
253a4bfc4feSMatthias Ringwald }
254a4bfc4feSMatthias Ringwald 
255a4bfc4feSMatthias Ringwald // Demo Application
256a4bfc4feSMatthias Ringwald 
257a4bfc4feSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
258a4bfc4feSMatthias Ringwald 
259a4bfc4feSMatthias Ringwald // On systems with STDIN, we can directly type on the console
260a4bfc4feSMatthias Ringwald static enum {
261a4bfc4feSMatthias Ringwald     W4_INPUT,
262a4bfc4feSMatthias Ringwald     W4_CAN_SEND_FROM_BUFFER,
263a4bfc4feSMatthias Ringwald     W4_CAN_SEND_KEY_UP,
264a4bfc4feSMatthias Ringwald } state;
265a4bfc4feSMatthias Ringwald 
266a4bfc4feSMatthias Ringwald // Buffer for 20 characters
267a4bfc4feSMatthias Ringwald static uint8_t ascii_input_storage[20];
268a4bfc4feSMatthias Ringwald static btstack_ring_buffer_t ascii_input_buffer;
269a4bfc4feSMatthias Ringwald 
270a4bfc4feSMatthias Ringwald static void typing_can_send_now(void){
271a4bfc4feSMatthias Ringwald     switch (state){
272a4bfc4feSMatthias Ringwald         case W4_CAN_SEND_FROM_BUFFER:
273a4bfc4feSMatthias Ringwald             while (1){
274a4bfc4feSMatthias Ringwald                 uint8_t c;
275a4bfc4feSMatthias Ringwald                 uint32_t num_bytes_read;
276a4bfc4feSMatthias Ringwald 
277a4bfc4feSMatthias Ringwald                 btstack_ring_buffer_read(&ascii_input_buffer, &c, 1, &num_bytes_read);
278a4bfc4feSMatthias Ringwald                 if (num_bytes_read == 0){
279a4bfc4feSMatthias Ringwald                     state = W4_INPUT;
280a4bfc4feSMatthias Ringwald                     break;
281a4bfc4feSMatthias Ringwald                 }
282a4bfc4feSMatthias Ringwald 
283a4bfc4feSMatthias Ringwald                 uint8_t modifier;
284a4bfc4feSMatthias Ringwald                 uint8_t keycode;
285a4bfc4feSMatthias Ringwald                 int found = keycode_and_modifer_us_for_character(c, &keycode, &modifier);
286a4bfc4feSMatthias Ringwald                 if (!found) continue;
287a4bfc4feSMatthias Ringwald 
288381856a5SMatthias Ringwald                 printf("sending: %c\n", c);
289381856a5SMatthias Ringwald 
290a4bfc4feSMatthias Ringwald                 send_report(modifier, keycode);
291a4bfc4feSMatthias Ringwald                 state = W4_CAN_SEND_KEY_UP;
292a4bfc4feSMatthias Ringwald                 hids_device_request_can_send_now_event(con_handle);
293a4bfc4feSMatthias Ringwald                 break;
294a4bfc4feSMatthias Ringwald             }
295a4bfc4feSMatthias Ringwald             break;
296a4bfc4feSMatthias Ringwald         case W4_CAN_SEND_KEY_UP:
297a4bfc4feSMatthias Ringwald             send_report(0, 0);
298a4bfc4feSMatthias Ringwald             if (btstack_ring_buffer_bytes_available(&ascii_input_buffer)){
299a4bfc4feSMatthias Ringwald                 state = W4_CAN_SEND_FROM_BUFFER;
300a4bfc4feSMatthias Ringwald                 hids_device_request_can_send_now_event(con_handle);
301a4bfc4feSMatthias Ringwald             } else {
302a4bfc4feSMatthias Ringwald                 state = W4_INPUT;
303a4bfc4feSMatthias Ringwald             }
304a4bfc4feSMatthias Ringwald             break;
305a4bfc4feSMatthias Ringwald         default:
306a4bfc4feSMatthias Ringwald             break;
307a4bfc4feSMatthias Ringwald     }
308a4bfc4feSMatthias Ringwald }
309a4bfc4feSMatthias Ringwald 
310a4bfc4feSMatthias Ringwald static void stdin_process(char character){
311a4bfc4feSMatthias Ringwald     uint8_t c = character;
312a4bfc4feSMatthias Ringwald     btstack_ring_buffer_write(&ascii_input_buffer, &c, 1);
313a4bfc4feSMatthias Ringwald     // start sending
314381856a5SMatthias Ringwald     if (state == W4_INPUT && con_handle != HCI_CON_HANDLE_INVALID){
315a4bfc4feSMatthias Ringwald         state = W4_CAN_SEND_FROM_BUFFER;
316a4bfc4feSMatthias Ringwald         hids_device_request_can_send_now_event(con_handle);
317a4bfc4feSMatthias Ringwald     }
318a4bfc4feSMatthias Ringwald }
319a4bfc4feSMatthias Ringwald 
320a4bfc4feSMatthias Ringwald #else
321a4bfc4feSMatthias Ringwald 
322a4bfc4feSMatthias Ringwald // On embedded systems, send constant demo text with fixed period
323a4bfc4feSMatthias Ringwald 
324a4bfc4feSMatthias Ringwald #define TYPING_PERIOD_MS 50
325a4bfc4feSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n";
326a4bfc4feSMatthias Ringwald 
327a4bfc4feSMatthias Ringwald static int demo_pos;
328a4bfc4feSMatthias Ringwald static btstack_timer_source_t typing_timer;
329a4bfc4feSMatthias Ringwald 
330a4bfc4feSMatthias Ringwald static int send_keycode;
331a4bfc4feSMatthias Ringwald static int send_modifier;
332a4bfc4feSMatthias Ringwald static int send_keyup;
333a4bfc4feSMatthias Ringwald 
334a4bfc4feSMatthias Ringwald static void send_key(int modifier, int keycode){
335a4bfc4feSMatthias Ringwald     send_keycode = keycode;
336a4bfc4feSMatthias Ringwald     send_modifier = modifier;
337a4bfc4feSMatthias Ringwald     hids_device_request_can_send_now_event(con_handle);
338a4bfc4feSMatthias Ringwald }
339a4bfc4feSMatthias Ringwald 
340a4bfc4feSMatthias Ringwald static void typing_can_send_now(void){
341a4bfc4feSMatthias Ringwald    send_report(send_modifier, send_keycode);
342a4bfc4feSMatthias Ringwald }
343a4bfc4feSMatthias Ringwald 
344a4bfc4feSMatthias Ringwald static void typing_timer_handler(btstack_timer_source_t * ts){
345a4bfc4feSMatthias Ringwald 
346a4bfc4feSMatthias Ringwald     if (send_keyup){
347a4bfc4feSMatthias Ringwald         // just send key up
348a4bfc4feSMatthias Ringwald         send_keyup = 0;
349a4bfc4feSMatthias Ringwald         send_key(0, 0);
350a4bfc4feSMatthias Ringwald     } else {
351a4bfc4feSMatthias Ringwald         // get next character
352a4bfc4feSMatthias Ringwald         uint8_t character = demo_text[demo_pos++];
353a4bfc4feSMatthias Ringwald         if (demo_text[demo_pos] == 0){
354a4bfc4feSMatthias Ringwald             demo_pos = 0;
355a4bfc4feSMatthias Ringwald         }
356a4bfc4feSMatthias Ringwald 
357a4bfc4feSMatthias Ringwald         // get keycode and send
358a4bfc4feSMatthias Ringwald         uint8_t modifier;
359a4bfc4feSMatthias Ringwald         uint8_t keycode;
360a4bfc4feSMatthias Ringwald         int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
361a4bfc4feSMatthias Ringwald         if (found){
362a4bfc4feSMatthias Ringwald             printf("%c\n", character);
363a4bfc4feSMatthias Ringwald             send_key(modifier, keycode);
364a4bfc4feSMatthias Ringwald             send_keyup = 1;
365a4bfc4feSMatthias Ringwald         }
366a4bfc4feSMatthias Ringwald     }
367a4bfc4feSMatthias Ringwald 
368a4bfc4feSMatthias Ringwald     // set next timer
369a4bfc4feSMatthias Ringwald     btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS);
370a4bfc4feSMatthias Ringwald     btstack_run_loop_add_timer(ts);
371a4bfc4feSMatthias Ringwald }
372a4bfc4feSMatthias Ringwald 
373a4bfc4feSMatthias Ringwald static void hid_embedded_start_typing(void){
374a4bfc4feSMatthias Ringwald     printf("Start typing..\n");
375a4bfc4feSMatthias Ringwald 
376a4bfc4feSMatthias Ringwald     demo_pos = 0;
377a4bfc4feSMatthias Ringwald     // set one-shot timer
378a4bfc4feSMatthias Ringwald     typing_timer.process = &typing_timer_handler;
379a4bfc4feSMatthias Ringwald     btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS);
380a4bfc4feSMatthias Ringwald     btstack_run_loop_add_timer(&typing_timer);
381a4bfc4feSMatthias Ringwald }
382a4bfc4feSMatthias Ringwald 
383a4bfc4feSMatthias Ringwald #endif
384a4bfc4feSMatthias Ringwald 
385a4bfc4feSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
386a4bfc4feSMatthias Ringwald     UNUSED(channel);
387a4bfc4feSMatthias Ringwald     UNUSED(size);
388a4bfc4feSMatthias Ringwald 
389a4bfc4feSMatthias Ringwald     switch (packet_type) {
390a4bfc4feSMatthias Ringwald         case HCI_EVENT_PACKET:
391a4bfc4feSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
392a4bfc4feSMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
393*d241e50bSMatthias Ringwald                     con_handle = HCI_CON_HANDLE_INVALID;
394a4bfc4feSMatthias Ringwald                     printf("Disconnected\n");
395a4bfc4feSMatthias Ringwald                     break;
396a4bfc4feSMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
397a4bfc4feSMatthias Ringwald                     printf("Just Works requested\n");
398a4bfc4feSMatthias Ringwald                     sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
399a4bfc4feSMatthias Ringwald                     break;
400a4bfc4feSMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
401a4bfc4feSMatthias Ringwald                     printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
402a4bfc4feSMatthias Ringwald                     sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
403a4bfc4feSMatthias Ringwald                     break;
404a4bfc4feSMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
405a4bfc4feSMatthias Ringwald                     printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
406a4bfc4feSMatthias Ringwald                     break;
407a4bfc4feSMatthias Ringwald                 case HCI_EVENT_HIDS_META:
408a4bfc4feSMatthias Ringwald                     switch (hci_event_hids_meta_get_subevent_code(packet)){
409a4bfc4feSMatthias Ringwald                         case HIDS_SUBEVENT_INPUT_REPORT_ENABLE:
410a4bfc4feSMatthias Ringwald                             con_handle = hids_subevent_input_report_enable_get_con_handle(packet);
411a4bfc4feSMatthias Ringwald                             printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet));
412a4bfc4feSMatthias Ringwald #ifndef HAVE_BTSTACK_STDIN
413a4bfc4feSMatthias Ringwald                             hid_embedded_start_typing();
414a4bfc4feSMatthias Ringwald #endif
415a4bfc4feSMatthias Ringwald                             break;
416a4bfc4feSMatthias Ringwald                         case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE:
417a4bfc4feSMatthias Ringwald                             con_handle = hids_subevent_input_report_enable_get_con_handle(packet);
418a4bfc4feSMatthias Ringwald                             printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet));
419a4bfc4feSMatthias Ringwald                             break;
420a4bfc4feSMatthias Ringwald                         case HIDS_SUBEVENT_PROTOCOL_MODE:
421a4bfc4feSMatthias Ringwald                             printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot");
422a4bfc4feSMatthias Ringwald                             break;
423a4bfc4feSMatthias Ringwald                         case HIDS_SUBEVENT_CAN_SEND_NOW:
424a4bfc4feSMatthias Ringwald                             typing_can_send_now();
425a4bfc4feSMatthias Ringwald                             break;
426a4bfc4feSMatthias Ringwald                         default:
427a4bfc4feSMatthias Ringwald                             break;
428a4bfc4feSMatthias Ringwald                     }
429a4bfc4feSMatthias Ringwald             }
430a4bfc4feSMatthias Ringwald             break;
431a4bfc4feSMatthias Ringwald     }
432a4bfc4feSMatthias Ringwald }
433a4bfc4feSMatthias Ringwald 
434a4bfc4feSMatthias Ringwald int btstack_main(void);
435a4bfc4feSMatthias Ringwald int btstack_main(void)
436a4bfc4feSMatthias Ringwald {
437a4bfc4feSMatthias Ringwald     le_keyboard_setup();
438a4bfc4feSMatthias Ringwald 
439a4bfc4feSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
440a4bfc4feSMatthias Ringwald     btstack_ring_buffer_init(&ascii_input_buffer, ascii_input_storage, sizeof(ascii_input_storage));
441a4bfc4feSMatthias Ringwald     btstack_stdin_setup(stdin_process);
442a4bfc4feSMatthias Ringwald #endif
443a4bfc4feSMatthias Ringwald 
444a4bfc4feSMatthias Ringwald     // turn on!
445a4bfc4feSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
446a4bfc4feSMatthias Ringwald 
447a4bfc4feSMatthias Ringwald     return 0;
448a4bfc4feSMatthias Ringwald }
449