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

Commit 0aa69dab authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Expose Bluetooth 5.0 properties to JNI

Bug: 30622771
Test: sl4a ConcurrentBleAdvertisingTest
Change-Id: I6185e6926e7363824aa573c7d65f7ab66f954e52
parent d4246efc
Loading
Loading
Loading
Loading
+19 −15
Original line number Diff line number Diff line
@@ -61,14 +61,6 @@ static inline OwnedArrayWrapper<T> OwnedArray(T* o) {
  return OwnedArrayWrapper<T>(o);
}

/* return the actual power in dBm based on the mapping in config file */
int8_t ble_tx_power[BTM_BLE_ADV_TX_POWER_MAX + 1] = BTM_BLE_ADV_TX_POWER;
int8_t ble_map_adv_tx_power(int tx_power_index) {
  if (0 <= tx_power_index && tx_power_index < BTM_BLE_ADV_TX_POWER_MAX)
    return (int8_t)ble_tx_power[tx_power_index];
  return 0;
}

class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface {
  ~BleAdvertiserInterfaceImpl(){};

@@ -102,16 +94,23 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface {
    do_in_jni_thread(Bind(cb, status));
  }

  virtual void SetParameters(int advertiser_id, int min_interval,
                             int max_interval, int adv_type, int chnl_map,
                             int tx_power, Callback cb) {
  void SetParameters(uint8_t advertiser_id,
                     uint16_t advertising_event_properties,
                     uint32_t min_interval, uint32_t max_interval, int chnl_map,
                     int tx_power, uint8_t primary_advertising_phy,
                     uint8_t secondary_advertising_phy,
                     uint8_t scan_request_notification_enable, Callback cb) {
    tBTM_BLE_ADV_PARAMS* params = new tBTM_BLE_ADV_PARAMS;

    params->advertising_event_properties = advertising_event_properties;
    params->adv_int_min = min_interval;
    params->adv_int_max = max_interval;
    params->adv_type = adv_type;
    params->channel_map = chnl_map;
    params->adv_filter_policy = 0;
    params->tx_power = ble_map_adv_tx_power(tx_power);
    params->tx_power = tx_power;
    params->primary_advertising_phy = primary_advertising_phy;
    params->secondary_advertising_phy = secondary_advertising_phy;
    params->scan_request_notification_enable = scan_request_notification_enable;

    do_in_bta_thread(FROM_HERE,
                     Bind(&BleAdvertisingManager::SetParameters,
@@ -166,12 +165,17 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface {
    VLOG(1) << __func__;

    tBTM_BLE_ADV_PARAMS* p_params = new tBTM_BLE_ADV_PARAMS;
    p_params->advertising_event_properties =
        params.advertising_event_properties;
    p_params->adv_int_min = params.min_interval;
    p_params->adv_int_max = params.max_interval;
    p_params->adv_type = params.adv_type;
    p_params->channel_map = params.channel_map;
    p_params->adv_filter_policy = 0;
    p_params->tx_power = ble_map_adv_tx_power(params.tx_power);
    p_params->tx_power = params.tx_power;
    p_params->primary_advertising_phy = params.primary_advertising_phy;
    p_params->secondary_advertising_phy = params.secondary_advertising_phy;
    p_params->scan_request_notification_enable =
        params.scan_request_notification_enable;

    do_in_bta_thread(
        FROM_HERE, Bind(&BleAdvertisingManager::StartAdvertising,
+4 −4
Original line number Diff line number Diff line
@@ -93,11 +93,11 @@ const int kAdvertisingIntervalLowMs = 100;
// the Max. Adv. Interval. (See http://b/24344075).
const int kAdvertisingIntervalDeltaUnit = 10;

// Advertising types (ADV_IND, ADV_SCAN_IND, etc.) that are exposed to
// Legacy Advertising types (ADV_IND, ADV_SCAN_IND, etc.) that are exposed to
// applications.
const int kAdvertisingEventTypeConnectable = 0;
const int kAdvertisingEventTypeScannable = 2;
const int kAdvertisingEventTypeNonConnectable = 3;
const uint16_t kAdvertisingEventTypeLegacyConnectable = 0x0011;
const uint16_t kAdvertisingEventTypeLegacyScannable = 0x0012;
const uint16_t kAdvertisingEventTypeLegacyNonConnectable = 0x0010;

// Advertising channels. These should be kept the same as those defined in the
// stack.
+35 −3
Original line number Diff line number Diff line
@@ -64,6 +64,29 @@ int GetAdvertisingIntervalUnit(AdvertiseSettings::Mode mode) {
  return (ms * 1000) / 625;
}

int8_t GetAdvertisingTxPower(AdvertiseSettings::TxPowerLevel tx_power) {
  int8_t power;

  switch (tx_power) {
    case AdvertiseSettings::TX_POWER_LEVEL_ULTRA_LOW:
      power = -21;
      break;
    case AdvertiseSettings::TX_POWER_LEVEL_LOW:
      power = -15;
      break;
    case AdvertiseSettings::TX_POWER_LEVEL_MEDIUM:
      power = -7;
      break;
    case AdvertiseSettings::TX_POWER_LEVEL_HIGH:
    // Fall through
    default:
      power = 1;
      break;
  }

  return power;
}

void GetAdvertiseParams(const AdvertiseSettings& settings, bool has_scan_rsp,
                        AdvertiseParameters* out_params) {
  CHECK(out_params);
@@ -73,13 +96,22 @@ void GetAdvertiseParams(const AdvertiseSettings& settings, bool has_scan_rsp,
      out_params->min_interval + kAdvertisingIntervalDeltaUnit;

  if (settings.connectable())
    out_params->adv_type = kAdvertisingEventTypeConnectable;
    out_params->advertising_event_properties =
        kAdvertisingEventTypeLegacyConnectable;
  else if (has_scan_rsp)
    out_params->adv_type = kAdvertisingEventTypeScannable;
    out_params->advertising_event_properties =
        kAdvertisingEventTypeLegacyScannable;
  else
    out_params->adv_type = kAdvertisingEventTypeNonConnectable;
    out_params->advertising_event_properties =
        kAdvertisingEventTypeLegacyNonConnectable;

  out_params->channel_map = kAdvertisingChannelAll;
  out_params->tx_power = GetAdvertisingTxPower(settings.tx_power_level());

  // TODO: expose those new setting through AdvertiseSettings
  out_params->primary_advertising_phy = 0x01;
  out_params->secondary_advertising_phy = 0x01;
  out_params->scan_request_notification_enable = 0;
}

void DoNothing(uint8_t status) {}
+7 −3
Original line number Diff line number Diff line
@@ -49,9 +49,13 @@ class MockAdvertiserHandler : public BleAdvertiserInterface {
               void(base::Callback<void(uint8_t /* advertiser_id */,
                                        uint8_t /* status */)>));
  MOCK_METHOD1(Unregister, void(uint8_t));
  MOCK_METHOD7(SetParameters,
               void(int advertiser_id, int min_interval, int max_interval,
                    int adv_type, int chnl_map, int tx_power, Callback cb));
  MOCK_METHOD10(SetParameters,
                void(uint8_t advertiser_id,
                     uint16_t advertising_event_properties,
                     uint32_t min_interval, uint32_t max_interval, int chnl_map,
                     int tx_power, uint8_t primary_advertising_phy,
                     uint8_t secondary_advertising_phy,
                     uint8_t scan_request_notification_enable, Callback cb));
  MOCK_METHOD4(SetData, void(int advertiser_id, bool set_scan_rsp,
                             std::vector<uint8_t> data, Callback cb));
  MOCK_METHOD5(Enable, void(uint8_t advertiser_id, bool enable, Callback cb,
+23 −9
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ using multiadv_cb = base::Callback<void(uint8_t /* status */)>;
struct AdvertisingInstance {
  uint8_t inst_id;
  bool in_use;
  uint8_t adv_evt;
  uint8_t advertising_event_properties;
  BD_ADDR rpa;
  alarm_t* adv_raddr_timer;
  int8_t tx_power;
@@ -47,7 +47,7 @@ struct AdvertisingInstance {
  AdvertisingInstance(int inst_id)
      : inst_id(inst_id),
        in_use(false),
        adv_evt(0),
        advertising_event_properties(0),
        rpa{0},
        tx_power(0),
        timeout_s(0),
@@ -82,6 +82,14 @@ void btm_ble_multi_adv_gen_rpa_cmpl(tBTM_RAND_ENC* p) {

void btm_ble_adv_raddr_timer_timeout(void* data);

bool is_legacy_connectable(uint16_t advertising_event_properties) {
  if (((advertising_event_properties & 0x10) != 0) &&
      ((advertising_event_properties & 0x01) != 0)) {
    return true;
  }
  return false;
}

class BleAdvertisingManagerImpl
    : public BleAdvertisingManager,
      public BleAdvertiserHciInterface::AdvertisingEventObserver {
@@ -316,15 +324,18 @@ class BleAdvertisingManagerImpl
             BD_ADDR_LEN);
    }

    p_inst->adv_evt = p_params->adv_type;
    p_inst->advertising_event_properties =
        p_params->advertising_event_properties;
    p_inst->tx_power = p_params->tx_power;
    BD_ADDR peer_address = {0, 0, 0, 0, 0, 0};

    GetHciInterface()->SetParameters(
        p_inst->inst_id, p_params->adv_type, p_params->adv_int_min,
        p_params->adv_int_max, p_params->channel_map, own_address_type, 0x00,
        peer_address, p_params->adv_filter_policy, p_inst->tx_power, 0x01, 0x01,
        0x01, 0x00, 0x00, cb);
        p_inst->inst_id, p_params->advertising_event_properties,
        p_params->adv_int_min, p_params->adv_int_max, p_params->channel_map,
        own_address_type, 0x00, peer_address, p_params->adv_filter_policy,
        p_inst->tx_power, p_params->primary_advertising_phy, 0x01, 0x01,
        p_params->secondary_advertising_phy,
        p_params->scan_request_notification_enable, cb);

    // TODO: re-enable only if it was enabled, properly call
    // SetParamsCallback
@@ -343,7 +354,8 @@ class BleAdvertisingManagerImpl
    AdvertisingInstance* p_inst = &adv_inst[inst_id];
    VLOG(1) << "is_scan_rsp = " << is_scan_rsp;

    if (!is_scan_rsp && p_inst->adv_evt != BTM_BLE_NON_CONNECT_EVT) {
    if (!is_scan_rsp &&
        is_legacy_connectable(p_inst->advertising_event_properties)) {
      uint8_t flags_val = BTM_GENERAL_DISCOVERABLE;

      if (p_inst->timeout_s) flags_val = BTM_LIMITED_DISCOVERABLE;
@@ -415,7 +427,9 @@ class BleAdvertisingManagerImpl
    if (p_inst->in_use == true) {
      // TODO(jpawlowski): we don't really allow to do directed advertising
      // right now. This should probably be removed, check with Andre.
      if (p_inst->adv_evt != BTM_BLE_CONNECT_DIR_EVT) {
      if ((p_inst->advertising_event_properties & 0x0C) ==
          0 /* directed advertising bits not set
      */) {
        GetHciInterface()->Enable(true, advertising_handle, 0x00, 0x00,
                                  Bind(DoNothing));
      } else {
Loading