Skip to content

Remove copyCBORStringToArray and copyCBORByteToArray in favor of explcit instructions #544

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

Closed
Closed
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
97 changes: 61 additions & 36 deletions src/cbor/IoTCloudMessageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@
#include "IoTCloudMessageDecoder.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
******************************************************************************/
Expand All @@ -47,7 +25,14 @@
ThingUpdateCmd * thingCommand = (ThingUpdateCmd *) msg;

// 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_value_is_text_string(iter)) {
return MessageDecoder::Status::Error;
}

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

if(_cbor_value_copy_string(
iter, thingCommand->params.thing_id, &dest_size, NULL) != CborNoError) {
return MessageDecoder::Status::Error;
}

Expand All @@ -58,7 +43,14 @@
ThingDetachCmd * thingCommand = (ThingDetachCmd *) msg;

// 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_value_is_text_string(iter)) {
return MessageDecoder::Status::Error;
}

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

if(_cbor_value_copy_string(
iter, thingCommand->params.thing_id, &dest_size, NULL) != CborNoError) {
return MessageDecoder::Status::Error;
}

Expand Down Expand Up @@ -125,33 +117,66 @@
}

MessageDecoder::Status OtaUpdateCommandDecoder::decode(CborValue* iter, Message *msg) {
CborError error = CborNoError;
OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) msg;

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

// decoding parameter id
size_t dest_size = sizeof(ota->params.id);

if (!cbor_value_is_byte_string(iter)) {
return MessageDecoder::Status::Error;
}

error = cbor_value_advance(iter);
// 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(iter, ota->params.id, &dest_size, NULL) != CborNoError) {
return MessageDecoder::Status::Error;

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

View check run for this annotation

Codecov / codecov/patch

src/cbor/IoTCloudMessageDecoder.cpp#L133

Added line #L133 was not covered by tests
}

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

error = cbor_value_advance(iter);
// decoding parameter url
dest_size = sizeof(ota->params.url);

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

error = cbor_value_advance(iter);
if(_cbor_value_copy_string(iter, ota->params.url, &dest_size, NULL) != CborNoError) {
return MessageDecoder::Status::Error;

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

View check run for this annotation

Codecov / codecov/patch

src/cbor/IoTCloudMessageDecoder.cpp#L148

Added line #L148 was not covered by tests
}

if(cbor_value_advance(iter) != CborNoError) {
return MessageDecoder::Status::Error;

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

View check run for this annotation

Codecov / codecov/patch

src/cbor/IoTCloudMessageDecoder.cpp#L152

Added line #L152 was not covered by tests
}

// decoding parameter initialSha256
dest_size = sizeof(ota->params.initialSha256);

if (!cbor_value_is_byte_string(iter)) {
return MessageDecoder::Status::Error;
}

// 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(iter, ota->params.initialSha256, &dest_size, NULL) != CborNoError) {
return MessageDecoder::Status::Error;

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

View check run for this annotation

Codecov / codecov/patch

src/cbor/IoTCloudMessageDecoder.cpp#L164

Added line #L164 was not covered by tests
}

if(cbor_value_advance(iter) != CborNoError) {
return MessageDecoder::Status::Error;

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

View check run for this annotation

Codecov / codecov/patch

src/cbor/IoTCloudMessageDecoder.cpp#L168

Added line #L168 was not covered by tests
}

// decoding parameter finalSha256
dest_size = sizeof(ota->params.finalSha256);

if (!cbor_value_is_byte_string(iter)) {
return MessageDecoder::Status::Error;
}

if ((error != CborNoError) ||
copyCBORByteToArray(iter, ota->params.finalSha256,
sizeof(ota->params.finalSha256)) != sizeof(ota->params.finalSha256)) {
// 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(iter, ota->params.finalSha256, &dest_size, NULL) != CborNoError) {
return MessageDecoder::Status::Error;
}

Expand Down
Loading