1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include "common/bidi_queue.h" 20 #include "hci/address.h" 21 #include "l2cap/cid.h" 22 #include "l2cap/dynamic_channel.h" 23 #include "l2cap/internal/channel_impl.h" 24 #include "l2cap/internal/ilink.h" 25 #include "l2cap/l2cap_packets.h" 26 #include "l2cap/mtu.h" 27 #include "l2cap/psm.h" 28 #include "os/handler.h" 29 #include "os/log.h" 30 31 namespace bluetooth { 32 namespace l2cap { 33 namespace internal { 34 35 class DynamicChannelImpl : public l2cap::internal::ChannelImpl { 36 public: 37 DynamicChannelImpl(Psm psm, Cid cid, Cid remote_cid, l2cap::internal::ILink* link, os::Handler* l2cap_handler); 38 39 DynamicChannelImpl(const DynamicChannelImpl&) = delete; 40 DynamicChannelImpl& operator=(const DynamicChannelImpl&) = delete; 41 42 virtual ~DynamicChannelImpl() = default; 43 44 hci::AddressWithType GetDevice() const; 45 46 virtual void RegisterOnCloseCallback(DynamicChannel::OnCloseCallback on_close_callback); 47 48 virtual void Close(); 49 virtual void OnClosed(hci::ErrorCode status); 50 virtual std::string ToString(); 51 GetQueueUpEnd()52 common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() { 53 return channel_queue_.GetUpEnd(); 54 } 55 GetQueueDownEnd()56 common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueDownEnd() { 57 return channel_queue_.GetDownEnd(); 58 } 59 GetCid()60 virtual Cid GetCid() const { 61 return cid_; 62 } 63 GetRemoteCid()64 virtual Cid GetRemoteCid() const { 65 return remote_cid_; 66 } 67 GetPsm()68 virtual Psm GetPsm() const { 69 return psm_; 70 } 71 SetChannelTxPriority(bool high_priority)72 virtual void SetChannelTxPriority(bool high_priority) { 73 link_->SetChannelTxPriority(cid_, high_priority); 74 } 75 76 // TODO(cmanton) Do something a little bit better than this 77 bool local_initiated_{false}; 78 79 private: 80 const Psm psm_; 81 const Cid cid_; 82 const Cid remote_cid_; 83 l2cap::internal::ILink* link_; 84 os::Handler* l2cap_handler_; 85 const hci::AddressWithType device_; 86 87 // User supported states 88 DynamicChannel::OnCloseCallback on_close_callback_{}; 89 90 // Internal states 91 bool closed_ = false; 92 hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS; 93 static constexpr size_t kChannelQueueSize = 5; 94 common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{ 95 kChannelQueueSize}; 96 }; 97 98 } // namespace internal 99 } // namespace l2cap 100 } // namespace bluetooth 101