Skip to content

Commit

Permalink
Changed char span size for codes buffers and fixed builds
Browse files Browse the repository at this point in the history
* Added documentation for OnboardingCodesUtil methods
* Changed char span sizes returned by codes encoding methods
to not include null terminator
* Aligned examples using GetQRCode and GetManualPairingCode
to the new API.
  • Loading branch information
kkasperczyk-no committed May 12, 2022
1 parent 2c3604b commit 1352528
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 32 deletions.
5 changes: 4 additions & 1 deletion examples/all-clusters-app/esp32/main/DeviceWithDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,11 @@ esp_err_t InitM5Stack(std::string qrCodeText)

void InitDeviceDisplay()
{
std::string qrCodeText;
// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan qrCodeText(qrCodeBuffer);

// Get QR Code and emulate its content using NFC tag
GetQRCode(qrCodeText, chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE));

// Initialize the display device.
Expand Down
6 changes: 4 additions & 2 deletions examples/chef/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ const char * TAG = "chef-app";
#if CONFIG_HAVE_DISPLAY
void printQRCode()
{
std::string qrCodeText;
// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan qrCodeText(qrCodeBuffer);

GetQRCode(qrCodeText, chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE));

Expand All @@ -153,7 +155,7 @@ void printQRCode()
ScreenManager::Init();

ESP_LOGI(TAG, "Opening QR code screen");
ESP_LOGI(TAG, "QR CODE Text: '%s'", qrCodeText.c_str());
ESP_LOGI(TAG, "QR CODE Text: '%s'", qrCodeText.data());
ScreenManager::PushScreen(chip::Platform::New<QRCodeScreen>(qrCodeText));
}
#endif // CONFIG_HAVE_DISPLAY
Expand Down
6 changes: 4 additions & 2 deletions examples/common/pigweed/rpc_services/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ class Device : public pw_rpc::nanopb::Device::Service<Device>
snprintf(response.serial_number, sizeof(response.serial_number), CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER);
}

std::string qrCodeText;
// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan qrCodeText(qrCodeBuffer);
if (GetQRCode(qrCodeText, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)) == CHIP_NO_ERROR)
{
snprintf(response.pairing_info.qr_code, sizeof(response.pairing_info.qr_code), "%s", qrCodeText.c_str());
snprintf(response.pairing_info.qr_code, sizeof(response.pairing_info.qr_code), "%s", qrCodeText.data());
GetQRCodeUrl(response.pairing_info.qr_code_url, sizeof(response.pairing_info.qr_code_url), qrCodeText);
response.has_pairing_info = true;
}
Expand Down
6 changes: 4 additions & 2 deletions examples/light-switch-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,13 @@ CHIP_ERROR AppTask::Init()

// Print setup info on LCD if available
#ifdef DISPLAY_ENABLED
std::string QRCode;
// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan QRCode(qrCodeBuffer);

if (GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)) == CHIP_NO_ERROR)
{
LCDWriteQRCode((uint8_t *) QRCode.c_str());
LCDWriteQRCode((uint8_t *) QRCode.data());
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions examples/lighting-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,13 @@ CHIP_ERROR AppTask::Init()

// Print setup info on LCD if available
#ifdef DISPLAY_ENABLED
std::string QRCode;
// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan QRCode(qrCodeBuffer);

if (GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)) == CHIP_NO_ERROR)
{
LCDWriteQRCode((uint8_t *) QRCode.c_str());
LCDWriteQRCode((uint8_t *) QRCode.data());
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions examples/lock-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ CHIP_ERROR AppTask::Init()

// Print setup info on LCD if available
#ifdef DISPLAY_ENABLED
std::string QRCode;
// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan QRCode(qrCodeBuffer);

if (GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)) == CHIP_NO_ERROR)
{
LCDWriteQRCode((uint8_t *) QRCode.c_str());
LCDWriteQRCode((uint8_t *) QRCode.data());
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions examples/ota-requestor-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ CHIP_ERROR AppTask::Init()

// Print setup info on LCD if available
#ifdef DISPLAY_ENABLED
std::string QRCode;
// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan QRCode(qrCodeBuffer);

if (GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)) == CHIP_NO_ERROR)
{
LCDWriteQRCode((uint8_t *) QRCode.c_str());
LCDWriteQRCode((uint8_t *) QRCode.data());
}
else
{
Expand Down
5 changes: 4 additions & 1 deletion examples/window-app/efr32/include/WindowAppImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ class WindowAppImpl : public WindowApp
QueueHandle_t mQueue = nullptr;
LEDWidget mStatusLED;
LEDWidget mActionLED;
std::string mQRCode;

// Get QR Code and emulate its content using NFC tag
char mQRCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan mQRCode(mQRCodeBuffer);
Timer mIconTimer;
LcdIcon mIcon = LcdIcon::None;
};
2 changes: 1 addition & 1 deletion examples/window-app/efr32/src/WindowAppImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ void WindowAppImpl::UpdateLCD()
{
if (GetQRCode(mQRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)) == CHIP_NO_ERROR)
{
LCDWriteQRCode((uint8_t *) mQRCode.c_str());
LCDWriteQRCode((uint8_t *) mQRCode.data());
}
}
#endif // QR_CODE_ENABLED
Expand Down
8 changes: 3 additions & 5 deletions src/app/server/OnboardingCodesUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void PrintOnboardingCodes(chip::RendezvousInformationFlags aRendezvousFlags)

