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 <base/logging.h>
26 #include <string.h>
27 #include <unordered_map>
28 
29 #include "bt_types.h"
30 #include "bt_utils.h"
31 #include "btm_int.h"
32 #include "btu.h"
33 #include "device/include/controller.h"
34 #include "hcimsgs.h"
35 #include "l2c_int.h"
36 #include "osi/include/allocator.h"
37 #include "osi/include/osi.h"
38 
39 #ifndef BTM_BLE_SCAN_PARAM_TOUT
40 #define BTM_BLE_SCAN_PARAM_TOUT 50 /* 50 seconds */
41 #endif
42 
43 static void btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state);
44 static void btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state);
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 typedef struct background_connection_t {
52   bt_bdaddr_t address;
53 } background_connection_t;
54 
55 struct KeyEqual {
operator ()KeyEqual56   bool operator()(const bt_bdaddr_t* x, const bt_bdaddr_t* y) const {
57     return bdaddr_equals(x, y);
58   }
59 };
60 
61 static std::unordered_map<bt_bdaddr_t*, background_connection_t*,
62                           std::hash<bt_bdaddr_t*>, KeyEqual>
63     background_connections;
64 
background_connection_add(bt_bdaddr_t * address)65 static void background_connection_add(bt_bdaddr_t* address) {
66   CHECK(address);
67 
68   auto map_iter = background_connections.find(address);
69   if (map_iter == background_connections.end()) {
70     background_connection_t* connection =
71         (background_connection_t*)osi_calloc(sizeof(background_connection_t));
72     connection->address = *address;
73     background_connections[&(connection->address)] = connection;
74   }
75 }
76 
background_connection_remove(bt_bdaddr_t * address)77 static void background_connection_remove(bt_bdaddr_t* address) {
78   background_connections.erase(address);
79 }
80 
background_connections_clear()81 static void background_connections_clear() { background_connections.clear(); }
82 
background_connections_pending()83 static bool background_connections_pending() {
84   for (const auto& map_el : background_connections) {
85     background_connection_t* connection = map_el.second;
86     const bool connected =
87         BTM_IsAclConnectionUp(connection->address.address, BT_TRANSPORT_LE);
88     if (!connected) {
89       return true;
90     }
91   }
92   return false;
93 }
94 
95 /*******************************************************************************
96  *
97  * Function         btm_update_scanner_filter_policy
98  *
99  * Description      This function updates the filter policy of scanner
100  ******************************************************************************/
btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy)101 void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy) {
102   tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
103 
104   uint32_t scan_interval =
105       !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
106   uint32_t scan_window =
107       !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
108 
109   BTM_TRACE_EVENT("%s", __func__);
110 
111   p_inq->sfp = scan_policy;
112   p_inq->scan_type = p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE
113                          ? BTM_BLE_SCAN_MODE_ACTI
114                          : p_inq->scan_type;
115 
116   btm_send_hci_set_scan_params(
117       p_inq->scan_type, (uint16_t)scan_interval, (uint16_t)scan_window,
118       btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, scan_policy);
119 }
120 /*******************************************************************************
121  *
122  * Function         btm_add_dev_to_controller
123  *
124  * Description      This function load the device into controller white list
125  ******************************************************************************/
btm_add_dev_to_controller(bool to_add,BD_ADDR bd_addr)126 bool btm_add_dev_to_controller(bool to_add, BD_ADDR bd_addr) {
127   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
128   bool started = false;
129   BD_ADDR dummy_bda = {0};
130 
131   if (p_dev_rec != NULL && p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) {
132     if (to_add) {
133       if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC ||
134           !BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
135         btsnd_hcic_ble_add_white_list(p_dev_rec->ble.ble_addr_type, bd_addr);
136         started = true;
137         p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
138       } else if (memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) !=
139                      0 &&
140                  memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) !=
141                      0) {
142         btsnd_hcic_ble_add_white_list(p_dev_rec->ble.static_addr_type,
143                                       p_dev_rec->ble.static_addr);
144         started = true;
145         p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
146       }
147     } else {
148       if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC ||
149           !BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
150         btsnd_hcic_ble_remove_from_white_list(p_dev_rec->ble.ble_addr_type,
151                                               bd_addr);
152         started = true;
153       }
154 
155       if (memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0 &&
156           memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0) {
157         btsnd_hcic_ble_remove_from_white_list(p_dev_rec->ble.static_addr_type,
158                                               p_dev_rec->ble.static_addr);
159         started = true;
160       }
161 
162       p_dev_rec->ble.in_controller_list &= ~BTM_WHITE_LIST_BIT;
163     }
164   } else {
165     /* not a known device, i.e. attempt to connect to device never seen before
166      */
167     uint8_t addr_type =
168         BTM_IS_PUBLIC_BDA(bd_addr) ? BLE_ADDR_PUBLIC : BLE_ADDR_RANDOM;
169     btsnd_hcic_ble_remove_from_white_list(addr_type, bd_addr);
170     started = true;
171     if (to_add) btsnd_hcic_ble_add_white_list(addr_type, bd_addr);
172   }
173 
174   return started;
175 }
176 /*******************************************************************************
177  *
178  * Function         btm_execute_wl_dev_operation
179  *
180  * Description      execute the pending whitelist device operation (loading or
181  *                                                                  removing)
182  ******************************************************************************/
btm_execute_wl_dev_operation(void)183 bool btm_execute_wl_dev_operation(void) {
184   tBTM_BLE_WL_OP* p_dev_op = btm_cb.ble_ctr_cb.wl_op_q;
185   uint8_t i = 0;
186   bool rt = true;
187 
188   for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM && rt; i++, p_dev_op++) {
189     if (p_dev_op->in_use) {
190       rt = btm_add_dev_to_controller(p_dev_op->to_add, p_dev_op->bd_addr);
191       memset(p_dev_op, 0, sizeof(tBTM_BLE_WL_OP));
192     } else
193       break;
194   }
195   return rt;
196 }
197 /*******************************************************************************
198  *
199  * Function         btm_enq_wl_dev_operation
200  *
201  * Description      enqueue the pending whitelist device operation (loading or
202  *                                                                  removing).
203  ******************************************************************************/
btm_enq_wl_dev_operation(bool to_add,BD_ADDR bd_addr)204 void btm_enq_wl_dev_operation(bool to_add, BD_ADDR bd_addr) {
205   tBTM_BLE_WL_OP* p_dev_op = btm_cb.ble_ctr_cb.wl_op_q;
206   uint8_t i = 0;
207 
208   for (i = 0; i < BTM_BLE_MAX_BG_CONN_DEV_NUM; i++, p_dev_op++) {
209     if (p_dev_op->in_use && !memcmp(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN)) {
210       p_dev_op->to_add = to_add;
211       return;
212     } else if (!p_dev_op->in_use)
213       break;
214   }
215   if (i != BTM_BLE_MAX_BG_CONN_DEV_NUM) {
216     p_dev_op->in_use = true;
217     p_dev_op->to_add = to_add;
218     memcpy(p_dev_op->bd_addr, bd_addr, BD_ADDR_LEN);
219   } else {
220     BTM_TRACE_ERROR("max pending WL operation reached, discard");
221   }
222   return;
223 }
224 
225 /*******************************************************************************
226  *
227  * Function         btm_update_dev_to_white_list
228  *
229  * Description      This function adds or removes a device into/from
230  *                  the white list.
231  *
232  ******************************************************************************/
btm_update_dev_to_white_list(bool to_add,BD_ADDR bd_addr)233 bool btm_update_dev_to_white_list(bool to_add, BD_ADDR bd_addr) {
234   tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
235 
236   if (to_add && p_cb->white_list_avail_size == 0) {
237     BTM_TRACE_ERROR("%s Whitelist full, unable to add device", __func__);
238     return false;
239   }
240 
241   if (to_add)
242     background_connection_add((bt_bdaddr_t*)bd_addr);
243   else
244     background_connection_remove((bt_bdaddr_t*)bd_addr);
245 
246   btm_suspend_wl_activity(p_cb->wl_state);
247   btm_enq_wl_dev_operation(to_add, bd_addr);
248   btm_resume_wl_activity(p_cb->wl_state);
249   return true;
250 }
251 
252 /*******************************************************************************
253  *
254  * Function         btm_ble_clear_white_list
255  *
256  * Description      This function clears the white list.
257  *
258  ******************************************************************************/
btm_ble_clear_white_list(void)259 void btm_ble_clear_white_list(void) {
260   BTM_TRACE_EVENT("btm_ble_clear_white_list");
261   btsnd_hcic_ble_clear_white_list();
262   background_connections_clear();
263 }
264 
265 /*******************************************************************************
266  *
267  * Function         btm_ble_clear_white_list_complete
268  *
269  * Description      Indicates white list cleared.
270  *
271  ******************************************************************************/
btm_ble_clear_white_list_complete(uint8_t * p_data,UNUSED_ATTR uint16_t evt_len)272 void btm_ble_clear_white_list_complete(uint8_t* p_data,
273                                        UNUSED_ATTR uint16_t evt_len) {
274   tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
275   uint8_t status;
276 
277   BTM_TRACE_EVENT("btm_ble_clear_white_list_complete");
278   STREAM_TO_UINT8(status, p_data);
279 
280   if (status == HCI_SUCCESS)
281     p_cb->white_list_avail_size =
282         controller_get_interface()->get_ble_white_list_size();
283 }
284 
285 /*******************************************************************************
286  *
287  * Function         btm_ble_white_list_init
288  *
289  * Description      Initialize white list size
290  *
291  ******************************************************************************/
btm_ble_white_list_init(uint8_t white_list_size)292 void btm_ble_white_list_init(uint8_t white_list_size) {
293   BTM_TRACE_DEBUG("%s white_list_size = %d", __func__, white_list_size);
294   btm_cb.ble_ctr_cb.white_list_avail_size = white_list_size;
295 }
296 
297 /*******************************************************************************
298  *
299  * Function         btm_ble_add_2_white_list_complete
300  *
301  * Description      White list element added
302  *
303  ******************************************************************************/
btm_ble_add_2_white_list_complete(uint8_t status)304 void btm_ble_add_2_white_list_complete(uint8_t status) {
305   BTM_TRACE_EVENT("%s status=%d", __func__, status);
306   if (status == HCI_SUCCESS) --btm_cb.ble_ctr_cb.white_list_avail_size;
307 }
308 
309 /*******************************************************************************
310  *
311  * Function         btm_ble_remove_from_white_list_complete
312  *
313  * Description      White list element removal complete
314  *
315  ******************************************************************************/
btm_ble_remove_from_white_list_complete(uint8_t * p,UNUSED_ATTR uint16_t evt_len)316 void btm_ble_remove_from_white_list_complete(uint8_t* p,
317                                              UNUSED_ATTR uint16_t evt_len) {
318   BTM_TRACE_EVENT("%s status=%d", __func__, *p);
319   if (*p == HCI_SUCCESS) ++btm_cb.ble_ctr_cb.white_list_avail_size;
320 }
321 
btm_send_hci_create_connection(uint16_t scan_int,uint16_t scan_win,uint8_t init_filter_policy,uint8_t addr_type_peer,BD_ADDR bda_peer,uint8_t addr_type_own,uint16_t conn_int_min,uint16_t conn_int_max,uint16_t conn_latency,uint16_t conn_timeout,uint16_t min_ce_len,uint16_t max_ce_len,uint8_t initiating_phys)322 void btm_send_hci_create_connection(
323     uint16_t scan_int, uint16_t scan_win, uint8_t init_filter_policy,
324     uint8_t addr_type_peer, BD_ADDR bda_peer, uint8_t addr_type_own,
325     uint16_t conn_int_min, uint16_t conn_int_max, uint16_t conn_latency,
326     uint16_t conn_timeout, uint16_t min_ce_len, uint16_t max_ce_len,
327     uint8_t initiating_phys) {
328   if (controller_get_interface()->supports_ble_extended_advertising()) {
329     EXT_CONN_PHY_CFG phy_cfg[3];  // maximum three phys
330 
331     int phy_cnt =
332         std::bitset<std::numeric_limits<uint8_t>::digits>(initiating_phys)
333             .count();
334 
335     LOG_ASSERT(phy_cnt < 3) << "More than three phys provided";
336     // TODO(jpawlowski): tune parameters for different transports
337     for (int i = 0; i < phy_cnt; i++) {
338       phy_cfg[i].scan_int = scan_int;
339       phy_cfg[i].scan_win = scan_win;
340       phy_cfg[i].conn_int_min = conn_int_min;
341       phy_cfg[i].conn_int_max = conn_int_max;
342       phy_cfg[i].conn_latency = conn_latency;
343       phy_cfg[i].sup_timeout = conn_timeout;
344       phy_cfg[i].min_ce_len = min_ce_len;
345       phy_cfg[i].max_ce_len = max_ce_len;
346     }
347 
348     addr_type_peer &= ~BLE_ADDR_TYPE_ID_BIT;
349     btsnd_hcic_ble_ext_create_conn(init_filter_policy, addr_type_own,
350                                    addr_type_peer, bda_peer, initiating_phys,
351                                    phy_cfg);
352   } else {
353     btsnd_hcic_ble_create_ll_conn(scan_int, scan_win, init_filter_policy,
354                                   addr_type_peer, bda_peer, addr_type_own,
355                                   conn_int_min, conn_int_max, conn_latency,
356                                   conn_timeout, min_ce_len, max_ce_len);
357   }
358 }
359 
360 /*******************************************************************************
361  *
362  * Function         btm_ble_start_auto_conn
363  *
364  * Description      This function is to start/stop auto connection procedure.
365  *
366  * Parameters       start: true to start; false to stop.
367  *
368  * Returns          void
369  *
370  ******************************************************************************/
btm_ble_start_auto_conn(bool start)371 bool btm_ble_start_auto_conn(bool start) {
372   tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
373   BD_ADDR dummy_bda = {0};
374   bool exec = true;
375   uint16_t scan_int;
376   uint16_t scan_win;
377   uint8_t own_addr_type = p_cb->addr_mgnt_cb.own_addr_type;
378   uint8_t peer_addr_type = BLE_ADDR_PUBLIC;
379 
380   uint8_t phy = PHY_LE_1M;
381   if (controller_get_interface()->supports_ble_2m_phy()) phy |= PHY_LE_2M;
382   if (controller_get_interface()->supports_ble_coded_phy()) phy |= PHY_LE_CODED;
383 
384   if (start) {
385     if (p_cb->conn_state == BLE_CONN_IDLE && background_connections_pending() &&
386         btm_ble_topology_check(BTM_BLE_STATE_INIT)) {
387       p_cb->wl_state |= BTM_BLE_WL_INIT;
388 
389       btm_execute_wl_dev_operation();
390 
391 #if (BLE_PRIVACY_SPT == TRUE)
392       btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_INIT);
393 #endif
394       scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF)
395                      ? BTM_BLE_SCAN_SLOW_INT_1
396                      : p_cb->scan_int;
397       scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF)
398                      ? BTM_BLE_SCAN_SLOW_WIN_1
399                      : p_cb->scan_win;
400 
401 #if (BLE_PRIVACY_SPT == TRUE)
402       if (btm_cb.ble_ctr_cb.rl_state != BTM_BLE_RL_IDLE &&
403           controller_get_interface()->supports_ble_privacy()) {
404         own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
405         peer_addr_type |= BLE_ADDR_TYPE_ID_BIT;
406       }
407 #endif
408 
409       btm_send_hci_create_connection(
410           scan_int,                       /* uint16_t scan_int      */
411           scan_win,                       /* uint16_t scan_win      */
412           0x01,                           /* uint8_t white_list     */
413           peer_addr_type,                 /* uint8_t addr_type_peer */
414           dummy_bda,                      /* BD_ADDR bda_peer     */
415           own_addr_type,                  /* uint8_t addr_type_own */
416           BTM_BLE_CONN_INT_MIN_DEF,       /* uint16_t conn_int_min  */
417           BTM_BLE_CONN_INT_MAX_DEF,       /* uint16_t conn_int_max  */
418           BTM_BLE_CONN_SLAVE_LATENCY_DEF, /* uint16_t conn_latency  */
419           BTM_BLE_CONN_TIMEOUT_DEF,       /* uint16_t conn_timeout  */
420           0,                              /* uint16_t min_len       */
421           0,                              /* uint16_t max_len       */
422           phy);
423       btm_ble_set_conn_st(BLE_BG_CONN);
424     } else {
425       exec = false;
426     }
427   } else {
428     if (p_cb->conn_state == BLE_BG_CONN) {
429       btsnd_hcic_ble_create_conn_cancel();
430       btm_ble_set_conn_st(BLE_CONN_CANCEL);
431       p_cb->wl_state &= ~BTM_BLE_WL_INIT;
432     } else {
433       BTM_TRACE_DEBUG("conn_st = %d, not in auto conn state, cannot stop",
434                       p_cb->conn_state);
435       exec = false;
436     }
437   }
438   return exec;
439 }
440 
441 /*******************************************************************************
442  *
443  * Function         btm_ble_suspend_bg_conn
444  *
445  * Description      This function is to suspend an active background connection
446  *                  procedure.
447  *
448  * Parameters       none.
449  *
450  * Returns          none.
451  *
452  ******************************************************************************/
btm_ble_suspend_bg_conn(void)453 bool btm_ble_suspend_bg_conn(void) {
454   BTM_TRACE_EVENT("%s", __func__);
455 
456   if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO)
457     return btm_ble_start_auto_conn(false);
458 
459   return false;
460 }
461 /*******************************************************************************
462  *
463  * Function         btm_suspend_wl_activity
464  *
465  * Description      This function is to suspend white list related activity
466  *
467  * Returns          none.
468  *
469  ******************************************************************************/
btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state)470 static void btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state) {
471   if (wl_state & BTM_BLE_WL_INIT) {
472     btm_ble_start_auto_conn(false);
473   }
474   if (wl_state & BTM_BLE_WL_ADV) {
475     btm_ble_stop_adv();
476   }
477 }
478 /*******************************************************************************
479  *
480  * Function         btm_resume_wl_activity
481  *
482  * Description      This function is to resume white list related activity
483  *
484  * Returns          none.
485  *
486  ******************************************************************************/
btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state)487 static void btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state) {
488   btm_ble_resume_bg_conn();
489 
490   if (wl_state & BTM_BLE_WL_ADV) {
491     btm_ble_start_adv();
492   }
493 }
494 /*******************************************************************************
495  *
496  * Function         btm_ble_resume_bg_conn
497  *
498  * Description      This function is to resume a background auto connection
499  *                  procedure.
500  *
501  * Parameters       none.
502  *
503  * Returns          none.
504  *
505  ******************************************************************************/
btm_ble_resume_bg_conn(void)506 bool btm_ble_resume_bg_conn(void) {
507   tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
508   if (p_cb->bg_conn_type == BTM_BLE_CONN_AUTO) {
509     return btm_ble_start_auto_conn(true);
510   }
511 
512   return false;
513 }
514 /*******************************************************************************
515  *
516  * Function         btm_ble_get_conn_st
517  *
518  * Description      This function get BLE connection state
519  *
520  * Returns          connection state
521  *
522  ******************************************************************************/
btm_ble_get_conn_st(void)523 tBTM_BLE_CONN_ST btm_ble_get_conn_st(void) {
524   return btm_cb.ble_ctr_cb.conn_state;
525 }
526 /*******************************************************************************
527  *
528  * Function         btm_ble_set_conn_st
529  *
530  * Description      This function set BLE connection state
531  *
532  * Returns          None.
533  *
534  ******************************************************************************/
btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st)535 void btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st) {
536   btm_cb.ble_ctr_cb.conn_state = new_st;
537 
538   if (new_st == BLE_BG_CONN || new_st == BLE_DIR_CONN)
539     btm_ble_set_topology_mask(BTM_BLE_STATE_INIT_BIT);
540   else
541     btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
542 }
543 
544 /*******************************************************************************
545  *
546  * Function         btm_ble_enqueue_direct_conn_req
547  *
548  * Description      This function enqueue the direct connection request
549  *
550  * Returns          None.
551  *
552  ******************************************************************************/
btm_ble_enqueue_direct_conn_req(void * p_param)553 void btm_ble_enqueue_direct_conn_req(void* p_param) {
554   tBTM_BLE_CONN_REQ* p =
555       (tBTM_BLE_CONN_REQ*)osi_malloc(sizeof(tBTM_BLE_CONN_REQ));
556 
557   p->p_param = p_param;
558 
559   fixed_queue_enqueue(btm_cb.ble_ctr_cb.conn_pending_q, p);
560 }
561 /*******************************************************************************
562  *
563  * Function         btm_ble_dequeue_direct_conn_req
564  *
565  * Description      This function dequeues the direct connection request
566  *
567  * Returns          None.
568  *
569  ******************************************************************************/
btm_ble_dequeue_direct_conn_req(BD_ADDR rem_bda)570 void btm_ble_dequeue_direct_conn_req(BD_ADDR rem_bda) {
571   if (fixed_queue_is_empty(btm_cb.ble_ctr_cb.conn_pending_q)) return;
572 
573   list_t* list = fixed_queue_get_list(btm_cb.ble_ctr_cb.conn_pending_q);
574   for (const list_node_t* node = list_begin(list); node != list_end(list);
575        node = list_next(node)) {
576     tBTM_BLE_CONN_REQ* p_req = (tBTM_BLE_CONN_REQ*)list_node(node);
577     tL2C_LCB* p_lcb = (tL2C_LCB*)p_req->p_param;
578     if ((p_lcb == NULL) || (!p_lcb->in_use)) {
579       continue;
580     }
581     // If BD address matches
582     if (!memcmp(rem_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN)) {
583       fixed_queue_try_remove_from_queue(btm_cb.ble_ctr_cb.conn_pending_q,
584                                         p_req);
585       l2cu_release_lcb((tL2C_LCB*)p_req->p_param);
586       osi_free((void*)p_req);
587       break;
588     }
589   }
590 }
591 /*******************************************************************************
592  *
593  * Function         btm_send_pending_direct_conn
594  *
595  * Description      This function send the pending direct connection request in
596  *                  queue
597  *
598  * Returns          true if started, false otherwise
599  *
600  ******************************************************************************/
btm_send_pending_direct_conn(void)601 bool btm_send_pending_direct_conn(void) {
602   tBTM_BLE_CONN_REQ* p_req;
603   bool rt = false;
604 
605   p_req = (tBTM_BLE_CONN_REQ*)fixed_queue_try_dequeue(
606       btm_cb.ble_ctr_cb.conn_pending_q);
607   if (p_req != NULL) {
608     tL2C_LCB* p_lcb = (tL2C_LCB*)(p_req->p_param);
609     /* Ignore entries that might have been released while queued. */
610     if (p_lcb->in_use) rt = l2cble_init_direct_conn(p_lcb);
611     osi_free(p_req);
612   }
613 
614   return rt;
615 }
616