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