From e539d021a3cdacb98818809e170d27fada02808a Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Tue, 14 Mar 2023 11:34:00 -0700 Subject: [PATCH] Added flag for BLE v2 in nearby connections PiperOrigin-RevId: 516585139 --- Package.swift | 2 + connections/dart/BUILD | 1 + connections/dart/core_adapter_dart.cc | 7 +- connections/implementation/BUILD | 2 + connections/implementation/flags/BUILD | 55 ++++++++ .../implementation/flags/connections_flags.cc | 125 ++++++++++++++++++ .../implementation/flags/connections_flags.h | 96 ++++++++++++++ .../flags/connections_flags_test.cc | 121 +++++++++++++++++ .../flags/nearby_connections_feature_flags.h | 42 ++++++ .../implementation/mediums/ble_v2/BUILD | 1 + .../ble_v2/ble_advertisement_header.cc | 7 +- .../implementation/p2p_cluster_pcp_handler.cc | 25 ++-- .../p2p_cluster_pcp_handler_test.cc | 9 +- .../p2p_point_to_point_pcp_handler_test.cc | 9 +- internal/platform/feature_flags.h | 4 - 15 files changed, 484 insertions(+), 22 deletions(-) create mode 100644 connections/implementation/flags/BUILD create mode 100644 connections/implementation/flags/connections_flags.cc create mode 100644 connections/implementation/flags/connections_flags.h create mode 100644 connections/implementation/flags/connections_flags_test.cc create mode 100644 connections/implementation/flags/nearby_connections_feature_flags.h diff --git a/Package.swift b/Package.swift index 98318cbb..18d3d36e 100644 --- a/Package.swift +++ b/Package.swift @@ -401,6 +401,7 @@ let package = Package( "WORKSPACE", // build files "connections/implementation/analytics/BUILD", + "connections/implementation/flags/BUILD", "connections/implementation/mediums/ble_v2/BUILD", "connections/implementation/mediums/BUILD", "connections/implementation/BUILD", @@ -438,6 +439,7 @@ let package = Package( "connections/implementation/wifi_direct_bwu_test.cc", "connections/implementation/wifi_hotspot_test.cc", "connections/implementation/analytics/analytics_recorder_test.cc", + "connections/implementation/flags/connections_flags_test.cc", "connections/implementation/analytics/throughput_recorder_test.cc", "connections/implementation/mediums/ble_v2_test.cc", "connections/implementation/mediums/ble_v2/bloom_filter_test.cc", diff --git a/connections/dart/BUILD b/connections/dart/BUILD index d1fa824f..6c8a2105 100644 --- a/connections/dart/BUILD +++ b/connections/dart/BUILD @@ -31,6 +31,7 @@ lexan.cc_windows_dll( deps = [ "//connections:core", "//connections/c", + "//connections/implementation/flags:connections_flags", "//internal/platform/implementation/windows", "//third_party/dart_lang/v2:dart_api_dl", "//third_party/webrtc/files/stable/webrtc/api:create_peerconnection_factory", diff --git a/connections/dart/core_adapter_dart.cc b/connections/dart/core_adapter_dart.cc index bfb82e3e..570d63c0 100644 --- a/connections/dart/core_adapter_dart.cc +++ b/connections/dart/core_adapter_dart.cc @@ -18,6 +18,8 @@ #include #include "connections/core.h" +#include "connections/implementation/flags/connections_flags.h" +#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/payload.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/logging.h" @@ -375,7 +377,10 @@ void EnableBleV2Dart(Core *pCore, int64_t enable, Dart_Port result_cb) { } port = result_cb; - FeatureFlags::GetMutableFlagsForTesting().support_ble_v2 = enable; + connections::ConnectionsFlags::GetInstance().OverrideBoolFlagValue( + connections::config_package_nearby::nearby_connections_feature:: + kEnableBleV2, + enable); PostResult(result_cb, Status::kSuccess); } diff --git a/connections/implementation/BUILD b/connections/implementation/BUILD index b782d0b6..ff951c94 100644 --- a/connections/implementation/BUILD +++ b/connections/implementation/BUILD @@ -147,6 +147,7 @@ cc_library( ":ukey2", "//connections:core_types", "//connections/implementation/analytics", + "//connections/implementation/flags:connections_flags", "//connections/implementation/mediums", "//connections/implementation/mediums:utils", "//connections/implementation/proto:offline_wire_formats_cc_proto", @@ -256,6 +257,7 @@ cc_test( ":internal_test", ":ukey2", "//connections:core_types", + "//connections/implementation/flags:connections_flags", "//connections/implementation/mediums", "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/analytics:event_logger", diff --git a/connections/implementation/flags/BUILD b/connections/implementation/flags/BUILD new file mode 100644 index 00000000..4267100b --- /dev/null +++ b/connections/implementation/flags/BUILD @@ -0,0 +1,55 @@ +# Copyright 2023 Google LLC +# +# 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 +# +# https://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. +licenses(["notice"]) + +cc_library( + name = "connections_flags", + srcs = [ + "connections_flags.cc", + ], + hdrs = [ + "connections_flags.h", + "nearby_connections_feature_flags.h", + ], + copts = [ + "-Ithird_party", + ], + visibility = [ + "//connections:__subpackages__", + "//internal/platform:__subpackages__", + "//location/nearby/cpp:__subpackages__", + ], + deps = [ + "//internal/flags:flag_reader", + "//internal/platform:types", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/synchronization", + ], +) + +cc_test( + name = "connections_flags_test", + srcs = ["connections_flags_test.cc"], + deps = [ + ":connections_flags", + "//internal/flags:flag_reader", + "//internal/platform/implementation/g3", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/connections/implementation/flags/connections_flags.cc b/connections/implementation/flags/connections_flags.cc new file mode 100644 index 00000000..e8f95aa4 --- /dev/null +++ b/connections/implementation/flags/connections_flags.cc @@ -0,0 +1,125 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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. + +#include "connections/implementation/flags/connections_flags.h" + +#include + +#include "internal/platform/mutex.h" +#include "internal/platform/mutex_lock.h" + +namespace nearby { +namespace connections { + +ConnectionsFlags& ConnectionsFlags::GetInstance() { + static ConnectionsFlags* sharing_flags = new ConnectionsFlags(); + return *sharing_flags; +} + +bool ConnectionsFlags::GetBoolFlag(const flags::Flag& flag) { + MutexLock lock(&mutex_); + + const auto& it = overrided_bool_flag_values_.find(flag.name()); + if (it != overrided_bool_flag_values_.end()) { + return it->second; + } + + if (flag_reader_ != nullptr) { + return flag_reader_->GetBoolFlag(flag); + } + return default_flag_reader_.GetBoolFlag(flag); +} + +int64_t ConnectionsFlags::GetInt64Flag(const flags::Flag& flag) { + MutexLock lock(&mutex_); + + const auto& it = overrided_int64_flag_values_.find(flag.name()); + if (it != overrided_int64_flag_values_.end()) { + return it->second; + } + + if (flag_reader_ != nullptr) { + return flag_reader_->GetInt64Flag(flag); + } + return default_flag_reader_.GetInt64Flag(flag); +} + +double ConnectionsFlags::GetDoubleFlag(const flags::Flag& flag) { + MutexLock lock(&mutex_); + + const auto& it = overrided_double_flag_values_.find(flag.name()); + if (it != overrided_double_flag_values_.end()) { + return it->second; + } + + if (flag_reader_ != nullptr) { + return flag_reader_->GetDoubleFlag(flag); + } + return default_flag_reader_.GetDoubleFlag(flag); +} + +std::string ConnectionsFlags::GetStringFlag( + const flags::Flag& flag) { + MutexLock lock(&mutex_); + + const auto& it = overrided_string_flag_values_.find(flag.name()); + if (it != overrided_string_flag_values_.end()) { + return it->second; + } + + if (flag_reader_ != nullptr) { + return flag_reader_->GetStringFlag(flag); + } + return default_flag_reader_.GetStringFlag(flag); +} + +void ConnectionsFlags::SetFlagReader(flags::FlagReader& flag_reader) { + MutexLock lock(&mutex_); + flag_reader_ = &flag_reader; +} + +void ConnectionsFlags::OverrideBoolFlagValue(const flags::Flag& flag, + bool new_value) { + MutexLock lock(&mutex_); + overrided_bool_flag_values_[flag.name()] = new_value; +} + +void ConnectionsFlags::OverrideInt64FlagValue(const flags::Flag& flag, + int64_t new_value) { + MutexLock lock(&mutex_); + overrided_int64_flag_values_[flag.name()] = new_value; +} + +void ConnectionsFlags::OverrideDoubleFlagValue(const flags::Flag& flag, + double new_value) { + MutexLock lock(&mutex_); + overrided_double_flag_values_[flag.name()] = new_value; +} + +void ConnectionsFlags::OverrideStringFlagValue( + const flags::Flag& flag, absl::string_view new_value) { + MutexLock lock(&mutex_); + overrided_string_flag_values_[flag.name()] = std::string(new_value); +} + +void ConnectionsFlags::ResetOverridedValues() { + MutexLock lock(&mutex_); + overrided_bool_flag_values_.clear(); + overrided_int64_flag_values_.clear(); + overrided_double_flag_values_.clear(); + overrided_string_flag_values_.clear(); +} + +} // namespace connections +} // namespace nearby diff --git a/connections/implementation/flags/connections_flags.h b/connections/implementation/flags/connections_flags.h new file mode 100644 index 00000000..7094c8a6 --- /dev/null +++ b/connections/implementation/flags/connections_flags.h @@ -0,0 +1,96 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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. + +#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_CONNECTIONS_FLAGS_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_CONNECTIONS_FLAGS_H_ + +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" +#include "connections/implementation/flags/nearby_connections_feature_flags.h" +#include "internal/flags/default_flag_reader.h" +#include "internal/flags/flag_reader.h" +#include "internal/platform/mutex.h" + +namespace nearby { +namespace connections { + +class ConnectionsFlags : public nearby::flags::FlagReader { + public: + ~ConnectionsFlags() override = default; + + static ConnectionsFlags& GetInstance(); + + // Reads flag with boolean value. + bool GetBoolFlag(const flags::Flag& flag) override + ABSL_LOCKS_EXCLUDED(mutex_); + + // Reads flag with int64_t value. + int64_t GetInt64Flag(const flags::Flag& flag) override + ABSL_LOCKS_EXCLUDED(mutex_); + + // Reads flag with double value. + double GetDoubleFlag(const flags::Flag& flag) override + ABSL_LOCKS_EXCLUDED(mutex_); + + // Reads flag with string value. + std::string GetStringFlag(const flags::Flag& flag) override + ABSL_LOCKS_EXCLUDED(mutex_); + + void SetFlagReader(flags::FlagReader& flag_reader) + ABSL_LOCKS_EXCLUDED(mutex_); + + // Override the default value of the flags. The major purpose of the method is + // for test. + void OverrideBoolFlagValue(const flags::Flag& flag, bool new_value) + ABSL_LOCKS_EXCLUDED(mutex_); + + void OverrideInt64FlagValue(const flags::Flag& flag, + int64_t new_value) ABSL_LOCKS_EXCLUDED(mutex_); + + void OverrideDoubleFlagValue(const flags::Flag& flag, + double new_value) ABSL_LOCKS_EXCLUDED(mutex_); + + void OverrideStringFlagValue(const flags::Flag& flag, + absl::string_view new_value) + ABSL_LOCKS_EXCLUDED(mutex_); + + // Reset all overrided values. + void ResetOverridedValues() ABSL_LOCKS_EXCLUDED(mutex_); + + private: + ConnectionsFlags() = default; + + flags::FlagReader* flag_reader_ = nullptr; + flags::DefaultFlagReader default_flag_reader_; + + mutable Mutex mutex_; + absl::flat_hash_map overrided_bool_flag_values_ + ABSL_GUARDED_BY(mutex_); + + absl::flat_hash_map overrided_int64_flag_values_ + ABSL_GUARDED_BY(mutex_); + + absl::flat_hash_map overrided_double_flag_values_ + ABSL_GUARDED_BY(mutex_); + + absl::flat_hash_map overrided_string_flag_values_ + ABSL_GUARDED_BY(mutex_); +}; + +} // namespace connections +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_CONNECTIONS_FLAGS_H_ diff --git a/connections/implementation/flags/connections_flags_test.cc b/connections/implementation/flags/connections_flags_test.cc new file mode 100644 index 00000000..7bc588f0 --- /dev/null +++ b/connections/implementation/flags/connections_flags_test.cc @@ -0,0 +1,121 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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. + +#include "connections/implementation/flags/connections_flags.h" + +#include +#include +#include +#include + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" +#include "absl/strings/string_view.h" +#include "internal/flags/flag.h" +#include "internal/flags/flag_reader.h" + +namespace nearby { +namespace connections { +namespace { + +constexpr auto kTestBoolFlag = + flags::Flag("test_package", "45401515", false); +constexpr auto kTestInt64Flag = + flags::Flag("test_package", "45401516", 666); +constexpr auto kTestDoubleFlag = + flags::Flag("test_package", "45401517", 66.66); +constexpr auto kTestStringFlag = + flags::Flag("test_package", "45401518", "string"); + +constexpr bool kTestBoolFlagTestValue = true; +constexpr int64_t kTestInt64FlagTestValue = 666; +constexpr double kTestDoubleFlagTestValue = 88.8; +constexpr std::string_view kTestStringFlagTestValue = "hello"; + +class MockFlagReader : public flags::FlagReader { + public: + MOCK_METHOD(bool, GetBoolFlag, (const flags::Flag& flag), (override)); + MOCK_METHOD(int64_t, GetInt64Flag, (const flags::Flag& flag), + (override)); + MOCK_METHOD(double, GetDoubleFlag, (const flags::Flag& flag), + (override)); + MOCK_METHOD(std::string, GetStringFlag, + (const flags::Flag& flag), (override)); +}; + +TEST(ConnectionsFlags, GetDefaultValues) { + EXPECT_EQ(ConnectionsFlags::GetInstance().GetBoolFlag(kTestBoolFlag), + kTestBoolFlag.default_value()); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetInt64Flag(kTestInt64Flag), + kTestInt64Flag.default_value()); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetDoubleFlag(kTestDoubleFlag), + kTestDoubleFlag.default_value()); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetStringFlag(kTestStringFlag), + kTestStringFlag.default_value()); +} + +TEST(ConnectionsFlags, OverrideDefaultValues) { + ConnectionsFlags::GetInstance().OverrideBoolFlagValue(kTestBoolFlag, + kTestBoolFlagTestValue); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetBoolFlag(kTestBoolFlag), + kTestBoolFlagTestValue); + ConnectionsFlags::GetInstance().OverrideInt64FlagValue( + kTestInt64Flag, kTestInt64FlagTestValue); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetInt64Flag(kTestInt64Flag), + kTestInt64FlagTestValue); + ConnectionsFlags::GetInstance().OverrideDoubleFlagValue( + kTestDoubleFlag, kTestDoubleFlagTestValue); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetDoubleFlag(kTestDoubleFlag), + kTestDoubleFlagTestValue); + ConnectionsFlags::GetInstance().OverrideStringFlagValue( + kTestStringFlag, kTestStringFlagTestValue); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetStringFlag(kTestStringFlag), + kTestStringFlagTestValue); + ConnectionsFlags::GetInstance().ResetOverridedValues(); +} + +TEST(ConnectionsFlags, SetFlagReader) { + auto flag_reader = std::make_unique<::testing::NiceMock>(); + ConnectionsFlags::GetInstance().SetFlagReader(*flag_reader.get()); + EXPECT_CALL(*flag_reader, GetBoolFlag(::testing::_)) + .WillOnce(::testing::Invoke([=](const flags::Flag& flag) { + return kTestBoolFlagTestValue; + })); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetBoolFlag(kTestBoolFlag), + kTestBoolFlagTestValue); + EXPECT_CALL(*flag_reader, GetInt64Flag(::testing::_)) + .WillOnce(::testing::Invoke([=](const flags::Flag& flag) { + return kTestInt64FlagTestValue; + })); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetInt64Flag(kTestInt64Flag), + kTestInt64FlagTestValue); + EXPECT_CALL(*flag_reader, GetDoubleFlag(::testing::_)) + .WillOnce(::testing::Invoke([=](const flags::Flag& flag) { + return kTestDoubleFlagTestValue; + })); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetDoubleFlag(kTestDoubleFlag), + kTestDoubleFlagTestValue); + EXPECT_CALL(*flag_reader, GetStringFlag(::testing::_)) + .WillOnce( + ::testing::Invoke([=](const flags::Flag& flag) { + return std::string(kTestStringFlagTestValue); + })); + EXPECT_EQ(ConnectionsFlags::GetInstance().GetStringFlag(kTestStringFlag), + kTestStringFlagTestValue); +} + +} // namespace +} // namespace connections +} // namespace nearby diff --git a/connections/implementation/flags/nearby_connections_feature_flags.h b/connections/implementation/flags/nearby_connections_feature_flags.h new file mode 100644 index 00000000..4ea46e4e --- /dev/null +++ b/connections/implementation/flags/nearby_connections_feature_flags.h @@ -0,0 +1,42 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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. + +#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_NEARBY_CONNECTIONS_FEATURE_FLAGS_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_NEARBY_CONNECTIONS_FEATURE_FLAGS_H_ + +#include "absl/strings/string_view.h" +#include "internal/flags/flag.h" + +namespace nearby { +namespace connections { +namespace config_package_nearby { + +#ifndef PHENOTYPE_CONFIG_PACKAGE_NEARBY +#define PHENOTYPE_CONFIG_PACKAGE_NEARBY +constexpr absl::string_view kConfigPackage = "nearby"; +#endif // PHENOTYPE_CONFIG_PACKAGE_NEARBY + +// The Nearby Connections features. +namespace nearby_connections_feature { + +// Disable/Enable BLE v2 in Nearby Connections SDK. +constexpr auto kEnableBleV2 = + flags::Flag(kConfigPackage, "45401515", false); + +} // namespace nearby_connections_feature +} // namespace config_package_nearby +} // namespace connections +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_NEARBY_CONNECTIONS_FEATURE_FLAGS_H_ diff --git a/connections/implementation/mediums/ble_v2/BUILD b/connections/implementation/mediums/ble_v2/BUILD index 58926906..dd566fb2 100644 --- a/connections/implementation/mediums/ble_v2/BUILD +++ b/connections/implementation/mediums/ble_v2/BUILD @@ -40,6 +40,7 @@ cc_library( ], deps = [ "//connections:core_types", + "//connections/implementation/flags:connections_flags", "//connections/implementation/mediums:utils", "//internal/platform:base", "//internal/platform:comm", diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc b/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc index 65e96bb3..ad1aa187 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc +++ b/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc @@ -20,6 +20,8 @@ #include #include "absl/strings/str_cat.h" +#include "connections/implementation/flags/connections_flags.h" +#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "internal/platform/base64_utils.h" #include "internal/platform/base_input_stream.h" #include "internal/platform/byte_array.h" @@ -142,9 +144,8 @@ BleAdvertisementHeader::operator ByteArray() const { std::string(advertisement_hash_), std::string(psm_bytes)); // clang-format on - if (FeatureFlags::GetInstance() - .GetFlags() - .enable_ble_v2_advertisement_base64_encoding) { + if (ConnectionsFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature::kEnableBleV2)) { return ByteArray(Base64Utils::Encode(ByteArray(std::move(out)))); } else { return ByteArray(std::move(out)); diff --git a/connections/implementation/p2p_cluster_pcp_handler.cc b/connections/implementation/p2p_cluster_pcp_handler.cc index f62de6b7..75ca574c 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.cc +++ b/connections/implementation/p2p_cluster_pcp_handler.cc @@ -26,6 +26,8 @@ #include "connections/implementation/ble_v2_endpoint_channel.h" #include "connections/implementation/bluetooth_endpoint_channel.h" #include "connections/implementation/bwu_manager.h" +#include "connections/implementation/flags/connections_flags.h" +#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/mediums/utils.h" #include "connections/implementation/wifi_lan_endpoint_channel.h" #include "internal/platform/nsd_service_info.h" @@ -81,7 +83,8 @@ P2pClusterPcpHandler::GetConnectionMediumsByPriority() { if (bluetooth_medium_.IsAvailable()) { mediums.push_back(location::nearby::proto::connections::BLUETOOTH); } - if (FeatureFlags::GetInstance().GetFlags().support_ble_v2) { + if (ConnectionsFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature::kEnableBleV2)) { if (ble_v2_medium_.IsAvailable()) { mediums.push_back(location::nearby::proto::connections::BLE); } @@ -136,7 +139,8 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( } if (advertising_options.allowed.ble) { - if (FeatureFlags::GetInstance().GetFlags().support_ble_v2) { + if (ConnectionsFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature::kEnableBleV2)) { location::nearby::proto::connections::Medium ble_v2_medium = StartBleV2Advertising(client, service_id, local_endpoint_id, local_endpoint_info, advertising_options, @@ -192,7 +196,8 @@ Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) { bluetooth_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); - if (FeatureFlags::GetInstance().GetFlags().support_ble_v2) { + if (ConnectionsFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature::kEnableBleV2)) { ble_v2_medium_.StopAdvertising(client->GetAdvertisingServiceId()); ble_v2_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); } else { @@ -961,7 +966,8 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( } if (discovery_options.allowed.ble) { - if (FeatureFlags::GetInstance().GetFlags().support_ble_v2) { + if (ConnectionsFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature::kEnableBleV2)) { location::nearby::proto::connections::Medium ble_v2_medium = StartBleV2Scanning( { @@ -1029,7 +1035,8 @@ Status P2pClusterPcpHandler::StopDiscoveryImpl(ClientProxy* client) { << bluetooth_classic_discoverer_client_id_; } - if (FeatureFlags::GetInstance().GetFlags().support_ble_v2) { + if (ConnectionsFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature::kEnableBleV2)) { ble_v2_medium_.StopScanning(client->GetDiscoveryServiceId()); } else { ble_medium_.StopScanning(client->GetDiscoveryServiceId()); @@ -1079,7 +1086,9 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::ConnectImpl( break; } case location::nearby::proto::connections::Medium::BLE: { - if (FeatureFlags::GetInstance().GetFlags().support_ble_v2) { + if (ConnectionsFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature:: + kEnableBleV2)) { auto* ble_v2_endpoint = down_cast(endpoint); if (ble_v2_endpoint) { return BleV2ConnectImpl(client, ble_v2_endpoint); @@ -1876,8 +1885,8 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl( endpoint->service_id, endpoint->service_info, client->GetCancellationFlag(endpoint->endpoint_id)); NEARBY_LOGS(INFO) << "In WifiLanConnectImpl(), connect to service " - << " socket=" << &socket.GetImpl() - << " for endpoint(id=" << endpoint->endpoint_id << ")."; + << " socket=" << &socket.GetImpl() + << " for endpoint(id=" << endpoint->endpoint_id << ")."; if (!socket.IsValid()) { NEARBY_LOGS(ERROR) << "In WifiLanConnectImpl(), failed to connect to service " diff --git a/connections/implementation/p2p_cluster_pcp_handler_test.cc b/connections/implementation/p2p_cluster_pcp_handler_test.cc index 0ba74b5c..947f43ee 100644 --- a/connections/implementation/p2p_cluster_pcp_handler_test.cc +++ b/connections/implementation/p2p_cluster_pcp_handler_test.cc @@ -22,6 +22,8 @@ #include "gtest/gtest.h" #include "absl/time/time.h" #include "connections/implementation/bwu_manager.h" +#include "connections/implementation/flags/connections_flags.h" +#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/injected_bluetooth_device_store.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/logging.h" @@ -60,15 +62,16 @@ constexpr BooleanMediumSelector kTestCases[] = { }, }; -// Combines the bool `support_ble_v2` as param testing but should revert it back +// Combines the bool `kEnableBleV2` as param testing but should revert it back // if ble_v2 is done and ble will be replaced by ble_v2. class P2pClusterPcpHandlerTest : public testing::TestWithParam> { protected: void SetUp() override { NEARBY_LOG(INFO, "SetUp: begin"); - FeatureFlags::GetMutableFlagsForTesting().support_ble_v2 = - std::get<1>(GetParam()); + ConnectionsFlags::GetInstance().OverrideBoolFlagValue( + config_package_nearby::nearby_connections_feature::kEnableBleV2, + std::get<1>(GetParam())); if (advertising_options_.allowed.ble) { NEARBY_LOG(INFO, "SetUp: BLE enabled"); } diff --git a/connections/implementation/p2p_point_to_point_pcp_handler_test.cc b/connections/implementation/p2p_point_to_point_pcp_handler_test.cc index 0d947cc4..6a5c9df7 100644 --- a/connections/implementation/p2p_point_to_point_pcp_handler_test.cc +++ b/connections/implementation/p2p_point_to_point_pcp_handler_test.cc @@ -25,6 +25,8 @@ #include "gtest/gtest.h" #include "absl/time/time.h" #include "connections/implementation/bwu_manager.h" +#include "connections/implementation/flags/connections_flags.h" +#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/injected_bluetooth_device_store.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/logging.h" @@ -69,15 +71,16 @@ constexpr BooleanMediumSelector kTestCases[] = { }, }; -// Combines the bool `support_ble_v2` as param testing but should revert it back +// Combines the bool `kEnableBleV2` as param testing but should revert it back // if ble_v2 is done and ble will be replaced by ble_v2. class P2pPointToPointPcpHandlerTest : public testing::TestWithParam> { protected: void SetUp() override { NEARBY_LOG(INFO, "SetUp: begin"); - FeatureFlags::GetMutableFlagsForTesting().support_ble_v2 = - std::get<1>(GetParam()); + ConnectionsFlags::GetInstance().OverrideBoolFlagValue( + config_package_nearby::nearby_connections_feature::kEnableBleV2, + std::get<1>(GetParam())); if (advertising_options_.allowed.ble) { NEARBY_LOG(INFO, "SetUp: BLE enabled"); } diff --git a/internal/platform/feature_flags.h b/internal/platform/feature_flags.h index fa002cfc..9aae0f39 100644 --- a/internal/platform/feature_flags.h +++ b/internal/platform/feature_flags.h @@ -45,10 +45,6 @@ class FeatureFlags { // necessary to properly support multiple BWU mediums, multiple service, and // multiple endpionts. bool support_multiple_bwu_mediums = true; - // Ble v2/v1 switch flag: the flag will be removed once v2 refactor is done. - bool support_ble_v2 = false; - // Controls BLE advertisement data format in base64 or not. - bool enable_ble_v2_advertisement_base64_encoding = false; // Allows the code to change the bluetooth radio state bool enable_set_radio_state = false; // If the feature is enabled, medium connection will timeout when cannot