1717#include " IoTCloudMessageDecoder.h"
1818#include < AIoTC_Config.h>
1919
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-
4220/* *****************************************************************************
4321 MESSAGE DECODE FUNCTIONS
4422 ******************************************************************************/
@@ -47,7 +25,14 @@ MessageDecoder::Status ThingUpdateCommandDecoder::decode(CborValue* iter, Messag
4725 ThingUpdateCmd * thingCommand = (ThingUpdateCmd *) msg;
4826
4927 // Message is composed of a single parameter, a string (thing_id)
50- if (!copyCBORStringToArray (iter, thingCommand->params .thing_id , sizeof (thingCommand->params .thing_id ))) {
28+ if (!cbor_value_is_text_string (iter)) {
29+ return MessageDecoder::Status::Error;
30+ }
31+
32+ size_t dest_size = sizeof (thingCommand->params .thing_id );
33+
34+ if (_cbor_value_copy_string (
35+ iter, thingCommand->params .thing_id , &dest_size, NULL ) != CborNoError) {
5136 return MessageDecoder::Status::Error;
5237 }
5338
@@ -58,7 +43,14 @@ MessageDecoder::Status ThingDetachCommandDecoder::decode(CborValue* iter, Messag
5843 ThingDetachCmd * thingCommand = (ThingDetachCmd *) msg;
5944
6045 // Message is composed of a single parameter, a string (thing_id)
61- if (!copyCBORStringToArray (iter, thingCommand->params .thing_id , sizeof (thingCommand->params .thing_id ))) {
46+ if (!cbor_value_is_text_string (iter)) {
47+ return MessageDecoder::Status::Error;
48+ }
49+
50+ size_t dest_size = sizeof (thingCommand->params .thing_id );
51+
52+ if (_cbor_value_copy_string (
53+ iter, thingCommand->params .thing_id , &dest_size, NULL ) != CborNoError) {
6254 return MessageDecoder::Status::Error;
6355 }
6456
@@ -125,33 +117,66 @@ MessageDecoder::Status LastValuesUpdateCommandDecoder::decode(CborValue* iter, M
125117}
126118
127119MessageDecoder::Status OtaUpdateCommandDecoder::decode (CborValue* iter, Message *msg) {
128- CborError error = CborNoError;
129120 OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) msg;
130121
131122 // Message is composed 4 parameters: id, url, initialSha, finalSha
132- if (!copyCBORByteToArray (iter, ota->params .id , sizeof (ota->params .id ))) {
123+
124+ // decoding parameter id
125+ size_t dest_size = sizeof (ota->params .id );
126+
127+ if (!cbor_value_is_byte_string (iter)) {
133128 return MessageDecoder::Status::Error;
134129 }
135130
136- error = cbor_value_advance (iter);
131+ // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
132+ if (_cbor_value_copy_string (iter, ota->params .id , &dest_size, NULL ) != CborNoError) {
133+ return MessageDecoder::Status::Error;
134+ }
137135
138- if ((error != CborNoError) || ! copyCBORStringToArray (iter, ota-> params . url , sizeof (ota-> params . url )) ) {
136+ if ( cbor_value_advance (iter) != CborNoError) {
139137 return MessageDecoder::Status::Error;
140138 }
141139
142- error = cbor_value_advance (iter);
140+ if (!cbor_value_is_text_string (iter)) {
141+ return MessageDecoder::Status::Error;
142+ }
143+
144+ // decoding parameter url
145+ dest_size = sizeof (ota->params .url );
146+
147+ if (_cbor_value_copy_string (iter, ota->params .url , &dest_size, NULL ) != CborNoError) {
148+ return MessageDecoder::Status::Error;
149+ }
150+
151+ if (cbor_value_advance (iter) != CborNoError) {
152+ return MessageDecoder::Status::Error;
153+ }
154+
155+ // decoding parameter initialSha256
156+ dest_size = sizeof (ota->params .initialSha256 );
143157
144- if ((error != CborNoError) ||
145- copyCBORByteToArray (iter, ota->params .initialSha256 ,
146- sizeof (ota->params .initialSha256 )) != sizeof (ota->params .initialSha256 )) {
158+ if (!cbor_value_is_byte_string (iter)) {
147159 return MessageDecoder::Status::Error;
148160 }
149161
150- error = cbor_value_advance (iter);
162+ // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
163+ if (_cbor_value_copy_string (iter, ota->params .initialSha256 , &dest_size, NULL ) != CborNoError) {
164+ return MessageDecoder::Status::Error;
165+ }
166+
167+ if (cbor_value_advance (iter) != CborNoError) {
168+ return MessageDecoder::Status::Error;
169+ }
170+
171+ // decoding parameter finalSha256
172+ dest_size = sizeof (ota->params .finalSha256 );
173+
174+ if (!cbor_value_is_byte_string (iter)) {
175+ return MessageDecoder::Status::Error;
176+ }
151177
152- if ((error != CborNoError) ||
153- copyCBORByteToArray (iter, ota->params .finalSha256 ,
154- sizeof (ota->params .finalSha256 )) != sizeof (ota->params .finalSha256 )) {
178+ // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
179+ if (_cbor_value_copy_string (iter, ota->params .finalSha256 , &dest_size, NULL ) != CborNoError) {
155180 return MessageDecoder::Status::Error;
156181 }
157182
0 commit comments