Skip to content

Commit

Permalink
[Payload] Add SetAllowInvalidPayload and SetForceShortCode methods to…
Browse files Browse the repository at this point in the history
… QRCodeSetupPayloadGenerator and ManualSetupPayloadGenerator
  • Loading branch information
vivien-apple committed Mar 28, 2022
1 parent 125e73c commit 89e097c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/setup_payload/ManualSetupPayloadGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ CHIP_ERROR ManualSetupPayloadGenerator::payloadDecimalStringRepresentation(Mutab
static_assert(kManualSetupChunk2PINCodeLsbitsLength + kManualSetupChunk3PINCodeMsbitsLength == kSetupPINCodeFieldLengthInBits,
"PIN code won't fit");

if (!mPayloadContents.isValidManualCode())
if (!mAllowInvalidPayload && !mPayloadContents.isValidManualCode())
{
ChipLogError(SetupPayload, "Failed encoding invalid payload");
return CHIP_ERROR_INVALID_ARGUMENT;
}

bool useLongCode = (mPayloadContents.commissioningFlow != CommissioningFlow::kStandard);
bool useLongCode = (mPayloadContents.commissioningFlow != CommissioningFlow::kStandard) && !mForceShortCode;

// Add two for the check digit and null terminator.
if ((useLongCode && outBuffer.size() < kManualSetupLongCodeCharLength + 2) ||
Expand Down
19 changes: 19 additions & 0 deletions src/setup_payload/ManualSetupPayloadGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ class ManualSetupPayloadGenerator
// Populates decimal string representation of the payload into outDecimalString.
// Wrapper for using std::string.
CHIP_ERROR payloadDecimalStringRepresentation(std::string & outDecimalString);

/**
* This function disable internal checks about the validity of the generated payload.
* It allows using the generator to generates invalid payloads.
* Defaults is false.
*/
void SetAllowInvalidPayload(bool allow) { mAllowInvalidPayload = allow; }

/**
* This function allow to forced the generation of a short code when the commissioning
* flow is not standard by ignoring the vendor id and product id informations but with
* the VID/PID present flag set.
* Defaults is false.
*/
void SetForceShortCode(bool useShort) { mForceShortCode = useShort; }

private:
bool mAllowInvalidPayload = false;
bool mForceShortCode = false;
};

} // namespace chip
2 changes: 1 addition & 1 deletion src/setup_payload/QRCodeSetupPayloadGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ CHIP_ERROR QRCodeSetupPayloadGenerator::payloadBase38Representation(std::string
{
size_t tlvDataLengthInBytes = 0;

VerifyOrReturnError(mPayload.isValidQRCodePayload(), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(mAllowInvalidPayload || mPayload.isValidQRCodePayload(), CHIP_ERROR_INVALID_ARGUMENT);
ReturnErrorOnFailure(generateTLVFromOptionalData(mPayload, tlvDataStart, tlvDataStartSize, tlvDataLengthInBytes));

std::vector<uint8_t> bits(kTotalPayloadDataSizeInBytes + tlvDataLengthInBytes);
Expand Down
9 changes: 9 additions & 0 deletions src/setup_payload/QRCodeSetupPayloadGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ class QRCodeSetupPayloadGenerator
*/
CHIP_ERROR payloadBase38Representation(std::string & base38Representation, uint8_t * tlvDataStart, uint32_t tlvDataStartSize);

/**
* This function disable internal checks about the validity of the generated payload.
* It allows using the generator to generates invalid payloads.
* Defaults is false.
*/
void SetAllowInvalidPayload(bool allow) { mAllowInvalidPayload = allow; }

private:
CHIP_ERROR generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvDataStart, uint32_t maxLen,
size_t & tlvDataLengthInBytes);

bool mAllowInvalidPayload = false;
};

/**
Expand Down

0 comments on commit 89e097c

Please sign in to comment.