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 "l2cap/cid.h" 22 #include "os/handler.h" 23 #include "packet/base_packet_builder.h" 24 #include "packet/packet_view.h" 25 26 namespace bluetooth { 27 namespace l2cap { 28 namespace le { 29 30 namespace internal { 31 class FixedChannelImpl; 32 } // namespace internal 33 34 /** 35 * L2CAP fixed channel object. When a new object is created, it must be 36 * acquired through calling {@link FixedChannel#Acquire()} within X seconds. 37 * Otherwise, {@link FixedChannel#Release()} will be called automatically. 38 * 39 */ 40 class FixedChannel { 41 public: 42 // Should only be constructed by modules that have access to LinkManager FixedChannel(std::shared_ptr<internal::FixedChannelImpl> impl,os::Handler * l2cap_handler)43 FixedChannel(std::shared_ptr<internal::FixedChannelImpl> impl, os::Handler* l2cap_handler) 44 : impl_(std::move(impl)), l2cap_handler_(l2cap_handler) { 45 ASSERT(impl_ != nullptr); 46 ASSERT(l2cap_handler_ != nullptr); 47 } 48 49 hci::AddressWithType GetDevice() const; 50 51 /** 52 * Return the role we have in the associated link 53 */ 54 hci::Role GetRole() const; 55 56 hci::AclConnection* GetAclConnection() const; 57 58 /** 59 * Register close callback. If close callback is registered, when a channel is closed, the channel's resource will 60 * only be freed after on_close callback is invoked. Otherwise, if no on_close callback is registered, the channel's 61 * resource will be freed immediately after closing. 62 * 63 * @param user_handler The handler used to invoke the callback on 64 * @param on_close_callback The callback invoked upon channel closing. 65 */ 66 using OnCloseCallback = common::OnceCallback<void(hci::ErrorCode)>; 67 void RegisterOnCloseCallback(os::Handler* user_handler, OnCloseCallback on_close_callback); 68 69 /** 70 * Indicate that this Fixed Channel is being used. This will prevent ACL connection from being disconnected. 71 */ 72 void Acquire(); 73 74 /** 75 * Indicate that this Fixed Channel is no longer being used. ACL connection will be disconnected after 76 * kLinkIdleDisconnectTimeout if no other DynamicChannel is connected or no other Fixed Channel is using this 77 * ACL connection. However a module can still receive data on this channel as long as it remains open. 78 */ 79 void Release(); 80 81 /** 82 * This method will retrieve the data channel queue to send and receive packets. 83 * 84 * {@see BidiQueueEnd} 85 * 86 * @return The upper end of a bi-directional queue. 87 */ 88 common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>* GetQueueUpEnd() const; 89 90 private: 91 std::shared_ptr<internal::FixedChannelImpl> impl_; 92 os::Handler* l2cap_handler_; 93 }; 94 95 } // namespace le 96 } // namespace l2cap 97 } // namespace bluetooth 98