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