btm_ble_bgconn.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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_UPDATE_WHITELIST_CBACK *update_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 (update_wl_cb){
  254. update_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 (update_wl_cb){
  284. update_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 (update_wl_cb){
  293. update_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 (update_wl_cb){
  302. update_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 (update_wl_cb){
  311. update_wl_cb(HCI_SUCCESS,to_add);
  312. }
  313. return TRUE;
  314. }
  315. }
  316. if (update_wl_cb){
  317. //save add whitelist complete callback
  318. p_cb->update_wl_cb = update_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 (tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
  336. {
  337. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  338. BTM_TRACE_EVENT ("btm_ble_clear_white_list");
  339. btsnd_hcic_ble_clear_white_list();
  340. background_connections_clear();
  341. if (update_wl_cb) {
  342. p_cb->update_wl_cb = update_wl_cb;
  343. }
  344. }
  345. /*******************************************************************************
  346. **
  347. ** Function btm_ble_clear_white_list_complete
  348. **
  349. ** Description Indicates white list cleared.
  350. **
  351. *******************************************************************************/
  352. void btm_ble_clear_white_list_complete(UINT8 *p_data, UINT16 evt_len)
  353. {
  354. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  355. UINT8 status;
  356. UNUSED(evt_len);
  357. BTM_TRACE_EVENT ("btm_ble_clear_white_list_complete");
  358. STREAM_TO_UINT8 (status, p_data);
  359. if (status == HCI_SUCCESS) {
  360. p_cb->white_list_avail_size = controller_get_interface()->get_ble_white_list_size();
  361. } else {
  362. BTM_TRACE_ERROR ("%s failed, status 0x%x\n", __func__, status);
  363. }
  364. if (p_cb->update_wl_cb) {
  365. (*p_cb->update_wl_cb)(status, BTM_WHITELIST_CLEAR);
  366. }
  367. }
  368. /*******************************************************************************
  369. **
  370. ** Function btm_ble_white_list_init
  371. **
  372. ** Description Initialize white list size
  373. **
  374. *******************************************************************************/
  375. void btm_ble_white_list_init(UINT8 white_list_size)
  376. {
  377. BTM_TRACE_DEBUG("%s white_list_size = %d", __func__, white_list_size);
  378. btm_cb.ble_ctr_cb.white_list_avail_size = white_list_size;
  379. }
  380. /*******************************************************************************
  381. **
  382. ** Function btm_ble_add_2_white_list_complete
  383. **
  384. ** Description White list element added
  385. **
  386. *******************************************************************************/
  387. void btm_ble_add_2_white_list_complete(UINT8 status)
  388. {
  389. BTM_TRACE_EVENT("%s status=%d", __func__, status);
  390. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  391. if (status == HCI_SUCCESS) {
  392. --btm_cb.ble_ctr_cb.white_list_avail_size;
  393. }
  394. // add whitelist complete callback
  395. if (p_cb->update_wl_cb)
  396. {
  397. (*p_cb->update_wl_cb)(status, BTM_WHITELIST_ADD);
  398. }
  399. }
  400. /*******************************************************************************
  401. **
  402. ** Function btm_ble_remove_from_white_list_complete
  403. **
  404. ** Description White list element removal complete
  405. **
  406. *******************************************************************************/
  407. void btm_ble_remove_from_white_list_complete(UINT8 *p, UINT16 evt_len)
  408. {
  409. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  410. UNUSED(evt_len);
  411. BTM_TRACE_EVENT ("%s status=%d", __func__, *p);
  412. if (*p == HCI_SUCCESS) {
  413. ++btm_cb.ble_ctr_cb.white_list_avail_size;
  414. }
  415. if (p_cb->update_wl_cb)
  416. {
  417. (*p_cb->update_wl_cb)(*p, BTM_WHITELIST_REMOVE);
  418. }
  419. }
  420. /*******************************************************************************
  421. **
  422. ** Function btm_ble_start_auto_conn
  423. **
  424. ** Description This function is to start/stop auto connection procedure.
  425. **
  426. ** Parameters start: TRUE to start; FALSE to stop.
  427. **
  428. ** Returns void
  429. **
  430. *******************************************************************************/
  431. BOOLEAN btm_ble_start_auto_conn(BOOLEAN start)
  432. {
  433. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  434. BD_ADDR dummy_bda = {0};
  435. BOOLEAN exec = TRUE;
  436. UINT16 scan_int;
  437. UINT16 scan_win;
  438. UINT8 own_addr_type = p_cb->addr_mgnt_cb.own_addr_type;
  439. UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
  440. if (start) {
  441. if (p_cb->conn_state == BLE_CONN_IDLE && background_connections_pending()
  442. && btm_ble_topology_check(BTM_BLE_STATE_INIT)) {
  443. p_cb->wl_state |= BTM_BLE_WL_INIT;
  444. btm_execute_wl_dev_operation();
  445. #if BLE_PRIVACY_SPT == TRUE
  446. btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_INIT);
  447. #endif
  448. scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ?
  449. BTM_BLE_SCAN_SLOW_INT_1 : p_cb->scan_int;
  450. scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ?
  451. BTM_BLE_SCAN_SLOW_WIN_1 : p_cb->scan_win;
  452. #if BLE_PRIVACY_SPT == TRUE
  453. if (btm_cb.ble_ctr_cb.rl_state != BTM_BLE_RL_IDLE
  454. && controller_get_interface()->supports_ble_privacy()) {
  455. own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
  456. peer_addr_type |= BLE_ADDR_TYPE_ID_BIT;
  457. }
  458. #endif
  459. if (!btsnd_hcic_ble_create_ll_conn (scan_int, /* UINT16 scan_int */
  460. scan_win, /* UINT16 scan_win */
  461. 0x01, /* UINT8 white_list */
  462. peer_addr_type, /* UINT8 addr_type_peer */
  463. dummy_bda, /* BD_ADDR bda_peer */
  464. own_addr_type, /* UINT8 addr_type_own */
  465. BTM_BLE_CONN_INT_MIN_DEF, /* UINT16 conn_int_min */
  466. BTM_BLE_CONN_INT_MAX_DEF, /* UINT16 conn_int_max */
  467. BTM_BLE_CONN_SLAVE_LATENCY_DEF, /* UINT16 conn_latency */
  468. BTM_BLE_CONN_TIMEOUT_DEF, /* UINT16 conn_timeout */
  469. 0, /* UINT16 min_len */
  470. 0)) { /* UINT16 max_len */
  471. /* start auto connection failed */
  472. exec = FALSE;
  473. p_cb->wl_state &= ~BTM_BLE_WL_INIT;
  474. } else {
  475. btm_ble_set_conn_st (BLE_BG_CONN);
  476. }
  477. } else {
  478. exec = FALSE;
  479. }
  480. } else {
  481. if (p_cb->conn_state == BLE_BG_CONN) {
  482. btsnd_hcic_ble_create_conn_cancel();
  483. btm_ble_set_conn_st (BLE_CONN_CANCEL);
  484. p_cb->wl_state &= ~BTM_BLE_WL_INIT;
  485. } else {
  486. BTM_TRACE_DEBUG("conn_st = %d, not in auto conn state, cannot stop", p_cb->conn_state);
  487. exec = FALSE;
  488. }
  489. }
  490. return exec;
  491. }
  492. /*******************************************************************************
  493. **
  494. ** Function btm_ble_start_select_conn
  495. **
  496. ** Description This function is to start/stop selective connection procedure.
  497. **
  498. ** Parameters start: TRUE to start; FALSE to stop.
  499. ** p_select_cback: callback function to return application
  500. ** selection.
  501. **
  502. ** Returns BOOLEAN: selective connection procedure is started.
  503. **
  504. *******************************************************************************/
  505. BOOLEAN btm_ble_start_select_conn(BOOLEAN start, tBTM_BLE_SEL_CBACK *p_select_cback)
  506. {
  507. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  508. UINT32 scan_int = p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
  509. UINT32 scan_win = p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
  510. BTM_TRACE_EVENT ("%s", __func__);
  511. if (start) {
  512. if (!BTM_BLE_IS_SCAN_ACTIVE(p_cb->scan_activity)) {
  513. if (p_select_cback != NULL) {
  514. btm_cb.ble_ctr_cb.p_select_cback = p_select_cback;
  515. }
  516. btm_execute_wl_dev_operation();
  517. btm_update_scanner_filter_policy(SP_ADV_WL);
  518. btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_PASS;
  519. /* Process advertising packets only from devices in the white list */
  520. if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
  521. /* use passive scan by default */
  522. if (!btsnd_hcic_ble_set_scan_params(BTM_BLE_SCAN_MODE_PASS,
  523. scan_int,
  524. scan_win,
  525. p_cb->addr_mgnt_cb.own_addr_type,
  526. SP_ADV_WL)) {
  527. return FALSE;
  528. }
  529. } else {
  530. if (!btm_ble_send_extended_scan_params(BTM_BLE_SCAN_MODE_PASS,
  531. scan_int,
  532. scan_win,
  533. p_cb->addr_mgnt_cb.own_addr_type,
  534. SP_ADV_WL)) {
  535. return FALSE;
  536. }
  537. }
  538. if (!btm_ble_topology_check(BTM_BLE_STATE_PASSIVE_SCAN)) {
  539. BTM_TRACE_ERROR("peripheral device cannot initiate passive scan for a selective connection");
  540. return FALSE;
  541. } else if (background_connections_pending()) {
  542. #if BLE_PRIVACY_SPT == TRUE
  543. btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
  544. #endif
  545. if (!btsnd_hcic_ble_set_scan_enable(TRUE, TRUE)) { /* duplicate filtering enabled */
  546. return FALSE;
  547. }
  548. /* mark up inquiry status flag */
  549. p_cb->scan_activity |= BTM_LE_SELECT_CONN_ACTIVE;
  550. p_cb->wl_state |= BTM_BLE_WL_SCAN;
  551. }
  552. } else {
  553. BTM_TRACE_ERROR("scan active, can not start selective connection procedure");
  554. return FALSE;
  555. }
  556. } else { /* disable selective connection mode */
  557. p_cb->scan_activity &= ~BTM_LE_SELECT_CONN_ACTIVE;
  558. p_cb->p_select_cback = NULL;
  559. p_cb->wl_state &= ~BTM_BLE_WL_SCAN;
  560. /* stop scanning */
  561. if (!BTM_BLE_IS_SCAN_ACTIVE(p_cb->scan_activity)) {
  562. btm_ble_stop_scan(); /* duplicate filtering enabled */
  563. }
  564. }
  565. return TRUE;
  566. }
  567. /*******************************************************************************
  568. **
  569. ** Function btm_ble_initiate_select_conn
  570. **
  571. ** Description This function is to start/stop selective connection procedure.
  572. **
  573. ** Parameters start: TRUE to start; FALSE to stop.
  574. ** p_select_cback: callback function to return application
  575. ** selection.
  576. **
  577. ** Returns BOOLEAN: selective connection procedure is started.
  578. **
  579. *******************************************************************************/
  580. void btm_ble_initiate_select_conn(BD_ADDR bda)
  581. {
  582. BTM_TRACE_EVENT ("btm_ble_initiate_select_conn");
  583. /* use direct connection procedure to initiate connection */
  584. if (!L2CA_ConnectFixedChnl(L2CAP_ATT_CID, bda, BLE_ADDR_UNKNOWN_TYPE, FALSE)) {
  585. BTM_TRACE_ERROR("btm_ble_initiate_select_conn failed");
  586. }
  587. }
  588. /*******************************************************************************
  589. **
  590. ** Function btm_ble_suspend_bg_conn
  591. **
  592. ** Description This function is to suspend an active background connection
  593. ** procedure.
  594. **
  595. ** Parameters none.
  596. **
  597. ** Returns none.
  598. **
  599. *******************************************************************************/
  600. BOOLEAN btm_ble_suspend_bg_conn(void)
  601. {
  602. BTM_TRACE_EVENT ("%s\n", __func__);
  603. if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO) {
  604. return btm_ble_start_auto_conn(FALSE);
  605. } else if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE) {
  606. return btm_ble_start_select_conn(FALSE, NULL);
  607. }
  608. return FALSE;
  609. }
  610. /*******************************************************************************
  611. **
  612. ** Function btm_suspend_wl_activity
  613. **
  614. ** Description This function is to suspend white list related activity
  615. **
  616. ** Returns none.
  617. **
  618. *******************************************************************************/
  619. static void btm_suspend_wl_activity(tBTM_BLE_WL_STATE wl_state)
  620. {
  621. if (wl_state & BTM_BLE_WL_INIT) {
  622. btm_ble_start_auto_conn(FALSE);
  623. }
  624. if (wl_state & BTM_BLE_WL_SCAN) {
  625. btm_ble_start_select_conn(FALSE, NULL);
  626. }
  627. if (wl_state & BTM_BLE_WL_ADV) {
  628. btm_ble_stop_adv();
  629. }
  630. }
  631. /*******************************************************************************
  632. **
  633. ** Function btm_resume_wl_activity
  634. **
  635. ** Description This function is to resume white list related activity
  636. **
  637. ** Returns none.
  638. **
  639. *******************************************************************************/
  640. void btm_resume_wl_activity(tBTM_BLE_WL_STATE wl_state)
  641. {
  642. btm_ble_resume_bg_conn();
  643. if (wl_state & BTM_BLE_WL_ADV) {
  644. btm_ble_start_adv();
  645. }
  646. }
  647. /*******************************************************************************
  648. **
  649. ** Function btm_wl_update_to_controller
  650. **
  651. ** Description This function is to update white list to controller
  652. **
  653. ** Returns none.
  654. **
  655. *******************************************************************************/
  656. static void btm_wl_update_to_controller(void)
  657. {
  658. /* whitelist will be added in the btm_ble_resume_bg_conn(), we do not
  659. support background connection now, so we nedd to use btm_execute_wl_dev_operation
  660. to add whitelist directly ,if we support background connection in the future,
  661. please delete btm_execute_wl_dev_operation(). */
  662. btm_execute_wl_dev_operation();
  663. }
  664. /*******************************************************************************
  665. **
  666. ** Function btm_ble_resume_bg_conn
  667. **
  668. ** Description This function is to resume a background auto connection
  669. ** procedure.
  670. **
  671. ** Parameters none.
  672. **
  673. ** Returns none.
  674. **
  675. *******************************************************************************/
  676. BOOLEAN btm_ble_resume_bg_conn(void)
  677. {
  678. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  679. BOOLEAN ret = FALSE;
  680. if (p_cb->bg_conn_type != BTM_BLE_CONN_NONE) {
  681. if (p_cb->bg_conn_type == BTM_BLE_CONN_AUTO) {
  682. ret = btm_ble_start_auto_conn(TRUE);
  683. }
  684. if (p_cb->bg_conn_type == BTM_BLE_CONN_SELECTIVE) {
  685. ret = btm_ble_start_select_conn(TRUE, btm_cb.ble_ctr_cb.p_select_cback);
  686. }
  687. }
  688. return ret;
  689. }
  690. /*******************************************************************************
  691. **
  692. ** Function btm_ble_get_conn_st
  693. **
  694. ** Description This function get BLE connection state
  695. **
  696. ** Returns connection state
  697. **
  698. *******************************************************************************/
  699. tBTM_BLE_CONN_ST btm_ble_get_conn_st(void)
  700. {
  701. return btm_cb.ble_ctr_cb.conn_state;
  702. }
  703. /*******************************************************************************
  704. **
  705. ** Function btm_ble_set_conn_st
  706. **
  707. ** Description This function set BLE connection state
  708. **
  709. ** Returns None.
  710. **
  711. *******************************************************************************/
  712. void btm_ble_set_conn_st(tBTM_BLE_CONN_ST new_st)
  713. {
  714. btm_cb.ble_ctr_cb.conn_state = new_st;
  715. if (new_st == BLE_BG_CONN || new_st == BLE_DIR_CONN) {
  716. btm_ble_set_topology_mask(BTM_BLE_STATE_INIT_BIT);
  717. } else {
  718. btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
  719. }
  720. }
  721. /*******************************************************************************
  722. **
  723. ** Function btm_ble_enqueue_direct_conn_req
  724. **
  725. ** Description This function enqueue the direct connection request
  726. **
  727. ** Returns None.
  728. **
  729. *******************************************************************************/
  730. void btm_ble_enqueue_direct_conn_req(void *p_param)
  731. {
  732. tBTM_BLE_CONN_REQ *p = (tBTM_BLE_CONN_REQ *)osi_malloc(sizeof(tBTM_BLE_CONN_REQ));
  733. p->p_param = p_param;
  734. fixed_queue_enqueue(btm_cb.ble_ctr_cb.conn_pending_q, p, FIXED_QUEUE_MAX_TIMEOUT);
  735. }
  736. /*******************************************************************************
  737. **
  738. ** Function btm_send_pending_direct_conn
  739. **
  740. ** Description This function send the pending direct connection request in queue
  741. **
  742. ** Returns TRUE if started, FALSE otherwise
  743. **
  744. *******************************************************************************/
  745. BOOLEAN btm_send_pending_direct_conn(void)
  746. {
  747. tBTM_BLE_CONN_REQ *p_req;
  748. BOOLEAN rt = FALSE;
  749. p_req = (tBTM_BLE_CONN_REQ*)fixed_queue_dequeue(btm_cb.ble_ctr_cb.conn_pending_q, 0);
  750. if (p_req != NULL) {
  751. rt = l2cble_init_direct_conn((tL2C_LCB *)(p_req->p_param));
  752. osi_free((void *)p_req);
  753. }
  754. return rt;
  755. }
  756. #endif