diff --git a/src/darwin/Framework/CHIP/MTRSetupPayload.h b/src/darwin/Framework/CHIP/MTRSetupPayload.h index 926e344fa06f90..e82ab104e7467d 100644 --- a/src/darwin/Framework/CHIP/MTRSetupPayload.h +++ b/src/darwin/Framework/CHIP/MTRSetupPayload.h @@ -73,6 +73,10 @@ typedef NS_ENUM(NSUInteger, MTROptionalQRCodeInfoType) { * Generate a random Matter-valid setup PIN. */ + (NSUInteger)generateRandomPIN; + +/** Get 11 digit manual entry code from the setup payload. */ +- (nullable NSString *)manualEntryCode; + @end NS_ASSUME_NONNULL_END diff --git a/src/darwin/Framework/CHIP/MTRSetupPayload.mm b/src/darwin/Framework/CHIP/MTRSetupPayload.mm index 17f1696d23770a..39c18a33d1a66c 100644 --- a/src/darwin/Framework/CHIP/MTRSetupPayload.mm +++ b/src/darwin/Framework/CHIP/MTRSetupPayload.mm @@ -18,6 +18,7 @@ #import "MTRError.h" #import "MTRError_Internal.h" #import "MTRSetupPayload_Internal.h" +#import "setup_payload/ManualSetupPayloadGenerator.h" #import @implementation MTROptionalQRCodeInfo @@ -196,4 +197,29 @@ - (nullable instancetype)initWithCoder:(NSCoder *)decoder return payload; } +- (nullable NSString *)manualEntryCode +{ + CHIP_ERROR err = CHIP_NO_ERROR; + std::string outDecimalString; + chip::SetupPayload payload; + + /// The 11 digit manual pairing code only requires the version, VID_PID present flag, + /// discriminator, and the setup pincode. + payload.version = [self.version unsignedCharValue]; + if (self.hasShortDiscriminator) { + payload.discriminator.SetShortValue([self.discriminator unsignedCharValue]); + } else { + payload.discriminator.SetLongValue([self.discriminator unsignedShortValue]); + } + payload.setUpPINCode = [self.setUpPINCode unsignedIntValue]; + + err = chip::ManualSetupPayloadGenerator(payload).payloadDecimalStringRepresentation(outDecimalString); + + if (err != CHIP_NO_ERROR) { + return nil; + } + + return [NSString stringWithUTF8String:outDecimalString.c_str()]; +} + @end