-
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.
Add Verhoeff algorithm check/generate in chip-tool (#13048)
- Loading branch information
Showing
5 changed files
with
135 additions
and
2 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
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
76 changes: 76 additions & 0 deletions
76
examples/chip-tool/commands/payload/SetupPayloadVerhoeff.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,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 <setup_payload/SetupPayload.h> | ||
#include <support/verhoeff/Verhoeff.h> | ||
|
||
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; | ||
} | ||
// |
49 changes: 49 additions & 0 deletions
49
examples/chip-tool/commands/payload/SetupPayloadVerhoeff.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,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 <setup_payload/SetupPayload.h> | ||
|
||
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); | ||
}; |