1 /****************************************************************************** 2 * 3 * Copyright (C) 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 19 /****************************************************************************** 20 * 21 * this file contains the main Bluetooth Manager (BTM) internal 22 * definitions. 23 * 24 ******************************************************************************/ 25 26 #ifndef BTM_BLE_INT_H 27 #define BTM_BLE_INT_H 28 29 #include "bt_target.h" 30 #include "gki.h" 31 #include "hcidefs.h" 32 #include "btm_ble_api.h" 33 #include "btm_int.h" 34 35 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE 36 #include "smp_api.h" 37 #endif 38 39 40 /* scanning enable status */ 41 #define BTM_BLE_SCAN_ENABLE 0x01 42 #define BTM_BLE_SCAN_DISABLE 0x00 43 44 /* advertising enable status */ 45 #define BTM_BLE_ADV_ENABLE 0x01 46 #define BTM_BLE_ADV_DISABLE 0x00 47 48 /* use the high 4 bits unused by inquiry mode */ 49 #define BTM_BLE_SELECT_SCAN 0x20 50 #define BTM_BLE_NAME_REQUEST 0x40 51 #define BTM_BLE_OBSERVE 0x80 52 53 #define BTM_BLE_MAX_WL_ENTRY 1 54 #define BTM_BLE_AD_DATA_LEN 31 55 56 #define BTM_BLE_ENC_MASK 0x03 57 58 #define BTM_BLE_DUPLICATE_ENABLE 1 59 #define BTM_BLE_DUPLICATE_DISABLE 0 60 61 #define BTM_BLE_GAP_DISC_SCAN_INT 18 /* Interval(scan_int) = 11.25 ms= 0x0010 * 0.625 ms */ 62 #define BTM_BLE_GAP_DISC_SCAN_WIN 18 /* scan_window = 11.25 ms= 0x0010 * 0.625 ms */ 63 #define BTM_BLE_GAP_ADV_INT 512 /* Tgap(gen_disc) = 1.28 s= 512 * 0.625 ms */ 64 #define BTM_BLE_GAP_LIM_TOUT 180 /* Tgap(lim_timeout) = 180s max */ 65 66 67 #define BTM_BLE_GAP_ADV_FAST_INT_1 48 /* TGAP(adv_fast_interval1) = 30(used) ~ 60 ms = 48 *0.625 */ 68 #define BTM_BLE_GAP_ADV_FAST_INT_2 160 /* TGAP(adv_fast_interval2) = 100(used) ~ 150 ms = 160 * 0.625 ms */ 69 #define BTM_BLE_GAP_ADV_SLOW_INT 2048 /* Tgap(adv_slow_interval) = 1.28 s= 512 * 0.625 ms */ 70 #define BTM_BLE_GAP_ADV_DIR_MAX_INT 800 /* Tgap(dir_conn_adv_int_max) = 500 ms = 800 * 0.625 ms */ 71 #define BTM_BLE_GAP_ADV_DIR_MIN_INT 400 /* Tgap(dir_conn_adv_int_min) = 250 ms = 400 * 0.625 ms */ 72 73 #define BTM_BLE_GAP_FAST_ADV_TOUT 30 74 75 #define BTM_BLE_SEC_REQ_ACT_NONE 0 76 #define BTM_BLE_SEC_REQ_ACT_ENCRYPT 1 /* encrypt the link using current key or key refresh */ 77 #define BTM_BLE_SEC_REQ_ACT_PAIR 2 78 #define BTM_BLE_SEC_REQ_ACT_DISCARD 3 /* discard the sec request while encryption is started but not completed */ 79 typedef UINT8 tBTM_BLE_SEC_REQ_ACT; 80 81 #define BLE_STATIC_PRIVATE_MSB_MASK 0x3f 82 #define BLE_RESOLVE_ADDR_MSB 0x40 /* most significant bit, bit7, bit6 is 01 to be resolvable random */ 83 #define BLE_RESOLVE_ADDR_MASK 0xc0 /* bit 6, and bit7 */ 84 #define BTM_BLE_IS_RESOLVE_BDA(x) ((x[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB) 85 86 /* LE scan activity bit mask, continue with LE inquiry bits */ 87 #define BTM_LE_SELECT_CONN_ACTIVE 0x40 /* selection connection is in progress */ 88 #define BTM_LE_OBSERVE_ACTIVE 0x80 /* observe is in progress */ 89 90 /* BLE scan activity mask checking */ 91 #define BTM_BLE_IS_SCAN_ACTIVE(x) ((x) & BTM_BLE_SCAN_ACTIVE_MASK) 92 #define BTM_BLE_IS_INQ_ACTIVE(x) ((x) & BTM_BLE_INQUIRY_MASK) 93 #define BTM_BLE_IS_OBS_ACTIVE(x) ((x) & BTM_LE_OBSERVE_ACTIVE) 94 #define BTM_BLE_IS_SEL_CONN_ACTIVE(x) ((x) & BTM_LE_SELECT_CONN_ACTIVE) 95 96 typedef struct 97 { 98 UINT16 data_mask; 99 UINT8 *p_flags; 100 UINT8 ad_data[BTM_BLE_AD_DATA_LEN]; 101 UINT8 *p_pad; 102 }tBTM_BLE_LOCAL_ADV_DATA; 103 104 typedef struct 105 { 106 UINT32 inq_count; /* Used for determining if a response has already been */ 107 /* received for the current inquiry operation. (We do not */ 108 /* want to flood the caller with multiple responses from */ 109 /* the same device. */ 110 BOOLEAN scan_rsp; 111 tBLE_BD_ADDR le_bda; 112 } tINQ_LE_BDADDR; 113 114 #define BTM_BLE_ADV_DATA_LEN_MAX 31 115 #define BTM_BLE_CACHE_ADV_DATA_MAX 62 116 117 #define BTM_BLE_VALID_PRAM(x, min, max) (((x) >= (min) && (x) <= (max)) || ((x) == BTM_BLE_CONN_PARAM_UNDEF)) 118 119 #define BTM_BLE_PRIVATE_ADDR_INT 900 /* 15 minutes minimum for 120 random address refreshing */ 121 122 typedef struct 123 { 124 125 UINT16 discoverable_mode; 126 UINT16 connectable_mode; 127 UINT16 scan_window; 128 UINT16 scan_interval; 129 UINT8 scan_type; /* current scan type: active or passive */ 130 UINT16 adv_interval_min; 131 UINT16 adv_interval_max; 132 tBTM_BLE_AFP afp; /* advertising filter policy */ 133 tBTM_BLE_SFP sfp; /* scanning filter policy */ 134 135 tBLE_ADDR_TYPE adv_addr_type; 136 UINT8 evt_type; 137 UINT8 adv_mode; 138 tBLE_BD_ADDR direct_bda; 139 BOOLEAN directed_conn; 140 BOOLEAN fast_adv_on; 141 TIMER_LIST_ENT fast_adv_timer; 142 143 UINT8 adv_len; 144 UINT8 adv_data_cache[BTM_BLE_CACHE_ADV_DATA_MAX]; 145 146 /* inquiry BD addr database */ 147 UINT8 num_bd_entries; 148 UINT8 max_bd_entries; 149 150 151 tBTM_BLE_LOCAL_ADV_DATA adv_data; 152 tBTM_BLE_ADV_CHNL_MAP adv_chnl_map; 153 154 TIMER_LIST_ENT inq_timer_ent; 155 BOOLEAN scan_rsp; 156 UINT8 state; /* Current state that the inquiry process is in */ 157 INT8 tx_power; 158 } tBTM_BLE_INQ_CB; 159 160 161 /* random address resolving complete callback */ 162 typedef void (tBTM_BLE_RESOLVE_CBACK) (void * match_rec, void *p); 163 164 typedef void (tBTM_BLE_ADDR_CBACK) (BD_ADDR_PTR static_random, void *p); 165 166 /* random address management control block */ 167 typedef struct 168 { 169 tBLE_ADDR_TYPE own_addr_type; /* local device LE address type */ 170 BD_ADDR private_addr; 171 BD_ADDR random_bda; 172 BOOLEAN busy; 173 UINT16 index; 174 tBTM_BLE_RESOLVE_CBACK *p_resolve_cback; 175 tBTM_BLE_ADDR_CBACK *p_generate_cback; 176 void *p; 177 TIMER_LIST_ENT raddr_timer_ent; 178 } tBTM_LE_RANDOM_CB; 179 180 #define BTM_BLE_MAX_BG_CONN_DEV_NUM 10 181 182 typedef struct 183 { 184 UINT16 min_conn_int; 185 UINT16 max_conn_int; 186 UINT16 slave_latency; 187 UINT16 supervision_tout; 188 189 }tBTM_LE_CONN_PRAMS; 190 191 192 typedef struct 193 { 194 BD_ADDR bd_addr; 195 UINT8 attr; 196 BOOLEAN is_connected; 197 BOOLEAN in_use; 198 }tBTM_LE_BG_CONN_DEV; 199 200 /* white list using state as a bit mask */ 201 #define BTM_BLE_WL_IDLE 0 202 #define BTM_BLE_WL_INIT 1 203 #define BTM_BLE_WL_SCAN 2 204 #define BTM_BLE_WL_ADV 4 205 typedef UINT8 tBTM_BLE_WL_STATE; 206 207 /* BLE connection state */ 208 #define BLE_CONN_IDLE 0 209 #define BLE_DIR_CONN 1 210 #define BLE_BG_CONN 2 211 #define BLE_CONN_CANCEL 3 212 typedef UINT8 tBTM_BLE_CONN_ST; 213 214 typedef struct 215 { 216 void *p_param; 217 }tBTM_BLE_CONN_REQ; 218 219 /* LE state request */ 220 #define BTM_BLE_STATE_INVALID 0 221 #define BTM_BLE_STATE_CONN_ADV 1 222 #define BTM_BLE_STATE_INIT 2 223 #define BTM_BLE_STATE_MASTER 3 224 #define BTM_BLE_STATE_SLAVE 4 225 #define BTM_BLE_STATE_LO_DUTY_DIR_ADV 5 226 #define BTM_BLE_STATE_HI_DUTY_DIR_ADV 6 227 #define BTM_BLE_STATE_NON_CONN_ADV 7 228 #define BTM_BLE_STATE_PASSIVE_SCAN 8 229 #define BTM_BLE_STATE_ACTIVE_SCAN 9 230 #define BTM_BLE_STATE_SCAN_ADV 10 231 #define BTM_BLE_STATE_MAX 11 232 typedef UINT8 tBTM_BLE_STATE; 233 234 #define BTM_BLE_STATE_CONN_ADV_BIT 0x0001 235 #define BTM_BLE_STATE_INIT_BIT 0x0002 236 #define BTM_BLE_STATE_MASTER_BIT 0x0004 237 #define BTM_BLE_STATE_SLAVE_BIT 0x0008 238 #define BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT 0x0010 239 #define BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT 0x0020 240 #define BTM_BLE_STATE_NON_CONN_ADV_BIT 0x0040 241 #define BTM_BLE_STATE_PASSIVE_SCAN_BIT 0x0080 242 #define BTM_BLE_STATE_ACTIVE_SCAN_BIT 0x0100 243 #define BTM_BLE_STATE_SCAN_ADV_BIT 0x0200 244 typedef UINT16 tBTM_BLE_STATE_MASK; 245 246 #define BTM_BLE_STATE_ALL_MASK 0x03ff 247 #define BTM_BLE_STATE_ALL_ADV_MASK (BTM_BLE_STATE_CONN_ADV_BIT|BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT|BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT|BTM_BLE_STATE_SCAN_ADV_BIT) 248 #define BTM_BLE_STATE_ALL_SCAN_MASK (BTM_BLE_STATE_PASSIVE_SCAN_BIT|BTM_BLE_STATE_ACTIVE_SCAN_BIT) 249 #define BTM_BLE_STATE_ALL_CONN_MASK (BTM_BLE_STATE_MASTER_BIT|BTM_BLE_STATE_SLAVE_BIT) 250 251 typedef struct 252 { 253 BOOLEAN in_use; 254 BOOLEAN to_add; 255 BD_ADDR bd_addr; 256 UINT8 attr; 257 }tBTM_BLE_WL_OP; 258 /* Define BLE Device Management control structure 259 */ 260 typedef struct 261 { 262 UINT8 scan_activity; /* LE scan activity mask */ 263 264 /***************************************************** 265 ** BLE Inquiry 266 *****************************************************/ 267 tBTM_BLE_INQ_CB inq_var; 268 269 /* observer callback and timer */ 270 tBTM_INQ_RESULTS_CB *p_obs_results_cb; 271 tBTM_CMPL_CB *p_obs_cmpl_cb; 272 TIMER_LIST_ENT obs_timer_ent; 273 274 /* background connection procedure cb value */ 275 tBTM_BLE_CONN_TYPE bg_conn_type; 276 UINT16 scan_int; 277 UINT16 scan_win; 278 tBTM_BLE_SEL_CBACK *p_select_cback; 279 TIMER_LIST_ENT scan_param_idle_timer; 280 281 /* white list information */ 282 UINT8 num_empty_filter; /* Number of entries in white list */ 283 UINT8 max_filter_entries; /* Maximum number of entries that can be stored */ 284 tBTM_BLE_WL_STATE wl_state; 285 UINT8 bg_dev_num; 286 tBTM_LE_BG_CONN_DEV bg_dev_list[BTM_BLE_MAX_BG_CONN_DEV_NUM]; 287 288 BUFFER_Q conn_pending_q; 289 tBTM_BLE_CONN_ST conn_state; 290 291 /* random address management control block */ 292 tBTM_LE_RANDOM_CB addr_mgnt_cb; 293 294 BOOLEAN enabled; 295 BOOLEAN privacy; /* local privacy enabled or disabled */ 296 tBTM_BLE_WL_OP wl_op_q[BTM_BLE_MAX_BG_CONN_DEV_NUM]; 297 298 #ifdef BTM_BLE_PC_ADV_TEST_MODE 299 tBTM_BLE_SCAN_REQ_CBACK *p_scan_req_cback; 300 #endif 301 302 /* current BLE link state */ 303 tBTM_BLE_STATE_MASK cur_states; /* bit mask of tBTM_BLE_STATE */ 304 305 } tBTM_BLE_CB; 306 307 #ifdef __cplusplus 308 extern "C" { 309 #endif 310 311 extern void btm_ble_timeout(TIMER_LIST_ENT *p_tle); 312 extern void btm_ble_process_adv_pkt (UINT8 *p); 313 extern void btm_ble_proc_scan_rsp_rpt (UINT8 *p); 314 extern tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb); 315 extern BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda); 316 317 extern tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode); 318 extern tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode); 319 extern tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8 duration); 320 extern void btm_ble_dir_adv_tout(void); 321 322 extern void btm_ble_stop_scan(); 323 extern void btm_ble_stop_inquiry(void); 324 extern void btm_ble_init (void); 325 extern void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role, tBLE_ADDR_TYPE addr_type, BOOLEAN addr_matched); 326 extern void btm_ble_read_remote_features_complete(UINT8 *p); 327 extern void btm_ble_write_adv_enable_complete(UINT8 * p); 328 extern void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len); 329 extern void btm_read_ble_local_supported_states_complete(UINT8 *p, UINT16 evt_len); 330 extern tBTM_BLE_CONN_ST btm_ble_get_conn_st(void); 331 extern void btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st); 332 extern UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst, 333 tBTM_BLE_ADV_DATA *p_data); 334 extern tBTM_STATUS btm_ble_start_adv(void); 335 extern tBTM_STATUS btm_ble_stop_adv(void); 336 extern tBTM_STATUS btm_ble_start_scan (UINT8 filter_enb); 337 extern void btm_ble_create_ll_conn_complete (UINT8 status); 338 339 /* LE security function from btm_sec.c */ 340 #if SMP_INCLUDED == TRUE 341 extern void btm_ble_link_sec_check(BD_ADDR bd_addr, tBTM_LE_AUTH_REQ auth_req, tBTM_BLE_SEC_REQ_ACT *p_sec_req_act); 342 extern void btm_ble_ltk_request_reply(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk); 343 extern UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data); 344 extern tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role); 345 extern void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv); 346 extern tBTM_STATUS btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk); 347 extern void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable); 348 #endif 349 350 /* LE device management functions */ 351 extern void btm_ble_reset_id( void ); 352 353 /* security related functions */ 354 extern void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local ); 355 extern BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div); 356 extern BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types); 357 358 extern void btm_ble_test_command_complete(UINT8 *p); 359 extern void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback); 360 361 extern void btm_sec_save_le_key(BD_ADDR bd_addr, tBTM_LE_KEY_TYPE key_type, tBTM_LE_KEY_VALUE *p_keys, BOOLEAN pass_to_application); 362 extern void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size); 363 extern UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr); 364 365 /* white list function */ 366 extern BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr); 367 extern BOOLEAN btm_update_bg_conn_list(BOOLEAN to_add, BD_ADDR bd_addr); 368 extern void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy); 369 extern void btm_update_scan_filter_policy(tBTM_BLE_AFP adv_policy); 370 extern void btm_ble_clear_white_list (void); 371 372 /* background connection function */ 373 extern void btm_ble_suspend_bg_conn(void); 374 extern BOOLEAN btm_ble_resume_bg_conn(void); 375 extern void btm_ble_initiate_select_conn(BD_ADDR bda); 376 extern BOOLEAN btm_ble_start_auto_conn(BOOLEAN start); 377 extern BOOLEAN btm_ble_start_select_conn(BOOLEAN start,tBTM_BLE_SEL_CBACK *p_select_cback); 378 extern BOOLEAN btm_ble_renew_bg_conn_params(BOOLEAN add, BD_ADDR bd_addr); 379 extern UINT8 btm_ble_count_unconn_dev_in_whitelist(void); 380 extern void btm_write_dir_conn_wl(BD_ADDR target_addr); 381 extern void btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bda, UINT8 status); 382 extern BOOLEAN btm_execute_wl_dev_operation(void); 383 384 /* direct connection utility */ 385 extern BOOLEAN btm_send_pending_direct_conn(void); 386 extern void btm_ble_enqueue_direct_conn_req(void *p_param); 387 388 /* BLE address management */ 389 extern void btm_gen_resolvable_private_addr (void *p_cmd_cplt_cback); 390 extern void btm_gen_non_resolvable_private_addr (tBTM_BLE_ADDR_CBACK *p_cback, void *p); 391 extern void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK * p_cback, void *p); 392 extern void btm_ble_update_reconnect_address(BD_ADDR bd_addr); 393 extern void btm_gen_resolve_paddr_low(tBTM_RAND_ENC *p); 394 395 extern void btm_ble_multi_adv_configure_rpa (tBTM_BLE_MULTI_ADV_INST *p_inst); 396 extern void btm_ble_multi_adv_init(void); 397 extern void* btm_ble_multi_adv_get_ref(UINT8 inst_id); 398 extern void btm_ble_multi_adv_cleanup(void); 399 extern void btm_ble_multi_adv_reenable(UINT8 inst_id); 400 extern void btm_ble_multi_adv_enb_privacy(BOOLEAN enable); 401 extern char btm_ble_map_adv_tx_power(int tx_power_index); 402 extern void btm_ble_batchscan_init(void); 403 extern void btm_ble_batchscan_cleanup(void); 404 extern void btm_ble_adv_filter_init(void); 405 extern void btm_ble_adv_filter_cleanup(void); 406 extern BOOLEAN btm_ble_topology_check(tBTM_BLE_STATE_MASK request); 407 extern BOOLEAN btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state); 408 extern BOOLEAN btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state); 409 410 /* BLE address mapping with CS feature */ 411 extern BOOLEAN btm_public_addr_to_random_pseudo(BD_ADDR bd_addr, UINT8 *p_addr_type); 412 extern BOOLEAN btm_random_pseudo_to_public(BD_ADDR random_pseudo, UINT8 *p_static_addr_type); 413 extern void btm_ble_refresh_rra(BD_ADDR pseudo_bda, BD_ADDR rra); 414 415 #if BTM_BLE_CONFORMANCE_TESTING == TRUE 416 BT_API extern void btm_ble_set_no_disc_if_pair_fail (BOOLEAN disble_disc); 417 BT_API extern void btm_ble_set_test_mac_value (BOOLEAN enable, UINT8 *p_test_mac_val); 418 BT_API extern void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr); 419 BT_API extern void btm_set_random_address(BD_ADDR random_bda); 420 BT_API extern void btm_ble_set_keep_rfu_in_auth_req(BOOLEAN keep_rfu); 421 #endif 422 423 424 #ifdef __cplusplus 425 } 426 #endif 427 428 #endif 429