Skip to content

Commit

Permalink
Add a generic typedef id (^SyncWorkQueueBlockWithReturnValue)(void)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Dec 14, 2022
1 parent efbc9b9 commit c2f331a
Showing 1 changed file with 58 additions and 53 deletions.
111 changes: 58 additions & 53 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
static NSString * const kErrorSpake2pVerifierSerializationFailed = @"PASE verifier serialization failed";

typedef void (^SyncWorkQueueBlock)(void);
typedef BOOL (^SyncWorkQueueBlockWithReturnValue)(void);
typedef id (^SyncWorkQueueBlockWithReturnValue)(void);
typedef BOOL (^SyncWorkQueueBlockWithBoolReturnValue)(void);

@interface MTRDeviceController ()

Expand Down Expand Up @@ -369,14 +370,9 @@ - (BOOL)startup:(MTRDeviceControllerStartupParamsInternal *)startupParams

- (NSNumber *)controllerNodeID
{
__block NSNumber * nodeID = nil;

auto block = ^{
nodeID = @(self->_cppCommissioner->GetNodeId());
};

[self syncRunOnWorkQueue:block error:nil];
auto block = ^NSNumber * { return @(self->_cppCommissioner->GetNodeId()); };

auto nodeID = [self syncRunOnWorkQueueWithReturnValue:block error:nil];
if (!nodeID) {
MTR_LOG_ERROR("A controller has no node id if it has not been started");
}
Expand All @@ -388,7 +384,7 @@ - (BOOL)setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload
newNodeID:(NSNumber *)newNodeID
error:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
// Try to get a QR code if possible (because it has a better
// discriminator, etc), then fall back to manual code if that fails.
NSString * pairingCode = [payload qrCodeString:nil];
Expand All @@ -405,14 +401,14 @@ - (BOOL)setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (BOOL)commissionNodeWithID:(NSNumber *)nodeID
commissioningParams:(MTRCommissioningParameters *)commissioningParams
error:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
chip::Controller::CommissioningParameters params;
if (commissioningParams.csrNonce) {
params.SetCSRNonce(AsByteSpan(commissioningParams.csrNonce));
Expand Down Expand Up @@ -458,14 +454,14 @@ - (BOOL)commissionNodeWithID:(NSNumber *)nodeID
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (BOOL)continueCommissioningDevice:(void *)device
ignoreAttestationFailure:(BOOL)ignoreAttestationFailure
error:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
auto lastAttestationResult = self->_deviceAttestationDelegateBridge
? self->_deviceAttestationDelegateBridge->attestationVerificationResult()
: chip::Credentials::AttestationVerificationResult::kSuccess;
Expand All @@ -476,45 +472,43 @@ - (BOOL)continueCommissioningDevice:(void *)device
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (BOOL)cancelCommissioningForNodeID:(NSNumber *)nodeID error:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
self->_operationalCredentialsDelegate->ResetDeviceID();
auto errorCode = self.cppCommissioner->StopPairing([nodeID unsignedLongLongValue]);
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorStopPairing error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (BOOL)prepareCommissioningSession:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
auto errorCode = chip::DeviceLayer::PlatformMgrImpl().PrepareCommissioning();
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPrepareCommissioning error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (MTRBaseDevice *)deviceBeingCommissionedWithNodeID:(NSNumber *)nodeID error:(NSError * __autoreleasing *)error
{
__block MTRBaseDevice * device = nil;

auto block = ^{
auto block = ^MTRBaseDevice *
{
chip::CommissioneeDeviceProxy * deviceProxy;

auto errorCode = self->_cppCommissioner->GetDeviceBeingCommissioned(nodeID.unsignedLongLongValue, &deviceProxy);
VerifyOrReturn(![MTRDeviceController checkForError:errorCode logMsg:kErrorStopPairing error:error]);
VerifyOrReturnValue(![MTRDeviceController checkForError:errorCode logMsg:kErrorStopPairing error:error], nil);

device = [[MTRBaseDevice alloc] initWithPASEDevice:deviceProxy controller:self];
return [[MTRBaseDevice alloc] initWithPASEDevice:deviceProxy controller:self];
};

[self syncRunOnWorkQueue:block error:error];
return device;
return [self syncRunOnWorkQueueWithReturnValue:block error:error];
}

- (MTRBaseDevice *)baseDeviceForNodeID:(NSNumber *)nodeID
Expand Down Expand Up @@ -578,7 +572,7 @@ - (BOOL)setOperationalCertificateIssuer:(nullable id<MTROperationalCertificateIs
return YES;
};

return [self syncRunOnWorkQueueWithReturnValue:block error:nil];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:nil];
}

+ (nullable NSData *)computePASEVerifierForSetupPasscode:(NSNumber *)setupPasscode
Expand All @@ -604,24 +598,22 @@ + (nullable NSData *)computePASEVerifierForSetupPasscode:(NSNumber *)setupPassco

- (NSData * _Nullable)attestationChallengeForDeviceID:(NSNumber *)deviceID
{
__block NSData * attestationChallenge = nil;

auto block = ^{
auto block = ^NSData *
{
chip::CommissioneeDeviceProxy * deviceProxy;
auto errorCode = self.cppCommissioner->GetDeviceBeingCommissioned([deviceID unsignedLongLongValue], &deviceProxy);
VerifyOrReturn(![MTRDeviceController checkForError:errorCode logMsg:kErrorGetCommissionee error:nil]);
VerifyOrReturnValue(![MTRDeviceController checkForError:errorCode logMsg:kErrorGetCommissionee error:nil], nil);

uint8_t challengeBuffer[chip::Crypto::kAES_CCM128_Key_Length];
chip::ByteSpan challenge(challengeBuffer);

errorCode = deviceProxy->GetAttestationChallenge(challenge);
VerifyOrReturn(![MTRDeviceController checkForError:errorCode logMsg:kErrorGetAttestationChallenge error:nil]);
VerifyOrReturnValue(![MTRDeviceController checkForError:errorCode logMsg:kErrorGetAttestationChallenge error:nil], nil);

attestationChallenge = AsData(challenge);
return AsData(challenge);
};

[self syncRunOnWorkQueue:block error:nil];
return attestationChallenge;
return [self syncRunOnWorkQueueWithReturnValue:block error:nil];
}

- (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg
Expand Down Expand Up @@ -787,7 +779,21 @@ - (void)syncRunOnWorkQueue:(SyncWorkQueueBlock)block error:(NSError * __autorele
});
}

- (BOOL)syncRunOnWorkQueueWithReturnValue:(SyncWorkQueueBlockWithReturnValue)block error:(NSError * __autoreleasing *)error
- (id)syncRunOnWorkQueueWithReturnValue:(SyncWorkQueueBlockWithReturnValue)block error:(NSError * __autoreleasing *)error
{
__block id rv = nil;

VerifyOrReturnValue([self checkIsRunning:error], rv);

dispatch_sync(_chipWorkQueue, ^{
VerifyOrReturn([self checkIsRunning:error]);
rv = block();
});

return rv;
}

- (BOOL)syncRunOnWorkQueueWithBoolReturnValue:(SyncWorkQueueBlockWithBoolReturnValue)block error:(NSError * __autoreleasing *)error
{
__block BOOL success = NO;

Expand Down Expand Up @@ -1018,7 +1024,7 @@ - (BOOL)pairDevice:(uint64_t)deviceID
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
std::string manualPairingCode;
chip::SetupPayload payload;
payload.discriminator.SetLongValue(discriminator);
Expand All @@ -1032,7 +1038,7 @@ - (BOOL)pairDevice:(uint64_t)deviceID
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (BOOL)pairDevice:(uint64_t)deviceID
Expand All @@ -1041,7 +1047,7 @@ - (BOOL)pairDevice:(uint64_t)deviceID
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
chip::Inet::IPAddress addr;
chip::Inet::IPAddress::FromString([address UTF8String], addr);
chip::Transport::PeerAddress peerAddress = chip::Transport::PeerAddress::UDP(addr, port);
Expand All @@ -1053,18 +1059,18 @@ - (BOOL)pairDevice:(uint64_t)deviceID
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPayload error:(NSError * __autoreleasing *)error
{
auto block = ^{
auto block = ^BOOL {
self->_operationalCredentialsDelegate->SetDeviceID(deviceID);
auto errorCode = self.cppCommissioner->EstablishPASEConnection(deviceID, [onboardingPayload UTF8String]);
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (BOOL)commissionDevice:(uint64_t)deviceID
Expand Down Expand Up @@ -1094,13 +1100,13 @@ - (BOOL)openPairingWindow:(uint64_t)deviceID duration:(NSUInteger)duration error
return NO;
}

auto block = ^{
auto block = ^BOOL {
auto errorCode = chip::Controller::AutoCommissioningWindowOpener::OpenBasicCommissioningWindow(
self.cppCommissioner, deviceID, chip::System::Clock::Seconds16(static_cast<uint16_t>(duration)));
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorOpenPairingWindow error:error];
};

return [self syncRunOnWorkQueueWithReturnValue:block error:error];
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
}

- (NSString *)openPairingWindowWithPIN:(uint64_t)deviceID
Expand Down Expand Up @@ -1133,30 +1139,29 @@ - (NSString *)openPairingWindowWithPIN:(uint64_t)deviceID
return nil;
}

__block NSString * rv = nil;

auto block = ^{
auto block = ^NSString *
{
chip::SetupPayload setupPayload;
auto errorCode = chip::Controller::AutoCommissioningWindowOpener::OpenCommissioningWindow(self.cppCommissioner, deviceID,
chip::System::Clock::Seconds16(static_cast<uint16_t>(duration)), chip::Crypto::kSpake2p_Min_PBKDF_Iterations,
static_cast<uint16_t>(discriminator), chip::MakeOptional(static_cast<uint32_t>(setupPIN)), chip::NullOptional,
setupPayload);

VerifyOrReturn(![MTRDeviceController checkForError:errorCode logMsg:kErrorOpenPairingWindow error:error]);
VerifyOrReturnValue(![MTRDeviceController checkForError:errorCode logMsg:kErrorOpenPairingWindow error:error], nil);

chip::ManualSetupPayloadGenerator generator(setupPayload);
std::string outCode;

if (generator.payloadDecimalStringRepresentation(outCode) == CHIP_NO_ERROR) {
MTR_LOG_ERROR("Setup code is %s", outCode.c_str());
rv = [NSString stringWithCString:outCode.c_str() encoding:[NSString defaultCStringEncoding]];
} else {
if (CHIP_NO_ERROR != generator.payloadDecimalStringRepresentation(outCode)) {
MTR_LOG_ERROR("Failed to get decimal setup code");
return nil;
}

MTR_LOG_ERROR("Setup code is %s", outCode.c_str());
return [NSString stringWithCString:outCode.c_str() encoding:[NSString defaultCStringEncoding]];
};

[self syncRunOnWorkQueue:block error:error];
return rv;
return [self syncRunOnWorkQueueWithReturnValue:block error:error];
}

- (nullable NSData *)computePaseVerifier:(uint32_t)setupPincode iterations:(uint32_t)iterations salt:(NSData *)salt
Expand Down

0 comments on commit c2f331a

Please sign in to comment.