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

Commit 187ce75c authored by Jakub Tyszkowski's avatar Jakub Tyszkowski Committed by Jakub Pawlowski
Browse files

btm: Add a way for CSIS to register scan callback

When registered, callback will be called whenever any scanning is ongoing.
It will be usefull for native service (CSIS) wanting to hook into scanning
performed by the Settings application. CSIS can also start or stop the
scan procedure with respect to the main scanner. It will not interrupt nor
change the scan duration if the main scanner is already running.

Bug: 150670922
Tag: #feature
Test: compile & manual testing
Sponsor: jpawlowski@
Change-Id: Ic0cc508ec5881d0c84dfd919ddb38618164ffbeb
parent 87aaa652
Loading
Loading
Loading
Loading
+99 −13
Original line number Diff line number Diff line
@@ -3304,6 +3304,57 @@ static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
  }
}

/*******************************************************************************
 *
 * Function         bta_dm_opportunistic_observe_results_cb
 *
 * Description      Callback for BLE Observe result
 *
 *
 * Returns          void
 *
 ******************************************************************************/
static void bta_dm_opportunistic_observe_results_cb(tBTM_INQ_RESULTS* p_inq,
                                                    uint8_t* p_eir,
                                                    uint16_t eir_len) {
  tBTA_DM_SEARCH result;
  tBTM_INQ_INFO* p_inq_info;

  result.inq_res.bd_addr = p_inq->remote_bd_addr;
  result.inq_res.rssi = p_inq->rssi;
  result.inq_res.ble_addr_type = p_inq->ble_addr_type;
  result.inq_res.inq_result_type = p_inq->inq_result_type;
  result.inq_res.device_type = p_inq->device_type;
  result.inq_res.flag = p_inq->flag;
  result.inq_res.ble_evt_type = p_inq->ble_evt_type;
  result.inq_res.ble_primary_phy = p_inq->ble_primary_phy;
  result.inq_res.ble_secondary_phy = p_inq->ble_secondary_phy;
  result.inq_res.ble_advertising_sid = p_inq->ble_advertising_sid;
  result.inq_res.ble_tx_power = p_inq->ble_tx_power;
  result.inq_res.ble_periodic_adv_int = p_inq->ble_periodic_adv_int;

  /* application will parse EIR to find out remote device name */
  result.inq_res.p_eir = p_eir;
  result.inq_res.eir_len = eir_len;

  p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr);
  if (p_inq_info != NULL) {
    /* initialize remt_name_not_required to false so that we get the name by
     * default */
    result.inq_res.remt_name_not_required = false;
  }

  if (bta_dm_search_cb.p_csis_scan_cback)
    bta_dm_search_cb.p_csis_scan_cback(BTA_DM_INQ_RES_EVT, &result);

  if (p_inq_info) {
    /* application indicates if it knows the remote name, inside the callback
     copy that to the inquiry data base*/
    if (result.inq_res.remt_name_not_required)
      p_inq_info->appl_knows_rem_name = true;
  }
}

/*******************************************************************************
 *
 * Function         bta_dm_observe_cmpl_cb
@@ -3323,6 +3374,9 @@ static void bta_dm_observe_cmpl_cb(void* p_result) {
  if (bta_dm_search_cb.p_scan_cback) {
    bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
  }
  if (bta_dm_search_cb.p_csis_scan_cback) {
    bta_dm_search_cb.p_csis_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
  }
}

static void ble_io_req(const RawAddress& bd_addr, tBTM_IO_CAP* p_io_cap,
@@ -3642,33 +3696,65 @@ void bta_dm_ble_config_local_privacy(bool privacy_enable) {
}
#endif

void bta_dm_ble_observe(bool start, uint8_t duration,
                        tBTA_DM_SEARCH_CBACK* p_cback) {
  if (!start) {
    bta_dm_search_cb.p_scan_cback = NULL;
    BTM_BleObserve(false, 0, NULL, NULL);
    return;
  }
static void bta_dm_start_scan(uint8_t duration_sec) {
  tBTM_STATUS status = BTM_BleObserve(
      true, duration_sec, bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb);

  /*Save the  callback to be called when a scan results are available */
  bta_dm_search_cb.p_scan_cback = p_cback;
  tBTM_STATUS status = BTM_BleObserve(true, duration, bta_dm_observe_results_cb,
                                      bta_dm_observe_cmpl_cb);
  if (status != BTM_CMD_STARTED) {
    APPL_TRACE_WARNING(" %s BTM_BleObserve  failed. status %d", __func__,
                       status);
    tBTA_DM_SEARCH data = {
        .inq_cmpl =
            {
                .num_resps = 0,
            },
    };
    APPL_TRACE_WARNING(" %s BTM_BleObserve  failed. status %d", __func__,
                       status);
    if (bta_dm_search_cb.p_scan_cback) {
      bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
    }
    if (bta_dm_search_cb.p_csis_scan_cback) {
      bta_dm_search_cb.p_csis_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
    }
  }
}

void bta_dm_ble_observe(bool start, uint8_t duration,
                        tBTA_DM_SEARCH_CBACK* p_cback) {
  if (!start) {
    bta_dm_search_cb.p_scan_cback = NULL;
    BTM_BleObserve(false, 0, NULL, NULL);
    return;
  }

  /*Save the  callback to be called when a scan results are available */
  bta_dm_search_cb.p_scan_cback = p_cback;
  bta_dm_start_scan(duration);
}

void bta_dm_ble_scan(bool start, uint8_t duration_sec) {
  /* Start or stop only if there is no active main scanner */
  if (bta_dm_search_cb.p_scan_cback != NULL) return;

  if (!start) {
    BTM_BleObserve(false, 0, NULL, NULL);
    return;
  }

  bta_dm_start_scan(duration_sec);
}

