From 22df336aba922b57b574b0a4213928e2627616a6 Mon Sep 17 00:00:00 2001 From: "Josh V [Apple]" Date: Thu, 16 Dec 2021 04:23:05 -0800 Subject: [PATCH] Add Verhoeff algorithm check/generate in chip-tool (#13048) --- examples/chip-tool/BUILD.gn | 2 + .../chip-tool/commands/payload/Commands.h | 8 +- .../payload/SetupPayloadParseCommand.h | 2 +- .../commands/payload/SetupPayloadVerhoeff.cpp | 76 +++++++++++++++++++ .../commands/payload/SetupPayloadVerhoeff.h | 49 ++++++++++++ 5 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 examples/chip-tool/commands/payload/SetupPayloadVerhoeff.cpp create mode 100644 examples/chip-tool/commands/payload/SetupPayloadVerhoeff.h diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 0d5925b976ae57..89d9a1799cc5e6 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -45,6 +45,7 @@ executable("chip-tool") { "commands/pairing/PairingCommand.cpp", "commands/payload/AdditionalDataParseCommand.cpp", "commands/payload/SetupPayloadParseCommand.cpp", + "commands/payload/SetupPayloadVerhoeff.cpp", "commands/tests/TestCommand.cpp", "config/PersistentStorage.cpp", "main.cpp", @@ -66,6 +67,7 @@ executable("chip-tool") { include_dirs = [ ".", "${chip_root}/zzz_generated/chip-tool", + "${chip_root}/src/lib", ] if (chip_enable_transport_trace) { diff --git a/examples/chip-tool/commands/payload/Commands.h b/examples/chip-tool/commands/payload/Commands.h index e0ed52f25795f0..7d72b97e702905 100644 --- a/examples/chip-tool/commands/payload/Commands.h +++ b/examples/chip-tool/commands/payload/Commands.h @@ -20,11 +20,17 @@ #include "AdditionalDataParseCommand.h" #include "SetupPayloadParseCommand.h" +#include "SetupPayloadVerhoeff.h" void registerCommandsPayload(Commands & commands) { const char * clusterName = "Payload"; - commands_list clusterCommands = { make_unique(), make_unique() }; + commands_list clusterCommands = { + make_unique(), + make_unique(), + make_unique(), + make_unique(), + }; commands.Register(clusterName, clusterCommands); } diff --git a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h index 83193f909e35b8..08474023a1ff9f 100644 --- a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h +++ b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h @@ -26,10 +26,10 @@ class SetupPayloadParseCommand : public Command public: SetupPayloadParseCommand() : Command("parse-setup-payload") { AddArgument("payload", &mCode); } CHIP_ERROR Run() override; + static bool IsQRCode(std::string codeString); private: char * mCode; CHIP_ERROR Parse(std::string codeString, chip::SetupPayload & payload); CHIP_ERROR Print(chip::SetupPayload payload); - bool IsQRCode(std::string codeString); }; diff --git a/examples/chip-tool/commands/payload/SetupPayloadVerhoeff.cpp b/examples/chip-tool/commands/payload/SetupPayloadVerhoeff.cpp new file mode 100644 index 00000000000000..238e753708921c --- /dev/null +++ b/examples/chip-tool/commands/payload/SetupPayloadVerhoeff.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2020 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 "SetupPayloadVerhoeff.h" +#include "SetupPayloadParseCommand.h" +#include +#include + +using namespace ::chip; + +CHIP_ERROR SetupPayloadVerhoeffVerify::Run() +{ + std::string codeString(mSetupCode); + + bool isQRCode = SetupPayloadParseCommand::IsQRCode(codeString); + + VerifyOrReturnError(codeString.length() > mPos && mPos >= 0, CHIP_ERROR_INVALID_STRING_LENGTH); + VerifyOrReturnError(!isQRCode, CHIP_ERROR_NOT_IMPLEMENTED); + + ChipLogProgress(SetupPayload, "%s is %sVALID at position %u", codeString.c_str(), Verify(codeString) ? "" : "IN", mPos); + + return CHIP_NO_ERROR; +} + +bool SetupPayloadVerhoeffVerify::Verify(std::string codeString) +{ + char checkChar; + bool result; + + checkChar = codeString.at(mPos); + codeString.erase(mPos, mPos); + ChipLogDetail(SetupPayload, "Verifying Manual Code: %s", codeString.c_str()); + + result = Verhoeff10::ValidateCheckChar(checkChar, codeString.c_str()); + + return result; +} + +CHIP_ERROR SetupPayloadVerhoeffGenerate::Run() +{ + std::string codeString(mSetupCode); + char generatedChar; + + ReturnErrorOnFailure(GenerateChar(codeString, generatedChar)); + ChipLogProgress(SetupPayload, "Generated Char: %c", generatedChar); + + return CHIP_NO_ERROR; +} + +CHIP_ERROR SetupPayloadVerhoeffGenerate::GenerateChar(std::string codeString, char & generatedChar) +{ + bool isQRCode = SetupPayloadParseCommand::IsQRCode(codeString); + + VerifyOrReturnError(!isQRCode, CHIP_ERROR_INVALID_ARGUMENT); + + ChipLogDetail(SetupPayload, "Generating Character for: %s", codeString.c_str()); + generatedChar = Verhoeff10::ComputeCheckChar(codeString.c_str()); + + return CHIP_NO_ERROR; +} +// diff --git a/examples/chip-tool/commands/payload/SetupPayloadVerhoeff.h b/examples/chip-tool/commands/payload/SetupPayloadVerhoeff.h new file mode 100644 index 00000000000000..0f531e3dd822bb --- /dev/null +++ b/examples/chip-tool/commands/payload/SetupPayloadVerhoeff.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020 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 SetupPayloadVerhoeffVerify : public Command +{ +public: + SetupPayloadVerhoeffVerify() : Command("verhoeff-verify") + { + AddArgument("payload", &mSetupCode); + AddArgument("position", 0, UINT8_MAX, &mPos); + } + CHIP_ERROR Run() override; + +private: + char * mSetupCode; + uint8_t mPos; + bool Verify(std::string codeString); +}; + +class SetupPayloadVerhoeffGenerate : public Command +{ +public: + SetupPayloadVerhoeffGenerate() : Command("verhoeff-generate") { AddArgument("payload", &mSetupCode); } + CHIP_ERROR Run() override; + +private: + char * mSetupCode; + CHIP_ERROR GenerateChar(std::string codeString, char & generatedChar); +};