diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 03f97a667ad84a..926bd4064e2959 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -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", diff --git a/examples/chip-tool/commands/payload/Commands.h b/examples/chip-tool/commands/payload/Commands.h index 7d72b97e702905..1b5eb2c7c5c62a 100644 --- a/examples/chip-tool/commands/payload/Commands.h +++ b/examples/chip-tool/commands/payload/Commands.h @@ -19,6 +19,7 @@ #pragma once #include "AdditionalDataParseCommand.h" +#include "SetupPayloadGenerateCommand.h" #include "SetupPayloadParseCommand.h" #include "SetupPayloadVerhoeff.h" @@ -26,10 +27,12 @@ void registerCommandsPayload(Commands & commands) { const char * clusterName = "Payload"; commands_list clusterCommands = { - make_unique(), - make_unique(), - make_unique(), - make_unique(), + make_unique(), // + make_unique(), // + make_unique(), // + make_unique(), // + make_unique(), // + make_unique(), // }; commands.Register(clusterName, clusterCommands); diff --git a/examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.cpp b/examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.cpp new file mode 100644 index 00000000000000..0a31405d8ff109 --- /dev/null +++ b/examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.cpp @@ -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 +#include +#include +#include +#include + +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(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(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; +} diff --git a/examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.h b/examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.h new file mode 100644 index 00000000000000..f461a9f36b1a54 --- /dev/null +++ b/examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.h @@ -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 + +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 mDiscriminator; + chip::Optional mSetUpPINCode; + chip::Optional mVersion; + chip::Optional mVendorId; + chip::Optional mProductId; + chip::Optional mRendezvous; + chip::Optional mPayload; + chip::Optional mCommissioningMode; + chip::Optional 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 mDiscriminator; + chip::Optional mSetUpPINCode; + chip::Optional mVersion; + chip::Optional mVendorId; + chip::Optional mProductId; + chip::Optional mCommissioningMode; + chip::Optional mPayload; + chip::Optional mAllowInvalidPayload; + chip::Optional mForceShortCode; +};