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