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 // LE Secure Connections 253 SM_PH2_SEND_PUBLIC_KEY_COMMAND, 254 SM_PH2_SEND_CONFIRMATION, 255 SM_PH2_SEND_PAIRING_RANDOM_SC, 256 SM_PH2_SEND_DHKEY_CHECK_COMMAND, 257 SM_PH2_W4_PAIRING_RANDOM, 258 SM_PH2_W4_DHKEY_CHECK_COMMAND, 259 260 // Phase 3: Transport Specific Key Distribution 261 // calculate DHK, Y, EDIV, and LTK 262 SM_PH3_GET_RANDOM, 263 SM_PH3_W4_RANDOM, 264 SM_PH3_GET_DIV, 265 SM_PH3_W4_DIV, 266 SM_PH3_Y_GET_ENC, 267 SM_PH3_Y_W4_ENC, 268 SM_PH3_LTK_GET_ENC, 269 // state = 30 270 SM_PH3_LTK_W4_ENC, 271 SM_PH3_CSRK_GET_ENC, 272 SM_PH3_CSRK_W4_ENC, 273 274 // exchange keys 275 SM_PH3_DISTRIBUTE_KEYS, 276 SM_PH3_RECEIVE_KEYS, 277 278 // RESPONDER ROLE 279 // state = 35 280 SM_RESPONDER_IDLE, 281 SM_RESPONDER_SEND_SECURITY_REQUEST, 282 SM_RESPONDER_PH0_RECEIVED_LTK, 283 SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY, 284 SM_RESPONDER_PH1_W4_PAIRING_REQUEST, 285 SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED, 286 SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE, 287 SM_RESPONDER_PH1_W4_PAIRING_CONFIRM, 288 SM_RESPONDER_PH2_W4_PAIRING_RANDOM, 289 SM_RESPONDER_PH2_W4_LTK_REQUEST, 290 SM_RESPONDER_PH2_SEND_LTK_REPLY, 291 // LE Secure Connection 292 SM_RESPONDER_PH2_W4_PUBLIC_KEY_COMMAND, 293 SM_RESPONDER_PH2_W4_LTK_REQUEST_SC, 294 295 // Phase 4: re-establish previously distributed LTK 296 // state == 46 297 SM_RESPONDER_PH4_Y_GET_ENC, 298 SM_RESPONDER_PH4_Y_W4_ENC, 299 SM_RESPONDER_PH4_LTK_GET_ENC, 300 SM_RESPONDER_PH4_LTK_W4_ENC, 301 SM_RESPONDER_PH4_SEND_LTK, 302 303 // INITITIATOR ROLE 304 // state = 51 305 SM_INITIATOR_CONNECTED, 306 SM_INITIATOR_PH0_HAS_LTK, 307 SM_INITIATOR_PH0_SEND_START_ENCRYPTION, 308 SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED, 309 SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST, 310 SM_INITIATOR_PH1_SEND_PAIRING_REQUEST, 311 SM_INITIATOR_PH1_W4_PAIRING_RESPONSE, 312 SM_INITIATOR_PH2_W4_PAIRING_CONFIRM, 313 SM_INITIATOR_PH2_W4_PAIRING_RANDOM, 314 SM_INITIATOR_PH3_SEND_START_ENCRYPTION, 315 // 316 } security_manager_state_t; 317 318 typedef enum { 319 IRK_LOOKUP_IDLE, 320 IRK_LOOKUP_W4_READY, 321 IRK_LOOKUP_STARTED, 322 IRK_LOOKUP_SUCCEEDED, 323 IRK_LOOKUP_FAILED 324 } irk_lookup_state_t; 325 326 // Authorization state 327 typedef enum { 328 AUTHORIZATION_UNKNOWN, 329 AUTHORIZATION_PENDING, 330 AUTHORIZATION_DECLINED, 331 AUTHORIZATION_GRANTED 332 } authorization_state_t; 333 334 typedef uint8_t sm_pairing_packet_t[7]; 335 336 // connection info available as long as connection exists 337 typedef struct sm_connection { 338 hci_con_handle_t sm_handle; 339 uint8_t sm_role; // 0 - IamMaster, 1 = IamSlave 340 uint8_t sm_security_request_received; 341 uint8_t sm_bonding_requested; 342 uint8_t sm_peer_addr_type; 343 bd_addr_t sm_peer_address; 344 security_manager_state_t sm_engine_state; 345 irk_lookup_state_t sm_irk_lookup_state; 346 uint8_t sm_connection_encrypted; 347 uint8_t sm_connection_authenticated; // [0..1] 348 uint8_t sm_actual_encryption_key_size; 349 sm_pairing_packet_t sm_m_preq; // only used during c1 350 authorization_state_t sm_connection_authorization_state; 351 uint16_t sm_local_ediv; 352 uint8_t sm_local_rand[8]; 353 int sm_le_db_index; 354 } sm_connection_t; 355 356 typedef struct { 357 // linked list - assert: first field 358 btstack_linked_item_t item; 359 360 // remote side 361 bd_addr_t address; 362 363 // module handle 364 hci_con_handle_t con_handle; 365 366 // le public, le random, classic 367 bd_addr_type_t address_type; 368 369 // role: 0 - master, 1 - slave 370 uint8_t role; 371 372 // connection state 373 CONNECTION_STATE state; 374 375 // bonding 376 uint16_t bonding_flags; 377 uint8_t bonding_status; 378 // requested security level 379 gap_security_level_t requested_security_level; 380 381 // 382 link_key_type_t link_key_type; 383 384 // remote supported features 385 uint8_t remote_supported_feature_eSCO; 386 387 // errands 388 uint32_t authentication_flags; 389 390 btstack_timer_source_t timeout; 391 392 // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS) 393 uint32_t timestamp; 394 395 // ACL packet recombination - PRE_BUFFER + ACL Header + ACL payload 396 uint8_t acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4 + HCI_ACL_BUFFER_SIZE]; 397 uint16_t acl_recombination_pos; 398 uint16_t acl_recombination_length; 399 400 // number packets sent to controller 401 uint8_t num_acl_packets_sent; 402 uint8_t num_sco_packets_sent; 403 404 // LE Connection parameter update 405 le_con_parameter_update_state_t le_con_parameter_update_state; 406 uint8_t le_con_param_update_identifier; 407 uint16_t le_conn_interval_min; 408 uint16_t le_conn_interval_max; 409 uint16_t le_conn_latency; 410 uint16_t le_supervision_timeout; 411 412 #ifdef ENABLE_BLE 413 // LE Security Manager 414 sm_connection_t sm_connection; 415 #endif 416 417 } hci_connection_t; 418 419 420 /** 421 * HCI Inititizlization State Machine 422 */ 423 typedef enum hci_init_state{ 424 HCI_INIT_SEND_RESET = 0, 425 HCI_INIT_W4_SEND_RESET, 426 HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION, 427 HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION, 428 429 HCI_INIT_SEND_BAUD_CHANGE, 430 HCI_INIT_W4_SEND_BAUD_CHANGE, 431 HCI_INIT_CUSTOM_INIT, 432 HCI_INIT_W4_CUSTOM_INIT, 433 HCI_INIT_SEND_RESET_CSR_WARM_BOOT, 434 HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT, 435 HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET, 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 HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, 472 HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, 473 474 HCI_INIT_LE_READ_BUFFER_SIZE, 475 HCI_INIT_W4_LE_READ_BUFFER_SIZE, 476 HCI_INIT_WRITE_LE_HOST_SUPPORTED, 477 HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED, 478 HCI_INIT_READ_WHITE_LIST_SIZE, 479 HCI_INIT_W4_READ_WHITE_LIST_SIZE, 480 481 HCI_INIT_LE_SET_SCAN_PARAMETERS, 482 HCI_INIT_W4_LE_SET_SCAN_PARAMETERS, 483 484 HCI_INIT_DONE, 485 486 HCI_FALLING_ASLEEP_DISCONNECT, 487 HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE, 488 HCI_FALLING_ASLEEP_COMPLETE, 489 490 HCI_INIT_AFTER_SLEEP 491 492 } hci_substate_t; 493 494 enum { 495 LE_ADVERTISEMENT_TASKS_DISABLE = 1 << 0, 496 LE_ADVERTISEMENT_TASKS_SET_ADV_DATA = 1 << 1, 497 LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA = 1 << 2, 498 LE_ADVERTISEMENT_TASKS_SET_PARAMS = 1 << 3, 499 LE_ADVERTISEMENT_TASKS_ENABLE = 1 << 4, 500 }; 501 502 enum { 503 LE_WHITELIST_ON_CONTROLLER = 1 << 0, 504 LE_WHITELIST_ADD_TO_CONTROLLER = 1 << 1, 505 LE_WHITELIST_REMOVE_FROM_CONTROLLER = 1 << 2, 506 }; 507 508 typedef struct { 509 btstack_linked_item_t item; 510 bd_addr_t address; 511 bd_addr_type_t address_type; 512 uint8_t state; 513 } whitelist_entry_t; 514 515 /** 516 * main data structure 517 */ 518 typedef struct { 519 // transport component with configuration 520 const hci_transport_t * hci_transport; 521 const void * config; 522 523 // chipset driver 524 const btstack_chipset_t * chipset; 525 526 // hardware power controller 527 const btstack_control_t * control; 528 529 /* link key db */ 530 const btstack_link_key_db_t * link_key_db; 531 532 // list of existing baseband connections 533 btstack_linked_list_t connections; 534 535 /* callback to L2CAP layer */ 536 btstack_packet_handler_t acl_packet_handler; 537 538 /* callback for SCO data */ 539 btstack_packet_handler_t sco_packet_handler; 540 541 /* callbacks for events */ 542 btstack_linked_list_t event_handlers; 543 544 // local version information callback 545 void (*local_version_information_callback)(uint8_t * local_version_information); 546 547 // hardware error callback 548 void (*hardware_error_callback)(void); 549 550 // basic configuration 551 const char * local_name; 552 uint32_t class_of_device; 553 bd_addr_t local_bd_addr; 554 uint8_t ssp_enable; 555 uint8_t ssp_io_capability; 556 uint8_t ssp_authentication_requirement; 557 uint8_t ssp_auto_accept; 558 559 // single buffer for HCI packet assembly + additional prebuffer for H4 drivers 560 uint8_t hci_packet_buffer_prefix[HCI_OUTGOING_PRE_BUFFER_SIZE]; 561 uint8_t hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8) 562 uint8_t hci_packet_buffer_reserved; 563 uint16_t acl_fragmentation_pos; 564 uint16_t acl_fragmentation_total_size; 565 566 /* host to controller flow control */ 567 uint8_t num_cmd_packets; 568 uint8_t acl_packets_total_num; 569 uint16_t acl_data_packet_length; 570 uint8_t sco_packets_total_num; 571 uint8_t sco_data_packet_length; 572 uint8_t synchronous_flow_control_enabled; 573 uint8_t le_acl_packets_total_num; 574 uint16_t le_data_packets_length; 575 uint8_t sco_waiting_for_can_send_now; 576 577 /* local supported features */ 578 uint8_t local_supported_features[8]; 579 580 /* local supported commands summary - complete info is 64 bytes */ 581 /* 0 - read buffer size */ 582 /* 1 - write le host supported */ 583 uint8_t local_supported_commands[1]; 584 585 /* bluetooth device information from hci read local version information */ 586 // uint16_t hci_version; 587 // uint16_t hci_revision; 588 // uint16_t lmp_version; 589 uint16_t manufacturer; 590 // uint16_t lmp_subversion; 591 592 // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE 593 uint16_t packet_types; 594 595 596 /* hci state machine */ 597 HCI_STATE state; 598 hci_substate_t substate; 599 btstack_timer_source_t timeout; 600 uint8_t cmds_ready; 601 602 uint16_t last_cmd_opcode; 603 604 uint8_t discoverable; 605 uint8_t connectable; 606 uint8_t bondable; 607 608 /* buffer for scan enable cmd - 0xff no change */ 609 uint8_t new_scan_enable_value; 610 611 uint16_t sco_voice_setting; 612 613 uint8_t loopback_mode; 614 615 // buffer for single connection decline 616 uint8_t decline_reason; 617 bd_addr_t decline_addr; 618 619 uint8_t adv_addr_type; 620 bd_addr_t adv_address; 621 622 le_scanning_state_t le_scanning_state; 623 le_connecting_state_t le_connecting_state; 624 625 // buffer for le scan type command - 0xff not set 626 uint8_t le_scan_type; 627 uint16_t le_scan_interval; 628 uint16_t le_scan_window; 629 630 le_connection_parameter_range_t le_connection_parameter_range; 631 632 uint8_t * le_advertisements_data; 633 uint8_t le_advertisements_data_len; 634 635 uint8_t * le_scan_response_data; 636 uint8_t le_scan_response_data_len; 637 638 uint8_t le_advertisements_active; 639 uint8_t le_advertisements_enabled; 640 uint8_t le_advertisements_todo; 641 642 uint16_t le_advertisements_interval_min; 643 uint16_t le_advertisements_interval_max; 644 uint8_t le_advertisements_type; 645 uint8_t le_advertisements_own_address_type; 646 uint8_t le_advertisements_direct_address_type; 647 uint8_t le_advertisements_channel_map; 648 uint8_t le_advertisements_filter_policy; 649 bd_addr_t le_advertisements_direct_address; 650 651 // LE Whitelist Management 652 uint16_t le_whitelist_capacity; 653 btstack_linked_list_t le_whitelist; 654 655 // custom BD ADDR 656 bd_addr_t custom_bd_addr; 657 uint8_t custom_bd_addr_set; 658 659 } hci_stack_t; 660 661 662 /* API_START */ 663 664 665 // HCI init and configuration 666 667 668 /** 669 * @brief Set up HCI. Needs to be called before any other function. 670 */ 671 void hci_init(const hci_transport_t *transport, const void *config); 672 673 /** 674 * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information. 675 */ 676 void hci_set_chipset(const btstack_chipset_t *chipset_driver); 677 678 /** 679 * @brief Configure Bluetooth hardware control. Has to be called before power on. 680 */ 681 void hci_set_control(const btstack_control_t *hardware_control); 682 683 /** 684 * @brief Configure Bluetooth hardware control. Has to be called before power on. 685 */ 686 void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db); 687 688 /** 689 * @brief Set callback for Bluetooth Hardware Error 690 */ 691 void hci_set_hardware_error_callback(void (*fn)(void)); 692 693 /** 694 * @brief Set callback for local information from Bluetooth controller right after HCI Reset 695 * @note Can be used to select chipset driver dynamically during startup 696 */ 697 void hci_set_local_version_information_callback(void (*fn)(uint8_t * local_version_information)); 698 699 /** 700 * @brief Set Public BD ADDR - passed on to Bluetooth chipset during init if supported in bt_control_h 701 */ 702 void hci_set_bd_addr(bd_addr_t addr); 703 704 /** 705 * @brief Configure Voice Setting for use with SCO data in HSP/HFP 706 */ 707 void hci_set_sco_voice_setting(uint16_t voice_setting); 708 709 /** 710 * @brief Get SCO Voice Setting 711 * @return current voice setting 712 */ 713 uint16_t hci_get_sco_voice_setting(void); 714 715 /** 716 * @brief Requests the change of BTstack power mode. 717 */ 718 int hci_power_control(HCI_POWER_MODE mode); 719 720 /** 721 * @brief Shutdown HCI 722 */ 723 void hci_close(void); 724 725 726 // Callback registration 727 728 729 /** 730 * @brief Add event packet handler. 731 */ 732 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler); 733 734 /** 735 * @brief Registers a packet handler for ACL data. Used by L2CAP 736 */ 737 void hci_register_acl_packet_handler(btstack_packet_handler_t handler); 738 739 /** 740 * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 741 */ 742 void hci_register_sco_packet_handler(btstack_packet_handler_t handler); 743 744 745 // Sending HCI Commands 746 747 /** 748 * @brief Check if CMD packet can be sent to controller 749 */ 750 int hci_can_send_command_packet_now(void); 751 752 /** 753 * @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. 754 */ 755 int hci_send_cmd(const hci_cmd_t *cmd, ...); 756 757 758 // Sending SCO Packets 759 760 /** @brief Get SCO packet length for current SCO Voice setting 761 * @note Using SCO packets of the exact length is required for USB transfer 762 * @return Length of SCO packets in bytes (not audio frames) incl. 3 byte header 763 */ 764 int hci_get_sco_packet_length(void); 765 766 /** 767 * @brief Request emission of HCI_EVENT_SCO_CAN_SEND_NOW as soon as possible 768 * @note HCI_EVENT_SCO_CAN_SEND_NOW might be emitted during call to this function 769 * so packet handler should be ready to handle it 770 */ 771 void hci_request_sco_can_send_now_event(void); 772 773 /** 774 * @brief Check HCI packet buffer and if SCO packet can be sent to controller 775 */ 776 int hci_can_send_sco_packet_now(void); 777 778 /** 779 * @brief Check if SCO packet can be sent to controller 780 */ 781 int hci_can_send_prepared_sco_packet_now(void); 782 783 /** 784 * @brief Send SCO packet prepared in HCI packet buffer 785 */ 786 int hci_send_sco_packet_buffer(int size); 787 788 789 // Outgoing packet buffer, also used for SCO packets 790 // see hci_can_send_prepared_sco_packet_now amn hci_send_sco_packet_buffer 791 792 /** 793 * Reserves outgoing packet buffer. 794 * @return 1 on success 795 */ 796 int hci_reserve_packet_buffer(void); 797 798 /** 799 * Get pointer for outgoing packet buffer 800 */ 801 uint8_t* hci_get_outgoing_packet_buffer(void); 802 803 /** 804 * Release outgoing packet buffer\ 805 * @note only called instead of hci_send_preparared 806 */ 807 void hci_release_packet_buffer(void); 808 809 810 /* API_END */ 811 812 813 814 /** 815 * Get connection iterator. Only used by l2cap.c and sm.c 816 */ 817 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it); 818 819 /** 820 * Get internal hci_connection_t for given handle. Used by L2CAP, SM, daemon 821 */ 822 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle); 823 824 /** 825 * Get internal hci_connection_t for given Bluetooth addres. Called by L2CAP 826 */ 827 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type); 828 829 /** 830 * Check if outgoing packet buffer is reserved. Used for internal checks in l2cap.c 831 */ 832 int hci_is_packet_buffer_reserved(void); 833 834 /** 835 * Check hci packet buffer is free and a classic acl packet can be sent to controller 836 */ 837 int hci_can_send_acl_classic_packet_now(void); 838 839 /** 840 * Check hci packet buffer is free and an LE acl packet can be sent to controller 841 */ 842 int hci_can_send_acl_le_packet_now(void); 843 844 /** 845 * Check hci packet buffer is free and an acl packet for the given handle can be sent to controller 846 */ 847 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle); 848 849 /** 850 * Check if acl packet for the given handle can be sent to controller 851 */ 852 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle); 853 854 /** 855 * Send acl packet prepared in hci packet buffer 856 */ 857 int hci_send_acl_packet_buffer(int size); 858 859 /** 860 * Check if authentication is active. It delays automatic disconnect while no L2CAP connection 861 * Called by l2cap. 862 */ 863 int hci_authentication_active_for_handle(hci_con_handle_t handle); 864 865 /** 866 * Get maximal ACL Classic data packet length based on used buffer size. Called by L2CAP 867 */ 868 uint16_t hci_max_acl_data_packet_length(void); 869 870 /** 871 * Get supported packet types. Called by L2CAP 872 */ 873 uint16_t hci_usable_acl_packet_types(void); 874 875 /** 876 * Check if ACL packets marked as non flushable can be sent. Called by L2CAP 877 */ 878 int hci_non_flushable_packet_boundary_flag_supported(void); 879 880 /** 881 * Check if SSP is supported on both sides. Called by L2CAP 882 */ 883 int gap_ssp_supported_on_both_sides(hci_con_handle_t handle); 884 885 /** 886 * Disconn because of security block. Called by L2CAP 887 */ 888 void hci_disconnect_security_block(hci_con_handle_t con_handle); 889 890 /** 891 * Query if remote side supports eSCO 892 */ 893 int hci_remote_esco_supported(hci_con_handle_t con_handle); 894 895 /** 896 * Emit current HCI state. Called by daemon 897 */ 898 void hci_emit_state(void); 899 900 /** 901 * Send complete CMD packet. Called by daemon 902 */ 903 int hci_send_cmd_packet(uint8_t *packet, int size); 904 905 /** 906 * Disconnect all HCI connections. Called by daemon 907 */ 908 void hci_disconnect_all(void); 909 910 /** 911 * Get number of free acl slots for packets of given handle. Called by daemon 912 */ 913 int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle); 914 915 /** 916 * @brief Set Advertisement Parameters 917 * @param adv_int_min 918 * @param adv_int_max 919 * @param adv_type 920 * @param own_address_type 921 * @param direct_address_type 922 * @param direct_address 923 * @param channel_map 924 * @param filter_policy 925 * 926 * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 927 */ 928 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 929 uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address, 930 uint8_t channel_map, uint8_t filter_policy); 931 932 933 // Only for PTS testing 934 935 /** 936 * Disable automatic L2CAP disconnect if no L2CAP connection is established 937 */ 938 void hci_disable_l2cap_timeout_check(void); 939 940 #if defined __cplusplus 941 } 942 #endif 943 944 #endif // __HCI_H 945