Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 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 | |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 19 | #include "l2cap/cid.h" |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 20 | #include "l2cap/classic/internal/link.h" |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 21 | #include "l2cap/internal/dynamic_channel_allocator.h" |
| 22 | #include "l2cap/internal/dynamic_channel_impl.h" |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 23 | #include "l2cap/security_policy.h" |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 24 | #include "os/log.h" |
| 25 | |
| 26 | namespace bluetooth { |
| 27 | namespace l2cap { |
| 28 | namespace internal { |
| 29 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 30 | std::shared_ptr<DynamicChannelImpl> DynamicChannelAllocator::AllocateChannel(Psm psm, Cid remote_cid, |
| 31 | SecurityPolicy security_policy) { |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 32 | 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 Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 35 | 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 Zhang | d1b6c9b | 2019-10-22 16:11:53 -0700 | [diff] [blame] | 41 | if (used_cid_.find(cid) == used_cid_.end()) break; |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 42 | } |
| 43 | if (cid > kLastDynamicChannel) { |
| 44 | LOG_WARN("All cid are used"); |
| 45 | return nullptr; |
| 46 | } |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 47 | auto elem = |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 48 | channels_.try_emplace(cid, std::make_shared<DynamicChannelImpl>(psm, cid, remote_cid, link_, l2cap_handler_)); |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 49 | 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 Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 52 | used_remote_cid_.insert(remote_cid); |
Hansong Zhang | d1b6c9b | 2019-10-22 16:11:53 -0700 | [diff] [blame] | 53 | used_cid_.insert(cid); |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 54 | return elem.first->second; |
| 55 | } |
| 56 | |
Hansong Zhang | d1b6c9b | 2019-10-22 16:11:53 -0700 | [diff] [blame] | 57 | std::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 | |
| 76 | Cid 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 Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 89 | void 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 Zhang | d1b6c9b | 2019-10-22 16:11:53 -0700 | [diff] [blame] | 97 | used_cid_.erase(cid); |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 100 | bool 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 Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 109 | std::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 Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Chris Manton | 8b39a22 | 2019-10-31 20:39:11 -0700 | [diff] [blame] | 117 | std::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 He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 126 | size_t DynamicChannelAllocator::NumberOfChannels() const { |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 127 | return channels_.size(); |
| 128 | } |
| 129 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 130 | void DynamicChannelAllocator::OnAclDisconnected(hci::ErrorCode reason) { |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 131 | for (auto& elem : channels_) { |
| 132 | elem.second->OnClosed(reason); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } // namespace internal |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 137 | } // namespace l2cap |
| 138 | } // namespace bluetooth |