diff --git a/connections/implementation/mediums/ble_v2.cc b/connections/implementation/mediums/ble_v2.cc index a5a8a137..c17d2c99 100644 --- a/connections/implementation/mediums/ble_v2.cc +++ b/connections/implementation/mediums/ble_v2.cc @@ -254,12 +254,9 @@ bool BleV2::StartLegacyAdvertising( << "Can't turn on BLE v2 legacy advertising. BLE is not available."; return false; } - // Checking to avoid conflicts to the bool StartAdvertsing invokes. - // TODO(hais) remove this if check after deprecating bool StartAdvertising. - if (IsAdvertisingLocked(input_service_id)) { - NEARBY_LOGS(INFO) << "Failed to BLE v2 legacy device advertise as ble is " - "already advertising."; - return false; + if (medium_.IsExtendedAdvertisementsAvailable()) { + NEARBY_LOGS(INFO) << "Skip dummy advertising for non legacy device"; + return true; } std::string service_id = input_service_id + "-Legacy"; if (service_ids_to_advertising_sessions_.find(service_id) != diff --git a/connections/implementation/mediums/ble_v2_test.cc b/connections/implementation/mediums/ble_v2_test.cc index eac8a811..618a1e74 100644 --- a/connections/implementation/mediums/ble_v2_test.cc +++ b/connections/implementation/mediums/ble_v2_test.cc @@ -626,7 +626,7 @@ TEST_F(BleV2Test, CanNotStopLegacyAdvertisingForNonExistingServiceId) { env_.Stop(); } -TEST_F(BleV2Test, StartLegacyAdvertisingBlockedByRegularAdvertising) { +TEST_F(BleV2Test, StartLegacyAdvertisingNotBlockedByRegularAdvertising) { env_.Start(); BluetoothRadio radio_a; BleV2 ble_a{radio_a}; @@ -638,10 +638,10 @@ TEST_F(BleV2Test, StartLegacyAdvertisingBlockedByRegularAdvertising) { PowerLevel::kHighPower, /*is_fast_advertisement=*/false); EXPECT_TRUE(ble_a.IsAdvertising(service_id)); - EXPECT_FALSE( + EXPECT_TRUE( ble_a.StartLegacyAdvertising(service_id, std::string(kLocalEndpointId), std::string(kFastAdvertisementServiceUuid))); - EXPECT_FALSE(ble_a.IsAdvertisingForLegacyDevice(service_id)); + EXPECT_TRUE(ble_a.IsAdvertisingForLegacyDevice(service_id)); ble_a.StopAdvertising(std::string(kServiceIDA)); env_.Stop(); } diff --git a/connections/implementation/p2p_cluster_pcp_handler.cc b/connections/implementation/p2p_cluster_pcp_handler.cc index 0490b9af..13f26157 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.cc +++ b/connections/implementation/p2p_cluster_pcp_handler.cc @@ -168,6 +168,28 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( bluetooth_medium_.TurnOffDiscoverability(); bluetooth_medium_.StopAcceptingConnections(service_id); } + } else if ((api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kChromeOS || + api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kLinux) && + NearbyFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature:: + kEnableBleV2)) { + if (ble_v2_medium_.StartLegacyAdvertising( + service_id, local_endpoint_id, + advertising_options.fast_advertisement_service_uuid)) { + NEARBY_LOGS(INFO) + << __func__ << "Ble v2 started advertising for legacy device."; + mediums_started_successfully.push_back(bluetooth_medium); + NEARBY_LOGS(INFO) << __func__ << "After Ble v2, BT added"; + bluetooth_classic_advertiser_client_id_ = client->GetClientId(); + } else { + NEARBY_LOG(WARNING, + "P2pClusterPcpHandler::StartAdvertisingImpl: BLE legacy " + "failed, revert BTC"); + bluetooth_medium_.TurnOffDiscoverability(); + bluetooth_medium_.StopAcceptingConnections(service_id); + } } else { NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: BT added"); @@ -228,6 +250,14 @@ Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) { !NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature::kEnableBleV2)) { ble_medium_.StopLegacyAdvertising(client->GetAdvertisingServiceId()); + } else if ((api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kChromeOS || + api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kLinux) && + NearbyFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature:: + kEnableBleV2)) { + ble_v2_medium_.StopLegacyAdvertising(client->GetAdvertisingServiceId()); } bluetooth_classic_advertiser_client_id_ = 0; } else { @@ -446,7 +476,6 @@ void P2pClusterPcpHandler::BluetoothDeviceLostHandler( bool P2pClusterPcpHandler::IsRecognizedBleEndpoint( const std::string& service_id, const BleAdvertisement& advertisement) const { - if (advertisement.GetPcp() != GetPcp()) { NEARBY_LOGS(INFO) << "BleAdvertisement doesn't match on Pcp; expected " << PcpToStrategy(GetPcp()).GetName() << ", found " @@ -681,8 +710,6 @@ void P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler( if (!IsRecognizedBleV2Endpoint(service_id, advertisement)) return; // Report the discovered endpoint to the client. - // BleV2EndpointState ble_endpoint_state(/*ble=*/true, /*l2cap=*/false, - // /*bt=alse*/false); BleV2EndpointState ble_endpoint_state; ByteArray peripheral_id = peripheral.GetId(); found_endpoints_in_ble_discover_cb_.insert( @@ -1277,6 +1304,15 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( // TODO(hais): update this after ble_v2 refactor. if (api::ImplementationPlatform::GetCurrentOS() == api::OSName::kChromeOS) { mediums_->GetBle().StopLegacyAdvertising(std::string(service_id)); + } else if ((api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kChromeOS || + api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kLinux) && + NearbyFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature:: + kEnableBleV2)) { + mediums_->GetBleV2().StopLegacyAdvertising( + client->GetAdvertisingServiceId()); } } @@ -1355,18 +1391,41 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( NEARBY_LOGS(INFO) << "P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl: " "Ble legacy started advertising"; - NEARBY_LOG( - INFO, - "P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl: BT added"); + NEARBY_LOG(INFO, + "P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl: " + "BT added"); restarted_mediums.push_back(Medium::BLUETOOTH); } else { NEARBY_LOG(WARNING, "P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl: " - "BLE legacy " - "failed, revert BTC"); + "BLE legacy failed, revert BTC"); bluetooth_medium_.TurnOffDiscoverability(); bluetooth_medium_.StopAcceptingConnections(std::string(service_id)); } + } else if ((api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kChromeOS || + api::ImplementationPlatform::GetCurrentOS() == + api::OSName::kLinux) && + NearbyFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature:: + kEnableBleV2)) { + if (ble_v2_medium_.StartLegacyAdvertising( + std::string(service_id), std::string(local_endpoint_id), + advertising_options.fast_advertisement_service_uuid)) { + NEARBY_LOGS(INFO) + << __func__ << "Ble v2 started advertising for legacy device."; + restarted_mediums.push_back(Medium::BLUETOOTH); + NEARBY_LOGS(INFO) << __func__ + << "After Ble v2 started advertising, for " + "legacy, BT added to restarted mediums"; + } else { + NEARBY_LOGS(WARNING) + << __func__ + << "BLE v2 failed advertising for legacy device, revert BTC"; + bluetooth_medium_.TurnOffDiscoverability(); + bluetooth_medium_.StopAcceptingConnections(std::string(service_id)); + } + } else { restarted_mediums.push_back(Medium::BLUETOOTH); } @@ -1448,8 +1507,8 @@ P2pClusterPcpHandler::UpdateDiscoveryOptionsImpl( location::nearby::proto::connections::UNKNOWN_MEDIUM) { restarted_mediums.push_back(Medium::BLE); } else { - NEARBY_LOGS(WARNING) - << "UpdateDiscoveryOptionsImpl: unable to restart blev2 scanning"; + NEARBY_LOGS(WARNING) << "UpdateDiscoveryOptionsImpl: unable to " + "restart blev2 scanning"; } } else { if (StartBleScanning( diff --git a/connections/implementation/p2p_cluster_pcp_handler_test.cc b/connections/implementation/p2p_cluster_pcp_handler_test.cc index 5ca45ecf..ecf49f4a 100644 --- a/connections/implementation/p2p_cluster_pcp_handler_test.cc +++ b/connections/implementation/p2p_cluster_pcp_handler_test.cc @@ -74,9 +74,10 @@ class P2pClusterPcpHandlerTest protected: void SetUp() override { NEARBY_LOG(INFO, "SetUp: begin"); + auto ble_v2_enabled = std::get<1>(GetParam()); NearbyFlags::GetInstance().OverrideBoolFlagValue( config_package_nearby::nearby_connections_feature::kEnableBleV2, - std::get<1>(GetParam())); + ble_v2_enabled); if (advertising_options_.allowed.ble) { NEARBY_LOG(INFO, "SetUp: BLE enabled"); } @@ -89,6 +90,7 @@ class P2pClusterPcpHandlerTest if (advertising_options_.allowed.web_rtc) { NEARBY_LOG(INFO, "SetUp: WebRTC enabled"); } + NEARBY_LOG(INFO, "SetUp: ble v2 enabled: %d", ble_v2_enabled); NEARBY_LOG(INFO, "SetUp: end"); } @@ -157,6 +159,35 @@ TEST_P(P2pClusterPcpHandlerTest, CanAdvertise) { handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, {.endpoint_info = ByteArray{endpoint_name}}), Status{Status::kSuccess}); + handler_a.StopAdvertising(&client_a_); + env_.Stop(); +} + +TEST_P(P2pClusterPcpHandlerTest, AdvertiseForLegacyDeviceWithBt) { + env_.Start(); + std::string endpoint_name{"endpoint_name"}; + Mediums mediums_a; + EndpointChannelManager ecm_a; + EndpointManager em_a(&ecm_a); + BwuManager bwu_a(mediums_a, em_a, ecm_a, {}, {}); + InjectedBluetoothDeviceStore ibds_a; + P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a); + EXPECT_EQ( + handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, + {.endpoint_info = ByteArray{endpoint_name}}), + Status{Status::kSuccess}); + // advertising for legacy device depends on both BT and BLE V2 enabled. + if (std::get<0>(GetParam()).bluetooth && std::get<1>(GetParam())) { + EXPECT_TRUE(mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } else { + EXPECT_FALSE( + mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } + handler_a.StopAdvertising(&client_a_); + if (std::get<0>(GetParam()).bluetooth && std::get<1>(GetParam())) { + EXPECT_FALSE( + mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } env_.Stop(); } @@ -188,11 +219,15 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateAdvertisingOptions) { ASSERT_FALSE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); mediums_a.GetBleV2().StopAdvertising(service_id_); ASSERT_FALSE(mediums_a.GetBleV2().IsAdvertising(service_id_)); + BooleanMediumSelector enabled = advertising_options_.allowed; + if (ble_v2_enabled && enabled.bluetooth) { + EXPECT_FALSE( + mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } EXPECT_EQ( handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, {.endpoint_info = ByteArray{endpoint_name}}), Status{Status::kSuccess}); - BooleanMediumSelector enabled = advertising_options_.allowed; EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_)); EXPECT_EQ(enabled.wifi_lan, mediums_a.GetWifiLan().IsAdvertising(service_id_)); @@ -201,6 +236,9 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateAdvertisingOptions) { mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); EXPECT_EQ(enabled.bluetooth, mediums_a.GetBluetoothClassic().TurnOffDiscoverability()); + if (ble_v2_enabled && enabled.bluetooth) { + EXPECT_TRUE(mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } // Turn discoverability back on mediums_a.GetBluetoothClassic().TurnOnDiscoverability(service_id_); AdvertisingOptions new_options{ @@ -217,6 +255,11 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateAdvertisingOptions) { Status{Status::kSuccess}); if (ble_v2_enabled) { EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_)); + // Low power won't restart BT, nor BLE advertising for legacy device. + if (enabled.bluetooth) { + EXPECT_FALSE( + mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } } else { EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsAdvertising(service_id_)); } @@ -267,6 +310,10 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateAdvertisingOptionsNoLowPower) { true, // low_power false, // enable_bluetooth_listening }; + if (ble_v2_enabled && enabled.bluetooth) { + EXPECT_FALSE( + mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } EXPECT_EQ( handler_a.StartAdvertising(&client_a_, service_id_, old_options, {.endpoint_info = ByteArray{endpoint_name}}), @@ -277,6 +324,9 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateAdvertisingOptionsNoLowPower) { EXPECT_EQ( enabled.bluetooth, mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); + if (ble_v2_enabled && enabled.bluetooth) { + EXPECT_TRUE(mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } EXPECT_EQ(enabled.bluetooth, mediums_a.GetBluetoothClassic().TurnOffDiscoverability()); EXPECT_EQ(handler_a.UpdateAdvertisingOptions(&client_a_, service_id_, @@ -284,6 +334,10 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateAdvertisingOptionsNoLowPower) { Status{Status::kSuccess}); if (ble_v2_enabled) { EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_)); + if (enabled.bluetooth) { + EXPECT_TRUE( + mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } } else { EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsAdvertising(service_id_)); } @@ -295,6 +349,10 @@ TEST_P(P2pClusterPcpHandlerTest, CanUpdateAdvertisingOptionsNoLowPower) { enabled.bluetooth || enabled.ble, mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); handler_a.StopAdvertising(&client_a_); + if (ble_v2_enabled && enabled.bluetooth) { + EXPECT_FALSE( + mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + } env_.Stop(); } @@ -338,6 +396,51 @@ TEST_P(P2pClusterPcpHandlerTest, CanDiscover) { env_.Stop(); } +TEST_P(P2pClusterPcpHandlerTest, CanDiscoverLegacy) { + env_.Start(); + std::string endpoint_name{"endpoint_name"}; + Mediums mediums_a; + Mediums mediums_b; + EndpointChannelManager ecm_a; + EndpointChannelManager ecm_b; + EndpointManager em_a(&ecm_a); + EndpointManager em_b(&ecm_b); + BwuManager bwu_a(mediums_a, em_a, ecm_a, {}, {}); + BwuManager bwu_b(mediums_b, em_b, ecm_b, {}, {}); + InjectedBluetoothDeviceStore ibds_a; + InjectedBluetoothDeviceStore ibds_b; + P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a); + P2pClusterPcpHandler handler_b(&mediums_b, &em_b, &ecm_b, &bwu_b, ibds_b); + CountDownLatch latch(1); + EXPECT_EQ( + handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, + {.endpoint_info = ByteArray{endpoint_name}}), + Status{Status::kSuccess}); + EXPECT_EQ(handler_b.StartDiscovery( + &client_b_, service_id_, discovery_options_, + { + .endpoint_found_cb = + [&latch](const std::string& endpoint_id, + const ByteArray& endpoint_info, + const std::string& service_id) { + NEARBY_LOG(INFO, "Device discovered: id=%s", + endpoint_id.c_str()); + latch.CountDown(); + }, + }), + Status{Status::kSuccess}); + // advertising for legacy device depends on both BT and BLE V2 enabled. + // if (std::get<0>(GetParam()).bluetooth && std::get<1>(GetParam())) { + EXPECT_TRUE(latch.Await(absl::Milliseconds(1000)).result()); + /* } else { + EXPECT_FALSE(latch.Await(absl::Milliseconds(1000)).result()); + }*/ + // We discovered endpoint over one medium. Before we finish the test, we have + // to stop discovery for other mediums that may be still ongoing. + handler_b.StopDiscovery(&client_b_); + env_.Stop(); +} + TEST_P(P2pClusterPcpHandlerTest, CanBluetoothDiscoverChangeName) { env_.Start(); std::string endpoint_name{"endpoint_name"};