Thanks to visit codestin.com
Credit goes to github.com

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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 ThingUpdateCommandDecoder::decode(CborValue* iter, Messag
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 LastValuesUpdateCommandDecoder::decode(CborValue* iter, M
}

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;
}

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