1 /****************************************************************************** 2 * 3 * Copyright 1999-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 #ifndef BTM_INT_TYPES_H 19 #define BTM_INT_TYPES_H 20 21 #include "btif/include/btif_bqr.h" 22 #include "btm_api_types.h" 23 #include "btm_ble_api_types.h" 24 #include "btm_ble_int_types.h" 25 #include "hcidefs.h" 26 #include "osi/include/alarm.h" 27 #include "osi/include/list.h" 28 #include "rfcdefs.h" 29 30 typedef char tBTM_LOC_BD_NAME[BTM_MAX_LOC_BD_NAME_LEN + 1]; 31 32 #define BTM_ACL_IS_CONNECTED(bda) \ 33 (btm_bda_to_acl(bda, BT_TRANSPORT_BR_EDR) != NULL) 34 35 /* Definitions for Server Channel Number (SCN) management 36 */ 37 #define BTM_MAX_SCN PORT_MAX_RFC_PORTS 38 39 /* Define masks for supported and exception 2.0 ACL packet types 40 */ 41 #define BTM_ACL_SUPPORTED_PKTS_MASK \ 42 (HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM3 | \ 43 HCI_PKT_TYPES_MASK_DH3 | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5) 44 45 #define BTM_ACL_EXCEPTION_PKTS_MASK \ 46 (HCI_PKT_TYPES_MASK_NO_2_DH1 | HCI_PKT_TYPES_MASK_NO_3_DH1 | \ 47 HCI_PKT_TYPES_MASK_NO_2_DH3 | HCI_PKT_TYPES_MASK_NO_3_DH3 | \ 48 HCI_PKT_TYPES_MASK_NO_2_DH5 | HCI_PKT_TYPES_MASK_NO_3_DH5) 49 50 #define BTM_EPR_AVAILABLE(p) \ 51 ((HCI_ATOMIC_ENCRYPT_SUPPORTED((p)->peer_lmp_feature_pages[0]) && \ 52 HCI_ATOMIC_ENCRYPT_SUPPORTED( \ 53 controller_get_interface()->get_features_classic(0)->as_array)) \ 54 ? true \ 55 : false) 56 57 #define BTM_IS_BRCM_CONTROLLER() \ 58 (controller_get_interface()->get_bt_version()->manufacturer == \ 59 LMP_COMPID_BROADCOM) 60 61 /* Define the ACL Management control structure 62 */ 63 typedef struct { 64 uint16_t hci_handle; 65 uint16_t pkt_types_mask; 66 uint16_t clock_offset; 67 RawAddress remote_addr; 68 DEV_CLASS remote_dc; 69 BD_NAME remote_name; 70 71 uint16_t manufacturer; 72 uint16_t lmp_subversion; 73 uint16_t link_super_tout; 74 BD_FEATURES 75 peer_lmp_feature_pages[HCI_EXT_FEATURES_PAGE_MAX + 1]; /* Peer LMP Extended 76 features mask table 77 for the device */ 78 uint8_t num_read_pages; 79 uint8_t lmp_version; 80 81 bool in_use; 82 uint8_t link_role; 83 bool link_up_issued; /* True if busy_level link up has been issued */ 84 85 #define BTM_ACL_SWKEY_STATE_IDLE 0 86 #define BTM_ACL_SWKEY_STATE_MODE_CHANGE 1 87 #define BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF 2 88 #define BTM_ACL_SWKEY_STATE_SWITCHING 3 89 #define BTM_ACL_SWKEY_STATE_ENCRYPTION_ON 4 90 #define BTM_ACL_SWKEY_STATE_IN_PROGRESS 5 91 uint8_t switch_role_state; 92 93 #define BTM_MAX_SW_ROLE_FAILED_ATTEMPTS 3 94 uint8_t switch_role_failed_attempts; 95 96 #define BTM_ACL_ENCRYPT_STATE_IDLE 0 97 #define BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF 1 /* encryption turning off */ 98 #define BTM_ACL_ENCRYPT_STATE_TEMP_FUNC \ 99 2 /* temporarily off for change link key or role switch */ 100 #define BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON 3 /* encryption turning on */ 101 uint8_t encrypt_state; /* overall BTM encryption state */ 102 103 tBT_TRANSPORT transport; 104 RawAddress conn_addr; /* local device address used for this connection */ 105 uint8_t conn_addr_type; /* local device address type for this connection */ 106 RawAddress active_remote_addr; /* remote address used on this connection */ 107 uint8_t active_remote_addr_type; /* local device address type for this 108 connection */ 109 BD_FEATURES peer_le_features; /* Peer LE Used features mask for the device */ 110 111 } tACL_CONN; 112 113 /* Define the Device Management control structure 114 */ 115 typedef struct { 116 tBTM_DEV_STATUS_CB* p_dev_status_cb; /* Device status change callback */ 117 tBTM_VS_EVT_CB* p_vend_spec_cb 118 [BTM_MAX_VSE_CALLBACKS]; /* Register for vendor specific events */ 119 120 tBTM_CMPL_CB* 121 p_stored_link_key_cmpl_cb; /* Read/Write/Delete stored link key */ 122 123 alarm_t* read_local_name_timer; /* Read local name timer */ 124 tBTM_CMPL_CB* p_rln_cmpl_cb; /* Callback function to be called when */ 125 /* read local name function complete */ 126 127 alarm_t* read_rssi_timer; /* Read RSSI timer */ 128 tBTM_CMPL_CB* p_rssi_cmpl_cb; /* Callback function to be called when */ 129 /* read RSSI function completes */ 130 131 alarm_t* read_failed_contact_counter_timer; /* Read Failed Contact Counter */ 132 /* timer */ 133 tBTM_CMPL_CB* p_failed_contact_counter_cmpl_cb; /* Callback function to be */ 134 /* called when read Failed Contact Counter function completes */ 135 136 alarm_t* 137 read_automatic_flush_timeout_timer; /* Read Automatic Flush Timeout */ 138 /* timer */ 139 tBTM_CMPL_CB* p_automatic_flush_timeout_cmpl_cb; /* Callback function to be */ 140 /* called when read Automatic Flush Timeout function completes */ 141 142 alarm_t* read_link_quality_timer; 143 tBTM_CMPL_CB* p_link_qual_cmpl_cb; /* Callback function to be called when */ 144 /* read link quality function completes */ 145 146 alarm_t* read_inq_tx_power_timer; 147 tBTM_CMPL_CB* 148 p_inq_tx_power_cmpl_cb; /* Callback function to be called when */ 149 /* read inq tx power function completes */ 150 151 alarm_t* qos_setup_timer; /* QoS setup timer */ 152 tBTM_CMPL_CB* p_qos_setup_cmpl_cb; /* Callback function to be called when */ 153 /* qos setup function completes */ 154 155 tBTM_ROLE_SWITCH_CMPL switch_role_ref_data; 156 tBTM_CMPL_CB* p_switch_role_cb; /* Callback function to be called when */ 157 /* requested switch role is completed */ 158 159 alarm_t* read_tx_power_timer; /* Read tx power timer */ 160 tBTM_CMPL_CB* p_tx_power_cmpl_cb; /* Callback function to be called */ 161 162 DEV_CLASS dev_class; /* Local device class */ 163 164 tBTM_CMPL_CB* 165 p_le_test_cmd_cmpl_cb; /* Callback function to be called when 166 LE test mode command has been sent successfully */ 167 168 RawAddress read_tx_pwr_addr; /* read TX power target address */ 169 170 #define BTM_LE_SUPPORT_STATE_SIZE 8 171 uint8_t le_supported_states[BTM_LE_SUPPORT_STATE_SIZE]; 172 173 tBTM_BLE_LOCAL_ID_KEYS id_keys; /* local BLE ID keys */ 174 Octet16 ble_encryption_key_value; /* BLE encryption key */ 175 176 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE) 177 bool no_disc_if_pair_fail; 178 bool enable_test_mac_val; 179 BT_OCTET8 test_mac; 180 bool enable_test_local_sign_cntr; 181 uint32_t test_local_sign_cntr; 182 #endif 183 184 tBTM_IO_CAP loc_io_caps; /* IO capability of the local device */ 185 tBTM_AUTH_REQ loc_auth_req; /* the auth_req flag */ 186 bool secure_connections_only; /* Rejects service level 0 connections if */ 187 /* itself or peer device doesn't support */ 188 /* secure connections */ 189 } tBTM_DEVCB; 190 191 /* Define the structures and constants used for inquiry 192 */ 193 194 /* Definitions of limits for inquiries */ 195 #define BTM_PER_INQ_MIN_MAX_PERIOD HCI_PER_INQ_MIN_MAX_PERIOD 196 #define BTM_PER_INQ_MAX_MAX_PERIOD HCI_PER_INQ_MAX_MAX_PERIOD 197 #define BTM_PER_INQ_MIN_MIN_PERIOD HCI_PER_INQ_MIN_MIN_PERIOD 198 #define BTM_PER_INQ_MAX_MIN_PERIOD HCI_PER_INQ_MAX_MIN_PERIOD 199 #define BTM_MAX_INQUIRY_LENGTH HCI_MAX_INQUIRY_LENGTH 200 #define BTM_MIN_INQUIRY_LEN 0x01 201 202 #define BTM_MIN_INQ_TX_POWER (-70) 203 #define BTM_MAX_INQ_TX_POWER 20 204 205 typedef struct { 206 uint32_t inq_count; /* Used for determining if a response has already been */ 207 /* received for the current inquiry operation. (We do not */ 208 /* want to flood the caller with multiple responses from */ 209 /* the same device. */ 210 RawAddress bd_addr; 211 } tINQ_BDADDR; 212 213 typedef struct { 214 uint64_t time_of_resp; 215 uint32_t 216 inq_count; /* "timestamps" the entry with a particular inquiry count */ 217 /* Used for determining if a response has already been */ 218 /* received for the current inquiry operation. (We do not */ 219 /* want to flood the caller with multiple responses from */ 220 /* the same device. */ 221 tBTM_INQ_INFO inq_info; 222 bool in_use; 223 bool scan_rsp; 224 } tINQ_DB_ENT; 225 226 enum { INQ_NONE, INQ_GENERAL }; 227 typedef uint8_t tBTM_INQ_TYPE; 228 229 typedef struct { 230 tBTM_CMPL_CB* p_remname_cmpl_cb; 231 232 #define BTM_EXT_RMT_NAME_TIMEOUT_MS (40 * 1000) /* 40 seconds */ 233 234 alarm_t* remote_name_timer; 235 236 uint16_t discoverable_mode; 237 uint16_t connectable_mode; 238 uint16_t page_scan_window; 239 uint16_t page_scan_period; 240 uint16_t inq_scan_window; 241 uint16_t inq_scan_period; 242 uint16_t inq_scan_type; 243 uint16_t page_scan_type; /* current page scan type */ 244 tBTM_INQ_TYPE scan_type; 245 246 RawAddress remname_bda; /* Name of bd addr for active remote name request */ 247 #define BTM_RMT_NAME_INACTIVE 0 248 #define BTM_RMT_NAME_EXT 0x1 /* Initiated through API */ 249 #define BTM_RMT_NAME_SEC 0x2 /* Initiated internally by security manager */ 250 #define BTM_RMT_NAME_INQ 0x4 /* Remote name initiated internally by inquiry */ 251 bool remname_active; /* State of a remote name request by external API */ 252 253 tBTM_CMPL_CB* p_inq_cmpl_cb; 254 tBTM_INQ_RESULTS_CB* p_inq_results_cb; 255 tBTM_CMPL_CB* p_inqfilter_cmpl_cb; /* Called (if not NULL) after inquiry 256 filter completed */ 257 uint32_t inq_counter; /* Counter incremented each time an inquiry completes */ 258 /* Used for determining whether or not duplicate devices */ 259 /* have responded to the same inquiry */ 260 tINQ_BDADDR* p_bd_db; /* Pointer to memory that holds bdaddrs */ 261 uint16_t num_bd_entries; /* Number of entries in database */ 262 uint16_t max_bd_entries; /* Maximum number of entries that can be stored */ 263 tINQ_DB_ENT inq_db[BTM_INQ_DB_SIZE]; 264 tBTM_INQ_PARMS inqparms; /* Contains the parameters for the current inquiry */ 265 tBTM_INQUIRY_CMPL 266 inq_cmpl_info; /* Status and number of responses from the last inquiry */ 267 268 uint16_t per_min_delay; /* Current periodic minimum delay */ 269 uint16_t per_max_delay; /* Current periodic maximum delay */ 270 bool inqfilt_active; 271 uint8_t pending_filt_complete_event; /* to take care of 272 btm_event_filter_complete 273 corresponding to */ 274 /* inquiry that has been cancelled*/ 275 uint8_t inqfilt_type; /* Contains the inquiry filter type (BD ADDR, COD, or 276 Clear) */ 277 278 #define BTM_INQ_INACTIVE_STATE 0 279 #define BTM_INQ_CLR_FILT_STATE \ 280 1 /* Currently clearing the inquiry filter preceeding the inquiry request */ 281 /* (bypassed if filtering is not used) */ 282 #define BTM_INQ_SET_FILT_STATE \ 283 2 /* Sets the new filter (or turns off filtering) in this state */ 284 #define BTM_INQ_ACTIVE_STATE \ 285 3 /* Actual inquiry or periodic inquiry is in progress */ 286 #define BTM_INQ_REMNAME_STATE 4 /* Remote name requests are active */ 287 288 uint8_t state; /* Current state that the inquiry process is in */ 289 uint8_t inq_active; /* Bit Mask indicating type of inquiry is active */ 290 bool no_inc_ssp; /* true, to stop inquiry on incoming SSP */ 291 } tBTM_INQUIRY_VAR_ST; 292 293 /* The MSB of the clock offset field indicates whether the offset is valid. */ 294 #define BTM_CLOCK_OFFSET_VALID 0x8000 295 296 /* Define the structures needed by security management 297 */ 298 299 #define BTM_SEC_INVALID_HANDLE 0xFFFF 300 301 typedef uint8_t* BTM_BD_NAME_PTR; /* Pointer to Device name */ 302 303 /* Security callback is called by this unit when security 304 * procedures are completed. Parameters are 305 * BD Address of remote 306 * Result of the operation 307 */ 308 typedef tBTM_SEC_CBACK tBTM_SEC_CALLBACK; 309 310 typedef void(tBTM_SCO_IND_CBACK)(uint16_t sco_inx); 311 312 /* MACROs to convert from SCO packet types mask to ESCO and back */ 313 #define BTM_SCO_PKT_TYPE_MASK \ 314 (HCI_PKT_TYPES_MASK_HV1 | HCI_PKT_TYPES_MASK_HV2 | HCI_PKT_TYPES_MASK_HV3) 315 316 /* Mask defining only the SCO types of an esco packet type */ 317 #define BTM_ESCO_PKT_TYPE_MASK \ 318 (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | ESCO_PKT_TYPES_MASK_HV3) 319 320 #define BTM_SCO_2_ESCO(scotype) \ 321 ((uint16_t)(((scotype)&BTM_SCO_PKT_TYPE_MASK) >> 5)) 322 #define BTM_ESCO_2_SCO(escotype) \ 323 ((uint16_t)(((escotype)&BTM_ESCO_PKT_TYPE_MASK) << 5)) 324 325 /* Define masks for supported and exception 2.0 SCO packet types 326 */ 327 #define BTM_SCO_SUPPORTED_PKTS_MASK \ 328 (ESCO_PKT_TYPES_MASK_HV1 | ESCO_PKT_TYPES_MASK_HV2 | \ 329 ESCO_PKT_TYPES_MASK_HV3 | ESCO_PKT_TYPES_MASK_EV3 | \ 330 ESCO_PKT_TYPES_MASK_EV4 | ESCO_PKT_TYPES_MASK_EV5) 331 332 #define BTM_SCO_EXCEPTION_PKTS_MASK \ 333 (ESCO_PKT_TYPES_MASK_NO_2_EV3 | ESCO_PKT_TYPES_MASK_NO_3_EV3 | \ 334 ESCO_PKT_TYPES_MASK_NO_2_EV5 | ESCO_PKT_TYPES_MASK_NO_3_EV5) 335 336 #define BTM_SCO_ROUTE_UNKNOWN 0xff 337 338 /* Define the structure that contains (e)SCO data */ 339 typedef struct { 340 tBTM_ESCO_CBACK* p_esco_cback; /* Callback for eSCO events */ 341 enh_esco_params_t setup; 342 tBTM_ESCO_DATA data; /* Connection complete information */ 343 uint8_t hci_status; 344 } tBTM_ESCO_INFO; 345 346 /* Define the structure used for SCO Management 347 */ 348 typedef struct { 349 tBTM_ESCO_INFO esco; /* Current settings */ 350 tBTM_SCO_CB* p_conn_cb; /* Callback for when connected */ 351 tBTM_SCO_CB* p_disc_cb; /* Callback for when disconnect */ 352 uint16_t state; /* The state of the SCO link */ 353 uint16_t hci_handle; /* HCI Handle */ 354 bool is_orig; /* true if the originator */ 355 bool rem_bd_known; /* true if remote BD addr known */ 356 357 } tSCO_CONN; 358 359 /* SCO Management control block */ 360 typedef struct { 361 tBTM_SCO_IND_CBACK* app_sco_ind_cb; 362 tSCO_CONN sco_db[BTM_MAX_SCO_LINKS]; 363 enh_esco_params_t def_esco_parms; 364 uint16_t sco_disc_reason; 365 bool esco_supported; /* true if 1.2 cntlr AND supports eSCO links */ 366 esco_data_path_t sco_route; /* HCI, PCM, or TEST */ 367 } tSCO_CB; 368 369 extern void btm_set_sco_ind_cback(tBTM_SCO_IND_CBACK* sco_ind_cb); 370 extern void btm_accept_sco_link(uint16_t sco_inx, enh_esco_params_t* p_setup, 371 tBTM_SCO_CB* p_conn_cb, tBTM_SCO_CB* p_disc_cb); 372 extern void btm_reject_sco_link(uint16_t sco_inx); 373 extern void btm_sco_chk_pend_rolechange(uint16_t hci_handle); 374 extern void btm_sco_disc_chk_pend_for_modechange(uint16_t hci_handle); 375 376 /* 377 * Define structure for Security Service Record. 378 * A record exists for each service registered with the Security Manager 379 */ 380 #define BTM_SEC_OUT_FLAGS \ 381 (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHORIZE) 382 #define BTM_SEC_IN_FLAGS \ 383 (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHORIZE) 384 385 #define BTM_SEC_OUT_LEVEL4_FLAGS \ 386 (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_MITM | \ 387 BTM_SEC_MODE4_LEVEL4) 388 389 #define BTM_SEC_IN_LEVEL4_FLAGS \ 390 (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_MITM | \ 391 BTM_SEC_MODE4_LEVEL4) 392 typedef struct { 393 uint32_t mx_proto_id; /* Service runs over this multiplexer protocol */ 394 uint32_t orig_mx_chan_id; /* Channel on the multiplexer protocol */ 395 uint32_t term_mx_chan_id; /* Channel on the multiplexer protocol */ 396 uint16_t psm; /* L2CAP PSM value */ 397 uint16_t security_flags; /* Bitmap of required security features */ 398 uint8_t service_id; /* Passed in authorization callback */ 399 #if BTM_SEC_SERVICE_NAME_LEN > 0 400 uint8_t orig_service_name[BTM_SEC_SERVICE_NAME_LEN + 1]; 401 uint8_t term_service_name[BTM_SEC_SERVICE_NAME_LEN + 1]; 402 #endif 403 } tBTM_SEC_SERV_REC; 404 405 /* LE Security information of device in Slave Role */ 406 typedef struct { 407 Octet16 irk; /* peer diverified identity root */ 408 Octet16 pltk; /* peer long term key */ 409 Octet16 pcsrk; /* peer SRK peer device used to secured sign local data */ 410 411 Octet16 lltk; /* local long term key */ 412 Octet16 lcsrk; /* local SRK peer device used to secured sign local data */ 413 414 BT_OCTET8 rand; /* random vector for LTK generation */ 415 uint16_t ediv; /* LTK diversifier of this slave device */ 416 uint16_t div; /* local DIV to generate local LTK=d1(ER,DIV,0) and 417 CSRK=d1(ER,DIV,1) */ 418 uint8_t sec_level; /* local pairing security level */ 419 uint8_t key_size; /* key size of the LTK delivered to peer device */ 420 uint8_t srk_sec_level; /* security property of peer SRK for this device */ 421 uint8_t local_csrk_sec_level; /* security property of local CSRK for this 422 device */ 423 424 uint32_t counter; /* peer sign counter for verifying rcv signed cmd */ 425 uint32_t local_counter; /* local sign counter for sending signed write cmd*/ 426 } tBTM_SEC_BLE_KEYS; 427 428 typedef struct { 429 RawAddress pseudo_addr; /* LE pseudo address of the device if different from 430 device address */ 431 tBLE_ADDR_TYPE ble_addr_type; /* LE device type: public or random address */ 432 tBLE_ADDR_TYPE identity_addr_type; /* identity address type */ 433 RawAddress identity_addr; /* identity address */ 434 435 #define BTM_WHITE_LIST_BIT 0x01 436 #define BTM_RESOLVING_LIST_BIT 0x02 437 uint8_t in_controller_list; /* in controller resolving list or not */ 438 uint8_t resolving_list_index; 439 #if (BLE_PRIVACY_SPT == TRUE) 440 RawAddress cur_rand_addr; /* current random address */ 441 442 #define BTM_BLE_ADDR_PSEUDO 0 /* address index device record */ 443 #define BTM_BLE_ADDR_RRA 1 /* cur_rand_addr */ 444 #define BTM_BLE_ADDR_STATIC 2 /* static_addr */ 445 uint8_t active_addr_type; 446 #endif 447 448 tBTM_LE_KEY_TYPE key_type; /* bit mask of valid key types in record */ 449 tBTM_SEC_BLE_KEYS keys; /* LE device security info in slave rode */ 450 } tBTM_SEC_BLE; 451 452 /* Peering bond type */ 453 enum { BOND_TYPE_UNKNOWN, BOND_TYPE_PERSISTENT, BOND_TYPE_TEMPORARY }; 454 typedef uint8_t tBTM_BOND_TYPE; 455 456 /* 457 * Define structure for Security Device Record. 458 * A record exists for each device authenticated with this device 459 */ 460 typedef struct { 461 tBTM_SEC_SERV_REC* p_cur_service; 462 tBTM_SEC_CALLBACK* p_callback; 463 void* p_ref_data; 464 uint32_t timestamp; /* Timestamp of the last connection */ 465 uint32_t trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE]; /* Bitwise OR of trusted 466 services */ 467 uint16_t hci_handle; /* Handle to connection when exists */ 468 uint16_t clock_offset; /* Latest known clock offset */ 469 RawAddress bd_addr; /* BD_ADDR of the device */ 470 DEV_CLASS dev_class; /* DEV_CLASS of the device */ 471 LinkKey link_key; /* Device link key */ 472 uint8_t pin_code_length; /* Length of the pin_code used for paring */ 473 474 #define BTM_SEC_AUTHORIZED BTM_SEC_FLAG_AUTHORIZED /* 0x01 */ 475 #define BTM_SEC_AUTHENTICATED BTM_SEC_FLAG_AUTHENTICATED /* 0x02 */ 476 #define BTM_SEC_ENCRYPTED BTM_SEC_FLAG_ENCRYPTED /* 0x04 */ 477 #define BTM_SEC_NAME_KNOWN 0x08 478 #define BTM_SEC_LINK_KEY_KNOWN BTM_SEC_FLAG_LKEY_KNOWN /* 0x10 */ 479 #define BTM_SEC_LINK_KEY_AUTHED BTM_SEC_FLAG_LKEY_AUTHED /* 0x20 */ 480 #define BTM_SEC_ROLE_SWITCHED 0x40 481 #define BTM_SEC_IN_USE 0x80 482 /* LE link security flag */ 483 #define BTM_SEC_LE_AUTHENTICATED \ 484 0x0200 /* LE link is encrypted after pairing with MITM */ 485 #define BTM_SEC_LE_ENCRYPTED 0x0400 /* LE link is encrypted */ 486 #define BTM_SEC_LE_NAME_KNOWN 0x0800 /* not used */ 487 #define BTM_SEC_LE_LINK_KEY_KNOWN \ 488 0x1000 /* bonded with peer (peer LTK and/or SRK is saved) */ 489 #define BTM_SEC_LE_LINK_KEY_AUTHED 0x2000 /* pairing is done with MITM */ 490 #define BTM_SEC_16_DIGIT_PIN_AUTHED \ 491 0x4000 /* pairing is done with 16 digit pin */ 492 493 uint16_t sec_flags; /* Current device security state */ 494 495 tBTM_BD_NAME sec_bd_name; /* User friendly name of the device. (may be 496 truncated to save space in dev_rec table) */ 497 BD_FEATURES feature_pages[HCI_EXT_FEATURES_PAGE_MAX + 498 1]; /* Features supported by the device */ 499 uint8_t num_read_pages; 500 501 #define BTM_SEC_STATE_IDLE 0 502 #define BTM_SEC_STATE_AUTHENTICATING 1 503 #define BTM_SEC_STATE_ENCRYPTING 2 504 #define BTM_SEC_STATE_GETTING_NAME 3 505 #define BTM_SEC_STATE_AUTHORIZING 4 506 #define BTM_SEC_STATE_SWITCHING_ROLE 5 507 #define BTM_SEC_STATE_DISCONNECTING 6 /* disconnecting BR/EDR */ 508 #define BTM_SEC_STATE_DELAY_FOR_ENC \ 509 7 /* delay to check for encryption to work around */ 510 /* controller problems */ 511 #define BTM_SEC_STATE_DISCONNECTING_BLE 8 /* disconnecting BLE */ 512 #define BTM_SEC_STATE_DISCONNECTING_BOTH 9 /* disconnecting BR/EDR and BLE */ 513 514 uint8_t sec_state; /* Operating state */ 515 bool is_originator; /* true if device is originating connection */ 516 bool role_master; /* true if current mode is master */ 517 uint16_t security_required; /* Security required for connection */ 518 bool link_key_not_sent; /* link key notification has not been sent waiting for 519 name */ 520 uint8_t link_key_type; /* Type of key used in pairing */ 521 bool link_key_changed; /* Changed link key during current connection */ 522 523 #define BTM_MAX_PRE_SM4_LKEY_TYPE \ 524 BTM_LKEY_TYPE_REMOTE_UNIT /* the link key type used by legacy pairing */ 525 526 #define BTM_SM4_UNKNOWN 0x00 527 #define BTM_SM4_KNOWN 0x10 528 #define BTM_SM4_TRUE 0x11 529 #define BTM_SM4_REQ_PEND 0x08 /* set this bit when getting remote features */ 530 #define BTM_SM4_UPGRADE 0x04 /* set this bit when upgrading link key */ 531 #define BTM_SM4_RETRY \ 532 0x02 /* set this bit to retry on HCI_ERR_KEY_MISSING or \ 533 HCI_ERR_LMP_ERR_TRANS_COLLISION */ 534 #define BTM_SM4_DD_ACP \ 535 0x20 /* set this bit to indicate peer initiated dedicated bonding */ 536 #define BTM_SM4_CONN_PEND \ 537 0x40 /* set this bit to indicate accepting acl conn; to be cleared on \ 538 btm_acl_created */ 539 uint8_t sm4; /* BTM_SM4_TRUE, if the peer supports SM4 */ 540 tBTM_IO_CAP rmt_io_caps; /* IO capability of the peer device */ 541 tBTM_AUTH_REQ rmt_auth_req; /* the auth_req flag as in the IO caps rsp evt */ 542 bool remote_supports_secure_connections; 543 bool remote_features_needed; /* set to true if the local device is in */ 544 /* "Secure Connections Only" mode and it receives */ 545 /* HCI_IO_CAPABILITY_REQUEST_EVT from the peer before */ 546 /* it knows peer's support for Secure Connections */ 547 548 uint16_t ble_hci_handle; /* use in DUMO connection */ 549 uint8_t enc_key_size; /* current link encryption key size */ 550 tBT_DEVICE_TYPE device_type; 551 bool new_encryption_key_is_p256; /* Set to true when the newly generated LK 552 ** is generated from P-256. 553 ** Link encrypted with such LK can be used 554 ** for SM over BR/EDR. 555 */ 556 bool no_smp_on_br; /* if set to true then SMP on BR/EDR doesn't */ 557 /* work, i.e. link keys crosspairing */ 558 /* SC BR/EDR->SC LE doesn't happen */ 559 tBTM_BOND_TYPE bond_type; /* peering bond type */ 560 561 tBTM_SEC_BLE ble; 562 tBTM_LE_CONN_PRAMS conn_params; 563 564 #if (BTM_DISC_DURING_RS == TRUE) 565 #define BTM_SEC_RS_NOT_PENDING 0 /* Role Switch not in progress */ 566 #define BTM_SEC_RS_PENDING 1 /* Role Switch in progress */ 567 #define BTM_SEC_DISC_PENDING 2 /* Disconnect is pending */ 568 uint8_t rs_disc_pending; 569 #endif 570 #define BTM_SEC_NO_LAST_SERVICE_ID 0 571 uint8_t last_author_service_id; /* ID of last serviced authorized: Reset after 572 each l2cap connection */ 573 574 } tBTM_SEC_DEV_REC; 575 576 #define BTM_SEC_IS_SM4(sm) ((bool)(BTM_SM4_TRUE == ((sm)&BTM_SM4_TRUE))) 577 #define BTM_SEC_IS_SM4_LEGACY(sm) ((bool)(BTM_SM4_KNOWN == ((sm)&BTM_SM4_TRUE))) 578 #define BTM_SEC_IS_SM4_UNKNOWN(sm) \ 579 ((bool)(BTM_SM4_UNKNOWN == ((sm)&BTM_SM4_TRUE))) 580 581 #define BTM_SEC_LE_MASK \ 582 (BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED | \ 583 BTM_SEC_LE_LINK_KEY_KNOWN | BTM_SEC_LE_LINK_KEY_AUTHED) 584 585 /* 586 * Define device configuration structure 587 */ 588 typedef struct { 589 tBTM_LOC_BD_NAME bd_name; /* local Bluetooth device name */ 590 bool pin_type; /* true if PIN type is fixed */ 591 uint8_t pin_code_len; /* Bonding information */ 592 PIN_CODE pin_code; /* PIN CODE if pin type is fixed */ 593 bool connectable; /* If true page scan should be enabled */ 594 uint8_t def_inq_scan_mode; /* ??? limited/general/none */ 595 } tBTM_CFG; 596 597 enum { 598 BTM_PM_ST_ACTIVE = BTM_PM_STS_ACTIVE, 599 BTM_PM_ST_HOLD = BTM_PM_STS_HOLD, 600 BTM_PM_ST_SNIFF = BTM_PM_STS_SNIFF, 601 BTM_PM_ST_PARK = BTM_PM_STS_PARK, 602 BTM_PM_ST_PENDING = BTM_PM_STS_PENDING, 603 BTM_PM_ST_INVALID = 0xFF 604 }; 605 typedef uint8_t tBTM_PM_STATE; 606 607 enum { 608 BTM_PM_SET_MODE_EVT, /* Set power mode API is called. */ 609 BTM_PM_UPDATE_EVT, 610 BTM_PM_RD_MODE_EVT /* Read power mode API is called. */ 611 }; 612 typedef uint8_t tBTM_PM_EVENT; 613 614 typedef struct { 615 uint16_t event; 616 uint16_t len; 617 uint8_t link_ind; 618 } tBTM_PM_MSG_DATA; 619 620 typedef struct { 621 uint8_t hci_status; 622 uint8_t mode; 623 uint16_t interval; 624 } tBTM_PM_MD_CHG_DATA; 625 626 typedef struct { 627 uint8_t pm_id; /* the entity that calls SetPowerMode API */ 628 tBTM_PM_PWR_MD* p_pmd; 629 } tBTM_PM_SET_MD_DATA; 630 631 typedef struct { 632 void* p_data; 633 uint8_t link_ind; 634 } tBTM_PM_SM_DATA; 635 636 typedef struct { 637 tBTM_PM_PWR_MD req_mode[BTM_MAX_PM_RECORDS + 1]; /* the desired mode and 638 parameters of the 639 connection*/ 640 tBTM_PM_PWR_MD 641 set_mode; /* the mode and parameters sent down to the host controller. */ 642 uint16_t interval; /* the interval from last mode change event. */ 643 #if (BTM_SSR_INCLUDED == TRUE) 644 uint16_t max_lat; /* stored SSR maximum latency */ 645 uint16_t min_rmt_to; /* stored SSR minimum remote timeout */ 646 uint16_t min_loc_to; /* stored SSR minimum local timeout */ 647 #endif 648 tBTM_PM_STATE state; /* contains the current mode of the connection */ 649 bool chg_ind; /* a request change indication */ 650 } tBTM_PM_MCB; 651 652 #define BTM_PM_REC_NOT_USED 0 653 typedef struct { 654 tBTM_PM_STATUS_CBACK* 655 cback; /* to notify the registered party of mode change event */ 656 uint8_t mask; /* registered request mask. 0, if this entry is not used */ 657 } tBTM_PM_RCB; 658 659 enum { 660 BTM_BLI_ACL_UP_EVT, 661 BTM_BLI_ACL_DOWN_EVT, 662 BTM_BLI_PAGE_EVT, 663 BTM_BLI_PAGE_DONE_EVT, 664 BTM_BLI_INQ_EVT, 665 BTM_BLI_INQ_CANCEL_EVT, 666 BTM_BLI_INQ_DONE_EVT 667 }; 668 typedef uint8_t tBTM_BLI_EVENT; 669 670 /* Pairing State */ 671 enum { 672 BTM_PAIR_STATE_IDLE, /* Idle */ 673 BTM_PAIR_STATE_GET_REM_NAME, /* Getting the remote name (to check for SM4) */ 674 BTM_PAIR_STATE_WAIT_PIN_REQ, /* Started authentication, waiting for PIN req 675 (PIN is pre-fetched) */ 676 BTM_PAIR_STATE_WAIT_LOCAL_PIN, /* Waiting for local PIN code */ 677 BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM, /* Waiting user 'yes' to numeric 678 confirmation */ 679 BTM_PAIR_STATE_KEY_ENTRY, /* Key entry state (we are a keyboard) */ 680 BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP, /* Waiting for local response to peer OOB 681 data */ 682 BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS, /* Waiting for local IO capabilities and OOB 683 data */ 684 BTM_PAIR_STATE_INCOMING_SSP, /* Incoming SSP (got peer IO caps when idle) */ 685 BTM_PAIR_STATE_WAIT_AUTH_COMPLETE, /* All done, waiting authentication 686 cpmplete */ 687 BTM_PAIR_STATE_WAIT_DISCONNECT /* Waiting to disconnect the ACL */ 688 }; 689 typedef uint8_t tBTM_PAIRING_STATE; 690 691 #define BTM_PAIR_FLAGS_WE_STARTED_DD \ 692 0x01 /* We want to do dedicated bonding */ 693 #define BTM_PAIR_FLAGS_PEER_STARTED_DD \ 694 0x02 /* Peer initiated dedicated bonding */ 695 #define BTM_PAIR_FLAGS_DISC_WHEN_DONE 0x04 /* Disconnect when done */ 696 #define BTM_PAIR_FLAGS_PIN_REQD \ 697 0x08 /* set this bit when pin_callback is called */ 698 #define BTM_PAIR_FLAGS_PRE_FETCH_PIN \ 699 0x10 /* set this bit when pre-fetch pin */ 700 #define BTM_PAIR_FLAGS_REJECTED_CONNECT \ 701 0x20 /* set this bit when rejected incoming connection */ 702 #define BTM_PAIR_FLAGS_WE_CANCEL_DD \ 703 0x40 /* set this bit when cancelling a bonding procedure */ 704 #define BTM_PAIR_FLAGS_LE_ACTIVE \ 705 0x80 /* use this bit when SMP pairing is active */ 706 707 typedef struct { 708 bool is_mux; 709 RawAddress bd_addr; 710 uint16_t psm; 711 bool is_orig; 712 tBTM_SEC_CALLBACK* p_callback; 713 void* p_ref_data; 714 uint32_t mx_proto_id; 715 uint32_t mx_chan_id; 716 tBT_TRANSPORT transport; 717 tBTM_BLE_SEC_ACT sec_act; 718 } tBTM_SEC_QUEUE_ENTRY; 719 720 #define CONN_ORIENT_TERM false 721 #define CONN_ORIENT_ORIG true 722 typedef bool CONNECTION_TYPE; 723 724 // Bluetooth Quality Report - Report receiver 725 typedef void(tBTM_BT_QUALITY_REPORT_RECEIVER)(uint8_t len, uint8_t* p_stream); 726 727 /* Define a structure to hold all the BTM data 728 */ 729 730 #define BTM_STATE_BUFFER_SIZE 5 /* size of state buffer */ 731 732 typedef struct { 733 tBTM_CFG cfg; /* Device configuration */ 734 735 /**************************************************** 736 ** ACL Management 737 ****************************************************/ 738 tACL_CONN acl_db[MAX_L2CAP_LINKS]; 739 uint8_t btm_scn[BTM_MAX_SCN]; /* current SCNs: true if SCN is in use */ 740 uint16_t btm_def_link_policy; 741 uint16_t btm_def_link_super_tout; 742 743 tBTM_BL_EVENT_MASK bl_evt_mask; 744 tBTM_BL_CHANGE_CB* p_bl_changed_cb; /* Callback for when Busy Level changed */ 745 746 /**************************************************** 747 ** Power Management 748 ****************************************************/ 749 tBTM_PM_MCB pm_mode_db[MAX_L2CAP_LINKS]; /* per ACL link */ 750 tBTM_PM_RCB pm_reg_db[BTM_MAX_PM_RECORDS + 1]; /* per application/module */ 751 uint8_t pm_pend_link; /* the index of acl_db, which has a pending PM cmd */ 752 uint8_t pm_pend_id; /* the id pf the module, which has a pending PM cmd */ 753 754 /***************************************************** 755 ** Device control 756 *****************************************************/ 757 tBTM_DEVCB devcb; 758 759 /***************************************************** 760 ** BLE Device controllers 761 *****************************************************/ 762 tBTM_BLE_CB ble_ctr_cb; 763 764 uint16_t enc_handle; 765 BT_OCTET8 enc_rand; /* received rand value from LTK request*/ 766 uint16_t ediv; /* received ediv value from LTK request */ 767 uint8_t key_size; 768 tBTM_BLE_VSC_CB cmn_ble_vsc_cb; 769 770 /* Packet types supported by the local device */ 771 uint16_t btm_acl_pkt_types_supported; 772 uint16_t btm_sco_pkt_types_supported; 773 774 /***************************************************** 775 ** Inquiry 776 *****************************************************/ 777 tBTM_INQUIRY_VAR_ST btm_inq_vars; 778 779 /***************************************************** 780 ** SCO Management 781 *****************************************************/ 782 tSCO_CB sco_cb; 783 784 /***************************************************** 785 ** Security Management 786 *****************************************************/ 787 tBTM_APPL_INFO api; 788 789 #define BTM_SEC_MAX_RMT_NAME_CALLBACKS 2 790 tBTM_RMT_NAME_CALLBACK* p_rmt_name_callback[BTM_SEC_MAX_RMT_NAME_CALLBACKS]; 791 792 tBTM_SEC_DEV_REC* p_collided_dev_rec; 793 alarm_t* sec_collision_timer; 794 uint64_t collision_start_time; 795 uint32_t dev_rec_count; /* Counter used for device record timestamp */ 796 uint8_t security_mode; 797 bool pairing_disabled; 798 bool connect_only_paired; 799 bool security_mode_changed; /* mode changed during bonding */ 800 bool pin_type_changed; /* pin type changed during bonding */ 801 bool sec_req_pending; /* true if a request is pending */ 802 803 uint8_t pin_code_len; /* for legacy devices */ 804 PIN_CODE pin_code; /* for legacy devices */ 805 tBTM_PAIRING_STATE pairing_state; /* The current pairing state */ 806 uint8_t pairing_flags; /* The current pairing flags */ 807 RawAddress pairing_bda; /* The device currently pairing */ 808 alarm_t* pairing_timer; /* Timer for pairing process */ 809 uint16_t disc_handle; /* for legacy devices */ 810 uint8_t disc_reason; /* for legacy devices */ 811 tBTM_SEC_SERV_REC sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS]; 812 list_t* sec_dev_rec; /* list of tBTM_SEC_DEV_REC */ 813 tBTM_SEC_SERV_REC* p_out_serv; 814 tBTM_MKEY_CALLBACK* mkey_cback; 815 816 RawAddress connecting_bda; 817 DEV_CLASS connecting_dc; 818 819 uint8_t acl_disc_reason; 820 uint8_t trace_level; 821 uint8_t busy_level; /* the current busy level */ 822 bool is_paging; /* true, if paging is in progess */ 823 bool is_inquiry; /* true, if inquiry is in progess */ 824 fixed_queue_t* page_queue; 825 bool paging; 826 bool discing; 827 fixed_queue_t* sec_pending_q; /* pending sequrity requests in 828 tBTM_SEC_QUEUE_ENTRY format */ 829 830 char state_temp_buffer[BTM_STATE_BUFFER_SIZE]; 831 // BQR Receiver 832 tBTM_BT_QUALITY_REPORT_RECEIVER* p_bqr_report_receiver; 833 } tBTM_CB; 834 835 /* security action for L2CAP COC channels */ 836 #define BTM_SEC_OK 1 837 #define BTM_SEC_ENCRYPT 2 /* encrypt the link with current key */ 838 #define BTM_SEC_ENCRYPT_NO_MITM 3 /* unauthenticated encryption or better */ 839 #define BTM_SEC_ENCRYPT_MITM 4 /* authenticated encryption */ 840 #define BTM_SEC_ENC_PENDING 5 /* wait for link encryption pending */ 841 842 typedef uint8_t tBTM_SEC_ACTION; 843 844 #endif // BTM_INT_TYPES_H 845