Home | History | Annotate | Download | only in btm
      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        30          /* Tgap(lim_timeout) = 30.72 s max, round down to 30 */
     65 
     66 
     67 #define BTM_BLE_SEC_REQ_ACT_NONE           0
     68 #define BTM_BLE_SEC_REQ_ACT_ENCRYPT        1 /* encrypt the link using current key or key refresh */
     69 #define BTM_BLE_SEC_REQ_ACT_PAIR           2
     70 #define BTM_BLE_SEC_REQ_ACT_DISCARD        3 /* discard the sec request while encryption is started but not completed */
     71 typedef UINT8   tBTM_BLE_SEC_REQ_ACT;
     72 
     73 #define BLE_STATIC_PRIVATE_MSB_MASK          0x3f
     74 #define BLE_RESOLVE_ADDR_MSB                 0x40   /*  most significant bit, bit7, bit6 is 01 to be resolvable random */
     75 #define BLE_RESOLVE_ADDR_MASK                0xc0   /* bit 6, and bit7 */
     76 #define BTM_BLE_IS_RESOLVE_BDA(x)           ((x[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB)
     77 
     78 typedef struct
     79 {
     80     UINT16              data_mask;
     81     UINT8               *p_flags;
     82     UINT8               ad_data[BTM_BLE_AD_DATA_LEN];
     83     UINT8               *p_pad;
     84 }tBTM_BLE_LOCAL_ADV_DATA;
     85 
     86 typedef struct
     87 {
     88     UINT32          inq_count;          /* Used for determining if a response has already been      */
     89                                         /* received for the current inquiry operation. (We do not   */
     90                                         /* want to flood the caller with multiple responses from    */
     91                                         /* the same device.                                         */
     92     BOOLEAN         scan_rsp;
     93     tBLE_BD_ADDR    le_bda;
     94 } tINQ_LE_BDADDR;
     95 
     96 #define BTM_BLE_ADV_DATA_LEN_MAX        31
     97 #define BTM_BLE_CACHE_ADV_DATA_MAX      62
     98 
     99 #define BTM_BLE_VALID_PRAM(x, min, max)  (((x) >= (min) && (x) <= (max)) || ((x) == BTM_BLE_CONN_PARAM_UNDEF))
    100 
    101 typedef struct
    102 {
    103 
    104     UINT16           discoverable_mode;
    105     UINT16           connectable_mode;
    106     UINT16           br_edr_supported_flag;  /* combined BR EDR discoverable and connectable mode */
    107                                              /* only meaningful when it is zero. This means
    108                                                 BR EDR is not supported*/
    109     UINT8            proc_mode;        /* current procedure mode : inquiry or discovery */
    110 
    111     UINT16           scan_window;
    112     UINT16           scan_interval;
    113     UINT8            scan_type;        /* current scan type: active or passive */
    114     UINT16           adv_interval_min;
    115     UINT16           adv_interval_max;
    116     tBTM_BLE_AFP     afp;               /* advertising filter policy */
    117     tBTM_BLE_SFP     sfp;               /* scanning filter policy */
    118 
    119     tBLE_ADDR_TYPE   adv_addr_type;
    120     UINT8            evt_type;
    121     UINT8            adv_mode;
    122     tBLE_BD_ADDR     direct_bda;
    123     BOOLEAN          directed_conn;
    124 
    125     UINT8            adv_len;
    126     UINT8            adv_data_cache[BTM_BLE_CACHE_ADV_DATA_MAX];
    127 
    128     /* inquiry BD addr database */
    129     UINT8               num_bd_entries;
    130     UINT8               max_bd_entries;
    131 
    132 
    133     tBTM_BLE_LOCAL_ADV_DATA   adv_data;
    134     tBTM_BLE_ADV_CHNL_MAP     adv_chnl_map;
    135 
    136     TIMER_LIST_ENT   inq_timer_ent;
    137     BOOLEAN          scan_rsp;
    138     UINT8            state;             /* Current state that the inquiry process is in */
    139     INT8             tx_power;
    140 } tBTM_BLE_INQ_CB;
    141 
    142 
    143 /* random address resolving complete callback */
    144 typedef void (tBTM_BLE_RESOLVE_CBACK) (void * match_rec, void *p);
    145 
    146 typedef void (tBTM_BLE_ADDR_CBACK) (BD_ADDR_PTR static_random, void *p);
    147 
    148 /* random address management control block */
    149 typedef struct
    150 {
    151     tBLE_ADDR_TYPE	            own_addr_type;         /* local device LE address type */
    152     BD_ADDR			            private_addr;
    153     BD_ADDR                     random_bda;
    154     BOOLEAN                     busy;
    155     UINT16                       index;
    156     tBTM_BLE_RESOLVE_CBACK      *p_resolve_cback;
    157     tBTM_BLE_ADDR_CBACK         *p_generate_cback;
    158     void                        *p;
    159     TIMER_LIST_ENT              raddr_timer_ent;
    160 } tBTM_LE_RANDOM_CB;
    161 
    162 #define BTM_BLE_MAX_BG_CONN_DEV_NUM    10
    163 
    164 typedef struct
    165 {
    166     UINT16              min_conn_int;
    167     UINT16              max_conn_int;
    168     UINT16              slave_latency;
    169     UINT16              supervision_tout;
    170 
    171 }tBTM_LE_CONN_PRAMS;
    172 
    173 
    174 typedef struct
    175 {
    176     BD_ADDR     bd_addr;
    177     UINT8       attr;
    178     BOOLEAN     is_connected;
    179     BOOLEAN     in_use;
    180 }tBTM_LE_BG_CONN_DEV;
    181 
    182   /* white list using state as a bit mask */
    183 #define BTM_BLE_WL_IDLE         0
    184 #define BTM_BLE_WL_INIT         1
    185 #define BTM_BLE_WL_SCAN         2
    186 #define BTM_BLE_WL_ADV          4
    187 typedef UINT8 tBTM_BLE_WL_STATE;
    188 
    189 /* BLE connection state */
    190 #define BLE_CONN_IDLE    0
    191 #define BLE_DIR_CONN     1
    192 #define BLE_BG_CONN      2
    193 #define BLE_CONN_CANCEL  3
    194 typedef UINT8 tBTM_BLE_CONN_ST;
    195 
    196 typedef struct
    197 {
    198     void    *p_param;
    199 }tBTM_BLE_CONN_REQ;
    200 
    201 
    202 typedef struct
    203 {
    204     BOOLEAN     in_use;
    205     BOOLEAN     to_add;
    206     BD_ADDR     bd_addr;
    207     UINT8       attr;
    208 }tBTM_BLE_WL_OP;
    209 /* Define BLE Device Management control structure
    210 */
    211 typedef struct
    212 {
    213     /*****************************************************
    214     **      BLE Inquiry
    215     *****************************************************/
    216     tBTM_BLE_INQ_CB     inq_var;
    217 
    218     /* background connection procedure cb value */
    219     tBTM_BLE_CONN_TYPE  bg_conn_type;
    220     UINT16              scan_int;
    221     UINT16              scan_win;
    222     tBTM_BLE_SEL_CBACK  *p_select_cback;
    223     TIMER_LIST_ENT      scan_param_idle_timer;
    224 
    225     /* white list information */
    226     UINT8                   num_empty_filter;      /* Number of entries in white list */
    227     UINT8                   max_filter_entries;    /* Maximum number of entries that can be stored */
    228     tBTM_BLE_WL_STATE       wl_state;
    229     UINT8                   bg_dev_num;
    230     tBTM_LE_BG_CONN_DEV     bg_dev_list[BTM_BLE_MAX_BG_CONN_DEV_NUM];
    231 
    232     BUFFER_Q                conn_pending_q;
    233     tBTM_BLE_CONN_ST        conn_state;
    234 
    235     /* random address management control block */
    236     tBTM_LE_RANDOM_CB   addr_mgnt_cb;
    237 
    238     BOOLEAN          enabled;
    239     tBTM_BLE_WL_OP  wl_op_q[BTM_BLE_MAX_BG_CONN_DEV_NUM];
    240 
    241 #ifdef BTM_BLE_PC_ADV_TEST_MODE
    242     tBTM_BLE_SCAN_REQ_CBACK *p_scan_req_cback;
    243 #endif
    244 
    245 } tBTM_BLE_CB;
    246 
    247 #ifdef __cplusplus
    248 extern "C" {
    249 #endif
    250 
    251 extern void btm_ble_timeout(TIMER_LIST_ENT *p_tle);
    252 extern void btm_ble_process_adv_pkt (UINT8 *p);
    253 extern void btm_ble_proc_scan_rsp_rpt (UINT8 *p);
    254 extern tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb);
    255 extern BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda);
    256 
    257 extern tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode);
    258 extern tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode);
    259 extern tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8   duration);
    260 extern void btm_ble_dir_adv_tout(void);
    261 
    262 extern void btm_ble_stop_scan(void);
    263 extern void btm_ble_att_db_init(void);
    264 extern void btm_ble_init (void);
    265 extern void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role, tBLE_ADDR_TYPE addr_type, BOOLEAN addr_matched);
    266 extern void btm_ble_read_remote_features_complete(UINT8 *p);
    267 extern void btm_ble_write_adv_enable_complete(UINT8 * p);
    268 extern void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len);
    269 extern tBTM_BLE_CONN_ST btm_ble_get_conn_st(void);
    270 extern void btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st);
    271 
    272 
    273 /* LE security function from btm_sec.c */
    274 #if SMP_INCLUDED == TRUE
    275 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);
    276 extern void btm_ble_ltk_request_reply(BD_ADDR bda,  BOOLEAN use_stk, BT_OCTET16 stk);
    277 extern UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data);
    278 extern tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role);
    279 extern void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv);
    280 extern BOOLEAN btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk);
    281 extern void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable);
    282 #endif
    283 
    284 /* LE device management functions */
    285 extern void btm_ble_reset_id( void );
    286 
    287 /* security related functions */
    288 extern void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local );
    289 extern BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div);
    290 extern BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types);
    291 
    292 extern void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback);
    293 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);
    294 extern void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size);
    295 extern UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr);
    296 
    297 /* white list function */
    298 extern BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr,UINT8 wl_type);
    299 extern BOOLEAN btm_update_bg_conn_list(BOOLEAN to_add, BD_ADDR bd_addr, UINT8 *p_attr_tag);
    300 extern void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy);
    301 extern void btm_update_adv_filter_policy(tBTM_BLE_AFP adv_policy);
    302 extern void btm_ble_clear_white_list (void);
    303 
    304 /* background connection function */
    305 extern void btm_ble_suspend_bg_conn(void);
    306 extern BOOLEAN btm_ble_resume_bg_conn(void);
    307 extern void btm_ble_initiate_select_conn(BD_ADDR bda);
    308 extern BOOLEAN btm_ble_start_auto_conn(BOOLEAN start);
    309 extern BOOLEAN btm_ble_start_select_conn(BOOLEAN start,tBTM_BLE_SEL_CBACK   *p_select_cback);
    310 extern BOOLEAN btm_ble_renew_bg_conn_params(BOOLEAN add, BD_ADDR bd_addr);
    311 extern UINT8 btm_ble_count_unconn_dev_in_whitelist(void);
    312 extern void btm_write_dir_conn_wl(BD_ADDR target_addr);
    313 extern void btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bda, BOOLEAN conn_ccancel);
    314 
    315 /* direct connection utility */
    316 extern BOOLEAN btm_send_pending_direct_conn(void);
    317 extern void btm_ble_enqueue_direct_conn_req(void *p_param);
    318 
    319 /* BLE address management */
    320 extern void btm_gen_resolvable_private_addr (void);
    321 extern void btm_gen_non_resolvable_private_addr (tBTM_BLE_ADDR_CBACK *p_cback, void *p);
    322 extern void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK * p_cback, void *p);
    323 extern void btm_ble_update_reconnect_address(BD_ADDR bd_addr);
    324 
    325 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
    326 BT_API extern void btm_ble_set_no_disc_if_pair_fail (BOOLEAN disble_disc);
    327 BT_API extern void btm_ble_set_test_mac_value (BOOLEAN enable, UINT8 *p_test_mac_val);
    328 BT_API extern void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr);
    329 BT_API extern void btm_set_random_address(BD_ADDR random_bda);
    330 #endif
    331 
    332 
    333 #ifdef __cplusplus
    334 }
    335 #endif
    336 
    337 #endif
    338