• 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 #include <unordered_map>
18 
19 #include "l2cap/cid.h"
20 #include "l2cap/classic/internal/link.h"
21 #include "l2cap/internal/dynamic_channel_allocator.h"
22 #include "l2cap/internal/dynamic_channel_impl.h"
23 #include "l2cap/security_policy.h"
24 #include "os/log.h"
25 
26 namespace bluetooth {
27 namespace l2cap {
28 namespace internal {
29 
AllocateChannel(Psm psm,Cid remote_cid,SecurityPolicy security_policy)30 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::AllocateChannel(Psm psm, Cid remote_cid,
31                                                                              SecurityPolicy security_policy) {
32   ASSERT_LOG(IsPsmValid(psm), "Psm 0x%x is invalid", psm);
33 
34   if (used_remote_cid_.find(remote_cid) != used_remote_cid_.end()) {
35     LOG_INFO("Remote cid 0x%x is used", remote_cid);
36     return nullptr;
37   }
38   Cid cid = kFirstDynamicChannel;
39   for (; cid <= kLastDynamicChannel; cid++) {
40     if (used_cid_.find(cid) == used_cid_.end()) break;
41   }
42   if (cid > kLastDynamicChannel) {
43     LOG_WARN("All cid are used");
44     return nullptr;
45   }
46   auto elem =
47       channels_.try_emplace(cid, std::make_shared<DynamicChannelImpl>(psm, cid, remote_cid, link_, l2cap_handler_));
48   ASSERT_LOG(elem.second, "Failed to create channel for psm 0x%x device %s", psm,
49              link_->GetDevice().ToString().c_str());
50   ASSERT(elem.first->second != nullptr);
51   used_remote_cid_.insert(remote_cid);
52   used_cid_.insert(cid);
53   return elem.first->second;
54 }
55 
AllocateReservedChannel(Cid reserved_cid,Psm psm,Cid remote_cid,SecurityPolicy security_policy)56 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::AllocateReservedChannel(Cid reserved_cid, Psm psm,
57                                                                                      Cid remote_cid,
58                                                                                      SecurityPolicy security_policy) {
59   ASSERT_LOG(IsPsmValid(psm), "Psm 0x%x is invalid", psm);
60 
61   if (used_remote_cid_.find(remote_cid) != used_remote_cid_.end()) {
62     LOG_INFO("Remote cid 0x%x is used", remote_cid);
63     return nullptr;
64   }
65   auto elem = channels_.try_emplace(
66       reserved_cid, std::make_shared<DynamicChannelImpl>(psm, reserved_cid, remote_cid, link_, l2cap_handler_));
67   ASSERT_LOG(elem.second, "Failed to create channel for psm 0x%x device %s", psm,
68              link_->GetDevice().ToString().c_str());
69   ASSERT(elem.first->second != nullptr);
70   used_remote_cid_.insert(remote_cid);
71   return elem.first->second;
72 }
73 
ReserveChannel()74 Cid DynamicChannelAllocator::ReserveChannel() {
75   Cid cid = kFirstDynamicChannel;
76   for (; cid <= kLastDynamicChannel; cid++) {
77     if (used_cid_.find(cid) == used_cid_.end()) break;
78   }
79   if (cid > kLastDynamicChannel) {
80     LOG_WARN("All cid are used");
81     return kInvalidCid;
82   }
83   used_cid_.insert(cid);
84   return cid;
85 }
86 
FreeChannel(Cid cid)87 void DynamicChannelAllocator::FreeChannel(Cid cid) {
88   used_cid_.erase(cid);
89   auto channel = FindChannelByCid(cid);
90   if (channel == nullptr) {
91     LOG_INFO("Channel is not in use: psm %d, device %s", cid, link_->GetDevice().ToString().c_str());
92     return;
93   }
94   used_remote_cid_.erase(channel->GetRemoteCid());
95   channels_.erase(cid);
96 }
97 
IsPsmUsed(Psm psm) const98 bool DynamicChannelAllocator::IsPsmUsed(Psm psm) const {
99   for (const auto& channel : channels_) {
100     if (channel.second->GetPsm() == psm) {
101       return true;
102     }
103   }
104   return false;
105 }
106 
FindChannelByCid(Cid cid)107 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByCid(Cid cid) {
108   if (channels_.find(cid) == channels_.end()) {
109     LOG_WARN("Can't find cid %d", cid);
110     return nullptr;
111   }
112   return channels_.find(cid)->second;
113 }
114 
FindChannelByRemoteCid(Cid remote_cid)115 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByRemoteCid(Cid remote_cid) {
116   for (auto& channel : channels_) {
117     if (channel.second->GetRemoteCid() == remote_cid) {
118       return channel.second;
119     }
120   }
121   return nullptr;
122 }
123 
NumberOfChannels() const124 size_t DynamicChannelAllocator::NumberOfChannels() const {
125   return channels_.size();
126 }
127 
OnAclDisconnected(hci::ErrorCode reason)128 void DynamicChannelAllocator::OnAclDisconnected(hci::ErrorCode reason) {
129   for (auto& elem : channels_) {
130     elem.second->OnClosed(reason);
131   }
132 }
133 
134 }  // namespace internal
135 }  // namespace l2cap
136 }  // namespace bluetooth
137