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 Original line Diff line number Diff line
@@ -178,6 +178,7 @@ bool VolumeControlDevice::UpdateHandles(void) {
      vcs_found = set_volume_control_service_handles(service);
      vcs_found = set_volume_control_service_handles(service);
      if (!vcs_found) break;
      if (!vcs_found) break;


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


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


  // the handles are not valid, so discard pending GATT operations
  // the handles are not valid, so discard pending GATT operations
+10 −16
Original line number Original line Diff line number Diff line
@@ -35,16 +35,11 @@ namespace internal {
class VolumeControlDevice : public bluetooth::common::IRedactableLoggable {
class VolumeControlDevice : public bluetooth::common::IRedactableLoggable {
 public:
 public:
  RawAddress address;
  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'.
  /* We are making active attempt to connect to this device */
   * This is true only during initial phase of first connection. */
  bool connecting_actively;
  bool connecting_actively;


  bool service_changed_rcvd;
  bool known_service_handles_;


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


  VolumeOffsets audio_offsets;
  VolumeOffsets audio_offsets;


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


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


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


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


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


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


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


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


    LOG_INFO(": address=%s", ADDRESS_TO_LOGGABLE_CSTR(device->address));
    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");
      LOG_INFO("Device already is waiting for new services");
      return;
      return;
    }
    }
@@ -217,9 +213,7 @@ class VolumeControlImpl : public VolumeControl {


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


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


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


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


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


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


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


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


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


  void OnGattDisconnected(uint16_t connection_id, tGATT_IF /*client_if*/,
  void OnGattDisconnected(uint16_t connection_id, tGATT_IF /*client_if*/,
                          RawAddress remote_bda,
                          RawAddress remote_bda, tGATT_DISCONN_REASON reason) {
                          tGATT_DISCONN_REASON /*reason*/) {
    VolumeControlDevice* device =
    VolumeControlDevice* device =
        volume_control_devices_.FindByConnId(connection_id);
        volume_control_devices_.FindByConnId(connection_id);
    if (!device) {
    if (!device) {
@@ -661,15 +656,10 @@ class VolumeControlImpl : public VolumeControl {
      return;
      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);
    device_cleanup_helper(device, device->connecting_actively);


    if (device_ready) {
    if (reason != GATT_CONN_TERMINATE_LOCAL_HOST &&
      device->first_connection = true;
        device->connecting_actively) {
      device->connecting_actively = true;

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


      device->connecting_actively = true;

      device->first_connection = false;

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