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 /* 39 * hci.h 40 * 41 * Created by Matthias Ringwald on 4/29/09. 42 * 43 */ 44 45 #ifndef __HCI_H 46 #define __HCI_H 47 48 #include "btstack_config.h" 49 50 #include "btstack_chipset.h" 51 #include "btstack_control.h" 52 #include "btstack_linked_list.h" 53 #include "btstack_util.h" 54 #include "classic/remote_device_db.h" 55 #include "hci_cmd.h" 56 #include "hci_transport.h" 57 58 #include <stdint.h> 59 #include <stdlib.h> 60 #include <stdarg.h> 61 62 #if defined __cplusplus 63 extern "C" { 64 #endif 65 66 // packet buffer sizes 67 // HCI_ACL_PAYLOAD_SIZE is configurable and defined in config.h 68 // addition byte in even to terminate remote name request with '\0' 69 #define HCI_EVENT_BUFFER_SIZE (HCI_EVENT_HEADER_SIZE + HCI_EVENT_PAYLOAD_SIZE + 1) 70 #define HCI_CMD_BUFFER_SIZE (HCI_CMD_HEADER_SIZE + HCI_CMD_PAYLOAD_SIZE) 71 #define HCI_ACL_BUFFER_SIZE (HCI_ACL_HEADER_SIZE + HCI_ACL_PAYLOAD_SIZE) 72 73 // size of hci buffers, big enough for command, event, or acl packet without H4 packet type 74 // @note cmd buffer is bigger than event buffer 75 #ifdef HCI_PACKET_BUFFER_SIZE 76 #if HCI_PACKET_BUFFER_SIZE < HCI_ACL_BUFFER_SIZE 77 #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_ACL_BUFFER_SIZE 78 #endif 79 #if HCI_PACKET_BUFFER_SIZE < HCI_CMD_BUFFER_SIZE 80 #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_CMD_BUFFER_SIZE 81 #endif 82 #else 83 #if HCI_ACL_BUFFER_SIZE > HCI_CMD_BUFFER_SIZE 84 #define HCI_PACKET_BUFFER_SIZE HCI_ACL_BUFFER_SIZE 85 #else 86 #define HCI_PACKET_BUFFER_SIZE HCI_CMD_BUFFER_SIZE 87 #endif 88 #endif 89 90 // additional pre-buffer space for packets to Bluetooth module, for now, used for HCI Transport H4 DMA 91 #define HCI_OUTGOING_PRE_BUFFER_SIZE 1 92 93 // BNEP may uncompress the IP Header by 16 bytes 94 #ifdef HAVE_BNEP 95 #define HCI_INCOMING_PRE_BUFFER_SIZE (16 - HCI_ACL_HEADER_SIZE - 4) 96 #endif 97 #ifndef HCI_INCOMING_PRE_BUFFER_SIZE 98 #define HCI_INCOMING_PRE_BUFFER_SIZE 0 99 #endif 100 101 // 102 #define IS_COMMAND(packet, command) (little_endian_read_16(packet,0) == command.opcode) 103 104 105 /** 106 * LE connection parameter update state 107 */ 108 109 typedef enum { 110 CON_PARAMETER_UPDATE_NONE, 111 CON_PARAMETER_UPDATE_SEND_REQUEST, 112 CON_PARAMETER_UPDATE_SEND_RESPONSE, 113 CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS, 114 CON_PARAMETER_UPDATE_DENY 115 } le_con_parameter_update_state_t; 116 117 typedef struct le_connection_parameter_range{ 118 uint16_t le_conn_interval_min; 119 uint16_t le_conn_interval_max; 120 uint16_t le_conn_latency_min; 121 uint16_t le_conn_latency_max; 122 uint16_t le_supervision_timeout_min; 123 uint16_t le_supervision_timeout_max; 124 } le_connection_parameter_range_t; 125 126 // Authentication flags 127 typedef enum { 128 AUTH_FLAGS_NONE = 0x0000, 129 RECV_LINK_KEY_REQUEST = 0x0001, 130 HANDLE_LINK_KEY_REQUEST = 0x0002, 131 SENT_LINK_KEY_REPLY = 0x0004, 132 SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008, 133 RECV_LINK_KEY_NOTIFICATION = 0x0010, 134 DENY_PIN_CODE_REQUEST = 0x0040, 135 RECV_IO_CAPABILITIES_REQUEST = 0x0080, 136 SEND_IO_CAPABILITIES_REPLY = 0x0100, 137 SEND_USER_CONFIRM_REPLY = 0x0200, 138 SEND_USER_PASSKEY_REPLY = 0x0400, 139 140 // pairing status 141 LEGACY_PAIRING_ACTIVE = 0x2000, 142 SSP_PAIRING_ACTIVE = 0x4000, 143 144 // connection status 145 CONNECTION_ENCRYPTED = 0x8000, 146 } hci_authentication_flags_t; 147 148 /** 149 * Connection State 150 */ 151 typedef enum { 152 SEND_CREATE_CONNECTION = 0, 153 SENT_CREATE_CONNECTION, 154 SEND_CANCEL_CONNECTION, 155 SENT_CANCEL_CONNECTION, 156 RECEIVED_CONNECTION_REQUEST, 157 ACCEPTED_CONNECTION_REQUEST, 158 REJECTED_CONNECTION_REQUEST, 159 OPEN, 160 SEND_DISCONNECT, 161 SENT_DISCONNECT, 162 RECEIVED_DISCONNECTION_COMPLETE 163 } CONNECTION_STATE; 164 165 // bonding flags 166 enum { 167 BONDING_REQUEST_REMOTE_FEATURES = 0x01, 168 BONDING_RECEIVED_REMOTE_FEATURES = 0x02, 169 BONDING_REMOTE_SUPPORTS_SSP = 0x04, 170 BONDING_DISCONNECT_SECURITY_BLOCK = 0x08, 171 BONDING_DISCONNECT_DEDICATED_DONE = 0x10, 172 BONDING_SEND_AUTHENTICATE_REQUEST = 0x20, 173 BONDING_SEND_ENCRYPTION_REQUEST = 0x40, 174 BONDING_DEDICATED = 0x80, 175 BONDING_EMIT_COMPLETE_ON_DISCONNECT = 0x100 176 }; 177 178 typedef enum { 179 BLUETOOTH_OFF = 1, 180 BLUETOOTH_ON, 181 BLUETOOTH_ACTIVE 182 } BLUETOOTH_STATE; 183 184 // le central scanning state 185 typedef enum { 186 LE_SCAN_IDLE, 187 LE_START_SCAN, 188 LE_SCANNING, 189 LE_STOP_SCAN, 190 } le_scanning_state_t; 191 192 typedef enum { 193 LE_CONNECTING_IDLE, 194 LE_CONNECTING_DIRECT, 195 LE_CONNECTING_WHITELIST, 196 } le_connecting_state_t; 197 198 // 199 // SM internal types and globals 200 // 201 202 typedef enum { 203 204 // general states 205 // state = 0 206 SM_GENERAL_IDLE, 207 SM_GENERAL_SEND_PAIRING_FAILED, 208 SM_GENERAL_TIMEOUT, // no other security messages are exchanged 209 210 // Phase 1: Pairing Feature Exchange 211 SM_PH1_W4_USER_RESPONSE, 212 213 // Phase 2: Authenticating and Encrypting 214 215 // get random number for use as TK Passkey if we show it 216 SM_PH2_GET_RANDOM_TK, 217 SM_PH2_W4_RANDOM_TK, 218 219 // get local random number for confirm c1 220 SM_PH2_C1_GET_RANDOM_A, 221 SM_PH2_C1_W4_RANDOM_A, 222 SM_PH2_C1_GET_RANDOM_B, 223 SM_PH2_C1_W4_RANDOM_B, 224 225 // calculate confirm value for local side 226 // state = 10 227 SM_PH2_C1_GET_ENC_A, 228 SM_PH2_C1_W4_ENC_A, 229 SM_PH2_C1_GET_ENC_B, 230 SM_PH2_C1_W4_ENC_B, 231 232 // calculate confirm value for remote side 233 SM_PH2_C1_GET_ENC_C, 234 SM_PH2_C1_W4_ENC_C, 235 SM_PH2_C1_GET_ENC_D, 236 SM_PH2_C1_W4_ENC_D, 237 238 SM_PH2_C1_SEND_PAIRING_CONFIRM, 239 SM_PH2_SEND_PAIRING_RANDOM, 240 241 // calc STK 242 // state = 20 243 SM_PH2_CALC_STK, 244 SM_PH2_W4_STK, 245 246 SM_PH2_W4_CONNECTION_ENCRYPTED, 247 248 // Phase 3: Transport Specific Key Distribution 249 // calculate DHK, Y, EDIV, and LTK 250 SM_PH3_GET_RANDOM, 251 SM_PH3_W4_RANDOM, 252 SM_PH3_GET_DIV, 253 SM_PH3_W4_DIV, 254 SM_PH3_Y_GET_ENC, 255 SM_PH3_Y_W4_ENC, 256 SM_PH3_LTK_GET_ENC, 257 // state = 30 258 SM_PH3_LTK_W4_ENC, 259 SM_PH3_CSRK_GET_ENC, 260 SM_PH3_CSRK_W4_ENC, 261 262 // exchange keys 263 SM_PH3_DISTRIBUTE_KEYS, 264 SM_PH3_RECEIVE_KEYS, 265 266 // RESPONDER ROLE 267 // state = 35 268 SM_RESPONDER_IDLE, 269 SM_RESPONDER_SEND_SECURITY_REQUEST, 270 SM_RESPONDER_PH0_RECEIVED_LTK, 271 SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY, 272 SM_RESPONDER_PH1_W4_PAIRING_REQUEST, 273 SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED, 274 SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE, 275 SM_RESPONDER_PH1_W4_PAIRING_CONFIRM, 276 SM_RESPONDER_PH2_W4_PAIRING_RANDOM, 277 SM_RESPONDER_PH2_W4_LTK_REQUEST, 278 SM_RESPONDER_PH2_SEND_LTK_REPLY, 279 280 // Phase 4: re-establish previously distributed LTK 281 // state == 46 282 SM_RESPONDER_PH4_Y_GET_ENC, 283 SM_RESPONDER_PH4_Y_W4_ENC, 284 SM_RESPONDER_PH4_LTK_GET_ENC, 285 SM_RESPONDER_PH4_LTK_W4_ENC, 286 SM_RESPONDER_PH4_SEND_LTK, 287 288 // INITITIATOR ROLE 289 // state = 51 290 SM_INITIATOR_CONNECTED, 291 SM_INITIATOR_PH0_HAS_LTK, 292 SM_INITIATOR_PH0_SEND_START_ENCRYPTION, 293 SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED, 294 SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST, 295 SM_INITIATOR_PH1_SEND_PAIRING_REQUEST, 296 SM_INITIATOR_PH1_W4_PAIRING_RESPONSE, 297 SM_INITIATOR_PH2_W4_PAIRING_CONFIRM, 298 SM_INITIATOR_PH2_W4_PAIRING_RANDOM, 299 SM_INITIATOR_PH3_SEND_START_ENCRYPTION, 300 301 } security_manager_state_t; 302 303 typedef enum { 304 IRK_LOOKUP_IDLE, 305 IRK_LOOKUP_W4_READY, 306 IRK_LOOKUP_STARTED, 307 IRK_LOOKUP_SUCCEEDED, 308 IRK_LOOKUP_FAILED 309 } irk_lookup_state_t; 310 311 // Authorization state 312 typedef enum { 313 AUTHORIZATION_UNKNOWN, 314 AUTHORIZATION_PENDING, 315 AUTHORIZATION_DECLINED, 316 AUTHORIZATION_GRANTED 317 } authorization_state_t; 318 319 typedef struct sm_pairing_packet { 320 uint8_t code; 321 uint8_t io_capability; 322 uint8_t oob_data_flag; 323 uint8_t auth_req; 324 uint8_t max_encryption_key_size; 325 uint8_t initiator_key_distribution; 326 uint8_t responder_key_distribution; 327 } sm_pairing_packet_t; 328 329 // connection info available as long as connection exists 330 typedef struct sm_connection { 331 uint16_t sm_handle; 332 uint8_t sm_role; // 0 - IamMaster, 1 = IamSlave 333 uint8_t sm_security_request_received; 334 uint8_t sm_bonding_requested; 335 uint8_t sm_peer_addr_type; 336 bd_addr_t sm_peer_address; 337 security_manager_state_t sm_engine_state; 338 irk_lookup_state_t sm_irk_lookup_state; 339 uint8_t sm_connection_encrypted; 340 uint8_t sm_connection_authenticated; // [0..1] 341 uint8_t sm_actual_encryption_key_size; 342 sm_pairing_packet_t sm_m_preq; // only used during c1 343 authorization_state_t sm_connection_authorization_state; 344 uint16_t sm_local_ediv; 345 uint8_t sm_local_rand[8]; 346 int sm_le_db_index; 347 } sm_connection_t; 348 349 typedef struct { 350 // linked list - assert: first field 351 btstack_linked_item_t item; 352 353 // remote side 354 bd_addr_t address; 355 356 // module handle 357 hci_con_handle_t con_handle; 358 359 // le public, le random, classic 360 bd_addr_type_t address_type; 361 362 // role: 0 - master, 1 - slave 363 uint8_t role; 364 365 // connection state 366 CONNECTION_STATE state; 367 368 // bonding 369 uint16_t bonding_flags; 370 uint8_t bonding_status; 371 // requested security level 372 gap_security_level_t requested_security_level; 373 374 // 375 link_key_type_t link_key_type; 376 377 // remote supported features 378 uint8_t remote_supported_feature_eSCO; 379 380 // errands 381 uint32_t authentication_flags; 382 383 btstack_timer_source_t timeout; 384 385 #ifdef HAVE_TIME 386 // timer 387 struct timeval timestamp; 388 #endif 389 #ifdef HAVE_TICK 390 uint32_t timestamp; // timestamp in system ticks 391 #endif 392 #ifdef HAVE_TIME_MS 393 uint32_t timestamp; // timestamp in ms 394 #endif 395 396 // ACL packet recombination - PRE_BUFFER + ACL Header + ACL payload 397 uint8_t acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4 + HCI_ACL_BUFFER_SIZE]; 398 uint16_t acl_recombination_pos; 399 uint16_t acl_recombination_length; 400 401 // number packets sent to controller 402 uint8_t num_acl_packets_sent; 403 uint8_t num_sco_packets_sent; 404 405 // LE Connection parameter update 406 le_con_parameter_update_state_t le_con_parameter_update_state; 407 uint8_t le_con_param_update_identifier; 408 uint16_t le_conn_interval_min; 409 uint16_t le_conn_interval_max; 410 uint16_t le_conn_latency; 411 uint16_t le_supervision_timeout; 412 413 #ifdef ENABLE_BLE 414 // LE Security Manager 415 sm_connection_t sm_connection; 416 #endif 417 418 } hci_connection_t; 419 420 421 /** 422 * HCI Inititizlization State Machine 423 */ 424 typedef enum hci_init_state{ 425 HCI_INIT_SEND_RESET = 0, 426 HCI_INIT_W4_SEND_RESET, 427 HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION, 428 HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION, 429 430 HCI_INIT_SEND_BAUD_CHANGE, 431 HCI_INIT_W4_SEND_BAUD_CHANGE, 432 HCI_INIT_CUSTOM_INIT, 433 HCI_INIT_W4_CUSTOM_INIT, 434 HCI_INIT_SEND_RESET_CSR_WARM_BOOT, 435 HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT, 436 437 HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS, 438 HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS, 439 440 HCI_INIT_SEND_BAUD_CHANGE_BCM, 441 HCI_INIT_W4_SEND_BAUD_CHANGE_BCM, 442 443 HCI_INIT_SET_BD_ADDR, 444 HCI_INIT_W4_SET_BD_ADDR, 445 446 HCI_INIT_SEND_RESET_ST_WARM_BOOT, 447 HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT, 448 449 HCI_INIT_READ_BD_ADDR, 450 HCI_INIT_W4_READ_BD_ADDR, 451 452 HCI_INIT_READ_BUFFER_SIZE, 453 HCI_INIT_W4_READ_BUFFER_SIZE, 454 HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES, 455 HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES, 456 HCI_INIT_SET_EVENT_MASK, 457 HCI_INIT_W4_SET_EVENT_MASK, 458 HCI_INIT_WRITE_SIMPLE_PAIRING_MODE, 459 HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE, 460 HCI_INIT_WRITE_PAGE_TIMEOUT, 461 HCI_INIT_W4_WRITE_PAGE_TIMEOUT, 462 HCI_INIT_WRITE_CLASS_OF_DEVICE, 463 HCI_INIT_W4_WRITE_CLASS_OF_DEVICE, 464 HCI_INIT_WRITE_LOCAL_NAME, 465 HCI_INIT_W4_WRITE_LOCAL_NAME, 466 HCI_INIT_WRITE_SCAN_ENABLE, 467 HCI_INIT_W4_WRITE_SCAN_ENABLE, 468 469 HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, 470 HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, 471 472 HCI_INIT_LE_READ_BUFFER_SIZE, 473 HCI_INIT_W4_LE_READ_BUFFER_SIZE, 474 HCI_INIT_WRITE_LE_HOST_SUPPORTED, 475 HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED, 476 HCI_INIT_READ_WHITE_LIST_SIZE, 477 HCI_INIT_W4_READ_WHITE_LIST_SIZE, 478 479 HCI_INIT_LE_SET_SCAN_PARAMETERS, 480 HCI_INIT_W4_LE_SET_SCAN_PARAMETERS, 481 482 HCI_INIT_DONE, 483 484 HCI_FALLING_ASLEEP_DISCONNECT, 485 HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE, 486 HCI_FALLING_ASLEEP_COMPLETE, 487 488 HCI_INIT_AFTER_SLEEP 489 490 } hci_substate_t; 491 492 enum { 493 LE_ADVERTISEMENT_TASKS_DISABLE = 1 << 0, 494 LE_ADVERTISEMENT_TASKS_SET_DATA = 1 << 1, 495 LE_ADVERTISEMENT_TASKS_SET_PARAMS = 1 << 2, 496 LE_ADVERTISEMENT_TASKS_ENABLE = 1 << 3, 497 }; 498 499 enum { 500 LE_WHITELIST_ON_CONTROLLER = 1 << 0, 501 LE_WHITELIST_ADD_TO_CONTROLLER = 1 << 1, 502 LE_WHITELIST_REMOVE_FROM_CONTROLLER = 1 << 2, 503 }; 504 505 typedef struct { 506 btstack_linked_item_t item; 507 bd_addr_t address; 508 bd_addr_type_t address_type; 509 uint8_t state; 510 } whitelist_entry_t; 511 512 /** 513 * main data structure 514 */ 515 typedef struct { 516 // transport component with configuration 517 const hci_transport_t * hci_transport; 518 const void * config; 519 520 // chipset driver 521 const btstack_chipset_t * chipset; 522 523 // hardware power controller 524 const btstack_control_t * control; 525 526 // basic configuration 527 const char * local_name; 528 uint32_t class_of_device; 529 bd_addr_t local_bd_addr; 530 uint8_t ssp_enable; 531 uint8_t ssp_io_capability; 532 uint8_t ssp_authentication_requirement; 533 uint8_t ssp_auto_accept; 534 535 536 // list of existing baseband connections 537 btstack_linked_list_t connections; 538 539 // single buffer for HCI packet assembly + additional prebuffer for H4 drivers 540 uint8_t hci_packet_buffer_prefix[HCI_OUTGOING_PRE_BUFFER_SIZE]; 541 uint8_t hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8) 542 uint8_t hci_packet_buffer_reserved; 543 uint16_t acl_fragmentation_pos; 544 uint16_t acl_fragmentation_total_size; 545 546 /* host to controller flow control */ 547 uint8_t num_cmd_packets; 548 uint8_t acl_packets_total_num; 549 uint16_t acl_data_packet_length; 550 uint8_t sco_packets_total_num; 551 uint8_t sco_data_packet_length; 552 uint8_t synchronous_flow_control_enabled; 553 uint8_t le_acl_packets_total_num; 554 uint16_t le_data_packets_length; 555 556 /* local supported features */ 557 uint8_t local_supported_features[8]; 558 559 /* local supported commands summary - complete info is 64 bytes */ 560 /* 0 - read buffer size */ 561 /* 1 - write le host supported */ 562 uint8_t local_supported_commands[1]; 563 564 /* bluetooth device information from hci read local version information */ 565 // uint16_t hci_version; 566 // uint16_t hci_revision; 567 // uint16_t lmp_version; 568 uint16_t manufacturer; 569 // uint16_t lmp_subversion; 570 571 // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE 572 uint16_t packet_types; 573 574 /* callback to L2CAP layer */ 575 void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 576 577 /* callback for SCO data */ 578 void (*sco_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 579 580 /* remote device db */ 581 remote_device_db_t const*remote_device_db; 582 583 /* hci state machine */ 584 HCI_STATE state; 585 hci_substate_t substate; 586 btstack_timer_source_t timeout; 587 uint8_t cmds_ready; 588 589 uint16_t last_cmd_opcode; 590 591 uint8_t discoverable; 592 uint8_t connectable; 593 uint8_t bondable; 594 595 /* buffer for scan enable cmd - 0xff no change */ 596 uint8_t new_scan_enable_value; 597 598 uint16_t sco_voice_setting; 599 600 uint8_t loopback_mode; 601 602 // buffer for single connection decline 603 uint8_t decline_reason; 604 bd_addr_t decline_addr; 605 606 uint8_t adv_addr_type; 607 bd_addr_t adv_address; 608 609 le_scanning_state_t le_scanning_state; 610 le_connecting_state_t le_connecting_state; 611 612 // buffer for le scan type command - 0xff not set 613 uint8_t le_scan_type; 614 uint16_t le_scan_interval; 615 uint16_t le_scan_window; 616 617 le_connection_parameter_range_t le_connection_parameter_range; 618 619 uint8_t * le_advertisements_data; 620 uint8_t le_advertisements_data_len; 621 622 uint8_t le_advertisements_active; 623 uint8_t le_advertisements_enabled; 624 uint8_t le_advertisements_todo; 625 626 uint16_t le_advertisements_interval_min; 627 uint16_t le_advertisements_interval_max; 628 uint8_t le_advertisements_type; 629 uint8_t le_advertisements_own_address_type; 630 uint8_t le_advertisements_direct_address_type; 631 uint8_t le_advertisements_channel_map; 632 uint8_t le_advertisements_filter_policy; 633 bd_addr_t le_advertisements_direct_address; 634 635 // LE Whitelist Management 636 uint16_t le_whitelist_capacity; 637 btstack_linked_list_t le_whitelist; 638 639 // custom BD ADDR 640 bd_addr_t custom_bd_addr; 641 uint8_t custom_bd_addr_set; 642 643 // hardware error callback 644 void (*hardware_error_callback)(void); 645 646 // local version information callback 647 void (*local_version_information_callback)(uint8_t * local_version_information); 648 649 } hci_stack_t; 650 651 /** 652 * set connection iterator 653 */ 654 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it); 655 656 // create and send hci command packets based on a template and a list of parameters 657 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...); 658 659 /** 660 * run the hci control loop once 661 */ 662 void hci_run(void); 663 664 // send ACL packet prepared in hci packet buffer 665 int hci_send_acl_packet_buffer(int size); 666 667 // send SCO packet prepared in hci packet buffer 668 int hci_send_sco_packet_buffer(int size); 669 670 671 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle); 672 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle); 673 int hci_can_send_sco_packet_now(hci_con_handle_t con_handle); 674 int hci_can_send_prepared_sco_packet_now(hci_con_handle_t con_handle); 675 676 // reserves outgoing packet buffer. @returns 1 if successful 677 int hci_reserve_packet_buffer(void); 678 void hci_release_packet_buffer(void); 679 680 // used for internal checks in l2cap[-le].c 681 int hci_is_packet_buffer_reserved(void); 682 683 // get point to packet buffer 684 uint8_t* hci_get_outgoing_packet_buffer(void); 685 686 687 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle); 688 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type); 689 int hci_is_le_connection(hci_connection_t * connection); 690 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle); 691 uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle); 692 int hci_authentication_active_for_handle(hci_con_handle_t handle); 693 uint16_t hci_max_acl_data_packet_length(void); 694 uint16_t hci_max_acl_le_data_packet_length(void); 695 uint16_t hci_usable_acl_packet_types(void); 696 int hci_non_flushable_packet_boundary_flag_supported(void); 697 698 void hci_disconnect_all(void); 699 700 void hci_emit_state(void); 701 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status); 702 void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 703 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason); 704 void hci_emit_nr_connections_changed(void); 705 void hci_emit_hci_open_failed(void); 706 void hci_emit_btstack_version(void); 707 void hci_emit_system_bluetooth_enabled(uint8_t enabled); 708 void hci_emit_remote_name_cached(bd_addr_t addr, device_name_t *name); 709 void hci_emit_discoverable_enabled(uint8_t enabled); 710 void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 711 void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 712 713 // query if the local side supports SSP 714 int hci_local_ssp_activated(void); 715 716 // query if the remote side supports SSP 717 int hci_remote_ssp_supported(hci_con_handle_t con_handle); 718 719 // query if both sides support SSP 720 int hci_ssp_supported_on_both_sides(hci_con_handle_t handle); 721 722 // disable automatic L2CAP disconnect for testing 723 void hci_disable_l2cap_timeout_check(void); 724 725 // disconnect because of security block 726 void hci_disconnect_security_block(hci_con_handle_t con_handle); 727 728 // send complete CMD packet 729 int hci_send_cmd_packet(uint8_t *packet, int size); 730 731 // query if remote side supports eSCO 732 int hci_remote_eSCO_supported(hci_con_handle_t con_handle); 733 734 /* API_START */ 735 736 void gap_le_get_connection_parameter_range(le_connection_parameter_range_t range); 737 void gap_le_set_connection_parameter_range(le_connection_parameter_range_t range); 738 739 /* LE Client Start */ 740 741 uint8_t le_central_start_scan(void); 742 uint8_t le_central_stop_scan(void); 743 uint8_t le_central_connect(bd_addr_t addr, bd_addr_type_t addr_type); 744 uint8_t le_central_connect_cancel(void); 745 uint8_t gap_disconnect(hci_con_handle_t handle); 746 void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window); 747 748 /* LE Client End */ 749 750 void hci_connectable_control(uint8_t enable); 751 void hci_close(void); 752 753 /** 754 * @note New functions replacing: hci_can_send_packet_now[_using_packet_buffer] 755 */ 756 int hci_can_send_command_packet_now(void); 757 758 /** 759 * @brief Gets local address. 760 */ 761 void hci_local_bd_addr(bd_addr_t address_buffer); 762 763 /** 764 * @brief Set up HCI. Needs to be called before any other function. 765 */ 766 void hci_init(const hci_transport_t *transport, void *config, remote_device_db_t const* remote_device_db); 767 768 /** 769 * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information. 770 */ 771 void hci_set_chipset(const btstack_chipset_t *chipset_driver); 772 773 /** 774 * @brief Configure Bluetooth hardware control. Has to be called before power on. 775 */ 776 void hci_set_control(const btstack_control_t *hardware_control); 777 778 /** 779 * @brief Set class of device that will be set during Bluetooth init. 780 */ 781 void hci_set_class_of_device(uint32_t class_of_device); 782 783 /** 784 * @brief Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 785 */ 786 void hci_set_bd_addr(bd_addr_t addr); 787 788 /** 789 * @brief Registers a packet handler. Used if L2CAP is not used (rarely). 790 */ 791 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 792 793 /** 794 * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 795 */ 796 void hci_register_sco_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 797 798 /** 799 * @brief Requests the change of BTstack power mode. 800 */ 801 int hci_power_control(HCI_POWER_MODE mode); 802 803 /** 804 * @brief Allows to control if device is discoverable. OFF by default. 805 */ 806 void hci_discoverable_control(uint8_t enable); 807 808 /** 809 * @brief Creates and sends HCI command packets based on a template and a list of parameters. Will return error if outgoing data buffer is occupied. 810 */ 811 int hci_send_cmd(const hci_cmd_t *cmd, ...); 812 813 /** 814 * @brief Deletes link key for remote device with baseband address. 815 */ 816 void hci_drop_link_key_for_bd_addr(bd_addr_t addr); 817 818 /* Configure Secure Simple Pairing */ 819 820 /** 821 * @brief Enable will enable SSP during init. 822 */ 823 void hci_ssp_set_enable(int enable); 824 825 /** 826 * @brief If set, BTstack will respond to io capability request using authentication requirement. 827 */ 828 void hci_ssp_set_io_capability(int ssp_io_capability); 829 void hci_ssp_set_authentication_requirement(int authentication_requirement); 830 831 /** 832 * @brief If set, BTstack will confirm a numeric comparison and enter '000000' if requested. 833 */ 834 void hci_ssp_set_auto_accept(int auto_accept); 835 836 /** 837 * @brief Get addr type and address used in advertisement packets. 838 */ 839 void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t addr); 840 841 /** 842 * @brief Set callback for Bluetooth Hardware Error 843 */ 844 void hci_set_hardware_error_callback(void (*fn)(void)); 845 846 /** 847 * @brief Set callback for local information from Bluetooth controller right after HCI Reset 848 * @note Can be used to select chipset driver dynamically during startup 849 */ 850 void hci_set_local_version_information_callback(void (*fn)(uint8_t * local_version_information)); 851 852 /** 853 * @brief Configure Voice Setting for use with SCO data in HSP/HFP 854 */ 855 void hci_set_sco_voice_setting(uint16_t voice_setting); 856 857 /** 858 * @brief Get SCO Voice Setting 859 * @return current voice setting 860 */ 861 uint16_t hci_get_sco_voice_setting(void); 862 863 /** @brief Get SCO packet length for current SCO Voice setting 864 * @note Using SCO packets of the exact length is required for USB transfer 865 * @return Length of SCO packets in bytes (not audio frames) incl. 3 byte header 866 */ 867 int hci_get_sco_packet_length(void); 868 869 /* API_END */ 870 871 /** 872 * @brief Set Advertisement Parameters 873 * @param adv_int_min 874 * @param adv_int_max 875 * @param adv_type 876 * @param own_address_type 877 * @param direct_address_type 878 * @param direct_address 879 * @param channel_map 880 * @param filter_policy 881 * 882 * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 883 */ 884 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 885 uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address, 886 uint8_t channel_map, uint8_t filter_policy); 887 888 #if defined __cplusplus 889 } 890 #endif 891 892 #endif // __HCI_H 893