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