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 } 228 memcpy(last_keys, new_keys, NUM_KEYS); 229 } 230 231 /** 232 * @section Test if advertisement contains HID UUID 233 * @param packet 234 * @param size 235 * @returns true if it does 236 */ 237 static bool adv_event_contains_hid_service(const uint8_t * packet){ 238 const uint8_t * ad_data = gap_event_advertising_report_get_data(packet); 239 uint8_t ad_len = gap_event_advertising_report_get_data_length(packet); 240 return ad_data_contains_uuid16(ad_len, ad_data, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE); 241 } 242 243 /** 244 * Start scanning 245 */ 246 static void hog_start_scan(void){ 247 printf("Scanning for LE HID devices...\n"); 248 app_state = W4_HID_DEVICE_FOUND; 249 // Passive scanning, 100% (scan interval = scan window) 250 gap_set_scan_parameters(0,48,48); 251 gap_start_scan(); 252 } 253 254 /** 255 * Handle timeout for outgoing connection 256 * @param ts 257 */ 258 static void hog_connection_timeout(btstack_timer_source_t * ts){ 259 UNUSED(ts); 260 printf("Timeout - abort connection\n"); 261 gap_connect_cancel(); 262 hog_start_scan(); 263 } 264 265 266 /** 267 * Connect to remote device but set timer for timeout 268 */ 269 static void hog_connect(void) { 270 // set timer 271 btstack_run_loop_set_timer(&connection_timer, 10000); 272 btstack_run_loop_set_timer_handler(&connection_timer, &hog_connection_timeout); 273 btstack_run_loop_add_timer(&connection_timer); 274 app_state = W4_CONNECTED; 275 gap_connect(remote_device.addr, remote_device.addr_type); 276 } 277 278 /** 279 * Handle timer event to trigger reconnect 280 * @param ts 281 */ 282 static void hog_reconnect_timeout(btstack_timer_source_t * ts){ 283 UNUSED(ts); 284 switch (app_state){ 285 case W4_TIMEOUT_THEN_RECONNECT: 286 hog_connect(); 287 break; 288 case W4_TIMEOUT_THEN_SCAN: 289 hog_start_scan(); 290 break; 291 default: 292 break; 293 } 294 } 295 296 /** 297 * Start connecting after boot up: connect to last used device if possible, start scan otherwise 298 */ 299 static void hog_start_connect(void){ 300 // check if we have a bonded device 301 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 302 if (btstack_tlv_singleton_impl){ 303 int len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, TLV_TAG_HOGD, (uint8_t *) &remote_device, sizeof(remote_device)); 304 if (len == sizeof(remote_device)){ 305 printf("Bonded, connect to device with %s address %s ...\n", remote_device.addr_type == 0 ? "public" : "random" , bd_addr_to_str(remote_device.addr)); 306 hog_connect(); 307 return; 308 } 309 } 310 // otherwise, scan for HID devices 311 hog_start_scan(); 312 } 313 314 /** 315 * In case of error, disconnect and start scanning again 316 */ 317 static void handle_outgoing_connection_error(void){ 318 printf("Error occurred, disconnect and start over\n"); 319 gap_disconnect(connection_handle); 320 hog_start_scan(); 321 } 322 323 /** 324 * Handle GATT Client Events dependent on current state 325 * 326 * @param packet_type 327 * @param channel 328 * @param packet 329 * @param size 330 */ 331 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 332 UNUSED(packet_type); 333 UNUSED(channel); 334 UNUSED(size); 335 336 uint8_t status; 337 338 if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META){ 339 return; 340 } 341 342 switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 343 case GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED: 344 status = gattservice_subevent_hid_service_connected_get_status(packet); 345 switch (status){ 346 case ERROR_CODE_SUCCESS: 347 printf("HID service client connected, found %d services\n", 348 gattservice_subevent_hid_service_connected_get_num_instances(packet)); 349 350 // store device as bonded 351 if (btstack_tlv_singleton_impl){ 352 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, TLV_TAG_HOGD, (const uint8_t *) &remote_device, sizeof(remote_device)); 353 } 354 // done 355 printf("Ready - please start typing or mousing..\n"); 356 app_state = READY; 357 break; 358 default: 359 printf("HID service client connection failed, status 0x%02x.\n", status); 360 handle_outgoing_connection_error(); 361 break; 362 } 363 break; 364 365 case GATTSERVICE_SUBEVENT_HID_SERVICE_DISCONNECTED: 366 printf("HID service client disconnected\n"); 367 hog_start_connect(); 368 break; 369 370 case GATTSERVICE_SUBEVENT_HID_REPORT: 371 hid_handle_input_report( 372 gattservice_subevent_hid_report_get_service_index(packet), 373 gattservice_subevent_hid_report_get_report(packet), 374 gattservice_subevent_hid_report_get_report_len(packet)); 375 break; 376 377 default: 378 break; 379 } 380 } 381 382 /* LISTING_START(packetHandler): Packet Handler */ 383 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 384 /* LISTING_PAUSE */ 385 UNUSED(channel); 386 UNUSED(size); 387 uint8_t event; 388 /* LISTING_RESUME */ 389 switch (packet_type) { 390 case HCI_EVENT_PACKET: 391 event = hci_event_packet_get_type(packet); 392 switch (event) { 393 case BTSTACK_EVENT_STATE: 394 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 395 btstack_assert(app_state == W4_WORKING); 396 397 hog_start_connect(); 398 break; 399 case GAP_EVENT_ADVERTISING_REPORT: 400 if (app_state != W4_HID_DEVICE_FOUND) break; 401 if (adv_event_contains_hid_service(packet) == false) break; 402 // stop scan 403 gap_stop_scan(); 404 // store remote device address and type 405 gap_event_advertising_report_get_address(packet, remote_device.addr); 406 remote_device.addr_type = gap_event_advertising_report_get_address_type(packet); 407 // connect 408 printf("Found, connect to device with %s address %s ...\n", remote_device.addr_type == 0 ? "public" : "random" , bd_addr_to_str(remote_device.addr)); 409 hog_connect(); 410 break; 411 case HCI_EVENT_DISCONNECTION_COMPLETE: 412 if (app_state != READY) break; 413 connection_handle = HCI_CON_HANDLE_INVALID; 414 switch (app_state){ 415 case READY: 416 printf("\nDisconnected, try to reconnect...\n"); 417 app_state = W4_TIMEOUT_THEN_RECONNECT; 418 break; 419 default: 420 printf("\nDisconnected, start over...\n"); 421 app_state = W4_TIMEOUT_THEN_SCAN; 422 break; 423 } 424 // set timer 425 btstack_run_loop_set_timer(&connection_timer, 100); 426 btstack_run_loop_set_timer_handler(&connection_timer, &hog_reconnect_timeout); 427 btstack_run_loop_add_timer(&connection_timer); 428 break; 429 case HCI_EVENT_META_GAP: 430 // wait for connection complete 431 if (hci_event_gap_meta_get_subevent_code(packet) != GAP_SUBEVENT_LE_CONNECTION_COMPLETE) break; 432 if (app_state != W4_CONNECTED) return; 433 btstack_run_loop_remove_timer(&connection_timer); 434 connection_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); 435 // request security 436 app_state = W4_ENCRYPTED; 437 sm_request_pairing(connection_handle); 438 break; 439 default: 440 break; 441 } 442 break; 443 default: 444 break; 445 } 446 } 447 /* LISTING_END */ 448 449 /* @section HCI packet handler 450 * 451 * @text The SM packet handler receives Security Manager Events required for pairing. 452 * It also receives events generated during Identity Resolving 453 * see Listing SMPacketHandler. 454 */ 455 456 /* LISTING_START(SMPacketHandler): Scanning and receiving advertisements */ 457 458 static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 459 UNUSED(channel); 460 UNUSED(size); 461 462 if (packet_type != HCI_EVENT_PACKET) return; 463 464 bool connect_to_service = false; 465 466 switch (hci_event_packet_get_type(packet)) { 467 case SM_EVENT_JUST_WORKS_REQUEST: 468 printf("Just works requested\n"); 469 sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 470 break; 471 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 472 printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 473 sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 474 break; 475 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 476 printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 477 break; 478 case SM_EVENT_PAIRING_COMPLETE: 479 switch (sm_event_pairing_complete_get_status(packet)){ 480 case ERROR_CODE_SUCCESS: 481 printf("Pairing complete, success\n"); 482 connect_to_service = true; 483 break; 484 case ERROR_CODE_CONNECTION_TIMEOUT: 485 printf("Pairing failed, timeout\n"); 486 break; 487 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: 488 printf("Pairing failed, disconnected\n"); 489 break; 490 case ERROR_CODE_AUTHENTICATION_FAILURE: 491 printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); 492 break; 493 default: 494 break; 495 } 496 break; 497 case SM_EVENT_REENCRYPTION_COMPLETE: 498 printf("Re-encryption complete, success\n"); 499 connect_to_service = true; 500 break; 501 default: 502 break; 503 } 504 505 if (connect_to_service){ 506 // continue - query primary services 507 printf("Search for HID service.\n"); 508 app_state = W4_HID_CLIENT_CONNECTED; 509 hids_client_connect(connection_handle, handle_gatt_client_event, protocol_mode, &hids_cid); 510 } 511 } 512 /* LISTING_END */ 513 514 int btstack_main(int argc, const char * argv[]); 515 int btstack_main(int argc, const char * argv[]){ 516 517 (void)argc; 518 (void)argv; 519 520 /* LISTING_START(HogBootHostSetup): HID-over-GATT Host Setup */ 521 522 l2cap_init(); 523 524 // setup SM: Display only 525 sm_init(); 526 sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 527 sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING); 528 529 // 530 gatt_client_init(); 531 532 // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android and iOS 533 att_server_init(profile_data, NULL, NULL); 534 535 hids_client_init(hid_descriptor_storage, sizeof(hid_descriptor_storage)); 536 537 // register for events from HCI 538 hci_event_callback_registration.callback = &packet_handler; 539 hci_add_event_handler(&hci_event_callback_registration); 540 541 // register for events from Security Manager 542 sm_event_callback_registration.callback = &sm_packet_handler; 543 sm_add_event_handler(&sm_event_callback_registration); 544 sm_set_authentication_requirements( SM_AUTHREQ_BONDING); 545 546 /* LISTING_END */ 547 548 // Disable stdout buffering 549 setvbuf(stdin, NULL, _IONBF, 0); 550 551 app_state = W4_WORKING; 552 553 // Turn on the device 554 hci_power_control(HCI_POWER_ON); 555 return 0; 556 } 557 558 /* EXAMPLE_END */ 559