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 <type_traits> 20 #include <unordered_map> 21 22 #include "hci/hci_packets.h" 23 #include "l2cap/cid.h" 24 #include "os/handler.h" 25 #include "os/log.h" 26 27 namespace bluetooth { 28 namespace l2cap { 29 30 namespace classic { 31 namespace internal { 32 class DumpsysHelper; 33 } // namespace internal 34 } // namespace classic 35 36 namespace internal { 37 38 // Helper class for keeping channels in a Link. It allocates and frees Channel object, and supports querying whether a 39 // channel is in use 40 template <typename FixedChannelImplType, typename LinkType> 41 class FixedChannelAllocator { 42 public: FixedChannelAllocator(LinkType * link,os::Handler * l2cap_handler)43 FixedChannelAllocator(LinkType* link, os::Handler* l2cap_handler) : link_(link), l2cap_handler_(l2cap_handler) { 44 log::assert_that(link_ != nullptr, "assert failed: link_ != nullptr"); 45 log::assert_that(l2cap_handler_ != nullptr, "assert failed: l2cap_handler_ != nullptr"); 46 } 47 48 virtual ~FixedChannelAllocator() = default; 49 50 // Allocates a channel. If cid is used, return nullptr. NOTE: The returned BaseFixedChannelImpl object is still 51 // owned by the channel allocator, NOT the client. AllocateChannel(Cid cid)52 virtual std::shared_ptr<FixedChannelImplType> AllocateChannel(Cid cid) { 53 log::assert_that( 54 !IsChannelAllocated(cid), 55 "Cid 0x{:x} for link {} is already in use", 56 cid, 57 ToLoggableStr(*link_)); 58 59 log::assert_that( 60 cid >= kFirstFixedChannel && cid <= kLastFixedChannel, "Cid {} out of bound", cid); 61 auto elem = channels_.try_emplace(cid, std::make_shared<FixedChannelImplType>(cid, link_, l2cap_handler_)); 62 log::assert_that( 63 elem.second, 64 "Failed to create channel for cid 0x{:x} link {}", 65 cid, 66 ToLoggableStr(*link_)); // TODO RENAME ADDRESS_TO_LOGGABLE_CSTR 67 log::assert_that(elem.first->second != nullptr, "assert failed: elem.first->second != nullptr"); 68 return elem.first->second; 69 } 70 71 // Frees a channel. If cid doesn't exist, it will crash FreeChannel(Cid cid)72 virtual void FreeChannel(Cid cid) { 73 log::assert_that( 74 IsChannelAllocated(cid), 75 "Channel is not in use: cid {}, link {}", 76 cid, 77 ToLoggableStr(*link_)); 78 79 channels_.erase(cid); 80 } 81 IsChannelAllocated(Cid cid)82 virtual bool IsChannelAllocated(Cid cid) const { 83 return channels_.find(cid) != channels_.end(); 84 } 85 FindChannel(Cid cid)86 virtual std::shared_ptr<FixedChannelImplType> FindChannel(Cid cid) { 87 log::assert_that( 88 IsChannelAllocated(cid), 89 "Channel is not in use: cid {}, link {}", 90 cid, 91 ToLoggableStr(*link_)); 92 93 return channels_.find(cid)->second; 94 } 95 NumberOfChannels()96 virtual size_t NumberOfChannels() const { 97 return channels_.size(); 98 } 99 OnAclDisconnected(hci::ErrorCode hci_status)100 virtual void OnAclDisconnected(hci::ErrorCode hci_status) { 101 for (auto& elem : channels_) { 102 elem.second->OnClosed(hci_status); 103 } 104 } 105 GetRefCount()106 virtual int GetRefCount() { 107 int ref_count = 0; 108 for (auto& elem : channels_) { 109 if (elem.second->IsAcquired()) { 110 ref_count++; 111 } 112 } 113 return ref_count; 114 } 115 116 private: 117 friend class bluetooth::l2cap::classic::internal::DumpsysHelper; 118 LinkType* link_; 119 os::Handler* l2cap_handler_; 120 std::unordered_map<Cid, std::shared_ptr<FixedChannelImplType>> channels_; 121 }; 122 123 } // namespace internal 124 } // namespace l2cap 125 } // namespace bluetooth 126