Skip to content

Commit

Permalink
Stop assuming CHIP_ERROR is an integer in Darwin code. (#8585)
Browse files Browse the repository at this point in the history
Some of the fixes have to do with the fact that %@ is not a format
ChipLogging supports, not strictly with the CHIP_ERROR changes.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Jul 26, 2021
1 parent 930c148 commit 2894530
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/CHIPDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ - (BOOL)openPairingWindow:(NSUInteger)duration error:(NSError * __autoreleasing
[self.lock unlock];

if (err != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Error(%d): %@, Open Pairing Window failed", err, [CHIPError errorForCHIPErrorCode:err]);
CHIP_LOG_ERROR("Error(%s): Open Pairing Window failed", chip::ErrorStr(err));
if (error) {
*error = [CHIPError errorForCHIPErrorCode:err];
}
Expand Down Expand Up @@ -117,7 +117,7 @@ - (NSString *)openPairingWindowWithPIN:(NSUInteger)duration
[self.lock unlock];

if (err != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Error(%d): %@, Open Pairing Window failed", err, [CHIPError errorForCHIPErrorCode:err]);
CHIP_LOG_ERROR("Error(%s): Open Pairing Window failed", chip::ErrorStr(err));
if (error) {
*error = [CHIPError errorForCHIPErrorCode:err];
}
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ - (void)updateDevice:(uint64_t)deviceID fabricId:(uint64_t)fabricId
dispatch_sync(_chipWorkQueue, ^{
if ([self isRunning]) {
errorCode = self.cppCommissioner->UpdateDevice(deviceID, fabricId);
CHIP_LOG_ERROR("Update device address returned: %d", errorCode);
CHIP_LOG_ERROR("Update device address returned: %s", chip::ErrorStr(errorCode));
}
});
}
Expand Down Expand Up @@ -501,7 +501,7 @@ - (BOOL)checkForError:(CHIP_ERROR)errorCode logMsg:(NSString *)logMsg error:(NSE
return NO;
}

CHIP_LOG_ERROR("Error(%d): %@, %@", errorCode, [CHIPError errorForCHIPErrorCode:errorCode], logMsg);
CHIP_LOG_ERROR("Error(%s): %s", chip::ErrorStr(errorCode), [logMsg UTF8String]);
if (error) {
*error = [CHIPError errorForCHIPErrorCode:errorCode];
}
Expand Down
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIP/CHIPDevicePairingDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

void CHIPDevicePairingDelegateBridge::OnPairingComplete(CHIP_ERROR error)
{
NSLog(@"DevicePairingDelegate Pairing complete. Status %d", error);
NSLog(@"DevicePairingDelegate Pairing complete. Status %s", chip::ErrorStr(error));

id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onPairingComplete:)]) {
Expand All @@ -82,7 +82,7 @@

void CHIPDevicePairingDelegateBridge::OnPairingDeleted(CHIP_ERROR error)
{
NSLog(@"DevicePairingDelegate Pairing deleted. Status %d", error);
NSLog(@"DevicePairingDelegate Pairing deleted. Status %s", chip::ErrorStr(error));

id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onPairingDeleted:)]) {
Expand All @@ -97,7 +97,7 @@

void CHIPDevicePairingDelegateBridge::OnAddressUpdateComplete(chip::NodeId nodeId, CHIP_ERROR error)
{
NSLog(@"OnAddressUpdateComplete. Status %d", error);
NSLog(@"OnAddressUpdateComplete. Status %s", chip::ErrorStr(error));

id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onAddressUpdated:)]) {
Expand Down
57 changes: 39 additions & 18 deletions src/darwin/Framework/CHIP/CHIPError.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,61 @@ @implementation CHIPError

+ (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode
{
switch (errorCode) {
case CHIP_ERROR_INVALID_STRING_LENGTH:
if (errorCode == CHIP_ERROR_INVALID_STRING_LENGTH) {
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeInvalidStringLength
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"A list length is invalid.", nil) }];
case CHIP_ERROR_INVALID_INTEGER_VALUE:
}

if (errorCode == CHIP_ERROR_INVALID_INTEGER_VALUE) {
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeInvalidIntegerValue
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Unexpected integer value.", nil) }];
case CHIP_ERROR_INVALID_ARGUMENT:
}

if (errorCode == CHIP_ERROR_INVALID_ARGUMENT) {
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeInvalidArgument
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"An argument is invalid.", nil) }];
case CHIP_ERROR_INVALID_MESSAGE_LENGTH:
}

if (errorCode == CHIP_ERROR_INVALID_MESSAGE_LENGTH) {
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeInvalidMessageLength
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"A message length is invalid.", nil) }];
case CHIP_ERROR_INCORRECT_STATE:
}

if (errorCode == CHIP_ERROR_INCORRECT_STATE) {
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeInvalidState
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Invalid object state.", nil) }];
case CHIP_ERROR_INTEGRITY_CHECK_FAILED:
}

if (errorCode == CHIP_ERROR_INTEGRITY_CHECK_FAILED) {
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeIntegrityCheckFailed
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Integrity check failed.", nil) }];
}

