/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define BTSTACK_FILE__ "hid_keyboard_demo.c" // ***************************************************************************** /* EXAMPLE_START(hid_keyboard_demo): HID Keyboard Classic * * @text This HID Device example demonstrates how to implement * an HID keyboard. Without a HAVE_BTSTACK_STDIN, a fixed demo text is sent * If HAVE_BTSTACK_STDIN is defined, you can type from the terminal */ // ***************************************************************************** #include #include #include #include #include #include "btstack.h" #ifdef HAVE_BTSTACK_STDIN #include "btstack_stdin.h" #endif // to enable demo text on POSIX systems // #undef HAVE_BTSTACK_STDIN // from USB HID Specification 1.1, Appendix B.1 const uint8_t hid_descriptor_keyboard_boot_mode[] = { 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x06, // Usage (Keyboard) 0xa1, 0x01, // Collection (Application) // Modifier byte 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x05, 0x07, // Usage Page (Key codes) 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) 0x81, 0x02, // Input (Data, Variable, Absolute) // Reserved byte 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x81, 0x03, // Input (Constant, Variable, Absolute) // LED report + padding 0x95, 0x05, // Report Count (5) 0x75, 0x01, // Report Size (1) 0x05, 0x08, // Usage Page (LEDs) 0x19, 0x01, // Usage Minimum (Num Lock) 0x29, 0x05, // Usage Maxium (Kana) 0x91, 0x02, // Output (Data, Variable, Absolute) 0x95, 0x01, // Report Count (1) 0x75, 0x03, // Report Size (3) 0x91, 0x03, // Output (Constant, Variable, Absolute) // Keycodes 0x95, 0x06, // Report Count (6) 0x75, 0x08, // Report Size (8) 0x15, 0x00, // Logical Minimum (0) 0x25, 0xff, // Logical Maximum (1) 0x05, 0x07, // Usage Page (Key codes) 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 0x29, 0xff, // Usage Maxium (Reserved) 0x81, 0x00, // Input (Data, Array) 0xc0, // End collection }; // #define CHAR_ILLEGAL 0xff #define CHAR_RETURN '\n' #define CHAR_ESCAPE 27 #define CHAR_TAB '\t' #define CHAR_BACKSPACE 0x7f // Simplified US Keyboard with Shift modifier /** * English (US) */ static const uint8_t keytable_us_none [] = { CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ }; static const uint8_t keytable_us_shift[] = { CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ }; // STATE static uint8_t hid_service_buffer[250]; static uint8_t device_id_sdp_service_buffer[100]; static const char hid_device_name[] = "BTstack HID Keyboard"; static btstack_packet_callback_registration_t hci_event_callback_registration; static uint16_t hid_cid; static bd_addr_t device_addr; static uint8_t hid_boot_device = 0; #ifdef HAVE_BTSTACK_STDIN static const char * device_addr_string = "BC:EC:5D:E6:15:03"; #endif static enum { APP_BOOTING, APP_NOT_CONNECTED, APP_CONNECTING, APP_CONNECTED } app_state = APP_BOOTING; // HID Keyboard lookup static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){ int i; for (i=0;i