-
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.
[chip-tool] Add a command to generate/update a qrcode and a command t…
…o generate/update a manual code
- Loading branch information
1 parent
261d13d
commit 398eea4
Showing
4 changed files
with
218 additions
and
4 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
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
130 changes: 130 additions & 0 deletions
130
examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.cpp
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,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
80
examples/chip-tool/commands/payload/SetupPayloadGenerateCommand.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,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; | ||
}; |