Skip to content

Removing copyCBORStringToArray and copyCBORStringToArray functions #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extras/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ target_include_directories(
${cloudutils_SOURCE_DIR}/src/cbor
)

target_include_directories(
cloudutils INTERFACE
${cloudutils_SOURCE_DIR}/src/cbor/utils
)

target_include_directories(
cloudutils INTERFACE
${cloudutils_SOURCE_DIR}/src/interfaces
Expand Down
26 changes: 26 additions & 0 deletions extras/test/src/test_command_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,32 @@ SCENARIO("Test the decoding of command messages") {
}
}

/****************************************************************************/

WHEN("Decode the OtaUpdateCmdDown message without url field")
{
CommandDown command;

/*
DA 00010100 # tag(65792)
80 # array(1)
50 # bytes(16)
C73CB045F9C2434585AFFA36A307BFE7"\xC7<\xB0E\xF9\xC2CE\x85\xAF\xFA6\xA3\a\xBF\xE7"

*/
uint8_t const payload[] = {0xda, 0x00, 0x01, 0x01, 0x00, 0x81, 0x50, 0xc7,
0x3c, 0xb0, 0x45, 0xf9, 0xc2, 0x43, 0x45, 0x85,
0xaf, 0xfa, 0x36, 0xa3, 0x07, 0xbf, 0xe7};

size_t payload_length = sizeof(payload) / sizeof(uint8_t);
CBORMessageDecoder decoder;
MessageDecoder::Status err = decoder.decode((Message*)&command, payload, payload_length);

THEN("The decode is unsuccessful") {
REQUIRE(err == MessageDecoder::Status::Error);
}
}

/****************************************************************************/

WHEN("Decode the OtaBeginUp message")
Expand Down
85 changes: 49 additions & 36 deletions src/cbor/IoTCloudMessageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,22 @@
#include <Arduino.h>

#include "IoTCloudMessageDecoder.h"
#include <cbor/utils/decoder.h>
#include <AIoTC_Config.h>

static inline bool copyCBORStringToArray(CborValue * param, char * dest, size_t dest_size) {
if (cbor_value_is_text_string(param)) {
// NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
if(_cbor_value_copy_string(param, dest, &dest_size, NULL) == CborNoError) {
return true;
}
}

return false;
}

static inline size_t copyCBORByteToArray(CborValue * param, uint8_t * dest, size_t dest_size) {
if (cbor_value_is_byte_string(param)) {
// NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
if(_cbor_value_copy_string(param, dest, &dest_size, NULL) == CborNoError) {
return dest_size;
}
}

return 0;
}

/******************************************************************************
MESSAGE DECODE FUNCTIONS
******************************************************************************/

MessageDecoder::Status ThingUpdateCommandDecoder::decode(CborValue* iter, Message *msg) {
ThingUpdateCmd * thingCommand = (ThingUpdateCmd *) msg;

size_t dest_size = sizeof(thingCommand->params.thing_id);

// Message is composed of a single parameter, a string (thing_id)
if (!copyCBORStringToArray(iter, thingCommand->params.thing_id, sizeof(thingCommand->params.thing_id))) {
if (cbor::utils::copyCBORStringToArray(
iter, thingCommand->params.thing_id,
dest_size) == MessageDecoder::Status::Error) {
return MessageDecoder::Status::Error;
}

Expand All @@ -57,8 +40,14 @@
MessageDecoder::Status ThingDetachCommandDecoder::decode(CborValue* iter, Message *msg) {
ThingDetachCmd * thingCommand = (ThingDetachCmd *) msg;

size_t dest_size = sizeof(thingCommand->params.thing_id);


// Message is composed of a single parameter, a string (thing_id)
if (!copyCBORStringToArray(iter, thingCommand->params.thing_id, sizeof(thingCommand->params.thing_id))) {
if (cbor::utils::copyCBORStringToArray(
iter,
thingCommand->params.thing_id,
dest_size) == MessageDecoder::Status::Error) {
return MessageDecoder::Status::Error;
}

Expand Down Expand Up @@ -125,33 +114,57 @@
}

MessageDecoder::Status OtaUpdateCommandDecoder::decode(CborValue* iter, Message *msg) {
CborError error = CborNoError;
OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) msg;
size_t dest_size = sizeof(ota->params.id);

// Message is composed 4 parameters: id, url, initialSha, finalSha
if (!copyCBORByteToArray(iter, ota->params.id, sizeof(ota->params.id))) {

// decoding parameter id
if (cbor::utils::copyCBORByteToArray(
iter,
ota->params.id,
dest_size) == MessageDecoder::Status::Error) {
return MessageDecoder::Status::Error;
}

error = cbor_value_advance(iter);
// decoding parameter url
if(cbor_value_advance(iter) != CborNoError) {
return MessageDecoder::Status::Error;

Check warning on line 132 in src/cbor/IoTCloudMessageDecoder.cpp

View check run for this annotation

Codecov / codecov/patch

src/cbor/IoTCloudMessageDecoder.cpp#L132

Added line #L132 was not covered by tests
}

dest_size = sizeof(ota->params.url);

if (cbor::utils::copyCBORStringToArray(iter,
ota->params.url,
dest_size) == MessageDecoder::Status::Error) {
return MessageDecoder::Status::Error;
}

if ((error != CborNoError) || !copyCBORStringToArray(iter, ota->params.url, sizeof(ota->params.url))) {
// decoding parameter initialSha256
if(cbor_value_advance(iter) != CborNoError) {
return MessageDecoder::Status::Error;
}

error = cbor_value_advance(iter);
dest_size = sizeof(ota->params.initialSha256);

if (cbor::utils::copyCBORByteToArray(iter,
ota->params.initialSha256,
dest_size) == MessageDecoder::Status::Error ||
dest_size != sizeof(ota->params.initialSha256)) {
return MessageDecoder::Status::Error;
}

if ((error != CborNoError) ||
copyCBORByteToArray(iter, ota->params.initialSha256,
sizeof(ota->params.initialSha256)) != sizeof(ota->params.initialSha256)) {
// decoding parameter finalSha256
if(cbor_value_advance(iter) != CborNoError) {
return MessageDecoder::Status::Error;
}

error = cbor_value_advance(iter);
dest_size = sizeof(ota->params.finalSha256);

if ((error != CborNoError) ||
copyCBORByteToArray(iter, ota->params.finalSha256,
sizeof(ota->params.finalSha256)) != sizeof(ota->params.finalSha256)) {
if (cbor::utils::copyCBORByteToArray(iter,
ota->params.finalSha256,
dest_size) == MessageDecoder::Status::Error ||
dest_size != sizeof(ota->params.finalSha256)) {
return MessageDecoder::Status::Error;
}

Expand Down
Loading