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