if (errorCode == CHIP_NO_ERROR) {
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPSuccess
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Success.", nil) }];
}

return
[NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeUndefinedError
userInfo:@{
NSLocalizedDescriptionKey : [NSString stringWithFormat:NSLocalizedString(@"Undefined error:%u.", nil),
chip::ChipError::AsInteger(errorCode)]
}];
;
}

+ (NSError *)errorForZCLErrorCode:(uint8_t)errorCode
{
switch (errorCode) {
case EMBER_ZCL_STATUS_DUPLICATE_EXISTS:
return [NSError
errorWithDomain:CHIPErrorDomain
Expand All @@ -61,18 +91,13 @@ + (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeUnsupportedAttribute
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Attribute is not supported.", nil) }];
case CHIP_NO_ERROR:
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPSuccess
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Success.", nil) }];
default:
return [NSError errorWithDomain:CHIPErrorDomain
code:CHIPErrorCodeUndefinedError
userInfo:@{
NSLocalizedDescriptionKey :
[NSString stringWithFormat:NSLocalizedString(@"Undefined error:%d.", nil), errorCode]
NSLocalizedDescriptionKey : [NSString
stringWithFormat:NSLocalizedString(@"Undefined data model error:%u.", nil), errorCode]
}];
;
}
}

Expand All @@ -83,10 +108,6 @@ + (CHIP_ERROR)errorToCHIPErrorCode:(NSError *)error
}

switch (error.code) {
case CHIPErrorCodeUnsupportedAttribute:
return EMBER_ZCL_STATUS_UNSUPPORTED_ATTRIBUTE;
case CHIPErrorCodeDuplicateExists:
return EMBER_ZCL_STATUS_DUPLICATE_EXISTS;
case CHIPErrorCodeInvalidStringLength:
return CHIP_ERROR_INVALID_STRING_LENGTH;
case CHIPErrorCodeInvalidIntegerValue:
Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/CHIPError_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface CHIPError : NSObject
+ (nullable NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode;
+ (nullable NSError *)errorForZCLErrorCode:(uint8_t)errorCode;
+ (CHIP_ERROR)errorToCHIPErrorCode:(NSError *)errorCode;
@end

Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/CHIPManualSetupPayloadParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (CHIPSetupPayload *)populatePayload:(NSError * __autoreleasing *)error
if (_chipManualSetupPayloadParser) {
CHIP_ERROR chipError = _chipManualSetupPayloadParser->populatePayload(cPlusPluspayload);

if (chipError == 0) {
if (chipError == CHIP_NO_ERROR) {
payload = [[CHIPSetupPayload alloc] initWithSetupPayload:cPlusPluspayload];
} else if (error) {
*error = [CHIPError errorForCHIPErrorCode:chipError];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static BOOL isRunningTests(void)
// err = SetIssuerID(storage);
}

CHIP_LOG_ERROR("CHIPOperationalCredentialsDelegate::init returning %d", err);
CHIP_LOG_ERROR("CHIPOperationalCredentialsDelegate::init returning %s", chip::ErrorStr(err));
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/CHIPQRCodeSetupPayloadParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (CHIPSetupPayload *)populatePayload:(NSError * __autoreleasing *)error
if (_chipQRCodeSetupPayloadParser) {
CHIP_ERROR chipError = _chipQRCodeSetupPayloadParser->populatePayload(cPlusPluspayload);

if (chipError == 0) {
if (chipError == CHIP_NO_ERROR) {
payload = [[CHIPSetupPayload alloc] initWithSetupPayload:cPlusPluspayload];
} else if (error) {
*error = [CHIPError errorForCHIPErrorCode:chipError];
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/CHIPThreadOperationalDataset.mm
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ - (nullable instancetype)initWithData:(NSData *)data
auto dataset = chip::Thread::OperationalDataset();
CHIP_ERROR error = dataset.Init(span);
if (error != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Failed to parse data, cannot construct Operational Dataset. %d", error);
CHIP_LOG_ERROR("Failed to parse data, cannot construct Operational Dataset. %s", chip::ErrorStr(error));
return nil;
}
// len+1 for null termination
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/gen/CHIPClustersObjc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void CallbackFn(void * context, uint8_t status)
CHIPDefaultFailureCallbackBridge * callback = reinterpret_cast<CHIPDefaultFailureCallbackBridge *>(context);
if (callback && callback->mQueue) {
dispatch_async(callback->mQueue, ^{
NSError * error = [CHIPError errorForCHIPErrorCode:status];
NSError * error = [CHIPError errorForZCLErrorCode:status];
callback->mHandler(error, nil);
callback->Cancel();
delete callback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public:
CHIPDefaultFailureCallbackBridge * callback = reinterpret_cast<CHIPDefaultFailureCallbackBridge *>(context);
if (callback && callback->mQueue) {
dispatch_async(callback->mQueue, ^{
NSError * error = [CHIPError errorForCHIPErrorCode:status];
NSError * error = [CHIPError errorForZCLErrorCode:status];
callback->mHandler(error, nil);
callback->Cancel();
delete callback;
Expand Down

0 comments on commit 2894530

Please sign in to comment.