Skip to content

Commit

Permalink
Expose method to get the manual entry code from a setup payload. (#22110
Browse files Browse the repository at this point in the history
)

* Expose method to get the manual entry code from a setup payload.

* Restyled by clang-format

* Update src/darwin/Framework/CHIP/MTRSetupPayload.h

Co-authored-by: Justin Wood <[email protected]>

* Update src/darwin/Framework/CHIP/MTRSetupPayload.mm

Co-authored-by: Justin Wood <[email protected]>

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Justin Wood <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2022
1 parent b8caa3d commit 1952eb7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRSetupPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions src/darwin/Framework/CHIP/MTRSetupPayload.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "MTRError.h"
#import "MTRError_Internal.h"
#import "MTRSetupPayload_Internal.h"
#import "setup_payload/ManualSetupPayloadGenerator.h"
#import <setup_payload/SetupPayload.h>

@implementation MTROptionalQRCodeInfo
Expand Down Expand Up @@ -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

0 comments on commit 1952eb7

Please sign in to comment.