From df31f7810c4c7f368b99ecb009b932008d307940 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Wed, 7 Jun 2023 17:03:50 -0700 Subject: [PATCH] Added flag to control GATT enable/disable PiperOrigin-RevId: 538631107 --- .../flags/nearby_platform_feature_flags.h | 4 ++++ .../implementation/windows/ble_gatt_client.cc | 9 +++++++++ .../platform/implementation/windows/ble_v2.cc | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/internal/platform/flags/nearby_platform_feature_flags.h b/internal/platform/flags/nearby_platform_feature_flags.h index 32e5d7b3..6465404f 100644 --- a/internal/platform/flags/nearby_platform_feature_flags.h +++ b/internal/platform/flags/nearby_platform_feature_flags.h @@ -35,6 +35,10 @@ constexpr auto kEnableHotspotWin32Socket = constexpr auto kEnablePlatformThreadToNetwork = flags::Flag(kConfigPackage, "45412711", true); +// Disable/Enable GATT feature in BLE V2. +constexpr auto kEnableBleV2Gatt = + flags::Flag(kConfigPackage, "45415180", true); + } // namespace nearby_platform_feature } // namespace config_package_nearby } // namespace platform diff --git a/internal/platform/implementation/windows/ble_gatt_client.cc b/internal/platform/implementation/windows/ble_gatt_client.cc index c75600fb..505f56ca 100644 --- a/internal/platform/implementation/windows/ble_gatt_client.cc +++ b/internal/platform/implementation/windows/ble_gatt_client.cc @@ -33,7 +33,9 @@ #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "absl/types/optional.h" +#include "internal/flags/nearby_flags.h" #include "internal/platform/byte_array.h" +#include "internal/platform/flags/nearby_platform_feature_flags.h" #include "internal/platform/implementation/ble_v2.h" #include "internal/platform/implementation/windows/utils.h" #include "internal/platform/logging.h" @@ -109,6 +111,13 @@ BleGattClient::~BleGattClient() { bool BleGattClient::DiscoverServiceAndCharacteristics( const Uuid& service_uuid, const std::vector& characteristic_uuids) { + if (!NearbyFlags::GetInstance().GetBoolFlag( + platform::config_package_nearby::nearby_platform_feature:: + kEnableBleV2Gatt)) { + NEARBY_LOGS(WARNING) << __func__ << ": GATT is disabled."; + return false; + } + std::string flat_characteristics = absl::StrJoin(characteristic_uuids, ",", [](std::string* out, Uuid uuid) { absl::StrAppend(out, std::string(uuid)); diff --git a/internal/platform/implementation/windows/ble_v2.cc b/internal/platform/implementation/windows/ble_v2.cc index 09047a79..da8fc63d 100644 --- a/internal/platform/implementation/windows/ble_v2.cc +++ b/internal/platform/implementation/windows/ble_v2.cc @@ -29,10 +29,12 @@ #include "absl/strings/str_format.h" #include "absl/synchronization/mutex.h" #include "absl/time/time.h" +#include "internal/flags/nearby_flags.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/cancellation_flag_listener.h" +#include "internal/platform/flags/nearby_platform_feature_flags.h" #include "internal/platform/implementation/ble_v2.h" #include "internal/platform/implementation/windows/ble_gatt_client.h" #include "internal/platform/implementation/windows/ble_gatt_server.h" @@ -369,6 +371,13 @@ std::unique_ptr BleV2Medium::StartGattServer( api::ble_v2::ServerGattConnectionCallback callback) { NEARBY_LOGS(INFO) << __func__ << ": Start GATT server."; + if (!NearbyFlags::GetInstance().GetBoolFlag( + platform::config_package_nearby::nearby_platform_feature:: + kEnableBleV2Gatt)) { + NEARBY_LOGS(WARNING) << __func__ << ": GATT is disabled."; + return nullptr; + } + auto gatt_server = std::make_unique(adapter_, std::move(callback)); @@ -383,6 +392,14 @@ std::unique_ptr BleV2Medium::ConnectToGattServer( NEARBY_LOGS(INFO) << "ConnectToGattServer is called, address: " << peripheral.GetAddress() << ", power:" << TxPowerLevelToName(tx_power_level); + + if (!NearbyFlags::GetInstance().GetBoolFlag( + platform::config_package_nearby::nearby_platform_feature:: + kEnableBleV2Gatt)) { + NEARBY_LOGS(WARNING) << __func__ << ": GATT is disabled."; + return nullptr; + } + try { BluetoothLEDevice ble_device = BluetoothLEDevice::FromBluetoothAddressAsync(