void bta_dm_ble_csis_observe(bool observe, tBTA_DM_SEARCH_CBACK* p_cback) {
  if (!observe) {
    bta_dm_search_cb.p_csis_scan_cback = NULL;
    BTM_BleOpportunisticObserve(false, NULL);
    return;
  }

  /* Save the callback to be called when a scan results are available */
  bta_dm_search_cb.p_csis_scan_cback = p_cback;
  BTM_BleOpportunisticObserve(true, bta_dm_opportunistic_observe_results_cb);
}

/** This function set the maximum transmission packet size */
void bta_dm_ble_set_data_length(const RawAddress& bd_addr) {
  const controller_t* controller = controller_get_interface();
+39 −0
Original line number Diff line number Diff line
@@ -646,6 +646,45 @@ extern void BTA_DmBleObserve(bool start, uint8_t duration,
      FROM_HERE, base::Bind(bta_dm_ble_observe, start, duration, p_results_cb));
}

/*******************************************************************************
 *
 * Function         BTA_DmBleScan
 *
 * Description      Start or stop the scan procedure if it's not already started
 *                  with BTA_DmBleObserve().
 *
 * Parameters       start: start or stop the scan procedure,
 *                  duration_sec: Duration of the scan. Continuous scan if 0 is
 *                                passed,
 *
 * Returns          void
 *
 ******************************************************************************/
extern void BTA_DmBleScan(bool start, uint8_t duration_sec) {
  APPL_TRACE_API("%s:start = %d ", __func__, start);
  do_in_main_thread(FROM_HERE,
                    base::Bind(bta_dm_ble_scan, start, duration_sec));
}

/*******************************************************************************
 *
 * Function         BTA_DmBleCsisObserve
 *
 * Description      This procedure keeps the external observer listening for
 *                  advertising events from a CSIS grouped device.
 *
 * Parameters       observe: enable or disable passive observe,
 *                  p_results_cb: Callback to be called with scan results,
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_DmBleCsisObserve(bool observe, tBTA_DM_SEARCH_CBACK* p_results_cb) {
  APPL_TRACE_API("%s:enable = %d ", __func__, observe);
  do_in_main_thread(FROM_HERE,
                    base::Bind(bta_dm_ble_csis_observe, observe, p_results_cb));
}

/*******************************************************************************
 *
 * Function         BTA_VendorInit
+3 −0
Original line number Diff line number Diff line
@@ -369,6 +369,7 @@ typedef struct {
  uint8_t peer_scn;
  tBT_TRANSPORT transport;
  tBTA_DM_SEARCH_CBACK* p_scan_cback;
  tBTA_DM_SEARCH_CBACK* p_csis_scan_cback;
  tGATT_IF client_if;
  uint8_t uuid_to_search;
  bool gatt_disc_active;
@@ -505,6 +506,8 @@ extern void bta_dm_ble_set_conn_params(const RawAddress&, uint16_t, uint16_t,
                                       uint16_t, uint16_t);
extern void bta_dm_close_gatt_conn(tBTA_DM_MSG* p_data);
extern void bta_dm_ble_observe(bool, uint8_t, tBTA_DM_SEARCH_CBACK*);
extern void bta_dm_ble_scan(bool, uint8_t);
extern void bta_dm_ble_csis_observe(bool, tBTA_DM_SEARCH_CBACK*);
extern void bta_dm_ble_update_conn_params(const RawAddress&, uint16_t, uint16_t,
                                          uint16_t, uint16_t, uint16_t,
                                          uint16_t);
+32 −0
Original line number Diff line number Diff line
@@ -1090,6 +1090,38 @@ extern void BTA_DmSetEncryption(const RawAddress& bd_addr,
extern void BTA_DmBleObserve(bool start, uint8_t duration,
                             tBTA_DM_SEARCH_CBACK* p_results_cb);

/*******************************************************************************
 *
 * Function         BTA_DmBleScan
 *
 * Description      Start or stop the scan procedure if it's not already started
 *                  with BTA_DmBleObserve().
 *
 * Parameters       start: start or stop the scan procedure,
 *                  duration_sec: Duration of the scan. Continuous scan if 0 is
 *                                passed,
 *
 * Returns          void
 *
 ******************************************************************************/
extern void BTA_DmBleScan(bool start, uint8_t duration);

/*******************************************************************************
 *
 * Function         BTA_DmBleCsisObserve
 *
 * Description      This procedure keeps the external observer listening for
 *                  advertising events from a CSIS grouped device.
 *
 * Parameters       observe: enable or disable passive observe,
 *                  p_results_cb: Callback to be called with scan results,
 *
 * Returns          void
 *
 ******************************************************************************/
extern void BTA_DmBleCsisObserve(bool observe,
                                 tBTA_DM_SEARCH_CBACK* p_results_cb);

/*******************************************************************************
 *
 * Function         BTA_DmBleConfigLocalPrivacy
+6 −0
Original line number Diff line number Diff line
@@ -142,4 +142,10 @@ void BTA_DmBleObserve(bool start, uint8_t duration,
                      tBTA_DM_SEARCH_CBACK* p_results_cb) {
  mock_function_count_map[__func__]++;
}
void BTA_DmBleScan(bool start, uint8_t duration) {
  mock_function_count_map[__func__]++;
}
void BTA_DmBleCsisObserve(bool observe, tBTA_DM_SEARCH_CBACK* p_results_cb) {
  mock_function_count_map[__func__]++;
}
void BTA_VendorInit(void) { mock_function_count_map[__func__]++; }