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(elem.second, "Failed to create channel for psm 0x%x device %s", psm,
46 link_->GetDevice().ToString().c_str());
47 ASSERT(elem.first->second != nullptr);
48 used_remote_cid_.insert(remote_cid);
49 used_cid_.insert(cid);
50 return elem.first->second;
51 }
52
AllocateReservedChannel(Cid reserved_cid,Psm psm,Cid remote_cid)53 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::AllocateReservedChannel(Cid reserved_cid, Psm psm,
54 Cid remote_cid) {
55 if (used_remote_cid_.find(remote_cid) != used_remote_cid_.end()) {
56 LOG_INFO("Remote cid 0x%x is used", remote_cid);
57 return nullptr;
58 }
59 auto elem = channels_.try_emplace(
60 reserved_cid, std::make_shared<DynamicChannelImpl>(psm, reserved_cid, remote_cid, link_, l2cap_handler_));
61 ASSERT_LOG(elem.second, "Failed to create channel for psm 0x%x device %s", psm,
62 link_->GetDevice().ToString().c_str());
63 ASSERT(elem.first->second != nullptr);
64 used_remote_cid_.insert(remote_cid);
65 return elem.first->second;
66 }
67
ReserveChannel()68 Cid DynamicChannelAllocator::ReserveChannel() {
69 Cid cid = kFirstDynamicChannel;
70 for (; cid <= kLastDynamicChannel; cid++) {
71 if (used_cid_.find(cid) == used_cid_.end()) break;
72 }
73 if (cid > kLastDynamicChannel) {
74 LOG_WARN("All cid are used");
75 return kInvalidCid;
76 }
77 used_cid_.insert(cid);
78 return cid;
79 }
80
FreeChannel(Cid cid)81 void DynamicChannelAllocator::FreeChannel(Cid cid) {
82 used_cid_.erase(cid);
83 auto channel = FindChannelByCid(cid);
84 if (channel == nullptr) {
85 LOG_INFO("Channel is not in use: cid %d, device %s", cid, link_->GetDevice().ToString().c_str());
86 return;
87 }
88 used_remote_cid_.erase(channel->GetRemoteCid());
89 channels_.erase(cid);
90 }
91
IsPsmUsed(Psm psm) const92 bool DynamicChannelAllocator::IsPsmUsed(Psm psm) const {
93 for (const auto& channel : channels_) {
94 if (channel.second->GetPsm() == psm) {
95 return true;
96 }
97 }
98 return false;
99 }
100
FindChannelByCid(Cid cid)101 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByCid(Cid cid) {
102 if (channels_.find(cid) == channels_.end()) {
103 LOG_WARN("Can't find cid %d", cid);
104 return nullptr;
105 }
106 return channels_.find(cid)->second;
107 }
108
FindChannelByRemoteCid(Cid remote_cid)109 std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByRemoteCid(Cid remote_cid) {
110 for (auto& channel : channels_) {
111 if (channel.second->GetRemoteCid() == remote_cid) {
112 return channel.second;
113 }
114 }
115 return nullptr;
116 }
117
NumberOfChannels() const118 size_t DynamicChannelAllocator::NumberOfChannels() const {
119 return channels_.size();
120 }
121
OnAclDisconnected(hci::ErrorCode reason)122 void DynamicChannelAllocator::OnAclDisconnected(hci::ErrorCode reason) {
123 for (auto& elem : channels_) {
124 elem.second->OnClosed(reason);
125 }
126 }
127
128 } // namespace internal
129 } // namespace l2cap
130 } // namespace bluetooth
131