Hansong Zhang | c229758 | 2019-09-20 14:14:01 -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 | #pragma once |
| 17 | |
| 18 | #include "common/bidi_queue.h" |
| 19 | #include "common/callback.h" |
| 20 | #include "hci/acl_manager.h" |
| 21 | #include "os/handler.h" |
| 22 | #include "packet/base_packet_builder.h" |
| 23 | #include "packet/packet_view.h" |
| 24 | |
| 25 | namespace bluetooth { |
| 26 | namespace l2cap { |
| 27 | |
| 28 | namespace internal { |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 29 | class DynamicChannelImpl; |
Hansong Zhang | c229758 | 2019-09-20 14:14:01 -0700 | [diff] [blame] | 30 | } // namespace internal |
| 31 | |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 32 | namespace classic { |
| 33 | |
Hansong Zhang | c229758 | 2019-09-20 14:14:01 -0700 | [diff] [blame] | 34 | /** |
| 35 | * L2CAP Dynamic channel object. User needs to call Close() when user no longer wants to use it. Otherwise the link |
| 36 | * won't be disconnected. |
| 37 | */ |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 38 | class DynamicChannel { |
Hansong Zhang | c229758 | 2019-09-20 14:14:01 -0700 | [diff] [blame] | 39 | public: |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 40 | // Should only be constructed by modules that have access to LinkManager |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 41 | DynamicChannel(std::shared_ptr<l2cap::internal::DynamicChannelImpl> impl, os::Handler* l2cap_handler) |
Hansong Zhang | c229758 | 2019-09-20 14:14:01 -0700 | [diff] [blame] | 42 | : impl_(std::move(impl)), l2cap_handler_(l2cap_handler) { |
| 43 | ASSERT(impl_ != nullptr); |
| 44 | ASSERT(l2cap_handler_ != nullptr); |
| 45 | } |
| 46 | |
| 47 | hci::Address GetDevice() const; |
| 48 | |
| 49 | /** |
| 50 | * Register close callback. If close callback is registered, when a channel is closed, the channel's resource will |
| 51 | * only be freed after on_close callback is invoked. Otherwise, if no on_close callback is registered, the channel's |
| 52 | * resource will be freed immediately after closing. |
| 53 | * |
| 54 | * @param user_handler The handler used to invoke the callback on |
| 55 | * @param on_close_callback The callback invoked upon channel closing. |
| 56 | */ |
| 57 | using OnCloseCallback = common::OnceCallback<void(hci::ErrorCode)>; |
| 58 | void RegisterOnCloseCallback(os::Handler* user_handler, OnCloseCallback on_close_callback); |
| 59 | |
| 60 | /** |
| 61 | * Indicate that this Dynamic Channel should be closed. OnCloseCallback will be invoked when channel close is done. |
| 62 | * L2cay layer may terminate this ACL connection to free the resource after channel is closed. |
| 63 | */ |
| 64 | void Close(); |
| 65 | |
| 66 | /** |
| 67 | * This method will retrieve the data channel queue to send and receive packets. |
| 68 | * |
| 69 | * {@see BidiQueueEnd} |
| 70 | * |
| 71 | * @return The upper end of a bi-directional queue. |
| 72 | */ |
| 73 | common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() const; |
| 74 | |
| 75 | private: |
Hansong Zhang | 47dec64 | 2019-11-25 12:01:15 -0800 | [diff] [blame^] | 76 | std::shared_ptr<l2cap::internal::DynamicChannelImpl> impl_; |
Hansong Zhang | c229758 | 2019-09-20 14:14:01 -0700 | [diff] [blame] | 77 | os::Handler* l2cap_handler_; |
| 78 | }; |
| 79 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 80 | } // namespace classic |
Hansong Zhang | c229758 | 2019-09-20 14:14:01 -0700 | [diff] [blame] | 81 | } // namespace l2cap |
| 82 | } // namespace bluetooth |