Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d4eb8538 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Refactor LE scanning HAL (2/3)

This patch converts btgatt_scanner_interface_t struct into
BleScannerInterface class. It also refactors three most important
methods from this interface: RegisterAdvertiser, Scan, and Unregister.
Rest of this interface will be updated in following patches.

Bug: 30622771
Test: sl4a BleScanApiTest
Change-Id: Ie35356f6c3c4f5488514ef55a48a32c93fb21b83
parent 7eacee8b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@

extern const btgatt_client_interface_t btgattClientInterface;
extern const btgatt_server_interface_t btgattServerInterface;
extern const btgatt_scanner_interface_t btgattScannerInterface;

BleAdvertiserInterface* get_ble_advertiser_instance();
BleScannerInterface* get_ble_scanner_instance();
#endif
+191 −207
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include <hardware/bt_gatt.h>

#include "bta_api.h"
#include "bta_closure_api.h"
#include "bta_gatt_api.h"
#include "btif_config.h"
#include "btif_dm.h"
@@ -47,8 +48,7 @@
using base::Bind;
using base::Owned;
using std::vector;
using RegisterCallback =
    base::Callback<void(uint8_t /* scanner_id */, uint8_t /* status */)>;
using RegisterCallback = BleScannerInterface::RegisterCallback;

extern bt_status_t do_in_jni_thread(const base::Closure& task);
extern const btgatt_callbacks_t* bt_gatt_callbacks;
@@ -363,55 +363,6 @@ void bta_track_adv_event_cb(tBTA_DM_BLE_TRACK_ADV_DATA* p_track_adv_data) {
  SCAN_CBACK_IN_JNI(track_adv_event_cb, Owned(btif_scan_track_cb));
}

bt_status_t btif_gattc_register_scanner(bt_uuid_t* uuid) {
  CHECK_BTGATT_INIT();

  tBT_UUID bt_uuid;
  btif_to_bta_uuid(&bt_uuid, uuid);

  return do_in_jni_thread(Bind(
      [](tBT_UUID bt_uuid) {
        BTA_GATTC_AppRegister(
            bta_gatts_cback,
            base::Bind(
                [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) {
                  do_in_jni_thread(Bind(
                      [](tBT_UUID bt_uuid, uint8_t client_id, uint8_t status) {
                        bt_uuid_t app_uuid;
                        bta_to_btif_uuid(&app_uuid, &bt_uuid);

                        HAL_CBACK(bt_gatt_callbacks,
                                  scanner->register_scanner_cb, status,
                                  client_id, &app_uuid);
                      },
                      bt_uuid, client_id, status));
                },
                bt_uuid));
      },
      bt_uuid));
}

void btif_gattc_unregister_scanner_impl(int client_if) {
  BTA_GATTC_AppDeregister(client_if);
}

bt_status_t btif_gattc_unregister_scanner(int scanner_id) {
  CHECK_BTGATT_INIT();
  return do_in_jni_thread(
      Bind(&btif_gattc_unregister_scanner_impl, scanner_id));
}

bt_status_t btif_gattc_scan(bool start) {
  CHECK_BTGATT_INIT();
  if (start) {
    btif_gattc_init_dev_cb();
    return do_in_jni_thread(Bind(&BTA_DmBleObserve, true, 0,
                                 (tBTA_DM_SEARCH_CBACK*)bta_scan_results_cb));
  }

  return do_in_jni_thread(Bind(&BTA_DmBleObserve, false, 0, nullptr));
}

void btif_gattc_scan_filter_param_setup_impl(
    int client_if, uint8_t action, int filt_index,
    tBTA_DM_BLE_PF_FILT_PARAMS* adv_filt_param) {
@@ -423,29 +374,6 @@ void btif_gattc_scan_filter_param_setup_impl(
                           bta_scan_filt_param_setup_cb, client_if);
}

bt_status_t btif_gattc_scan_filter_param_setup(
    btgatt_filt_param_setup_t filt_param) {
  CHECK_BTGATT_INIT();
  BTIF_TRACE_DEBUG("%s", __func__);

  tBTA_DM_BLE_PF_FILT_PARAMS* adv_filt_param = new tBTA_DM_BLE_PF_FILT_PARAMS;
  adv_filt_param->feat_seln = filt_param.feat_seln;
  adv_filt_param->list_logic_type = filt_param.list_logic_type;
  adv_filt_param->filt_logic_type = filt_param.filt_logic_type;
  adv_filt_param->rssi_high_thres = filt_param.rssi_high_thres;
  adv_filt_param->rssi_low_thres = filt_param.rssi_low_thres;
  adv_filt_param->dely_mode = filt_param.dely_mode;
  adv_filt_param->found_timeout = filt_param.found_timeout;
  adv_filt_param->lost_timeout = filt_param.lost_timeout;
  adv_filt_param->found_timeout_cnt = filt_param.found_timeout_cnt;
  adv_filt_param->num_of_tracking_entries = filt_param.num_of_tracking_entries;

  return do_in_jni_thread(
      Bind(base::IgnoreResult(&btif_gattc_scan_filter_param_setup_impl),
           filt_param.client_if, filt_param.action, filt_param.filt_index,
           base::Owned(adv_filt_param)));
}

