1 /****************************************************************************** 2 * 3 * Copyright 2019 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #pragma once 20 21 #include "common/callback.h" 22 #include "os/queue.h" 23 24 namespace bluetooth { 25 namespace common { 26 27 template <typename TENQUEUE, typename TDEQUEUE> 28 class BidiQueueEnd 29 : public ::bluetooth::os::IQueueEnqueue<TENQUEUE>, 30 public ::bluetooth::os::IQueueDequeue<TDEQUEUE> { 31 public: 32 using EnqueueCallback = Callback<std::unique_ptr<TENQUEUE>()>; 33 using DequeueCallback = Callback<void()>; 34 BidiQueueEnd(::bluetooth::os::IQueueEnqueue<TENQUEUE> * tx,::bluetooth::os::IQueueDequeue<TDEQUEUE> * rx)35 BidiQueueEnd(::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx, ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx) 36 : tx_(tx), rx_(rx) { 37 } 38 RegisterEnqueue(::bluetooth::os::Handler * handler,EnqueueCallback callback)39 void RegisterEnqueue(::bluetooth::os::Handler* handler, EnqueueCallback callback) override { 40 tx_->RegisterEnqueue(handler, callback); 41 } 42 UnregisterEnqueue()43 void UnregisterEnqueue() override { 44 tx_->UnregisterEnqueue(); 45 } 46 RegisterDequeue(::bluetooth::os::Handler * handler,DequeueCallback callback)47 void RegisterDequeue(::bluetooth::os::Handler* handler, DequeueCallback callback) override { 48 rx_->RegisterDequeue(handler, callback); 49 } 50 UnregisterDequeue()51 void UnregisterDequeue() override { 52 rx_->UnregisterDequeue(); 53 } 54 TryDequeue()55 std::unique_ptr<TDEQUEUE> TryDequeue() override { 56 return rx_->TryDequeue(); 57 } 58 59 private: 60 ::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx_; 61 ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx_; 62 }; 63 64 template <typename TUP, typename TDOWN> 65 class BidiQueue { 66 public: BidiQueue(size_t capacity)67 explicit BidiQueue(size_t capacity) 68 : up_queue_(capacity), 69 down_queue_(capacity), 70 up_end_(&down_queue_, &up_queue_), 71 down_end_(&up_queue_, &down_queue_) { 72 } 73 GetUpEnd()74 BidiQueueEnd<TDOWN, TUP>* GetUpEnd() { 75 return &up_end_; 76 } 77 GetDownEnd()78 BidiQueueEnd<TUP, TDOWN>* GetDownEnd() { 79 return &down_end_; 80 } 81 82 private: 83 ::bluetooth::os::Queue<TUP> up_queue_; 84 ::bluetooth::os::Queue<TDOWN> down_queue_; 85 BidiQueueEnd<TDOWN, TUP> up_end_; 86 BidiQueueEnd<TUP, TDOWN> down_end_; 87 }; 88 89 } // namespace common 90 } // namespace bluetooth 91