• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define BTM_BLE_CONNECT_EVT     0x00
40 #define BTM_BLE_CONNECT_DIR_EVT 0x01
41 #define BTM_BLE_DISCOVER_EVT    0x02
42 #define BTM_BLE_NON_CONNECT_EVT 0x03
43 #define BTM_BLE_SCAN_RSP_EVT    0x04
44 #define BTM_BLE_SCAN_REQ_EVT    0x06
45 #define BTM_BLE_UNKNOWN_EVT     0xff
46 
47 #define BTM_BLE_UNKNOWN_EVT     0xff
48 
49 /* scanning enable status */
50 #define BTM_BLE_SCAN_ENABLE      0x01
51 #define BTM_BLE_SCAN_DISABLE     0x00
52 
53 /* advertising enable status */
54 #define BTM_BLE_ADV_ENABLE     0x01
55 #define BTM_BLE_ADV_DISABLE    0x00
56 
57 /* use the high 4 bits unused by inquiry mode */
58 #define BTM_BLE_SELECT_SCAN     0x20
59 #define BTM_BLE_NAME_REQUEST    0x40
60 #define BTM_BLE_OBSERVE         0x80
61 
62 #define BTM_BLE_MAX_WL_ENTRY        1
63 #define BTM_BLE_AD_DATA_LEN         31
64 
65 #define BTM_BLE_ENC_MASK    0x03
66 
67 #define BTM_BLE_DUPLICATE_ENABLE        1
68 #define BTM_BLE_DUPLICATE_DISABLE       0
69 
70 #define BTM_BLE_GAP_DISC_SCAN_INT   18         /* Interval(scan_int) = 11.25 ms= 0x0010 * 0.625 ms */
71 #define BTM_BLE_GAP_DISC_SCAN_WIN   18         /* scan_window = 11.25 ms= 0x0010 * 0.625 ms */
72 #define BTM_BLE_GAP_ADV_INT         512         /* Tgap(gen_disc) = 1.28 s= 512 * 0.625 ms */
73 #define BTM_BLE_GAP_LIM_TOUT        30          /* Tgap(lim_timeout) = 30.72 s max, round down to 30 */
74 
75 
76 #define BTM_BLE_SEC_REQ_ACT_NONE           0
77 #define BTM_BLE_SEC_REQ_ACT_ENCRYPT        1 /* encrypt the link using current key or key refresh */
78 #define BTM_BLE_SEC_REQ_ACT_PAIR           2
79 #define BTM_BLE_SEC_REQ_ACT_DISCARD        3 /* discard the sec request while encryption is started but not completed */
80 typedef UINT8   tBTM_BLE_SEC_REQ_ACT;
81 
82 
83 
84 typedef struct
85 {
86     UINT16              data_mask;
87     UINT8               *p_flags;
88     UINT8               ad_data[BTM_BLE_AD_DATA_LEN];
89     UINT8               *p_pad;
90 }tBTM_BLE_LOCAL_ADV_DATA;
91 
92 typedef struct
93 {
94     UINT32          inq_count;          /* Used for determining if a response has already been      */
95                                         /* received for the current inquiry operation. (We do not   */
96                                         /* want to flood the caller with multiple responses from    */
97                                         /* the same device.                                         */
98     BOOLEAN         scan_rsp;
99     tBLE_BD_ADDR    le_bda;
100 } tINQ_LE_BDADDR;
101 
102 #define BTM_BLE_ADV_DATA_LEN_MAX        31
103 #define BTM_BLE_CACHE_ADV_DATA_MAX      62
104 
105 #define BTM_BLE_VALID_PRAM(x, min, max)  (((x) >= (min) && (x) <= (max)) || ((x) == BTM_BLE_CONN_PARAM_UNDEF))
106 
107 typedef struct
108 {
109 
110     UINT16           discoverable_mode;
111     UINT16           connectable_mode;
112     UINT16           br_edr_supported_flag;  /* combined BR EDR discoverable and connectable mode */
113                                              /* only meaningful when it is zero. This means
114                                                 BR EDR is not supported*/
115     UINT8            proc_mode;        /* current procedure mode : inquiry or discovery */
116 
117     UINT16           scan_window;
118     UINT16           scan_interval;
119     UINT8            scan_type;        /* current scan type: active or passive */
120     UINT16           adv_interval_min;
121     UINT16           adv_interval_max;
122     tBLE_ADDR_TYPE	 own_addr_type;
123     tBTM_BLE_AFP     afp;               /* advertising filter policy */
124     tBTM_BLE_SFP     sfp;               /* scanning filter policy */
125 
126     UINT8            evt_type;
127     UINT8            adv_mode;
128     tBLE_BD_ADDR     direct_bda;
129 
130     UINT8            adv_len;
131     UINT8            adv_data_cache[BTM_BLE_CACHE_ADV_DATA_MAX];
132 
133     /* inquiry BD addr database */
134     UINT8               num_bd_entries;
135     UINT8               max_bd_entries;
136 
137     tBLE_BD_ADDR        local_bda;
138 
139     tBTM_BLE_LOCAL_ADV_DATA   adv_data;
140     tBTM_BLE_ADV_CHNL_MAP     adv_chnl_map;
141 
142     TIMER_LIST_ENT   inq_timer_ent;
143     BOOLEAN          scan_rsp;
144     UINT8            state;             /* Current state that the inquiry process is in */
145     UINT8           tx_power;
146 } tBTM_BLE_INQ_CB;
147 
148 
149 /* random address resolving complete callback */
150 typedef void (tBTM_BLE_RESOLVE_CBACK) (void * match_rec, void *p);
151 
152 /* random address management control block */
153 typedef struct
154 {
155     BD_ADDR			            private_addr;
156     BD_ADDR                     random_bda;
157     BOOLEAN                     busy;
158     UINT16                       index;
159     tBTM_BLE_RESOLVE_CBACK      *p_resolve_cback;
160     void                        *p;
161     TIMER_LIST_ENT              raddr_timer_ent;
162 } tBTM_LE_RANDOM_CB;
163 
164 #define BTM_BLE_MAX_BG_CONN_DEV_NUM    10
165 
166 typedef struct
167 {
168     UINT16              min_conn_int;
169     UINT16              max_conn_int;
170     UINT16              slave_latency;
171     UINT16              supervision_tout;
172 
173 }tBTM_LE_CONN_PRAMS;
174 
175 /* Define BLE Device Management control structure
176 */
177 typedef struct
178 {
179     /*****************************************************
180     **      BLE Inquiry
181     *****************************************************/
182     tBTM_BLE_INQ_CB     inq_var;
183 
184     /* background connection procedure cb value */
185     tBTM_BLE_CONN_TYPE  bg_conn_type;
186     UINT16              scan_int;
187     UINT16              scan_win;
188     tBTM_BLE_SEL_CBACK  *p_select_cback;
189     TIMER_LIST_ENT      scan_param_idle_timer;
190 
191     UINT8               bg_conn_dev_num;
192     BD_ADDR             bg_conn_dev_list[BTM_BLE_MAX_BG_CONN_DEV_NUM];
193 
194 #define BLE_BG_CONN_IDLE    0
195 #define BLE_BG_CONN_ACTIVE  1
196 #define BLE_BG_CONN_SUSPEND 2
197 
198     UINT8               bg_conn_state;
199 
200     /* random address management control block */
201     tBTM_LE_RANDOM_CB   addr_mgnt_cb;
202 
203     /* white list information */
204     UINT8            num_empty_filter;      /* Number of entries in white list */
205     UINT8            max_filter_entries;    /* Maximum number of entries that can be stored */
206     BOOLEAN          enabled;
207     BOOLEAN          privacy;               /* privacy enabled or disabled */
208 
209 #ifdef BTM_BLE_PC_ADV_TEST_MODE
210     tBTM_BLE_SCAN_REQ_CBACK *p_scan_req_cback;
211 #endif
212 
213 } tBTM_BLE_CB;
214 
215 #ifdef __cplusplus
216 extern "C" {
217 #endif
218 
219 extern void btm_ble_timeout(TIMER_LIST_ENT *p_tle);
220 extern void btm_ble_process_adv_pkt (UINT8 *p);
221 extern void btm_ble_proc_scan_rsp_rpt (UINT8 *p);
222 extern tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb);
223 extern BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda);
224 
225 extern tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode);
226 extern tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode);
227 extern tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8   duration);
228 
229 extern void btm_ble_stop_scan(void);
230 extern void btm_ble_att_db_init(void);
231 extern void btm_ble_init (void);
232 extern void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role);
233 extern void btm_ble_read_remote_features_complete(UINT8 *p);
234 extern void btm_ble_stop_adv(void);
235 extern void btm_ble_write_adv_enable_complete(UINT8 * p);
236 
237 /* LE security function from btm_sec.c */
238 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
239 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);
240 extern void btm_ble_ltk_request_reply(BD_ADDR bda,  BOOLEAN use_stk, BT_OCTET16 stk);
241 extern UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data);
242 extern tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role);
243 extern void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv);
244 extern BOOLEAN btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk);
245 extern void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable);
246 #endif
247 
248 /* LE device management functions */
249 extern void btm_ble_reset_id( void );
250 
251 /* security related functions */
252 extern void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local );
253 extern BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div);
254 extern BOOLEAN btm_ble_check_link_type (BD_ADDR bd_addr);
255 extern BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types);
256 
257 extern void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback);
258 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);
259 extern void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size);
260 extern UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr);
261 
262 /* white list function */
263 extern BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr,tBLE_ADDR_TYPE addr_type);
264 extern BOOLEAN btm_update_bg_conn_list(BOOLEAN to_add, BD_ADDR bd_addr);
265 extern void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy);
266 extern void btm_update_adv_filter_policy(tBTM_BLE_AFP adv_policy);
267 extern void btm_ble_clear_white_list (void);
268 extern void btm_write_bg_conn_wl(void);
269 
270 /* background connection function */
271 extern void btm_ble_suspend_bg_conn(void);
272 extern BOOLEAN btm_ble_resume_bg_conn(tBTM_BLE_SEL_CBACK *p_sele_callback, BOOLEAN def_param);
273 extern void btm_ble_update_bg_state(void);
274 extern void btm_ble_initiate_select_conn(BD_ADDR bda);
275 extern BOOLEAN btm_ble_start_auto_conn(BOOLEAN start);
276 extern BOOLEAN btm_ble_start_select_conn(BOOLEAN start,tBTM_BLE_SEL_CBACK   *p_select_cback);
277 extern BOOLEAN btm_ble_find_dev_in_whitelist(BD_ADDR bd_addr);
278 extern BOOLEAN btm_ble_renew_bg_conn_params(BOOLEAN add, BD_ADDR bd_addr);
279 extern void btm_ble_scan_param_idle(void);
280 extern UINT8 btm_ble_count_unconn_dev_in_whitelist(void);
281 
282 /* BLE address management */
283 extern tBLE_ADDR_TYPE btm_ble_map_bda_to_conn_bda(BD_ADDR bda);
284 extern void btm_gen_resolvable_private_addr (void);
285 extern void btm_gen_non_resolvable_private_addr (void);
286 extern void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK * p_cback, void *p);
287 
288 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
289 BT_API extern void btm_ble_set_no_disc_if_pair_fail (BOOLEAN disble_disc);
290 BT_API extern void btm_ble_set_test_mac_value (BOOLEAN enable, UINT8 *p_test_mac_val);
291 BT_API extern void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr);
292 #endif
293 
294 
295 #ifdef __cplusplus
296 }
297 #endif
298 
299 #endif
300