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 functions for BLE whitelist operation.
22  *
23  ******************************************************************************/
24 
25 #include <string.h>
26 #include "common/bt_trace.h"
27 #include "device/controller.h"
28 #include "osi/allocator.h"
29 #include "osi/hash_map.h"
30 #include "stack/bt_types.h"
31 #include "stack/btu.h"
32 #include "btm_int.h"
33 #include "l2c_int.h"
34 #include "stack/hcimsgs.h"
35 //#include "bt_utils.h"
36 
37 #ifndef BTM_BLE_SCAN_PARAM_TOUT
38 #define BTM_BLE_SCAN_PARAM_TOUT      50    /* 50 seconds */
39 #endif
40 
41 #if (BLE_INCLUDED == TRUE)
42 
43 static void btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state);
44 static void btm_wl_update_to_controller(void);
45 
46 // Unfortunately (for now?) we have to maintain a copy of the device whitelist
47 // on the host to determine if a device is pending to be connected or not. This
48 // controls whether the host should keep trying to scan for whitelisted
49 // peripherals or not.
50 // TODO: Move all of this to controller/le/background_list or similar?
51 static const size_t background_connection_buckets = 42;
52 static hash_map_t *background_connections = NULL;
53 
54 typedef struct background_connection_t {
55     bt_bdaddr_t address;
56 } background_connection_t;
57 
bdaddr_equality_fn(const void * x,const void * y)58 static bool bdaddr_equality_fn(const void *x, const void *y)
59 {
60     return bdaddr_equals((bt_bdaddr_t *)x, (bt_bdaddr_t *)y);
61 }
62 
background_connections_lazy_init(void)63 static void background_connections_lazy_init(void)
64 {
65     if (!background_connections) {
66         background_connections = hash_map_new(background_connection_buckets,
67                                       hash_function_bdaddr, NULL, osi_free_func, bdaddr_equality_fn);
68         assert(background_connections);
69     }
70 }
71 
background_connection_add(bt_bdaddr_t * address)72 static BOOLEAN background_connection_add(bt_bdaddr_t *address)
73 {
74     assert(address);
75     background_connections_lazy_init();
76     background_connection_t *connection = hash_map_get(background_connections, address);
77     if (!connection) {
78         connection = osi_calloc(sizeof(background_connection_t));
79         connection->address = *address;
80         hash_map_set(background_connections, &(connection->address), connection);
81         return TRUE;
82     }
83     return FALSE;
84 }
85 
background_connection_remove(bt_bdaddr_t * address)86 static BOOLEAN background_connection_remove(bt_bdaddr_t *address)
87 {
88     if (address && background_connections) {
89         return hash_map_erase(background_connections, address);
90     }
91     return FALSE;
92 }
93 
background_connections_clear(void)94 static void background_connections_clear(void)
95 {
96     if (background_connections) {
97         hash_map_clear(background_connections);
98     }
99 }
100 
background_connections_pending_cb(hash_map_entry_t * hash_entry,void * context)101 static bool background_connections_pending_cb(hash_map_entry_t *hash_entry, void *context)
102 {
103     bool *pending_connections = context;
104     background_connection_t *connection = hash_entry->data;
105     const bool connected = BTM_IsAclConnectionUp(connection->address.address, BT_TRANSPORT_LE);
106     if (!connected) {
107         *pending_connections = true;
108         return false;
109     }
110     return true;
111 }
112 
background_connections_pending(void)113 static bool background_connections_pending(void)
114 {
115     bool pending_connections = false;
116     if (background_connections) {
117         hash_map_foreach(background_connections, background_connections_pending_cb, &pending_connections);
118     }
119     return pending_connections;
120 }
121 
122 /*******************************************************************************
123 **
124 ** Function         btm_update_scanner_filter_policy
125 **
126 ** Description      This function updates the filter policy of scanner
127 *******************************************************************************/
btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy)128 void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy)
129 {
130     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
131 
132     UINT32 scan_interval = !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
133     UINT32 scan_window = !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
134 
135     BTM_TRACE_EVENT ("%s\n", __func__);
136 
137     p_inq->sfp = scan_policy;
138     p_inq->scan_type = p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE ? BTM_BLE_SCAN_MODE_ACTI : p_inq->scan_type;
139 
140     if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
141         btsnd_hcic_ble_set_scan_params(p_inq->scan_type, (UINT16)scan_interval,
142                                        (UINT16)scan_window,
143                                        btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
144                                        scan_policy);
145     } else {
146         btm_ble_send_extended_scan_params(p_inq->scan_type, scan_interval, scan_window,
147                                           btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
148                                           scan_policy);
149     }
150 }
151 /*******************************************************************************
152 **
153 ** Function         btm_add_dev_to_controller
154 **
155 ** Description      This function load the device into controller white list
156 *******************************************************************************/
btm_add_dev_to_controller(BOOLEAN to_add,BD_ADDR bd_addr,tBLE_ADDR_TYPE wl_addr_type)157 BOOLEAN btm_add_dev_to_controller (BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE wl_addr_type)
158 {
159     /*
160     tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_dev (bd_addr);
161     tBLE_ADDR_TYPE  addr_type = BLE_ADDR_PUBLIC;
162     BOOLEAN             started = FALSE;
163     BD_ADDR             dummy_bda = {0};
164     tBT_DEVICE_TYPE dev_type;
165 
166     if (p_dev_rec != NULL &&
167             p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) {
168         if (to_add) {
169             if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC || !BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
170                 started = btsnd_hcic_ble_add_white_list (p_dev_rec->ble.ble_addr_type, bd_addr);
171                 p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
172             } else if (memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0 &&
173                        memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0) {
174                 started = btsnd_hcic_ble_add_white_list (p_dev_rec->ble.static_addr_type,
175                           p_dev_rec->ble.static_addr);
176                 p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
177             }
178         } else {
179             if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC || !BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
180                 started = btsnd_hcic_ble_remove_from_white_list (p_dev_rec->ble.ble_addr_type, bd_addr);
181             }
182             if (memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0 &&
183                     memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0) {
184                 started = btsnd_hcic_ble_remove_from_white_list (p_dev_rec->ble.static_addr_type, p_dev_rec->ble.static_addr);
185             }
186             p_dev_rec->ble.in_controller_list &= ~BTM_WHITE_LIST_BIT;
187         }
188     }    // if not a known device, shall we add it?
189     else {
190         BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
191 
192         if (to_add) {
193             started = btsnd_hcic_ble_add_white_list (addr_type, bd_addr);
194         }else{
195             started = btsnd_hcic_ble_remove_from_white_list (addr_type, bd_addr);
196         }
197     }
198 
199     return started;
200     */
201 
202     /* Controller do not support resolvable address now, only support public address and static random address */
203     BOOLEAN  started = FALSE;
204     if(wl_addr_type > BLE_ADDR_RANDOM) {
205         BTM_TRACE_ERROR("wl_addr_type is error\n");
206         return started;
207     }
208 
209     if (to_add) {
210         started = btsnd_hcic_ble_add_white_list (wl_addr_type, bd_addr);
211     }else{
212         started = btsnd_hcic_ble_remove_from_white_list (wl_addr_type, bd_addr);
213     }
214 
215     return started;
216 
217 
218 }
219 /*******************************************************************************
220 **
221 ** Function         btm_execute_wl_dev_operation
222 **
223 ** Description      execute the pending whitelist device operation(loading or removing)
224 *******************************************************************************/
btm_execute_wl_dev_operation(void)225 BOOLEAN btm_execute_wl_dev_operation(void)
226 {
227     tBTM_BLE_WL_OP *p_dev_op = btm_cb.ble_ctr_cb.wl_op_q;
228     UINT8   i = 0;
229     BOOLEAN rt = TRUE;
230 
231     for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM && rt; i ++, p_dev_op ++) {
232         if (p_dev_op->in_use) {
233             rt = btm_add_dev_to_controller(p_dev_op->to_add, p_dev_op->bd_addr, p_dev_op->addr_type);
234             memset(p_dev_op, 0, sizeof(tBTM_BLE_WL_OP));
235         } else {
236             break;
237         }
238     }
239     return rt;
240 }
241 /*******************************************************************************
242 **
243 ** Function         btm_enq_wl_dev_operation
244 **
245 ** Description      enqueue the pending whitelist device operation(loading or removing).
246 *******************************************************************************/
btm_enq_wl_dev_operation(BOOLEAN to_add,BD_ADDR bd_addr,tBLE_ADDR_TYPE addr_type)247 void btm_enq_wl_dev_operation(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type)
248 {
249     tBTM_BLE_WL_OP *p_dev_op = btm_cb.ble_ctr_cb.wl_op_q;
250     UINT8   i = 0;
251 
252     for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM; i ++, p_dev_op ++) {
253         if (p_dev_op->in_use && p_dev_op->addr_type == addr_type && !memcmp(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN)) {
254             p_dev_op->to_add = to_add;
255             return;
256         } else if (!p_dev_op->in_use) {
257             break;
258         }
259     }
260     if (i != BTM_BLE_MAX_BG_CONN_DEV_NUM) {
261         p_dev_op->in_use = TRUE;
262         p_dev_op->to_add = to_add;
263         p_dev_op->addr_type = addr_type;
264         memcpy(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN);
265     } else {
266         BTM_TRACE_ERROR("max pending WL operation reached, discard");
267     }
268     return;
269 }
270 
271 /*******************************************************************************
272 **
273 ** Function         btm_update_dev_to_white_list
274 **
275 ** Description      This function adds or removes a device into/from
276 **                  the white list.
277 **
278 *******************************************************************************/
btm_update_dev_to_white_list(BOOLEAN to_add,BD_ADDR bd_addr,tBLE_ADDR_TYPE addr_type,tBTM_UPDATE_WHITELIST_CBACK * update_wl_cb)279 BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type, tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
280 {
281     if(addr_type > BLE_ADDR_RANDOM) {
282         BTM_TRACE_ERROR("%s address type is error, unable to add device", __func__);
283         if (update_wl_cb){
284             update_wl_cb(HCI_ERR_ILLEGAL_PARAMETER_FMT,to_add);
285         }
286         return FALSE;
287     }
288 
289     BD_ADDR invalid_rand_addr_a, invalid_rand_addr_b;
290     memset(invalid_rand_addr_a, 0xff, sizeof(BD_ADDR));
291     memset(invalid_rand_addr_b, 0x00, sizeof(BD_ADDR));
292 
293     // look for public address information
294     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
295     // p_dev_rec is created at bluetooth initialization, p_dev_rec->ble.static_addr maybe be all 0 before pairing
296     if(p_dev_rec && memcmp(invalid_rand_addr_b, p_dev_rec->ble.static_addr, BD_ADDR_LEN) != 0) {
297         memcpy(bd_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
298         addr_type = p_dev_rec->ble.static_addr_type;
299     }
300 
301     // The device to be added to white list must be public address or random address
302     if(addr_type == BLE_ADDR_RANDOM) {
303         /*
304         A static address is a 48-bit randomly generated address and shall meet the following requirements:
305         • The two most significant bits of the address shall be equal to 1
306         • All bits of the random part of the address shall not be equal to 1
307         • All bits of the random part of the address shall not be equal to 0
308         */
309         invalid_rand_addr_b[0] = invalid_rand_addr_b[0] | BT_STATIC_RAND_ADDR_MASK;
310         if(memcmp(invalid_rand_addr_a, bd_addr, BD_ADDR_LEN) != 0
311             && memcmp(invalid_rand_addr_b, bd_addr, BD_ADDR_LEN) != 0){
312             // do nothing
313         } else {
314             BTC_TRACE_ERROR(" controller not support resolvable address");
315             if (update_wl_cb){
316                 update_wl_cb(HCI_ERR_ILLEGAL_PARAMETER_FMT,to_add);
317             }
318             return FALSE;
319         }
320 
321     }
322 
323     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
324 
325     if (to_add && p_cb->white_list_avail_size == 0) {
326         BTM_TRACE_ERROR("%s Whitelist full, unable to add device", __func__);
327         if (update_wl_cb){
328             update_wl_cb(HCI_ERR_MEMORY_FULL,to_add);
329         }
330         return FALSE;
331     }
332 
333     if (to_add) {
334         /* added the bd_addr to the connection hash map queue */
335         if(!background_connection_add((bt_bdaddr_t *)bd_addr)) {
336             /* if the bd_addr already exist in whitelist, just callback return TRUE */
337             if (update_wl_cb){
338                 update_wl_cb(HCI_SUCCESS,to_add);
339             }
340             return TRUE;
341         }
342     } else {
343         /* remove the bd_addr to the connection hash map queue */
344         if(!background_connection_remove((bt_bdaddr_t *)bd_addr)){
345             /* if the bd_addr don't exist in whitelist, just callback return TRUE */
346             if (update_wl_cb){
347                 update_wl_cb(HCI_SUCCESS,to_add);
348             }
349             return TRUE;
350         }
351     }
352 
353     if (update_wl_cb){
354         //save add whitelist complete callback
355         p_cb->update_wl_cb = update_wl_cb;
356     }
357     /* stop the auto connect */
358     btm_suspend_wl_activity(p_cb->wl_state);
359     /* save the bd_addr to the btm_cb env */
360     btm_enq_wl_dev_operation(to_add, bd_addr, addr_type);
361     /* save the ba_addr to the controller white list */
362     btm_wl_update_to_controller();
363     return TRUE;
364 }
365 
366 /*******************************************************************************
367 **
368 ** Function         btm_ble_clear_white_list
369 **
370 ** Description      This function clears the white list.
371 **
372 *******************************************************************************/
btm_ble_clear_white_list(tBTM_UPDATE_WHITELIST_CBACK * update_wl_cb)373 void btm_ble_clear_white_list (tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
374 {
375     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
376 
377     BTM_TRACE_EVENT ("btm_ble_clear_white_list");
378     btsnd_hcic_ble_clear_white_list();
379     background_connections_clear();
380 
381     if (update_wl_cb) {
382         p_cb->update_wl_cb = update_wl_cb;
383     }
384 }
385 
386 /*******************************************************************************
387 **
388 ** Function         btm_ble_clear_white_list_complete
389 **
390 ** Description      Indicates white list cleared.
391 **
392 *******************************************************************************/
btm_ble_clear_white_list_complete(UINT8 * p_data,UINT16 evt_len)393 void btm_ble_clear_white_list_complete(UINT8 *p_data, UINT16 evt_len)
394 {
395     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
396     UINT8       status;
397     UNUSED(evt_len);
398 
399     BTM_TRACE_EVENT ("btm_ble_clear_white_list_complete");
400     STREAM_TO_UINT8  (status, p_data);
401 
402     if (status == HCI_SUCCESS) {
403         p_cb->white_list_avail_size = controller_get_interface()->get_ble_white_list_size();
404     } else {
405         BTM_TRACE_ERROR ("%s failed, status 0x%x\n", __func__, status);
406     }
407 
408     if (p_cb->update_wl_cb) {
409         (*p_cb->update_wl_cb)(status, BTM_WHITELIST_CLEAR);
410     }
411 }
412 
413 /*******************************************************************************
414 **
415 ** Function         btm_ble_white_list_init
416 **
417 ** Description      Initialize white list size
418 **
419 *******************************************************************************/
btm_ble_white_list_init(UINT8 white_list_size)420 void btm_ble_white_list_init(UINT8 white_list_size)
421 {
422     BTM_TRACE_DEBUG("%s white_list_size = %d", __func__, white_list_size);
423     btm_cb.ble_ctr_cb.white_list_avail_size = white_list_size;
424 }
425 
426 /*******************************************************************************
427 **
428 ** Function         btm_ble_add_2_white_list_complete
429 **
430 ** Description      White list element added
431 **
432 *******************************************************************************/
btm_ble_add_2_white_list_complete(UINT8 status)433 void btm_ble_add_2_white_list_complete(UINT8 status)
434 {
435     BTM_TRACE_EVENT("%s status=%d", __func__, status);
436     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
437     if (status == HCI_SUCCESS) {
438         --btm_cb.ble_ctr_cb.white_list_avail_size;
439     }
440     // add whitelist complete callback
441     if (p_cb->update_wl_cb)
442     {
443         (*p_cb->update_wl_cb)(status, BTM_WHITELIST_ADD);
444     }
445 
446 }
447 
448 /*******************************************************************************
449 **
450 ** Function         btm_ble_remove_from_white_list_complete
451 **
452 ** Description      White list element removal complete
453 **
454 *******************************************************************************/
btm_ble_remove_from_white_list_complete(UINT8 * p,UINT16 evt_len)455 void btm_ble_remove_from_white_list_complete(UINT8 *p, UINT16 evt_len)
456 {
457     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
458     UNUSED(evt_len);
459     BTM_TRACE_EVENT ("%s status=%d", __func__, *p);
460     if (*p == HCI_SUCCESS) {
461         ++btm_cb.ble_ctr_cb.white_list_avail_size;
462     }
463     if (p_cb->update_wl_cb)
464     {
465         (*p_cb->update_wl_cb)(*p, BTM_WHITELIST_REMOVE);
466     }
467 }
468 
469 /*******************************************************************************
470 **
471 ** Function         btm_ble_start_auto_conn
472 **
473 ** Description      This function is to start/stop auto connection procedure.
474 **
475 ** Parameters       start: TRUE to start; FALSE to stop.
476 **
477 ** Returns          void
478 **
479 *******************************************************************************/
btm_ble_start_auto_conn(BOOLEAN start)480 BOOLEAN btm_ble_start_auto_conn(BOOLEAN start)
481 {
482     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
483     BD_ADDR dummy_bda = {0};
484     BOOLEAN exec = TRUE;
485     UINT16 scan_int;
486     UINT16 scan_win;
487     UINT8 own_addr_type = p_cb->addr_mgnt_cb.own_addr_type;
488     UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
489 
490     if (start) {
491         if (p_cb->conn_state == BLE_CONN_IDLE && background_connections_pending()
492                 && btm_ble_topology_check(BTM_BLE_STATE_INIT)) {
493             p_cb->wl_state  |= BTM_BLE_WL_INIT;
494 
495             btm_execute_wl_dev_operation();
496 
497 #if BLE_PRIVACY_SPT == TRUE
498             btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_INIT);
499 #endif
500             scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ?
501                        BTM_BLE_SCAN_SLOW_INT_1 : p_cb->scan_int;
502             scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ?
503                        BTM_BLE_SCAN_SLOW_WIN_1 : p_cb->scan_win;
504 
505 #if BLE_PRIVACY_SPT == TRUE
506             if (btm_cb.ble_ctr_cb.rl_state != BTM_BLE_RL_IDLE
507                     && controller_get_interface()->supports_ble_privacy()) {
508                 own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
509                 peer_addr_type |= BLE_ADDR_TYPE_ID_BIT;
510             }
511 #endif
512 
513             if (!btsnd_hcic_ble_create_ll_conn (scan_int,  /* UINT16 scan_int      */
514                                                 scan_win,    /* UINT16 scan_win      */
515                                                 0x01,                   /* UINT8 white_list     */
516                                                 peer_addr_type,        /* UINT8 addr_type_peer */
517                                                 dummy_bda,              /* BD_ADDR bda_peer     */
518                                                 own_addr_type,          /* UINT8 addr_type_own */
519                                                 BTM_BLE_CONN_INT_MIN_DEF,   /* UINT16 conn_int_min  */
520                                                 BTM_BLE_CONN_INT_MAX_DEF,   /* UINT16 conn_int_max  */
521                                                 BTM_BLE_CONN_SLAVE_LATENCY_DEF,  /* UINT16 conn_latency  */
522                                                 BTM_BLE_CONN_TIMEOUT_DEF,        /* UINT16 conn_timeout  */
523                                                 0,                       /* UINT16 min_len       */
524                                                 0)) {                    /* UINT16 max_len       */
525                 /* start auto connection failed */
526                 exec =  FALSE;
527                 p_cb->wl_state &= ~BTM_BLE_WL_INIT;
528             } else {
529                 btm_ble_set_conn_st (BLE_BG_CONN);
530             }
531         } else {
532             exec = FALSE;
533         }
534     } else {
535         if (p_cb->conn_state == BLE_BG_CONN) {
536             btsnd_hcic_ble_create_conn_cancel();
537             btm_ble_set_conn_st (BLE_CONN_CANCEL);
538             p_cb->wl_state &= ~BTM_BLE_WL_INIT;
539         } else {
540             BTM_TRACE_DEBUG("conn_st = %d, not in auto conn state, cannot stop", p_cb->conn_state);
541             exec = FALSE;
542         }
543     }
544     return exec;
545 }
546 
547 /*******************************************************************************
548 **
549 ** Function         btm_ble_start_select_conn
550 **
551 ** Description      This function is to start/stop selective connection procedure.
552 **
553 ** Parameters       start: TRUE to start; FALSE to stop.
554 **                  p_select_cback: callback function to return application
555 **                                  selection.
556 **
557 ** Returns          BOOLEAN: selective connection procedure is started.
558 **
559 *******************************************************************************/
btm_ble_start_select_conn(BOOLEAN start,tBTM_BLE_SEL_CBACK * p_select_cback)560 BOOLEAN btm_ble_start_select_conn(BOOLEAN start, tBTM_BLE_SEL_CBACK *p_select_cback)
561 {
562     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
563     UINT32 scan_int = p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
564     UINT32 scan_win = p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
565 
566     BTM_TRACE_EVENT ("%s", __func__);
567 
568     if (start) {
569         if (!BTM_BLE_IS_SCAN_ACTIVE(p_cb->scan_activity)) {
570             if (p_select_cback != NULL) {
571                 btm_cb.ble_ctr_cb.p_select_cback = p_select_cback;
572             }
573 
574             btm_execute_wl_dev_operation();
575 
576             btm_update_scanner_filter_policy(SP_ADV_WL);
577             btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_PASS;
578 
579             /* Process advertising packets only from devices in the white list */
580             if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
581                 /* use passive scan by default */
582                 if (!btsnd_hcic_ble_set_scan_params(BTM_BLE_SCAN_MODE_PASS,
583                                                     scan_int,
584                                                     scan_win,
585                                                     p_cb->addr_mgnt_cb.own_addr_type,
586                                                     SP_ADV_WL)) {
587                     return FALSE;
588                 }
589             } else {
590                 if (!btm_ble_send_extended_scan_params(BTM_BLE_SCAN_MODE_PASS,
591                                                        scan_int,
592                                                        scan_win,
593                                                        p_cb->addr_mgnt_cb.own_addr_type,
594                                                        SP_ADV_WL)) {
595                     return FALSE;
596                 }
597             }
598 
599             if (!btm_ble_topology_check(BTM_BLE_STATE_PASSIVE_SCAN)) {
600                 BTM_TRACE_ERROR("peripheral device cannot initiate passive scan for a selective connection");
601                 return FALSE;
602             } else if (background_connections_pending()) {
603 #if BLE_PRIVACY_SPT == TRUE
604                 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
605 #endif
606                 if (!btsnd_hcic_ble_set_scan_enable(TRUE, TRUE)) { /* duplicate filtering enabled */
607                     return FALSE;
608                 }
609                 /* mark up inquiry status flag */
610                 p_cb->scan_activity |= BTM_LE_SELECT_CONN_ACTIVE;
611                 p_cb->wl_state |= BTM_BLE_WL_SCAN;
612             }
613         } else {
614             BTM_TRACE_ERROR("scan active, can not start selective connection procedure");
615             return FALSE;
616         }
617     } else { /* disable selective connection mode */
618         p_cb->scan_activity &= ~BTM_LE_SELECT_CONN_ACTIVE;
619         p_cb->p_select_cback = NULL;
620         p_cb->wl_state &= ~BTM_BLE_WL_SCAN;
621 
622         /* stop scanning */
623         if (!BTM_BLE_IS_SCAN_ACTIVE(p_cb->scan_activity)) {
624             btm_ble_stop_scan();    /* duplicate filtering enabled */
625         }
626     }
627     return TRUE;
628 }
629 /*******************************************************************************
630 **
631 ** Function         btm_ble_initiate_select_conn
632 **
633 ** Description      This function is to start/stop selective connection procedure.
634 **
635 ** Parameters       start: TRUE to start; FALSE to stop.
636 **                  p_select_cback: callback function to return application
637 **                                  selection.
638 **
639 ** Returns          BOOLEAN: selective connection procedure is started.
640 **
641 *******************************************************************************/
btm_ble_initiate_select_conn(BD_ADDR bda)642 void btm_ble_initiate_select_conn(BD_ADDR bda)
643 {
644     BTM_TRACE_EVENT ("btm_ble_initiate_select_conn");
645 
646     /* use direct connection procedure to initiate connection */
647     if (!L2CA_ConnectFixedChnl(L2CAP_ATT_CID, bda, BLE_ADDR_UNKNOWN_TYPE, FALSE)) {
648         BTM_TRACE_ERROR("btm_ble_initiate_select_conn failed");
649     }
650 }
651 /*******************************************************************************
652 **
653 ** Function         btm_ble_suspend_bg_conn
654 **
655 ** Description      This function is to suspend an active background connection
656 **                  procedure.
657 **
658 ** Parameters       none.
659 **
660 ** Returns          none.
661 **
662 *******************************************************************************/
btm_ble_suspend_bg_conn(void)663 BOOLEAN btm_ble_suspend_bg_conn(void)
664 {
665     BTM_TRACE_EVENT ("%s\n", __func__);
666 
667     if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO) {
668         return btm_ble_start_auto_conn(FALSE);
669     } else if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE) {
670         return btm_ble_start_select_conn(FALSE, NULL);
671     }
672 
673     return FALSE;
674 }
675 /*******************************************************************************
676 **
677 ** Function         btm_suspend_wl_activity
678 **
679 ** Description      This function is to suspend white list related activity
680 **
681 ** Returns          none.
682 **
683 *******************************************************************************/
btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state)684 static void btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state)
685 {
686     if (wl_state & BTM_BLE_WL_INIT) {
687         btm_ble_start_auto_conn(FALSE);
688     }
689     if (wl_state & BTM_BLE_WL_SCAN) {
690         btm_ble_start_select_conn(FALSE, NULL);
691     }
692     if (wl_state & BTM_BLE_WL_ADV) {
693         btm_ble_stop_adv();
694     }
695 
696 }
697 /*******************************************************************************
698 **
699 ** Function         btm_resume_wl_activity
700 **
701 ** Description      This function is to resume white list related activity
702 **
703 ** Returns          none.
704 **
705 *******************************************************************************/
btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state)706 void btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state)
707 {
708     btm_ble_resume_bg_conn();
709     if (wl_state & BTM_BLE_WL_ADV) {
710         btm_ble_start_adv();
711     }
712 
713 }
714 
715 /*******************************************************************************
716 **
717 ** Function         btm_wl_update_to_controller
718 **
719 ** Description      This function is to update white list to controller
720 **
721 ** Returns          none.
722 **
723 *******************************************************************************/
btm_wl_update_to_controller(void)724 static void btm_wl_update_to_controller(void)
725 {
726     /* whitelist will be added in the btm_ble_resume_bg_conn(), we do not
727        support background connection now, so we nedd to use btm_execute_wl_dev_operation
728        to add whitelist directly ,if we support background connection in the future,
729        please delete btm_execute_wl_dev_operation(). */
730     btm_execute_wl_dev_operation();
731 
732 }
733 /*******************************************************************************
734 **
735 ** Function         btm_ble_resume_bg_conn
736 **
737 ** Description      This function is to resume a background auto connection
738 **                  procedure.
739 **
740 ** Parameters       none.
741 **
742 ** Returns          none.
743 **
744 *******************************************************************************/
btm_ble_resume_bg_conn(void)745 BOOLEAN btm_ble_resume_bg_conn(void)
746 {
747     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
748     BOOLEAN ret = FALSE;
749 
750     if (p_cb->bg_conn_type != BTM_BLE_CONN_NONE) {
751         if (p_cb->bg_conn_type == BTM_BLE_CONN_AUTO) {
752             ret = btm_ble_start_auto_conn(TRUE);
753         }
754 
755         if (p_cb->bg_conn_type == BTM_BLE_CONN_SELECTIVE) {
756             ret = btm_ble_start_select_conn(TRUE, btm_cb.ble_ctr_cb.p_select_cback);
757         }
758     }
759 
760     return ret;
761 }
762 /*******************************************************************************
763 **
764 ** Function         btm_ble_get_conn_st
765 **
766 ** Description      This function get BLE connection state
767 **
768 ** Returns          connection state
769 **
770 *******************************************************************************/
btm_ble_get_conn_st(void)771 tBTM_BLE_CONN_ST btm_ble_get_conn_st(void)
772 {
773     return btm_cb.ble_ctr_cb.conn_state;
774 }
775 /*******************************************************************************
776 **
777 ** Function         btm_ble_set_conn_st
778 **
779 ** Description      This function set BLE connection state
780 **
781 ** Returns          None.
782 **
783 *******************************************************************************/
btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st)784 void btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st)
785 {
786     btm_cb.ble_ctr_cb.conn_state = new_st;
787 
788     if (new_st == BLE_BG_CONN || new_st == BLE_DIR_CONN) {
789         btm_ble_set_topology_mask(BTM_BLE_STATE_INIT_BIT);
790     } else {
791         btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
792     }
793 }
794 
795 /*******************************************************************************
796 **
797 ** Function         btm_ble_enqueue_direct_conn_req
798 **
799 ** Description      This function enqueue the direct connection request
800 **
801 ** Returns          None.
802 **
803 *******************************************************************************/
btm_ble_enqueue_direct_conn_req(void * p_param)804 void btm_ble_enqueue_direct_conn_req(void *p_param)
805 {
806     tBTM_BLE_CONN_REQ   *p = (tBTM_BLE_CONN_REQ *)osi_malloc(sizeof(tBTM_BLE_CONN_REQ));
807 
808     p->p_param = p_param;
809 
810     fixed_queue_enqueue(btm_cb.ble_ctr_cb.conn_pending_q, p, FIXED_QUEUE_MAX_TIMEOUT);
811 }
812 /*******************************************************************************
813 **
814 ** Function         btm_send_pending_direct_conn
815 **
816 ** Description      This function send the pending direct connection request in queue
817 **
818 ** Returns          TRUE if started, FALSE otherwise
819 **
820 *******************************************************************************/
btm_send_pending_direct_conn(void)821 BOOLEAN btm_send_pending_direct_conn(void)
822 {
823     tBTM_BLE_CONN_REQ *p_req;
824     BOOLEAN     rt = FALSE;
825 
826     p_req = (tBTM_BLE_CONN_REQ*)fixed_queue_dequeue(btm_cb.ble_ctr_cb.conn_pending_q, 0);
827     if (p_req != NULL) {
828         rt = l2cble_init_direct_conn((tL2C_LCB *)(p_req->p_param));
829 
830         osi_free((void *)p_req);
831     }
832 
833     return rt;
834 }
835 
836 #endif
837