Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 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 | |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 20 | #include "hci/acl_manager.h" |
Martin Brabham | 94db40c | 2019-03-29 10:24:52 -0700 | [diff] [blame] | 21 | #include "hci/address.h" |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 22 | #include "l2cap/cid.h" |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 23 | #include "l2cap/classic/fixed_channel.h" |
| 24 | #include "l2cap/classic/fixed_channel_service.h" |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 25 | #include "l2cap/security_policy.h" |
| 26 | #include "os/handler.h" |
| 27 | |
| 28 | namespace bluetooth { |
| 29 | namespace l2cap { |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 30 | namespace classic { |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 31 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 32 | class L2capClassicModule; |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 33 | |
Jack He | 4f8230e | 2019-11-22 04:04:02 -0800 | [diff] [blame] | 34 | namespace testing { |
| 35 | class MockFixedChannelManager; |
| 36 | } |
| 37 | |
Chris Manton | 7be4ea7 | 2019-07-29 18:26:27 -0700 | [diff] [blame] | 38 | namespace internal { |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 39 | class LinkManager; |
| 40 | class FixedChannelServiceManagerImpl; |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 41 | } // namespace internal |
Chris Manton | 7be4ea7 | 2019-07-29 18:26:27 -0700 | [diff] [blame] | 42 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 43 | class FixedChannelManager { |
Chris Manton | 7be4ea7 | 2019-07-29 18:26:27 -0700 | [diff] [blame] | 44 | public: |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 45 | 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 He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 56 | /** |
| 57 | * OnConnectionFailureCallback(std::string failure_reason); |
| 58 | */ |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 59 | using OnConnectionFailureCallback = common::OnceCallback<void(ConnectionResult result)>; |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 62 | * OnConnectionOpenCallback(FixedChannel channel); |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 63 | */ |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 64 | using OnConnectionOpenCallback = common::Callback<void(std::unique_ptr<FixedChannel>)>; |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 65 | |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 66 | 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 He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 73 | * OnRegistrationFailureCallback(RegistrationResult result, FixedChannelService service); |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 74 | */ |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 75 | using OnRegistrationCompleteCallback = |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 76 | common::OnceCallback<void(RegistrationResult, std::unique_ptr<FixedChannelService>)>; |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 77 | |
| 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 He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 86 | * supplied through RegisterService() will be triggered to provide the actual FixedChannel objects |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 87 | * - If HCI connection failed, on_fail_callback will be triggered with FAIL_HCI_ERROR |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 88 | * - If fixed channel on a remote device is already reported as connected via on_open_callback and has been acquired |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 89 | * via FixedChannel#Acquire() API, it won't be reported again |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 90 | * - 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 He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 93 | * |
| 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 He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 98 | * channel immediately after receiving on_open_callback via FixedChannel#Release() |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 99 | * |
| 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 He | 4f8230e | 2019-11-22 04:04:02 -0800 | [diff] [blame] | 109 | virtual bool ConnectServices(hci::Address device, OnConnectionFailureCallback on_fail_callback, os::Handler* handler); |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 110 | |
| 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 He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 117 | * FixedChannelService object. The registered service can be managed from that object. |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 118 | * - If a CID is already registered or some other error happens, on_registration_complete will be triggered with a |
| 119 | * non-SUCCESS value |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 120 | * - After a service is registered, any classic ACL connection will create a FixedChannel object that is |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 121 | * delivered through on_open_callback |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 122 | * - on_open_callback, will only be triggered after on_service_registered callback |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 123 | * |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 124 | * @param cid: cid used to receive incoming connections |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 125 | * @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 He | 4f8230e | 2019-11-22 04:04:02 -0800 | [diff] [blame] | 131 | 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 Manton | 7be4ea7 | 2019-07-29 18:26:27 -0700 | [diff] [blame] | 136 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 137 | friend class L2capClassicModule; |
Jack He | 4f8230e | 2019-11-22 04:04:02 -0800 | [diff] [blame] | 138 | friend class testing::MockFixedChannelManager; |
Chris Manton | 7be4ea7 | 2019-07-29 18:26:27 -0700 | [diff] [blame] | 139 | |
| 140 | private: |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 141 | // The constructor is not to be used by user code |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 142 | FixedChannelManager(internal::FixedChannelServiceManagerImpl* service_manager, internal::LinkManager* link_manager, |
| 143 | os::Handler* l2cap_layer_handler) |
Jack He | b5360fb | 2019-08-19 18:16:33 -0700 | [diff] [blame] | 144 | : service_manager_(service_manager), link_manager_(link_manager), l2cap_layer_handler_(l2cap_layer_handler) {} |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 145 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 146 | internal::FixedChannelServiceManagerImpl* service_manager_ = nullptr; |
| 147 | internal::LinkManager* link_manager_ = nullptr; |
Chris Manton | 7be4ea7 | 2019-07-29 18:26:27 -0700 | [diff] [blame] | 148 | os::Handler* l2cap_layer_handler_ = nullptr; |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 149 | DISALLOW_COPY_AND_ASSIGN(FixedChannelManager); |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
Jack He | ff38d89 | 2019-10-03 17:11:07 -0700 | [diff] [blame] | 152 | } // namespace classic |
Jack He | b4b4a37 | 2019-08-09 15:10:44 -0700 | [diff] [blame] | 153 | } // namespace l2cap |
Martin Brabham | 94db40c | 2019-03-29 10:24:52 -0700 | [diff] [blame] | 154 | } // namespace bluetooth |