• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ASSERT(link_ != nullptr);
45     ASSERT(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     ASSERT_LOG(!IsChannelAllocated((cid)), "Cid 0x%x for link %s is already in use", cid, link_->ToString().c_str());
54     ASSERT_LOG(cid >= kFirstFixedChannel && cid <= kLastFixedChannel, "Cid %d out of bound", cid);
55     auto elem = channels_.try_emplace(cid, std::make_shared<FixedChannelImplType>(cid, link_, l2cap_handler_));
56     ASSERT_LOG(elem.second, "Failed to create channel for cid 0x%x link %s", cid, link_->ToString().c_str());
57     ASSERT(elem.first->second != nullptr);
58     return elem.first->second;
59   }
60 
61   // Frees a channel. If cid doesn't exist, it will crash
FreeChannel(Cid cid)62   virtual void FreeChannel(Cid cid) {
63     ASSERT_LOG(IsChannelAllocated(cid), "Channel is not in use: cid %d, link %s", cid, link_->ToString().c_str());
64     channels_.erase(cid);
65   }
66 
IsChannelAllocated(Cid cid)67   virtual bool IsChannelAllocated(Cid cid) const {
68     return channels_.find(cid) != channels_.end();
69   }
70 
FindChannel(Cid cid)71   virtual std::shared_ptr<FixedChannelImplType> FindChannel(Cid cid) {
72     ASSERT_LOG(IsChannelAllocated(cid), "Channel is not in use: cid %d, link %s", cid, link_->ToString().c_str());
73     return channels_.find(cid)->second;
74   }
75 
NumberOfChannels()76   virtual size_t NumberOfChannels() const {
77     return channels_.size();
78   }
79 
OnAclDisconnected(hci::ErrorCode hci_status)80   virtual void OnAclDisconnected(hci::ErrorCode hci_status) {
81     for (auto& elem : channels_) {
82       elem.second->OnClosed(hci_status);
83     }
84   }
85 
GetRefCount()86   virtual int GetRefCount() {
87     int ref_count = 0;
88     for (auto& elem : channels_) {
89       if (elem.second->IsAcquired()) {
90         ref_count++;
91       }
92     }
93     return ref_count;
94   }
95 
96  private:
97   friend class bluetooth::l2cap::classic::internal::DumpsysHelper;
98   LinkType* link_;
99   os::Handler* l2cap_handler_;
100   std::unordered_map<Cid, std::shared_ptr<FixedChannelImplType>> channels_;
101 };
102 
103 }  // namespace internal
104 }  // namespace l2cap
105 }  // namespace bluetooth
106