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

Commit 98fae21a authored by Hansong Zhang's avatar Hansong Zhang
Browse files

HCI Layer: Register CommandComplete and CommandStatus on Start()

Module.GetHandler() works only after a Module is started. Move
RegisterEventHandler() for CommandComplete and CommandStatus to Start().

Test: atest bluetooth_test_gd --host
Change-Id: I7331c8ddb8d61d697aa4480e7c591c3817d21afa
parent 094e3beb
Loading
Loading
Loading
Loading
+6 −7
Original line number Diff line number Diff line
@@ -58,16 +58,10 @@ using common::BidiQueueEnd;
using os::Handler;

struct HciLayer::impl : public hal::HciHalCallbacks {
  impl(HciLayer& module) : hal_(nullptr), module_(module) {
    RegisterEventHandler(EventCode::COMMAND_COMPLETE, [this](EventPacketView event) { CommandCompleteCallback(event); },
                         module_.GetHandler());
    RegisterEventHandler(EventCode::COMMAND_STATUS, [this](EventPacketView event) { CommandStatusCallback(event); },
                         module_.GetHandler());
  }
  impl(HciLayer& module) : hal_(nullptr), module_(module) {}

  void Start(hal::HciHal* hal) {
    hal_ = hal;
    hal_->registerIncomingPacketCallback(this);

    send_acl_ = [this](std::unique_ptr<hci::BasePacketBuilder> packet) {
      std::vector<uint8_t> bytes;
@@ -84,6 +78,11 @@ struct HciLayer::impl : public hal::HciHalCallbacks {
    auto queue_end = acl_queue_.GetDownEnd();
    Handler* handler = module_.GetHandler();
    queue_end->RegisterDequeue(handler, [queue_end, this]() { send_acl_(queue_end->TryDequeue()); });
    RegisterEventHandler(EventCode::COMMAND_COMPLETE, [this](EventPacketView event) { CommandCompleteCallback(event); },
                         handler);
    RegisterEventHandler(EventCode::COMMAND_STATUS, [this](EventPacketView event) { CommandStatusCallback(event); },
                         handler);
    hal_->registerIncomingPacketCallback(this);
  }

  void Stop() {
+8 −1
Original line number Diff line number Diff line
@@ -231,11 +231,18 @@ TEST_F(HciTest, createConnectionTest) {
  uint16_t handle = 0x123;
  LinkType link_type = LinkType::ACL;
  Enable encryption_enabled = Enable::DISABLED;
  hal->callbacks->hciEventReceived(GetPacketBytes(CreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 1)));

  // Verify the event
  auto event = upper->GetReceivedEvent();
  ASSERT_TRUE(event.IsValid());
  ASSERT_EQ(EventCode::COMMAND_STATUS, event.GetEventCode());

  hal->callbacks->hciEventReceived(
      GetPacketBytes(ConnectionCompleteBuilder::Create(status, handle, bd_addr, link_type, encryption_enabled)));

  // Verify the event
  auto event = upper->GetReceivedEvent();
  event = upper->GetReceivedEvent();
  ASSERT_TRUE(event.IsValid());
  ASSERT_EQ(EventCode::CONNECTION_COMPLETE, event.GetEventCode());
  ConnectionCompleteView connection_complete_view = ConnectionCompleteView::Create(event);
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
}

Handler* Module::GetHandler() {
  ASSERT_LOG(handler_ != nullptr, "Can't get handler when it's not started");
  return handler_;
}

+1 −1
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ class Module {
 private:
  Module* GetDependency(const ModuleFactory* module) const;

  ::bluetooth::os::Handler* handler_;
  ::bluetooth::os::Handler* handler_ = nullptr;
  ModuleList dependencies_;
  ModuleRegistry* registry_;
};