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

Commit f674fd9d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "handle the fail and ble disconnection" into main

parents d01654df 7ef72779
Loading
Loading
Loading
Loading
+44 −27
Original line number Diff line number Diff line
@@ -510,22 +510,32 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
            handler_->BindOnceOn(this, &impl::on_cs_set_procedure_parameters));
  }

  void handle_cs_setup_failure(uint16_t connection_handle, DistanceMeasurementErrorCode errorCode) {
    auto it = cs_trackers_.find(connection_handle);
    if (it == cs_trackers_.end()) {
      return;
    }
    if (it->second.role == CsRole::INITIATOR) {
      if (it->second.waiting_for_start_callback) {
        distance_measurement_callbacks_->OnDistanceMeasurementStartFail(it->second.address,
                                                                        errorCode, METHOD_CS);
      } else {
        distance_measurement_callbacks_->OnDistanceMeasurementStopped(it->second.address, errorCode,
                                                                      METHOD_CS);
      }
      it->second.repeating_alarm->Cancel();
      it->second.repeating_alarm.reset();
    }
    // the cs_tracker should be kept until the connection is disconnected
  }

  void send_le_cs_procedure_enable(uint16_t connection_handle, Enable enable) {
    log::debug("cmd {}", enable);
    if (cs_trackers_.find(connection_handle) == cs_trackers_.end()) {
      log::warn("Can't find cs tracker for connection {}", connection_handle);
      return;
    }
    Address address = cs_trackers_[connection_handle].address;
    // Check if the connection still exists
    if (connection_handle == kIllegalConnectionHandle) {
      log::warn("Can't find connection for {}", address);
      distance_measurement_callbacks_->OnDistanceMeasurementStopped(
              address, REASON_NO_LE_CONNECTION, METHOD_CS);
      cs_trackers_[connection_handle].repeating_alarm->Cancel();
      cs_trackers_[connection_handle].repeating_alarm.reset();
      cs_trackers_.erase(connection_handle);
      return;
    }

    hci_layer_->EnqueueCommand(
            LeCsProcedureEnableBuilder::Create(connection_handle, kConfigId, enable),
            handler_->BindOnce(check_status<LeCsProcedureEnableStatusView>));
@@ -550,13 +560,15 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    if (!event_view.IsValid()) {
      log::warn("Get invalid LeCsReadRemoteSupportedCapabilitiesCompleteView");
      return;
    } else if (event_view.GetStatus() != ErrorCode::SUCCESS) {
    }
    uint16_t connection_handle = event_view.GetConnectionHandle();
    if (event_view.GetStatus() != ErrorCode::SUCCESS) {
      std::string error_code = ErrorCodeText(event_view.GetStatus());
      log::warn("Received LeCsReadRemoteSupportedCapabilitiesCompleteView with error code {}",
                error_code);
      handle_cs_setup_failure(connection_handle, REASON_INTERNAL_ERROR);
      return;
    }
    uint16_t connection_handle = event_view.GetConnectionHandle();
    send_le_cs_set_default_settings(event_view.GetConnectionHandle());
    if (cs_trackers_.find(connection_handle) != cs_trackers_.end() &&
        cs_trackers_[connection_handle].local_start) {
@@ -582,9 +594,12 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    if (!complete_view.IsValid()) {
      log::warn("Get invalid LeCsSetDefaultSettingsComplete");
      return;
    } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
    }
    if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
      std::string error_code = ErrorCodeText(complete_view.GetStatus());
      log::warn("Received LeCsSetDefaultSettingsComplete with error code {}", error_code);
      uint16_t connection_handle = complete_view.GetConnectionHandle();
      handle_cs_setup_failure(connection_handle, REASON_INTERNAL_ERROR);
      return;
    }
  }
@@ -593,12 +608,14 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    if (!event_view.IsValid()) {
      log::warn("Get invalid LeCsSecurityEnableCompleteView");
      return;
    } else if (event_view.GetStatus() != ErrorCode::SUCCESS) {
    }
    uint16_t connection_handle = event_view.GetConnectionHandle();
    if (event_view.GetStatus() != ErrorCode::SUCCESS) {
      std::string error_code = ErrorCodeText(event_view.GetStatus());
      log::warn("Received LeCsSecurityEnableCompleteView with error code {}", error_code);
      handle_cs_setup_failure(connection_handle, REASON_INTERNAL_ERROR);
      return;
    }
    uint16_t connection_handle = event_view.GetConnectionHandle();
    if (cs_trackers_.find(connection_handle) == cs_trackers_.end()) {
      log::warn("Can't find cs tracker for connection_handle {}", connection_handle);
      return;
@@ -615,12 +632,14 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    if (!event_view.IsValid()) {
      log::warn("Get invalid LeCsConfigCompleteView");
      return;
    } else if (event_view.GetStatus() != ErrorCode::SUCCESS) {
    }
    uint16_t connection_handle = event_view.GetConnectionHandle();
    if (event_view.GetStatus() != ErrorCode::SUCCESS) {
      std::string error_code = ErrorCodeText(event_view.GetStatus());
      log::warn("Received LeCsConfigCompleteView with error code {}", error_code);
      handle_cs_setup_failure(connection_handle, REASON_INTERNAL_ERROR);
      return;
    }
    uint16_t connection_handle = event_view.GetConnectionHandle();
    if (cs_trackers_.find(connection_handle) == cs_trackers_.end()) {
      log::warn("Can't find cs tracker for connection_handle {}", connection_handle);
      return;
@@ -645,12 +664,15 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    if (!complete_view.IsValid()) {
      log::warn("Get Invalid LeCsSetProcedureParametersCompleteView");
      return;
    } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
    }
    uint16_t connection_handle = complete_view.GetConnectionHandle();
    if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
      std::string error_code = ErrorCodeText(complete_view.GetStatus());
      log::warn("Received LeCsSetProcedureParametersCompleteView with error code {}", error_code);
      handle_cs_setup_failure(connection_handle, REASON_INTERNAL_ERROR);
      return;
    }
    uint16_t connection_handle = complete_view.GetConnectionHandle();

    if (cs_trackers_.find(connection_handle) == cs_trackers_.end()) {
      log::warn("Can't find cs tracker for connection_handle {}", connection_handle);
      return;
@@ -671,18 +693,13 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
  void on_cs_procedure_enable_complete(LeCsProcedureEnableCompleteView event_view) {
    log::assert_that(event_view.IsValid(), "assert failed: event_view.IsValid()");
    uint16_t connection_handle = event_view.GetConnectionHandle();
    log::debug("on cs procedure enabled complete");
    if (event_view.GetStatus() != ErrorCode::SUCCESS) {
      std::string error_code = ErrorCodeText(event_view.GetStatus());
      log::warn("Received LeCsProcedureEnableCompleteView with error code {}", error_code);
      if (cs_trackers_.find(connection_handle) != cs_trackers_.end() &&
          cs_trackers_[connection_handle].waiting_for_start_callback) {
        cs_trackers_[connection_handle].waiting_for_start_callback = false;
        distance_measurement_callbacks_->OnDistanceMeasurementStartFail(
                cs_trackers_[connection_handle].address, REASON_INTERNAL_ERROR, METHOD_CS);
      }
      handle_cs_setup_failure(connection_handle, REASON_INTERNAL_ERROR);
      return;
    }

    if (event_view.GetState() == Enable::ENABLED) {
      log::debug("Procedure enabled, {}", event_view.ToString());
      if (cs_trackers_.find(connection_handle) == cs_trackers_.end()) {