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

Commit 392a4936 authored by Zach Johnson's avatar Zach Johnson Committed by Gerrit Code Review
Browse files

Merge changes I980a3c28,I4f686d13

* changes:
  Add ContextualCallback and start using for hci events
  Create ContextualOnceCallback, to bind together a context and a callback
parents 717697eb c599487a
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -25,13 +25,14 @@ using base::Bind;
using base::BindOnce;
using base::ConstRef;
using base::IgnoreResult;
using base::MakeUnboundRunType;
using base::Owned;
using base::Passed;
using base::RetainedRef;
using base::Unretained;

template <typename T, typename Functor, typename... Args>
inline base::Callback<base::MakeUnboundRunType<Functor, T, Args...>> BindOn(T* obj, Functor&& functor, Args&&... args) {
inline base::Callback<MakeUnboundRunType<Functor, T, Args...>> BindOn(T* obj, Functor&& functor, Args&&... args) {
  return common::Bind(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...);
}

+84 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "common/bind.h"
#include "common/callback.h"

namespace bluetooth {
namespace common {

class IPostableContext {
 public:
  virtual ~IPostableContext(){};
  virtual void Post(OnceClosure closure) = 0;
};

template <typename R, typename... Args>
class ContextualOnceCallback;

// A callback bound to an execution context that can be invoked only once.
template <typename R, typename... Args>
class ContextualOnceCallback<R(Args...)> {
 public:
  ContextualOnceCallback(common::OnceCallback<R(Args...)>&& callback, IPostableContext* context)
      : callback_(std::move(callback)), context_(context) {}

  constexpr ContextualOnceCallback() = default;

  DISALLOW_COPY_AND_ASSIGN(ContextualOnceCallback);

  ContextualOnceCallback(ContextualOnceCallback&&) noexcept = default;
  ContextualOnceCallback& operator=(ContextualOnceCallback&&) noexcept = default;

  void Invoke(Args... args) {
    context_->Post(common::BindOnce(std::move(callback_), std::forward<Args>(args)...));
  }

 private:
  common::OnceCallback<R(Args...)> callback_;
  IPostableContext* context_;
};

template <typename R, typename... Args>
class ContextualCallback;

// A callback bound to an execution context that can be invoked multiple times.
template <typename R, typename... Args>
class ContextualCallback<R(Args...)> {
 public:
  ContextualCallback(common::Callback<R(Args...)>&& callback, IPostableContext* context)
      : callback_(std::move(callback)), context_(context) {}

  constexpr ContextualCallback() = default;

  ContextualCallback(const ContextualCallback&) = default;
  ContextualCallback& operator=(const ContextualCallback&) = default;
  ContextualCallback(ContextualCallback&&) noexcept = default;
  ContextualCallback& operator=(ContextualCallback&&) noexcept = default;

  void Invoke(Args... args) {
    context_->Post(common::BindOnce(callback_, std::forward<Args>(args)...));
  }

 private:
  common::Callback<R(Args...)> callback_;
  IPostableContext* context_;
};

}  // namespace common
}  // namespace bluetooth
+61 −96

File changed.

Preview size limit exceeded, changes collapsed.

+16 −18
Original line number Diff line number Diff line
@@ -118,7 +118,7 @@ class TestController : public Controller {
class TestHciLayer : public HciLayer {
 public:
  void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
                      common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) override {
                      common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
    command_queue_.push(std::move(command));
    command_status_callbacks.push_front(std::move(on_status));
    if (command_promise_ != nullptr) {
@@ -128,7 +128,7 @@ class TestHciLayer : public HciLayer {
  }

  void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
                      common::OnceCallback<void(CommandCompleteView)> on_complete, os::Handler* handler) override {
                      common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
    command_queue_.push(std::move(command));
    command_complete_callbacks.push_front(std::move(on_complete));
    if (command_promise_ != nullptr) {
@@ -167,8 +167,8 @@ class TestHciLayer : public HciLayer {
    return command;
  }

  void RegisterEventHandler(EventCode event_code, common::Callback<void(EventPacketView)> event_handler,
                            os::Handler* handler) override {
  void RegisterEventHandler(EventCode event_code,
                            common::ContextualCallback<void(EventPacketView)> event_handler) override {
    registered_events_[event_code] = event_handler;
  }

@@ -176,8 +176,8 @@ class TestHciLayer : public HciLayer {
    registered_events_.erase(event_code);
  }

  void RegisterLeEventHandler(SubeventCode subevent_code, common::Callback<void(LeMetaEventView)> event_handler,
                              os::Handler* handler) override {
  void RegisterLeEventHandler(SubeventCode subevent_code,
                              common::ContextualCallback<void(LeMetaEventView)> event_handler) override {
    registered_le_events_[subevent_code] = event_handler;
  }

@@ -191,7 +191,7 @@ class TestHciLayer : public HciLayer {
    ASSERT_TRUE(event.IsValid());
    EventCode event_code = event.GetEventCode();
    ASSERT_TRUE(registered_events_.find(event_code) != registered_events_.end()) << EventCodeText(event_code);
    registered_events_[event_code].Run(event);
    registered_events_[event_code].Invoke(event);
  }

  void IncomingLeMetaEvent(std::unique_ptr<LeMetaEventBuilder> event_builder) {
@@ -201,7 +201,7 @@ class TestHciLayer : public HciLayer {
    EXPECT_TRUE(meta_event_view.IsValid());
    SubeventCode subevent_code = meta_event_view.GetSubeventCode();
    EXPECT_TRUE(registered_le_events_.find(subevent_code) != registered_le_events_.end());
    registered_le_events_[subevent_code].Run(meta_event_view);
    registered_le_events_[subevent_code].Invoke(meta_event_view);
  }

  void IncomingAclData(uint16_t handle) {
@@ -231,14 +231,14 @@ class TestHciLayer : public HciLayer {
  void CommandCompleteCallback(EventPacketView event) {
    CommandCompleteView complete_view = CommandCompleteView::Create(event);
    ASSERT(complete_view.IsValid());
    std::move(command_complete_callbacks.front()).Run(complete_view);
    std::move(command_complete_callbacks.front()).Invoke(complete_view);
    command_complete_callbacks.pop_front();
  }

  void CommandStatusCallback(EventPacketView event) {
    CommandStatusView status_view = CommandStatusView::Create(event);
    ASSERT(status_view.IsValid());
    std::move(command_status_callbacks.front()).Run(status_view);
    std::move(command_status_callbacks.front()).Invoke(status_view);
    command_status_callbacks.pop_front();
  }

@@ -259,17 +259,16 @@ class TestHciLayer : public HciLayer {
  void ListDependencies(ModuleList* list) override {}
  void Start() override {
    RegisterEventHandler(EventCode::COMMAND_COMPLETE,
                         base::Bind(&TestHciLayer::CommandCompleteCallback, common::Unretained(this)), nullptr);
    RegisterEventHandler(EventCode::COMMAND_STATUS,
                         base::Bind(&TestHciLayer::CommandStatusCallback, common::Unretained(this)), nullptr);
                         GetHandler()->BindOn(this, &TestHciLayer::CommandCompleteCallback));
    RegisterEventHandler(EventCode::COMMAND_STATUS, GetHandler()->BindOn(this, &TestHciLayer::CommandStatusCallback));
  }
  void Stop() override {}

 private:
  std::map<EventCode, common::Callback<void(EventPacketView)>> registered_events_;
  std::map<SubeventCode, common::Callback<void(LeMetaEventView)>> registered_le_events_;
  std::list<base::OnceCallback<void(CommandCompleteView)>> command_complete_callbacks;
  std::list<base::OnceCallback<void(CommandStatusView)>> command_status_callbacks;
  std::map<EventCode, common::ContextualCallback<void(EventPacketView)>> registered_events_;
  std::map<SubeventCode, common::ContextualCallback<void(LeMetaEventView)>> registered_le_events_;
  std::list<common::ContextualOnceCallback<void(CommandCompleteView)>> command_complete_callbacks;
  std::list<common::ContextualOnceCallback<void(CommandStatusView)>> command_status_callbacks;
  BidiQueue<AclPacketView, AclPacketBuilder> acl_queue_{3 /* TODO: Set queue depth */};

  std::queue<std::unique_ptr<CommandPacketBuilder>> command_queue_;
@@ -281,7 +280,6 @@ class AclManagerNoCallbacksTest : public ::testing::Test {
 protected:
  void SetUp() override {
    test_hci_layer_ = new TestHciLayer;  // Ownership is transferred to registry
    test_hci_layer_->Start();
    test_controller_ = new TestController;
    fake_registry_.InjectTestModule(&HciLayer::Factory, test_hci_layer_);
    fake_registry_.InjectTestModule(&Controller::Factory, test_controller_);
+5 −5
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

#pragma once

#include "common/callback.h"
#include "common/contextual_callback.h"
#include "hci/hci_packets.h"
#include "os/handler.h"
#include "os/utils.h"
@@ -31,11 +31,11 @@ class CommandInterface {
  virtual ~CommandInterface() = default;
  DISALLOW_COPY_AND_ASSIGN(CommandInterface);

  virtual void EnqueueCommand(std::unique_ptr<T> command, common::OnceCallback<void(CommandCompleteView)> on_complete,
                              os::Handler* handler) = 0;
  virtual void EnqueueCommand(std::unique_ptr<T> command,
                              common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) = 0;

  virtual void EnqueueCommand(std::unique_ptr<T> command, common::OnceCallback<void(CommandStatusView)> on_status,
                              os::Handler* handler) = 0;
  virtual void EnqueueCommand(std::unique_ptr<T> command,
                              common::ContextualOnceCallback<void(CommandStatusView)> on_status) = 0;
};
}  // namespace hci
}  // namespace bluetooth
Loading