Skip to content

Commit

Permalink
Properly round-trip serialNumber in MTRSetupPayload. (#26693)
Browse files Browse the repository at this point in the history
Without this when we convert to QRCode we are dropping the serialNumber.

Fixes #26684
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Jun 15, 2023
1 parent f3bf4de commit 1646484
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/darwin/Framework/CHIP/MTRSetupPayload.mm
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,18 @@ - (NSString * _Nullable)qrCodeString:(NSError * __autoreleasing *)error
payload.rendezvousInformation = [MTRSetupPayload convertDiscoveryCapabilities:self.discoveryCapabilities];
payload.discriminator.SetLongValue([self.discriminator unsignedShortValue]);
payload.setUpPINCode = [self.setupPasscode unsignedIntValue];
if (self.serialNumber != nil) {
CHIP_ERROR err = payload.addSerialNumber(self.serialNumber.UTF8String);
if (err != CHIP_NO_ERROR) {
if (error != nil) {
*error = [MTRError errorForCHIPErrorCode:err];
}
return nil;
}
}

std::string outDecimalString;
CHIP_ERROR err = chip::QRCodeSetupPayloadGenerator(payload).payloadBase38Representation(outDecimalString);
std::string outQRCodeString;
CHIP_ERROR err = chip::QRCodeSetupPayloadGenerator(payload).payloadBase38RepresentationWithAutoTLVBuffer(outQRCodeString);

if (err != CHIP_NO_ERROR) {
if (error != nil) {
Expand All @@ -366,7 +375,7 @@ - (NSString * _Nullable)qrCodeString:(NSError * __autoreleasing *)error
return nil;
}

return [NSString stringWithUTF8String:outDecimalString.c_str()];
return [NSString stringWithUTF8String:outQRCodeString.c_str()];
}

+ (nullable NSNumber *)_boxDiscoveryCapabilities:(MTRDiscoveryCapabilities)discoveryCapabilities
Expand Down
25 changes: 25 additions & 0 deletions src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,29 @@ - (void)testQRCodePropertyAliases
XCTAssertEqualObjects(payload.setUpPINCode, @(2));
}

- (void)testSerialNumberRoundTrip
{
NSError * error;
MTRSetupPayload * payload =
[MTRSetupPayload setupPayloadWithOnboardingPayload:@"MT:M5L90MP500K64J0A33P0SET70.QT52B.E23-WZE0WISA0DK5N1K8SQ1RYCU1O0"
error:&error];
XCTAssertNil(error);
XCTAssertNotNil(payload);

XCTAssertEqualObjects(payload.serialNumber, @"123456789");

NSString * serialNumber = @"12345";
payload.serialNumber = serialNumber;

NSString * qrCode = [payload qrCodeString:&error];
XCTAssertNil(error);
XCTAssertNotNil(qrCode);

MTRSetupPayload * newPayload = [MTRSetupPayload setupPayloadWithOnboardingPayload:qrCode error:&error];
XCTAssertNil(error);
XCTAssertNotNil(newPayload);

XCTAssertEqualObjects(newPayload.serialNumber, serialNumber);
}

@end

0 comments on commit 1646484

Please sign in to comment.