diff --git a/src/setup_payload/ManualSetupPayloadGenerator.cpp b/src/setup_payload/ManualSetupPayloadGenerator.cpp index e8d7a6289c91a4..a1e26dbcb93073 100644 --- a/src/setup_payload/ManualSetupPayloadGenerator.cpp +++ b/src/setup_payload/ManualSetupPayloadGenerator.cpp @@ -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) || diff --git a/src/setup_payload/ManualSetupPayloadGenerator.h b/src/setup_payload/ManualSetupPayloadGenerator.h index 2d85e903035b49..716d6b6da047fe 100644 --- a/src/setup_payload/ManualSetupPayloadGenerator.h +++ b/src/setup_payload/ManualSetupPayloadGenerator.h @@ -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 diff --git a/src/setup_payload/QRCodeSetupPayloadGenerator.cpp b/src/setup_payload/QRCodeSetupPayloadGenerator.cpp index 86c8d44d605166..c8e7dc7fc1b2ad 100644 --- a/src/setup_payload/QRCodeSetupPayloadGenerator.cpp +++ b/src/setup_payload/QRCodeSetupPayloadGenerator.cpp @@ -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 bits(kTotalPayloadDataSizeInBytes + tlvDataLengthInBytes); diff --git a/src/setup_payload/QRCodeSetupPayloadGenerator.h b/src/setup_payload/QRCodeSetupPayloadGenerator.h index 091b6df5b459d8..c82aaa6ac71f77 100644 --- a/src/setup_payload/QRCodeSetupPayloadGenerator.h +++ b/src/setup_payload/QRCodeSetupPayloadGenerator.h @@ -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; }; /**