Skip to content

Commit

Permalink
[Darwin] Add NSSecureCoding conformance for setup payload (#20333)
Browse files Browse the repository at this point in the history
  • Loading branch information
carol-apple authored and pull[bot] committed Aug 24, 2023
1 parent 8dfdb1a commit 2630722
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/darwin/Framework/CHIP/MTRSetupPayload.mm
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,57 @@ - (void)getSerialNumber:(chip::SetupPayload)setupPayload
}
return allOptionalData;
}

#pragma mark - NSSecureCoding

static NSString * const MTRSetupPayloadCodingKeyVersion = @"MTRSP.ck.version";
static NSString * const MTRSetupPayloadCodingKeyVendorID = @"MTRSP.ck.vendorID";
static NSString * const MTRSetupPayloadCodingKeyProductID = @"MTRSP.ck.productID";
static NSString * const MTRSetupPayloadCodingKeyCommissioningFlow = @"MTRSP.ck.commissioningFlow";
static NSString * const MTRSetupPayloadCodingKeyRendezvousFlags = @"MTRSP.ck.rendezvousFlags";
static NSString * const MTRSetupPayloadCodingKeyDiscriminator = @"MTRSP.ck.discriminator";
static NSString * const MTRSetupPayloadCodingKeySetupPINCode = @"MTRSP.ck.setupPINCode";
static NSString * const MTRSetupPayloadCodingKeySerialNumber = @"MTRSP.ck.serialNumber";

+ (BOOL)supportsSecureCoding
{
return YES;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.version forKey:MTRSetupPayloadCodingKeyVersion];
[coder encodeObject:self.vendorID forKey:MTRSetupPayloadCodingKeyVendorID];
[coder encodeObject:self.productID forKey:MTRSetupPayloadCodingKeyProductID];
[coder encodeInteger:self.commissioningFlow forKey:MTRSetupPayloadCodingKeyCommissioningFlow];
[coder encodeInteger:self.rendezvousInformation forKey:MTRSetupPayloadCodingKeyRendezvousFlags];
[coder encodeObject:self.discriminator forKey:MTRSetupPayloadCodingKeyDiscriminator];
[coder encodeObject:self.setUpPINCode forKey:MTRSetupPayloadCodingKeySetupPINCode];
[coder encodeObject:self.serialNumber forKey:MTRSetupPayloadCodingKeySerialNumber];
}

- (nullable instancetype)initWithCoder:(NSCoder *)decoder
{
NSNumber * version = [decoder decodeObjectOfClass:[NSNumber class] forKey:MTRSetupPayloadCodingKeyVersion];
NSNumber * vendorID = [decoder decodeObjectOfClass:[NSNumber class] forKey:MTRSetupPayloadCodingKeyVendorID];
NSNumber * productID = [decoder decodeObjectOfClass:[NSNumber class] forKey:MTRSetupPayloadCodingKeyProductID];
NSUInteger commissioningFlow = [decoder decodeIntegerForKey:MTRSetupPayloadCodingKeyCommissioningFlow];
NSUInteger rendezvousInformation = [decoder decodeIntegerForKey:MTRSetupPayloadCodingKeyRendezvousFlags];
NSNumber * discriminator = [decoder decodeObjectOfClass:[NSNumber class] forKey:MTRSetupPayloadCodingKeyDiscriminator];
NSNumber * setUpPINCode = [decoder decodeObjectOfClass:[NSNumber class] forKey:MTRSetupPayloadCodingKeySetupPINCode];
NSString * serialNumber = [decoder decodeObjectOfClass:[NSString class] forKey:MTRSetupPayloadCodingKeySerialNumber];

MTRSetupPayload * payload = [[MTRSetupPayload alloc] init];
payload.version = version;
payload.vendorID = vendorID;
payload.productID = productID;
payload.commissioningFlow = (MTRCommissioningFlow) commissioningFlow;
payload.rendezvousInformation = (MTRRendezvousInformationFlags) rendezvousInformation;
payload.discriminator = discriminator;
payload.setUpPINCode = setUpPINCode;
payload.serialNumber = serialNumber;

return payload;
}

@end
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRSetupPayload_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#import <setup_payload/SetupPayload.h>
#endif

@interface MTRSetupPayload ()
@interface MTRSetupPayload () <NSSecureCoding>

#ifdef __cplusplus
- (id)initWithSetupPayload:(chip::SetupPayload)setupPayload;
Expand Down

0 comments on commit 2630722

Please sign in to comment.