Add Objective-C GATT characteristic container.

PiperOrigin-RevId: 547954006
This commit is contained in:
Nick Bourdakos
2023-07-13 16:05:18 -07:00
committed by Copybara-Service
parent 48184e6b4d
commit e0b2e176c8
5 changed files with 167 additions and 2 deletions
@@ -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 <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>
/** 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
@@ -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 <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>
@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
@@ -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",
@@ -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",
@@ -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 <CoreBluetooth/CoreBluetooth.h>
#import "XCTest/XCTest.h"
#import <XCTest/XCTest.h>
#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