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 | #pragma once |
| 18 | |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 19 | #include <unordered_map> |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 20 | #include <unordered_set> |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 21 | |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 22 | #include "hci/acl_manager.h" |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 23 | #include "l2cap/cid.h" |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 24 | #include "l2cap/internal/ilink.h" |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 25 | #include "l2cap/psm.h" |
| 26 | #include "l2cap/security_policy.h" |
| 27 | #include "os/handler.h" |
| 28 | #include "os/log.h" |
| 29 | |
| 30 | namespace bluetooth { |
| 31 | namespace l2cap { |
| 32 | namespace internal { |
| 33 | |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 34 | class DynamicChannelImpl; |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 35 | |
| 36 | // Helper class for keeping channels in a Link. It allocates and frees Channel object, and supports querying whether a |
| 37 | // channel is in use |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 38 | class DynamicChannelAllocator { |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 39 | public: |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 40 | DynamicChannelAllocator(l2cap::internal::ILink* link, os::Handler* l2cap_handler) |
| 41 | : link_(link), l2cap_handler_(l2cap_handler) { |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 42 | ASSERT(link_ != nullptr); |
| 43 | ASSERT(l2cap_handler_ != nullptr); |
| 44 | } |
| 45 | |
| 46 | // Allocates a channel. If psm is used, OR the remote cid already exists, return nullptr. |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 47 | // NOTE: The returned DynamicChannelImpl object is still owned by the channel allocator, NOT the client. |
| 48 | std::shared_ptr<DynamicChannelImpl> AllocateChannel(Psm psm, Cid remote_cid, SecurityPolicy security_policy); |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 49 | |
Hansong Zhang | d1b6c9b | 2019-10-22 16:11:53 -0700 | [diff] [blame] | 50 | std::shared_ptr<DynamicChannelImpl> AllocateReservedChannel(Cid reserved_cid, Psm psm, Cid remote_cid, |
| 51 | SecurityPolicy security_policy); |
| 52 | |
| 53 | // Gives an unused Cid to be used for opening a channel. If a channel is used, call AllocateReservedChannel. If no |
| 54 | // longer needed, use FreeChannel. |
| 55 | Cid ReserveChannel(); |
| 56 | |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 57 | // Frees a channel. If psm doesn't exist, it will crash |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 58 | void FreeChannel(Cid cid); |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 59 | |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 60 | bool IsPsmUsed(Psm psm) const; |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 61 | |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 62 | std::shared_ptr<DynamicChannelImpl> FindChannelByCid(Cid cid); |
Chris Manton | 8b39a22 | 2019-10-31 20:39:11 -0700 | [diff] [blame] | 63 | std::shared_ptr<DynamicChannelImpl> FindChannelByRemoteCid(Cid cid); |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 64 | |
Hansong Zhang | d1b6c9b | 2019-10-22 16:11:53 -0700 | [diff] [blame] | 65 | // Returns number of open, but not reserved channels |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 66 | size_t NumberOfChannels() const; |
| 67 | |
| 68 | void OnAclDisconnected(hci::ErrorCode hci_status); |
| 69 | |
| 70 | private: |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 71 | l2cap::internal::ILink* link_; |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 72 | os::Handler* l2cap_handler_; |
Hansong Zhang | d1b6c9b | 2019-10-22 16:11:53 -0700 | [diff] [blame] | 73 | std::unordered_set<Cid> used_cid_; |
Hansong Zhang | cd0c319 | 2019-09-20 15:50:42 -0700 | [diff] [blame] | 74 | std::unordered_map<Cid, std::shared_ptr<DynamicChannelImpl>> channels_; |
| 75 | std::unordered_set<Cid> used_remote_cid_; |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace internal |
Hansong Zhang | ff4f4a4 | 2019-10-01 11:06:32 -0700 | [diff] [blame] | 79 | } // namespace l2cap |
| 80 | } // namespace bluetooth |