Skip to content

Commit

Permalink
Allow serial number to be both an unsigned int and a string (#1043)
Browse files Browse the repository at this point in the history
* [SetupPayload] Allow serial number to be string/int

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
shana-apple and restyled-commits authored Jun 23, 2020
1 parent a5d370e commit 6a19765
Show file tree
Hide file tree
Showing 17 changed files with 686 additions and 254 deletions.
13 changes: 2 additions & 11 deletions examples/wifi-echo/server/esp32/main/QRCodeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,11 @@ string createSetupPayload()
payload.version = 1;
payload.vendorID = EXAMPLE_VENDOR_ID;
payload.productID = 1;

OptionalQRCodeInfo ssidInfo;
ssidInfo.tag = EXAMPLE_VENDOR_TAG_SSID;
ssidInfo.type = optionalQRCodeInfoTypeString;
ssidInfo.data = ap_ssid;
payload.addVendorOptionalData(ssidInfo);
payload.addVendorOptionalData(EXAMPLE_VENDOR_TAG_SSID, ap_ssid);

char gw_ip[INET6_ADDRSTRLEN];
GetGatewayIP(gw_ip, sizeof(gw_ip));
OptionalQRCodeInfo ipInfo;
ipInfo.tag = EXAMPLE_VENDOR_TAG_IP;
ipInfo.type = optionalQRCodeInfoTypeString;
ipInfo.data = gw_ip;
payload.addVendorOptionalData(ipInfo);
payload.addVendorOptionalData(EXAMPLE_VENDOR_TAG_IP, gw_ip);

QRCodeSetupPayloadGenerator generator(payload);
string result;
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/CHIPTool/CHIPTool/QRCode/QRCodeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ - (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString

NSLog(@"Payload vendorID %@", payload.vendorID);
if ([payload.vendorID isEqualToNumber:[NSNumber numberWithInt:EXAMPLE_VENDOR_ID]]) {
NSArray * optionalInfo = [payload getAllOptionalData:nil];
NSArray * optionalInfo = [payload getAllOptionalVendorData:nil];
NSLog(@"Count of payload info %@", @(optionalInfo.count));
for (CHIPOptionalQRCodeInfo * info in optionalInfo) {
NSNumber * tag = info.tag;
Expand Down Expand Up @@ -282,7 +282,7 @@ - (void)scannedQRCode:(NSString *)qrCode
dispatch_async(dispatch_get_main_queue(), ^{
[self->_captureSession stopRunning];
});
CHIPQRCodeSetupPayloadParser * parser = [[CHIPQRCodeSetupPayloadParser alloc] initWithBase45Representation:qrCode];
CHIPQRCodeSetupPayloadParser * parser = [[CHIPQRCodeSetupPayloadParser alloc] initWithBase41Representation:qrCode];
NSError * error;
CHIPSetupPayload * payload = [parser populatePayload:&error];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B20252952459E34F00F97062"
BuildableName = "CHIPTests.xctest"
BlueprintName = "CHIPTests"
ReferencedContainer = "container:CHIP.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B20252952459E34F00F97062"
BuildableName = "CHIPTests.xctest"
BlueprintName = "CHIPTests"
ReferencedContainer = "container:CHIP.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/CHIPQRCodeSetupPayloadParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
NS_ASSUME_NONNULL_BEGIN

@interface CHIPQRCodeSetupPayloadParser : NSObject
- (id)initWithBase45Representation:(NSString *)base45Representation;
- (id)initWithBase41Representation:(NSString *)base41Representation;
- (CHIPSetupPayload *)populatePayload:(NSError * __autoreleasing *)error;
@end
;
Expand Down
8 changes: 4 additions & 4 deletions src/darwin/Framework/CHIP/CHIPQRCodeSetupPayloadParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
#import <setup_payload/SetupPayload.h>

@implementation CHIPQRCodeSetupPayloadParser {
NSString * _base45Representation;
NSString * _base41Representation;
chip::QRCodeSetupPayloadParser * _chipQRCodeSetupPayloadParser;
}

- (id)initWithBase45Representation:(NSString *)base45Representation
- (id)initWithBase41Representation:(NSString *)base41Representation
{
if (self = [super init]) {
_base45Representation = base45Representation;
_chipQRCodeSetupPayloadParser = new chip::QRCodeSetupPayloadParser(std::string([base45Representation UTF8String]));
_base41Representation = base41Representation;
_chipQRCodeSetupPayloadParser = new chip::QRCodeSetupPayloadParser(std::string([base41Representation UTF8String]));
}
return self;
}
Expand Down
8 changes: 6 additions & 2 deletions src/darwin/Framework/CHIP/CHIPSetupPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, OptionalQRCodeInfoType) { kOptionalQRCodeInfoTypeString, kOptionalQRCodeInfoTypeInt };
typedef NS_ENUM(NSUInteger, OptionalQRCodeInfoType) {
kOptionalQRCodeInfoTypeUnknown,
kOptionalQRCodeInfoTypeString,
kOptionalQRCodeInfoTypeInt32
};

@interface CHIPOptionalQRCodeInfo : NSObject
@property (nonatomic, strong) NSNumber * infoType;
Expand All @@ -44,7 +48,7 @@ typedef NS_ENUM(NSUInteger, OptionalQRCodeInfoType) { kOptionalQRCodeInfoTypeStr
@property (nonatomic, strong) NSNumber * setUpPINCode;

@property (nonatomic, strong) NSString * serialNumber;
- (NSArray<CHIPOptionalQRCodeInfo *> *)getAllOptionalData:(NSError * __autoreleasing *)error;
- (NSArray<CHIPOptionalQRCodeInfo *> *)getAllOptionalVendorData:(NSError * __autoreleasing *)error;

#ifdef __cplusplus
- (id)initWithSetupPayload:(chip::SetupPayload)setupPayload;
Expand Down
22 changes: 16 additions & 6 deletions src/darwin/Framework/CHIP/CHIPSetupPayload.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,25 @@ - (id)initWithSetupPayload:(chip::SetupPayload)setupPayload
_rendezvousInformation = [NSNumber numberWithUnsignedShort:setupPayload.rendezvousInformation];
_discriminator = [NSNumber numberWithUnsignedShort:setupPayload.discriminator];
_setUpPINCode = [NSNumber numberWithUnsignedLong:setupPayload.setUpPINCode];
_serialNumber = [NSString stringWithUTF8String:setupPayload.serialNumber.c_str()];

[self getSerialNumber:setupPayload];
}
return self;
}

- (NSArray<CHIPOptionalQRCodeInfo *> *)getAllOptionalData:(NSError * __autoreleasing *)error
- (void)getSerialNumber:(chip::SetupPayload)setupPayload
{
std::string serialNumberC;
CHIP_ERROR err = setupPayload.getSerialNumber(serialNumberC);
if (err == CHIP_NO_ERROR) {
_serialNumber = [NSString stringWithUTF8String:serialNumberC.c_str()];
}
}

- (NSArray<CHIPOptionalQRCodeInfo *> *)getAllOptionalVendorData:(NSError * __autoreleasing *)error
{
NSMutableArray<CHIPOptionalQRCodeInfo *> * allOptionalData = [NSMutableArray new];
vector<chip::OptionalQRCodeInfo> chipOptionalData = _chipSetupPayload.getAllOptionalData();
vector<chip::OptionalQRCodeInfo> chipOptionalData = _chipSetupPayload.getAllOptionalVendorData();
for (chip::OptionalQRCodeInfo chipInfo : chipOptionalData) {
CHIPOptionalQRCodeInfo * info = [CHIPOptionalQRCodeInfo new];
info.tag = [NSNumber numberWithUnsignedChar:chipInfo.tag];
Expand All @@ -53,9 +63,9 @@ - (id)initWithSetupPayload:(chip::SetupPayload)setupPayload
info.infoType = [NSNumber numberWithInt:kOptionalQRCodeInfoTypeString];
info.stringValue = [NSString stringWithUTF8String:chipInfo.data.c_str()];
break;
case chip::optionalQRCodeInfoTypeInt:
info.infoType = [NSNumber numberWithInt:kOptionalQRCodeInfoTypeInt];
info.integerValue = [NSNumber numberWithInt:chipInfo.integer];
case chip::optionalQRCodeInfoTypeInt32:
info.infoType = [NSNumber numberWithInt:kOptionalQRCodeInfoTypeInt32];
info.integerValue = [NSNumber numberWithInt:chipInfo.int32];
break;
default:
if (error) {
Expand Down
48 changes: 41 additions & 7 deletions src/darwin/Framework/CHIPTests/CHIPSetupPayloadParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ - (void)testManualParser
{
NSError * error;
CHIPManualSetupPayloadParser * parser =
[[CHIPManualSetupPayloadParser alloc] initWithDecimalStringRepresentation:@"34896656190000100001"];
[[CHIPManualSetupPayloadParser alloc] initWithDecimalStringRepresentation:@"000003949100001000011"];
CHIPSetupPayload * payload = [parser populatePayload:&error];

XCTAssertNotNil(payload);
XCTAssertNil(error);

XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 13);
XCTAssertEqual(payload.setUpPINCode.unsignedIntegerValue, 2345);
XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 1);
XCTAssertEqual(payload.setUpPINCode.unsignedIntegerValue, 1234);
XCTAssertEqual(payload.vendorID.unsignedIntegerValue, 1);
XCTAssertEqual(payload.productID.unsignedIntegerValue, 1);
XCTAssertTrue(payload.requiresCustomFlow);
Expand All @@ -65,18 +65,19 @@ - (void)testManualParser_Error
- (void)testQRCodeParser_Error
{
NSError * error;
CHIPManualSetupPayloadParser * parser =
[[CHIPManualSetupPayloadParser alloc] initWithDecimalStringRepresentation:@"B20800G.0G8G000"];
CHIPQRCodeSetupPayloadParser * parser =
[[CHIPQRCodeSetupPayloadParser alloc] initWithBase41Representation:@"CH:B20800G.0G8G000"];
CHIPSetupPayload * payload = [parser populatePayload:&error];

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

- (void)testQRCodeParser
{
NSError * error;
CHIPQRCodeSetupPayloadParser * parser = [[CHIPQRCodeSetupPayloadParser alloc] initWithBase45Representation:@"B20800G00G8G000"];
CHIPQRCodeSetupPayloadParser * parser =
[[CHIPQRCodeSetupPayloadParser alloc] initWithBase41Representation:@"CH:J20800G008008000"];
CHIPSetupPayload * payload = [parser populatePayload:&error];

XCTAssertNotNil(payload);
Expand All @@ -91,4 +92,37 @@ - (void)testQRCodeParser
XCTAssertEqual(payload.rendezvousInformation.unsignedIntegerValue, 1);
}

- (void)testQRCodeParserWithOptionalData
{
NSError * error;
CHIPQRCodeSetupPayloadParser * parser =
[[CHIPQRCodeSetupPayloadParser alloc] initWithBase41Representation:@"CH:J20800G008008006DL200UOGMHARTHOMJ300IDL530.I7"];
CHIPSetupPayload * payload = [parser populatePayload:&error];

XCTAssertNotNil(payload);
XCTAssertNil(error);

XCTAssertEqual(payload.version.unsignedIntegerValue, 5);
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.rendezvousInformation.unsignedIntegerValue, 1);
XCTAssertTrue([payload.serialNumber isEqualToString:@"1"]);

NSArray<CHIPOptionalQRCodeInfo *> * vendorOptionalInfo = [payload getAllOptionalVendorData:&error];
XCTAssertNil(error);
XCTAssertEqual([vendorOptionalInfo count], 2);
for (CHIPOptionalQRCodeInfo * info in vendorOptionalInfo) {
if (info.tag.intValue == 2) {
XCTAssertEqual(info.infoType.intValue, kOptionalQRCodeInfoTypeString);
XCTAssertTrue([info.stringValue isEqualToString:@"myData"]);
} else if (info.tag.intValue == 3) {
XCTAssertEqual(info.infoType.intValue, kOptionalQRCodeInfoTypeInt32);
XCTAssertEqual(info.integerValue.intValue, 12);
}
}
}

@end
70 changes: 51 additions & 19 deletions src/setup_payload/QRCodeSetupPayloadGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,48 +82,68 @@ static CHIP_ERROR populateTLVBits(uint8_t * bits, int & offset, uint8_t * tlvBuf
return err;
}

static void addCHIPInfoToOptionalData(SetupPayload & outPayload)
CHIP_ERROR writeTag(TLVWriter & writer, uint64_t tag, OptionalQRCodeInfo & info)
{
if (outPayload.serialNumber.length() > 0)
CHIP_ERROR err = CHIP_NO_ERROR;

if (info.type == optionalQRCodeInfoTypeString)
{
OptionalQRCodeInfo info;
info.type = optionalQRCodeInfoTypeString;
info.tag = kSerialNumberTag;
info.data = outPayload.serialNumber;
outPayload.addCHIPOptionalData(info);
err = writer.PutString(tag, info.data.c_str());
}
else if (info.type == optionalQRCodeInfoTypeInt32)
{
err = writer.Put(tag, info.int32);
}
else
{
err = CHIP_ERROR_INVALID_ARGUMENT;
}

return err;
}

CHIP_ERROR writeTag(TLVWriter & writer, uint64_t tag, OptionalQRCodeInfo & info)
CHIP_ERROR writeTag(TLVWriter & writer, uint64_t tag, OptionalQRCodeInfoExtension & info)
{
CHIP_ERROR err = CHIP_NO_ERROR;

if (info.type == optionalQRCodeInfoTypeString)
if (info.type == optionalQRCodeInfoTypeString || info.type == optionalQRCodeInfoTypeInt32)
{
err = writer.PutString(tag, info.data.c_str());
err = writeTag(writer, tag, static_cast<OptionalQRCodeInfo &>(info));
}
else if (info.type == optionalQRCodeInfoTypeInt64)
{
err = writer.Put(tag, info.int64);
}
else if (info.type == optionalQRCodeInfoTypeInt)
else if (info.type == optionalQRCodeInfoTypeUInt32)
{
err = writer.Put(tag, static_cast<int64_t>(info.integer));
err = writer.Put(tag, info.uint32);
}
else if (info.type == optionalQRCodeInfoTypeUInt64)
{
err = writer.Put(tag, info.uint64);
}
else
{
err = CHIP_ERROR_INVALID_ARGUMENT;
}

return err;
}

CHIP_ERROR generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvDataStart, uint32_t maxLen,
uint32_t & tlvDataLengthInBytes)
CHIP_ERROR QRCodeSetupPayloadGenerator::generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvDataStart,
uint32_t maxLen, uint32_t & tlvDataLengthInBytes)
{
CHIP_ERROR err = CHIP_NO_ERROR;
addCHIPInfoToOptionalData(outPayload);
vector<OptionalQRCodeInfo> optionalData = outPayload.getAllOptionalData();
VerifyOrExit(optionalData.size() != 0, err = CHIP_NO_ERROR);
CHIP_ERROR err = CHIP_NO_ERROR;
vector<OptionalQRCodeInfo> optionalData = outPayload.getAllOptionalVendorData();
vector<OptionalQRCodeInfoExtension> optionalExtensionData = outPayload.getAllOptionalExtensionData();
VerifyOrExit(optionalData.size() != 0 || optionalExtensionData.size() != 0, err = CHIP_NO_ERROR);

TLVWriter rootWriter;
rootWriter.Init(tlvDataStart, maxLen);
rootWriter.ImplicitProfileId = chip::Profiles::kChipProfile_ServiceProvisioning;

// The cost (in bytes) of the top-level container is amortized as soon as there is at least 4 optionals elements.
if (optionalData.size() >= 4)
if ((optionalData.size() + optionalExtensionData.size()) >= 4)
{

TLVWriter innerStructureWriter;
Expand All @@ -138,6 +158,12 @@ CHIP_ERROR generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvD
SuccessOrExit(err);
}

for (OptionalQRCodeInfoExtension info : optionalExtensionData)
{
err = writeTag(innerStructureWriter, ContextTag(info.tag), info);
SuccessOrExit(err);
}

err = rootWriter.CloseContainer(innerStructureWriter);
SuccessOrExit(err);
}
Expand All @@ -148,6 +174,12 @@ CHIP_ERROR generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvD
err = writeTag(rootWriter, ProfileTag(rootWriter.ImplicitProfileId, info.tag), info);
SuccessOrExit(err);
}

for (OptionalQRCodeInfoExtension info : optionalExtensionData)
{
err = writeTag(rootWriter, ProfileTag(rootWriter.ImplicitProfileId, info.tag), info);
SuccessOrExit(err);
}
}
err = rootWriter.Finalize();
SuccessOrExit(err);
Expand Down
4 changes: 4 additions & 0 deletions src/setup_payload/QRCodeSetupPayloadGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class QRCodeSetupPayloadGenerator
* producing the requested string.
*/
CHIP_ERROR payloadBase41Representation(string & base41Representation, uint8_t * tlvDataStart, size_t tlvDataStartSize);

private:
CHIP_ERROR generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvDataStart, uint32_t maxLen,
uint32_t & tlvDataLengthInBytes);
};

}; // namespace chip
Expand Down
Loading

0 comments on commit 6a19765

Please sign in to comment.