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

Commit 2e85cfec authored by Jakub Pawłowski's avatar Jakub Pawłowski Committed by Gerrit Code Review
Browse files

Merge "vc: Simplify the connection logic"

parents 722a55ae c853a2bc
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -178,6 +178,7 @@ bool VolumeControlDevice::UpdateHandles(void) {
      vcs_found = set_volume_control_service_handles(service);
      if (!vcs_found) break;

      known_service_handles_ = true;
      for (auto const& included : service.included_services) {
        const gatt::Service* service =
            BTA_GATTC_GetOwningService(connection_id, included.start_handle);
@@ -198,6 +199,7 @@ bool VolumeControlDevice::UpdateHandles(void) {
}

void VolumeControlDevice::ResetHandles(void) {
  known_service_handles_ = false;
  device_ready = false;

  // the handles are not valid, so discard pending GATT operations
+10 −16
Original line number Diff line number Diff line
@@ -35,16 +35,11 @@ namespace internal {
class VolumeControlDevice : public bluetooth::common::IRedactableLoggable {
 public:
  RawAddress address;
  /* This is true only during first connection to profile, until we store the
   * device
   */
  bool first_connection;

  /* we are making active attempt to connect to this device, 'direct connect'.
   * This is true only during initial phase of first connection. */
  /* We are making active attempt to connect to this device */
  bool connecting_actively;

  bool service_changed_rcvd;
  bool known_service_handles_;

  uint8_t volume;
  uint8_t change_counter;
@@ -62,14 +57,14 @@ class VolumeControlDevice : public bluetooth::common::IRedactableLoggable {

  VolumeOffsets audio_offsets;

  bool device_ready; /* Set when device read server status and registgered for
                        notifications */
  /* Set when device successfully reads server status and registers for
   * notifications */
  bool device_ready;

  VolumeControlDevice(const RawAddress& address, bool first_connection)
  VolumeControlDevice(const RawAddress& address, bool connecting_actively)
      : address(address),
        first_connection(first_connection),
        connecting_actively(first_connection),
        service_changed_rcvd(false),
        connecting_actively(connecting_actively),
        known_service_handles_(false),
        volume(0),
        change_counter(0),
        mute(false),
@@ -109,7 +104,6 @@ class VolumeControlDevice : public bluetooth::common::IRedactableLoggable {
           << "    mute: " << +mute << "\n"
           << "    flags: " << +flags << "\n"
           << "    device read: " << device_ready << "\n"
           << "    first_connection_: " << first_connection << "\n"
           << "    connecting_actively_: " << connecting_actively << "\n";

    dprintf(fd, "%s", stream.str().c_str());
@@ -170,10 +164,10 @@ class VolumeControlDevice : public bluetooth::common::IRedactableLoggable {

class VolumeControlDevices {
 public:
  void Add(const RawAddress& address, bool first_connection) {
  void Add(const RawAddress& address, bool connecting_actively) {
    if (FindByAddress(address) != nullptr) return;

    devices_.emplace_back(address, first_connection);
    devices_.emplace_back(address, connecting_actively);
  }

  void Remove(const RawAddress& address) {
+12 −26
Original line number Diff line number Diff line
@@ -179,10 +179,7 @@ class VolumeControlImpl : public VolumeControl {
      // If the encryption failed, do not remove the device.
      // Disconnect only, since the Android will try to re-enable encryption
      // after disconnection
      device->Disconnect(gatt_if_);
      if (device->connecting_actively)
        callbacks_->OnConnectionState(ConnectionState::DISCONNECTED,
                                      device->address);
      device_cleanup_helper(device, device->connecting_actively);
      return;
    }

@@ -194,7 +191,6 @@ class VolumeControlImpl : public VolumeControl {
                                     OnGattWriteCccStatic);

    } else {
      device->first_connection = true;
      BTA_GATTC_ServiceSearchRequest(device->connection_id,
                                     &kVolumeControlUuid);
    }
@@ -207,7 +203,7 @@ class VolumeControlImpl : public VolumeControl {
    }

    LOG_INFO(": address=%s", ADDRESS_TO_LOGGABLE_CSTR(device->address));
    if (device->service_changed_rcvd) {
    if (device->known_service_handles_ == false) {
      LOG_INFO("Device already is waiting for new services");
      return;
    }
@@ -217,9 +213,7 @@ class VolumeControlImpl : public VolumeControl {

    RemovePendingVolumeControlOperations(devices,
                                         bluetooth::groups::kGroupUnknown);
    device->first_connection = true;
    device->service_changed_rcvd = true;
    BtaGattQueue::Clean(device->connection_id);
    device->ResetHandles();
    BTA_GATTC_ServiceSearchRequest(device->connection_id, &kVolumeControlUuid);
  }

@@ -244,10 +238,11 @@ class VolumeControlImpl : public VolumeControl {
      return;
    }

    if (device->service_changed_rcvd)
    if (device->known_service_handles_ == false) {
      BTA_GATTC_ServiceSearchRequest(device->connection_id,
                                     &kVolumeControlUuid);
    }
  }

  void OnServiceSearchComplete(uint16_t connection_id, tGATT_STATUS status) {
    VolumeControlDevice* device =
@@ -259,19 +254,19 @@ class VolumeControlImpl : public VolumeControl {
    }

    /* Known device, nothing to do */
    if (!device->first_connection) return;
    if (device->IsReady()) return;

    if (status != GATT_SUCCESS) {
      /* close connection and report service discovery complete with error */
      LOG(ERROR) << "Service discovery failed";
      device_cleanup_helper(device, device->first_connection);
      device_cleanup_helper(device, device->connecting_actively);
      return;
    }

    bool success = device->UpdateHandles();
    if (!success) {
      LOG(ERROR) << "Incomplete service database";
      device_cleanup_helper(device, true);
      device_cleanup_helper(device, device->connecting_actively);
      return;
    }

@@ -638,12 +633,12 @@ class VolumeControlImpl : public VolumeControl {

    LOG(INFO) << __func__ << " GAP_EVT_CONN_CLOSED: "
              << ADDRESS_TO_LOGGABLE_STR(device->address);
    device->connecting_actively = false;
    device_cleanup_helper(device, true);
  }

  void OnGattDisconnected(uint16_t connection_id, tGATT_IF /*client_if*/,
                          RawAddress remote_bda,
                          tGATT_DISCONN_REASON /*reason*/) {
                          RawAddress remote_bda, tGATT_DISCONN_REASON reason) {
    VolumeControlDevice* device =
        volume_control_devices_.FindByConnId(connection_id);
    if (!device) {
@@ -661,15 +656,10 @@ class VolumeControlImpl : public VolumeControl {
      return;
    }

    // If we get here, it means, device has not been exlicitly disconnected.
    bool device_ready = device->IsReady();

    device_cleanup_helper(device, device->connecting_actively);

    if (device_ready) {
      device->first_connection = true;
      device->connecting_actively = true;

    if (reason != GATT_CONN_TERMINATE_LOCAL_HOST &&
        device->connecting_actively) {
      /* Add device into BG connection to accept remote initiated connection */
      BTA_GATTC_Open(gatt_if_, remote_bda, BTM_BLE_BKG_CONNECT_ALLOW_LIST,
                     false);
@@ -1081,10 +1071,6 @@ class VolumeControlImpl : public VolumeControl {
      callbacks_->OnConnectionState(ConnectionState::CONNECTED,
                                    device->address);

      device->connecting_actively = true;

      device->first_connection = false;

      // once profile connected we can notify current states
      callbacks_->OnVolumeStateChanged(device->address, device->volume,
                                       device->mute, false);