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

Commit dea3d8e5 authored by Zach Johnson's avatar Zach Johnson
Browse files

Reorganize HciLayer::impl to group related functions together

acl

sending commands
dealing with command responses

registering for events
dispatching events

Test: fuzz/run --host bluetooth_gd_hci_layer_fuzz_test
Change-Id: I9c2029ef4d5b3356d2013b33f9f59453a013ccb2
parent 48df508a
Loading
Loading
Loading
Loading
+29 −29
Original line number Diff line number Diff line
@@ -126,6 +126,12 @@ struct HciLayer::impl {
    hal_->sendAclData(bytes);
  }

  template <typename TResponse>
  void enqueue_command(unique_ptr<CommandPacketBuilder> command, ContextualOnceCallback<void(TResponse)> on_response) {
    command_queue_.emplace_back(move(command), move(on_response));
    send_next_command();
  }

  void on_command_status(EventPacketView event) {
    handle_command_response<CommandStatusView>(event, "status");
  }
@@ -160,31 +166,6 @@ struct HciLayer::impl {
    send_next_command();
  }

  void on_le_meta_event(EventPacketView event) {
    LeMetaEventView meta_event_view = LeMetaEventView::Create(event);
    ASSERT(meta_event_view.IsValid());
    SubeventCode subevent_code = meta_event_view.GetSubeventCode();
    ASSERT_LOG(subevent_handlers_.find(subevent_code) != subevent_handlers_.end(),
               "Unhandled le event of type 0x%02hhx (%s)", subevent_code, SubeventCodeText(subevent_code).c_str());
    subevent_handlers_[subevent_code].Invoke(meta_event_view);
  }

  void on_hci_event(EventPacketView event) {
    ASSERT(event.IsValid());
    EventCode event_code = event.GetEventCode();
    if (event_handlers_.find(event_code) == event_handlers_.end()) {
      LOG_DEBUG("Dropping unregistered event of type 0x%02hhx (%s)", event_code, EventCodeText(event_code).c_str());
      return;
    }
    event_handlers_[event_code].Invoke(event);
  }

  template <typename TResponse>
  void enqueue_command(unique_ptr<CommandPacketBuilder> command, ContextualOnceCallback<void(TResponse)> on_response) {
    command_queue_.emplace_back(move(command), move(on_response));
    send_next_command();
  }

  void send_next_command() {
    if (command_credits_ == 0) {
      return;
@@ -228,6 +209,25 @@ struct HciLayer::impl {
    subevent_handlers_.erase(subevent_handlers_.find(event));
  }

  void on_hci_event(EventPacketView event) {
    ASSERT(event.IsValid());
    EventCode event_code = event.GetEventCode();
    if (event_handlers_.find(event_code) == event_handlers_.end()) {
      LOG_DEBUG("Dropping unregistered event of type 0x%02hhx (%s)", event_code, EventCodeText(event_code).c_str());
      return;
    }
    event_handlers_[event_code].Invoke(event);
  }

  void on_le_meta_event(EventPacketView event) {
    LeMetaEventView meta_event_view = LeMetaEventView::Create(event);
    ASSERT(meta_event_view.IsValid());
    SubeventCode subevent_code = meta_event_view.GetSubeventCode();
    ASSERT_LOG(subevent_handlers_.find(subevent_code) != subevent_handlers_.end(),
               "Unhandled le event of type 0x%02hhx (%s)", subevent_code, SubeventCodeText(subevent_code).c_str());
    subevent_handlers_[subevent_code].Invoke(meta_event_view);
  }

  hal::HciHal* hal_;
  HciLayer& module_;

@@ -281,6 +281,10 @@ HciLayer::HciLayer() : impl_(nullptr), hal_callbacks_(nullptr) {}
HciLayer::~HciLayer() {
}

common::BidiQueueEnd<AclPacketBuilder, AclPacketView>* HciLayer::GetAclQueueEnd() {
  return impl_->acl_queue_.GetUpEnd();
}

void HciLayer::EnqueueCommand(unique_ptr<CommandPacketBuilder> command,
                              common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) {
  CallOn(impl_, &impl::enqueue_command<CommandCompleteView>, move(command), move(on_complete));
@@ -291,10 +295,6 @@ void HciLayer::EnqueueCommand(unique_ptr<CommandPacketBuilder> command,
  CallOn(impl_, &impl::enqueue_command<CommandStatusView>, move(command), move(on_status));
}

common::BidiQueueEnd<AclPacketBuilder, AclPacketView>* HciLayer::GetAclQueueEnd() {
  return impl_->acl_queue_.GetUpEnd();
}

void HciLayer::RegisterEventHandler(EventCode event, ContextualCallback<void(EventPacketView)> handler) {
  CallOn(impl_, &impl::register_event, event, handler);
}