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