1515#include < Arduino.h>
1616
1717#include " IoTCloudMessageDecoder.h"
18+ #include < cbor/utils/decoder.h>
1819#include < AIoTC_Config.h>
1920
20- static inline bool copyCBORStringToArray (CborValue * param, char * dest, size_t dest_size) {
21- if (cbor_value_is_text_string (param)) {
22- // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
23- if (_cbor_value_copy_string (param, dest, &dest_size, NULL ) == CborNoError) {
24- return true ;
25- }
26- }
27-
28- return false ;
29- }
30-
31- static inline size_t copyCBORByteToArray (CborValue * param, uint8_t * dest, size_t dest_size) {
32- if (cbor_value_is_byte_string (param)) {
33- // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
34- if (_cbor_value_copy_string (param, dest, &dest_size, NULL ) == CborNoError) {
35- return dest_size;
36- }
37- }
38-
39- return 0 ;
40- }
41-
4221/* *****************************************************************************
4322 MESSAGE DECODE FUNCTIONS
4423 ******************************************************************************/
4524
4625MessageDecoder::Status ThingUpdateCommandDecoder::decode (CborValue* iter, Message *msg) {
4726 ThingUpdateCmd * thingCommand = (ThingUpdateCmd *) msg;
4827
28+ size_t dest_size = sizeof (thingCommand->params .thing_id );
29+
4930 // Message is composed of a single parameter, a string (thing_id)
50- if (!copyCBORStringToArray (iter, thingCommand->params .thing_id , sizeof (thingCommand->params .thing_id ))) {
31+ if (cbor::utils::copyCBORStringToArray (
32+ iter, thingCommand->params .thing_id ,
33+ dest_size) == MessageDecoder::Status::Error) {
5134 return MessageDecoder::Status::Error;
5235 }
5336
@@ -57,8 +40,14 @@ MessageDecoder::Status ThingUpdateCommandDecoder::decode(CborValue* iter, Messag
5740MessageDecoder::Status ThingDetachCommandDecoder::decode (CborValue* iter, Message *msg) {
5841 ThingDetachCmd * thingCommand = (ThingDetachCmd *) msg;
5942
43+ size_t dest_size = sizeof (thingCommand->params .thing_id );
44+
45+
6046 // Message is composed of a single parameter, a string (thing_id)
61- if (!copyCBORStringToArray (iter, thingCommand->params .thing_id , sizeof (thingCommand->params .thing_id ))) {
47+ if (cbor::utils::copyCBORStringToArray (
48+ iter,
49+ thingCommand->params .thing_id ,
50+ dest_size) == MessageDecoder::Status::Error) {
6251 return MessageDecoder::Status::Error;
6352 }
6453
@@ -125,33 +114,57 @@ MessageDecoder::Status LastValuesUpdateCommandDecoder::decode(CborValue* iter, M
125114}
126115
127116MessageDecoder::Status OtaUpdateCommandDecoder::decode (CborValue* iter, Message *msg) {
128- CborError error = CborNoError;
129117 OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) msg;
118+ size_t dest_size = sizeof (ota->params .id );
130119
131120 // Message is composed 4 parameters: id, url, initialSha, finalSha
132- if (!copyCBORByteToArray (iter, ota->params .id , sizeof (ota->params .id ))) {
121+
122+ // decoding parameter id
123+ if (cbor::utils::copyCBORByteToArray (
124+ iter,
125+ ota->params .id ,
126+ dest_size) == MessageDecoder::Status::Error) {
133127 return MessageDecoder::Status::Error;
134128 }
135129
136- error = cbor_value_advance (iter);
130+ // decoding parameter url
131+ if (cbor_value_advance (iter) != CborNoError) {
132+ return MessageDecoder::Status::Error;
133+ }
134+
135+ dest_size = sizeof (ota->params .url );
136+
137+ if (cbor::utils::copyCBORStringToArray (iter,
138+ ota->params .url ,
139+ dest_size) == MessageDecoder::Status::Error) {
140+ return MessageDecoder::Status::Error;
141+ }
137142
138- if ((error != CborNoError) || !copyCBORStringToArray (iter, ota->params .url , sizeof (ota->params .url ))) {
143+ // decoding parameter initialSha256
144+ if (cbor_value_advance (iter) != CborNoError) {
139145 return MessageDecoder::Status::Error;
140146 }
141147
142- error = cbor_value_advance (iter);
148+ dest_size = sizeof (ota->params .initialSha256 );
149+
150+ if (cbor::utils::copyCBORByteToArray (iter,
151+ ota->params .initialSha256 ,
152+ dest_size) == MessageDecoder::Status::Error ||
153+ dest_size != sizeof (ota->params .initialSha256 )) {
154+ return MessageDecoder::Status::Error;
155+ }
143156
144- if ((error != CborNoError) ||
145- copyCBORByteToArray (iter, ota->params .initialSha256 ,
146- sizeof (ota->params .initialSha256 )) != sizeof (ota->params .initialSha256 )) {
157+ // decoding parameter finalSha256
158+ if (cbor_value_advance (iter) != CborNoError) {
147159 return MessageDecoder::Status::Error;
148160 }
149161
150- error = cbor_value_advance (iter );
162+ dest_size = sizeof (ota-> params . finalSha256 );
151163
152- if ((error != CborNoError) ||
153- copyCBORByteToArray (iter, ota->params .finalSha256 ,
154- sizeof (ota->params .finalSha256 )) != sizeof (ota->params .finalSha256 )) {
164+ if (cbor::utils::copyCBORByteToArray (iter,
165+ ota->params .finalSha256 ,
166+ dest_size) == MessageDecoder::Status::Error ||
167+ dest_size != sizeof (ota->params .finalSha256 )) {
155168 return MessageDecoder::Status::Error;
156169 }
157170
0 commit comments