Skip to content

Commit

Permalink
Add Pairing API in CHIPFramework that takes in an onboarding payload … (
Browse files Browse the repository at this point in the history
#6358)

* Add Pairing API in CHIPFramework that takes in an onboarding payload string

* Restyled by whitespace

* Restyled by clang-format

* Fixing headers

* Restyled by clang-format

* Correct ordering of imports

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Justin Wood <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Jul 9, 2021
1 parent 2ee37c5 commit 2242a7f
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
991DC0892475F47D00C13860 /* CHIPDeviceController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 991DC0872475F47D00C13860 /* CHIPDeviceController.mm */; };
991DC08B247704DC00C13860 /* CHIPLogging.h in Headers */ = {isa = PBXBuildFile; fileRef = 991DC08A247704DC00C13860 /* CHIPLogging.h */; };
B20252972459E34F00F97062 /* CHIP.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B202528D2459E34F00F97062 /* CHIP.framework */; };
B289D4212639C0D300D4E314 /* CHIPOnboardingPayloadParser.h in Headers */ = {isa = PBXBuildFile; fileRef = B289D41F2639C0D300D4E314 /* CHIPOnboardingPayloadParser.h */; settings = {ATTRIBUTES = (Public, ); }; };
B289D4222639C0D300D4E314 /* CHIPOnboardingPayloadParser.m in Sources */ = {isa = PBXBuildFile; fileRef = B289D4202639C0D300D4E314 /* CHIPOnboardingPayloadParser.m */; };
B2E0D7B1245B0B5C003C5B48 /* CHIP.h in Headers */ = {isa = PBXBuildFile; fileRef = B2E0D7A8245B0B5C003C5B48 /* CHIP.h */; settings = {ATTRIBUTES = (Public, ); }; };
B2E0D7B2245B0B5C003C5B48 /* CHIPManualSetupPayloadParser.h in Headers */ = {isa = PBXBuildFile; fileRef = B2E0D7A9245B0B5C003C5B48 /* CHIPManualSetupPayloadParser.h */; settings = {ATTRIBUTES = (Public, ); }; };
B2E0D7B3245B0B5C003C5B48 /* CHIPError.mm in Sources */ = {isa = PBXBuildFile; fileRef = B2E0D7AA245B0B5C003C5B48 /* CHIPError.mm */; };
Expand Down Expand Up @@ -112,6 +114,8 @@
B20252912459E34F00F97062 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B20252962459E34F00F97062 /* CHIPTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CHIPTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
B202529D2459E34F00F97062 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B289D41F2639C0D300D4E314 /* CHIPOnboardingPayloadParser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CHIPOnboardingPayloadParser.h; sourceTree = "<group>"; };
B289D4202639C0D300D4E314 /* CHIPOnboardingPayloadParser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CHIPOnboardingPayloadParser.m; sourceTree = "<group>"; };
B2E0D7A8245B0B5C003C5B48 /* CHIP.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIP.h; sourceTree = "<group>"; };
B2E0D7A9245B0B5C003C5B48 /* CHIPManualSetupPayloadParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPManualSetupPayloadParser.h; sourceTree = "<group>"; };
B2E0D7AA245B0B5C003C5B48 /* CHIPError.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPError.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -224,6 +228,8 @@
B2E0D7AB245B0B5C003C5B48 /* CHIPError.h */,
B2E0D7AA245B0B5C003C5B48 /* CHIPError.mm */,
991DC08A247704DC00C13860 /* CHIPLogging.h */,
B289D41F2639C0D300D4E314 /* CHIPOnboardingPayloadParser.h */,
B289D4202639C0D300D4E314 /* CHIPOnboardingPayloadParser.m */,
B2E0D7A9245B0B5C003C5B48 /* CHIPManualSetupPayloadParser.h */,
B2E0D7AD245B0B5C003C5B48 /* CHIPManualSetupPayloadParser.mm */,
B2E0D7AC245B0B5C003C5B48 /* CHIPQRCodeSetupPayloadParser.h */,
Expand Down Expand Up @@ -264,6 +270,7 @@
buildActionMask = 2147483647;
files = (
2CB7163B252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.h in Headers */,
B289D4212639C0D300D4E314 /* CHIPOnboardingPayloadParser.h in Headers */,
2CB7163F252F731E0026E2BB /* CHIPDevicePairingDelegate.h in Headers */,
2C222AD0255C620600E446B9 /* CHIPDevice.h in Headers */,
991DC0842475F45400C13860 /* CHIPDeviceController.h in Headers */,
Expand Down Expand Up @@ -422,6 +429,7 @@
1EC4CE5D25CC26E900D7304F /* CHIPClustersObjc.mm in Sources */,
1E9BD1C72621AFF100FC3246 /* attribute-size.cpp in Sources */,
B2E0D7B3245B0B5C003C5B48 /* CHIPError.mm in Sources */,
B289D4222639C0D300D4E314 /* CHIPOnboardingPayloadParser.m in Sources */,
1EC4CE5E25CC26E900D7304F /* call-command-handler.cpp in Sources */,
1EC4CE3B25CC263E00D7304F /* reporting-default-configuration.cpp in Sources */,
1EC4CE4F25CC267700D7304F /* process-cluster-message.cpp in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#import <Foundation/Foundation.h>

#import <CHIP/CHIPOnboardingPayloadParser.h>

@class CHIPDevice;

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -35,16 +37,24 @@ NS_ASSUME_NONNULL_BEGIN
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error;

- (BOOL)pairDevice:(uint64_t)deviceID
address:(NSString *)address
port:(uint16_t)port
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error;

- (BOOL)pairDeviceWithoutSecurity:(uint64_t)deviceID
address:(NSString *)address
port:(uint16_t)port
error:(NSError * __autoreleasing *)error;

- (BOOL)pairDevice:(uint64_t)deviceID
onboardingPayload:(NSString *)onboardingPayload
onboardingPayloadType:(CHIPOnboardingPayloadType)onboardingPayloadType
error:(NSError * __autoreleasing *)error;

- (void)setListenPort:(uint16_t)port;
- (BOOL)unpairDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;
- (BOOL)stopDevicePairing:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;
Expand Down
20 changes: 20 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "CHIPError.h"
#import "CHIPLogging.h"
#import "CHIPPersistentStorageDelegateBridge.h"
#import "CHIPSetupPayload.h"
#import "gen/CHIPClustersObjc.h"

#include <platform/CHIPDeviceBuildConfig.h>
Expand Down Expand Up @@ -264,6 +265,25 @@ - (BOOL)pairDeviceWithoutSecurity:(uint64_t)deviceID
return success;
}

- (BOOL)pairDevice:(uint64_t)deviceID
onboardingPayload:(NSString *)onboardingPayload
onboardingPayloadType:(CHIPOnboardingPayloadType)onboardingPayloadType
error:(NSError * __autoreleasing *)error
{
BOOL didSucceed = NO;
CHIPSetupPayload * setupPayload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:onboardingPayload
ofType:onboardingPayloadType
error:error];
if (setupPayload) {
uint16_t discriminator = setupPayload.discriminator.unsignedShortValue;
uint32_t setupPINCode = setupPayload.setUpPINCode.unsignedIntValue;
didSucceed = [self pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode error:error];
} else {
CHIP_LOG_ERROR("Failed to create CHIPSetupPayload for pairing with error %@", *error);
}
return didSucceed;
}

- (BOOL)unpairDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error
{
__block BOOL success;
Expand Down
39 changes: 39 additions & 0 deletions src/darwin/Framework/CHIP/CHIPOnboardingPayloadParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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
*
* http://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 <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class CHIPSetupPayload;

typedef NS_ENUM(NSUInteger, CHIPOnboardingPayloadType) {
CHIPOnboardingPayloadTypeQRCode = 0,
CHIPOnboardingPayloadTypeManualCode,
CHIPOnboardingPayloadTypeNFC,
CHIPOnboardingPayloadTypeAdmin,
};

@interface CHIPOnboardingPayloadParser : NSObject

+ (nullable CHIPSetupPayload *)setupPayloadForOnboardingPayload:(NSString *)onboardingPayload
ofType:(CHIPOnboardingPayloadType)type
error:(NSError * __autoreleasing *)error;

@end

NS_ASSUME_NONNULL_END
60 changes: 60 additions & 0 deletions src/darwin/Framework/CHIP/CHIPOnboardingPayloadParser.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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
*
* http://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 "CHIPOnboardingPayloadParser.h"
#import "CHIPManualSetupPayloadParser.h"
#import "CHIPQRCodeSetupPayloadParser.h"
#import "CHIPSetupPayload.h"

@implementation CHIPOnboardingPayloadParser

+ (nullable CHIPSetupPayload *)setupPayloadForOnboardingPayload:(NSString *)onboardingPayload
ofType:(CHIPOnboardingPayloadType)type
error:(NSError * __autoreleasing *)error
{
CHIPSetupPayload * payload;
switch (type) {
case CHIPOnboardingPayloadTypeManualCode:
case CHIPOnboardingPayloadTypeAdmin:
payload = [self setupPayloadForManualCodeOnboardingPayload:onboardingPayload error:error];
break;
case CHIPOnboardingPayloadTypeQRCode:
case CHIPOnboardingPayloadTypeNFC:
payload = [self setupPayloadForQRCodeOnboardingPayload:onboardingPayload error:error];
break;
default:
break;
}
return payload;
}

+ (nullable CHIPSetupPayload *)setupPayloadForQRCodeOnboardingPayload:(NSString *)onboardingPayload
error:(NSError * __autoreleasing *)error
{
CHIPQRCodeSetupPayloadParser * qrCodeParser =
[[CHIPQRCodeSetupPayloadParser alloc] initWithBase38Representation:onboardingPayload];
return [qrCodeParser populatePayload:error];
}

+ (nullable CHIPSetupPayload *)setupPayloadForManualCodeOnboardingPayload:(NSString *)onboardingPayload
error:(NSError * __autoreleasing *)error
{
CHIPManualSetupPayloadParser * manualParser =
[[CHIPManualSetupPayloadParser alloc] initWithDecimalStringRepresentation:onboardingPayload];
return [manualParser populatePayload:error];
}
@end
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/CHIPSetupPayload.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ - (id)initWithSetupPayload:(chip::SetupPayload)setupPayload
_requiresCustomFlow = setupPayload.requiresCustomFlow == 1;
_rendezvousInformation = [self valueOf:setupPayload.rendezvousInformation];
_discriminator = [NSNumber numberWithUnsignedShort:setupPayload.discriminator];
_setUpPINCode = [NSNumber numberWithUnsignedLong:setupPayload.setUpPINCode];
_setUpPINCode = [NSNumber numberWithUnsignedInt:setupPayload.setUpPINCode];

[self getSerialNumber:setupPayload];
}
Expand Down
123 changes: 123 additions & 0 deletions src/darwin/Framework/CHIPTests/CHIPSetupPayloadParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
// module headers
#import "CHIPManualSetupPayloadParser.h"
#import "CHIPOnboardingPayloadParser.h"
#import "CHIPQRCodeSetupPayloadParser.h"
#import "CHIPSetupPayload.h"

Expand All @@ -34,6 +35,128 @@ @interface CHIPSetupPayloadParserTests : XCTestCase

@implementation CHIPSetupPayloadParserTests

- (void)testOnboardingPayloadParser_Manual_NoError
{
NSError * error;
CHIPSetupPayload * payload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015"
ofType:CHIPOnboardingPayloadTypeManualCode
error:&error];

XCTAssertNotNil(payload);
XCTAssertNil(error);

XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 2560);
XCTAssertEqual(payload.setUpPINCode.unsignedIntegerValue, 123456780);
XCTAssertEqual(payload.vendorID.unsignedIntegerValue, 1);
XCTAssertEqual(payload.productID.unsignedIntegerValue, 1);
XCTAssertTrue(payload.requiresCustomFlow);
XCTAssertEqual(payload.version.unsignedIntegerValue, 0);
XCTAssertEqual(payload.rendezvousInformation, kRendezvousInformationNone);
}

- (void)testOnboardingPayloadParser_Manual_WrongType
{
NSError * error;
CHIPSetupPayload * payload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015"
ofType:CHIPOnboardingPayloadTypeQRCode
error:&error];

XCTAssertNil(payload);
XCTAssertEqual(error.code, CHIPErrorCodeInvalidArgument);
}

- (void)testOnboardingPayloadParser_Admin_NoError
{
NSError * error;
CHIPSetupPayload * payload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015"
ofType:CHIPOnboardingPayloadTypeAdmin
error:&error];

XCTAssertNotNil(payload);
XCTAssertNil(error);

XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 2560);
XCTAssertEqual(payload.setUpPINCode.unsignedIntegerValue, 123456780);
XCTAssertEqual(payload.vendorID.unsignedIntegerValue, 1);
XCTAssertEqual(payload.productID.unsignedIntegerValue, 1);
XCTAssertTrue(payload.requiresCustomFlow);
XCTAssertEqual(payload.version.unsignedIntegerValue, 0);
XCTAssertEqual(payload.rendezvousInformation, kRendezvousInformationNone);
}

- (void)testOnboardingPayloadParser_Admin_WrongType
{
NSError * error;
CHIPSetupPayload * payload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015"
ofType:CHIPOnboardingPayloadTypeQRCode
error:&error];

XCTAssertNil(payload);
XCTAssertEqual(error.code, CHIPErrorCodeInvalidArgument);
}

- (void)testOnboardingPayloadParser_QRCode_NoError
{
NSError * error;
CHIPSetupPayload * payload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"CH:R5L90UV200A3L900000"
ofType:CHIPOnboardingPayloadTypeQRCode
error:&error];

XCTAssertNotNil(payload);
XCTAssertNil(error);

XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 128);
XCTAssertEqual(payload.setUpPINCode.unsignedIntegerValue, 2048);
XCTAssertEqual(payload.vendorID.unsignedIntegerValue, 12);
XCTAssertEqual(payload.productID.unsignedIntegerValue, 1);
XCTAssertFalse(payload.requiresCustomFlow);
XCTAssertEqual(payload.version.unsignedIntegerValue, 5);
XCTAssertEqual(payload.rendezvousInformation, kRendezvousInformationSoftAP);
}

- (void)testOnboardingPayloadParser_QRCode_WrongType
{
NSError * error;
CHIPSetupPayload * payload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"CH:R5L90UV200A3L900000"
ofType:CHIPOnboardingPayloadTypeAdmin
error:&error];

XCTAssertNil(payload);
XCTAssertEqual(error.code, CHIPErrorCodeIntegrityCheckFailed);
}

- (void)testOnboardingPayloadParser_NFC_NoError
{
NSError * error;
CHIPSetupPayload * payload =
[CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"CH:R5L90UV200A3L90A33P0GQ670.QT52B.E23O6DE044U1077U.3"
ofType:CHIPOnboardingPayloadTypeNFC
error:&error];

XCTAssertNotNil(payload);
XCTAssertNil(error);

XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 128);
XCTAssertEqual(payload.setUpPINCode.unsignedIntegerValue, 2048);
XCTAssertEqual(payload.vendorID.unsignedIntegerValue, 12);
XCTAssertEqual(payload.productID.unsignedIntegerValue, 1);
XCTAssertFalse(payload.requiresCustomFlow);
XCTAssertEqual(payload.version.unsignedIntegerValue, 5);
XCTAssertEqual(payload.rendezvousInformation, kRendezvousInformationSoftAP);
}

- (void)testOnboardingPayloadParser_NFC_WrongType
{
NSError * error;
CHIPSetupPayload * payload =
[CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:@"CH:R5L90UV200A3L90A33P0GQ670.QT52B.E23O6DE044U1077U.3"
ofType:CHIPOnboardingPayloadTypeManualCode
error:&error];

XCTAssertNil(payload);
XCTAssertEqual(error.code, CHIPErrorCodeIntegrityCheckFailed);
}

- (void)testManualParser
{
NSError * error;
Expand Down

0 comments on commit 2242a7f

Please sign in to comment.