From e0b2e176c8b17ef61ad09c16df2f2262062df548 Mon Sep 17 00:00:00 2001 From: Nick Bourdakos Date: Thu, 13 Jul 2023 16:02:21 -0700 Subject: [PATCH] Add Objective-C GATT characteristic container. PiperOrigin-RevId: 547954006 --- .../Mediums/BLEv2/GNCBLEGATTCharacteristic.h | 69 +++++++++++++++++++ .../Mediums/BLEv2/GNCBLEGATTCharacteristic.m | 40 +++++++++++ .../implementation/apple/Mediums/BUILD | 2 + .../platform/implementation/apple/Tests/BUILD | 5 +- .../Tests/GNCBLEGATTCharacteristicTest.mm | 53 ++++++++++++++ 5 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h create mode 100644 internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.m create mode 100644 internal/platform/implementation/apple/Tests/GNCBLEGATTCharacteristicTest.mm diff --git a/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h new file mode 100644 index 00000000..a0e56440 --- /dev/null +++ b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h @@ -0,0 +1,69 @@ +// 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. + +#import +#import + +/** A container for GATT characteristic information. */ +@interface GNCBLEGATTCharacteristic : NSObject + +/** @remark init is not an available initializer. */ +- (nonnull instancetype)init NS_UNAVAILABLE; + +/** + * Creates a container for GATT characteristic information. + * + * @param characteristicUUID The UUID of the characteristic. + * @param serviceUUID The UUID of the service. + */ +- (instancetype)initWithUUID:(nonnull CBUUID *)characteristicUUID + serviceUUID:(nonnull CBUUID *)serviceUUID; + +/** + * Creates a container for GATT characteristic information with permissions and properties. + * + * @param characteristicUUID A 128-bit UUID that identifies the characteristic. + * @param serviceUUID A 128-bit UUID that identifies the service that the characteristic belongs to. + * @param permissions The permissions of the characteristic value. + * @param properties The properties of the characteristic. + */ +- (nonnull instancetype)initWithUUID:(nonnull CBUUID *)characteristicUUID + serviceUUID:(nonnull CBUUID *)serviceUUID + permissions:(CBAttributePermissions)permissions + properties:(CBCharacteristicProperties)properties + NS_DESIGNATED_INITIALIZER; + +/** The 128-bit UUID that identifies the characteristic. */ +@property(nonatomic, readonly, nonnull) CBUUID *characteristicUUID; + +/** The 128-bit UUID that identifies the service that the characteristic belongs to. */ +@property(nonatomic, readonly, nonnull) CBUUID *serviceUUID; + +/** + * The permissions of the characteristic value. + * + * Characteristic permissions represent the read and write permissions for a characteristic's value. + * These permissions can be a combination of multiple values. + */ +@property(nonatomic, readonly) CBAttributePermissions permissions; + +/** + * The properties of the characteristic. + * + * The properties of a characteristic determine the access to and use of the characteristic's value + * and descriptors. These properties can be a combination of multiple values. + */ +@property(nonatomic, readonly) CBCharacteristicProperties properties; + +@end diff --git a/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.m b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.m new file mode 100644 index 00000000..de8dea86 --- /dev/null +++ b/internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.m @@ -0,0 +1,40 @@ +// 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. + +#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h" + +#import +#import + +@implementation GNCBLEGATTCharacteristic + +- (instancetype)initWithUUID:(CBUUID *)characteristicUUID serviceUUID:(CBUUID *)serviceUUID { + return [self initWithUUID:characteristicUUID serviceUUID:serviceUUID permissions:0 properties:0]; +} + +- (instancetype)initWithUUID:(CBUUID *)characteristicUUID + serviceUUID:(CBUUID *)serviceUUID + permissions:(CBAttributePermissions)permissions + properties:(CBCharacteristicProperties)properties { + self = [super init]; + if (self) { + _characteristicUUID = [characteristicUUID copy]; + _serviceUUID = [serviceUUID copy]; + _permissions = permissions; + _properties = properties; + } + return self; +} + +@end diff --git a/internal/platform/implementation/apple/Mediums/BUILD b/internal/platform/implementation/apple/Mediums/BUILD index e5ec3b9f..43b4056e 100644 --- a/internal/platform/implementation/apple/Mediums/BUILD +++ b/internal/platform/implementation/apple/Mediums/BUILD @@ -18,6 +18,7 @@ package(default_visibility = ["//internal/platform/implementation/apple:__subpac objc_library( name = "Mediums", srcs = [ + "BLEv2/GNCBLEGATTCharacteristic.m", "Ble/GNCMBleCentral.m", "Ble/GNCMBleConnection.m", "Ble/GNCMBlePeripheral.m", @@ -33,6 +34,7 @@ objc_library( "WiFiLAN/GNCWiFiLANSocket.m", ], hdrs = [ + "BLEv2/GNCBLEGATTCharacteristic.h", "Ble/GNCMBleCentral.h", "Ble/GNCMBleConnection.h", "Ble/GNCMBlePeripheral.h", diff --git a/internal/platform/implementation/apple/Tests/BUILD b/internal/platform/implementation/apple/Tests/BUILD index 12775041..09a087c5 100644 --- a/internal/platform/implementation/apple/Tests/BUILD +++ b/internal/platform/implementation/apple/Tests/BUILD @@ -21,8 +21,9 @@ package(default_visibility = ["//visibility:public"]) objc_library( name = "PlatformTestslib", - testonly = 1, + testonly = True, srcs = [ + "GNCBLEGATTCharacteristicTest.mm", "GNCBleTest.mm", "GNCBluetoothAdapterTest.mm", "GNCCryptoTest.mm", @@ -32,7 +33,6 @@ objc_library( "GNCSingleThreadExecutorTest.mm", "GNCUtilsTest.mm", ], - features = ["-layering_check"], deps = [ "//internal/platform:base", "//internal/platform/implementation:comm", @@ -40,6 +40,7 @@ objc_library( "//internal/platform/implementation:types", "//internal/platform/implementation/apple", "//internal/platform/implementation/apple/Mediums", + "//third_party/apple_frameworks:CoreBluetooth", "//third_party/apple_frameworks:Foundation", "//third_party/apple_frameworks:XCTest", "@com_google_absl//absl/time", diff --git a/internal/platform/implementation/apple/Tests/GNCBLEGATTCharacteristicTest.mm b/internal/platform/implementation/apple/Tests/GNCBLEGATTCharacteristicTest.mm new file mode 100644 index 00000000..c7b99b88 --- /dev/null +++ b/internal/platform/implementation/apple/Tests/GNCBLEGATTCharacteristicTest.mm @@ -0,0 +1,53 @@ +// 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. + +#import +#import "XCTest/XCTest.h" +#import + +#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h" + +@interface GNCBLEGATTCharacteristicTest : XCTestCase +@end + +@implementation GNCBLEGATTCharacteristicTest + +- (void)testConvenienceInit { + CBUUID *characteristicUUID = [CBUUID UUIDWithString:@"00000000-0000-3000-8000-000000000000"]; + CBUUID *serviceUUID = [CBUUID UUIDWithString:@"0000FEF3-0000-1000-8000-00805F9B34FB"]; + GNCBLEGATTCharacteristic *characteristic = + [[GNCBLEGATTCharacteristic alloc] initWithUUID:characteristicUUID serviceUUID:serviceUUID]; + XCTAssertEqualObjects(characteristic.characteristicUUID, characteristicUUID); + XCTAssertEqualObjects(characteristic.serviceUUID, serviceUUID); + XCTAssertEqual(characteristic.permissions, 0); + XCTAssertEqual(characteristic.properties, 0); +} + +- (void)testInit { + CBUUID *characteristicUUID = [CBUUID UUIDWithString:@"00000000-0000-3000-8000-000000000000"]; + CBUUID *serviceUUID = [CBUUID UUIDWithString:@"0000FEF3-0000-1000-8000-00805F9B34FB"]; + CBAttributePermissions permissions = CBAttributePermissionsReadable; + CBCharacteristicProperties properties = CBCharacteristicPropertyRead; + GNCBLEGATTCharacteristic *characteristic = + [[GNCBLEGATTCharacteristic alloc] initWithUUID:characteristicUUID + serviceUUID:serviceUUID + permissions:permissions + properties:properties]; + XCTAssertEqualObjects(characteristic.characteristicUUID, characteristicUUID); + XCTAssertEqualObjects(characteristic.serviceUUID, serviceUUID); + XCTAssertEqual(characteristic.permissions, permissions); + XCTAssertEqual(characteristic.properties, properties); +} + +@end