btm_ble_batchscan.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /******************************************************************************
  2. *
  3. * Copyright 2014 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. #include <base/bind.h>
  19. #include <stddef.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <vector>
  23. #include "bt_target.h"
  24. #include "bt_types.h"
  25. #include "bt_utils.h"
  26. #include "btm_ble_api.h"
  27. #include "btm_int.h"
  28. #include "btu.h"
  29. #include "device/include/controller.h"
  30. #include "hcimsgs.h"
  31. using base::Bind;
  32. using base::Callback;
  33. using hci_cmd_cb = base::Callback<void(uint8_t* /* return_parameters */,
  34. uint16_t /* return_parameters_length*/)>;
  35. tBTM_BLE_BATCH_SCAN_CB ble_batchscan_cb;
  36. tBTM_BLE_ADV_TRACK_CB ble_advtrack_cb;
  37. /* length of each batch scan command */
  38. #define BTM_BLE_BATCH_SCAN_STORAGE_CFG_LEN 4
  39. #define BTM_BLE_BATCH_SCAN_PARAM_CONFIG_LEN 12
  40. #define BTM_BLE_BATCH_SCAN_ENB_DISB_LEN 2
  41. #define BTM_BLE_BATCH_SCAN_READ_RESULTS_LEN 2
  42. namespace {
  43. bool can_do_batch_scan() {
  44. if (!controller_get_interface()->supports_ble()) return false;
  45. tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
  46. BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
  47. if (cmn_ble_vsc_cb.tot_scan_results_strg == 0) return false;
  48. return true;
  49. }
  50. /* VSE callback for batch scan, filter, and tracking events */
  51. void btm_ble_batchscan_filter_track_adv_vse_cback(uint8_t len, uint8_t* p) {
  52. tBTM_BLE_TRACK_ADV_DATA adv_data;
  53. uint8_t sub_event = 0;
  54. tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
  55. if (len == 0) return;
  56. STREAM_TO_UINT8(sub_event, p);
  57. BTM_TRACE_EVENT(
  58. "btm_ble_batchscan_filter_track_adv_vse_cback called with event:%x",
  59. sub_event);
  60. if (HCI_VSE_SUBCODE_BLE_THRESHOLD_SUB_EVT == sub_event &&
  61. NULL != ble_batchscan_cb.p_thres_cback) {
  62. ble_batchscan_cb.p_thres_cback(ble_batchscan_cb.ref_value);
  63. return;
  64. }
  65. if (HCI_VSE_SUBCODE_BLE_TRACKING_SUB_EVT == sub_event &&
  66. NULL != ble_advtrack_cb.p_track_cback) {
  67. if (len < 10) return;
  68. memset(&adv_data, 0, sizeof(tBTM_BLE_TRACK_ADV_DATA));
  69. BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
  70. adv_data.client_if = (uint8_t)ble_advtrack_cb.ref_value;
  71. if (cmn_ble_vsc_cb.version_supported > BTM_VSC_CHIP_CAPABILITY_L_VERSION) {
  72. STREAM_TO_UINT8(adv_data.filt_index, p);
  73. STREAM_TO_UINT8(adv_data.advertiser_state, p);
  74. STREAM_TO_UINT8(adv_data.advertiser_info_present, p);
  75. STREAM_TO_BDADDR(adv_data.bd_addr, p);
  76. STREAM_TO_UINT8(adv_data.addr_type, p);
  77. /* Extract the adv info details */
  78. if (ADV_INFO_PRESENT == adv_data.advertiser_info_present) {
  79. if (len < 15) return;
  80. STREAM_TO_UINT8(adv_data.tx_power, p);
  81. STREAM_TO_UINT8(adv_data.rssi_value, p);
  82. STREAM_TO_UINT16(adv_data.time_stamp, p);
  83. STREAM_TO_UINT8(adv_data.adv_pkt_len, p);
  84. if (adv_data.adv_pkt_len > 0) {
  85. adv_data.p_adv_pkt_data =
  86. static_cast<uint8_t*>(osi_malloc(adv_data.adv_pkt_len));
  87. memcpy(adv_data.p_adv_pkt_data, p, adv_data.adv_pkt_len);
  88. }
  89. STREAM_TO_UINT8(adv_data.scan_rsp_len, p);
  90. if (adv_data.scan_rsp_len > 0) {
  91. adv_data.p_scan_rsp_data =
  92. static_cast<uint8_t*>(osi_malloc(adv_data.scan_rsp_len));
  93. memcpy(adv_data.p_scan_rsp_data, p, adv_data.scan_rsp_len);
  94. }
  95. }
  96. } else {
  97. /* Based on L-release version */
  98. STREAM_TO_UINT8(adv_data.filt_index, p);
  99. STREAM_TO_UINT8(adv_data.addr_type, p);
  100. STREAM_TO_BDADDR(adv_data.bd_addr, p);
  101. STREAM_TO_UINT8(adv_data.advertiser_state, p);
  102. }
  103. BTM_TRACE_EVENT("track_adv_vse_cback called: %d, %d, %d",
  104. adv_data.filt_index, adv_data.addr_type,
  105. adv_data.advertiser_state);
  106. // Make sure the device is known
  107. BTM_SecAddBleDevice(adv_data.bd_addr, NULL, BT_DEVICE_TYPE_BLE,
  108. adv_data.addr_type);
  109. ble_advtrack_cb.p_track_cback(&adv_data);
  110. return;
  111. }
  112. }
  113. void feat_enable_cb(uint8_t* p, uint16_t len) {
  114. if (len < 2) {
  115. BTM_TRACE_ERROR("%s: wrong length", __func__);
  116. return;
  117. }
  118. uint8_t status, subcode;
  119. STREAM_TO_UINT8(status, p);
  120. STREAM_TO_UINT8(subcode, p);
  121. uint8_t expected_opcode = BTM_BLE_BATCH_SCAN_ENB_DISAB_CUST_FEATURE;
  122. if (subcode != expected_opcode) {
  123. BTM_TRACE_ERROR("%s: bad subcode, expected: %d got: %d", __func__,
  124. expected_opcode, subcode);
  125. return;
  126. }
  127. if (ble_batchscan_cb.cur_state != BTM_BLE_SCAN_ENABLE_CALLED)
  128. BTM_TRACE_ERROR("%s: state should be ENABLE_CALLED", __func__);
  129. ble_batchscan_cb.cur_state = BTM_BLE_SCAN_ENABLED_STATE;
  130. }
  131. void storage_config_cb(Callback<void(uint8_t /* status */)> cb, uint8_t* p,
  132. uint16_t len) {
  133. if (len < 2) {
  134. BTM_TRACE_ERROR("%s: wrong length", __func__);
  135. return;
  136. }
  137. uint8_t status, subcode;
  138. STREAM_TO_UINT8(status, p);
  139. STREAM_TO_UINT8(subcode, p);
  140. uint8_t expected_opcode = BTM_BLE_BATCH_SCAN_SET_STORAGE_PARAM;
  141. if (subcode != expected_opcode) {
  142. BTM_TRACE_ERROR("%s: bad subcode, expected: %d got: %d", __func__,
  143. expected_opcode, subcode);
  144. return;
  145. }
  146. cb.Run(status);
  147. }
  148. void param_enable_cb(Callback<void(uint8_t /* status */)> cb, uint8_t* p,
  149. uint16_t len) {
  150. if (len < 2) {
  151. BTM_TRACE_ERROR("%s: wrong length", __func__);
  152. return;
  153. }
  154. uint8_t status, subcode;
  155. STREAM_TO_UINT8(status, p);
  156. STREAM_TO_UINT8(subcode, p);
  157. uint8_t expected_opcode = BTM_BLE_BATCH_SCAN_SET_PARAMS;
  158. if (subcode != expected_opcode) {
  159. BTM_TRACE_ERROR("%s: bad subcode: 0x%02x 0x%02x", __func__, expected_opcode,
  160. subcode);
  161. return;
  162. }
  163. cb.Run(status);
  164. }
  165. void disable_cb(base::Callback<void(uint8_t /* status */)> cb, uint8_t* p,
  166. uint16_t len) {
  167. if (len < 2) {
  168. BTM_TRACE_ERROR("%s: wrong length", __func__);
  169. return;
  170. }
  171. uint8_t status, subcode;
  172. STREAM_TO_UINT8(status, p);
  173. STREAM_TO_UINT8(subcode, p);
  174. uint8_t expected_opcode = BTM_BLE_BATCH_SCAN_SET_PARAMS;
  175. if (subcode != expected_opcode) {
  176. BTM_TRACE_ERROR("%s: bad subcode: 0x%02x 0x%02x", __func__, expected_opcode,
  177. subcode);
  178. return;
  179. }
  180. if (ble_batchscan_cb.cur_state != BTM_BLE_SCAN_DISABLE_CALLED) {
  181. BTM_TRACE_ERROR("%s: state should be DISABLE_CALLED", __func__);
  182. }
  183. if (BTM_SUCCESS == status) {
  184. ble_batchscan_cb.cur_state = BTM_BLE_SCAN_DISABLED_STATE;
  185. } else {
  186. BTM_TRACE_ERROR("%s: Invalid state after disabled", __func__);
  187. ble_batchscan_cb.cur_state = BTM_BLE_SCAN_INVALID_STATE;
  188. }
  189. cb.Run(status);
  190. }
  191. /**
  192. * This function reads the reports from controller. |scan_mode| is the mode for
  193. * which the reports are to be read
  194. */
  195. void btm_ble_read_batchscan_reports(tBTM_BLE_BATCH_SCAN_MODE scan_mode,
  196. hci_cmd_cb cb) {
  197. uint8_t len = BTM_BLE_BATCH_SCAN_READ_RESULTS_LEN;
  198. uint8_t param[len];
  199. memset(param, 0, len);
  200. uint8_t* pp = param;
  201. UINT8_TO_STREAM(pp, BTM_BLE_BATCH_SCAN_READ_RESULTS);
  202. UINT8_TO_STREAM(pp, scan_mode);
  203. btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_BATCH_SCAN_OCF, param, len, cb);
  204. }
  205. /* read reports. data is accumulated in |data_all|, number of records is
  206. * accumulated in |num_records_all| */
  207. void read_reports_cb(std::vector<uint8_t> data_all, uint8_t num_records_all,
  208. tBTM_BLE_SCAN_REP_CBACK cb, uint8_t* p, uint16_t len) {
  209. if (len < 2) {
  210. BTM_TRACE_ERROR("%s: wrong length", __func__);
  211. return;
  212. }
  213. uint8_t status, subcode;
  214. STREAM_TO_UINT8(status, p);
  215. STREAM_TO_UINT8(subcode, p);
  216. uint8_t expected_opcode = BTM_BLE_BATCH_SCAN_READ_RESULTS;
  217. if (subcode != expected_opcode) {
  218. BTM_TRACE_ERROR("%s: bad subcode, expected: %d got: %d", __func__,
  219. expected_opcode, subcode);
  220. return;
  221. }
  222. uint8_t report_format, num_records;
  223. STREAM_TO_UINT8(report_format, p);
  224. STREAM_TO_UINT8(num_records, p);
  225. BTM_TRACE_DEBUG("%s: status=%d,len=%d,rec=%d", __func__, status, len - 4,
  226. num_records);
  227. if (num_records == 0) {
  228. cb.Run(status, report_format, num_records_all, data_all);
  229. return;
  230. }
  231. if (len > 4) {
  232. data_all.insert(data_all.end(), p, p + len - 4);
  233. num_records_all += num_records;
  234. /* More records could be in the buffer and needs to be pulled out */
  235. btm_ble_read_batchscan_reports(
  236. report_format, base::Bind(&read_reports_cb, std::move(data_all),
  237. num_records_all, std::move(cb)));
  238. }
  239. }
  240. /**
  241. * This function writes the storage configuration in controller
  242. *
  243. * Parameters batch_scan_full_max - Max storage space (in %) allocated to
  244. * full scanning
  245. * batch_scan_trunc_max - Max storage space (in %) allocated to
  246. * truncated scanning
  247. * batch_scan_notify_threshold - Set up notification level
  248. * based on total space
  249. *
  250. **/
  251. void btm_ble_set_storage_config(uint8_t batch_scan_full_max,
  252. uint8_t batch_scan_trunc_max,
  253. uint8_t batch_scan_notify_threshold,
  254. hci_cmd_cb cb) {
  255. uint8_t len = BTM_BLE_BATCH_SCAN_STORAGE_CFG_LEN;
  256. uint8_t param[len];
  257. memset(param, 0, len);
  258. uint8_t* pp = param;
  259. UINT8_TO_STREAM(pp, BTM_BLE_BATCH_SCAN_SET_STORAGE_PARAM);
  260. UINT8_TO_STREAM(pp, batch_scan_full_max);
  261. UINT8_TO_STREAM(pp, batch_scan_trunc_max);
  262. UINT8_TO_STREAM(pp, batch_scan_notify_threshold);
  263. btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_BATCH_SCAN_OCF, param, len, cb);
  264. }
  265. /* This function writes the batch scan params in controller */
  266. void btm_ble_set_batchscan_param(tBTM_BLE_BATCH_SCAN_MODE scan_mode,
  267. uint32_t scan_interval, uint32_t scan_window,
  268. tBLE_ADDR_TYPE addr_type,
  269. tBTM_BLE_DISCARD_RULE discard_rule,
  270. hci_cmd_cb cb) {
  271. // Override param and decide addr_type based on own addr type
  272. // TODO: Remove upper layer parameter?
  273. addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type;
  274. uint8_t len = BTM_BLE_BATCH_SCAN_PARAM_CONFIG_LEN;
  275. uint8_t param[len];
  276. memset(param, 0, len);
  277. uint8_t* p = param;
  278. UINT8_TO_STREAM(p, BTM_BLE_BATCH_SCAN_SET_PARAMS);
  279. UINT8_TO_STREAM(p, scan_mode);
  280. UINT32_TO_STREAM(p, scan_window);
  281. UINT32_TO_STREAM(p, scan_interval);
  282. UINT8_TO_STREAM(p, addr_type);
  283. UINT8_TO_STREAM(p, discard_rule);
  284. btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_BATCH_SCAN_OCF, param, len, cb);
  285. }
  286. /* This function enables the customer specific feature in controller */
  287. void btm_ble_enable_batchscan(hci_cmd_cb cb) {
  288. uint8_t len = BTM_BLE_BATCH_SCAN_ENB_DISB_LEN;
  289. uint8_t param[len];
  290. memset(param, 0, len);
  291. uint8_t* p = param;
  292. UINT8_TO_STREAM(p, BTM_BLE_BATCH_SCAN_ENB_DISAB_CUST_FEATURE);
  293. UINT8_TO_STREAM(p, 0x01 /* enable */);
  294. btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_BATCH_SCAN_OCF, param, len, cb);
  295. }
  296. } // namespace
  297. /*******************************************************************************
  298. *
  299. * Description This function is called to write storage config params.
  300. *
  301. * Parameters: batch_scan_full_max - Max storage space (in %) allocated to
  302. * full style
  303. * batch_scan_trunc_max - Max storage space (in %) allocated to
  304. * trunc style
  305. * batch_scan_notify_threshold - Setup notification level based
  306. * on total space
  307. * cb - Setup callback pointer
  308. * p_thres_cback - Threshold callback pointer
  309. * ref_value - Reference value
  310. *
  311. ******************************************************************************/
  312. void BTM_BleSetStorageConfig(uint8_t batch_scan_full_max,
  313. uint8_t batch_scan_trunc_max,
  314. uint8_t batch_scan_notify_threshold,
  315. Callback<void(uint8_t /* status */)> cb,
  316. tBTM_BLE_SCAN_THRESHOLD_CBACK* p_thres_cback,
  317. tBTM_BLE_REF_VALUE ref_value) {
  318. if (!can_do_batch_scan()) {
  319. cb.Run(BTM_ERR_PROCESSING);
  320. return;
  321. }
  322. BTM_TRACE_EVENT("%s: %d, %d, %d, %d, %d", __func__,
  323. ble_batchscan_cb.cur_state, ref_value, batch_scan_full_max,
  324. batch_scan_trunc_max, batch_scan_notify_threshold);
  325. ble_batchscan_cb.p_thres_cback = p_thres_cback;
  326. ble_batchscan_cb.ref_value = ref_value;
  327. if (batch_scan_full_max > BTM_BLE_ADV_SCAN_FULL_MAX ||
  328. batch_scan_trunc_max > BTM_BLE_ADV_SCAN_TRUNC_MAX ||
  329. batch_scan_notify_threshold > BTM_BLE_ADV_SCAN_THR_MAX) {
  330. BTM_TRACE_ERROR("Illegal set storage config params");
  331. cb.Run(BTM_ILLEGAL_VALUE);
  332. return;
  333. }
  334. if (BTM_BLE_SCAN_INVALID_STATE == ble_batchscan_cb.cur_state ||
  335. BTM_BLE_SCAN_DISABLED_STATE == ble_batchscan_cb.cur_state ||
  336. BTM_BLE_SCAN_DISABLE_CALLED == ble_batchscan_cb.cur_state) {
  337. btm_ble_enable_batchscan(Bind(&feat_enable_cb));
  338. ble_batchscan_cb.cur_state = BTM_BLE_SCAN_ENABLE_CALLED;
  339. }
  340. btm_ble_set_storage_config(batch_scan_full_max, batch_scan_trunc_max,
  341. batch_scan_notify_threshold,
  342. Bind(&storage_config_cb, cb));
  343. return;
  344. }
  345. /* This function is called to configure and enable batch scanning */
  346. void BTM_BleEnableBatchScan(tBTM_BLE_BATCH_SCAN_MODE scan_mode,
  347. uint32_t scan_interval, uint32_t scan_window,
  348. tBLE_ADDR_TYPE addr_type,
  349. tBTM_BLE_DISCARD_RULE discard_rule,
  350. Callback<void(uint8_t /* status */)> cb) {
  351. BTM_TRACE_EVENT("%s: %d, %d, %d, %d, %d, %d", __func__, scan_mode,
  352. scan_interval, scan_window, addr_type, discard_rule);
  353. if (!can_do_batch_scan()) {
  354. cb.Run(BTM_ERR_PROCESSING);
  355. return;
  356. }
  357. BTM_TRACE_DEBUG("%s: %d, %x, %x, %d, %d", __func__, scan_mode, scan_interval,
  358. scan_window, discard_rule, ble_batchscan_cb.cur_state);
  359. /* Only 16 bits will be used for scan interval and scan window as per
  360. * agreement with Google */
  361. /* So the standard LE range would suffice for scan interval and scan window */
  362. if ((BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN,
  363. BTM_BLE_SCAN_INT_MAX) ||
  364. BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN,
  365. BTM_BLE_SCAN_WIN_MAX)) &&
  366. (BTM_BLE_BATCH_SCAN_MODE_PASS == scan_mode ||
  367. BTM_BLE_BATCH_SCAN_MODE_ACTI == scan_mode ||
  368. BTM_BLE_BATCH_SCAN_MODE_PASS_ACTI == scan_mode) &&
  369. (BTM_BLE_DISCARD_OLD_ITEMS == discard_rule ||
  370. BTM_BLE_DISCARD_LOWER_RSSI_ITEMS == discard_rule)) {
  371. } else {
  372. BTM_TRACE_ERROR("%s: Illegal enable scan params", __func__);
  373. cb.Run(BTM_ILLEGAL_VALUE);
  374. return;
  375. }
  376. if (BTM_BLE_SCAN_INVALID_STATE == ble_batchscan_cb.cur_state ||
  377. BTM_BLE_SCAN_DISABLED_STATE == ble_batchscan_cb.cur_state ||
  378. BTM_BLE_SCAN_DISABLE_CALLED == ble_batchscan_cb.cur_state) {
  379. btm_ble_enable_batchscan(Bind(&feat_enable_cb));
  380. ble_batchscan_cb.cur_state = BTM_BLE_SCAN_ENABLE_CALLED;
  381. }
  382. ble_batchscan_cb.scan_mode = scan_mode;
  383. ble_batchscan_cb.scan_interval = scan_interval;
  384. ble_batchscan_cb.scan_window = scan_window;
  385. ble_batchscan_cb.addr_type = addr_type;
  386. ble_batchscan_cb.discard_rule = discard_rule;
  387. /* This command starts batch scanning, if enabled */
  388. btm_ble_set_batchscan_param(scan_mode, scan_interval, scan_window, addr_type,
  389. discard_rule, Bind(&param_enable_cb, cb));
  390. }
  391. /* This function is called to disable batch scanning */
  392. void BTM_BleDisableBatchScan(base::Callback<void(uint8_t /* status */)> cb) {
  393. BTM_TRACE_EVENT(" BTM_BleDisableBatchScan");
  394. if (!can_do_batch_scan()) {
  395. cb.Run(BTM_ERR_PROCESSING);
  396. return;
  397. }
  398. btm_ble_set_batchscan_param(
  399. BTM_BLE_BATCH_SCAN_MODE_DISABLE, ble_batchscan_cb.scan_interval,
  400. ble_batchscan_cb.scan_window, ble_batchscan_cb.addr_type,
  401. ble_batchscan_cb.discard_rule, Bind(&disable_cb, cb));
  402. ble_batchscan_cb.cur_state = BTM_BLE_SCAN_DISABLE_CALLED;
  403. }
  404. /* This function is called to start reading batch scan reports */
  405. void BTM_BleReadScanReports(tBTM_BLE_BATCH_SCAN_MODE scan_mode,
  406. tBTM_BLE_SCAN_REP_CBACK cb) {
  407. uint8_t read_scan_mode = 0;
  408. BTM_TRACE_EVENT("%s; %d", __func__, scan_mode);
  409. if (!can_do_batch_scan()) {
  410. BTM_TRACE_ERROR("Controller does not support batch scan");
  411. cb.Run(BTM_ERR_PROCESSING, 0, 0, {});
  412. return;
  413. }
  414. /* Check if the requested scan mode has already been setup by the user */
  415. read_scan_mode = ble_batchscan_cb.scan_mode & BTM_BLE_BATCH_SCAN_MODE_ACTI;
  416. if (0 == read_scan_mode)
  417. read_scan_mode = ble_batchscan_cb.scan_mode & BTM_BLE_BATCH_SCAN_MODE_PASS;
  418. /* Check only for modes, as scan reports can be called after disabling batch
  419. * scan */
  420. if (scan_mode != BTM_BLE_BATCH_SCAN_MODE_PASS &&
  421. scan_mode != BTM_BLE_BATCH_SCAN_MODE_ACTI) {
  422. BTM_TRACE_ERROR("Illegal read scan params: %d, %d, %d", read_scan_mode,
  423. scan_mode, ble_batchscan_cb.cur_state);
  424. cb.Run(BTM_ILLEGAL_VALUE, 0, 0, {});
  425. return;
  426. }
  427. btm_ble_read_batchscan_reports(
  428. scan_mode, base::Bind(&read_reports_cb, std::vector<uint8_t>(), 0, cb));
  429. return;
  430. }
  431. /* This function is called to setup the callback for tracking */
  432. void BTM_BleTrackAdvertiser(tBTM_BLE_TRACK_ADV_CBACK* p_track_cback,
  433. tBTM_BLE_REF_VALUE ref_value) {
  434. BTM_TRACE_EVENT("%s:", __func__);
  435. if (!can_do_batch_scan()) {
  436. BTM_TRACE_ERROR("Controller does not support batch scan");
  437. tBTM_BLE_TRACK_ADV_DATA track_adv_data;
  438. memset(&track_adv_data, 0, sizeof(tBTM_BLE_TRACK_ADV_DATA));
  439. track_adv_data.advertiser_info_present =
  440. NO_ADV_INFO_PRESENT; /* Indicates failure */
  441. track_adv_data.client_if = (uint8_t)ref_value;
  442. p_track_cback(&track_adv_data);
  443. return;
  444. }
  445. ble_advtrack_cb.p_track_cback = p_track_cback;
  446. ble_advtrack_cb.ref_value = ref_value;
  447. return;
  448. }
  449. /**
  450. * This function initialize the batch scan control block.
  451. **/
  452. void btm_ble_batchscan_init(void) {
  453. BTM_TRACE_EVENT(" btm_ble_batchscan_init");
  454. memset(&ble_batchscan_cb, 0, sizeof(tBTM_BLE_BATCH_SCAN_CB));
  455. memset(&ble_advtrack_cb, 0, sizeof(tBTM_BLE_ADV_TRACK_CB));
  456. BTM_RegisterForVSEvents(btm_ble_batchscan_filter_track_adv_vse_cback, true);
  457. }
  458. /**
  459. * This function cleans the batch scan control block.
  460. **/
  461. void btm_ble_batchscan_cleanup(void) {
  462. BTM_TRACE_EVENT("%s", __func__);
  463. memset(&ble_batchscan_cb, 0, sizeof(tBTM_BLE_BATCH_SCAN_CB));
  464. memset(&ble_advtrack_cb, 0, sizeof(tBTM_BLE_ADV_TRACK_CB));
  465. }