-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add payload parser to darwin framework tool
- Loading branch information
1 parent
19d0f52
commit b91a2ad
Showing
5 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
examples/darwin-framework-tool/commands/payload/Commands.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "SetupPayloadParseCommand.h" | ||
|
||
void registerCommandsPayload(Commands & commands) | ||
{ | ||
const char * clusterName = "Payload"; | ||
commands_list clusterCommands = { | ||
make_unique<SetupPayloadParseCommand>(), // | ||
}; | ||
|
||
commands.Register(clusterName, clusterCommands); | ||
} |
41 changes: 41 additions & 0 deletions
41
examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#import <CHIP/CHIP.h> | ||
#include <commands/common/Command.h> | ||
|
||
class SetupPayloadParseCommand : public Command { | ||
public: | ||
SetupPayloadParseCommand() | ||
: Command("parse-setup-payload") | ||
{ | ||
AddArgument("payload", &mCode); | ||
} | ||
CHIP_ERROR Run() override; | ||
static bool IsQRCode(NSString * codeString); | ||
|
||
private: | ||
char * mCode; | ||
CHIP_ERROR Print(CHIPSetupPayload * payload); | ||
|
||
// Will log the given string and given error (as progress if success, error | ||
// if failure). | ||
void LogNSError(const char * logString, NSError * error); | ||
}; |
142 changes: 142 additions & 0 deletions
142
examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "SetupPayloadParseCommand.h" | ||
#import <CHIP/CHIP.h> | ||
#import <CHIP/CHIPError_Internal.h> | ||
|
||
using namespace ::chip; | ||
|
||
namespace { | ||
|
||
#if CHIP_PROGRESS_LOGGING | ||
|
||
NSString * CustomFlowString(CHIPCommissioningFlow flow) | ||
{ | ||
switch (flow) { | ||
case kCommissioningFlowStandard: | ||
return @"STANDARD"; | ||
case kCommissioningFlowUserActionRequired: | ||
return @"USER ACTION REQUIRED"; | ||
case kCommissioningFlowCustom: | ||
return @"CUSTOM"; | ||
case kCommissioningFlowInvalid: | ||
return @"INVALID"; | ||
} | ||
|
||
return @"???"; | ||
} | ||
|
||
#endif // CHIP_PROGRESS_LOGGING | ||
|
||
} // namespace | ||
|
||
void SetupPayloadParseCommand::LogNSError(const char * logString, NSError * error) | ||
{ | ||
CHIP_ERROR err = [CHIPError errorToCHIPErrorCode:error]; | ||
if (err == CHIP_NO_ERROR) { | ||
ChipLogProgress(chipTool, "%s: %s", logString, chip::ErrorStr(err)); | ||
} else { | ||
ChipLogError(chipTool, "%s: %s", logString, chip::ErrorStr(err)); | ||
} | ||
} | ||
|
||
CHIP_ERROR SetupPayloadParseCommand::Run() | ||
{ | ||
NSString * codeString = [NSString stringWithCString:mCode encoding:NSASCIIStringEncoding]; | ||
NSError * error; | ||
CHIPSetupPayload * payload; | ||
CHIPOnboardingPayloadType codeType; | ||
if (IsQRCode(codeString)) { | ||
codeType = CHIPOnboardingPayloadTypeQRCode; | ||
} else { | ||
codeType = CHIPOnboardingPayloadTypeManualCode; | ||
} | ||
payload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:codeString ofType:codeType error:&error]; | ||
if (error) { | ||
LogNSError("Error: ", error); | ||
return CHIP_ERROR_INTERNAL; | ||
} | ||
ReturnErrorOnFailure(Print(payload)); | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
CHIP_ERROR SetupPayloadParseCommand::Print(CHIPSetupPayload * payload) | ||
{ | ||
NSLog(@"Version: %@", payload.version); | ||
NSLog(@"VendorID: %@", payload.vendorID); | ||
NSLog(@"ProductID: %@", payload.productID); | ||
NSLog(@"Custom flow: %lu (%@)", payload.commissioningFlow, CustomFlowString(payload.commissioningFlow)); | ||
{ | ||
NSMutableString * humanFlags = [[NSMutableString alloc] init]; | ||
|
||
if (payload.rendezvousInformation) { | ||
if (payload.rendezvousInformation & kRendezvousInformationNone) { | ||
[humanFlags appendString:@"NONE"]; | ||
} else { | ||
if (payload.rendezvousInformation & kRendezvousInformationSoftAP) { | ||
[humanFlags appendString:@"SoftAP"]; | ||
} | ||
if (payload.rendezvousInformation & kRendezvousInformationBLE) { | ||
if (!humanFlags) { | ||
[humanFlags appendString:@", "]; | ||
} | ||
[humanFlags appendString:@"BLE"]; | ||
} | ||
if (payload.rendezvousInformation & kRendezvousInformationOnNetwork) { | ||
if (!humanFlags) { | ||
[humanFlags appendString:@", "]; | ||
} | ||
[humanFlags appendString:@"ON NETWORK"]; | ||
} | ||
} | ||
} else { | ||
[humanFlags appendString:@"NONE"]; | ||
} | ||
|
||
NSLog(@"Capabilities: 0x%02lX (%@)", payload.rendezvousInformation, humanFlags); | ||
} | ||
NSLog(@"Discriminator: %@", payload.discriminator); | ||
NSLog(@"Passcode: %@", payload.setUpPINCode); | ||
|
||
if (payload.serialNumber) { | ||
NSLog(@"SerialNumber: %@", payload.serialNumber); | ||
} | ||
NSError * error; | ||
NSArray<CHIPOptionalQRCodeInfo *> * optionalVendorData = [payload getAllOptionalVendorData:&error]; | ||
if (error) { | ||
LogNSError("Error: ", error); | ||
return CHIP_ERROR_INTERNAL; | ||
} | ||
for (const CHIPOptionalQRCodeInfo * info : optionalVendorData) { | ||
bool isTypeString = info.infoType == [[NSNumber alloc] initWithInt:kOptionalQRCodeInfoTypeString]; | ||
bool isTypeInt32 = info.infoType == [[NSNumber alloc] initWithInt:kOptionalQRCodeInfoTypeInt32]; | ||
VerifyOrReturnError(isTypeString || isTypeInt32, CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
if (isTypeString) { | ||
NSLog(@"OptionalQRCodeInfo: tag=%@,string value=%@", info.tag, info.stringValue); | ||
} else { | ||
NSLog(@"OptionalQRCodeInfo: tag=%@,int value=%@", info.tag, info.integerValue); | ||
} | ||
} | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
bool SetupPayloadParseCommand::IsQRCode(NSString * codeString) { return [codeString hasPrefix:@"MT:"]; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters