• 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 <string>
19 
20 #include "hci/acl_manager.h"
21 #include "hci/address_with_type.h"
22 #include "l2cap/cid.h"
23 #include "l2cap/le/fixed_channel.h"
24 #include "l2cap/le/fixed_channel_service.h"
25 #include "l2cap/security_policy.h"
26 #include "os/handler.h"
27 
28 namespace bluetooth {
29 namespace l2cap {
30 namespace le {
31 
32 class L2capLeModule;
33 
34 namespace internal {
35 class LinkManager;
36 class FixedChannelServiceManagerImpl;
37 }  // namespace internal
38 
39 class FixedChannelManager {
40  public:
41   enum class ConnectionResultCode {
42     SUCCESS = 0,
43     FAIL_NO_SERVICE_REGISTERED = 1,      // No service is registered
44     FAIL_ALL_SERVICES_HAVE_CHANNEL = 2,  // All registered services already have a channel
45     FAIL_HCI_ERROR = 3,                  // See hci_error
46   };
47 
48   struct ConnectionResult {
49     ConnectionResultCode connection_result_code = ConnectionResultCode::SUCCESS;
50     hci::ErrorCode hci_error = hci::ErrorCode::SUCCESS;
51   };
52   /**
53    * OnConnectionFailureCallback(ConnectionResult failure_reason);
54    */
55   using OnConnectionFailureCallback = common::OnceCallback<void(ConnectionResult)>;
56 
57   /**
58    * OnConnectionOpenCallback(FixedChannel channel);
59    */
60   using OnConnectionOpenCallback = common::Callback<void(std::unique_ptr<FixedChannel>)>;
61 
62   enum class RegistrationResult {
63     SUCCESS = 0,
64     FAIL_DUPLICATE_SERVICE = 1,  // Duplicate service registration for the same CID
65     FAIL_INVALID_SERVICE = 2,    // Invalid CID
66   };
67 
68   /**
69    * OnRegistrationFailureCallback(RegistrationResult result, FixedChannelService service);
70    */
71   using OnRegistrationCompleteCallback =
72       common::OnceCallback<void(RegistrationResult, std::unique_ptr<FixedChannelService>)>;
73 
74   /**
75    * Connect to ALL fixed channels on a remote device
76    *
77    * - This method is asynchronous
78    * - When false is returned, the connection fails immediately
79    * - When true is returned, method caller should wait for on_fail_callback or on_open_callback registered through
80    *   RegisterService() API.
81    * - If an ACL connection does not exist, this method will create an ACL connection. As a result, on_open_callback
82    *   supplied through RegisterService() will be triggered to provide the actual FixedChannel objects
83    * - If HCI connection failed, on_fail_callback will be triggered with FAIL_HCI_ERROR
84    * - If fixed channel on a remote device is already reported as connected via on_open_callback and has been acquired
85    *   via FixedChannel#Acquire() API, it won't be reported again
86    * - If no service is registered, on_fail_callback will be triggered with FAIL_NO_SERVICE_REGISTERED
87    * - If there is an ACL connection and channels for each service is allocated, on_fail_callback will be triggered with
88    *   FAIL_ALL_SERVICES_HAVE_CHANNEL
89    *
90    * NOTE:
91    * This call will initiate an effort to connect all fixed channel services on a remote device.
92    * Due to the connectionless nature of fixed channels, all fixed channels will be connected together.
93    * If a fixed channel service does not need a particular fixed channel. It should release the received
94    * channel immediately after receiving on_open_callback via FixedChannel#Release()
95    *
96    * A module calling ConnectServices() must have called RegisterService() before.
97    * The callback will come back from on_open_callback in the service that is registered
98    *
99    * @param address_with_type: Remote device with type to make this connection.
100    * @param address_type: Address type of remote device
101    * @param on_fail_callback: A callback to indicate connection failure along with a status code.
102    * @param handler: The handler context in which to execute the @callback parameters.
103    *
104    * Returns: true if connection was able to be initiated, false otherwise.
105    */
106   bool ConnectServices(hci::AddressWithType address_with_type, OnConnectionFailureCallback on_fail_callback,
107                        os::Handler* handler);
108 
109   /**
110    * Register a service to receive incoming connections bound to a specific channel.
111    *
112    * - This method is asynchronous.
113    * - When false is returned, the registration fails immediately.
114    * - When true is returned, method caller should wait for on_service_registered callback that contains a
115    *   FixedChannelService object. The registered service can be managed from that object.
116    * - If a CID is already registered or some other error happens, on_registration_complete will be triggered with a
117    *   non-SUCCESS value
118    * - After a service is registered, any classic ACL connection will create a FixedChannel object that is
119    *   delivered through on_open_callback
120    * - on_open_callback, will only be triggered after on_service_registered callback
121    *
122    * @param cid:  cid used to receive incoming connections
123    * @param security_policy: The security policy used for the connection.
124    * @param on_registration_complete: A callback to indicate the service setup has completed. If the return status is
125    *        not SUCCESS, it means service is not registered due to reasons like CID already take
126    * @param on_open_callback: A callback to indicate success of a connection initiated from a remote device.
127    * @param handler: The handler context in which to execute the @callback parameter.
128    */
129   bool RegisterService(Cid cid, const SecurityPolicy& security_policy,
130                        OnRegistrationCompleteCallback on_registration_complete,
131                        OnConnectionOpenCallback on_connection_open, os::Handler* handler);
132 
133   friend class L2capLeModule;
134 
135  private:
136   // The constructor is not to be used by user code
FixedChannelManager(internal::FixedChannelServiceManagerImpl * service_manager,internal::LinkManager * link_manager,os::Handler * l2cap_layer_handler)137   FixedChannelManager(internal::FixedChannelServiceManagerImpl* service_manager, internal::LinkManager* link_manager,
138                       os::Handler* l2cap_layer_handler)
139       : service_manager_(service_manager), link_manager_(link_manager), l2cap_layer_handler_(l2cap_layer_handler) {}
140   internal::FixedChannelServiceManagerImpl* service_manager_ = nullptr;
141   internal::LinkManager* link_manager_ = nullptr;
142   os::Handler* l2cap_layer_handler_ = nullptr;
143   DISALLOW_COPY_AND_ASSIGN(FixedChannelManager);
144 };
145 
146 }  // namespace le
147 }  // namespace l2cap
148 }  // namespace bluetooth
149