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