From eb1c163b7a599658beacd60047c0eda14619f8c2 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Fri, 9 May 2025 11:21:28 +0200 Subject: [PATCH] Removing copyCBORStringToArray and copyCBORStringToArray utility functions in favor of cloud utils defined --- src/cbor/IoTCloudMessageDecoder.cpp | 85 +++++++++++++++++------------ 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/src/cbor/IoTCloudMessageDecoder.cpp b/src/cbor/IoTCloudMessageDecoder.cpp index e25962219..8a17ba1f3 100644 --- a/src/cbor/IoTCloudMessageDecoder.cpp +++ b/src/cbor/IoTCloudMessageDecoder.cpp @@ -15,30 +15,9 @@ #include #include "IoTCloudMessageDecoder.h" +#include #include -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 ******************************************************************************/ @@ -46,8 +25,12 @@ static inline size_t copyCBORByteToArray(CborValue * param, uint8_t * dest, size 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; } @@ -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; } @@ -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; }