Loading system/gd/common/testing/bind_test_util.h 0 → 100644 +29 −0 Original line number Diff line number Diff line /* * Copyright 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "base/test/bind_test_util.h" namespace bluetooth { namespace common { namespace testing { using base::BindLambdaForTesting; } // namespace testing } // namespace common } // namespace bluetooth system/gd/l2cap/Android.bp +2 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ filegroup { "classic_fixed_channel_service.cc", "internal/classic_fixed_channel_service_manager_impl.cc", "internal/classic_fixed_channel_allocator.cc", "internal/classic_link_manager.cc", ], } Loading @@ -17,6 +18,7 @@ filegroup { "l2cap_packet_test.cc", "internal/classic_fixed_channel_service_manager_test.cc", "internal/classic_fixed_channel_allocator_test.cc", "internal/classic_link_manager_test.cc", "signal_id_test.cc", ], } Loading system/gd/l2cap/classic_fixed_channel.h +13 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,10 @@ namespace bluetooth { namespace l2cap { namespace internal { class ClassicFixedChannelImpl; } // namespace internal /** * L2CAP fixed channel object. When a new object is created, it must be * acquired through calling {@link FixedChannel#Acquire()} within X seconds. Loading Loading @@ -65,6 +69,15 @@ class ClassicFixedChannel { * @return The upper end of a bi-directional queue. */ common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueUpEnd() const; friend class internal::ClassicFixedChannelImpl; private: ClassicFixedChannel(os::Handler* l2cap_handler, internal::ClassicFixedChannelImpl* classic_fixed_channel_impl) : l2cap_handler_(l2cap_handler), classic_fixed_channel_impl_(classic_fixed_channel_impl) {} os::Handler* l2cap_handler_; internal::ClassicFixedChannelImpl* classic_fixed_channel_impl_; DISALLOW_COPY_AND_ASSIGN(ClassicFixedChannel); }; } // namespace l2cap Loading system/gd/l2cap/classic_fixed_channel_manager.cc +15 −10 Original line number Diff line number Diff line Loading @@ -15,28 +15,33 @@ */ #include "l2cap/classic_fixed_channel_manager.h" #include "l2cap/internal/classic_fixed_channel_service.h" #include "l2cap/internal/classic_fixed_channel_service_impl.h" #include "l2cap/internal/classic_fixed_channel_service_manager_impl.h" #include "l2cap/internal/classic_link_manager.h" namespace bluetooth { namespace l2cap { bool ClassicFixedChannelManager::ConnectServices(common::Address device, OnConnectionFailureCallback on_connection_failure, bool ClassicFixedChannelManager::ConnectServices(common::Address device, OnConnectionFailureCallback on_fail_callback, os::Handler* handler) { return false; internal::ClassicLinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{ .on_fail_callback_ = std::move(on_fail_callback), .handler_ = handler}; l2cap_layer_handler_->Post(common::BindOnce(&internal::ClassicLinkManager::ConnectFixedChannelServices, common::Unretained(link_manager_), device, std::move(pending_fixed_channel_connection))); return true; } bool ClassicFixedChannelManager::RegisterService(Cid cid, const SecurityPolicy& security_policy, OnRegistrationCompleteCallback on_registration_complete, OnConnectionOpenCallback on_connection_open, os::Handler* handler) { internal::ClassicFixedChannelServiceImpl::Builder builder; builder.SetUserHandler(handler) .SetOnRegister(std::move(on_registration_complete)) .SetOnChannelOpen(std::move(on_connection_open)); internal::ClassicFixedChannelServiceImpl::PendingRegistration pending_registration{ .user_handler_ = handler, .on_registration_complete_callback_ = std::move(on_registration_complete), .on_connection_open_callback_ = std::move(on_connection_open)}; l2cap_layer_handler_->Post(common::BindOnce(&internal::ClassicFixedChannelServiceManagerImpl::Register, common::Unretained(manager_), cid, std::move(builder))); common::Unretained(service_manager_), cid, std::move(pending_registration))); return true; } Loading system/gd/l2cap/classic_fixed_channel_manager.h +40 −12 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <string> #include "common/address.h" #include "hci/acl_manager.h" #include "l2cap/cid.h" #include "l2cap/classic_fixed_channel.h" #include "l2cap/classic_fixed_channel_service.h" Loading @@ -27,28 +28,47 @@ namespace bluetooth { namespace l2cap { class L2capLayer; namespace internal { class ClassicLinkManager; class ClassicFixedChannelServiceManagerImpl; } } // namespace internal class ClassicFixedChannelManager { public: enum class ConnectionResultCode { SUCCESS = 0, FAIL_NO_SERVICE_REGISTERED = 1, // No service is registered FAIL_ALL_SERVICES_HAVE_CHANNEL = 2, // All registered services already have a channel FAIL_HCI_ERROR = 3, // See hci_error }; struct ConnectionResult { ConnectionResultCode connection_result_code = ConnectionResultCode::SUCCESS; hci::ErrorCode hci_error = hci::ErrorCode::SUCCESS; }; /** * OnConnectionFailureCallback(std::string failure_reason); */ using OnConnectionFailureCallback = common::Callback<void(std::string)>; using OnConnectionFailureCallback = common::OnceCallback<void(ConnectionResult result)>; /** * OnConnectionOpenCallback(ClassicFixedChannel channel); */ using OnConnectionOpenCallback = common::Callback<void(ClassicFixedChannel)>; using OnConnectionOpenCallback = common::Callback<void(std::unique_ptr<ClassicFixedChannel>)>; enum class RegistrationResult { SUCCESS, FAIL }; enum class RegistrationResult { SUCCESS = 0, FAIL_DUPLICATE_SERVICE = 1, // Duplicate service registration for the same CID FAIL_INVALID_SERVICE = 2, // Invalid CID }; /** * OnRegistrationFailureCallback(RegistrationResult result, ClassicFixedChannelService service); */ using OnRegistrationCompleteCallback = common::OnceCallback<void(RegistrationResult, ClassicFixedChannelService)>; using OnRegistrationCompleteCallback = common::OnceCallback<void(RegistrationResult, std::unique_ptr<ClassicFixedChannelService>)>; /** * Connect to ALL fixed channels on a remote device Loading @@ -59,9 +79,12 @@ class ClassicFixedChannelManager { * RegisterService() API. * - If an ACL connection does not exist, this method will create an ACL connection. As a result, on_open_callback * supplied through RegisterService() will be triggered to provide the actual ClassicFixedChannel objects * - If HCI connection failed, on_fail_callback will be triggered with FAIL_HCI_ERROR * - If fixed channel on a remote device is already reported as connected via on_open_callback and has been acquired * via ClassicFixedChannel#Acquire() API, it won't be reported again * - If no service is registered, this call is a no-op and on on_fail_callback will be triggered * - If no service is registered, on_fail_callback will be triggered with FAIL_NO_SERVICE_REGISTERED * - If there is an ACL connection and channels for each service is allocated, on_fail_callback will be triggered with * FAIL_ALL_SERVICES_HAVE_CHANNEL * * NOTE: * This call will initiate an effort to connect all fixed channel services on a remote device. Loading @@ -87,10 +110,11 @@ class ClassicFixedChannelManager { * - When false is returned, the registration fails immediately. * - When true is returned, method caller should wait for on_service_registered callback that contains a * ClassicFixedChannelService object. The registered service can be managed from that object. * - If a CID is already registered or some other error happens, * - If a CID is already registered or some other error happens, on_registration_complete will be triggered with a * non-SUCCESS value * - After a service is registered, any classic ACL connection will create a ClassicFixedChannel object that is * delivered through on_open_callback * - on_open_callback, if any, must be triggered after on_service_registered callback * - on_open_callback, will only be triggered after on_service_registered callback * * @param cid: Classic cid used to receive incoming connections * @param security_policy: The security policy used for the connection. Loading @@ -103,13 +127,17 @@ class ClassicFixedChannelManager { OnRegistrationCompleteCallback on_registration_complete, OnConnectionOpenCallback on_connection_open, os::Handler* handler); // The constructor is not to be used by user code ClassicFixedChannelManager(internal::ClassicFixedChannelServiceManagerImpl* manager, os::Handler* l2cap_layer_handler) : manager_(manager), l2cap_layer_handler_(l2cap_layer_handler) {} friend class L2capLayer; private: internal::ClassicFixedChannelServiceManagerImpl* manager_ = nullptr; // The constructor is not to be used by user code ClassicFixedChannelManager(internal::ClassicFixedChannelServiceManagerImpl* service_manager, internal::ClassicLinkManager* link_manager, os::Handler* l2cap_layer_handler) : service_manager_(service_manager), link_manager_(link_manager), l2cap_layer_handler_(l2cap_layer_handler) {} internal::ClassicFixedChannelServiceManagerImpl* service_manager_ = nullptr; internal::ClassicLinkManager* link_manager_ = nullptr; os::Handler* l2cap_layer_handler_ = nullptr; DISALLOW_COPY_AND_ASSIGN(ClassicFixedChannelManager); }; } // namespace l2cap Loading Loading
system/gd/common/testing/bind_test_util.h 0 → 100644 +29 −0 Original line number Diff line number Diff line /* * Copyright 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "base/test/bind_test_util.h" namespace bluetooth { namespace common { namespace testing { using base::BindLambdaForTesting; } // namespace testing } // namespace common } // namespace bluetooth
system/gd/l2cap/Android.bp +2 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ filegroup { "classic_fixed_channel_service.cc", "internal/classic_fixed_channel_service_manager_impl.cc", "internal/classic_fixed_channel_allocator.cc", "internal/classic_link_manager.cc", ], } Loading @@ -17,6 +18,7 @@ filegroup { "l2cap_packet_test.cc", "internal/classic_fixed_channel_service_manager_test.cc", "internal/classic_fixed_channel_allocator_test.cc", "internal/classic_link_manager_test.cc", "signal_id_test.cc", ], } Loading
system/gd/l2cap/classic_fixed_channel.h +13 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,10 @@ namespace bluetooth { namespace l2cap { namespace internal { class ClassicFixedChannelImpl; } // namespace internal /** * L2CAP fixed channel object. When a new object is created, it must be * acquired through calling {@link FixedChannel#Acquire()} within X seconds. Loading Loading @@ -65,6 +69,15 @@ class ClassicFixedChannel { * @return The upper end of a bi-directional queue. */ common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>* GetQueueUpEnd() const; friend class internal::ClassicFixedChannelImpl; private: ClassicFixedChannel(os::Handler* l2cap_handler, internal::ClassicFixedChannelImpl* classic_fixed_channel_impl) : l2cap_handler_(l2cap_handler), classic_fixed_channel_impl_(classic_fixed_channel_impl) {} os::Handler* l2cap_handler_; internal::ClassicFixedChannelImpl* classic_fixed_channel_impl_; DISALLOW_COPY_AND_ASSIGN(ClassicFixedChannel); }; } // namespace l2cap Loading
system/gd/l2cap/classic_fixed_channel_manager.cc +15 −10 Original line number Diff line number Diff line Loading @@ -15,28 +15,33 @@ */ #include "l2cap/classic_fixed_channel_manager.h" #include "l2cap/internal/classic_fixed_channel_service.h" #include "l2cap/internal/classic_fixed_channel_service_impl.h" #include "l2cap/internal/classic_fixed_channel_service_manager_impl.h" #include "l2cap/internal/classic_link_manager.h" namespace bluetooth { namespace l2cap { bool ClassicFixedChannelManager::ConnectServices(common::Address device, OnConnectionFailureCallback on_connection_failure, bool ClassicFixedChannelManager::ConnectServices(common::Address device, OnConnectionFailureCallback on_fail_callback, os::Handler* handler) { return false; internal::ClassicLinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{ .on_fail_callback_ = std::move(on_fail_callback), .handler_ = handler}; l2cap_layer_handler_->Post(common::BindOnce(&internal::ClassicLinkManager::ConnectFixedChannelServices, common::Unretained(link_manager_), device, std::move(pending_fixed_channel_connection))); return true; } bool ClassicFixedChannelManager::RegisterService(Cid cid, const SecurityPolicy& security_policy, OnRegistrationCompleteCallback on_registration_complete, OnConnectionOpenCallback on_connection_open, os::Handler* handler) { internal::ClassicFixedChannelServiceImpl::Builder builder; builder.SetUserHandler(handler) .SetOnRegister(std::move(on_registration_complete)) .SetOnChannelOpen(std::move(on_connection_open)); internal::ClassicFixedChannelServiceImpl::PendingRegistration pending_registration{ .user_handler_ = handler, .on_registration_complete_callback_ = std::move(on_registration_complete), .on_connection_open_callback_ = std::move(on_connection_open)}; l2cap_layer_handler_->Post(common::BindOnce(&internal::ClassicFixedChannelServiceManagerImpl::Register, common::Unretained(manager_), cid, std::move(builder))); common::Unretained(service_manager_), cid, std::move(pending_registration))); return true; } Loading
system/gd/l2cap/classic_fixed_channel_manager.h +40 −12 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <string> #include "common/address.h" #include "hci/acl_manager.h" #include "l2cap/cid.h" #include "l2cap/classic_fixed_channel.h" #include "l2cap/classic_fixed_channel_service.h" Loading @@ -27,28 +28,47 @@ namespace bluetooth { namespace l2cap { class L2capLayer; namespace internal { class ClassicLinkManager; class ClassicFixedChannelServiceManagerImpl; } } // namespace internal class ClassicFixedChannelManager { public: enum class ConnectionResultCode { SUCCESS = 0, FAIL_NO_SERVICE_REGISTERED = 1, // No service is registered FAIL_ALL_SERVICES_HAVE_CHANNEL = 2, // All registered services already have a channel FAIL_HCI_ERROR = 3, // See hci_error }; struct ConnectionResult { ConnectionResultCode connection_result_code = ConnectionResultCode::SUCCESS; hci::ErrorCode hci_error = hci::ErrorCode::SUCCESS; }; /** * OnConnectionFailureCallback(std::string failure_reason); */ using OnConnectionFailureCallback = common::Callback<void(std::string)>; using OnConnectionFailureCallback = common::OnceCallback<void(ConnectionResult result)>; /** * OnConnectionOpenCallback(ClassicFixedChannel channel); */ using OnConnectionOpenCallback = common::Callback<void(ClassicFixedChannel)>; using OnConnectionOpenCallback = common::Callback<void(std::unique_ptr<ClassicFixedChannel>)>; enum class RegistrationResult { SUCCESS, FAIL }; enum class RegistrationResult { SUCCESS = 0, FAIL_DUPLICATE_SERVICE = 1, // Duplicate service registration for the same CID FAIL_INVALID_SERVICE = 2, // Invalid CID }; /** * OnRegistrationFailureCallback(RegistrationResult result, ClassicFixedChannelService service); */ using OnRegistrationCompleteCallback = common::OnceCallback<void(RegistrationResult, ClassicFixedChannelService)>; using OnRegistrationCompleteCallback = common::OnceCallback<void(RegistrationResult, std::unique_ptr<ClassicFixedChannelService>)>; /** * Connect to ALL fixed channels on a remote device Loading @@ -59,9 +79,12 @@ class ClassicFixedChannelManager { * RegisterService() API. * - If an ACL connection does not exist, this method will create an ACL connection. As a result, on_open_callback * supplied through RegisterService() will be triggered to provide the actual ClassicFixedChannel objects * - If HCI connection failed, on_fail_callback will be triggered with FAIL_HCI_ERROR * - If fixed channel on a remote device is already reported as connected via on_open_callback and has been acquired * via ClassicFixedChannel#Acquire() API, it won't be reported again * - If no service is registered, this call is a no-op and on on_fail_callback will be triggered * - If no service is registered, on_fail_callback will be triggered with FAIL_NO_SERVICE_REGISTERED * - If there is an ACL connection and channels for each service is allocated, on_fail_callback will be triggered with * FAIL_ALL_SERVICES_HAVE_CHANNEL * * NOTE: * This call will initiate an effort to connect all fixed channel services on a remote device. Loading @@ -87,10 +110,11 @@ class ClassicFixedChannelManager { * - When false is returned, the registration fails immediately. * - When true is returned, method caller should wait for on_service_registered callback that contains a * ClassicFixedChannelService object. The registered service can be managed from that object. * - If a CID is already registered or some other error happens, * - If a CID is already registered or some other error happens, on_registration_complete will be triggered with a * non-SUCCESS value * - After a service is registered, any classic ACL connection will create a ClassicFixedChannel object that is * delivered through on_open_callback * - on_open_callback, if any, must be triggered after on_service_registered callback * - on_open_callback, will only be triggered after on_service_registered callback * * @param cid: Classic cid used to receive incoming connections * @param security_policy: The security policy used for the connection. Loading @@ -103,13 +127,17 @@ class ClassicFixedChannelManager { OnRegistrationCompleteCallback on_registration_complete, OnConnectionOpenCallback on_connection_open, os::Handler* handler); // The constructor is not to be used by user code ClassicFixedChannelManager(internal::ClassicFixedChannelServiceManagerImpl* manager, os::Handler* l2cap_layer_handler) : manager_(manager), l2cap_layer_handler_(l2cap_layer_handler) {} friend class L2capLayer; private: internal::ClassicFixedChannelServiceManagerImpl* manager_ = nullptr; // The constructor is not to be used by user code ClassicFixedChannelManager(internal::ClassicFixedChannelServiceManagerImpl* service_manager, internal::ClassicLinkManager* link_manager, os::Handler* l2cap_layer_handler) : service_manager_(service_manager), link_manager_(link_manager), l2cap_layer_handler_(l2cap_layer_handler) {} internal::ClassicFixedChannelServiceManagerImpl* service_manager_ = nullptr; internal::ClassicLinkManager* link_manager_ = nullptr; os::Handler* l2cap_layer_handler_ = nullptr; DISALLOW_COPY_AND_ASSIGN(ClassicFixedChannelManager); }; } // namespace l2cap Loading