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