blob: 09a3588373cee13950b42cdb3c22c74c96321347 [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#pragma once
18
Hansong Zhangff4f4a42019-10-01 11:06:32 -070019#include <unordered_map>
Hansong Zhangcd0c3192019-09-20 15:50:42 -070020#include <unordered_set>
Hansong Zhangff4f4a42019-10-01 11:06:32 -070021
Hansong Zhang47dec642019-11-25 12:01:15 -080022#include "hci/acl_manager.h"
Hansong Zhangff4f4a42019-10-01 11:06:32 -070023#include "l2cap/cid.h"
Hansong Zhang47dec642019-11-25 12:01:15 -080024#include "l2cap/internal/ilink.h"
Hansong Zhangff4f4a42019-10-01 11:06:32 -070025#include "l2cap/psm.h"
26#include "l2cap/security_policy.h"
27#include "os/handler.h"
28#include "os/log.h"
29
30namespace bluetooth {
31namespace l2cap {
32namespace internal {
33
Hansong Zhang47dec642019-11-25 12:01:15 -080034class DynamicChannelImpl;
Hansong Zhangff4f4a42019-10-01 11:06:32 -070035
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 Heff38d892019-10-03 17:11:07 -070038class DynamicChannelAllocator {
Hansong Zhangff4f4a42019-10-01 11:06:32 -070039 public:
Hansong Zhang47dec642019-11-25 12:01:15 -080040 DynamicChannelAllocator(l2cap::internal::ILink* link, os::Handler* l2cap_handler)
41 : link_(link), l2cap_handler_(l2cap_handler) {
Hansong Zhangff4f4a42019-10-01 11:06:32 -070042 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 Heff38d892019-10-03 17:11:07 -070047 // 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 Zhangff4f4a42019-10-01 11:06:32 -070049
Hansong Zhangd1b6c9b2019-10-22 16:11:53 -070050 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 Zhangff4f4a42019-10-01 11:06:32 -070057 // Frees a channel. If psm doesn't exist, it will crash
Hansong Zhangcd0c3192019-09-20 15:50:42 -070058 void FreeChannel(Cid cid);
Hansong Zhangff4f4a42019-10-01 11:06:32 -070059
Hansong Zhangcd0c3192019-09-20 15:50:42 -070060 bool IsPsmUsed(Psm psm) const;
Hansong Zhangff4f4a42019-10-01 11:06:32 -070061
Hansong Zhangcd0c3192019-09-20 15:50:42 -070062 std::shared_ptr<DynamicChannelImpl> FindChannelByCid(Cid cid);
Chris Manton8b39a222019-10-31 20:39:11 -070063 std::shared_ptr<DynamicChannelImpl> FindChannelByRemoteCid(Cid cid);
Hansong Zhangff4f4a42019-10-01 11:06:32 -070064
Hansong Zhangd1b6c9b2019-10-22 16:11:53 -070065 // Returns number of open, but not reserved channels
Hansong Zhangff4f4a42019-10-01 11:06:32 -070066 size_t NumberOfChannels() const;
67
68 void OnAclDisconnected(hci::ErrorCode hci_status);
69
70 private:
Hansong Zhang47dec642019-11-25 12:01:15 -080071 l2cap::internal::ILink* link_;
Hansong Zhangff4f4a42019-10-01 11:06:32 -070072 os::Handler* l2cap_handler_;
Hansong Zhangd1b6c9b2019-10-22 16:11:53 -070073 std::unordered_set<Cid> used_cid_;
Hansong Zhangcd0c3192019-09-20 15:50:42 -070074 std::unordered_map<Cid, std::shared_ptr<DynamicChannelImpl>> channels_;
75 std::unordered_set<Cid> used_remote_cid_;
Hansong Zhangff4f4a42019-10-01 11:06:32 -070076};
77
78} // namespace internal
Hansong Zhangff4f4a42019-10-01 11:06:32 -070079} // namespace l2cap
80} // namespace bluetooth