xref: /btstack/example/hog_keyboard_demo.c (revision 7bbeb3ad8cec0c1816689843bf9383cf4c644ef8)
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 
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 
185a4bfc4feSMatthias Ringwald static void le_keyboard_setup(void){
186a4bfc4feSMatthias Ringwald 
187a4bfc4feSMatthias Ringwald     l2cap_init();
188a4bfc4feSMatthias Ringwald 
189a4bfc4feSMatthias Ringwald     // setup le device db
190a4bfc4feSMatthias Ringwald     le_device_db_init();
191a4bfc4feSMatthias Ringwald 
192a4bfc4feSMatthias Ringwald     // setup SM: Display only
193a4bfc4feSMatthias Ringwald     sm_init();
194a4bfc4feSMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
195a4bfc4feSMatthias Ringwald     sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
196a4bfc4feSMatthias Ringwald 
197a4bfc4feSMatthias Ringwald     // setup ATT server
198a4bfc4feSMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
199a4bfc4feSMatthias Ringwald 
200a4bfc4feSMatthias Ringwald     // setup battery service
201a4bfc4feSMatthias Ringwald     battery_service_server_init(battery);
202a4bfc4feSMatthias Ringwald 
203a4bfc4feSMatthias Ringwald     // setup device information service
204a4bfc4feSMatthias Ringwald     device_information_service_server_init();
205a4bfc4feSMatthias Ringwald 
206a4bfc4feSMatthias Ringwald     // setup HID Device service
207a4bfc4feSMatthias Ringwald     hids_device_init(0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode));
208a4bfc4feSMatthias Ringwald 
209a4bfc4feSMatthias Ringwald     // setup advertisements
210a4bfc4feSMatthias Ringwald     uint16_t adv_int_min = 0x0030;
211a4bfc4feSMatthias Ringwald     uint16_t adv_int_max = 0x0030;
212a4bfc4feSMatthias Ringwald     uint8_t adv_type = 0;
213a4bfc4feSMatthias Ringwald     bd_addr_t null_addr;
214a4bfc4feSMatthias Ringwald     memset(null_addr, 0, 6);
215a4bfc4feSMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
216a4bfc4feSMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
217a4bfc4feSMatthias Ringwald     gap_advertisements_enable(1);
218a4fe6467SMatthias Ringwald 
219a4fe6467SMatthias Ringwald     // register for HCI events
220a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
221a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
222a4fe6467SMatthias Ringwald 
223a4fe6467SMatthias Ringwald     // register for SM events
224a4fe6467SMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
225a4fe6467SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
226a4fe6467SMatthias Ringwald 
227a4fe6467SMatthias Ringwald     // register for HIDS
228a4fe6467SMatthias Ringwald     hids_device_register_packet_handler(packet_handler);
229a4bfc4feSMatthias Ringwald }
230a4bfc4feSMatthias Ringwald 
231a4bfc4feSMatthias Ringwald // HID Keyboard lookup
232a4bfc4feSMatthias Ringwald static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){
233a4bfc4feSMatthias Ringwald     int i;
234a4bfc4feSMatthias Ringwald     for (i=0;i<size;i++){
235a4bfc4feSMatthias Ringwald         if (table[i] != character) continue;
236a4bfc4feSMatthias Ringwald         *keycode = i;
237a4bfc4feSMatthias Ringwald         return 1;
238a4bfc4feSMatthias Ringwald     }
239a4bfc4feSMatthias Ringwald     return 0;
240a4bfc4feSMatthias Ringwald }
241a4bfc4feSMatthias Ringwald 
242a4bfc4feSMatthias Ringwald static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){
243a4bfc4feSMatthias Ringwald     int found;
244a4bfc4feSMatthias Ringwald     found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode);
245a4bfc4feSMatthias Ringwald     if (found) {
246a4bfc4feSMatthias Ringwald         *modifier = 0;  // none
247a4bfc4feSMatthias Ringwald         return 1;
248a4bfc4feSMatthias Ringwald     }
249a4bfc4feSMatthias Ringwald     found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode);
250a4bfc4feSMatthias Ringwald     if (found) {
251a4bfc4feSMatthias Ringwald         *modifier = 2;  // shift
252a4bfc4feSMatthias Ringwald         return 1;
253a4bfc4feSMatthias Ringwald     }
254a4bfc4feSMatthias Ringwald     return 0;
255a4bfc4feSMatthias Ringwald }
256a4bfc4feSMatthias Ringwald 
257a4bfc4feSMatthias Ringwald // HID Report sending
258a4bfc4feSMatthias Ringwald static void send_report(int modifier, int keycode){
259cf5ed549SMatthias Ringwald     uint8_t report[] = {  modifier, 0, keycode, 0, 0, 0, 0, 0};
26050304921SMatthias Ringwald     switch (protocol_mode){
26150304921SMatthias Ringwald         case 0:
26250304921SMatthias Ringwald             hids_device_send_boot_keyboard_input_report(con_handle, report, sizeof(report));
26350304921SMatthias Ringwald             break;
26450304921SMatthias Ringwald         case 1:
265a4bfc4feSMatthias Ringwald            hids_device_send_input_report(con_handle, report, sizeof(report));
26650304921SMatthias Ringwald            break;
26750304921SMatthias Ringwald         default:
26850304921SMatthias Ringwald             break;
26950304921SMatthias Ringwald     }
270a4bfc4feSMatthias Ringwald }
271a4bfc4feSMatthias Ringwald 
272a4bfc4feSMatthias Ringwald // Demo Application
273a4bfc4feSMatthias Ringwald 
274a4bfc4feSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
275a4bfc4feSMatthias Ringwald 
276a4bfc4feSMatthias Ringwald // On systems with STDIN, we can directly type on the console
277a4bfc4feSMatthias Ringwald static enum {
278a4bfc4feSMatthias Ringwald     W4_INPUT,
279a4bfc4feSMatthias Ringwald     W4_CAN_SEND_FROM_BUFFER,
280a4bfc4feSMatthias Ringwald     W4_CAN_SEND_KEY_UP,
281a4bfc4feSMatthias Ringwald } state;
282a4bfc4feSMatthias Ringwald 
283a4bfc4feSMatthias Ringwald // Buffer for 20 characters
284a4bfc4feSMatthias Ringwald static uint8_t ascii_input_storage[20];
285a4bfc4feSMatthias Ringwald static btstack_ring_buffer_t ascii_input_buffer;
286a4bfc4feSMatthias Ringwald 
287a4bfc4feSMatthias Ringwald static void typing_can_send_now(void){
288a4bfc4feSMatthias Ringwald     switch (state){
289a4bfc4feSMatthias Ringwald         case W4_CAN_SEND_FROM_BUFFER:
290a4bfc4feSMatthias Ringwald             while (1){
291a4bfc4feSMatthias Ringwald                 uint8_t c;
292a4bfc4feSMatthias Ringwald                 uint32_t num_bytes_read;
293a4bfc4feSMatthias Ringwald 
294a4bfc4feSMatthias Ringwald                 btstack_ring_buffer_read(&ascii_input_buffer, &c, 1, &num_bytes_read);
295a4bfc4feSMatthias Ringwald                 if (num_bytes_read == 0){
296a4bfc4feSMatthias Ringwald                     state = W4_INPUT;
297a4bfc4feSMatthias Ringwald                     break;
298a4bfc4feSMatthias Ringwald                 }
299a4bfc4feSMatthias Ringwald 
300a4bfc4feSMatthias Ringwald                 uint8_t modifier;
301a4bfc4feSMatthias Ringwald                 uint8_t keycode;
302a4bfc4feSMatthias Ringwald                 int found = keycode_and_modifer_us_for_character(c, &keycode, &modifier);
303a4bfc4feSMatthias Ringwald                 if (!found) continue;
304a4bfc4feSMatthias Ringwald 
305381856a5SMatthias Ringwald                 printf("sending: %c\n", c);
306381856a5SMatthias Ringwald 
307a4bfc4feSMatthias Ringwald                 send_report(modifier, keycode);
308a4bfc4feSMatthias Ringwald                 state = W4_CAN_SEND_KEY_UP;
309a4bfc4feSMatthias Ringwald                 hids_device_request_can_send_now_event(con_handle);
310a4bfc4feSMatthias Ringwald                 break;
311a4bfc4feSMatthias Ringwald             }
312a4bfc4feSMatthias Ringwald             break;
313a4bfc4feSMatthias Ringwald         case W4_CAN_SEND_KEY_UP:
314a4bfc4feSMatthias Ringwald             send_report(0, 0);
315a4bfc4feSMatthias Ringwald             if (btstack_ring_buffer_bytes_available(&ascii_input_buffer)){
316a4bfc4feSMatthias Ringwald                 state = W4_CAN_SEND_FROM_BUFFER;
317a4bfc4feSMatthias Ringwald                 hids_device_request_can_send_now_event(con_handle);
318a4bfc4feSMatthias Ringwald             } else {
319a4bfc4feSMatthias Ringwald                 state = W4_INPUT;
320a4bfc4feSMatthias Ringwald             }
321a4bfc4feSMatthias Ringwald             break;
322a4bfc4feSMatthias Ringwald         default:
323a4bfc4feSMatthias Ringwald             break;
324a4bfc4feSMatthias Ringwald     }
325a4bfc4feSMatthias Ringwald }
326a4bfc4feSMatthias Ringwald 
327a4bfc4feSMatthias Ringwald static void stdin_process(char character){
328a4bfc4feSMatthias Ringwald     uint8_t c = character;
329a4bfc4feSMatthias Ringwald     btstack_ring_buffer_write(&ascii_input_buffer, &c, 1);
330a4bfc4feSMatthias Ringwald     // start sending
331381856a5SMatthias Ringwald     if (state == W4_INPUT && con_handle != HCI_CON_HANDLE_INVALID){
332a4bfc4feSMatthias Ringwald         state = W4_CAN_SEND_FROM_BUFFER;
333a4bfc4feSMatthias Ringwald         hids_device_request_can_send_now_event(con_handle);
334a4bfc4feSMatthias Ringwald     }
335a4bfc4feSMatthias Ringwald }
336a4bfc4feSMatthias Ringwald 
337a4bfc4feSMatthias Ringwald #else
338a4bfc4feSMatthias Ringwald 
339a4bfc4feSMatthias Ringwald // On embedded systems, send constant demo text with fixed period
340a4bfc4feSMatthias Ringwald 
341a4bfc4feSMatthias Ringwald #define TYPING_PERIOD_MS 50
342a4bfc4feSMatthias Ringwald static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n";
343a4bfc4feSMatthias Ringwald 
344a4bfc4feSMatthias Ringwald static int demo_pos;
345a4bfc4feSMatthias Ringwald static btstack_timer_source_t typing_timer;
346a4bfc4feSMatthias Ringwald 
347a4bfc4feSMatthias Ringwald static int send_keycode;
348a4bfc4feSMatthias Ringwald static int send_modifier;
349a4bfc4feSMatthias Ringwald static int send_keyup;
350a4bfc4feSMatthias Ringwald 
351a4bfc4feSMatthias Ringwald static void send_key(int modifier, int keycode){
352a4bfc4feSMatthias Ringwald     send_keycode = keycode;
353a4bfc4feSMatthias Ringwald     send_modifier = modifier;
354a4bfc4feSMatthias Ringwald     hids_device_request_can_send_now_event(con_handle);
355a4bfc4feSMatthias Ringwald }
356a4bfc4feSMatthias Ringwald 
357a4bfc4feSMatthias Ringwald static void typing_can_send_now(void){
358a4bfc4feSMatthias Ringwald    send_report(send_modifier, send_keycode);
359a4bfc4feSMatthias Ringwald }
360a4bfc4feSMatthias Ringwald 
361a4bfc4feSMatthias Ringwald static void typing_timer_handler(btstack_timer_source_t * ts){
362a4bfc4feSMatthias Ringwald 
363a4bfc4feSMatthias Ringwald     if (send_keyup){
364a4bfc4feSMatthias Ringwald         // just send key up
365a4bfc4feSMatthias Ringwald         send_keyup = 0;
366a4bfc4feSMatthias Ringwald         send_key(0, 0);
367a4bfc4feSMatthias Ringwald     } else {
368a4bfc4feSMatthias Ringwald         // get next character
369a4bfc4feSMatthias Ringwald         uint8_t character = demo_text[demo_pos++];
370a4bfc4feSMatthias Ringwald         if (demo_text[demo_pos] == 0){
371a4bfc4feSMatthias Ringwald             demo_pos = 0;
372a4bfc4feSMatthias Ringwald         }
373a4bfc4feSMatthias Ringwald 
374a4bfc4feSMatthias Ringwald         // get keycode and send
375a4bfc4feSMatthias Ringwald         uint8_t modifier;
376a4bfc4feSMatthias Ringwald         uint8_t keycode;
377a4bfc4feSMatthias Ringwald         int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
378a4bfc4feSMatthias Ringwald         if (found){
379a4bfc4feSMatthias Ringwald             printf("%c\n", character);
380a4bfc4feSMatthias Ringwald             send_key(modifier, keycode);
381a4bfc4feSMatthias Ringwald             send_keyup = 1;
382a4bfc4feSMatthias Ringwald         }
383a4bfc4feSMatthias Ringwald     }
384a4bfc4feSMatthias Ringwald 
385a4bfc4feSMatthias Ringwald     // set next timer
386a4bfc4feSMatthias Ringwald     btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS);
387a4bfc4feSMatthias Ringwald     btstack_run_loop_add_timer(ts);
388a4bfc4feSMatthias Ringwald }
389a4bfc4feSMatthias Ringwald 
390a4bfc4feSMatthias Ringwald static void hid_embedded_start_typing(void){
391a4bfc4feSMatthias Ringwald     printf("Start typing..\n");
392a4bfc4feSMatthias Ringwald 
393a4bfc4feSMatthias Ringwald     demo_pos = 0;
394a4bfc4feSMatthias Ringwald     // set one-shot timer
395a4bfc4feSMatthias Ringwald     typing_timer.process = &typing_timer_handler;
396a4bfc4feSMatthias Ringwald     btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS);
397a4bfc4feSMatthias Ringwald     btstack_run_loop_add_timer(&typing_timer);
398a4bfc4feSMatthias Ringwald }
399a4bfc4feSMatthias Ringwald 
400a4bfc4feSMatthias Ringwald #endif
401a4bfc4feSMatthias Ringwald 
402a4bfc4feSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
403a4bfc4feSMatthias Ringwald     UNUSED(channel);
404a4bfc4feSMatthias Ringwald     UNUSED(size);
405a4bfc4feSMatthias Ringwald 
406*7bbeb3adSMilanka Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
407*7bbeb3adSMilanka Ringwald 
408a4bfc4feSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
409a4bfc4feSMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
410d241e50bSMatthias Ringwald             con_handle = HCI_CON_HANDLE_INVALID;
411a4bfc4feSMatthias Ringwald             printf("Disconnected\n");
412a4bfc4feSMatthias Ringwald             break;
413a4bfc4feSMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
414a4bfc4feSMatthias Ringwald             printf("Just Works requested\n");
415a4bfc4feSMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
416a4bfc4feSMatthias Ringwald             break;
417a4bfc4feSMatthias Ringwald         case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
418a4bfc4feSMatthias Ringwald             printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
419a4bfc4feSMatthias Ringwald             sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
420a4bfc4feSMatthias Ringwald             break;
421a4bfc4feSMatthias Ringwald         case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
422a4bfc4feSMatthias Ringwald             printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
423a4bfc4feSMatthias Ringwald             break;
424a4bfc4feSMatthias Ringwald         case HCI_EVENT_HIDS_META:
425a4bfc4feSMatthias Ringwald             switch (hci_event_hids_meta_get_subevent_code(packet)){
426a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_INPUT_REPORT_ENABLE:
427a4bfc4feSMatthias Ringwald                     con_handle = hids_subevent_input_report_enable_get_con_handle(packet);
428a4bfc4feSMatthias Ringwald                     printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet));
429a4bfc4feSMatthias Ringwald #ifndef HAVE_BTSTACK_STDIN
430a4bfc4feSMatthias Ringwald                     hid_embedded_start_typing();
431a4bfc4feSMatthias Ringwald #endif
432a4bfc4feSMatthias Ringwald                     break;
433a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE:
43431eaaa99SMatthias Ringwald                     con_handle = hids_subevent_boot_keyboard_input_report_enable_get_con_handle(packet);
435a4bfc4feSMatthias Ringwald                     printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet));
436a4bfc4feSMatthias Ringwald                     break;
437a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_PROTOCOL_MODE:
43850304921SMatthias Ringwald                     protocol_mode = hids_subevent_protocol_mode_get_protocol_mode(packet);
439a4bfc4feSMatthias Ringwald                     printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot");
440a4bfc4feSMatthias Ringwald                     break;
441a4bfc4feSMatthias Ringwald                 case HIDS_SUBEVENT_CAN_SEND_NOW:
442a4bfc4feSMatthias Ringwald                     typing_can_send_now();
443a4bfc4feSMatthias Ringwald                     break;
444a4bfc4feSMatthias Ringwald                 default:
445a4bfc4feSMatthias Ringwald                     break;
446a4bfc4feSMatthias Ringwald             }
447*7bbeb3adSMilanka Ringwald             break;
448*7bbeb3adSMilanka Ringwald 
449*7bbeb3adSMilanka Ringwald         default:
450a4bfc4feSMatthias Ringwald             break;
451a4bfc4feSMatthias Ringwald     }
452a4bfc4feSMatthias Ringwald }
453a4bfc4feSMatthias Ringwald 
454a4bfc4feSMatthias Ringwald int btstack_main(void);
455a4bfc4feSMatthias Ringwald int btstack_main(void)
456a4bfc4feSMatthias Ringwald {
457a4bfc4feSMatthias Ringwald     le_keyboard_setup();
458a4bfc4feSMatthias Ringwald 
459a4bfc4feSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
460a4bfc4feSMatthias Ringwald     btstack_ring_buffer_init(&ascii_input_buffer, ascii_input_storage, sizeof(ascii_input_storage));
461a4bfc4feSMatthias Ringwald     btstack_stdin_setup(stdin_process);
462a4bfc4feSMatthias Ringwald #endif
463a4bfc4feSMatthias Ringwald 
464a4bfc4feSMatthias Ringwald     // turn on!
465a4bfc4feSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
466a4bfc4feSMatthias Ringwald 
467a4bfc4feSMatthias Ringwald     return 0;
468a4bfc4feSMatthias Ringwald }
4694dc98401SMilanka Ringwald /* EXAMPLE_END */
470