• 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 
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 typedef UINT8 tBTM_BLE_CONN_ST;
194 
195 typedef struct
196 {
197     void    *p_param;
198 }tBTM_BLE_CONN_REQ;
199 /* Define BLE Device Management control structure
200 */
201 typedef struct
202 {
203     /*****************************************************
204     **      BLE Inquiry
205     *****************************************************/
206     tBTM_BLE_INQ_CB     inq_var;
207 
208     /* background connection procedure cb value */
209     tBTM_BLE_CONN_TYPE  bg_conn_type;
210     UINT16              scan_int;
211     UINT16              scan_win;
212     tBTM_BLE_SEL_CBACK  *p_select_cback;
213     TIMER_LIST_ENT      scan_param_idle_timer;
214 
215     /* white list information */
216     UINT8                   num_empty_filter;      /* Number of entries in white list */
217     UINT8                   max_filter_entries;    /* Maximum number of entries that can be stored */
218     tBTM_BLE_WL_STATE       wl_state;
219     UINT8                   bg_dev_num;
220     tBTM_LE_BG_CONN_DEV     bg_dev_list[BTM_BLE_MAX_BG_CONN_DEV_NUM];
221 
222     BUFFER_Q                conn_pending_q;
223     tBTM_BLE_CONN_ST        conn_state;
224 
225     /* random address management control block */
226     tBTM_LE_RANDOM_CB   addr_mgnt_cb;
227 
228     BOOLEAN          enabled;
229 
230 #ifdef BTM_BLE_PC_ADV_TEST_MODE
231     tBTM_BLE_SCAN_REQ_CBACK *p_scan_req_cback;
232 #endif
233 
234     BOOLEAN         scatternet_enable;
235 } tBTM_BLE_CB;
236 
237 #ifdef __cplusplus
238 extern "C" {
239 #endif
240 
241 extern void btm_ble_timeout(TIMER_LIST_ENT *p_tle);
242 extern void btm_ble_process_adv_pkt (UINT8 *p);
243 extern void btm_ble_proc_scan_rsp_rpt (UINT8 *p);
244 extern tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb);
245 extern BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda);
246 
247 extern tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode);
248 extern tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode);
249 extern tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8   duration);
250 extern void btm_ble_dir_adv_tout(void);
251 
252 extern void btm_ble_stop_scan(void);
253 extern void btm_ble_att_db_init(void);
254 extern void btm_ble_init (void);
255 extern void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role, tBLE_ADDR_TYPE addr_type, BOOLEAN addr_matched);
256 extern void btm_ble_read_remote_features_complete(UINT8 *p);
257 extern void btm_ble_write_adv_enable_complete(UINT8 * p);
258 extern void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len);
259 extern tBTM_BLE_CONN_ST btm_ble_get_conn_st(void);
260 extern void btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st);
261 
262 
263 /* LE security function from btm_sec.c */
264 #if SMP_INCLUDED == TRUE
265 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);
266 extern void btm_ble_ltk_request_reply(BD_ADDR bda,  BOOLEAN use_stk, BT_OCTET16 stk);
267 extern UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data);
268 extern tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role);
269 extern void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv);
270 extern BOOLEAN btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk);
271 extern void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable);
272 #endif
273 
274 /* LE device management functions */
275 extern void btm_ble_reset_id( void );
276 
277 /* security related functions */
278 extern void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local );
279 extern BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div);
280 extern BOOLEAN btm_ble_check_link_type (BD_ADDR bd_addr);
281 extern BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types);
282 
283 extern void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback);
284 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);
285 extern void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size);
286 extern UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr);
287 
288 /* white list function */
289 extern BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr,UINT8 wl_type);
290 extern BOOLEAN btm_update_bg_conn_list(BOOLEAN to_add, BD_ADDR bd_addr, UINT8 *p_attr_tag);
291 extern void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy);
292 extern void btm_update_adv_filter_policy(tBTM_BLE_AFP adv_policy);
293 extern void btm_ble_clear_white_list (void);
294 
295 /* background connection function */
296 extern void btm_ble_suspend_bg_conn(void);
297 extern BOOLEAN btm_ble_resume_bg_conn(void);
298 extern void btm_ble_initiate_select_conn(BD_ADDR bda);
299 extern BOOLEAN btm_ble_start_auto_conn(BOOLEAN start);
300 extern BOOLEAN btm_ble_start_select_conn(BOOLEAN start,tBTM_BLE_SEL_CBACK   *p_select_cback);
301 extern BOOLEAN btm_ble_renew_bg_conn_params(BOOLEAN add, BD_ADDR bd_addr);
302 extern UINT8 btm_ble_count_unconn_dev_in_whitelist(void);
303 extern void btm_write_dir_conn_wl(BD_ADDR target_addr);
304 extern void btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bda, BOOLEAN conn_ccancel);
305 
306 /* direct connection utility */
307 extern BOOLEAN btm_send_pending_direct_conn(void);
308 extern void btm_ble_enqueue_direct_conn_req(void *p_param);
309 
310 /* BLE address management */
311 extern void btm_gen_resolvable_private_addr (void);
312 extern void btm_gen_non_resolvable_private_addr (tBTM_BLE_ADDR_CBACK *p_cback, void *p);
313 extern void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK * p_cback, void *p);
314 extern void btm_ble_update_reconnect_address(BD_ADDR bd_addr);
315 
316 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
317 BT_API extern void btm_ble_set_no_disc_if_pair_fail (BOOLEAN disble_disc);
318 BT_API extern void btm_ble_set_test_mac_value (BOOLEAN enable, UINT8 *p_test_mac_val);
319 BT_API extern void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr);
320 BT_API extern void btm_set_random_address(BD_ADDR random_bda);
321 #endif
322 
323 
324 #ifdef __cplusplus
325 }
326 #endif
327 
328 #endif
329