Use base::Callbacks in BleScannerInterface (2/3)
The remaining four native callbacks need more internal refactoring
before they can be turned into a callbacks.
Bug: 30622771
Test: sl4a FilteringTest
Change-Id: I8e1532bdfa2b0f22545f095eb427fd5ac1a75b56
diff --git a/btif/src/btif_ble_scanner.cc b/btif/src/btif_ble_scanner.cc
index ed39cb4..4fb3517 100644
--- a/btif/src/btif_ble_scanner.cc
+++ b/btif/src/btif_ble_scanner.cc
@@ -98,60 +98,6 @@
void btif_gattc_init_dev_cb(void) { p_dev_cb.clear(); }
-btgattc_error_t btif_gattc_translate_btm_status(tBTM_STATUS status) {
- switch (status) {
- case BTM_SUCCESS:
- case BTM_SUCCESS_NO_SECURITY:
- return BT_GATTC_COMMAND_SUCCESS;
-
- case BTM_CMD_STARTED:
- return BT_GATTC_COMMAND_STARTED;
-
- case BTM_BUSY:
- return BT_GATTC_COMMAND_BUSY;
-
- case BTM_CMD_STORED:
- return BT_GATTC_COMMAND_STORED;
-
- case BTM_NO_RESOURCES:
- return BT_GATTC_NO_RESOURCES;
-
- case BTM_MODE_UNSUPPORTED:
- case BTM_WRONG_MODE:
- case BTM_MODE4_LEVEL4_NOT_SUPPORTED:
- return BT_GATTC_MODE_UNSUPPORTED;
-
- case BTM_ILLEGAL_VALUE:
- case BTM_SCO_BAD_LENGTH:
- return BT_GATTC_ILLEGAL_VALUE;
-
- case BTM_UNKNOWN_ADDR:
- return BT_GATTC_UNKNOWN_ADDR;
-
- case BTM_DEVICE_TIMEOUT:
- return BT_GATTC_DEVICE_TIMEOUT;
-
- case BTM_FAILED_ON_SECURITY:
- case BTM_REPEATED_ATTEMPTS:
- case BTM_NOT_AUTHORIZED:
- return BT_GATTC_SECURITY_ERROR;
-
- case BTM_DEV_RESET:
- case BTM_ILLEGAL_ACTION:
- return BT_GATTC_INCORRECT_STATE;
-
- case BTM_BAD_VALUE_RET:
- return BT_GATTC_INVALID_CONTROLLER_OUTPUT;
-
- case BTM_DELAY_CHECK:
- return BT_GATTC_DELAYED_ENCRYPTION_CHECK;
-
- case BTM_ERR_PROCESSING:
- default:
- return BT_GATTC_ERR_PROCESSING;
- }
-}
-
void btif_gatts_upstreams_evt(uint16_t event, char* p_param) {
LOG_VERBOSE(LOG_TAG, "%s: Event %d", __func__, event);
@@ -179,30 +125,6 @@
ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
}
-void bta_scan_param_setup_cb(tGATT_IF client_if, tBTM_STATUS status) {
- SCAN_CBACK_IN_JNI(scan_parameter_setup_completed_cb, client_if,
- btif_gattc_translate_btm_status(status));
-}
-
-void bta_scan_filt_cfg_cb(uint8_t filt_type, uint8_t client_if,
- tBTM_BLE_PF_AVBL_SPACE avbl_space,
- tBTM_BLE_PF_ACTION action, tBTA_STATUS status) {
- SCAN_CBACK_IN_JNI(scan_filter_cfg_cb, action, client_if, status, filt_type,
- avbl_space);
-}
-
-void bta_scan_filt_param_setup_cb(tBTM_BLE_REF_VALUE ref_value,
- tBTM_BLE_PF_AVBL_SPACE avbl_space,
- uint8_t action_type, tBTA_STATUS status) {
- SCAN_CBACK_IN_JNI(scan_filter_param_cb, action_type, ref_value, status,
- avbl_space);
-}
-
-void bta_scan_filt_status_cb(tBTM_BLE_REF_VALUE ref_value, uint8_t action,
- tBTA_STATUS status) {
- SCAN_CBACK_IN_JNI(scan_filter_status_cb, action, ref_value, status);
-}
-
void bta_batch_scan_threshold_cb(tBTM_BLE_REF_VALUE ref_value) {
SCAN_CBACK_IN_JNI(batchscan_threshold_cb, ref_value);
}
@@ -309,20 +231,14 @@
~BleScannerInterfaceImpl(){};
void RegisterScanner(RegisterCallback cb) override {
- do_in_bta_thread(
- FROM_HERE,
- Bind(
- [](RegisterCallback cb) {
- BTA_GATTC_AppRegister(
- bta_gatts_cback,
- base::Bind(
- [](RegisterCallback cb, uint8_t client_id,
- uint8_t status) {
- do_in_jni_thread(base::Bind(cb, client_id, status));
- },
- std::move(cb)));
- },
- std::move(cb)));
+ do_in_bta_thread(FROM_HERE,
+ Bind(
+ [](RegisterCallback cb) {
+ BTA_GATTC_AppRegister(
+ bta_gatts_cback,
+ jni_thread_wrapper(FROM_HERE, std::move(cb)));
+ },
+ std::move(cb)));
}
void Unregister(int scanner_id) override {
@@ -343,7 +259,8 @@
void ScanFilterParamSetup(
uint8_t client_if, uint8_t action, uint8_t filt_index,
- std::unique_ptr<btgatt_filt_param_setup_t> filt_param) override {
+ std::unique_ptr<btgatt_filt_param_setup_t> filt_param,
+ FilterParamSetupCallback cb) override {
BTIF_TRACE_DEBUG("%s", __func__);
if (filt_param && filt_param->dely_mode == 1) {
@@ -352,20 +269,19 @@
client_if));
}
- do_in_bta_thread(
- FROM_HERE,
- base::Bind(&BTM_BleAdvFilterParamSetup, action, filt_index,
- base::Passed(&filt_param),
- base::Bind(&bta_scan_filt_param_setup_cb, client_if)));
+ do_in_bta_thread(FROM_HERE,
+ base::Bind(&BTM_BleAdvFilterParamSetup, action, filt_index,
+ base::Passed(&filt_param),
+ jni_thread_wrapper(FROM_HERE, std::move(cb))));
}
- void ScanFilterAddRemove(int client_if, int action, int filt_type,
- int filt_index, int company_id, int company_id_mask,
+ void ScanFilterAddRemove(int action, int filt_type, int filt_index,
+ int company_id, int company_id_mask,
const bt_uuid_t* p_uuid,
const bt_uuid_t* p_uuid_mask,
const bt_bdaddr_t* bd_addr, char addr_type,
- vector<uint8_t> data,
- vector<uint8_t> mask) override {
+ vector<uint8_t> data, vector<uint8_t> mask,
+ FilterConfigCallback cb) override {
BTIF_TRACE_DEBUG("%s, %d, %d", __func__, action, filt_type);
/* If data is passed, both mask and data have to be the same length */
@@ -382,7 +298,7 @@
FROM_HERE,
base::Bind(&BTM_LE_PF_addr_filter, action, filt_index,
std::move(target_addr),
- Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
+ jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type))));
return;
}
@@ -401,7 +317,7 @@
FROM_HERE,
base::Bind(&BTM_LE_PF_uuid_filter, action, filt_index, filt_type,
bt_uuid, BTM_BLE_PF_LOGIC_AND, nullptr,
- Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
+ jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type))));
return;
}
@@ -411,15 +327,16 @@
FROM_HERE,
base::Bind(&BTM_LE_PF_uuid_filter, action, filt_index, filt_type,
bt_uuid, BTM_BLE_PF_LOGIC_AND, base::Owned(mask),
- Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
+ jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type))));
return;
}
case BTM_BLE_PF_LOCAL_NAME: {
do_in_bta_thread(
- FROM_HERE, base::Bind(&BTM_LE_PF_local_name, action, filt_index,
- std::move(data), Bind(&bta_scan_filt_cfg_cb,
- filt_type, client_if)));
+ FROM_HERE,
+ base::Bind(&BTM_LE_PF_local_name, action, filt_index,
+ std::move(data),
+ jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type))));
return;
}
@@ -428,7 +345,7 @@
FROM_HERE,
base::Bind(&BTM_LE_PF_manu_data, action, filt_index, company_id,
company_id_mask, std::move(data), std::move(mask),
- Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
+ jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type))));
return;
}
@@ -437,7 +354,7 @@
FROM_HERE,
base::Bind(&BTM_LE_PF_srvc_data_pattern, action, filt_index,
std::move(data), std::move(mask),
- Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
+ jni_thread_wrapper(FROM_HERE, Bind(cb, filt_type))));
return;
}
@@ -447,70 +364,55 @@
}
}
- void ScanFilterClear(int client_if, int filter_index) override {
+ void ScanFilterClear(int filter_index, FilterConfigCallback cb) override {
BTIF_TRACE_DEBUG("%s: filter_index: %d", __func__, filter_index);
do_in_bta_thread(FROM_HERE,
base::Bind(&BTM_LE_PF_clear, filter_index,
- Bind(&bta_scan_filt_cfg_cb, BTM_BLE_PF_TYPE_ALL,
- client_if)));
+ jni_thread_wrapper(
+ FROM_HERE, Bind(cb, BTM_BLE_PF_TYPE_ALL))));
}
- void ScanFilterEnable(int client_if, bool enable) override {
+ void ScanFilterEnable(bool enable, EnableCallback cb) override {
BTIF_TRACE_DEBUG("%s: enable: %d", __func__, enable);
uint8_t action = enable ? 1 : 0;
- do_in_bta_thread(
- FROM_HERE, base::Bind(&BTM_BleEnableDisableFilterFeature, action,
- base::Bind(&bta_scan_filt_status_cb, client_if)));
+ do_in_bta_thread(FROM_HERE,
+ base::Bind(&BTM_BleEnableDisableFilterFeature, action,
+ jni_thread_wrapper(FROM_HERE, std::move(cb))));
}
- void SetScanParameters(int client_if, int scan_interval,
- int scan_window) override {
+ void SetScanParameters(int scan_interval, int scan_window,
+ Callback cb) override {
do_in_bta_thread(
- FROM_HERE,
- base::Bind(&BTM_BleSetScanParams, client_if, scan_interval, scan_window,
- BTM_BLE_SCAN_MODE_ACTI, bta_scan_param_setup_cb));
+ FROM_HERE, base::Bind(&BTM_BleSetScanParams, scan_interval, scan_window,
+ BTM_BLE_SCAN_MODE_ACTI,
+ jni_thread_wrapper(FROM_HERE, std::move(cb))));
}
void BatchscanConfigStorage(int client_if, int batch_scan_full_max,
int batch_scan_trunc_max,
- int batch_scan_notify_threshold) override {
- base::Callback<void(uint8_t /* status */)> cb = base::Bind(
- [](int client_if, uint8_t status) {
- SCAN_CBACK_IN_JNI(batchscan_cfg_storage_cb, client_if, status);
- },
- client_if);
-
+ int batch_scan_notify_threshold,
+ Callback cb) override {
do_in_bta_thread(
FROM_HERE,
base::Bind(&BTM_BleSetStorageConfig, (uint8_t)batch_scan_full_max,
(uint8_t)batch_scan_trunc_max,
- (uint8_t)batch_scan_notify_threshold, cb,
+ (uint8_t)batch_scan_notify_threshold,
+ jni_thread_wrapper(FROM_HERE, cb),
bta_batch_scan_threshold_cb, (tBTM_BLE_REF_VALUE)client_if));
}
- void BatchscanEnable(int client_if, int scan_mode, int scan_interval,
- int scan_window, int addr_type,
- int discard_rule) override {
- auto cb = base::Bind(
- [](int client_if, uint8_t status) {
- SCAN_CBACK_IN_JNI(batchscan_enb_disable_cb, 1, client_if, status);
- },
- client_if);
-
+ void BatchscanEnable(int scan_mode, int scan_interval, int scan_window,
+ int addr_type, int discard_rule, Callback cb) override {
do_in_bta_thread(
FROM_HERE, base::Bind(&BTM_BleEnableBatchScan, scan_mode, scan_interval,
- scan_window, discard_rule, addr_type, cb));
+ scan_window, discard_rule, addr_type,
+ jni_thread_wrapper(FROM_HERE, cb)));
}
- void BatchscanDisable(int client_if) override {
- auto cb = base::Bind(
- [](int client_if, uint8_t status) {
- SCAN_CBACK_IN_JNI(batchscan_enb_disable_cb, 1, client_if, status);
- },
- client_if);
-
- do_in_bta_thread(FROM_HERE, base::Bind(&BTM_BleDisableBatchScan, cb));
+ void BatchscanDisable(Callback cb) override {
+ do_in_bta_thread(FROM_HERE, base::Bind(&BTM_BleDisableBatchScan,
+ jni_thread_wrapper(FROM_HERE, cb)));
}
void BatchscanReadReports(int client_if, int scan_mode) override {
diff --git a/service/gatt_server_old.cc b/service/gatt_server_old.cc
index 7dfd2b1..7860745 100644
--- a/service/gatt_server_old.cc
+++ b/service/gatt_server_old.cc
@@ -444,16 +444,9 @@
const btgatt_scanner_callbacks_t gatt_scanner_callbacks = {
ScanResultCallback,
- nullptr, /* batchscan_cfg_storage_cb; */
- nullptr, /* batchscan_enb_disable_cb; */
nullptr, /* batchscan_reports_cb; */
nullptr, /* batchscan_threshold_cb; */
nullptr, /* track_adv_event_cb; */
- nullptr, /* scan_parameter_setup_completed_cb; */
- nullptr, /* scan_filter_cfg_cb; */
- nullptr, /* scan_filter_param_cb; */
- nullptr, /* scan_filter_status_cb; */
-
};
const btgatt_callbacks_t gatt_callbacks = {
diff --git a/service/hal/bluetooth_gatt_interface.cc b/service/hal/bluetooth_gatt_interface.cc
index ea72e7b..3830d4e 100644
--- a/service/hal/bluetooth_gatt_interface.cc
+++ b/service/hal/bluetooth_gatt_interface.cc
@@ -380,15 +380,9 @@
const btgatt_scanner_callbacks_t gatt_scanner_callbacks = {
ScanResultCallback,
- nullptr, // batchscan_cfg_storage_cb
- nullptr, // batchscan_enb_disable_cb
nullptr, // batchscan_reports_cb
nullptr, // batchscan_threshold_cb
nullptr, // track_adv_event_cb
- nullptr, // scan_parameter_setup_completed_cb
- nullptr, // scan_filter_cfg_cb
- nullptr, // scan_filter_param_cb
- nullptr, // scan_filter_status_cb
};
const btgatt_client_callbacks_t gatt_client_callbacks = {
diff --git a/service/test/low_energy_scanner_unittest.cc b/service/test/low_energy_scanner_unittest.cc
index 48762d6..a53d2c4 100644
--- a/service/test/low_energy_scanner_unittest.cc
+++ b/service/test/low_energy_scanner_unittest.cc
@@ -45,38 +45,43 @@
MOCK_METHOD1(Unregister, void(int));
MOCK_METHOD1(Scan, void(bool));
- MOCK_METHOD4(ScanFilterParamSetupImpl,
+ MOCK_METHOD5(ScanFilterParamSetupImpl,
void(uint8_t client_if, uint8_t action, uint8_t filt_index,
- btgatt_filt_param_setup_t* filt_param));
- MOCK_METHOD2(ScanFilterClear, void(int client_if, int filt_index));
- MOCK_METHOD2(ScanFilterEnable, void(int client_if, bool enable));
+ btgatt_filt_param_setup_t* filt_param,
+ FilterParamSetupCallback cb));
+ MOCK_METHOD2(ScanFilterClear, void(int filt_index, FilterConfigCallback cb));
+ MOCK_METHOD2(ScanFilterEnable, void(bool enable, EnableCallback cb));
MOCK_METHOD3(SetScanParameters,
- void(int client_if, int scan_interval, int scan_window));
+ void(int scan_interval, int scan_window, Callback cb));
- MOCK_METHOD4(BatchscanConfigStorage,
+ MOCK_METHOD5(BatchscanConfigStorage,
void(int client_if, int batch_scan_full_max,
- int batch_scan_trunc_max, int batch_scan_notify_threshold));
+ int batch_scan_trunc_max, int batch_scan_notify_threshold,
+ Callback cb));
MOCK_METHOD6(BatchscanEnable,
- void(int client_if, int scan_mode, int scan_interval,
- int scan_window, int addr_type, int discard_rule));
+ void(int scan_mode, int scan_interval, int scan_window,
+ int addr_type, int discard_rule, Callback cb));
- MOCK_METHOD1(BatchscanDisable, void(int client_if));
+ MOCK_METHOD1(BatchscanDisable, void(Callback cb));
MOCK_METHOD2(BatchscanReadReports, void(int client_if, int scan_mode));
- void ScanFilterAddRemove(int client_if, int action, int filt_type,
- int filt_index, int company_id, int company_id_mask,
+ void ScanFilterAddRemove(int action, int filt_type, int filt_index,
+ int company_id, int company_id_mask,
const bt_uuid_t* p_uuid,
const bt_uuid_t* p_uuid_mask,
const bt_bdaddr_t* bd_addr, char addr_type,
std::vector<uint8_t> data,
- std::vector<uint8_t> p_mask){};
+ std::vector<uint8_t> p_mask,
+ FilterConfigCallback cb){};
void ScanFilterParamSetup(
uint8_t client_if, uint8_t action, uint8_t filt_index,
- std::unique_ptr<btgatt_filt_param_setup_t> filt_param) {
- ScanFilterParamSetupImpl(client_if, action, filt_index, filt_param.get());
+ std::unique_ptr<btgatt_filt_param_setup_t> filt_param,
+ FilterParamSetupCallback cb) {
+ ScanFilterParamSetupImpl(client_if, action, filt_index, filt_param.get(),
+ std::move(cb));
}
};
diff --git a/stack/btm/btm_ble_gap.cc b/stack/btm/btm_ble_gap.cc
index 05389f1..5a55c92 100644
--- a/stack/btm/btm_ble_gap.cc
+++ b/stack/btm/btm_ble_gap.cc
@@ -24,6 +24,7 @@
#define LOG_TAG "bt_btm_ble"
+#include <base/callback.h>
#include <base/strings/string_number_conversions.h>
#include <stddef.h>
#include <stdio.h>
@@ -1017,24 +1018,13 @@
return status;
}
-/*******************************************************************************
- *
- * Function BTM_BleSetScanParams
- *
- * Description This function is called to set scan parameters.
- *
- * Parameters client_if - Client IF
- * scan_interval - Scan interval
- * scan_window - Scan window
- * scan_mode - Scan mode
- * scan_setup_status_cback - Scan param setup status callback
- *
- * Returns void
- *
- ******************************************************************************/
-void BTM_BleSetScanParams(tGATT_IF client_if, uint32_t scan_interval,
- uint32_t scan_window, tBLE_SCAN_MODE scan_mode,
- tBLE_SCAN_PARAM_SETUP_CBACK scan_setup_status_cback) {
+/**
+ * This function is called to set scan parameters. |cb| is called with operation
+ * status
+ **/
+void BTM_BleSetScanParams(uint32_t scan_interval, uint32_t scan_window,
+ tBLE_SCAN_MODE scan_mode,
+ base::Callback<void(uint8_t)> cb) {
tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
uint32_t max_scan_interval;
uint32_t max_scan_window;
@@ -1064,11 +1054,9 @@
p_cb->scan_interval = scan_interval;
p_cb->scan_window = scan_window;
- if (scan_setup_status_cback != NULL)
- scan_setup_status_cback(client_if, BTM_SUCCESS);
+ cb.Run(BTM_SUCCESS);
} else {
- if (scan_setup_status_cback != NULL)
- scan_setup_status_cback(client_if, BTM_ILLEGAL_VALUE);
+ cb.Run(BTM_ILLEGAL_VALUE);
BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d",
scan_interval, scan_window);
diff --git a/stack/include/btm_ble_api.h b/stack/include/btm_ble_api.h
index 73f8c69..bb8c6ff 100644
--- a/stack/include/btm_ble_api.h
+++ b/stack/include/btm_ble_api.h
@@ -25,6 +25,7 @@
#ifndef BTM_BLE_API_H
#define BTM_BLE_API_H
+#include <base/callback_forward.h>
#include <hardware/bt_common_types.h>
#include <memory>
#include "bt_common.h"
@@ -102,25 +103,13 @@
******************************************************************************/
extern void BTM_BleObtainVendorCapabilities(tBTM_BLE_VSC_CB* p_cmn_vsc_cb);
-/*******************************************************************************
- *
- * Function BTM_BleSetScanParams
- *
- * Description This function is called to set Scan parameters.
- *
- * Parameters client_if - Client IF value
- * scan_interval - Scan interval
- * scan_window - Scan window
- * scan_type - Scan type
- * scan_setup_status_cback - Scan setup status callback
- *
- * Returns void
- *
- ******************************************************************************/
-extern void BTM_BleSetScanParams(
- tGATT_IF client_if, uint32_t scan_interval, uint32_t scan_window,
- tBLE_SCAN_MODE scan_type,
- tBLE_SCAN_PARAM_SETUP_CBACK scan_setup_status_cback);
+/**
+ * This function is called to set scan parameters. |cb| is called with operation
+ * status
+ **/
+extern void BTM_BleSetScanParams(uint32_t scan_interval, uint32_t scan_window,
+ tBLE_SCAN_MODE scan_type,
+ base::Callback<void(uint8_t)> cb);
/*******************************************************************************
*