1 /* 2 * Copyright (C) 2020 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 BLUEKITCHEN 24 * GMBH 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_host_demo.c" 39 40 /* 41 * hog_host_demo.c 42 */ 43 44 /* EXAMPLE_START(hog_host_demo): HID Host LE 45 * 46 * @text This example implements a minimal HID-over-GATT Host. It scans for LE HID devices, connects to it, 47 * discovers the Characteristics relevant for the HID Service and enables Notifications on them. 48 * It then dumps all Boot Keyboard and Mouse Input Reports 49 */ 50 51 #include <inttypes.h> 52 #include <stdio.h> 53 #include <btstack_tlv.h> 54 55 #include "btstack_config.h" 56 #include "btstack.h" 57 58 // hog_host_demo.gatt contains the declaration of the provided GATT Services + Characteristics 59 // hog_host_demo.h contains the binary representation of gatt_browser.gatt 60 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py hog_host_demo.gatt hog_host_demo.h 61 // it needs to be regenerated when the GATT Database declared in gatt_browser.gatt file is modified 62 #include "hog_host_demo.h" 63 64 // TAG to store remote device address and type in TLV 65 #define TLV_TAG_HOGD ((((uint32_t) 'H') << 24 ) | (((uint32_t) 'O') << 16) | (((uint32_t) 'G') << 8) | 'D') 66 67 typedef struct { 68 bd_addr_t addr; 69 bd_addr_type_t addr_type; 70 } le_device_addr_t; 71 72 static enum { 73 W4_WORKING, 74 W4_HID_DEVICE_FOUND, 75 W4_CONNECTED, 76 W4_ENCRYPTED, 77 W4_HID_CLIENT_CONNECTED, 78 READY, 79 W4_TIMEOUT_THEN_SCAN, 80 W4_TIMEOUT_THEN_RECONNECT, 81 } app_state; 82 83 static le_device_addr_t remote_device; 84 static hci_con_handle_t connection_handle; 85 static uint16_t hids_cid; 86 static hid_protocol_mode_t protocol_mode = HID_PROTOCOL_MODE_REPORT; 87 88 // SDP 89 static uint8_t hid_descriptor_storage[500]; 90 91 // used to implement connection timeout and reconnect timer 92 static btstack_timer_source_t connection_timer; 93 94 // register for events from HCI/GAP and SM 95 static btstack_packet_callback_registration_t hci_event_callback_registration; 96 static btstack_packet_callback_registration_t sm_event_callback_registration; 97 98 // used to store remote device in TLV 99 static const btstack_tlv_t * btstack_tlv_singleton_impl; 100 static void * btstack_tlv_singleton_context; 101 102 // Simplified US Keyboard with Shift modifier 103 104 #define CHAR_ILLEGAL 0xff 105 #define CHAR_RETURN '\n' 106 #define CHAR_ESCAPE 27 107 #define CHAR_TAB '\t' 108 #define CHAR_BACKSPACE 0x7f 109 110 /** 111 * English (US) 112 */ 113 static const uint8_t keytable_us_none [] = { 114 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 115 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */ 116 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */ 117 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */ 118 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */ 119 CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 120 '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */ 121 '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 122 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 123 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 124 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 125 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 126 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 127 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 128 '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 129 '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */ 130 }; 131 132 static const uint8_t keytable_us_shift[] = { 133 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */ 134 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */ 135 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */ 136 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */ 137 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */ 138 CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */ 139 '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */ 140 '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */ 141 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */ 142 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */ 143 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */ 144 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */ 145 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */ 146 CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */ 147 '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */ 148 '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */ 149 }; 150 151 152 153 #define NUM_KEYS 6 154 static uint8_t last_keys[NUM_KEYS]; 155 static void hid_handle_input_report(uint8_t service_index, const uint8_t * report, uint16_t report_len){ 156 // check if HID Input Report 157 158 if (report_len < 1) return; 159 160 btstack_hid_parser_t parser; 161 162 switch (protocol_mode){ 163 case HID_PROTOCOL_MODE_BOOT: 164 btstack_hid_parser_init(&parser, 165 btstack_hid_get_boot_descriptor_data(), 166 btstack_hid_get_boot_descriptor_len(), 167 HID_REPORT_TYPE_INPUT, report, report_len); 168 break; 169 170 default: 171 btstack_hid_parser_init(&parser, 172 hids_client_descriptor_storage_get_descriptor_data(hids_cid, service_index), 173 hids_client_descriptor_storage_get_descriptor_len(hids_cid, service_index), 174 HID_REPORT_TYPE_INPUT, report, report_len); 175 break; 176 177 } 178 179 int shift = 0; 180 uint8_t new_keys[NUM_KEYS]; 181 memset(new_keys, 0, sizeof(new_keys)); 182 int new_keys_count = 0; 183 while (btstack_hid_parser_has_more(&parser)){ 184 uint16_t usage_page; 185 uint16_t usage; 186 int32_t value; 187 btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value); 188 if (usage_page != 0x07) continue; 189 switch (usage){ 190 case 0xe1: 191 case 0xe6: 192 if (value){ 193 shift = 1; 194 } 195 continue; 196 case 0x00: 197 continue; 198 default: 199 break; 200 } 201 if (usage >= sizeof(keytable_us_none)) continue; 202 203 // store new keys 204 new_keys[new_keys_count++] = (uint8_t) usage; 205 206 // check if usage was used last time (and ignore in that case) 207 int i; 208 for (i=0;i<NUM_KEYS;i++){ 209 if (usage == last_keys[i]){ 210 usage = 0; 211 } 212 } 213 if (usage == 0) continue; 214 215 uint8_t key; 216 if (shift){ 217 key = keytable_us_shift[usage]; 218 } else { 219 key = keytable_us_none[usage]; 220 } 221 if (key == CHAR_ILLEGAL) continue; 222 if (key == CHAR_BACKSPACE){ 223 printf("\b \b"); // go back one char, print space, go back one char again 224 continue; 225 } 226 printf("%c", key); 227 fflush(stdout); 228 } 229 memcpy(last_keys, new_keys, NUM_KEYS); 230 } 231 232 /** 233 * @section Test if advertisement contains HID UUID 234 * @param packet 235 * @param size 236 * @returns true if it does 237 */ 238 static bool adv_event_contains_hid_service(const uint8_t * packet){ 239 const uint8_t * ad_data = gap_event_advertising_report_get_data(packet); 240 uint8_t ad_len = gap_event_advertising_report_get_data_length(packet); 241 return ad_data_contains_uuid16(ad_len, ad_data, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE); 242 } 243 244 /** 245 * Start scanning 246 */ 247 static void hog_start_scan(void){ 248 printf("Scanning for LE HID devices...\n"); 249 app_state = W4_HID_DEVICE_FOUND; 250 // Passive scanning, 100% (scan interval = scan window) 251 gap_set_scan_parameters(0,48,48); 252 gap_start_scan(); 253 } 254 255 /** 256 * Handle timeout for outgoing connection 257 * @param ts 258 */ 259 static void hog_connection_timeout(btstack_timer_source_t * ts){ 260 UNUSED(ts); 261 printf("Timeout - abort connection\n"); 262 gap_connect_cancel(); 263 hog_start_scan(); 264 } 265 266 267 /** 268 * Connect to remote device but set timer for timeout 269 */ 270 static void hog_connect(void) { 271 // set timer 272 btstack_run_loop_set_timer(&connection_timer, 10000); 273 btstack_run_loop_set_timer_handler(&connection_timer, &hog_connection_timeout); 274 btstack_run_loop_add_timer(&connection_timer); 275 app_state = W4_CONNECTED; 276 gap_connect(remote_device.addr, remote_device.addr_type); 277 } 278 279 /** 280 * Handle timer event to trigger reconnect 281 * @param ts 282 */ 283 static void hog_reconnect_timeout(btstack_timer_source_t * ts){ 284 UNUSED(ts); 285 switch (app_state){ 286 case W4_TIMEOUT_THEN_RECONNECT: 287 hog_connect(); 288 break; 289 case W4_TIMEOUT_THEN_SCAN: 290 hog_start_scan(); 291 break; 292 default: 293 break; 294 } 295 } 296 297 /** 298 * Start connecting after boot up: connect to last used device if possible, start scan otherwise 299 */ 300 static void hog_start_connect(void){ 301 // check if we have a bonded device 302 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 303 if (btstack_tlv_singleton_impl){ 304 int len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, TLV_TAG_HOGD, (uint8_t *) &remote_device, sizeof(remote_device)); 305 if (len == sizeof(remote_device)){ 306 printf("Bonded, connect to device with %s address %s ...\n", remote_device.addr_type == 0 ? "public" : "random" , bd_addr_to_str(remote_device.addr)); 307 hog_connect(); 308 return; 309 } 310 } 311 // otherwise, scan for HID devices 312 hog_start_scan(); 313 } 314 315 /** 316 * In case of error, disconnect and start scanning again 317 */ 318 static void handle_outgoing_connection_error(void){ 319 printf("Error occurred, disconnect and start over\n"); 320 gap_disconnect(connection_handle); 321 hog_start_scan(); 322 } 323 324 /** 325 * Handle GATT Client Events dependent on current state 326 * 327 * @param packet_type 328 * @param channel 329 * @param packet 330 * @param size 331 */ 332 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 333 UNUSED(packet_type); 334 UNUSED(channel); 335 UNUSED(size); 336 337 uint8_t status; 338 339 if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META){ 340 return; 341 } 342 343 switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 344 case GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED: 345 status = gattservice_subevent_hid_service_connected_get_status(packet); 346 switch (status){ 347 case ERROR_CODE_SUCCESS: 348 printf("HID service client connected, found %d services\n", 349 gattservice_subevent_hid_service_connected_get_num_instances(packet)); 350 351 // store device as bonded 352 if (btstack_tlv_singleton_impl){ 353 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, TLV_TAG_HOGD, (const uint8_t *) &remote_device, sizeof(remote_device)); 354 } 355 // done 356 printf("Ready - please start typing or mousing..\n"); 357 app_state = READY; 358 break; 359 default: 360 printf("HID service client connection failed, status 0x%02x.\n", status); 361 handle_outgoing_connection_error(); 362 break; 363 } 364 break; 365 366 case GATTSERVICE_SUBEVENT_HID_SERVICE_DISCONNECTED: 367 printf("HID service client disconnected\n"); 368 hog_start_connect(); 369 break; 370 371 case GATTSERVICE_SUBEVENT_HID_REPORT: 372 hid_handle_input_report( 373 gattservice_subevent_hid_report_get_service_index(packet), 374 gattservice_subevent_hid_report_get_report(packet), 375 gattservice_subevent_hid_report_get_report_len(packet)); 376 break; 377 378 default: 379 break; 380 } 381 } 382 383 /* LISTING_START(packetHandler): Packet Handler */ 384 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 385 /* LISTING_PAUSE */ 386 UNUSED(channel); 387 UNUSED(size); 388 uint8_t event; 389 /* LISTING_RESUME */ 390 switch (packet_type) { 391 case HCI_EVENT_PACKET: 392 event = hci_event_packet_get_type(packet); 393 switch (event) { 394 case BTSTACK_EVENT_STATE: 395 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 396 btstack_assert(app_state == W4_WORKING); 397 398 hog_start_connect(); 399 break; 400 case GAP_EVENT_ADVERTISING_REPORT: 401 if (app_state != W4_HID_DEVICE_FOUND) break; 402 if (adv_event_contains_hid_service(packet) == false) break; 403 // stop scan 404 gap_stop_scan(); 405 // store remote device address and type 406 gap_event_advertising_report_get_address(packet, remote_device.addr); 407 remote_device.addr_type = gap_event_advertising_report_get_address_type(packet); 408 // connect 409 printf("Found, connect to device with %s address %s ...\n", remote_device.addr_type == 0 ? "public" : "random" , bd_addr_to_str(remote_device.addr)); 410 hog_connect(); 411 break; 412 case HCI_EVENT_DISCONNECTION_COMPLETE: 413 if (app_state != READY) break; 414 connection_handle = HCI_CON_HANDLE_INVALID; 415 switch (app_state){ 416 case READY: 417 printf("\nDisconnected, try to reconnect...\n"); 418 app_state = W4_TIMEOUT_THEN_RECONNECT; 419 break; 420 default: 421 printf("\nDisconnected, start over...\n"); 422 app_state = W4_TIMEOUT_THEN_SCAN; 423 break; 424 } 425 // set timer 426 btstack_run_loop_set_timer(&connection_timer, 100); 427 btstack_run_loop_set_timer_handler(&connection_timer, &hog_reconnect_timeout); 428 btstack_run_loop_add_timer(&connection_timer); 429 break; 430 case HCI_EVENT_META_GAP: 431 // wait for connection complete 432 if (hci_event_gap_meta_get_subevent_code(packet) != GAP_SUBEVENT_LE_CONNECTION_COMPLETE) break; 433 if (app_state != W4_CONNECTED) return; 434 btstack_run_loop_remove_timer(&connection_timer); 435 connection_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); 436 // request security 437 app_state = W4_ENCRYPTED; 438 sm_request_pairing(connection_handle); 439 break; 440 default: 441 break; 442 } 443 break; 444 default: 445 break; 446 } 447 } 448 /* LISTING_END */ 449 450 /* @section HCI packet handler 451 * 452 * @text The SM packet handler receives Security Manager Events required for pairing. 453 * It also receives events generated during Identity Resolving 454 * see Listing SMPacketHandler. 455 */ 456 457 /* LISTING_START(SMPacketHandler): Scanning and receiving advertisements */ 458 459 static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 460 UNUSED(channel); 461 UNUSED(size); 462 463 if (packet_type != HCI_EVENT_PACKET) return; 464 465 bool connect_to_service = false; 466 467 switch (hci_event_packet_get_type(packet)) { 468 case SM_EVENT_JUST_WORKS_REQUEST: 469 printf("Just works requested\n"); 470 sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 471 break; 472 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 473 printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 474 sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 475 break; 476 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 477 printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 478 break; 479 case SM_EVENT_PAIRING_COMPLETE: 480 switch (sm_event_pairing_complete_get_status(packet)){ 481 case ERROR_CODE_SUCCESS: 482 printf("Pairing complete, success\n"); 483 connect_to_service = true; 484 break; 485 case ERROR_CODE_CONNECTION_TIMEOUT: 486 printf("Pairing failed, timeout\n"); 487 break; 488 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: 489 printf("Pairing failed, disconnected\n"); 490 break; 491 case ERROR_CODE_AUTHENTICATION_FAILURE: 492 printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); 493 break; 494 default: 495 break; 496 } 497 break; 498 case SM_EVENT_REENCRYPTION_COMPLETE: 499 printf("Re-encryption complete, success\n"); 500 connect_to_service = true; 501 break; 502 default: 503 break; 504 } 505 506 if (connect_to_service){ 507 // continue - query primary services 508 printf("Search for HID service.\n"); 509 app_state = W4_HID_CLIENT_CONNECTED; 510 hids_client_connect(connection_handle, handle_gatt_client_event, protocol_mode, &hids_cid); 511 } 512 } 513 /* LISTING_END */ 514 515 int btstack_main(int argc, const char * argv[]); 516 int btstack_main(int argc, const char * argv[]){ 517 518 (void)argc; 519 (void)argv; 520 521 /* LISTING_START(HogBootHostSetup): HID-over-GATT Host Setup */ 522 523 l2cap_init(); 524 525 // setup SM: Display only 526 sm_init(); 527 sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 528 sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING); 529 530 // 531 gatt_client_init(); 532 533 // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android and iOS 534 att_server_init(profile_data, NULL, NULL); 535 536 hids_client_init(hid_descriptor_storage, sizeof(hid_descriptor_storage)); 537 538 // register for events from HCI 539 hci_event_callback_registration.callback = &packet_handler; 540 hci_add_event_handler(&hci_event_callback_registration); 541 542 // register for events from Security Manager 543 sm_event_callback_registration.callback = &sm_packet_handler; 544 sm_add_event_handler(&sm_event_callback_registration); 545 sm_set_authentication_requirements( SM_AUTHREQ_BONDING); 546 547 /* LISTING_END */ 548 549 // Disable stdout buffering 550 setvbuf(stdin, NULL, _IONBF, 0); 551 552 app_state = W4_WORKING; 553 554 // Turn on the device 555 hci_power_control(HCI_POWER_ON); 556 return 0; 557 } 558 559 /* EXAMPLE_END */ 560