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 #ifdef HAVE_BNEP 96 #define HCI_INCOMING_PRE_BUFFER_SIZE (16 - HCI_ACL_HEADER_SIZE - 4) 97 #endif 98 #ifndef HCI_INCOMING_PRE_BUFFER_SIZE 99 #define HCI_INCOMING_PRE_BUFFER_SIZE 0 100 #endif 101 102 // 103 #define IS_COMMAND(packet, command) (little_endian_read_16(packet,0) == command.opcode) 104 105 // check if command complete event for given command 106 #define HCI_EVENT_IS_COMMAND_COMPLETE(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && little_endian_read_16(event,3) == cmd.opcode) 107 #define HCI_EVENT_IS_COMMAND_STATUS(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && little_endian_read_16(event,4) == cmd.opcode) 108 109 // Code+Len=2, Pkts+Opcode=3; total=5 110 #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5 111 112 // ACL Packet 113 #define READ_ACL_CONNECTION_HANDLE( buffer ) ( little_endian_read_16(buffer,0) & 0x0fff) 114 #define READ_ACL_FLAGS( buffer ) ( buffer[1] >> 4 ) 115 #define READ_ACL_LENGTH( buffer ) (little_endian_read_16(buffer, 2)) 116 117 // Sneak peak into L2CAP Packet 118 #define READ_L2CAP_LENGTH(buffer) ( little_endian_read_16(buffer, 4)) 119 #define READ_L2CAP_CHANNEL_ID(buffer) ( little_endian_read_16(buffer, 6)) 120 121 /** 122 * LE connection parameter update state 123 */ 124 125 typedef enum { 126 CON_PARAMETER_UPDATE_NONE, 127 CON_PARAMETER_UPDATE_SEND_REQUEST, 128 CON_PARAMETER_UPDATE_SEND_RESPONSE, 129 CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS, 130 CON_PARAMETER_UPDATE_DENY 131 } le_con_parameter_update_state_t; 132 133 // Authentication flags 134 typedef enum { 135 AUTH_FLAGS_NONE = 0x0000, 136 RECV_LINK_KEY_REQUEST = 0x0001, 137 HANDLE_LINK_KEY_REQUEST = 0x0002, 138 SENT_LINK_KEY_REPLY = 0x0004, 139 SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008, 140 RECV_LINK_KEY_NOTIFICATION = 0x0010, 141 DENY_PIN_CODE_REQUEST = 0x0040, 142 RECV_IO_CAPABILITIES_REQUEST = 0x0080, 143 SEND_IO_CAPABILITIES_REPLY = 0x0100, 144 SEND_USER_CONFIRM_REPLY = 0x0200, 145 SEND_USER_PASSKEY_REPLY = 0x0400, 146 147 // pairing status 148 LEGACY_PAIRING_ACTIVE = 0x2000, 149 SSP_PAIRING_ACTIVE = 0x4000, 150 151 // connection status 152 CONNECTION_ENCRYPTED = 0x8000, 153 } hci_authentication_flags_t; 154 155 /** 156 * Connection State 157 */ 158 typedef enum { 159 SEND_CREATE_CONNECTION = 0, 160 SENT_CREATE_CONNECTION, 161 SEND_CANCEL_CONNECTION, 162 SENT_CANCEL_CONNECTION, 163 RECEIVED_CONNECTION_REQUEST, 164 ACCEPTED_CONNECTION_REQUEST, 165 REJECTED_CONNECTION_REQUEST, 166 OPEN, 167 SEND_DISCONNECT, 168 SENT_DISCONNECT, 169 RECEIVED_DISCONNECTION_COMPLETE 170 } CONNECTION_STATE; 171 172 // bonding flags 173 enum { 174 BONDING_REQUEST_REMOTE_FEATURES = 0x01, 175 BONDING_RECEIVED_REMOTE_FEATURES = 0x02, 176 BONDING_REMOTE_SUPPORTS_SSP = 0x04, 177 BONDING_DISCONNECT_SECURITY_BLOCK = 0x08, 178 BONDING_DISCONNECT_DEDICATED_DONE = 0x10, 179 BONDING_SEND_AUTHENTICATE_REQUEST = 0x20, 180 BONDING_SEND_ENCRYPTION_REQUEST = 0x40, 181 BONDING_DEDICATED = 0x80, 182 BONDING_EMIT_COMPLETE_ON_DISCONNECT = 0x100 183 }; 184 185 typedef enum { 186 BLUETOOTH_OFF = 1, 187 BLUETOOTH_ON, 188 BLUETOOTH_ACTIVE 189 } BLUETOOTH_STATE; 190 191 // le central scanning state 192 typedef enum { 193 LE_SCAN_IDLE, 194 LE_START_SCAN, 195 LE_SCANNING, 196 LE_STOP_SCAN, 197 } le_scanning_state_t; 198 199 typedef enum { 200 LE_CONNECTING_IDLE, 201 LE_CONNECTING_DIRECT, 202 LE_CONNECTING_WHITELIST, 203 } le_connecting_state_t; 204 205 // 206 // SM internal types and globals 207 // 208 209 typedef enum { 210 211 // general states 212 // state = 0 213 SM_GENERAL_IDLE, 214 SM_GENERAL_SEND_PAIRING_FAILED, 215 SM_GENERAL_TIMEOUT, // no other security messages are exchanged 216 217 // Phase 1: Pairing Feature Exchange 218 SM_PH1_W4_USER_RESPONSE, 219 220 // Phase 2: Authenticating and Encrypting 221 222 // get random number for use as TK Passkey if we show it 223 SM_PH2_GET_RANDOM_TK, 224 SM_PH2_W4_RANDOM_TK, 225 226 // get local random number for confirm c1 227 SM_PH2_C1_GET_RANDOM_A, 228 SM_PH2_C1_W4_RANDOM_A, 229 SM_PH2_C1_GET_RANDOM_B, 230 SM_PH2_C1_W4_RANDOM_B, 231 232 // calculate confirm value for local side 233 // state = 10 234 SM_PH2_C1_GET_ENC_A, 235 SM_PH2_C1_W4_ENC_A, 236 SM_PH2_C1_GET_ENC_B, 237 SM_PH2_C1_W4_ENC_B, 238 239 // calculate confirm value for remote side 240 SM_PH2_C1_GET_ENC_C, 241 SM_PH2_C1_W4_ENC_C, 242 SM_PH2_C1_GET_ENC_D, 243 SM_PH2_C1_W4_ENC_D, 244 245 SM_PH2_C1_SEND_PAIRING_CONFIRM, 246 SM_PH2_SEND_PAIRING_RANDOM, 247 248 // calc STK 249 // state = 20 250 SM_PH2_CALC_STK, 251 SM_PH2_W4_STK, 252 253 SM_PH2_W4_CONNECTION_ENCRYPTED, 254 255 // Phase 3: Transport Specific Key Distribution 256 // calculate DHK, Y, EDIV, and LTK 257 SM_PH3_GET_RANDOM, 258 SM_PH3_W4_RANDOM, 259 SM_PH3_GET_DIV, 260 SM_PH3_W4_DIV, 261 SM_PH3_Y_GET_ENC, 262 SM_PH3_Y_W4_ENC, 263 SM_PH3_LTK_GET_ENC, 264 // state = 30 265 SM_PH3_LTK_W4_ENC, 266 SM_PH3_CSRK_GET_ENC, 267 SM_PH3_CSRK_W4_ENC, 268 269 // exchange keys 270 SM_PH3_DISTRIBUTE_KEYS, 271 SM_PH3_RECEIVE_KEYS, 272 273 // RESPONDER ROLE 274 // state = 35 275 SM_RESPONDER_IDLE, 276 SM_RESPONDER_SEND_SECURITY_REQUEST, 277 SM_RESPONDER_PH0_RECEIVED_LTK, 278 SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY, 279 SM_RESPONDER_PH1_W4_PAIRING_REQUEST, 280 SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED, 281 SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE, 282 SM_RESPONDER_PH1_W4_PAIRING_CONFIRM, 283 SM_RESPONDER_PH2_W4_PAIRING_RANDOM, 284 SM_RESPONDER_PH2_W4_LTK_REQUEST, 285 SM_RESPONDER_PH2_SEND_LTK_REPLY, 286 287 // Phase 4: re-establish previously distributed LTK 288 // state == 46 289 SM_RESPONDER_PH4_Y_GET_ENC, 290 SM_RESPONDER_PH4_Y_W4_ENC, 291 SM_RESPONDER_PH4_LTK_GET_ENC, 292 SM_RESPONDER_PH4_LTK_W4_ENC, 293 SM_RESPONDER_PH4_SEND_LTK, 294 295 // INITITIATOR ROLE 296 // state = 51 297 SM_INITIATOR_CONNECTED, 298 SM_INITIATOR_PH0_HAS_LTK, 299 SM_INITIATOR_PH0_SEND_START_ENCRYPTION, 300 SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED, 301 SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST, 302 SM_INITIATOR_PH1_SEND_PAIRING_REQUEST, 303 SM_INITIATOR_PH1_W4_PAIRING_RESPONSE, 304 SM_INITIATOR_PH2_W4_PAIRING_CONFIRM, 305 SM_INITIATOR_PH2_W4_PAIRING_RANDOM, 306 SM_INITIATOR_PH3_SEND_START_ENCRYPTION, 307 308 } security_manager_state_t; 309 310 typedef enum { 311 IRK_LOOKUP_IDLE, 312 IRK_LOOKUP_W4_READY, 313 IRK_LOOKUP_STARTED, 314 IRK_LOOKUP_SUCCEEDED, 315 IRK_LOOKUP_FAILED 316 } irk_lookup_state_t; 317 318 // Authorization state 319 typedef enum { 320 AUTHORIZATION_UNKNOWN, 321 AUTHORIZATION_PENDING, 322 AUTHORIZATION_DECLINED, 323 AUTHORIZATION_GRANTED 324 } authorization_state_t; 325 326 typedef struct sm_pairing_packet { 327 uint8_t code; 328 uint8_t io_capability; 329 uint8_t oob_data_flag; 330 uint8_t auth_req; 331 uint8_t max_encryption_key_size; 332 uint8_t initiator_key_distribution; 333 uint8_t responder_key_distribution; 334 } sm_pairing_packet_t; 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 #ifdef HAVE_TIME 393 // timer 394 struct timeval timestamp; 395 #endif 396 #ifdef HAVE_TICK 397 uint32_t timestamp; // timestamp in system ticks 398 #endif 399 #ifdef HAVE_TIME_MS 400 uint32_t timestamp; // timestamp in ms 401 #endif 402 403 // ACL packet recombination - PRE_BUFFER + ACL Header + ACL payload 404 uint8_t acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4 + HCI_ACL_BUFFER_SIZE]; 405 uint16_t acl_recombination_pos; 406 uint16_t acl_recombination_length; 407 408 // number packets sent to controller 409 uint8_t num_acl_packets_sent; 410 uint8_t num_sco_packets_sent; 411 412 // LE Connection parameter update 413 le_con_parameter_update_state_t le_con_parameter_update_state; 414 uint8_t le_con_param_update_identifier; 415 uint16_t le_conn_interval_min; 416 uint16_t le_conn_interval_max; 417 uint16_t le_conn_latency; 418 uint16_t le_supervision_timeout; 419 420 #ifdef ENABLE_BLE 421 // LE Security Manager 422 sm_connection_t sm_connection; 423 #endif 424 425 } hci_connection_t; 426 427 428 /** 429 * HCI Inititizlization State Machine 430 */ 431 typedef enum hci_init_state{ 432 HCI_INIT_SEND_RESET = 0, 433 HCI_INIT_W4_SEND_RESET, 434 HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION, 435 HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION, 436 437 HCI_INIT_SEND_BAUD_CHANGE, 438 HCI_INIT_W4_SEND_BAUD_CHANGE, 439 HCI_INIT_CUSTOM_INIT, 440 HCI_INIT_W4_CUSTOM_INIT, 441 HCI_INIT_SEND_RESET_CSR_WARM_BOOT, 442 HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT, 443 444 HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS, 445 HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS, 446 447 HCI_INIT_SEND_BAUD_CHANGE_BCM, 448 HCI_INIT_W4_SEND_BAUD_CHANGE_BCM, 449 450 HCI_INIT_SET_BD_ADDR, 451 HCI_INIT_W4_SET_BD_ADDR, 452 453 HCI_INIT_SEND_RESET_ST_WARM_BOOT, 454 HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT, 455 456 HCI_INIT_READ_BD_ADDR, 457 HCI_INIT_W4_READ_BD_ADDR, 458 459 HCI_INIT_READ_BUFFER_SIZE, 460 HCI_INIT_W4_READ_BUFFER_SIZE, 461 HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES, 462 HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES, 463 HCI_INIT_SET_EVENT_MASK, 464 HCI_INIT_W4_SET_EVENT_MASK, 465 HCI_INIT_WRITE_SIMPLE_PAIRING_MODE, 466 HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE, 467 HCI_INIT_WRITE_PAGE_TIMEOUT, 468 HCI_INIT_W4_WRITE_PAGE_TIMEOUT, 469 HCI_INIT_WRITE_CLASS_OF_DEVICE, 470 HCI_INIT_W4_WRITE_CLASS_OF_DEVICE, 471 HCI_INIT_WRITE_LOCAL_NAME, 472 HCI_INIT_W4_WRITE_LOCAL_NAME, 473 HCI_INIT_WRITE_SCAN_ENABLE, 474 HCI_INIT_W4_WRITE_SCAN_ENABLE, 475 476 HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, 477 HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, 478 479 HCI_INIT_LE_READ_BUFFER_SIZE, 480 HCI_INIT_W4_LE_READ_BUFFER_SIZE, 481 HCI_INIT_WRITE_LE_HOST_SUPPORTED, 482 HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED, 483 HCI_INIT_READ_WHITE_LIST_SIZE, 484 HCI_INIT_W4_READ_WHITE_LIST_SIZE, 485 486 HCI_INIT_LE_SET_SCAN_PARAMETERS, 487 HCI_INIT_W4_LE_SET_SCAN_PARAMETERS, 488 489 HCI_INIT_DONE, 490 491 HCI_FALLING_ASLEEP_DISCONNECT, 492 HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE, 493 HCI_FALLING_ASLEEP_COMPLETE, 494 495 HCI_INIT_AFTER_SLEEP 496 497 } hci_substate_t; 498 499 enum { 500 LE_ADVERTISEMENT_TASKS_DISABLE = 1 << 0, 501 LE_ADVERTISEMENT_TASKS_SET_DATA = 1 << 1, 502 LE_ADVERTISEMENT_TASKS_SET_PARAMS = 1 << 2, 503 LE_ADVERTISEMENT_TASKS_ENABLE = 1 << 3, 504 }; 505 506 enum { 507 LE_WHITELIST_ON_CONTROLLER = 1 << 0, 508 LE_WHITELIST_ADD_TO_CONTROLLER = 1 << 1, 509 LE_WHITELIST_REMOVE_FROM_CONTROLLER = 1 << 2, 510 }; 511 512 typedef struct { 513 btstack_linked_item_t item; 514 bd_addr_t address; 515 bd_addr_type_t address_type; 516 uint8_t state; 517 } whitelist_entry_t; 518 519 /** 520 * main data structure 521 */ 522 typedef struct { 523 // transport component with configuration 524 const hci_transport_t * hci_transport; 525 const void * config; 526 527 // chipset driver 528 const btstack_chipset_t * chipset; 529 530 // hardware power controller 531 const btstack_control_t * control; 532 533 /* link key db */ 534 const btstack_link_key_db_t * link_key_db; 535 536 // list of existing baseband connections 537 btstack_linked_list_t connections; 538 539 /* callback to L2CAP layer */ 540 void (*acl_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 541 542 /* callback for SCO data */ 543 void (*sco_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 544 545 /* callbacks for events */ 546 btstack_linked_list_t event_handlers; 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_advertisements_active; 637 uint8_t le_advertisements_enabled; 638 uint8_t le_advertisements_todo; 639 640 uint16_t le_advertisements_interval_min; 641 uint16_t le_advertisements_interval_max; 642 uint8_t le_advertisements_type; 643 uint8_t le_advertisements_own_address_type; 644 uint8_t le_advertisements_direct_address_type; 645 uint8_t le_advertisements_channel_map; 646 uint8_t le_advertisements_filter_policy; 647 bd_addr_t le_advertisements_direct_address; 648 649 // LE Whitelist Management 650 uint16_t le_whitelist_capacity; 651 btstack_linked_list_t le_whitelist; 652 653 // custom BD ADDR 654 bd_addr_t custom_bd_addr; 655 uint8_t custom_bd_addr_set; 656 657 } hci_stack_t; 658 659 660 /* API_START */ 661 662 663 // HCI init and configuration 664 665 666 /** 667 * @brief Set up HCI. Needs to be called before any other function. 668 */ 669 void hci_init(const hci_transport_t *transport, const void *config); 670 671 /** 672 * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information. 673 */ 674 void hci_set_chipset(const btstack_chipset_t *chipset_driver); 675 676 /** 677 * @brief Configure Bluetooth hardware control. Has to be called before power on. 678 */ 679 void hci_set_control(const btstack_control_t *hardware_control); 680 681 /** 682 * @brief Configure Bluetooth hardware control. Has to be called before power on. 683 */ 684 void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db); 685 686 /** 687 * @brief Set class of device that will be set during Bluetooth init. 688 */ 689 void hci_set_class_of_device(uint32_t class_of_device); 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 Public BD ADDR - passed on to Bluetooth chipset during init if supported in bt_control_h 698 */ 699 void hci_set_bd_addr(bd_addr_t addr); 700 701 /** 702 * @brief Configure Voice Setting for use with SCO data in HSP/HFP 703 */ 704 void hci_set_sco_voice_setting(uint16_t voice_setting); 705 706 /** 707 * @brief Get SCO Voice Setting 708 * @return current voice setting 709 */ 710 uint16_t hci_get_sco_voice_setting(void); 711 712 /** 713 * @brief Requests the change of BTstack power mode. 714 */ 715 int hci_power_control(HCI_POWER_MODE mode); 716 717 /** 718 * @brief Shutdown HCI 719 */ 720 void hci_close(void); 721 722 723 // Callback registration 724 725 726 /** 727 * @brief Add event packet handler. 728 */ 729 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler); 730 731 /** 732 * @brief Registers a packet handler for ACL data. Used by L2CAP 733 */ 734 void hci_register_acl_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 735 736 /** 737 * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 738 */ 739 void hci_register_sco_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)); 740 741 742 // Sending HCI Commands 743 744 /** 745 * @brief Check if CMD packet can be sent to controller 746 */ 747 int hci_can_send_command_packet_now(void); 748 749 /** 750 * @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. 751 */ 752 int hci_send_cmd(const hci_cmd_t *cmd, ...); 753 754 755 // Sending SCO Packets 756 757 /** @brief Get SCO packet length for current SCO Voice setting 758 * @note Using SCO packets of the exact length is required for USB transfer 759 * @return Length of SCO packets in bytes (not audio frames) incl. 3 byte header 760 */ 761 int hci_get_sco_packet_length(void); 762 763 /** 764 * @brief Check hci packet buffer and if SCO packet can be sent to controller 765 */ 766 int hci_can_send_sco_packet_now(void); 767 768 /** 769 * @brief Check if SCO packet can be sent to controller 770 */ 771 int hci_can_send_prepared_sco_packet_now(void); 772 773 /** 774 * @brief Send SCO packet prepared in hci packet buffer 775 */ 776 int hci_send_sco_packet_buffer(int size); 777 778 779 // Outgoing packet buffer, also used for SCO packets 780 // see hci_can_send_prepared_sco_packet_now amn hci_send_sco_packet_buffer 781 782 /** 783 * Reserves outgoing packet buffer. 784 * @return 1 on success 785 */ 786 int hci_reserve_packet_buffer(void); 787 788 /** 789 * Get pointer for outgoing packet buffer 790 */ 791 uint8_t* hci_get_outgoing_packet_buffer(void); 792 793 /** 794 * Release outgoing packet buffer\ 795 * @note only called instead of hci_send_preparared 796 */ 797 void hci_release_packet_buffer(void); 798 799 800 /* API_END */ 801 802 803 804 /** 805 * Get connection iterator. Only used by l2cap.c and sm.c 806 */ 807 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it); 808 809 /** 810 * Get internal hci_connection_t for given handle. Used by L2CAP, SM, daemon 811 */ 812 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle); 813 814 /** 815 * Get internal hci_connection_t for given Bluetooth addres. Called by L2CAP 816 */ 817 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type); 818 819 /** 820 * Check if outgoing packet buffer is reserved. Used for internal checks in l2cap.c 821 */ 822 int hci_is_packet_buffer_reserved(void); 823 824 /** 825 * Check hci packet buffer is free and a classic acl packet can be sent to controller 826 */ 827 int hci_can_send_acl_classic_packet_now(void); 828 829 /** 830 * Check hci packet buffer is free and an LE acl packet can be sent to controller 831 */ 832 int hci_can_send_acl_le_packet_now(void); 833 834 /** 835 * Check hci packet buffer is free and an acl packet for the given handle can be sent to controller 836 */ 837 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle); 838 839 /** 840 * Check if acl packet for the given handle can be sent to controller 841 */ 842 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle); 843 844 /** 845 * Send acl packet prepared in hci packet buffer 846 */ 847 int hci_send_acl_packet_buffer(int size); 848 849 /** 850 * Check if authentication is active. It delays automatic disconnect while no L2CAP connection 851 * Called by l2cap. 852 */ 853 int hci_authentication_active_for_handle(hci_con_handle_t handle); 854 855 /** 856 * Get maximal ACL Classic data packet length based on used buffer size. Called by L2CAP 857 */ 858 uint16_t hci_max_acl_data_packet_length(void); 859 860 /** 861 * Get supported packet types. Called by L2CAP 862 */ 863 uint16_t hci_usable_acl_packet_types(void); 864 865 /** 866 * Check if ACL packets marked as non flushable can be sent. Called by L2CAP 867 */ 868 int hci_non_flushable_packet_boundary_flag_supported(void); 869 870 /** 871 * Check if SSP is supported on both sides. Called by L2CAP 872 */ 873 int gap_ssp_supported_on_both_sides(hci_con_handle_t handle); 874 875 /** 876 * Disconn because of security block. Called by L2CAP 877 */ 878 void hci_disconnect_security_block(hci_con_handle_t con_handle); 879 880 /** 881 * Query if remote side supports eSCO 882 */ 883 int hci_remote_esco_supported(hci_con_handle_t con_handle); 884 885 /** 886 * Emit current HCI state. Called by daemon 887 */ 888 void hci_emit_state(void); 889 890 /** 891 * Send complete CMD packet. Called by daemon 892 */ 893 int hci_send_cmd_packet(uint8_t *packet, int size); 894 895 /** 896 * Disconnect all HCI connections. Called by daemon 897 */ 898 void hci_disconnect_all(void); 899 900 /** 901 * Get number of free acl slots for packets of given handle. Called by daemon 902 */ 903 int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle); 904 905 /** 906 * @brief Set Advertisement Parameters 907 * @param adv_int_min 908 * @param adv_int_max 909 * @param adv_type 910 * @param own_address_type 911 * @param direct_address_type 912 * @param direct_address 913 * @param channel_map 914 * @param filter_policy 915 * 916 * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 917 */ 918 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 919 uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address, 920 uint8_t channel_map, uint8_t filter_policy); 921 922 923 // Only for PTS testing 924 925 /** 926 * Disable automatic L2CAP disconnect if no L2CAP connection is established 927 */ 928 void hci_disable_l2cap_timeout_check(void); 929 930 #if defined __cplusplus 931 } 932 #endif 933 934 #endif // __HCI_H 935