1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "hog_keyboard_demo.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(hog_keyboard_demo): HID-over-GATT Keyboard 42 */ 43 // ***************************************************************************** 44 45 #include <stdint.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <inttypes.h> 50 51 #include "hog_keyboard_demo.h" 52 53 #include "btstack.h" 54 55 #include "ble/gatt-service/battery_service_server.h" 56 #include "ble/gatt-service/device_information_service_server.h" 57 #include "ble/gatt-service/hids_device.h" 58 59 // from USB HID Specification 1.1, Appendix B.1 60 const uint8_t hid_descriptor_keyboard_boot_mode[] = { 61 62 0x05, 0x01, // Usage Page (Generic Desktop) 63 0x09, 0x06, // Usage (Keyboard) 64 0xa1, 0x01, // Collection (Application) 65 66 0x85, 0x01, // Report ID 1 67 68 // Modifier byte 69 70 0x75, 0x01, // Report Size (1) 71 0x95, 0x08, // Report Count (8) 72 0x05, 0x07, // Usage Page (Key codes) 73 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 74 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI) 75 0x15, 0x00, // Logical Minimum (0) 76 0x25, 0x01, // Logical Maximum (1) 77 0x81, 0x02, // Input (Data, Variable, Absolute) 78 79 // Reserved byte 80 81 0x75, 0x01, // Report Size (1) 82 0x95, 0x08, // Report Count (8) 83 0x81, 0x03, // Input (Constant, Variable, Absolute) 84 85 // LED report + padding 86 87 0x95, 0x05, // Report Count (5) 88 0x75, 0x01, // Report Size (1) 89 0x05, 0x08, // Usage Page (LEDs) 90 0x19, 0x01, // Usage Minimum (Num Lock) 91 0x29, 0x05, // Usage Maxium (Kana) 92 0x91, 0x02, // Output (Data, Variable, Absolute) 93 94 0x95, 0x01, // Report Count (1) 95 0x75, 0x03, // Report Size (3) 96 0x91, 0x03, // Output (Constant, Variable, Absolute) 97 98 // Keycodes 99 100 0x95, 0x06, // Report Count (6) 101 0x75, 0x08, // Report Size (8) 102 0x15, 0x00, // Logical Minimum (0) 103 0x25, 0xff, // Logical Maximum (1) 104 0x05, 0x07, // Usage Page (Key codes) 105 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 106 0x29, 0xff, // Usage Maxium (Reserved) 107 0x81, 0x00, // Input (Data, Array) 108 109 0xc0, // End collection 110 }; 111 112 113 114 // 115 #define CHAR_ILLEGAL 0xff 116 #define CHAR_RETURN '\n' 117 #define CHAR_ESCAPE 27 118 #define CHAR_TAB '\t' 119 #define CHAR_BACKSPACE 0x7f 120 121 // Simplified US Keyboard with Shift modifier 122 123 /** 124 * English (US) 125 */ 126 static const uint8_t keytable_us_none [] = { 127 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 128 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 129 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 130 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 131 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 132 CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 133 '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 134 '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 135 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 136 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 137 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 138 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 139 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 140 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 141 '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 142 '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 143 }; 144 145 static const uint8_t keytable_us_shift[] = { 146 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 147 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 148 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 149 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 150 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 151 CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 152 '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 153 '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 154 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 155 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 156 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 157 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 158 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 159 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 160 '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 161 '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 162 }; 163 164 // static btstack_timer_source_t heartbeat; 165 static btstack_packet_callback_registration_t hci_event_callback_registration; 166 static btstack_packet_callback_registration_t sm_event_callback_registration; 167 static uint8_t battery = 100; 168 static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID; 169 170 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 171 172 const uint8_t adv_data[] = { 173 // Flags general discoverable, BR/EDR not supported 174 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 175 // Name 176 0x0d, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'H', 'I', 'D', ' ', 'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd', 177 // 16-bit Service UUIDs 178 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 // Appearance HID - Keyboard (Category 15, Sub-Category 1) 180 0x03, BLUETOOTH_DATA_TYPE_APPEARANCE, 0xC1, 0x03, 181 }; 182 const uint8_t adv_data_len = sizeof(adv_data); 183 184 static void le_keyboard_setup(void){ 185 186 l2cap_init(); 187 188 // setup le device db 189 le_device_db_init(); 190 191 // setup SM: Display only 192 sm_init(); 193 sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 194 sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING); 195 196 // setup ATT server 197 att_server_init(profile_data, NULL, NULL); 198 199 // setup battery service 200 battery_service_server_init(battery); 201 202 // setup device information service 203 device_information_service_server_init(); 204 205 // setup HID Device service 206 hids_device_init(0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode)); 207 208 // setup advertisements 209 uint16_t adv_int_min = 0x0030; 210 uint16_t adv_int_max = 0x0030; 211 uint8_t adv_type = 0; 212 bd_addr_t null_addr; 213 memset(null_addr, 0, 6); 214 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 215 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 216 gap_advertisements_enable(1); 217 218 // register for HCI events 219 hci_event_callback_registration.callback = &packet_handler; 220 hci_add_event_handler(&hci_event_callback_registration); 221 222 // register for SM events 223 sm_event_callback_registration.callback = &packet_handler; 224 sm_add_event_handler(&sm_event_callback_registration); 225 226 // register for HIDS 227 hids_device_register_packet_handler(packet_handler); 228 } 229 230 // HID Keyboard lookup 231 static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){ 232 int i; 233 for (i=0;i<size;i++){ 234 if (table[i] != character) continue; 235 *keycode = i; 236 return 1; 237 } 238 return 0; 239 } 240 241 static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){ 242 int found; 243 found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode); 244 if (found) { 245 *modifier = 0; // none 246 return 1; 247 } 248 found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode); 249 if (found) { 250 *modifier = 2; // shift 251 return 1; 252 } 253 return 0; 254 } 255 256 // HID Report sending 257 258 static void send_report(int modifier, int keycode){ 259 uint8_t report[] = { /* 0xa1, */ modifier, 0, 0, keycode, 0, 0, 0, 0, 0}; 260 hids_device_send_input_report(con_handle, report, sizeof(report)); 261 } 262 263 // Demo Application 264 265 #ifdef HAVE_BTSTACK_STDIN 266 267 // On systems with STDIN, we can directly type on the console 268 static enum { 269 W4_INPUT, 270 W4_CAN_SEND_FROM_BUFFER, 271 W4_CAN_SEND_KEY_UP, 272 } state; 273 274 // Buffer for 20 characters 275 static uint8_t ascii_input_storage[20]; 276 static btstack_ring_buffer_t ascii_input_buffer; 277 278 static void typing_can_send_now(void){ 279 switch (state){ 280 case W4_CAN_SEND_FROM_BUFFER: 281 while (1){ 282 uint8_t c; 283 uint32_t num_bytes_read; 284 285 btstack_ring_buffer_read(&ascii_input_buffer, &c, 1, &num_bytes_read); 286 if (num_bytes_read == 0){ 287 state = W4_INPUT; 288 break; 289 } 290 291 uint8_t modifier; 292 uint8_t keycode; 293 int found = keycode_and_modifer_us_for_character(c, &keycode, &modifier); 294 if (!found) continue; 295 296 printf("sending: %c\n", c); 297 298 send_report(modifier, keycode); 299 state = W4_CAN_SEND_KEY_UP; 300 hids_device_request_can_send_now_event(con_handle); 301 break; 302 } 303 break; 304 case W4_CAN_SEND_KEY_UP: 305 send_report(0, 0); 306 if (btstack_ring_buffer_bytes_available(&ascii_input_buffer)){ 307 state = W4_CAN_SEND_FROM_BUFFER; 308 hids_device_request_can_send_now_event(con_handle); 309 } else { 310 state = W4_INPUT; 311 } 312 break; 313 default: 314 break; 315 } 316 } 317 318 static void stdin_process(char character){ 319 uint8_t c = character; 320 btstack_ring_buffer_write(&ascii_input_buffer, &c, 1); 321 // start sending 322 if (state == W4_INPUT && con_handle != HCI_CON_HANDLE_INVALID){ 323 state = W4_CAN_SEND_FROM_BUFFER; 324 hids_device_request_can_send_now_event(con_handle); 325 } 326 } 327 328 #else 329 330 // On embedded systems, send constant demo text with fixed period 331 332 #define TYPING_PERIOD_MS 50 333 static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n"; 334 335 static int demo_pos; 336 static btstack_timer_source_t typing_timer; 337 338 static int send_keycode; 339 static int send_modifier; 340 static int send_keyup; 341 342 static void send_key(int modifier, int keycode){ 343 send_keycode = keycode; 344 send_modifier = modifier; 345 hids_device_request_can_send_now_event(con_handle); 346 } 347 348 static void typing_can_send_now(void){ 349 send_report(send_modifier, send_keycode); 350 } 351 352 static void typing_timer_handler(btstack_timer_source_t * ts){ 353 354 if (send_keyup){ 355 // just send key up 356 send_keyup = 0; 357 send_key(0, 0); 358 } else { 359 // get next character 360 uint8_t character = demo_text[demo_pos++]; 361 if (demo_text[demo_pos] == 0){ 362 demo_pos = 0; 363 } 364 365 // get keycode and send 366 uint8_t modifier; 367 uint8_t keycode; 368 int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier); 369 if (found){ 370 printf("%c\n", character); 371 send_key(modifier, keycode); 372 send_keyup = 1; 373 } 374 } 375 376 // set next timer 377 btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS); 378 btstack_run_loop_add_timer(ts); 379 } 380 381 static void hid_embedded_start_typing(void){ 382 printf("Start typing..\n"); 383 384 demo_pos = 0; 385 // set one-shot timer 386 typing_timer.process = &typing_timer_handler; 387 btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS); 388 btstack_run_loop_add_timer(&typing_timer); 389 } 390 391 #endif 392 393 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 394 UNUSED(channel); 395 UNUSED(size); 396 397 switch (packet_type) { 398 case HCI_EVENT_PACKET: 399 switch (hci_event_packet_get_type(packet)) { 400 case HCI_EVENT_DISCONNECTION_COMPLETE: 401 con_handle = HCI_CON_HANDLE_INVALID; 402 printf("Disconnected\n"); 403 break; 404 case SM_EVENT_JUST_WORKS_REQUEST: 405 printf("Just Works requested\n"); 406 sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 407 break; 408 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 409 printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 410 sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 411 break; 412 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 413 printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 414 break; 415 case HCI_EVENT_HIDS_META: 416 switch (hci_event_hids_meta_get_subevent_code(packet)){ 417 case HIDS_SUBEVENT_INPUT_REPORT_ENABLE: 418 con_handle = hids_subevent_input_report_enable_get_con_handle(packet); 419 printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet)); 420 #ifndef HAVE_BTSTACK_STDIN 421 hid_embedded_start_typing(); 422 #endif 423 break; 424 case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE: 425 con_handle = hids_subevent_boot_keyboard_input_report_enable_get_con_handle(packet); 426 printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet)); 427 break; 428 case HIDS_SUBEVENT_PROTOCOL_MODE: 429 printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot"); 430 break; 431 case HIDS_SUBEVENT_CAN_SEND_NOW: 432 typing_can_send_now(); 433 break; 434 default: 435 break; 436 } 437 } 438 break; 439 } 440 } 441 442 int btstack_main(void); 443 int btstack_main(void) 444 { 445 le_keyboard_setup(); 446 447 #ifdef HAVE_BTSTACK_STDIN 448 btstack_ring_buffer_init(&ascii_input_buffer, ascii_input_storage, sizeof(ascii_input_storage)); 449 btstack_stdin_setup(stdin_process); 450 #endif 451 452 // turn on! 453 hci_power_control(HCI_POWER_ON); 454 455 return 0; 456 } 457 /* EXAMPLE_END */ 458