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