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