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

Commit c64feaa5 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge changes I4ac830a6,I85af7a69 am: 3deda653 am: f36ac289 am: 28b78939

parents b81fc3b2 28b78939
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -435,6 +435,27 @@ static jboolean disconnectLeAudioNative(JNIEnv* env, jobject object,
  return JNI_TRUE;
}

static jboolean setEnableStateNative(JNIEnv* env, jobject object,
                                     jbyteArray address, jboolean enabled) {
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  jbyte* addr = env->GetByteArrayElements(address, nullptr);

  if (!sLeAudioClientInterface) {
    LOG(ERROR) << __func__ << ": Failed to get the Bluetooth LeAudio Interface";
    return JNI_FALSE;
  }

  if (!addr) {
    jniThrowIOException(env, EINVAL);
    return JNI_FALSE;
  }

  RawAddress* tmpraw = (RawAddress*)addr;
  sLeAudioClientInterface->SetEnableState(*tmpraw, enabled);
  env->ReleaseByteArrayElements(address, addr, 0);
  return JNI_TRUE;
}

static jboolean groupAddNodeNative(JNIEnv* env, jobject object, jint group_id,
                                   jbyteArray address) {
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
@@ -562,6 +583,7 @@ static JNINativeMethod sMethods[] = {
    {"cleanupNative", "()V", (void*)cleanupNative},
    {"connectLeAudioNative", "([B)Z", (void*)connectLeAudioNative},
    {"disconnectLeAudioNative", "([B)Z", (void*)disconnectLeAudioNative},
    {"setEnableStateNative", "([BZ)Z", (void*)setEnableStateNative},
    {"groupAddNodeNative", "(I[B)Z", (void*)groupAddNodeNative},
    {"groupRemoveNodeNative", "(I[B)Z", (void*)groupRemoveNodeNative},
    {"groupSetActiveNative", "(I)V", (void*)groupSetActiveNative},
+12 −0
Original line number Diff line number Diff line
@@ -255,6 +255,17 @@ public class LeAudioNativeInterface {
        return disconnectLeAudioNative(getByteAddress(device));
    }

    /**
     * Enable/Disable LeAudio for the group.
     *
     * @param device the remote device
     * @param enabled true if enabled, false to disabled
     * @return true on success, otherwise false.
     */
    public boolean setEnableState(BluetoothDevice device, boolean enabled) {
        return setEnableStateNative(getByteAddress(device), enabled);
    }

    /**
     * Add new Node into a group.
     * @param groupId group identifier
@@ -340,6 +351,7 @@ public class LeAudioNativeInterface {
    private native void cleanupNative();
    private native boolean connectLeAudioNative(byte[] address);
    private native boolean disconnectLeAudioNative(byte[] address);
    private native boolean setEnableStateNative(byte[] address, boolean enabled);
    private native boolean groupAddNodeNative(int groupId, byte[] address);
    private native boolean groupRemoveNodeNative(int groupId, byte[] address);
    private native void groupSetActiveNative(int groupId);
+18 −1
Original line number Diff line number Diff line
@@ -510,6 +510,17 @@ public class LeAudioService extends ProfileService {
        return descriptor;
    }

    private void setEnabledState(BluetoothDevice device, boolean enabled) {
        if (DBG) {
            Log.d(TAG, "setEnabledState: address:" + device + " enabled: " + enabled);
        }
        if (!mLeAudioNativeIsInitialized) {
            Log.e(TAG, "setEnabledState, mLeAudioNativeIsInitialized is not initialized");
            return;
        }
        mLeAudioNativeInterface.setEnableState(device, enabled);
    }

    public boolean connect(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "connect(): " + device);
@@ -2421,6 +2432,7 @@ public class LeAudioService extends ProfileService {
        }

        if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            setEnabledState(device, /* enabled = */ true);
            // Authorizes LEA GATT server services if already assigned to a group
            int groupId = getGroupId(device);
            if (groupId != LE_AUDIO_GROUP_ID_INVALID) {
@@ -2428,6 +2440,7 @@ public class LeAudioService extends ProfileService {
            }
            connect(device);
        } else if (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            setEnabledState(device, /* enabled = */ false);
            // Remove authorization for LEA GATT server services
            setAuthorizationForRelatedProfiles(device, false);
            disconnect(device);
@@ -2474,8 +2487,12 @@ public class LeAudioService extends ProfileService {
     * @hide
     */
    public int getConnectionPolicy(BluetoothDevice device) {
        return mDatabaseManager
        int connection_policy = mDatabaseManager
                .getProfileConnectionPolicy(device, BluetoothProfile.LE_AUDIO);
        if (DBG) {
            Log.d(TAG, device + " connection policy = " + connection_policy);
        }
        return connection_policy;
    }

    /**
+1 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ class LeAudioClient {
  virtual void RemoveDevice(const RawAddress& address) = 0;
  virtual void Connect(const RawAddress& address) = 0;
  virtual void Disconnect(const RawAddress& address) = 0;
  virtual void SetEnableState(const RawAddress& address, bool enabled) = 0;
  virtual void GroupAddNode(const int group_id, const RawAddress& addr) = 0;
  virtual void GroupRemoveNode(const int group_id, const RawAddress& addr) = 0;
  virtual void GroupStream(const int group_id, const uint16_t content_type) = 0;
+99 −39
Original line number Diff line number Diff line
@@ -1200,6 +1200,29 @@ class LeAudioClientImpl : public LeAudioClient {
    callbacks_->OnGroupStatus(active_group_id_, GroupStatus::ACTIVE);
  }

  void SetEnableState(const RawAddress& address, bool enabled) override {
    LOG_INFO(" %s: %s", ADDRESS_TO_LOGGABLE_CSTR(address),
             (enabled ? "enabled" : "disabled"));
    auto leAudioDevice = leAudioDevices_.FindByAddress(address);
    if (leAudioDevice == nullptr) {
      LOG_WARN("%s is null", ADDRESS_TO_LOGGABLE_CSTR(address));
      return;
    }

    auto group_id = leAudioDevice->group_id_;
    auto group = aseGroups_.FindById(group_id);
    if (group == nullptr) {
      LOG_WARN("Group %d is not available", group_id);
      return;
    }

    if (enabled) {
      group->Enable(gatt_if_, reconnection_mode_);
    } else {
      group->Disable(gatt_if_);
    }
  }

  void RemoveDevice(const RawAddress& address) override {
    LOG_INFO(": %s ", ADDRESS_TO_LOGGABLE_CSTR(address));
    LeAudioDevice* leAudioDevice = leAudioDevices_.FindByAddress(address);
@@ -1207,6 +1230,10 @@ class LeAudioClientImpl : public LeAudioClient {
      return;
    }

    /* Remove device from the background connect if it is there */
    BTA_GATTC_CancelOpen(gatt_if_, address, false);
    btif_storage_set_leaudio_autoconnect(address, false);

    LOG_INFO("%s, state: %s", ADDRESS_TO_LOGGABLE_CSTR(address),
             bluetooth::common::ToString(leAudioDevice->GetConnectionState())
                 .c_str());
@@ -1224,8 +1251,6 @@ class LeAudioClientImpl : public LeAudioClient {
        [[fallthrough]];
      case DeviceConnectState::DISCONNECTING:
      case DeviceConnectState::DISCONNECTING_AND_RECOVER:
        /* Remove device from the background connect if it is there */
        BTA_GATTC_CancelOpen(gatt_if_, address, false);
        /* Device is disconnecting, just mark it shall be removed after all. */
        leAudioDevice->SetConnectionState(DeviceConnectState::REMOVING);
        return;
@@ -1233,9 +1258,6 @@ class LeAudioClientImpl : public LeAudioClient {
        BTA_GATTC_CancelOpen(gatt_if_, address, true);
        [[fallthrough]];
      case DeviceConnectState::CONNECTING_AUTOCONNECT:
        /* Cancel background conection */
        BTA_GATTC_CancelOpen(gatt_if_, address, false);
        break;
      case DeviceConnectState::DISCONNECTED:
        /* Do nothing, just remove device  */
        break;
@@ -1269,6 +1291,17 @@ class LeAudioClientImpl : public LeAudioClient {

        return;
      }

      if (leAudioDevice->group_id_ != bluetooth::groups::kGroupUnknown) {
        auto group = GetGroupIfEnabled(leAudioDevice->group_id_);
        if (!group) {
          LOG_WARN(" %s, trying to connect to disabled group id %d",
                   ADDRESS_TO_LOGGABLE_CSTR(address), leAudioDevice->group_id_);
          callbacks_->OnConnectionState(ConnectionState::DISCONNECTED, address);
          return;
        }
      }

      leAudioDevice->SetConnectionState(DeviceConnectState::CONNECTING_BY_USER);

      le_audio::MetricsCollector::Get()->OnConnectionStateChanged(
@@ -1400,9 +1433,8 @@ class LeAudioClientImpl : public LeAudioClient {
  }

  void BackgroundConnectIfNeeded(LeAudioDevice* leAudioDevice) {
    DLOG(INFO) << __func__ << ADDRESS_TO_LOGGABLE_STR(leAudioDevice->address_);
    auto group = aseGroups_.FindById(leAudioDevice->group_id_);
    if (!group) {
    auto group = GetGroupIfEnabled(leAudioDevice->group_id_);
    if (group == nullptr) {
      LOG_INFO(" Device %s is not yet part of the group %d. ",
               ADDRESS_TO_LOGGABLE_CSTR(leAudioDevice->address_),
               leAudioDevice->group_id_);
@@ -1451,20 +1483,25 @@ class LeAudioClientImpl : public LeAudioClient {
        break;
      case DeviceConnectState::CONNECTED: {
        /* User is disconnecting the device, we shall remove the autoconnect
         * flag for this device and all others
         * flag for this device and all others if not TA is used
         */
        /* If target announcement is used, do not remove autoconnect
         */
        bool remove_from_autoconnect =
            (reconnection_mode_ != BTM_BLE_BKG_CONNECT_TARGETED_ANNOUNCEMENTS);

        if (leAudioDevice->autoconnect_flag_ && remove_from_autoconnect) {
          LOG_INFO("Removing autoconnect flag for group_id %d",
                   leAudioDevice->group_id_);

        if (leAudioDevice->autoconnect_flag_) {
          /* Removes device from background connect */
          BTA_GATTC_CancelOpen(gatt_if_, address, false);
          btif_storage_set_leaudio_autoconnect(address, false);
          leAudioDevice->autoconnect_flag_ = false;
        }
        auto group = aseGroups_.FindById(leAudioDevice->group_id_);

        if (group) {
        auto group = aseGroups_.FindById(leAudioDevice->group_id_);
        if (group && remove_from_autoconnect) {
          /* Remove devices from auto connect mode */
          for (auto dev = group->GetFirstDevice(); dev;
               dev = group->GetNextDevice(dev)) {
@@ -1824,16 +1861,24 @@ class LeAudioClientImpl : public LeAudioClient {
    LeAudioCharValueHandle(conn_id, hdl, len, value);
  }

  void AddToBackgroundConnectCheckGroupConnected(LeAudioDevice* leAudioDevice) {
    /* If device belongs to streaming group, add it on allow list */
    auto address = leAudioDevice->address_;
    auto group_id = leAudioDevice->group_id_;
  LeAudioDeviceGroup* GetGroupIfEnabled(int group_id) {
    auto group = aseGroups_.FindById(group_id);
    if (group == nullptr) {
      LOG_INFO("Group %d does not exist", group_id);
      return;
      return nullptr;
    }
    if (!group->IsEnabled()) {
      LOG_INFO("Group %d is disabled", group_id);
      return nullptr;
    }
    return group;
  }

  void AddToBackgroundConnectCheckGroupConnected(LeAudioDevice* leAudioDevice) {
    /* If device belongs to streaming group, add it on allow list */
    auto address = leAudioDevice->address_;
    auto group = GetGroupIfEnabled(leAudioDevice->group_id_);

    if (group != nullptr && group->IsAnyDeviceConnected()) {
      LOG_INFO("Group %d in connected state. Adding %s to allow list ",
               leAudioDevice->group_id_, ADDRESS_TO_LOGGABLE_CSTR(address));
@@ -1885,6 +1930,16 @@ class LeAudioClientImpl : public LeAudioClient {
      return;
    }

    if (leAudioDevice->group_id_ != bluetooth::groups::kGroupUnknown) {
      auto group = GetGroupIfEnabled(leAudioDevice->group_id_);
      if (group == nullptr) {
        LOG_WARN(
            "LeAudio profile is disabled for group_id: %d. %s is not connected",
            leAudioDevice->group_id_, ADDRESS_TO_LOGGABLE_CSTR(address));
        return;
      }
    }

    if (controller_get_interface()->supports_ble_2m_phy()) {
      LOG(INFO) << ADDRESS_TO_LOGGABLE_STR(address)
                << " set preferred PHY to 2M";
@@ -1996,13 +2051,12 @@ class LeAudioClientImpl : public LeAudioClient {
  }

  void OnEncryptionComplete(const RawAddress& address, uint8_t status) {
    LOG(INFO) << __func__ << " "
              << ADDRESS_TO_LOGGABLE_STR(address) << "status: " << int{status};

    LOG_INFO("%s status 0x%02x ", ADDRESS_TO_LOGGABLE_CSTR(address), status);
    LeAudioDevice* leAudioDevice = leAudioDevices_.FindByAddress(address);
    if (leAudioDevice == NULL) {
      LOG(WARNING) << "Skipping unknown device"
                   << ADDRESS_TO_LOGGABLE_STR(address);
    if (leAudioDevice == NULL ||
        (leAudioDevice->conn_id_ == GATT_INVALID_CONN_ID)) {
      LOG_WARN("Skipping device which is %s",
               (leAudioDevice ? " not connected by service." : " null"));
      return;
    }

@@ -2074,7 +2128,7 @@ class LeAudioClientImpl : public LeAudioClient {
  }

  void scheduleGroupConnectedCheck(int group_id) {
    LOG_INFO("Schedule group_id %d streaming check.", group_id);
    LOG_INFO("Schedule group_id %d connected check.", group_id);
    do_in_main_thread_delayed(
        FROM_HERE,
        base::BindOnce(
@@ -2101,9 +2155,15 @@ class LeAudioClientImpl : public LeAudioClient {
      return;
    }

    auto group = GetGroupIfEnabled(leAudioDevice->group_id_);

    if (group != nullptr) {
      leAudioDevice->SetConnectionState(
          DeviceConnectState::CONNECTING_AUTOCONNECT);
      BTA_GATTC_Open(gatt_if_, address, BTM_BLE_DIRECT_CONNECTION, false);
    } else {
      leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTED);
    }
  }

  void scheduleRecoveryReconnect(RawAddress& address) {
@@ -2123,7 +2183,7 @@ class LeAudioClientImpl : public LeAudioClient {

  void OnGattDisconnected(uint16_t conn_id, tGATT_IF client_if,
                          RawAddress address, tGATT_DISCONN_REASON reason) {
    LeAudioDevice* leAudioDevice = leAudioDevices_.FindByAddress(address);
    LeAudioDevice* leAudioDevice = leAudioDevices_.FindByConnId(conn_id);

    if (!leAudioDevice) {
      LOG(ERROR) << ", skipping unknown leAudioDevice, address: "
@@ -2178,8 +2238,9 @@ class LeAudioClientImpl : public LeAudioClient {
     * or if autoconnect is set and device got disconnected because of some
     * issues
     */
    if (group == nullptr) {
      LOG_ERROR("Group id %d is null", leAudioDevice->group_id_);
    if (group == nullptr || !group->IsEnabled()) {
      LOG_ERROR("Group id %d (%p) disabled or null", leAudioDevice->group_id_,
                group);
      leAudioDevice->SetConnectionState(DeviceConnectState::DISCONNECTED);
      return;
    }
@@ -2279,9 +2340,9 @@ class LeAudioClientImpl : public LeAudioClient {

  void OnServiceChangeEvent(const RawAddress& address) {
    LeAudioDevice* leAudioDevice = leAudioDevices_.FindByAddress(address);
    if (!leAudioDevice) {
      LOG_WARN("Skipping unknown leAudioDevice %s",
               ADDRESS_TO_LOGGABLE_CSTR(address));
    if (!leAudioDevice || (leAudioDevice->conn_id_ == GATT_INVALID_CONN_ID)) {
      LOG_WARN("Skipping unknown leAudioDevice %s (%p)",
               ADDRESS_TO_LOGGABLE_CSTR(address), leAudioDevice);
      return;
    }
    ClearDeviceInformationAndStartSearch(leAudioDevice);
@@ -2314,10 +2375,9 @@ class LeAudioClientImpl : public LeAudioClient {

  void OnGattServiceDiscoveryDone(const RawAddress& address) {
    LeAudioDevice* leAudioDevice = leAudioDevices_.FindByAddress(address);
    if (!leAudioDevice) {
      DLOG(ERROR) << __func__
                  << ", skipping unknown leAudioDevice, address: "
                  << ADDRESS_TO_LOGGABLE_STR(address);
    if (!leAudioDevice || (leAudioDevice->conn_id_ == GATT_INVALID_CONN_ID)) {
      LOG_VERBOSE("skipping unknown leAudioDevice, address %s (%p) ",
                  ADDRESS_TO_LOGGABLE_CSTR(address), leAudioDevice);
      return;
    }

Loading