blob: b61fcb9392b32a9461faf16a87a5d4d60988ae1a [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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 functions for BLE whitelist operation.
22 *
23 ******************************************************************************/
24
25#include <string.h>
26
27#include "bt_types.h"
28#include "btu.h"
29#include "btm_int.h"
30#include "l2c_int.h"
31#include "hcimsgs.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080032#include "bt_utils.h"
Satya Calloji0943c102014-05-12 09:13:02 -070033#if (defined BLE_VND_INCLUDED && BLE_VND_INCLUDED == TRUE)
34#include "vendor_ble.h"
35#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080036
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#ifndef BTM_BLE_SCAN_PARAM_TOUT
38#define BTM_BLE_SCAN_PARAM_TOUT 50 /* 50 seconds */
39#endif
40
41#if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080042
43static void btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state);
44static void btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state);
45
The Android Open Source Project5738f832012-12-12 16:00:35 -080046/*******************************************************************************
47**
48** Function btm_update_scanner_filter_policy
49**
50** Description This function update the filter policy of scnner or advertiser.
51*******************************************************************************/
52void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy)
53{
54 tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
Sharvil Nanavati83c52562014-05-04 00:46:57 -070055 BTM_TRACE_EVENT ("btm_update_scanner_filter_policy");
The Android Open Source Project5738f832012-12-12 16:00:35 -080056
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080057 p_inq->sfp = scan_policy;
58 p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE) ? BTM_BLE_SCAN_MODE_ACTI: p_inq->scan_type;
59
60 btsnd_hcic_ble_set_scan_params (p_inq->scan_type,
The Android Open Source Project5738f832012-12-12 16:00:35 -080061 (UINT16)(!p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval),
62 (UINT16)(!p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window),
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -070063 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
The Android Open Source Project5738f832012-12-12 16:00:35 -080064 scan_policy);
65}
66/*******************************************************************************
67**
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070068** Function btm_add_dev_to_controller
The Android Open Source Project5738f832012-12-12 16:00:35 -080069**
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070070** Description This function load the device into controller white list
The Android Open Source Project5738f832012-12-12 16:00:35 -080071*******************************************************************************/
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070072BOOLEAN btm_add_dev_to_controller (BOOLEAN to_add, BD_ADDR bd_addr, UINT8 attr)
The Android Open Source Project5738f832012-12-12 16:00:35 -080073{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080074 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080075 tBLE_ADDR_TYPE addr_type = BLE_ADDR_PUBLIC;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070076 BOOLEAN started = FALSE;
77 BD_ADDR dummy_bda = {0};
78 tBT_DEVICE_TYPE dev_type;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080079 UNUSED(attr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080080
81 if (p_dev_rec != NULL &&
The Android Open Source Project5738f832012-12-12 16:00:35 -080082 p_dev_rec->device_type == BT_DEVICE_TYPE_BLE)
83 {
The Android Open Source Project5738f832012-12-12 16:00:35 -080084
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080085 if (to_add)
The Android Open Source Project5738f832012-12-12 16:00:35 -080086 {
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070087 if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC || !BTM_BLE_IS_RESOLVE_BDA(bd_addr))
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080088 {
Satya Calloji0943c102014-05-12 09:13:02 -070089#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
90 /* add device into IRK list */
91 btm_ble_vendor_irk_list_load_dev(p_dev_rec);
92#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080093 started = btsnd_hcic_ble_add_white_list (p_dev_rec->ble.ble_addr_type, bd_addr);
94 }
95 if (memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0 &&
96 memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0)
97 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -070098 started = btsnd_hcic_ble_add_white_list (p_dev_rec->ble.static_addr_type,
99 p_dev_rec->ble.static_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800100 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800101 }
102 else
103 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700104 if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC || !BTM_BLE_IS_RESOLVE_BDA(bd_addr))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800105 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800106 started = btsnd_hcic_ble_remove_from_white_list (p_dev_rec->ble.ble_addr_type, bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800107 }
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700108 if (memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0 &&
109 memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800110 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800111 started = btsnd_hcic_ble_remove_from_white_list (p_dev_rec->ble.static_addr_type, p_dev_rec->ble.static_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800112 }
113 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800114 } /* if not a known device, shall we add it? */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800115 else
116 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800117 BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700118
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119 if (to_add)
120 started = btsnd_hcic_ble_add_white_list (addr_type, bd_addr);
121 else
122 started = btsnd_hcic_ble_remove_from_white_list (addr_type, bd_addr);
123 }
124
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700125 return started;
126
127}
128/*******************************************************************************
129**
130** Function btm_execute_wl_dev_operation
131**
132** Description execute the pending whitelist device operation(loading or removing)
133*******************************************************************************/
134BOOLEAN btm_execute_wl_dev_operation(void)
135{
136 tBTM_BLE_WL_OP *p_dev_op = btm_cb.ble_ctr_cb.wl_op_q;
137 UINT8 i = 0;
138 BOOLEAN rt = TRUE;
139
140 for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM && rt; i ++, p_dev_op ++)
141 {
142 if (p_dev_op->in_use)
143 {
144 rt = btm_add_dev_to_controller(p_dev_op->to_add, p_dev_op->bd_addr, p_dev_op->attr);
145 memset(p_dev_op, 0, sizeof(tBTM_BLE_WL_OP));
146 }
147 else
148 break;
149 }
150 return rt;
151}
152/*******************************************************************************
153**
154** Function btm_enq_wl_dev_operation
155**
156** Description enqueue the pending whitelist device operation(loading or removing).
157*******************************************************************************/
158void btm_enq_wl_dev_operation(BOOLEAN to_add, BD_ADDR bd_addr, UINT8 attr)
159{
160 tBTM_BLE_WL_OP *p_dev_op = btm_cb.ble_ctr_cb.wl_op_q;
161 UINT8 i = 0;
162
163 for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM; i ++, p_dev_op ++)
164 {
165 if (p_dev_op->in_use && !memcmp(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN))
166 {
167 p_dev_op->to_add = to_add;
168 p_dev_op->attr = attr;
169 return;
170 }
171 else if (!p_dev_op->in_use)
172 break;
173 }
174 if (i != BTM_BLE_MAX_BG_CONN_DEV_NUM)
175 {
176 p_dev_op->in_use = TRUE;
177 p_dev_op->to_add = to_add;
178 p_dev_op->attr = attr;
179 memcpy(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN);
180 }
181 else
182 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700183 BTM_TRACE_ERROR("max pending WL operation reached, discard");
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700184 }
185 return;
186}
187/*******************************************************************************
188**
189** Function btm_update_dev_to_white_list
190**
191** Description This function adds a device into white list.
192*******************************************************************************/
193BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, UINT8 attr)
194{
195 /* look up the sec device record, and find the address */
196 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
197 BOOLEAN started = FALSE;
198 UINT8 wl_state = p_cb->wl_state;
199
200 if ((to_add && p_cb->num_empty_filter == 0) ||
201 (!to_add && p_cb->num_empty_filter == p_cb->max_filter_entries))
202 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700203 BTM_TRACE_ERROR("WL full or empty, unable to update to WL. num_entry available: %d",
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700204 p_cb->num_empty_filter);
205 return started;
206 }
207
208 btm_suspend_wl_activity(wl_state);
209
210 /* enq pending WL device operation */
211 btm_enq_wl_dev_operation(to_add, bd_addr, attr);
212
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800213 btm_resume_wl_activity(wl_state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800214
215 return started;
216}
217/*******************************************************************************
218**
219** Function btm_ble_clear_white_list
220**
221** Description This function clears the white list.
222*******************************************************************************/
223void btm_ble_clear_white_list (void)
224{
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700225 BTM_TRACE_EVENT ("btm_ble_clear_white_list");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800226 btsnd_hcic_ble_clear_white_list();
227}
228
229/*******************************************************************************
230**
231** Function btm_ble_clear_white_list_complete
232**
233** Description This function clears the white list complete.
234*******************************************************************************/
235void btm_ble_clear_white_list_complete(UINT8 *p_data, UINT16 evt_len)
236{
237 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
238 UINT8 status;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800239 UNUSED(evt_len);
240
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700241 BTM_TRACE_EVENT ("btm_ble_clear_white_list_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800242 STREAM_TO_UINT8 (status, p_data);
243
244 if (status == HCI_SUCCESS)
245 p_cb->num_empty_filter = p_cb->max_filter_entries;
246
247}
248/*******************************************************************************
249**
250** Function btm_ble_add_2_white_list_complete
251**
252** Description This function read the current white list size.
253*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800254void btm_ble_add_2_white_list_complete(UINT8 status)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800255{
256 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700257 BTM_TRACE_EVENT ("btm_ble_add_2_white_list_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800258
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800259 if (status == HCI_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800260 {
261 p_cb->num_empty_filter --;
262 }
263}
264/*******************************************************************************
265**
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700266** Function btm_ble_remove_from_white_list_complete
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267**
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700268** Description This function remove the white list element complete.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800269*******************************************************************************/
270void btm_ble_remove_from_white_list_complete(UINT8 *p, UINT16 evt_len)
271{
272 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800273 UNUSED(evt_len);
274
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700275 BTM_TRACE_EVENT ("btm_ble_remove_from_white_list_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276 if (*p == HCI_SUCCESS)
277 {
278 p_cb->num_empty_filter ++;
279 }
280}
281/*******************************************************************************
282**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283** Function btm_ble_count_unconn_dev_in_whitelist
284**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800285** Description This function find the number of un-connected background device
The Android Open Source Project5738f832012-12-12 16:00:35 -0800286*******************************************************************************/
287UINT8 btm_ble_count_unconn_dev_in_whitelist(void)
288{
289 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800290 UINT8 i, count = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800291
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800292 for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM; i ++)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800293 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800294 if (p_cb->bg_dev_list[i].in_use &&
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700295 !BTM_IsAclConnectionUp(p_cb->bg_dev_list[i].bd_addr, BT_TRANSPORT_LE))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296 {
297 count ++;
298 }
299 }
300 return count;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800301
The Android Open Source Project5738f832012-12-12 16:00:35 -0800302}
303/*******************************************************************************
304**
305** Function btm_update_bg_conn_list
306**
307** Description This function update the local background connection device list.
308*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800309BOOLEAN btm_update_bg_conn_list(BOOLEAN to_add, BD_ADDR bd_addr, UINT8 *p_attr_tag)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800310{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800311 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
312 tBTM_LE_BG_CONN_DEV *p_bg_dev = &p_cb->bg_dev_list[0], *p_next, *p_cur;
313 UINT8 i, j;
314 BOOLEAN ret = FALSE;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800315 UNUSED(p_attr_tag);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800316
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700317 BTM_TRACE_EVENT ("btm_update_bg_conn_list");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800318
319 if ((to_add && (p_cb->bg_dev_num == BTM_BLE_MAX_BG_CONN_DEV_NUM || p_cb->num_empty_filter == 0)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700321 BTM_TRACE_DEBUG("num_empty_filter = %d", p_cb->num_empty_filter);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800322 return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800323 }
324
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800325 for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM; i ++, p_bg_dev ++)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800326 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800327 if (p_bg_dev->in_use && memcmp(p_bg_dev->bd_addr, bd_addr, BD_ADDR_LEN) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800328 {
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700329 if (!to_add)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800330 {
331 memset(p_bg_dev, 0, sizeof(tBTM_LE_BG_CONN_DEV));
332 p_cb->bg_dev_num --;
333 p_cur = p_bg_dev;
334 p_next = p_bg_dev + 1;
335 for (j = i + 1 ;j < BTM_BLE_MAX_BG_CONN_DEV_NUM && p_next->in_use ; j ++, p_cur ++, p_next ++ )
336 memcpy(p_cur, p_next, sizeof(tBTM_LE_BG_CONN_DEV));
337 }
338 ret = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800339 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800340 }
341 else if (!p_bg_dev->in_use && to_add)
342 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700343 BTM_TRACE_DEBUG("add new WL entry in bg_dev_list");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800344
345 memcpy(p_bg_dev->bd_addr, bd_addr, BD_ADDR_LEN);
346 p_bg_dev->in_use = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800347 p_cb->bg_dev_num ++;
348
349 ret = TRUE;
350 break;
351 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800353
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800354
355 return ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800357
The Android Open Source Project5738f832012-12-12 16:00:35 -0800358/*******************************************************************************
359**
360** Function btm_ble_start_auto_conn
361**
362** Description This function is to start/stop auto connection procedure.
363**
364** Parameters start: TRUE to start; FALSE to stop.
365**
366** Returns void
367**
368*******************************************************************************/
369BOOLEAN btm_ble_start_auto_conn(BOOLEAN start)
370{
371 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
372 BD_ADDR dummy_bda = {0};
373 BOOLEAN exec = TRUE;
374 UINT16 scan_int, scan_win;
375
The Android Open Source Project5738f832012-12-12 16:00:35 -0800376 if (start)
377 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700378 if ((p_cb->conn_state == BLE_CONN_IDLE && btm_ble_count_unconn_dev_in_whitelist() > 0)
379 && btm_ble_topology_check(BTM_BLE_STATE_INIT))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800380 {
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700381 btm_execute_wl_dev_operation();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800382
383 scan_int = (p_cb->scan_int == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_SLOW_INT_1 : p_cb->scan_int;
384 scan_win = (p_cb->scan_win == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_SLOW_WIN_1 : p_cb->scan_win;
385
386 if (!btsnd_hcic_ble_create_ll_conn (scan_int, /* UINT16 scan_int */
387 scan_win, /* UINT16 scan_win */
388 0x01, /* UINT8 white_list */
389 BLE_ADDR_PUBLIC, /* UINT8 addr_type_peer */
390 dummy_bda, /* BD_ADDR bda_peer */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700391 p_cb->addr_mgnt_cb.own_addr_type,
392 /* UINT8 addr_type_own,
393 not allow random address for central */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800394 BTM_BLE_CONN_INT_MIN_DEF, /* UINT16 conn_int_min */
395 BTM_BLE_CONN_INT_MAX_DEF, /* UINT16 conn_int_max */
396 BTM_BLE_CONN_SLAVE_LATENCY_DEF, /* UINT16 conn_latency */
397 BTM_BLE_CONN_TIMEOUT_DEF, /* UINT16 conn_timeout */
398 0, /* UINT16 min_len */
399 0)) /* UINT16 max_len */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800400 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800401 /* start auto connection failed */
402 exec = FALSE;
403 }
404 else
405 {
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700406 btm_ble_set_conn_st (BLE_BG_CONN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800407
The Android Open Source Project5738f832012-12-12 16:00:35 -0800408 }
409 }
410 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800411 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800412 exec = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800413 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800414 }
415 else
416 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800417 if (p_cb->conn_state == BLE_BG_CONN)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800418 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800419 btsnd_hcic_ble_create_conn_cancel();
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700420 btm_ble_set_conn_st (BLE_CONN_CANCEL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800421
Satya Calloji0943c102014-05-12 09:13:02 -0700422#if (defined BLE_VND_INCLUDED && BLE_VND_INCLUDED == TRUE)
423#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
424 btm_ble_vendor_disable_irk_list();
425#endif
426#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800427 }
428 else
429 {
430#if 0
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700431 BTM_TRACE_ERROR("conn_st = %d, not in auto conn state, can not stop.", p_cb->conn_state);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800432 exec = FALSE;
433#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434 }
435 }
436 return exec;
437}
438
439/*******************************************************************************
440**
441** Function btm_ble_start_select_conn
442**
443** Description This function is to start/stop selective connection procedure.
444**
445** Parameters start: TRUE to start; FALSE to stop.
446** p_select_cback: callback function to return application
447** selection.
448**
449** Returns BOOLEAN: selective connectino procedure is started.
450**
451*******************************************************************************/
452BOOLEAN btm_ble_start_select_conn(BOOLEAN start,tBTM_BLE_SEL_CBACK *p_select_cback)
453{
454 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
455 UINT16 scan_int, scan_win;
456
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700457 BTM_TRACE_EVENT ("btm_ble_start_select_conn");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800458
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800459 scan_int = (p_cb->scan_int == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
460 scan_win = (p_cb->scan_win == BTM_BLE_CONN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461
462 if (start)
463 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700464 if (!BTM_BLE_IS_SCAN_ACTIVE(p_cb->scan_activity))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800465 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800466 if (p_select_cback != NULL)
467 btm_cb.ble_ctr_cb.p_select_cback = p_select_cback;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800468
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700469 btm_execute_wl_dev_operation();
470
The Android Open Source Project5738f832012-12-12 16:00:35 -0800471 btm_update_scanner_filter_policy(SP_ADV_WL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800472 btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_PASS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800473
474 if (!btsnd_hcic_ble_set_scan_params(BTM_BLE_SCAN_MODE_PASS, /* use passive scan by default */
475 scan_int, /* scan interval */
476 scan_win, /* scan window */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700477 p_cb->addr_mgnt_cb.own_addr_type,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800478 SP_ADV_WL) /* process advertising packets only from devices in the White List */
479 )
480 return FALSE;
481
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700482 if (!btm_ble_topology_check(BTM_BLE_STATE_PASSIVE_SCAN))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800483 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700484 BTM_TRACE_ERROR("peripheral device cannot initiate passive scan for a selective connection");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800485 return FALSE;
486 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800487 else if (p_cb->bg_dev_num > 0 && btm_ble_count_unconn_dev_in_whitelist() > 0 )
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488 {
489
490 if (!btsnd_hcic_ble_set_scan_enable(TRUE, TRUE)) /* duplicate filtering enabled */
491 return FALSE;
492
493 /* mark up inquiry status flag */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700494 p_cb->scan_activity |= BTM_LE_SELECT_CONN_ACTIVE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800495 }
496 }
497 else
498 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700499 BTM_TRACE_ERROR("scan active, can not start selective connection procedure");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800500 return FALSE;
501 }
502 }
503 else /* disable selective connection mode */
504 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700505 p_cb->scan_activity &= ~BTM_LE_SELECT_CONN_ACTIVE;
506 p_cb->p_select_cback = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507
Satya Calloji0943c102014-05-12 09:13:02 -0700508#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
509#if (defined BLE_VND_INCLUDED && BLE_VND_INCLUDED == TRUE)
510 btm_ble_vendor_disable_irk_list();
511#endif
512#endif
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700513
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514 /* stop scanning */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700515 if (!BTM_BLE_IS_SCAN_ACTIVE(p_cb->scan_activity))
516 btm_ble_stop_scan(); /* duplicate filtering enabled */
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700517 btm_update_scanner_filter_policy(SP_ADV_ALL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800518 }
519 return TRUE;
520}
521/*******************************************************************************
522**
523** Function btm_ble_initiate_select_conn
524**
525** Description This function is to start/stop selective connection procedure.
526**
527** Parameters start: TRUE to start; FALSE to stop.
528** p_select_cback: callback function to return application
529** selection.
530**
531** Returns BOOLEAN: selective connectino procedure is started.
532**
533*******************************************************************************/
534void btm_ble_initiate_select_conn(BD_ADDR bda)
535{
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700536 BTM_TRACE_EVENT ("btm_ble_initiate_select_conn");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800537
538 /* use direct connection procedure to initiate connection */
539 if (!L2CA_ConnectFixedChnl(L2CAP_ATT_CID, bda))
540 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700541 BTM_TRACE_ERROR("btm_ble_initiate_select_conn failed");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800542 }
543}
544/*******************************************************************************
545**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800546** Function btm_ble_suspend_bg_conn
547**
548** Description This function is to suspend an active background connection
549** procedure.
550**
551** Parameters none.
552**
553** Returns none.
554**
555*******************************************************************************/
556void btm_ble_suspend_bg_conn(void)
557{
558 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700559 BTM_TRACE_EVENT ("btm_ble_suspend_bg_conn");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560
561 if (p_cb->bg_conn_type == BTM_BLE_CONN_AUTO)
562 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800563 btm_ble_start_auto_conn(FALSE);
564 }
565 else if (p_cb->bg_conn_type == BTM_BLE_CONN_SELECTIVE)
566 {
567 btm_ble_start_select_conn(FALSE, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568 }
569}
570/*******************************************************************************
571**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800572** Function btm_suspend_wl_activity
The Android Open Source Project5738f832012-12-12 16:00:35 -0800573**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800574** Description This function is to suspend white list related activity
575**
576** Returns none.
577**
578*******************************************************************************/
579static void btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800580{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800581 if (wl_state & BTM_BLE_WL_INIT)
582 {
583 btm_ble_start_auto_conn(FALSE);
584 }
585 if (wl_state & BTM_BLE_WL_SCAN)
586 {
587 btm_ble_start_select_conn(FALSE, NULL);
588 }
589 if (wl_state & BTM_BLE_WL_ADV)
590 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700591 btm_ble_stop_adv();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800592 }
593
594}
595/*******************************************************************************
596**
597** Function btm_resume_wl_activity
598**
599** Description This function is to resume white list related activity
600**
601** Returns none.
602**
603*******************************************************************************/
604static void btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state)
605{
606 btm_ble_resume_bg_conn();
607
608 if (wl_state & BTM_BLE_WL_ADV)
609 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700610 btm_ble_start_adv();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800611 }
612
The Android Open Source Project5738f832012-12-12 16:00:35 -0800613}
614/*******************************************************************************
615**
616** Function btm_ble_resume_bg_conn
617**
618** Description This function is to resume a background auto connection
619** procedure.
620**
621** Parameters none.
622**
623** Returns none.
624**
625*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800626BOOLEAN btm_ble_resume_bg_conn(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800627{
628 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
629 BOOLEAN ret = FALSE;
630
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800631 if (p_cb->bg_conn_type != BTM_BLE_CONN_NONE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800632 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800633 if (p_cb->bg_conn_type == BTM_BLE_CONN_AUTO)
634 ret = btm_ble_start_auto_conn(TRUE);
635
636 if (p_cb->bg_conn_type == BTM_BLE_CONN_SELECTIVE)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800637 ret = btm_ble_start_select_conn(TRUE, btm_cb.ble_ctr_cb.p_select_cback);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638 }
639
640 return ret;
641}
642/*******************************************************************************
643**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800644** Function btm_ble_get_conn_st
The Android Open Source Project5738f832012-12-12 16:00:35 -0800645**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800646** Description This function get BLE connection state
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800648** Returns connection state
The Android Open Source Project5738f832012-12-12 16:00:35 -0800649**
650*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800651tBTM_BLE_CONN_ST btm_ble_get_conn_st(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800652{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800653 return btm_cb.ble_ctr_cb.conn_state;
654}
655/*******************************************************************************
656**
657** Function btm_ble_set_conn_st
658**
659** Description This function set BLE connection state
660**
661** Returns None.
662**
663*******************************************************************************/
664void btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st)
665{
666 btm_cb.ble_ctr_cb.conn_state = new_st;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700667 if (new_st == BLE_BG_CONN || new_st == BLE_DIR_CONN)
668 btm_ble_set_topology_mask(BTM_BLE_STATE_INIT_BIT);
669 else
670 btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800671}
672
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800673/*******************************************************************************
674**
675** Function btm_ble_enqueue_direct_conn_req
676**
677** Description This function enqueue the direct connection request
678**
679** Returns None.
680**
681*******************************************************************************/
682void btm_ble_enqueue_direct_conn_req(void *p_param)
683{
684 tBTM_BLE_CONN_REQ *p = (tBTM_BLE_CONN_REQ *)GKI_getbuf(sizeof(tBTM_BLE_CONN_REQ));
685
686 p->p_param = p_param;
687
688 GKI_enqueue (&btm_cb.ble_ctr_cb.conn_pending_q, p);
689}
690/*******************************************************************************
691**
692** Function btm_send_pending_direct_conn
693**
694** Description This function send the pending direct connection request in queue
695**
696** Returns TRUE if started, FALSE otherwise
697**
698*******************************************************************************/
699BOOLEAN btm_send_pending_direct_conn(void )
700{
701 tBTM_BLE_CONN_REQ *p_req;
702 BOOLEAN rt = FALSE;
703
704 if ( btm_cb.ble_ctr_cb.conn_pending_q.count )
705 {
706 p_req = (tBTM_BLE_CONN_REQ*)GKI_dequeue (&btm_cb.ble_ctr_cb.conn_pending_q);
707
708 rt = l2cble_init_direct_conn((tL2C_LCB *)(p_req->p_param));
709
710 GKI_freebuf((void *)p_req);
711 }
712
713 return rt;
714}
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700715
The Android Open Source Project5738f832012-12-12 16:00:35 -0800716#endif
717
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800718