blob: 6f00274477022d12361560a9e307aad133e4c874 [file] [log] [blame]
Hansong Zhangff4f4a42019-10-01 11:06:32 -07001/*
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
Hansong Zhangff4f4a42019-10-01 11:06:32 -070019#include "l2cap/cid.h"
Jack Heff38d892019-10-03 17:11:07 -070020#include "l2cap/classic/internal/link.h"
Hansong Zhang47dec642019-11-25 12:01:15 -080021#include "l2cap/internal/dynamic_channel_allocator.h"
22#include "l2cap/internal/dynamic_channel_impl.h"
Hansong Zhangff4f4a42019-10-01 11:06:32 -070023#include "l2cap/security_policy.h"
Hansong Zhangff4f4a42019-10-01 11:06:32 -070024#include "os/log.h"
25
26namespace bluetooth {
27namespace l2cap {
28namespace internal {
29
Jack Heff38d892019-10-03 17:11:07 -070030std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::AllocateChannel(Psm psm, Cid remote_cid,
31 SecurityPolicy security_policy) {
Hansong Zhangcd0c3192019-09-20 15:50:42 -070032 ASSERT_LOG(!IsPsmUsed((psm)), "Psm 0x%x for device %s is already in use", psm, link_->GetDevice().ToString().c_str());
33 ASSERT_LOG(IsPsmValid(psm), "Psm 0x%x is invalid", psm);
34
Hansong Zhangff4f4a42019-10-01 11:06:32 -070035 if (used_remote_cid_.find(remote_cid) != used_remote_cid_.end()) {
36 LOG_INFO("Remote cid 0x%x is used", remote_cid);
37 return nullptr;
38 }
39 Cid cid = kFirstDynamicChannel;
40 for (; cid <= kLastDynamicChannel; cid++) {
Hansong Zhangd1b6c9b2019-10-22 16:11:53 -070041 if (used_cid_.find(cid) == used_cid_.end()) break;
Hansong Zhangff4f4a42019-10-01 11:06:32 -070042 }
43 if (cid > kLastDynamicChannel) {
44 LOG_WARN("All cid are used");
45 return nullptr;
46 }
Jack Heff38d892019-10-03 17:11:07 -070047 auto elem =
Hansong Zhangcd0c3192019-09-20 15:50:42 -070048 channels_.try_emplace(cid, std::make_shared<DynamicChannelImpl>(psm, cid, remote_cid, link_, l2cap_handler_));
Hansong Zhangff4f4a42019-10-01 11:06:32 -070049 ASSERT_LOG(elem.second, "Failed to create channel for psm 0x%x device %s", psm,
50 link_->GetDevice().ToString().c_str());
51 ASSERT(elem.first->second != nullptr);
Hansong Zhangff4f4a42019-10-01 11:06:32 -070052 used_remote_cid_.insert(remote_cid);
Hansong Zhangd1b6c9b2019-10-22 16:11:53 -070053 used_cid_.insert(cid);
Hansong Zhangff4f4a42019-10-01 11:06:32 -070054 return elem.first->second;
55}
56
Hansong Zhangd1b6c9b2019-10-22 16:11:53 -070057std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::AllocateReservedChannel(Cid reserved_cid, Psm psm,
58 Cid remote_cid,
59 SecurityPolicy security_policy) {
60 ASSERT_LOG(!IsPsmUsed((psm)), "Psm 0x%x for device %s is already in use", psm, link_->GetDevice().ToString().c_str());
61 ASSERT_LOG(IsPsmValid(psm), "Psm 0x%x is invalid", psm);
62
63 if (used_remote_cid_.find(remote_cid) != used_remote_cid_.end()) {
64 LOG_INFO("Remote cid 0x%x is used", remote_cid);
65 return nullptr;
66 }
67 auto elem = channels_.try_emplace(
68 reserved_cid, std::make_shared<DynamicChannelImpl>(psm, reserved_cid, remote_cid, link_, l2cap_handler_));
69 ASSERT_LOG(elem.second, "Failed to create channel for psm 0x%x device %s", psm,
70 link_->GetDevice().ToString().c_str());
71 ASSERT(elem.first->second != nullptr);
72 used_remote_cid_.insert(remote_cid);
73 return elem.first->second;
74}
75
76Cid DynamicChannelAllocator::ReserveChannel() {
77 Cid cid = kFirstDynamicChannel;
78 for (; cid <= kLastDynamicChannel; cid++) {
79 if (used_cid_.find(cid) == used_cid_.end()) break;
80 }
81 if (cid > kLastDynamicChannel) {
82 LOG_WARN("All cid are used");
83 return kInvalidCid;
84 }
85 used_cid_.insert(cid);
86 return cid;
87}
88
Hansong Zhangcd0c3192019-09-20 15:50:42 -070089void DynamicChannelAllocator::FreeChannel(Cid cid) {
90 auto channel = FindChannelByCid(cid);
91 if (channel == nullptr) {
92 LOG_INFO("Channel is not in use: psm %d, device %s", cid, link_->GetDevice().ToString().c_str());
93 return;
94 }
95 used_remote_cid_.erase(channel->GetRemoteCid());
96 channels_.erase(cid);
Hansong Zhangd1b6c9b2019-10-22 16:11:53 -070097 used_cid_.erase(cid);
Hansong Zhangff4f4a42019-10-01 11:06:32 -070098}
99
Hansong Zhangcd0c3192019-09-20 15:50:42 -0700100bool DynamicChannelAllocator::IsPsmUsed(Psm psm) const {
101 for (const auto& channel : channels_) {
102 if (channel.second->GetPsm() == psm) {
103 return true;
104 }
105 }
106 return false;
Hansong Zhangff4f4a42019-10-01 11:06:32 -0700107}
108
Hansong Zhangcd0c3192019-09-20 15:50:42 -0700109std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByCid(Cid cid) {
110 if (channels_.find(cid) == channels_.end()) {
111 LOG_WARN("Can't find cid %d", cid);
112 return nullptr;
113 }
114 return channels_.find(cid)->second;
Hansong Zhangff4f4a42019-10-01 11:06:32 -0700115}
116
Chris Manton8b39a222019-10-31 20:39:11 -0700117std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::FindChannelByRemoteCid(Cid remote_cid) {
118 for (auto& channel : channels_) {
119 if (channel.second->GetRemoteCid() == remote_cid) {
120 return channel.second;
121 }
122 }
123 return nullptr;
124}
125
Jack Heff38d892019-10-03 17:11:07 -0700126size_t DynamicChannelAllocator::NumberOfChannels() const {
Hansong Zhangff4f4a42019-10-01 11:06:32 -0700127 return channels_.size();
128}
129
Jack Heff38d892019-10-03 17:11:07 -0700130void DynamicChannelAllocator::OnAclDisconnected(hci::ErrorCode reason) {
Hansong Zhangff4f4a42019-10-01 11:06:32 -0700131 for (auto& elem : channels_) {
132 elem.second->OnClosed(reason);
133 }
134}
135
136} // namespace internal
Hansong Zhangff4f4a42019-10-01 11:06:32 -0700137} // namespace l2cap
138} // namespace bluetooth