17
17
#include " IoTCloudMessageDecoder.h"
18
18
#include < AIoTC_Config.h>
19
19
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
-
42
20
/* *****************************************************************************
43
21
MESSAGE DECODE FUNCTIONS
44
22
******************************************************************************/
@@ -47,7 +25,14 @@ MessageDecoder::Status ThingUpdateCommandDecoder::decode(CborValue* iter, Messag
47
25
ThingUpdateCmd * thingCommand = (ThingUpdateCmd *) msg;
48
26
49
27
// 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) {
51
36
return MessageDecoder::Status::Error;
52
37
}
53
38
@@ -58,7 +43,14 @@ MessageDecoder::Status ThingDetachCommandDecoder::decode(CborValue* iter, Messag
58
43
ThingDetachCmd * thingCommand = (ThingDetachCmd *) msg;
59
44
60
45
// 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) {
62
54
return MessageDecoder::Status::Error;
63
55
}
64
56
@@ -125,33 +117,66 @@ MessageDecoder::Status LastValuesUpdateCommandDecoder::decode(CborValue* iter, M
125
117
}
126
118
127
119
MessageDecoder::Status OtaUpdateCommandDecoder::decode (CborValue* iter, Message *msg) {
128
- CborError error = CborNoError;
129
120
OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) msg;
130
121
131
122
// 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)) {
133
128
return MessageDecoder::Status::Error;
134
129
}
135
130
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
+ }
137
135
138
- if ((error != CborNoError) || ! copyCBORStringToArray (iter, ota-> params . url , sizeof (ota-> params . url )) ) {
136
+ if ( cbor_value_advance (iter) != CborNoError) {
139
137
return MessageDecoder::Status::Error;
140
138
}
141
139
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 );
143
157
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)) {
147
159
return MessageDecoder::Status::Error;
148
160
}
149
161
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
+ }
151
177
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) {
155
180
return MessageDecoder::Status::Error;
156
181
}
157
182
0 commit comments