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

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

Merge changes I788bb019,Id5f7310c am: c7e53599

parents 60171035 c7e53599
Loading
Loading
Loading
Loading
+32 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.pandora

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothDevice.BOND_BONDED
import android.bluetooth.BluetoothDevice.TRANSPORT_LE
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothProfile
@@ -84,9 +85,9 @@ class Host(private val context: Context, private val server: Server) : HostImplB
    Log.i(TAG, "rebootBluetooth")

    val stateFlow =
      flow.filter { it.getAction() == BluetoothAdapter.ACTION_STATE_CHANGED }.map {
        it.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
      }
      flow
        .filter { it.getAction() == BluetoothAdapter.ACTION_STATE_CHANGED }
        .map { it.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) }

    if (bluetoothAdapter.isEnabled) {
      bluetoothAdapter.disable()
@@ -160,20 +161,30 @@ class Host(private val context: Context, private val server: Server) : HostImplB
    }
  }

  private suspend fun waitConnectionIntent(bluetoothDevice: BluetoothDevice) {
    Log.i(TAG, "waitConnectionIntent: device=$bluetoothDevice")
    flow
      .filter { it.action == BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED }
      .filter { it.getBluetoothDeviceExtra() == bluetoothDevice }
      .map { it.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.ERROR) }
      .filter { it == BluetoothAdapter.STATE_CONNECTED }
      .first()
  }

  private suspend fun waitBondIntent(bluetoothDevice: BluetoothDevice) {
    // We only wait for bonding to be completed since we only need the ACL connection to be
    // established with the peer device (on Android state connected is sent when all profiles
    // have been connected).
    Log.i(TAG, "waitBondIntent: device=$bluetoothDevice")
    flow
      .filter { it.getAction() == BluetoothDevice.ACTION_BOND_STATE_CHANGED }
      .filter { it.action == BluetoothDevice.ACTION_BOND_STATE_CHANGED }
      .filter { it.getBluetoothDeviceExtra() == bluetoothDevice }
      .map { it.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothAdapter.ERROR) }
      .filter { it == BluetoothDevice.BOND_BONDED }
      .filter { it == BOND_BONDED }
      .first()
  }

  private suspend fun waitConnectionIntent(bluetoothDevice: BluetoothDevice) {
  private suspend fun acceptPairingAndAwaitBonded(bluetoothDevice: BluetoothDevice) {
    val acceptPairingJob = scope.launch { waitPairingRequestIntent(bluetoothDevice) }
    waitBondIntent(bluetoothDevice)
    if (acceptPairingJob.isActive) {
@@ -185,7 +196,7 @@ class Host(private val context: Context, private val server: Server) : HostImplB
    request: WaitConnectionRequest,
    responseObserver: StreamObserver<WaitConnectionResponse>
  ) {
    grpcUnary<WaitConnectionResponse>(scope, responseObserver) {
    grpcUnary(scope, responseObserver) {
      val bluetoothDevice = request.address.toBluetoothDevice(bluetoothAdapter)

      Log.i(TAG, "waitConnection: device=$bluetoothDevice")
@@ -195,7 +206,7 @@ class Host(private val context: Context, private val server: Server) : HostImplB
        throw Status.UNKNOWN.asException()
      }

      waitConnectionIntent(bluetoothDevice)
      acceptPairingAndAwaitBonded(bluetoothDevice)

      WaitConnectionResponse.newBuilder()
        .setConnection(
@@ -208,14 +219,21 @@ class Host(private val context: Context, private val server: Server) : HostImplB
  }

  override fun connect(request: ConnectRequest, responseObserver: StreamObserver<ConnectResponse>) {
    grpcUnary<ConnectResponse>(scope, responseObserver) {
    grpcUnary(scope, responseObserver) {
      val bluetoothDevice = request.address.toBluetoothDevice(bluetoothAdapter)

      Log.i(TAG, "connect: address=$bluetoothDevice")

      if (!bluetoothDevice.isConnected()) {
        bluetoothDevice.createBond()
        if (bluetoothDevice.bondState == BOND_BONDED) {
          // already bonded, just reconnect
          bluetoothDevice.connect()
          waitConnectionIntent(bluetoothDevice)
        } else {
          // need to bond
          bluetoothDevice.createBond()
          acceptPairingAndAwaitBonded(bluetoothDevice)
        }
      }

      ConnectResponse.newBuilder()
+25 −1
Original line number Diff line number Diff line
@@ -24,7 +24,9 @@ AclConnection::AclConnection(AddressWithType address,
    : address_(address),
      own_address_(own_address),
      resolved_address_(resolved_address),
      type_(phy_type) {}
      type_(phy_type),
      last_packet_timestamp_(std::chrono::steady_clock::now()),
      timeout_(std::chrono::seconds(1)) {}

void AclConnection::Encrypt() { encrypted_ = true; };

@@ -58,4 +60,26 @@ bluetooth::hci::Role AclConnection::GetRole() const { return role_; };

void AclConnection::SetRole(bluetooth::hci::Role role) { role_ = role; }

void AclConnection::ResetLinkTimer() {
  last_packet_timestamp_ = std::chrono::steady_clock::now();
}

std::chrono::steady_clock::duration AclConnection::TimeUntilNearExpiring()
    const {
  return (last_packet_timestamp_ + timeout_ / 2) -
         std::chrono::steady_clock::now();
}

bool AclConnection::IsNearExpiring() const {
  return TimeUntilNearExpiring() < std::chrono::steady_clock::duration::zero();
}

std::chrono::steady_clock::duration AclConnection::TimeUntilExpired() const {
  return (last_packet_timestamp_ + timeout_) - std::chrono::steady_clock::now();
}

bool AclConnection::HasExpired() const {
  return TimeUntilExpired() < std::chrono::steady_clock::duration::zero();
}

}  // namespace rootcanal
+13 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#pragma once

#include <chrono>
#include <cstdint>

#include "hci/address_with_type.h"
@@ -57,6 +58,16 @@ class AclConnection {

  void SetRole(bluetooth::hci::Role role);

  void ResetLinkTimer();

  std::chrono::steady_clock::duration TimeUntilNearExpiring() const;

  bool IsNearExpiring() const;

  std::chrono::steady_clock::duration TimeUntilExpired() const;

  bool HasExpired() const;

 private:
  AddressWithType address_;
  AddressWithType own_address_;
@@ -67,6 +78,8 @@ class AclConnection {
  bool encrypted_{false};
  uint16_t link_policy_settings_{0};
  bluetooth::hci::Role role_{bluetooth::hci::Role::CENTRAL};
  std::chrono::steady_clock::time_point last_packet_timestamp_;
  std::chrono::steady_clock::duration timeout_;
};

}  // namespace rootcanal
+22 −0
Original line number Diff line number Diff line
@@ -564,4 +564,26 @@ std::vector<uint16_t> AclConnectionHandler::GetAclHandles() const {
  return keys;
}

void AclConnectionHandler::ResetLinkTimer(uint16_t handle) {
  acl_connections_.at(handle).ResetLinkTimer();
}

std::chrono::steady_clock::duration
AclConnectionHandler::TimeUntilLinkNearExpiring(uint16_t handle) const {
  return acl_connections_.at(handle).TimeUntilNearExpiring();
}

bool AclConnectionHandler::IsLinkNearExpiring(uint16_t handle) const {
  return acl_connections_.at(handle).IsNearExpiring();
}

std::chrono::steady_clock::duration AclConnectionHandler::TimeUntilLinkExpired(
    uint16_t handle) const {
  return acl_connections_.at(handle).TimeUntilExpired();
}

bool AclConnectionHandler::HasLinkExpired(uint16_t handle) const {
  return acl_connections_.at(handle).HasExpired();
}

}  // namespace rootcanal
+8 −0
Original line number Diff line number Diff line
@@ -130,6 +130,14 @@ class AclConnectionHandler {

  std::vector<uint16_t> GetAclHandles() const;

  void ResetLinkTimer(uint16_t handle);
  std::chrono::steady_clock::duration TimeUntilLinkNearExpiring(
      uint16_t handle) const;
  bool IsLinkNearExpiring(uint16_t handle) const;
  std::chrono::steady_clock::duration TimeUntilLinkExpired(
      uint16_t handle) const;
  bool HasLinkExpired(uint16_t handle) const;

 private:
  std::unordered_map<uint16_t, AclConnection> acl_connections_;
  std::unordered_map<uint16_t, ScoConnection> sco_connections_;
Loading