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