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

Commit 5018b899 authored by Antoine Soulier's avatar Antoine Soulier Committed by Gerrit Code Review
Browse files

Merge "HCI: Prevent usage of deleted instance" into main

parents 37deb0d9 e645383e
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
@@ -60,6 +60,9 @@ using os::Alarm;
using os::Handler;
using std::unique_ptr;

static std::recursive_mutex life_cycle_guard;
static bool life_cycle_stopped = true;

static std::chrono::milliseconds getHciTimeoutMs() {
  static auto sHciTimeoutMs = std::chrono::milliseconds(bluetooth::os::GetSystemPropertyUint32Base(
          "bluetooth.hci.timeout_milliseconds", HciLayer::kHciTimeoutMs.count()));
@@ -573,6 +576,10 @@ struct HciLayer::hal_callbacks : public hal::HciHalCallbacks {
  explicit hal_callbacks(HciLayer& module) : module_(module) {}

  void hciEventReceived(hal::HciPacket event_bytes) override {
    std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
    if (life_cycle_stopped) {
      return;
    }
    auto packet = packet::PacketView<packet::kLittleEndian>(
            std::make_shared<std::vector<uint8_t>>(event_bytes));
    EventView event = EventView::Create(packet);
@@ -580,6 +587,10 @@ struct HciLayer::hal_callbacks : public hal::HciHalCallbacks {
  }

  void aclDataReceived(hal::HciPacket data_bytes) override {
    std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
    if (life_cycle_stopped) {
      return;
    }
    auto packet = packet::PacketView<packet::kLittleEndian>(
            std::make_shared<std::vector<uint8_t>>(std::move(data_bytes)));
    auto acl = std::make_unique<AclView>(AclView::Create(packet));
@@ -587,6 +598,10 @@ struct HciLayer::hal_callbacks : public hal::HciHalCallbacks {
  }

  void scoDataReceived(hal::HciPacket data_bytes) override {
    std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
    if (life_cycle_stopped) {
      return;
    }
    auto packet = packet::PacketView<packet::kLittleEndian>(
            std::make_shared<std::vector<uint8_t>>(std::move(data_bytes)));
    auto sco = std::make_unique<ScoView>(ScoView::Create(packet));
@@ -594,6 +609,10 @@ struct HciLayer::hal_callbacks : public hal::HciHalCallbacks {
  }

  void isoDataReceived(hal::HciPacket data_bytes) override {
    std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
    if (life_cycle_stopped) {
      return;
    }
    auto packet = packet::PacketView<packet::kLittleEndian>(
            std::make_shared<std::vector<uint8_t>>(std::move(data_bytes)));
    auto iso = std::make_unique<IsoView>(IsoView::Create(packet));
@@ -621,12 +640,20 @@ common::BidiQueueEnd<IsoBuilder, IsoView>* HciLayer::GetIsoQueueEnd() {

void HciLayer::EnqueueCommand(unique_ptr<CommandBuilder> command,
                              ContextualOnceCallback<void(CommandCompleteView)> on_complete) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::enqueue_command<CommandCompleteView>, std::move(command),
         std::move(on_complete));
}

void HciLayer::EnqueueCommand(unique_ptr<CommandBuilder> command,
                              ContextualOnceCallback<void(CommandStatusView)> on_status) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::enqueue_command<CommandStatusView>, std::move(command),
         std::move(on_status));
}
@@ -639,28 +666,52 @@ void HciLayer::EnqueueCommand(
}

void HciLayer::RegisterEventHandler(EventCode event, ContextualCallback<void(EventView)> handler) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::register_event, event, handler);
}

void HciLayer::UnregisterEventHandler(EventCode event) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::unregister_event, event);
}

void HciLayer::RegisterLeEventHandler(SubeventCode event,
                                      ContextualCallback<void(LeMetaEventView)> handler) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::register_le_event, event, handler);
}

void HciLayer::UnregisterLeEventHandler(SubeventCode event) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::unregister_le_event, event);
}

void HciLayer::RegisterVendorSpecificEventHandler(
        VseSubeventCode event, ContextualCallback<void(VendorSpecificEventView)> handler) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::register_vs_event, event, handler);
}

void HciLayer::UnregisterVendorSpecificEventHandler(VseSubeventCode event) {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  if (life_cycle_stopped) {
    return;
  }
  CallOn(impl_, &impl::unregister_vs_event, event);
}

@@ -877,9 +928,11 @@ void HciLayer::ListDependencies(ModuleList* list) const {
}

void HciLayer::Start() {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  auto hal = GetDependency<hal::HciHal>();
  impl_ = new impl(hal, *this);
  hal_callbacks_ = new hal_callbacks(*this);
  life_cycle_stopped = false;

  Handler* handler = GetHandler();
  impl_->acl_queue_.GetDownEnd()->RegisterDequeue(handler,
@@ -907,6 +960,7 @@ void HciLayer::StartWithNoHalDependencies(Handler* handler) {
}

void HciLayer::Stop() {
  std::unique_lock<std::recursive_mutex> lock(life_cycle_guard);
  auto hal = GetDependency<hal::HciHal>();
  hal->unregisterIncomingPacketCallback();
  delete hal_callbacks_;
@@ -915,6 +969,8 @@ void HciLayer::Stop() {
  impl_->sco_queue_.GetDownEnd()->UnregisterDequeue();
  impl_->iso_queue_.GetDownEnd()->UnregisterDequeue();
  delete impl_;

  life_cycle_stopped = true;
}

}  // namespace hci