Jack He | 580337d | 2019-10-01 20:44:04 -0700 | [diff] [blame] | 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 "l2cap/security_policy.h" |
| 25 | #include "os/handler.h" |
| 26 | #include "os/log.h" |
| 27 | |
| 28 | namespace bluetooth { |
| 29 | namespace l2cap { |
| 30 | namespace internal { |
| 31 | |
| 32 | // Helper class for keeping channels in a Link. It allocates and frees Channel object, and supports querying whether a |
| 33 | // channel is in use |
| 34 | template <typename FixedChannelImplType, typename LinkType> |
| 35 | class FixedChannelAllocator { |
| 36 | public: |
| 37 | FixedChannelAllocator(LinkType* link, os::Handler* l2cap_handler) : link_(link), l2cap_handler_(l2cap_handler) { |
| 38 | ASSERT(link_ != nullptr); |
| 39 | ASSERT(l2cap_handler_ != nullptr); |
| 40 | } |
| 41 | |
| 42 | virtual ~FixedChannelAllocator() = default; |
| 43 | |
| 44 | // Allocates a channel. If cid is used, return nullptr. NOTE: The returned BaseFixedChannelImpl object is still |
| 45 | // owned by the channel allocator, NOT the client. |
| 46 | virtual std::shared_ptr<FixedChannelImplType> AllocateChannel(Cid cid, SecurityPolicy security_policy) { |
Jack He | 0aca2b2 | 2019-11-21 15:27:19 -0800 | [diff] [blame] | 47 | ASSERT_LOG(!IsChannelAllocated((cid)), "Cid 0x%x for link %s is already in use", cid, link_->ToString().c_str()); |
Jack He | 580337d | 2019-10-01 20:44:04 -0700 | [diff] [blame] | 48 | ASSERT_LOG(cid >= kFirstFixedChannel && cid <= kLastFixedChannel, "Cid %d out of bound", cid); |
| 49 | auto elem = channels_.try_emplace(cid, std::make_shared<FixedChannelImplType>(cid, link_, l2cap_handler_)); |
Jack He | 0aca2b2 | 2019-11-21 15:27:19 -0800 | [diff] [blame] | 50 | ASSERT_LOG(elem.second, "Failed to create channel for cid 0x%x link %s", cid, link_->ToString().c_str()); |
Jack He | 580337d | 2019-10-01 20:44:04 -0700 | [diff] [blame] | 51 | ASSERT(elem.first->second != nullptr); |
| 52 | return elem.first->second; |
| 53 | } |
| 54 | |
| 55 | // Frees a channel. If cid doesn't exist, it will crash |
| 56 | virtual void FreeChannel(Cid cid) { |
Jack He | 0aca2b2 | 2019-11-21 15:27:19 -0800 | [diff] [blame] | 57 | ASSERT_LOG(IsChannelAllocated(cid), "Channel is not in use: cid %d, link %s", cid, link_->ToString().c_str()); |
Jack He | 580337d | 2019-10-01 20:44:04 -0700 | [diff] [blame] | 58 | channels_.erase(cid); |
| 59 | } |
| 60 | |
| 61 | virtual bool IsChannelAllocated(Cid cid) const { |
| 62 | return channels_.find(cid) != channels_.end(); |
| 63 | } |
| 64 | |
| 65 | virtual std::shared_ptr<FixedChannelImplType> FindChannel(Cid cid) { |
Jack He | 0aca2b2 | 2019-11-21 15:27:19 -0800 | [diff] [blame] | 66 | ASSERT_LOG(IsChannelAllocated(cid), "Channel is not in use: cid %d, link %s", cid, link_->ToString().c_str()); |
Jack He | 580337d | 2019-10-01 20:44:04 -0700 | [diff] [blame] | 67 | return channels_.find(cid)->second; |
| 68 | } |
| 69 | |
| 70 | virtual size_t NumberOfChannels() const { |
| 71 | return channels_.size(); |
| 72 | } |
| 73 | |
| 74 | virtual void OnAclDisconnected(hci::ErrorCode hci_status) { |
| 75 | for (auto& elem : channels_) { |
| 76 | elem.second->OnClosed(hci_status); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | virtual int GetRefCount() { |
| 81 | int ref_count = 0; |
| 82 | for (auto& elem : channels_) { |
| 83 | if (elem.second->IsAcquired()) { |
| 84 | ref_count++; |
| 85 | } |
| 86 | } |
| 87 | return ref_count; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | LinkType* link_; |
| 92 | os::Handler* l2cap_handler_; |
| 93 | std::unordered_map<Cid, std::shared_ptr<FixedChannelImplType>> channels_; |
| 94 | }; |
| 95 | |
| 96 | } // namespace internal |
| 97 | } // namespace l2cap |
| 98 | } // namespace bluetooth |