Loading system/gd/Android.bp +12 −5 Original line number Diff line number Diff line Loading @@ -174,7 +174,6 @@ cc_defaults { "libbluetooth_smp_pdl", ], static_libs: [ "bluetooth_flags_c_lib", "libaconfig_storage_read_api_cc", "libbase", "libbluetooth-dumpsys", Loading Loading @@ -210,7 +209,10 @@ cc_library { "com.android.btservices", ], min_sdk_version: "31", static_libs: ["libchrome"], static_libs: [ "bluetooth_flags_c_lib", "libchrome", ], } cc_library_static { Loading @@ -229,7 +231,10 @@ cc_library_static { "com.android.btservices", ], min_sdk_version: "31", static_libs: ["libchrome"], static_libs: [ "bluetooth_flags_c_lib", "libchrome", ], } cc_library { Loading @@ -249,6 +254,7 @@ cc_library { "-Wno-unused-parameter", ], static_libs: [ "bluetooth_flags_c_lib", "libbluetooth-types", "libbt-common", "libchrome", Loading @@ -271,6 +277,7 @@ cc_library { "-DUSE_FAKE_TIMERS", ], static_libs: [ "bluetooth_flags_c_lib_for_test", "libbluetooth-types", "libbt-common", "libchrome", Loading Loading @@ -487,7 +494,7 @@ cc_test { "BluetoothGeneratedDumpsysTestData_h", ], static_libs: [ "bluetooth_flags_c_lib", "bluetooth_flags_c_lib_for_test", "libbase", "libbluetooth-protos", "libbluetooth-types", Loading @@ -509,12 +516,12 @@ cc_test { "libflatbuffers-cpp", "libgmock", "libosi", "server_configurable_flags", ], shared_libs: [ "libPlatformProperties", "libaconfig_storage_read_api_cc", "libcrypto", "server_configurable_flags", ], sanitize: { address: true, Loading system/gd/hci/acl_manager/le_impl.h +12 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <base/strings/stringprintf.h> #include <bluetooth/log.h> #include <com_android_bluetooth_flags.h> #include <cstdint> #include <memory> Loading Loading @@ -953,6 +954,17 @@ public: add_device_to_accept_list(address_with_type); } if (com::android::bluetooth::flags:: improve_create_connection_for_already_connecting_device()) { bool in_accept_list_due_to_direct_connect = direct_connections_.find(address_with_type) != direct_connections_.end(); if (already_in_accept_list && (in_accept_list_due_to_direct_connect || !is_direct)) { log::info("Device {} already in accept list. Stop here.", address_with_type); return; } } if (is_direct) { direct_connect_add(address_with_type); } Loading system/gd/hci/acl_manager/le_impl_test.cc +180 −140 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ #include "hci/acl_manager/le_impl.h" #include <bluetooth/log.h> #include <com_android_bluetooth_flags.h> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <log/log.h> Loading Loading @@ -278,7 +279,168 @@ protected: hci_layer_->IncomingEvent(LeSetRandomAddressCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); } void test_direct_connection_after_background_connection() { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // arrange: Create background connection. Remember that acl_manager adds device background list le_impl_->add_device_to_background_connection_list(address); le_impl_->create_le_connection(address, true, /* is_direct */ false); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // act: Create direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); auto cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); } auto raw_direct_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); // assert auto bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); auto direct_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_create_connection))); EXPECT_TRUE(direct_create_connection.IsValid()); log::info("Scan Interval {}", direct_create_connection.GetLeScanInterval()); ASSERT_NE(direct_create_connection.GetLeScanInterval(), bg_create_connection.GetLeScanInterval()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); // Simulate timeout on direct connect. Verify background connect is still in place EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); EXPECT_TRUE(cancel_connection.IsValid()); raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); } void test_direct_connect_after_direct_connect() { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // Create first direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_direct_1_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); // assert auto direct_1_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_1_create_connection))); EXPECT_TRUE(direct_1_create_connection.IsValid()); log::info("Second direct connect to the same device"); // Create second direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); sync_handler(); CommandView cancel_connection = CommandView::Create( PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>())); ; if (!com::android::bluetooth::flags:: improve_create_connection_for_already_connecting_device()) { cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); } auto raw_direct_2_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); auto direct_2_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_2_create_connection))); EXPECT_TRUE(direct_2_create_connection.IsValid()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); } else { hci_layer_->AssertNoQueuedCommand(); } log::info("Simulate timeout"); EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); EXPECT_TRUE(cancel_connection.IsValid()); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->GetCommand(OpCode::LE_REMOVE_DEVICE_FROM_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeRemoveDeviceFromFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->AssertNoQueuedCommand(); ASSERT_EQ(ConnectabilityState::DISARMED, le_impl_->connectability_state_); } void TearDown() override { com::android::bluetooth::flags::provider_->reset_flags(); // We cannot teardown our structure without unregistering // from our own structure we created. if (le_impl_->address_manager_registered) { Loading Loading @@ -1419,151 +1581,29 @@ TEST_F(LeImplTest, DisconnectionAcceptlistCallback) { } TEST_F(LeImplTest, direct_connection_after_background_connection) { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // arrange: Create background connection. Remember that acl_manager adds device background list le_impl_->add_device_to_background_connection_list(address); le_impl_->create_le_connection(address, true, /* is_direct */ false); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // act: Create direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); auto cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); // TODO b/356593752 - remove when test removing flag com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(false); test_direct_connection_after_background_connection(); } auto raw_direct_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); // assert auto bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); auto direct_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_create_connection))); EXPECT_TRUE(direct_create_connection.IsValid()); log::info("Scan Interval {}", direct_create_connection.GetLeScanInterval()); ASSERT_NE(direct_create_connection.GetLeScanInterval(), bg_create_connection.GetLeScanInterval()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); // Simulate timeout on direct connect. Verify background connect is still in place EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); EXPECT_TRUE(cancel_connection.IsValid()); raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); TEST_F(LeImplTest, direct_connection_after_background_connection_with_improvement) { com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(true); test_direct_connection_after_background_connection(); } TEST_F(LeImplTest, direct_connection_after_direct_connection) { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // Create first direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_direct_1_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); log::info("Second direct connect to the same device"); // Create second direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); sync_handler(); auto cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); // TODO b/356593752 - remove when test removing flag com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(false); test_direct_connect_after_direct_connect(); } auto raw_direct_2_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); // assert auto direct_1_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_1_create_connection))); EXPECT_TRUE(direct_1_create_connection.IsValid()); auto direct_2_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_2_create_connection))); EXPECT_TRUE(direct_2_create_connection.IsValid()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); log::info("Simulate timeout"); EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); EXPECT_TRUE(cancel_connection.IsValid()); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->GetCommand(OpCode::LE_REMOVE_DEVICE_FROM_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeRemoveDeviceFromFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->AssertNoQueuedCommand(); ASSERT_EQ(ConnectabilityState::DISARMED, le_impl_->connectability_state_); TEST_F(LeImplTest, direct_connection_after_direct_connection_with_improvement) { com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(true); test_direct_connect_after_direct_connect(); } } // namespace acl_manager Loading system/gd/hci/le_advertising_manager_test.cc +10 −6 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <flag_macros.h> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <log/log.h> #include <algorithm> #include <chrono> Loading Loading @@ -165,6 +166,7 @@ protected: class LeAdvertisingManagerTest : public ::testing::Test { protected: void SetUp() override { __android_log_set_minimum_priority(ANDROID_LOG_VERBOSE); test_hci_layer_ = new HciLayerFake; // Ownership is transferred to registry test_controller_ = new TestController; test_acl_manager_ = new TestAclManager; Loading @@ -182,6 +184,8 @@ protected: } void TearDown() override { TEST_BT::provider_->reset_flags(); sync_client_handler(); fake_registry_.SynchronizeModuleHandler(&LeAdvertisingManager::Factory, std::chrono::milliseconds(20)); Loading Loading @@ -1833,9 +1837,9 @@ TEST_F(LeExtendedAdvertisingManagerTest, use_public_address_type_if_public_addre EXPECT_EQ(set_parameters_command.GetOwnAddressType(), OwnAddressType::PUBLIC_DEVICE_ADDRESS); } TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest, use_nrpa_if_public_address_policy_non_connectable, REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, nrpa_non_connectable_adv))) { TEST_F(LeExtendedAdvertisingManagerTest, use_nrpa_if_public_address_policy_non_connectable) { TEST_BT::provider_->nrpa_non_connectable_adv(true); // arrange: use PUBLIC address policy test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS); Loading Loading @@ -1867,9 +1871,9 @@ TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest, EXPECT_EQ(address.data()[5] >> 6, 0b00); } TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest, use_public_if_requested_with_public_address_policy_non_connectable, REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, nrpa_non_connectable_adv))) { TEST_F(LeExtendedAdvertisingManagerTest, use_public_if_requested_with_public_address_policy_non_connectable) { TEST_BT::provider_->nrpa_non_connectable_adv(true); // arrange: use PUBLIC address policy test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS); Loading Loading
system/gd/Android.bp +12 −5 Original line number Diff line number Diff line Loading @@ -174,7 +174,6 @@ cc_defaults { "libbluetooth_smp_pdl", ], static_libs: [ "bluetooth_flags_c_lib", "libaconfig_storage_read_api_cc", "libbase", "libbluetooth-dumpsys", Loading Loading @@ -210,7 +209,10 @@ cc_library { "com.android.btservices", ], min_sdk_version: "31", static_libs: ["libchrome"], static_libs: [ "bluetooth_flags_c_lib", "libchrome", ], } cc_library_static { Loading @@ -229,7 +231,10 @@ cc_library_static { "com.android.btservices", ], min_sdk_version: "31", static_libs: ["libchrome"], static_libs: [ "bluetooth_flags_c_lib", "libchrome", ], } cc_library { Loading @@ -249,6 +254,7 @@ cc_library { "-Wno-unused-parameter", ], static_libs: [ "bluetooth_flags_c_lib", "libbluetooth-types", "libbt-common", "libchrome", Loading @@ -271,6 +277,7 @@ cc_library { "-DUSE_FAKE_TIMERS", ], static_libs: [ "bluetooth_flags_c_lib_for_test", "libbluetooth-types", "libbt-common", "libchrome", Loading Loading @@ -487,7 +494,7 @@ cc_test { "BluetoothGeneratedDumpsysTestData_h", ], static_libs: [ "bluetooth_flags_c_lib", "bluetooth_flags_c_lib_for_test", "libbase", "libbluetooth-protos", "libbluetooth-types", Loading @@ -509,12 +516,12 @@ cc_test { "libflatbuffers-cpp", "libgmock", "libosi", "server_configurable_flags", ], shared_libs: [ "libPlatformProperties", "libaconfig_storage_read_api_cc", "libcrypto", "server_configurable_flags", ], sanitize: { address: true, Loading
system/gd/hci/acl_manager/le_impl.h +12 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <base/strings/stringprintf.h> #include <bluetooth/log.h> #include <com_android_bluetooth_flags.h> #include <cstdint> #include <memory> Loading Loading @@ -953,6 +954,17 @@ public: add_device_to_accept_list(address_with_type); } if (com::android::bluetooth::flags:: improve_create_connection_for_already_connecting_device()) { bool in_accept_list_due_to_direct_connect = direct_connections_.find(address_with_type) != direct_connections_.end(); if (already_in_accept_list && (in_accept_list_due_to_direct_connect || !is_direct)) { log::info("Device {} already in accept list. Stop here.", address_with_type); return; } } if (is_direct) { direct_connect_add(address_with_type); } Loading
system/gd/hci/acl_manager/le_impl_test.cc +180 −140 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ #include "hci/acl_manager/le_impl.h" #include <bluetooth/log.h> #include <com_android_bluetooth_flags.h> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <log/log.h> Loading Loading @@ -278,7 +279,168 @@ protected: hci_layer_->IncomingEvent(LeSetRandomAddressCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); } void test_direct_connection_after_background_connection() { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // arrange: Create background connection. Remember that acl_manager adds device background list le_impl_->add_device_to_background_connection_list(address); le_impl_->create_le_connection(address, true, /* is_direct */ false); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // act: Create direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); auto cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); } auto raw_direct_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); // assert auto bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); auto direct_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_create_connection))); EXPECT_TRUE(direct_create_connection.IsValid()); log::info("Scan Interval {}", direct_create_connection.GetLeScanInterval()); ASSERT_NE(direct_create_connection.GetLeScanInterval(), bg_create_connection.GetLeScanInterval()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); // Simulate timeout on direct connect. Verify background connect is still in place EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); EXPECT_TRUE(cancel_connection.IsValid()); raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); } void test_direct_connect_after_direct_connect() { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // Create first direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_direct_1_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); // assert auto direct_1_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_1_create_connection))); EXPECT_TRUE(direct_1_create_connection.IsValid()); log::info("Second direct connect to the same device"); // Create second direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); sync_handler(); CommandView cancel_connection = CommandView::Create( PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>())); ; if (!com::android::bluetooth::flags:: improve_create_connection_for_already_connecting_device()) { cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); } auto raw_direct_2_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); auto direct_2_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_2_create_connection))); EXPECT_TRUE(direct_2_create_connection.IsValid()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); } else { hci_layer_->AssertNoQueuedCommand(); } log::info("Simulate timeout"); EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); EXPECT_TRUE(cancel_connection.IsValid()); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->GetCommand(OpCode::LE_REMOVE_DEVICE_FROM_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeRemoveDeviceFromFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->AssertNoQueuedCommand(); ASSERT_EQ(ConnectabilityState::DISARMED, le_impl_->connectability_state_); } void TearDown() override { com::android::bluetooth::flags::provider_->reset_flags(); // We cannot teardown our structure without unregistering // from our own structure we created. if (le_impl_->address_manager_registered) { Loading Loading @@ -1419,151 +1581,29 @@ TEST_F(LeImplTest, DisconnectionAcceptlistCallback) { } TEST_F(LeImplTest, direct_connection_after_background_connection) { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // arrange: Create background connection. Remember that acl_manager adds device background list le_impl_->add_device_to_background_connection_list(address); le_impl_->create_le_connection(address, true, /* is_direct */ false); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // act: Create direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); auto cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); // TODO b/356593752 - remove when test removing flag com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(false); test_direct_connection_after_background_connection(); } auto raw_direct_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); // assert auto bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); auto direct_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_create_connection))); EXPECT_TRUE(direct_create_connection.IsValid()); log::info("Scan Interval {}", direct_create_connection.GetLeScanInterval()); ASSERT_NE(direct_create_connection.GetLeScanInterval(), bg_create_connection.GetLeScanInterval()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); // Simulate timeout on direct connect. Verify background connect is still in place EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); EXPECT_TRUE(cancel_connection.IsValid()); raw_bg_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); bg_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_bg_create_connection))); EXPECT_TRUE(bg_create_connection.IsValid()); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); TEST_F(LeImplTest, direct_connection_after_background_connection_with_improvement) { com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(true); test_direct_connection_after_background_connection(); } TEST_F(LeImplTest, direct_connection_after_direct_connection) { set_random_device_address_policy(); hci::AddressWithType address({0x21, 0x22, 0x23, 0x24, 0x25, 0x26}, AddressType::PUBLIC_DEVICE_ADDRESS); // Create first direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); hci_layer_->GetCommand(OpCode::LE_ADD_DEVICE_TO_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeAddDeviceToFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); auto raw_direct_1_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); // Check state is ARMED ASSERT_EQ(ConnectabilityState::ARMED, le_impl_->connectability_state_); log::info("Second direct connect to the same device"); // Create second direct connection le_impl_->create_le_connection(address, true, /* is_direct */ true); sync_handler(); auto cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); if (cancel_connection.IsValid()) { hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); // TODO b/356593752 - remove when test removing flag com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(false); test_direct_connect_after_direct_connect(); } auto raw_direct_2_create_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION); // assert auto direct_1_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_1_create_connection))); EXPECT_TRUE(direct_1_create_connection.IsValid()); auto direct_2_create_connection = LeCreateConnectionView::Create(LeConnectionManagementCommandView::Create( AclCommandView::Create(raw_direct_2_create_connection))); EXPECT_TRUE(direct_2_create_connection.IsValid()); hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); sync_handler(); log::info("Simulate timeout"); EXPECT_CALL(mock_le_connection_callbacks_, OnLeConnectFail(_, ErrorCode::CONNECTION_ACCEPT_TIMEOUT)) .Times(1); le_impl_->on_create_connection_timeout(address); sync_handler(); cancel_connection = hci_layer_->GetCommand(OpCode::LE_CREATE_CONNECTION_CANCEL); EXPECT_TRUE(cancel_connection.IsValid()); hci_layer_->IncomingEvent( LeCreateConnectionCancelCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( ErrorCode::UNKNOWN_CONNECTION, kHciHandle, Role::CENTRAL, AddressType::PUBLIC_DEVICE_ADDRESS, Address::kEmpty, 0x0000, 0x0000, 0x0000, ClockAccuracy::PPM_30)); sync_handler(); ASSERT_TRUE(le_impl_->create_connection_timeout_alarms_.empty()); hci_layer_->GetCommand(OpCode::LE_REMOVE_DEVICE_FROM_FILTER_ACCEPT_LIST); hci_layer_->IncomingEvent( LeRemoveDeviceFromFilterAcceptListCompleteBuilder::Create(0x01, ErrorCode::SUCCESS)); hci_layer_->AssertNoQueuedCommand(); ASSERT_EQ(ConnectabilityState::DISARMED, le_impl_->connectability_state_); TEST_F(LeImplTest, direct_connection_after_direct_connection_with_improvement) { com::android::bluetooth::flags::provider_ ->improve_create_connection_for_already_connecting_device(true); test_direct_connect_after_direct_connect(); } } // namespace acl_manager Loading
system/gd/hci/le_advertising_manager_test.cc +10 −6 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <flag_macros.h> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <log/log.h> #include <algorithm> #include <chrono> Loading Loading @@ -165,6 +166,7 @@ protected: class LeAdvertisingManagerTest : public ::testing::Test { protected: void SetUp() override { __android_log_set_minimum_priority(ANDROID_LOG_VERBOSE); test_hci_layer_ = new HciLayerFake; // Ownership is transferred to registry test_controller_ = new TestController; test_acl_manager_ = new TestAclManager; Loading @@ -182,6 +184,8 @@ protected: } void TearDown() override { TEST_BT::provider_->reset_flags(); sync_client_handler(); fake_registry_.SynchronizeModuleHandler(&LeAdvertisingManager::Factory, std::chrono::milliseconds(20)); Loading Loading @@ -1833,9 +1837,9 @@ TEST_F(LeExtendedAdvertisingManagerTest, use_public_address_type_if_public_addre EXPECT_EQ(set_parameters_command.GetOwnAddressType(), OwnAddressType::PUBLIC_DEVICE_ADDRESS); } TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest, use_nrpa_if_public_address_policy_non_connectable, REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, nrpa_non_connectable_adv))) { TEST_F(LeExtendedAdvertisingManagerTest, use_nrpa_if_public_address_policy_non_connectable) { TEST_BT::provider_->nrpa_non_connectable_adv(true); // arrange: use PUBLIC address policy test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS); Loading Loading @@ -1867,9 +1871,9 @@ TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest, EXPECT_EQ(address.data()[5] >> 6, 0b00); } TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest, use_public_if_requested_with_public_address_policy_non_connectable, REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, nrpa_non_connectable_adv))) { TEST_F(LeExtendedAdvertisingManagerTest, use_public_if_requested_with_public_address_policy_non_connectable) { TEST_BT::provider_->nrpa_non_connectable_adv(true); // arrange: use PUBLIC address policy test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS); Loading