Skip to content

Commit

Permalink
[chip-tool] Add a command to generate/update a qrcode and a command t…
Browse files Browse the repository at this point in the history
…o generate/update a manual code
  • Loading branch information
vivien-apple committed Mar 28, 2022
1 parent 261d13d commit 398eea4
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static_library("chip-tool-utils") {
"commands/pairing/OpenCommissioningWindowCommand.h",
"commands/pairing/PairingCommand.cpp",
"commands/payload/AdditionalDataParseCommand.cpp",
"commands/payload/SetupPayloadGenerateCommand.cpp",
"commands/payload/SetupPayloadParseCommand.cpp",
"commands/payload/SetupPayloadVerhoeff.cpp",
"commands/tests/TestCommand.cpp",
Expand Down
11 changes: 7 additions & 4 deletions examples/chip-tool/commands/payload/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
#pragma once

#include "AdditionalDataParseCommand.h"
#include "SetupPayloadGenerateCommand.h"
#include "SetupPayloadParseCommand.h"
#include "SetupPayloadVerhoeff.h"

void registerCommandsPayload(Commands & commands)
{
const char * clusterName = "Payload";
commands_list clusterCommands = {
make_unique<SetupPayloadParseCommand>(),
make_unique<AdditionalDataParseCommand>(),
make_unique<SetupPayloadVerhoeffVerify>(),
make_unique<SetupPayloadVerhoeffGenerate>(),
make_unique<SetupPayloadGenerateQRCodeCommand>(), //
make_unique<SetupPayloadGenerateManualCodeCommand>(), //
make_unique<SetupPayloadParseCommand>(), //
make_unique<AdditionalDataParseCommand>(), //
make_unique<SetupPayloadVerhoeffVerify>(), //
make_unique<SetupPayloadVerhoeffGenerate>(), //
};

commands.Register(clusterName, clusterCommands);
Expand Down
130 changes: 130 additions & 0 deletions examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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 "SetupPayloadGenerateCommand.h"
#include <setup_payload/ManualSetupPayloadGenerator.h>
#include <setup_payload/ManualSetupPayloadParser.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/QRCodeSetupPayloadParser.h>
#include <setup_payload/SetupPayload.h>

using namespace ::chip;

CHIP_ERROR SetupPayloadGenerateQRCodeCommand::Run()
{
SetupPayload payload;

if (mPayload.HasValue())
{
QRCodeSetupPayloadParser(mPayload.Value()).populatePayload(payload);
}

if (mDiscriminator.HasValue())
{
payload.discriminator = mDiscriminator.Value();
}

if (mSetUpPINCode.HasValue())
{
payload.setUpPINCode = mSetUpPINCode.Value();
}

if (mVersion.HasValue())
{
payload.version = mVersion.Value();
}

if (mVendorId.HasValue())
{
payload.vendorID = mVendorId.Value();
}

if (mProductId.HasValue())
{
payload.productID = mProductId.Value();
}

if (mCommissioningMode.HasValue())
{
payload.commissioningFlow = static_cast<CommissioningFlow>(mCommissioningMode.Value());
}

if (mRendezvous.HasValue())
{
payload.rendezvousInformation.SetRaw(mRendezvous.Value());
}

QRCodeSetupPayloadGenerator generator(payload);
generator.SetAllowInvalidPayload(mAllowInvalidPayload.ValueOr(false));

std::string code;
ReturnErrorOnFailure(generator.payloadBase38Representation(code));
ChipLogProgress(chipTool, "QR Code: %s", code.c_str());

return CHIP_NO_ERROR;
}

CHIP_ERROR SetupPayloadGenerateManualCodeCommand::Run()
{
SetupPayload payload;

if (mPayload.HasValue())
{
ManualSetupPayloadParser(mPayload.Value()).populatePayload(payload);
}

if (mDiscriminator.HasValue())
{
payload.discriminator = mDiscriminator.Value();
}

if (mSetUpPINCode.HasValue())
{
payload.setUpPINCode = mSetUpPINCode.Value();
}

if (mVersion.HasValue())
{
payload.version = mVersion.Value();
}

if (mVendorId.HasValue())
{
payload.vendorID = mVendorId.Value();
}

if (mProductId.HasValue())
{
payload.productID = mProductId.Value();
}

if (mCommissioningMode.HasValue())
{
payload.commissioningFlow = static_cast<CommissioningFlow>(mCommissioningMode.Value());
}

ManualSetupPayloadGenerator generator(payload);
generator.SetAllowInvalidPayload(mAllowInvalidPayload.ValueOr(false));
generator.SetForceShortCode(mForceShortCode.ValueOr(false));

std::string code;
ReturnErrorOnFailure(generator.payloadDecimalStringRepresentation(code));
ChipLogProgress(chipTool, "Manual Code: %s", code.c_str());

return CHIP_NO_ERROR;
}
80 changes: 80 additions & 0 deletions examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 "../common/Command.h"
#include <setup_payload/SetupPayload.h>

class SetupPayloadGenerateQRCodeCommand : public Command
{
public:
SetupPayloadGenerateQRCodeCommand() : Command("generate-qrcode")
{
AddArgument("discriminator", 0, UINT16_MAX, &mDiscriminator);
AddArgument("setup-pin-code", 0, UINT32_MAX, &mSetUpPINCode);
AddArgument("version", 0, UINT8_MAX, &mVersion);
AddArgument("vendor-id", 0, UINT16_MAX, &mVendorId);
AddArgument("product-id", 0, UINT16_MAX, &mProductId);
AddArgument("rendezvous", 0, UINT8_MAX, &mRendezvous);
AddArgument("commissioning-mode", 0, UINT8_MAX, &mCommissioningMode);
AddArgument("payload", &mPayload);
AddArgument("allow-invalid-payload", 0, 1, &mAllowInvalidPayload);
}
CHIP_ERROR Run() override;

private:
chip::Optional<uint16_t> mDiscriminator;
chip::Optional<uint32_t> mSetUpPINCode;
chip::Optional<uint8_t> mVersion;
chip::Optional<uint16_t> mVendorId;
chip::Optional<uint16_t> mProductId;
chip::Optional<uint8_t> mRendezvous;
chip::Optional<char *> mPayload;
chip::Optional<uint8_t> mCommissioningMode;
chip::Optional<bool> mAllowInvalidPayload;
};

class SetupPayloadGenerateManualCodeCommand : public Command
{
public:
SetupPayloadGenerateManualCodeCommand() : Command("generate-manualcode")
{
AddArgument("discriminator", 0, UINT16_MAX, &mDiscriminator);
AddArgument("setup-pin-code", 0, UINT32_MAX, &mSetUpPINCode);
AddArgument("version", 0, UINT8_MAX, &mVersion);
AddArgument("vendor-id", 0, UINT16_MAX, &mVendorId);
AddArgument("product-id", 0, UINT16_MAX, &mProductId);
AddArgument("commissioning-mode", 0, UINT8_MAX, &mCommissioningMode);
AddArgument("payload", &mPayload);
AddArgument("allow-invalid-payload", 0, 1, &mAllowInvalidPayload);
AddArgument("force-short-code", 0, 1, &mForceShortCode);
}
CHIP_ERROR Run() override;

private:
chip::Optional<uint16_t> mDiscriminator;
chip::Optional<uint32_t> mSetUpPINCode;
chip::Optional<uint8_t> mVersion;
chip::Optional<uint16_t> mVendorId;
chip::Optional<uint16_t> mProductId;
chip::Optional<uint8_t> mCommissioningMode;
chip::Optional<char *> mPayload;
chip::Optional<bool> mAllowInvalidPayload;
chip::Optional<bool> mForceShortCode;
};

0 comments on commit 398eea4

Please sign in to comment.