Skip to content

Commit

Permalink
Add payload parser to darwin framework tool
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton36 authored and woody-apple committed Jul 2, 2022
1 parent 19d0f52 commit b91a2ad
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/darwin-framework-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ executable("darwin-framework-tool") {
"commands/pairing/OpenCommissioningWindowCommand.mm",
"commands/pairing/PairingCommandBridge.mm",
"commands/pairing/PairingDelegateBridge.mm",
"commands/payload/SetupPayloadParseCommand.mm",
"commands/storage/Commands.h",
"commands/storage/StorageManagementCommand.mm",
"main.mm",
Expand Down
31 changes: 31 additions & 0 deletions examples/darwin-framework-tool/commands/payload/Commands.h
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);
}
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);
};
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:"]; }
2 changes: 2 additions & 0 deletions examples/darwin-framework-tool/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "commands/common/Commands.h"
#include "commands/interactive/Commands.h"
#include "commands/pairing/Commands.h"
#include "commands/payload/Commands.h"
#include "commands/storage/Commands.h"

#include <zap-generated/cluster/Commands.h>
Expand All @@ -31,6 +32,7 @@ int main(int argc, const char * argv[])
Commands commands;
registerCommandsPairing(commands);
registerCommandsInteractive(commands);
registerCommandsPayload(commands);
registerCommandsStorage(commands);
registerCommandsTests(commands);
registerClusters(commands);
Expand Down

0 comments on commit b91a2ad

Please sign in to comment.