void PrintOnboardingCodes(const chip::PayloadContents & payload)
{
char payloadBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength];
char payloadBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan qrCode(payloadBuffer);

if (GetQRCode(qrCode, payload) == CHIP_NO_ERROR)
Expand Down Expand Up @@ -86,7 +86,7 @@ void PrintOnboardingCodes(const chip::PayloadContents & payload)
void ShareQRCodeOverNFC(chip::RendezvousInformationFlags aRendezvousFlags)
{
// Get QR Code and emulate its content using NFC tag
char payloadBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength];
char payloadBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan qrCode(payloadBuffer);

ReturnOnFailure(GetQRCode(qrCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)));
Expand Down Expand Up @@ -154,8 +154,6 @@ CHIP_ERROR GetQRCode(chip::MutableCharSpan & aQRCode, chip::RendezvousInformatio

CHIP_ERROR GetQRCode(chip::MutableCharSpan & aQRCode, const chip::PayloadContents & payload)
{
// TODO: Usage of STL will significantly increase the image size, this should be changed to more efficient method for
// generating payload
CHIP_ERROR err = chip::QRCodeBasicSetupPayloadGenerator(payload).payloadBase38Representation(aQRCode);
if (err != CHIP_NO_ERROR)
{
Expand All @@ -166,7 +164,7 @@ CHIP_ERROR GetQRCode(chip::MutableCharSpan & aQRCode, const chip::PayloadContent
return CHIP_NO_ERROR;
}

CHIP_ERROR GetQRCodeUrl(char * aQRCodeUrl, size_t aUrlMaxSize, chip::MutableCharSpan & aQRCode)
CHIP_ERROR GetQRCodeUrl(char * aQRCodeUrl, size_t aUrlMaxSize, const chip::CharSpan & aQRCode)
{
VerifyOrReturnError(aQRCodeUrl, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(aUrlMaxSize >= (strlen(kQrCodeBaseUrl) + strlen(kUrlDataAssignmentPhrase) + aQRCode.size() + 1),
Expand Down
31 changes: 30 additions & 1 deletion src/app/server/OnboardingCodesUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,40 @@
void PrintOnboardingCodes(chip::RendezvousInformationFlags aRendezvousFlags);
void PrintOnboardingCodes(const chip::PayloadContents & payload);
void ShareQRCodeOverNFC(chip::RendezvousInformationFlags aRendezvousFlags);

/**
* Creates a null-terminated QR code from the payload created based on rendezvous flag information.
*
* The resulting size of the QR code span will not include the null terminator.
*/
CHIP_ERROR GetQRCode(chip::MutableCharSpan & aQRCode, chip::RendezvousInformationFlags aRendezvousFlags);

/**
* Creates a null-terminated QR code based on the provided payload.
*
* The resulting size of the QR code span will not include the null terminator.
*/
CHIP_ERROR GetQRCode(chip::MutableCharSpan & aQRCode, const chip::PayloadContents & payload);
CHIP_ERROR GetQRCodeUrl(char * aQRCodeUrl, size_t aUrlMaxSize, chip::MutableCharSpan & aQRCode);

/**
* Creates a null-terminated QR code url.
*/
CHIP_ERROR GetQRCodeUrl(char * aQRCodeUrl, size_t aUrlMaxSize, const chip::CharSpan & aQRCode);

/**
* Creates a null-terminated manual pairing code from the payload created based on rendezvous flag information.
*
* The resulting size of the manual pairing code span will not include the null terminator.
*/
CHIP_ERROR GetManualPairingCode(chip::MutableCharSpan & aManualPairingCode, chip::RendezvousInformationFlags aRendezvousFlags);

/**
* Creates a null-terminated manual pairing code based on the provided payload.
*
* The resulting size of the manual pairing code span will not include the null terminator.
*/
CHIP_ERROR GetManualPairingCode(chip::MutableCharSpan & aManualPairingCode, const chip::PayloadContents & payload);

CHIP_ERROR GetPayloadContents(chip::PayloadContents & aPayload, chip::RendezvousInformationFlags aRendezvousFlags);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/controller/CommissioningWindowOpener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ CHIP_ERROR CommissioningWindowOpener::OpenCommissioningWindowInternal(Operationa
ReturnErrorOnFailure(cluster.InvokeCommand(request, this, OnOpenCommissioningWindowSuccess,
OnOpenCommissioningWindowFailure, MakeOptional(kTimedInvokeTimeoutMs)));

char payloadBuffer[QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength];
char payloadBuffer[QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];

MutableCharSpan manualCode(payloadBuffer);
ReturnErrorOnFailure(ManualSetupPayloadGenerator(mSetupPayload).payloadDecimalStringRepresentation(manualCode));
Expand Down
25 changes: 17 additions & 8 deletions src/lib/shell/commands/OnboardingCodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,55 @@ namespace Shell {
static CHIP_ERROR GetOnboardingQRCode(bool printHeader, chip::RendezvousInformationFlags aRendezvousFlags)
{
streamer_t * sout = streamer_get();
std::string QRCode;

// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan QRCode(qrCodeBuffer);

if (printHeader)
{
streamer_printf(sout, "QRCode: ");
}
ReturnErrorOnFailure(GetQRCode(QRCode, aRendezvousFlags));
streamer_printf(sout, "%s\r\n", QRCode.c_str());
streamer_printf(sout, "%s\r\n", QRCode.data());
return CHIP_NO_ERROR;
}

static CHIP_ERROR GetOnboardingQRCodeUrl(bool printHeader, chip::RendezvousInformationFlags aRendezvousFlags)
{
streamer_t * sout = streamer_get();
std::string QRCode;

// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan QRCode(qrCodeBuffer);

if (printHeader)
{
streamer_printf(sout, "QRCodeUrl: ");
}
ReturnErrorOnFailure(GetQRCode(QRCode, aRendezvousFlags));

char qrCodeBuffer[CHIP_SHELL_MAX_BUFFER_SIZE];
char qrCodeUrlBuffer[CHIP_SHELL_MAX_BUFFER_SIZE];

ReturnErrorOnFailure(GetQRCodeUrl(qrCodeBuffer, sizeof(qrCodeBuffer), QRCode));
streamer_printf(sout, "%s\r\n", qrCodeBuffer);
ReturnErrorOnFailure(GetQRCodeUrl(qrCodeUrlBuffer, sizeof(qrCodeUrlBuffer), QRCode));
streamer_printf(sout, "%s\r\n", qrCodeUrlBuffer);
return CHIP_NO_ERROR;
}

static CHIP_ERROR GetOnboardingManualPairingCode(bool printHeader, chip::RendezvousInformationFlags aRendezvousFlags)
{
streamer_t * sout = streamer_get();
std::string manualPairingCode;

// Create buffer for manual pariting code that can fit max size + check digit + null terminator.
char manualPairingCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kManualSetupLongCodeCharLength + 1];
chip::MutableCharSpan manualPairingCode(manualPairingCodeBuffer);

if (printHeader)
{
streamer_printf(sout, "ManualPairingCode: ");
}
ReturnErrorOnFailure(GetManualPairingCode(manualPairingCode, aRendezvousFlags));
streamer_printf(sout, "%s\r\n", manualPairingCode.c_str());
streamer_printf(sout, "%s\r\n", manualPairingCode.data());
return CHIP_NO_ERROR;
}

Expand Down
2 changes: 2 additions & 0 deletions src/setup_payload/Base38Encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ CHIP_ERROR base38Encode(ByteSpan in_buf, MutableCharSpan & out_buf)
if (out_idx < out_buf.size())
{
out_buf.data()[out_idx] = '\0';
// Reduce output span size to not include null-terminator.
out_buf.reduce_size(out_buf.size() - 1);
}
else
{
Expand Down
10 changes: 8 additions & 2 deletions src/setup_payload/Base38Encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@

namespace chip {

// out_buf is null-terminated on success
/*
* The out_buf is null-terminated on success.
*
* The resulting size of the out_buf span will not include the null terminator.
*/
CHIP_ERROR base38Encode(ByteSpan in_buf, MutableCharSpan & out_buf);

// returns size needed to store encoded string given number of input bytes
/*
* Returns size needed to store encoded string given number of input bytes including null terminator.
*/
size_t base38EncodedLength(size_t num_bytes);

} // namespace chip
3 changes: 3 additions & 0 deletions src/setup_payload/ManualSetupPayloadGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ CHIP_ERROR ManualSetupPayloadGenerator::payloadDecimalStringRepresentation(Mutab
int checkDigit = Verhoeff10::CharToVal(Verhoeff10::ComputeCheckChar(outBuffer.data()));
ReturnErrorOnFailure(decimalStringWithPadding(outBuffer.SubSpan(offset, 2), static_cast<uint32_t>(checkDigit)));

// Reduce size of outBuffer by 1 to not include null terminator.
outBuffer.reduce_size(outBuffer.size() - 1);

return CHIP_NO_ERROR;
}

Expand Down
2 changes: 2 additions & 0 deletions src/setup_payload/QRCodeSetupPayloadGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class QRCodeBasicSetupPayloadGenerator
* This function is called to encode the binary data of a payload to a
* base38 null-terminated string.
*
* The resulting size of the outBuffer span will not include the null terminator.
*
* @param[out] outBuffer
* The buffer to copy the base38 to.
*
Expand Down

0 comments on commit 1352528

Please sign in to comment.