summaryrefslogtreecommitdiff
path: root/system/gd/common/bidi_queue.h
blob: 4dad1865626a8d3e9ec4a60ecfe310acb94bb1b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/******************************************************************************
 *
 *  Copyright (C) 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/callback.h"
#include "os/queue.h"

namespace bluetooth {
namespace common {

//
// Interface for one context to send and receive data over
// a pair of queues (|BidiQueue|).
//
template <typename TENQUEUE, typename TDEQUEUE>
class BidiQueueEnd : public ::bluetooth::os::IQueueEnqueue<TENQUEUE>,
                     public ::bluetooth::os::IQueueDequeue<TDEQUEUE> {
public:
  using EnqueueCallback = Callback<std::unique_ptr<TENQUEUE>()>;
  using DequeueCallback = Callback<void()>;

  BidiQueueEnd(::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx,
               ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx)
      : tx_(tx), rx_(rx) {}

  void RegisterEnqueue(::bluetooth::os::Handler* handler, EnqueueCallback callback) override {
    tx_->RegisterEnqueue(handler, callback);
  }

  void UnregisterEnqueue() override { tx_->UnregisterEnqueue(); }

  void RegisterDequeue(::bluetooth::os::Handler* handler, DequeueCallback callback) override {
    rx_->RegisterDequeue(handler, callback);
  }

  void UnregisterDequeue() override { rx_->UnregisterDequeue(); }

  std::unique_ptr<TDEQUEUE> TryDequeue() override { return rx_->TryDequeue(); }

private:
  ::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx_;
  ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx_;
};

//
// Interface managing a pair of queues shared between two contexts
// (typically layers of the stack).
//
// The up queue can be used for data to indicate up to bluetooth host and
// the down queue can be used for data to sent to bluetooth controller.
// Each context uses its |BidiQueueEnd| to manage their data operations:
//
// The up end:
// - Receives data indicated from the down end.
// - Sends data to the down end.
//
// The down end:
// - Receives data sent from the up end.
// - Indicates data to the up end.
//
template <typename TUP, typename TDOWN>
class BidiQueue {
public:
  explicit BidiQueue(size_t capacity)
      : up_queue_(capacity),
        down_queue_(capacity),
        up_end_(&down_queue_, &up_queue_),
        down_end_(&up_queue_, &down_queue_) {}

  BidiQueueEnd<TDOWN, TUP>* GetUpEnd() { return &up_end_; }

  BidiQueueEnd<TUP, TDOWN>* GetDownEnd() { return &down_end_; }

private:
  ::bluetooth::os::Queue<TUP> up_queue_;
  ::bluetooth::os::Queue<TDOWN> down_queue_;
  BidiQueueEnd<TDOWN, TUP> up_end_;
  BidiQueueEnd<TUP, TDOWN> down_end_;
};

}  // namespace common
}  // namespace bluetooth