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 "l2cap/cid.h" 21 #include "l2cap/classic/fixed_channel.h" 22 #include "l2cap/internal/channel_impl.h" 23 #include "l2cap/l2cap_packets.h" 24 #include "os/handler.h" 25 #include "os/log.h" 26 27 namespace bluetooth { 28 namespace l2cap { 29 namespace classic { 30 namespace internal { 31 32 class Link; 33 34 class FixedChannelImpl : public l2cap::internal::ChannelImpl { 35 public: 36 FixedChannelImpl(Cid cid, Link* link, os::Handler* l2cap_handler); 37 38 FixedChannelImpl(const FixedChannelImpl&) = delete; 39 FixedChannelImpl& operator=(const FixedChannelImpl&) = delete; 40 41 virtual ~FixedChannelImpl() = default; 42 GetDevice()43 hci::Address GetDevice() const { 44 return device_.GetAddress(); 45 } 46 47 virtual void RegisterOnCloseCallback(os::Handler* user_handler, FixedChannel::OnCloseCallback on_close_callback); 48 49 virtual void Acquire(); 50 51 virtual void Release(); 52 IsAcquired()53 virtual bool IsAcquired() const { 54 return acquired_; 55 } 56 57 virtual void OnClosed(hci::ErrorCode status); 58 ToString()59 virtual std::string ToString() { 60 std::ostringstream ss; 61 ss << "Device " << device_ << " Cid 0x" << std::hex << cid_; 62 return ss.str(); 63 } 64 GetQueueUpEnd()65 common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() { 66 return channel_queue_.GetUpEnd(); 67 } 68 GetQueueDownEnd()69 common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueDownEnd() { 70 return channel_queue_.GetDownEnd(); 71 } 72 GetCid()73 Cid GetCid() const { 74 return cid_; 75 } 76 GetRemoteCid()77 Cid GetRemoteCid() const { 78 return cid_; 79 } 80 81 private: 82 // Constructor states 83 // For logging purpose only 84 const Cid cid_; 85 // For logging purpose only 86 const hci::AddressWithType device_; 87 // Needed to handle Acquire() and Release() 88 Link* link_; 89 os::Handler* l2cap_handler_; 90 91 // User supported states 92 os::Handler* user_handler_ = nullptr; 93 FixedChannel::OnCloseCallback on_close_callback_{}; 94 95 // Internal states 96 bool acquired_ = false; 97 bool closed_ = false; 98 hci::ErrorCode close_reason_ = hci::ErrorCode::SUCCESS; 99 static constexpr size_t kChannelQueueSize = 10; 100 common::BidiQueue<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder> channel_queue_{ 101 kChannelQueueSize}; 102 }; 103 104 } // namespace internal 105 } // namespace classic 106 } // namespace l2cap 107 } // namespace bluetooth 108