btm_ble_bgconn.c 30 KB

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