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 if (used_remote_cid_.find(remote_cid) != used_remote_cid_.end()) {
32 LOG_INFO("Remote cid 0x%x is used", remote_cid);
33 return nullptr;
34 }
35 Cid cid = kFirstDynamicChannel;
36 for (; cid <= kLastDynamicChannel; cid++) {
37 if (used_cid_.find(cid) == used_cid_.end()) break;
38 }
39 if (cid > kLastDynamicChannel) {
40 LOG_WARN("All cid are used");
41 return nullptr;
42 }
43 auto elem =
44 channels_.try_emplace(cid, std::make_shared<DynamicChannelImpl>(psm, cid, remote_cid, link_, l2cap_handler_));
45 ASSERT_LOG(
46 elem.second,
47 "Failed to create channel for psm 0x%x device %s",
48 psm,
49 ADDRESS_TO_LOGGABLE_CSTR(link_->GetDevice()));
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)56 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::AllocateReservedChannel(Cid reserved_cid, Psm psm,
57 Cid remote_cid) {
58 if (used_remote_cid_.find(remote_cid) != used_remote_cid_.end()) {
59 LOG_INFO("Remote cid 0x%x is used", remote_cid);
60 return nullptr;
61 }
62 auto elem = channels_.try_emplace(
63 reserved_cid, std::make_shared<DynamicChannelImpl>(psm, reserved_cid, remote_cid, link_, l2cap_handler_));
64 ASSERT_LOG(
65 elem.second,
66 "Failed to create channel for psm 0x%x device %s",
67 psm,
68 ADDRESS_TO_LOGGABLE_CSTR(link_->GetDevice()));
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: cid %d, device %s", cid,
92 ADDRESS_TO_LOGGABLE_CSTR(link_->GetDevice()));
93 return;
94 }
95 used_remote_cid_.erase(channel->GetRemoteCid());
96 channels_.erase(cid);
97 }
98
IsPsmUsed(Psm psm) const99 bool DynamicChannelAllocator::IsPsmUsed(Psm psm) const {
100 for (const auto& channel : channels_) {
101 if (channel.second->GetPsm() == psm) {
102 return true;
103 }
104 }
105 return false;
106 }
107
FindChannelByCid(Cid cid)108 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByCid(Cid cid) {
109 if (channels_.find(cid) == channels_.end()) {
110 LOG_WARN("Can't find cid %d", cid);
111 return nullptr;
112 }
113 return channels_.find(cid)->second;
114 }
115
FindChannelByRemoteCid(Cid remote_cid)116 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByRemoteCid(Cid remote_cid) {
117 for (auto& channel : channels_) {
118 if (channel.second->GetRemoteCid() == remote_cid) {
119 return channel.second;
120 }
121 }
122 return nullptr;
123 }
124
NumberOfChannels() const125 size_t DynamicChannelAllocator::NumberOfChannels() const {
126 return channels_.size();
127 }
128
OnAclDisconnected(hci::ErrorCode reason)129 void DynamicChannelAllocator::OnAclDisconnected(hci::ErrorCode reason) {
130 for (auto& elem : channels_) {
131 elem.second->OnClosed(reason);
132 }
133 }
134
135 } // namespace internal
136 } // namespace l2cap
137 } // namespace bluetooth
138