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