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

Commit e645383e authored by Antoine SOULIER's avatar Antoine SOULIER
Browse files

HCI: Prevent usage of deleted instance

Bug: 365653608
Flag: EXEMPT, bug fix
Test: m .
Change-Id: Ia2eb393dab94e45a058fb9f866ddbb01cc7578a4
parent 288279c5
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
@@ -59,6 +59,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()));
@@ -542,6 +545,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);
@@ -549,6 +556,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));
@@ -556,6 +567,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));
@@ -563,6 +578,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));
@@ -590,39 +609,71 @@ 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));
}

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);
}

@@ -830,9 +881,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,
@@ -860,6 +913,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_;
@@ -868,6 +922,8 @@ void HciLayer::Stop() {
  impl_->sco_queue_.GetDownEnd()->UnregisterDequeue();
  impl_->iso_queue_.GetDownEnd()->UnregisterDequeue();
  delete impl_;

  life_cycle_stopped = true;
}

}  // namespace hci