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

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

Add Module.Call() to cut down on module boilerplate

This is a shortcut for GetHandler->Post(BindOnce(XYZ)));

Start using inside hci_layer

Test: fuzz/run --host bluetooth_gd_hci_layer_fuzz_test
Change-Id: I436d235521b9160ffca5e41dc6a66e89f5520ca0
parent e325a3e8
Loading
Loading
Loading
Loading
+11 −15
Original line number Diff line number Diff line
@@ -240,8 +240,7 @@ struct HciLayer::impl : public hal::HciHalCallbacks {
    auto packet = packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(event_bytes));
    EventPacketView event = EventPacketView::Create(packet);
    ASSERT(event.IsValid());
    module_.GetHandler()->Post(
        BindOnce(&HciLayer::impl::hci_event_received_handler, common::Unretained(this), std::move(event)));
    module_.Call(&HciLayer::impl::hci_event_received_handler, common::Unretained(this), std::move(event));
  }

  void hci_event_received_handler(EventPacketView event) {
@@ -271,15 +270,14 @@ struct HciLayer::impl : public hal::HciHalCallbacks {

  void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
                      OnceCallback<void(CommandCompleteView)> on_complete, os::Handler* handler) {
    module_.GetHandler()->Post(common::BindOnce(&impl::handle_enqueue_command_with_complete, common::Unretained(this),
                                                std::move(command), std::move(on_complete),
                                                common::Unretained(handler)));
    module_.Call(&impl::handle_enqueue_command_with_complete, common::Unretained(this), std::move(command),
                 std::move(on_complete), common::Unretained(handler));
  }

  void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command, OnceCallback<void(CommandStatusView)> on_status,
                      os::Handler* handler) {
    module_.GetHandler()->Post(common::BindOnce(&impl::handle_enqueue_command_with_status, common::Unretained(this),
                                                std::move(command), std::move(on_status), common::Unretained(handler)));
    module_.Call(&impl::handle_enqueue_command_with_status, common::Unretained(this), std::move(command),
                 std::move(on_status), common::Unretained(handler));
  }

  void handle_enqueue_command_with_complete(std::unique_ptr<CommandPacketBuilder> command,
@@ -323,8 +321,8 @@ struct HciLayer::impl : public hal::HciHalCallbacks {
  }

  void RegisterEventHandler(EventCode event_code, Callback<void(EventPacketView)> event_handler, os::Handler* handler) {
    module_.GetHandler()->Post(common::BindOnce(&impl::handle_register_event_handler, common::Unretained(this),
                                                event_code, event_handler, common::Unretained(handler)));
    module_.Call(&impl::handle_register_event_handler, common::Unretained(this), event_code, event_handler,
                 common::Unretained(handler));
  }

  void handle_register_event_handler(EventCode event_code, Callback<void(EventPacketView)> event_handler,
@@ -335,8 +333,7 @@ struct HciLayer::impl : public hal::HciHalCallbacks {
  }

  void UnregisterEventHandler(EventCode event_code) {
    module_.GetHandler()->Post(
        common::BindOnce(&impl::handle_unregister_event_handler, common::Unretained(this), event_code));
    module_.Call(&impl::handle_unregister_event_handler, common::Unretained(this), event_code);
  }

  void handle_unregister_event_handler(EventCode event_code) {
@@ -345,8 +342,8 @@ struct HciLayer::impl : public hal::HciHalCallbacks {

  void RegisterLeEventHandler(SubeventCode subevent_code, Callback<void(LeMetaEventView)> event_handler,
                              os::Handler* handler) {
    module_.GetHandler()->Post(common::BindOnce(&impl::handle_register_le_event_handler, common::Unretained(this),
                                                subevent_code, event_handler, common::Unretained(handler)));
    module_.Call(&impl::handle_register_le_event_handler, common::Unretained(this), subevent_code, event_handler,
                 common::Unretained(handler));
  }

  void handle_register_le_event_handler(SubeventCode subevent_code, Callback<void(LeMetaEventView)> subevent_handler,
@@ -358,8 +355,7 @@ struct HciLayer::impl : public hal::HciHalCallbacks {
  }

  void UnregisterLeEventHandler(SubeventCode subevent_code) {
    module_.GetHandler()->Post(
        common::BindOnce(&impl::handle_unregister_le_event_handler, common::Unretained(this), subevent_code));
    module_.Call(&impl::handle_unregister_le_event_handler, common::Unretained(this), subevent_code);
  }

  void handle_unregister_le_event_handler(SubeventCode subevent_code) {
+5 −0
Original line number Diff line number Diff line
@@ -99,6 +99,11 @@ class Module {
    return static_cast<T*>(GetDependency(&T::Factory));
  }

  template <typename Functor, typename... Args>
  void Call(Functor&& functor, Args&&... args) {
    GetHandler()->Post(common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...));
  }

 private:
  Module* GetDependency(const ModuleFactory* module) const;