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 : public ::bluetooth::os::IQueueEnqueue<TENQUEUE>, public ::bluetooth::os::IQueueDequeue<TDEQUEUE> { 29 public: 30 using EnqueueCallback = Callback<std::unique_ptr<TENQUEUE>()>; 31 using DequeueCallback = Callback<void()>; 32 BidiQueueEnd(::bluetooth::os::IQueueEnqueue<TENQUEUE> * tx,::bluetooth::os::IQueueDequeue<TDEQUEUE> * rx)33 BidiQueueEnd(::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx, ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx) 34 : tx_(tx), rx_(rx) {} 35 RegisterEnqueue(::bluetooth::os::Handler * handler,EnqueueCallback callback)36 void RegisterEnqueue(::bluetooth::os::Handler* handler, EnqueueCallback callback) override { 37 tx_->RegisterEnqueue(handler, callback); 38 } 39 UnregisterEnqueue()40 void UnregisterEnqueue() override { 41 tx_->UnregisterEnqueue(); 42 } 43 RegisterDequeue(::bluetooth::os::Handler * handler,DequeueCallback callback)44 void RegisterDequeue(::bluetooth::os::Handler* handler, DequeueCallback callback) override { 45 rx_->RegisterDequeue(handler, callback); 46 } 47 UnregisterDequeue()48 void UnregisterDequeue() override { 49 rx_->UnregisterDequeue(); 50 } 51 TryDequeue()52 std::unique_ptr<TDEQUEUE> TryDequeue() override { 53 return rx_->TryDequeue(); 54 } 55 56 private: 57 ::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx_; 58 ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx_; 59 }; 60 61 template <typename TUP, typename TDOWN> 62 class BidiQueue { 63 public: BidiQueue(size_t capacity)64 explicit BidiQueue(size_t capacity) 65 : up_queue_(capacity), 66 down_queue_(capacity), 67 up_end_(&down_queue_, &up_queue_), 68 down_end_(&up_queue_, &down_queue_) {} 69 GetUpEnd()70 BidiQueueEnd<TDOWN, TUP>* GetUpEnd() { 71 return &up_end_; 72 } 73 GetDownEnd()74 BidiQueueEnd<TUP, TDOWN>* GetDownEnd() { 75 return &down_end_; 76 } 77 78 private: 79 ::bluetooth::os::Queue<TUP> up_queue_; 80 ::bluetooth::os::Queue<TDOWN> down_queue_; 81 BidiQueueEnd<TDOWN, TUP> up_end_; 82 BidiQueueEnd<TUP, TDOWN> down_end_; 83 }; 84 85 } // namespace common 86 } // namespace bluetooth 87