void btif_gattc_scan_filter_add_srvc_uuid(tBT_UUID uuid,
                                          tBTA_DM_BLE_PF_COND_MASK* p_uuid_mask,
                                          int action, int filt_type,
@@ -505,11 +433,70 @@ void btif_gattc_scan_filter_add_data_pattern(vector<uint8_t> pattern,
                              &bta_scan_filt_cfg_cb, client_if);
}

bt_status_t btif_gattc_scan_filter_add_remove(
class BleScannerInterfaceImpl : public BleScannerInterface {
  ~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)));
  }

  void Unregister(int scanner_id) override {
    do_in_bta_thread(FROM_HERE, Bind(&BTA_GATTC_AppDeregister, scanner_id));
  }

  void Scan(bool start) override {
    if (!start) {
      do_in_bta_thread(FROM_HERE, Bind(&BTA_DmBleObserve, false, 0, nullptr));
      return;
    }

    btif_gattc_init_dev_cb();
    do_in_bta_thread(FROM_HERE,
                     Bind(&BTA_DmBleObserve, true, 0,
                          (tBTA_DM_SEARCH_CBACK*)bta_scan_results_cb));
  }

  bt_status_t scan_filter_param_setup(
      btgatt_filt_param_setup_t filt_param) override {
    BTIF_TRACE_DEBUG("%s", __func__);

    tBTA_DM_BLE_PF_FILT_PARAMS* adv_filt_param = new tBTA_DM_BLE_PF_FILT_PARAMS;
    adv_filt_param->feat_seln = filt_param.feat_seln;
    adv_filt_param->list_logic_type = filt_param.list_logic_type;
    adv_filt_param->filt_logic_type = filt_param.filt_logic_type;
    adv_filt_param->rssi_high_thres = filt_param.rssi_high_thres;
    adv_filt_param->rssi_low_thres = filt_param.rssi_low_thres;
    adv_filt_param->dely_mode = filt_param.dely_mode;
    adv_filt_param->found_timeout = filt_param.found_timeout;
    adv_filt_param->lost_timeout = filt_param.lost_timeout;
    adv_filt_param->found_timeout_cnt = filt_param.found_timeout_cnt;
    adv_filt_param->num_of_tracking_entries =
        filt_param.num_of_tracking_entries;

    return do_in_jni_thread(
        Bind(base::IgnoreResult(&btif_gattc_scan_filter_param_setup_impl),
             filt_param.client_if, filt_param.action, filt_param.filt_index,
             base::Owned(adv_filt_param)));
  }

  bt_status_t scan_filter_add_remove(
      int client_if, 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) {
      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 {
    CHECK_BTGATT_INIT();
    BTIF_TRACE_DEBUG("%s, %d, %d", __func__, action, filt_type);

@@ -572,9 +559,9 @@ bt_status_t btif_gattc_scan_filter_add_remove(

      case BTA_DM_BLE_PF_MANU_DATA: {
        return do_in_jni_thread(Bind(&btif_gattc_scan_filter_add_manu_data,
                                   company_id, company_id_mask, std::move(data),
                                   std::move(mask), action, filt_type,
                                   filt_index, client_if));
                                     company_id, company_id_mask,
                                     std::move(data), std::move(mask), action,
                                     filt_type, filt_index, client_if));
      }

      case BTA_DM_BLE_PF_SRVC_DATA_PATTERN: {
@@ -589,7 +576,7 @@ bt_status_t btif_gattc_scan_filter_add_remove(
    }
  }

bt_status_t btif_gattc_scan_filter_clear(int client_if, int filter_index) {
  bt_status_t scan_filter_clear(int client_if, int filter_index) override {
    CHECK_BTGATT_INIT();
    BTIF_TRACE_DEBUG("%s: filter_index: %d", __func__, filter_index);

@@ -599,7 +586,7 @@ bt_status_t btif_gattc_scan_filter_clear(int client_if, int filter_index) {
                                 &bta_scan_filt_cfg_cb, client_if));
  }

bt_status_t btif_gattc_scan_filter_enable(int client_if, bool enable) {
  bt_status_t scan_filter_enable(int client_if, bool enable) override {
    CHECK_BTGATT_INIT();
    BTIF_TRACE_DEBUG("%s: enable: %d", __func__, enable);

@@ -609,8 +596,8 @@ bt_status_t btif_gattc_scan_filter_enable(int client_if, bool enable) {
                                 &bta_scan_filt_status_cb, client_if));
  }

bt_status_t btif_gattc_set_scan_parameters(int client_if, int scan_interval,
                                           int scan_window) {
  bt_status_t set_scan_parameters(int client_if, int scan_interval,
                                  int scan_window) override {
    CHECK_BTGATT_INIT();
    return do_in_jni_thread(
        Bind(BTA_DmSetBleScanParams, client_if, scan_interval, scan_window,
@@ -618,51 +605,48 @@ bt_status_t btif_gattc_set_scan_parameters(int client_if, int scan_interval,
             (tBLE_SCAN_PARAM_SETUP_CBACK)bta_scan_param_setup_cb));
  }

bt_status_t btif_gattc_cfg_storage(int client_if, int batch_scan_full_max,
  bt_status_t batchscan_cfg_storage(int client_if, int batch_scan_full_max,
                                    int batch_scan_trunc_max,
                                   int batch_scan_notify_threshold) {
                                    int batch_scan_notify_threshold) override {
    CHECK_BTGATT_INIT();
    return do_in_jni_thread(
      Bind(BTA_DmBleSetStorageParams, batch_scan_full_max, batch_scan_trunc_max,
           batch_scan_notify_threshold,
        Bind(BTA_DmBleSetStorageParams, batch_scan_full_max,
             batch_scan_trunc_max, batch_scan_notify_threshold,
             (tBTA_BLE_SCAN_SETUP_CBACK*)bta_batch_scan_setup_cb,
             (tBTA_BLE_SCAN_THRESHOLD_CBACK*)bta_batch_scan_threshold_cb,
             (tBTA_BLE_SCAN_REP_CBACK*)bta_batch_scan_reports_cb,
             (tBTA_DM_BLE_REF_VALUE)client_if));
  }

bt_status_t btif_gattc_enb_batch_scan(int client_if, int scan_mode,
  bt_status_t batchscan_enb_batch_scan(int client_if, int scan_mode,
                                       int scan_interval, int scan_window,
                                      int addr_type, int discard_rule) {
                                       int addr_type,
                                       int discard_rule) override {
    CHECK_BTGATT_INIT();
    return do_in_jni_thread(Bind(BTA_DmBleEnableBatchScan, scan_mode,
                                 scan_interval, scan_window, discard_rule,
                                 addr_type, client_if));
  }

bt_status_t btif_gattc_dis_batch_scan(int client_if) {
  bt_status_t batchscan_dis_batch_scan(int client_if) override {
    CHECK_BTGATT_INIT();
    return do_in_jni_thread(Bind(BTA_DmBleDisableBatchScan, client_if));
  }

bt_status_t btif_gattc_read_batch_scan_reports(int client_if, int scan_mode) {
  bt_status_t batchscan_read_reports(int client_if, int scan_mode) override {
    CHECK_BTGATT_INIT();
  return do_in_jni_thread(Bind(BTA_DmBleReadScanReports, scan_mode, client_if));
    return do_in_jni_thread(
        Bind(BTA_DmBleReadScanReports, scan_mode, client_if));
  }
};

BleScannerInterface* btLeScannerInstance = nullptr;

}  // namespace

const btgatt_scanner_interface_t btgattScannerInterface = {
    btif_gattc_register_scanner,
    btif_gattc_unregister_scanner,
    btif_gattc_scan,
    btif_gattc_scan_filter_param_setup,
    btif_gattc_scan_filter_add_remove,
    btif_gattc_scan_filter_clear,
    btif_gattc_scan_filter_enable,
    btif_gattc_set_scan_parameters,
    btif_gattc_cfg_storage,
    btif_gattc_enb_batch_scan,
    btif_gattc_dis_batch_scan,
    btif_gattc_read_batch_scan_reports,
};
BleScannerInterface* get_ble_scanner_instance() {
  if (btLeScannerInstance == nullptr)
    btLeScannerInstance = new BleScannerInterfaceImpl();

  return btLeScannerInstance;
}
+3 −2
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ static btgatt_interface_t btgattInterface = {

    &btgattClientInterface,
    &btgattServerInterface,
    &btgattScannerInterface,
    nullptr,  // filled in btif_gatt_get_interface
    nullptr   // filled in btif_gatt_get_interface
};

@@ -99,6 +99,7 @@ const btgatt_interface_t* btif_gatt_get_interface() {
  // TODO(jpawlowski) right now initializing advertiser field in static
  // structure cause explosion of dependencies. It must be initialized here
  // until those dependencies are properly abstracted for tests.
  btgattInterface.scanner = get_ble_scanner_instance();
  btgattInterface.advertiser = get_ble_advertiser_instance();
  return &btgattInterface;
}
+2 −16
Original line number Diff line number Diff line
@@ -360,11 +360,6 @@ void RegisterClientCallback(int status, int client_if, bt_uuid_t* app_uuid) {
      0 /* no timeout */, base::Bind(&DoNothing));
}

void RegisterScannerCallback(int status, int scanner_id, bt_uuid_t* app_uuid) {
  LOG_INFO(LOG_TAG, "%s: status:%d scanner_id:%d uuid[0]:%u", __func__, status,
           scanner_id, app_uuid->uu[0]);
}

void ServiceStoppedCallback(int status, int server_if, int srvc_handle) {
  LOG_INFO(LOG_TAG, "%s: status:%d server_if:%d srvc_handle:%d", __func__,
           status, server_if, srvc_handle);
@@ -445,7 +440,6 @@ const btgatt_client_callbacks_t gatt_client_callbacks = {
};

const btgatt_scanner_callbacks_t gatt_scanner_callbacks = {
    RegisterScannerCallback,
    ScanResultCallback,
    nullptr, /* batchscan_cfg_storage_cb; */
    nullptr, /* batchscan_enb_disable_cb; */
@@ -691,20 +685,12 @@ bool Server::Stop() {
}

bool Server::ScanEnable() {
  bt_status_t btstat = internal_->gatt->scanner->scan(true);
  if (btstat) {
    LOG_ERROR(LOG_TAG, "Enable scan failed: %d", btstat);
    return false;
  }
  internal_->gatt->scanner->Scan(true);
  return true;
}

bool Server::ScanDisable() {
  bt_status_t btstat = internal_->gatt->scanner->scan(false);
  if (btstat) {
    LOG_ERROR(LOG_TAG, "Disable scan failed: %d", btstat);
    return false;
  }
  internal_->gatt->scanner->Scan(false);
  return true;
}

+3 −29
Original line number Diff line number Diff line
@@ -88,17 +88,6 @@ void RegisterClientCallback(int status, int client_if, bt_uuid_t* app_uuid) {
      RegisterClientCallback(g_interface, status, client_if, *app_uuid));
}

void RegisterScannerCallback(int status, int scanner_id, bt_uuid_t* app_uuid) {
  shared_lock<shared_mutex_impl> lock(g_instance_lock);
  VLOG(2) << __func__ << " - status: " << status
          << " scanner_id: " << scanner_id;
  VERIFY_INTERFACE_OR_RETURN();
  CHECK(app_uuid);

  FOR_EACH_SCANNER_OBSERVER(
      RegisterScannerCallback(g_interface, status, scanner_id, *app_uuid));
}

void ScanResultCallback(
    bt_bdaddr_t* bda, int rssi,
    std::vector<uint8_t> adv_data) {  // NOLINT(pass-by-value)
@@ -387,7 +376,6 @@ void MtuChangedCallback(int conn_id, int mtu) {
// GATT client-role and GAP events.

const btgatt_scanner_callbacks_t gatt_scanner_callbacks = {
    RegisterScannerCallback,
    ScanResultCallback,
    nullptr,  // batchscan_cfg_storage_cb
    nullptr,  // batchscan_enb_disable_cb
@@ -481,7 +469,7 @@ class BluetoothGattInterfaceImpl : public BluetoothGattInterface {
    return hal_iface_->advertiser;
  }

  const btgatt_scanner_interface_t* GetScannerHALInterface() const override {
  BleScannerInterface* GetScannerHALInterface() const override {
    return hal_iface_->scanner;
  }

@@ -574,12 +562,6 @@ GetServerObservers() {
// Default observer implementations. These are provided so that the methods
// themselves are optional.

void BluetoothGattInterface::ScannerObserver::RegisterScannerCallback(
    BluetoothGattInterface* /* gatt_iface */, int /* status */,
    int /* scanner_id */, const bt_uuid_t& /* app_uuid */) {
  // Do nothing.
}

void BluetoothGattInterface::ScannerObserver::ScanResultCallback(
    BluetoothGattInterface* /* gatt_iface */, const bt_bdaddr_t& /* bda */,
    int /* rssi */,
@@ -807,11 +789,7 @@ bt_status_t BluetoothGattInterface::StartScan(int client_id) {
  // If this is the first scan client, then make a call into the stack. We
  // only do this when the reference count changes to or from 0.
  if (scan_client_set_.empty()) {
    bt_status_t status = GetScannerHALInterface()->scan(true);
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "HAL call to scan failed";
      return status;
    }
    GetScannerHALInterface()->Scan(true);
  }

  scan_client_set_.insert(client_id);
@@ -831,11 +809,7 @@ bt_status_t BluetoothGattInterface::StopScan(int client_id) {
  }

  if (scan_client_set_.size() == 1) {
    bt_status_t status = GetScannerHALInterface()->scan(false);
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "HAL call to stop scan failed";
      return status;
    }
    GetScannerHALInterface()->Scan(false);
  }

  scan_client_set_.erase(iter);
Loading