blob: 65c0b7341189df75ecb3b7e93bb30d2dcc414d81 [file] [log] [blame]
Jack Heb4b4a372019-08-09 15:10:44 -07001/*
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
Jack Heb5360fb2019-08-19 18:16:33 -070020#include "hci/acl_manager.h"
Martin Brabham94db40c2019-03-29 10:24:52 -070021#include "hci/address.h"
Jack Heb4b4a372019-08-09 15:10:44 -070022#include "l2cap/cid.h"
Jack Heff38d892019-10-03 17:11:07 -070023#include "l2cap/classic/fixed_channel.h"
24#include "l2cap/classic/fixed_channel_service.h"
Jack Heb4b4a372019-08-09 15:10:44 -070025#include "l2cap/security_policy.h"
26#include "os/handler.h"
27
28namespace bluetooth {
29namespace l2cap {
Jack Heff38d892019-10-03 17:11:07 -070030namespace classic {
Jack Heb4b4a372019-08-09 15:10:44 -070031
Jack Heff38d892019-10-03 17:11:07 -070032class L2capClassicModule;
Jack Heb5360fb2019-08-19 18:16:33 -070033
Jack He4f8230e2019-11-22 04:04:02 -080034namespace testing {
35class MockFixedChannelManager;
36}
37
Chris Manton7be4ea72019-07-29 18:26:27 -070038namespace internal {
Jack Heff38d892019-10-03 17:11:07 -070039class LinkManager;
40class FixedChannelServiceManagerImpl;
Jack Heb5360fb2019-08-19 18:16:33 -070041} // namespace internal
Chris Manton7be4ea72019-07-29 18:26:27 -070042
Jack Heff38d892019-10-03 17:11:07 -070043class FixedChannelManager {
Chris Manton7be4ea72019-07-29 18:26:27 -070044 public:
Jack Heb5360fb2019-08-19 18:16:33 -070045 enum class ConnectionResultCode {
46 SUCCESS = 0,
47 FAIL_NO_SERVICE_REGISTERED = 1, // No service is registered
48 FAIL_ALL_SERVICES_HAVE_CHANNEL = 2, // All registered services already have a channel
49 FAIL_HCI_ERROR = 3, // See hci_error
50 };
51
52 struct ConnectionResult {
53 ConnectionResultCode connection_result_code = ConnectionResultCode::SUCCESS;
54 hci::ErrorCode hci_error = hci::ErrorCode::SUCCESS;
55 };
Jack Heb4b4a372019-08-09 15:10:44 -070056 /**
57 * OnConnectionFailureCallback(std::string failure_reason);
58 */
Jack Heb5360fb2019-08-19 18:16:33 -070059 using OnConnectionFailureCallback = common::OnceCallback<void(ConnectionResult result)>;
Jack Heb4b4a372019-08-09 15:10:44 -070060
61 /**
Jack Heff38d892019-10-03 17:11:07 -070062 * OnConnectionOpenCallback(FixedChannel channel);
Jack Heb4b4a372019-08-09 15:10:44 -070063 */
Jack Heff38d892019-10-03 17:11:07 -070064 using OnConnectionOpenCallback = common::Callback<void(std::unique_ptr<FixedChannel>)>;
Jack Heb4b4a372019-08-09 15:10:44 -070065
Jack Heb5360fb2019-08-19 18:16:33 -070066 enum class RegistrationResult {
67 SUCCESS = 0,
68 FAIL_DUPLICATE_SERVICE = 1, // Duplicate service registration for the same CID
69 FAIL_INVALID_SERVICE = 2, // Invalid CID
70 };
Jack Heb4b4a372019-08-09 15:10:44 -070071
72 /**
Jack Heff38d892019-10-03 17:11:07 -070073 * OnRegistrationFailureCallback(RegistrationResult result, FixedChannelService service);
Jack Heb4b4a372019-08-09 15:10:44 -070074 */
Jack Heb5360fb2019-08-19 18:16:33 -070075 using OnRegistrationCompleteCallback =
Jack Heff38d892019-10-03 17:11:07 -070076 common::OnceCallback<void(RegistrationResult, std::unique_ptr<FixedChannelService>)>;
Jack Heb4b4a372019-08-09 15:10:44 -070077
78 /**
79 * Connect to ALL fixed channels on a remote device
80 *
81 * - This method is asynchronous
82 * - When false is returned, the connection fails immediately
83 * - When true is returned, method caller should wait for on_fail_callback or on_open_callback registered through
84 * RegisterService() API.
85 * - If an ACL connection does not exist, this method will create an ACL connection. As a result, on_open_callback
Jack Heff38d892019-10-03 17:11:07 -070086 * supplied through RegisterService() will be triggered to provide the actual FixedChannel objects
Jack Heb5360fb2019-08-19 18:16:33 -070087 * - If HCI connection failed, on_fail_callback will be triggered with FAIL_HCI_ERROR
Jack Heb4b4a372019-08-09 15:10:44 -070088 * - If fixed channel on a remote device is already reported as connected via on_open_callback and has been acquired
Jack Heff38d892019-10-03 17:11:07 -070089 * via FixedChannel#Acquire() API, it won't be reported again
Jack Heb5360fb2019-08-19 18:16:33 -070090 * - If no service is registered, on_fail_callback will be triggered with FAIL_NO_SERVICE_REGISTERED
91 * - If there is an ACL connection and channels for each service is allocated, on_fail_callback will be triggered with
92 * FAIL_ALL_SERVICES_HAVE_CHANNEL
Jack Heb4b4a372019-08-09 15:10:44 -070093 *
94 * NOTE:
95 * This call will initiate an effort to connect all fixed channel services on a remote device.
96 * Due to the connectionless nature of fixed channels, all fixed channels will be connected together.
97 * If a fixed channel service does not need a particular fixed channel. It should release the received
Jack Heff38d892019-10-03 17:11:07 -070098 * channel immediately after receiving on_open_callback via FixedChannel#Release()
Jack Heb4b4a372019-08-09 15:10:44 -070099 *
100 * A module calling ConnectServices() must have called RegisterService() before.
101 * The callback will come back from on_open_callback in the service that is registered
102 *
103 * @param device: Remote device to make this connection.
104 * @param on_fail_callback: A callback to indicate connection failure along with a status code.
105 * @param handler: The handler context in which to execute the @callback parameters.
106 *
107 * Returns: true if connection was able to be initiated, false otherwise.
108 */
Jack He4f8230e2019-11-22 04:04:02 -0800109 virtual bool ConnectServices(hci::Address device, OnConnectionFailureCallback on_fail_callback, os::Handler* handler);
Jack Heb4b4a372019-08-09 15:10:44 -0700110
111 /**
112 * Register a service to receive incoming connections bound to a specific channel.
113 *
114 * - This method is asynchronous.
115 * - When false is returned, the registration fails immediately.
116 * - When true is returned, method caller should wait for on_service_registered callback that contains a
Jack Heff38d892019-10-03 17:11:07 -0700117 * FixedChannelService object. The registered service can be managed from that object.
Jack Heb5360fb2019-08-19 18:16:33 -0700118 * - If a CID is already registered or some other error happens, on_registration_complete will be triggered with a
119 * non-SUCCESS value
Jack Heff38d892019-10-03 17:11:07 -0700120 * - After a service is registered, any classic ACL connection will create a FixedChannel object that is
Jack Heb4b4a372019-08-09 15:10:44 -0700121 * delivered through on_open_callback
Jack Heb5360fb2019-08-19 18:16:33 -0700122 * - on_open_callback, will only be triggered after on_service_registered callback
Jack Heb4b4a372019-08-09 15:10:44 -0700123 *
Jack Heff38d892019-10-03 17:11:07 -0700124 * @param cid: cid used to receive incoming connections
Jack Heb4b4a372019-08-09 15:10:44 -0700125 * @param security_policy: The security policy used for the connection.
126 * @param on_registration_complete: A callback to indicate the service setup has completed. If the return status is
127 * not SUCCESS, it means service is not registered due to reasons like CID already take
128 * @param on_open_callback: A callback to indicate success of a connection initiated from a remote device.
129 * @param handler: The handler context in which to execute the @callback parameter.
130 */
Jack He4f8230e2019-11-22 04:04:02 -0800131 virtual bool RegisterService(Cid cid, const SecurityPolicy& security_policy,
132 OnRegistrationCompleteCallback on_registration_complete,
133 OnConnectionOpenCallback on_connection_open, os::Handler* handler);
134
135 virtual ~FixedChannelManager() = default;
Chris Manton7be4ea72019-07-29 18:26:27 -0700136
Jack Heff38d892019-10-03 17:11:07 -0700137 friend class L2capClassicModule;
Jack He4f8230e2019-11-22 04:04:02 -0800138 friend class testing::MockFixedChannelManager;
Chris Manton7be4ea72019-07-29 18:26:27 -0700139
140 private:
Jack Heb5360fb2019-08-19 18:16:33 -0700141 // The constructor is not to be used by user code
Jack Heff38d892019-10-03 17:11:07 -0700142 FixedChannelManager(internal::FixedChannelServiceManagerImpl* service_manager, internal::LinkManager* link_manager,
143 os::Handler* l2cap_layer_handler)
Jack Heb5360fb2019-08-19 18:16:33 -0700144 : service_manager_(service_manager), link_manager_(link_manager), l2cap_layer_handler_(l2cap_layer_handler) {}
Martin Brabham80854c22019-11-12 14:52:42 -0800145
Jack Heff38d892019-10-03 17:11:07 -0700146 internal::FixedChannelServiceManagerImpl* service_manager_ = nullptr;
147 internal::LinkManager* link_manager_ = nullptr;
Chris Manton7be4ea72019-07-29 18:26:27 -0700148 os::Handler* l2cap_layer_handler_ = nullptr;
Jack Heff38d892019-10-03 17:11:07 -0700149 DISALLOW_COPY_AND_ASSIGN(FixedChannelManager);
Jack Heb4b4a372019-08-09 15:10:44 -0700150};
151
Jack Heff38d892019-10-03 17:11:07 -0700152} // namespace classic
Jack Heb4b4a372019-08-09 15:10:44 -0700153} // namespace l2cap
Martin Brabham94db40c2019-03-29 10:24:52 -0700154} // namespace bluetooth