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