From f7e96e518ccd353a6983398f5be89b972b3a737c Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 8 May 2025 16:03:23 -0700 Subject: [PATCH] Use DCT flag to control BLE encoding PiperOrigin-RevId: 756496067 --- Package.swift | 1 + internal/platform/implementation/apple/BUILD | 1 + .../platform/implementation/apple/Flags/BUILD | 29 +++++++++++++++++++ .../apple/Flags/GNCFeatureFlags.h | 23 +++++++++++++++ .../apple/Flags/GNCFeatureFlags.mm | 27 +++++++++++++++++ .../apple/Mediums/BLEv2/GNCBLEGATTServer.m | 8 ++--- .../apple/Mediums/BLEv2/GNCBLEMedium.m | 10 +++---- .../implementation/apple/Mediums/BUILD | 1 + .../implementation/apple/ble_gatt_client.mm | 19 ++++++------ 9 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 internal/platform/implementation/apple/Flags/BUILD create mode 100644 internal/platform/implementation/apple/Flags/GNCFeatureFlags.h create mode 100644 internal/platform/implementation/apple/Flags/GNCFeatureFlags.mm diff --git a/Package.swift b/Package.swift index 7c5fdc60..4c3aa2f8 100644 --- a/Package.swift +++ b/Package.swift @@ -423,6 +423,7 @@ let package = Package( "internal/weave/sockets/BUILD", "internal/platform/flags/BUILD", "internal/platform/implementation/shared/BUILD", + "internal/platform/implementation/apple/Flags/BUILD", "internal/platform/implementation/apple/Mediums/BUILD", "internal/platform/implementation/apple/Mediums/Ble/Sockets/BUILD", "internal/platform/implementation/apple/Tests/BUILD", diff --git a/internal/platform/implementation/apple/BUILD b/internal/platform/implementation/apple/BUILD index 071ae6b9..60115d34 100644 --- a/internal/platform/implementation/apple/BUILD +++ b/internal/platform/implementation/apple/BUILD @@ -118,6 +118,7 @@ objc_library( "//internal/platform:cancellation_flag", "//internal/platform:uuid", "//internal/platform/implementation:comm", + "//internal/platform/implementation/apple/Flags", "//internal/platform/implementation/apple/Mediums", "//internal/platform/implementation/apple/Mediums/Ble/Sockets:Central", "//internal/platform/implementation/apple/Mediums/Ble/Sockets:Peripheral", diff --git a/internal/platform/implementation/apple/Flags/BUILD b/internal/platform/implementation/apple/Flags/BUILD new file mode 100644 index 00000000..4eaf9295 --- /dev/null +++ b/internal/platform/implementation/apple/Flags/BUILD @@ -0,0 +1,29 @@ +# Copyright 2025 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"]) + +package(default_visibility = [ + "//internal/platform/implementation/apple:__subpackages__", +]) + +objc_library( + name = "Flags", + srcs = ["GNCFeatureFlags.mm"], + hdrs = ["GNCFeatureFlags.h"], + deps = [ + "//connections/implementation/flags:connections_flags", + "//internal/flags:nearby_flags", + "//third_party/apple_frameworks:Foundation", + ], +) diff --git a/internal/platform/implementation/apple/Flags/GNCFeatureFlags.h b/internal/platform/implementation/apple/Flags/GNCFeatureFlags.h new file mode 100644 index 00000000..54328ce7 --- /dev/null +++ b/internal/platform/implementation/apple/Flags/GNCFeatureFlags.h @@ -0,0 +1,23 @@ +// Copyright 2025 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. + +#import + +/** A utility class for accessing and overriding Nearby feature flags. */ +@interface GNCFeatureFlags : NSObject + +/** Checks whether DCT is enabled in the Nearby Connections SDK. */ +@property(nonatomic, class, readonly) BOOL dctEnabled; + +@end diff --git a/internal/platform/implementation/apple/Flags/GNCFeatureFlags.mm b/internal/platform/implementation/apple/Flags/GNCFeatureFlags.mm new file mode 100644 index 00000000..be4fc1c5 --- /dev/null +++ b/internal/platform/implementation/apple/Flags/GNCFeatureFlags.mm @@ -0,0 +1,27 @@ +// Copyright 2025 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. + +#import "internal/platform/implementation/apple/Flags/GNCFeatureFlags.h" + +#include "connections/implementation/flags/nearby_connections_feature_flags.h" +#include "internal/flags/nearby_flags.h" + +@implementation GNCFeatureFlags + ++ (BOOL)dctEnabled { + return nearby::NearbyFlags::GetInstance().GetBoolFlag( + nearby::connections::config_package_nearby::nearby_connections_feature::kEnableDct); +} + +@end diff --git a/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTServer.m b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTServer.m index 440e5f65..efc80d4d 100644 --- a/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTServer.m +++ b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTServer.m @@ -17,6 +17,7 @@ #import #import +#import "internal/platform/implementation/apple/Flags/GNCFeatureFlags.h" #import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEError.h" #import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h" #import "internal/platform/implementation/apple/Mediums/BLEv2/GNCPeripheralManager.h" @@ -204,11 +205,8 @@ static char *const kGNCBLEGATTServerQueueLabel = "com.nearby.GNCBLEGATTServer"; // data is unavailable. CBUUID *serviceUUID = [serviceData.allKeys objectAtIndex:0]; NSData *value = [serviceData objectForKey:serviceUUID]; -#if defined(NC_IOS_SDK) - NSString *encoded = [value base85EncodedString]; -#else - NSString *encoded = [value webSafeBase64EncodedString]; -#endif // defined(NC_IOS_SDK) + NSString *encoded = GNCFeatureFlags.dctEnabled ? [value base85EncodedString] + : [value webSafeBase64EncodedString]; // 23 bytes is the standard length which we used in the NC protocol with PSM. The BLE v2 // advertisement header is default with psm value included so the length is always 23 bytes. diff --git a/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEMedium.m b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEMedium.m index e14e7e18..783a95b2 100644 --- a/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEMedium.m +++ b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEMedium.m @@ -17,6 +17,7 @@ #import #import +#import "internal/platform/implementation/apple/Flags/GNCFeatureFlags.h" #import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEError.h" #import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTClient.h" #import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTServer.h" @@ -283,11 +284,10 @@ static GNCBLEL2CAPServer *_Nonnull CreateL2CapServer( if (!localName) { return @{}; } -#if defined(NC_IOS_SDK) - NSData *data = [[NSData alloc] initWithBase85EncodedString:localName]; -#else - NSData *data = [[NSData alloc] initWithWebSafeBase64EncodedString:localName]; -#endif // defined(NC_IOS_SDK) + + NSData *data = GNCFeatureFlags.dctEnabled + ? [[NSData alloc] initWithBase85EncodedString:localName] + : [[NSData alloc] initWithWebSafeBase64EncodedString:localName]; // A Nearby Apple advertisement should only have a single service, so simply grab the first one if // it exists. diff --git a/internal/platform/implementation/apple/Mediums/BUILD b/internal/platform/implementation/apple/Mediums/BUILD index 5ae83ced..03c1d3d3 100644 --- a/internal/platform/implementation/apple/Mediums/BUILD +++ b/internal/platform/implementation/apple/Mediums/BUILD @@ -79,6 +79,7 @@ objc_library( deps = [ "//internal/encoding:base85", "//internal/platform/implementation/apple:Shared", + "//internal/platform/implementation/apple/Flags", "//internal/platform/implementation/apple/Mediums/Ble/Sockets:Shared", "//proto/mediums:ble_frames_cc_proto", "//third_party/apple_frameworks:CoreBluetooth", diff --git a/internal/platform/implementation/apple/ble_gatt_client.mm b/internal/platform/implementation/apple/ble_gatt_client.mm index b099118b..c67332ca 100644 --- a/internal/platform/implementation/apple/ble_gatt_client.mm +++ b/internal/platform/implementation/apple/ble_gatt_client.mm @@ -23,6 +23,7 @@ #include "internal/platform/implementation/ble_v2.h" +#import "internal/platform/implementation/apple/Flags/GNCFeatureFlags.h" #import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTClient.h" #import "internal/platform/implementation/apple/ble_utils.h" #import "GoogleToolboxForMac/GTMLogger.h" @@ -128,15 +129,15 @@ bool GattClient::SetCharacteristicSubscription( } void GattClient::Disconnect() { -// There seems to be an issue between some iOS<>Android device pairs where the Android device will -// not connect to the iOS device if the iOS device disconnects and then attempts to reconnect. -// Because of this, we no-op here instead of calling `[gatt_client_ disconnect]`. -// See: b/375176623 -#if defined(NC_IOS_SDK) - // Avoid to impact GTV functionality, put the disconnect for NC iOS SDK only, so thatGATT client - // can reconnect to the GATT server. - [gatt_client_ disconnect]; -#endif + // There seems to be an issue between some iOS<>Android device pairs where the Android device will + // not connect to the iOS device if the iOS device disconnects and then attempts to reconnect. + // Because of this, we no-op here instead of calling `[gatt_client_ disconnect]`. + // See: b/375176623 + if (GNCFeatureFlags.dctEnabled) { + // Avoid to impact GTV functionality, put the disconnect for NC iOS SDK only, so thatGATT client + // can reconnect to the GATT server. + [gatt_client_ disconnect]; + } } } // namespace apple