15
15
#include < Arduino.h>
16
16
17
17
#include " IoTCloudMessageDecoder.h"
18
+ #include < cbor/utils/decoder.h>
18
19
#include < AIoTC_Config.h>
19
20
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
21
/* *****************************************************************************
43
22
MESSAGE DECODE FUNCTIONS
44
23
******************************************************************************/
45
24
46
25
MessageDecoder::Status ThingUpdateCommandDecoder::decode (CborValue* iter, Message *msg) {
47
26
ThingUpdateCmd * thingCommand = (ThingUpdateCmd *) msg;
48
27
28
+ size_t dest_size = sizeof (thingCommand->params .thing_id );
29
+
49
30
// 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) {
51
34
return MessageDecoder::Status::Error;
52
35
}
53
36
@@ -57,8 +40,14 @@ MessageDecoder::Status ThingUpdateCommandDecoder::decode(CborValue* iter, Messag
57
40
MessageDecoder::Status ThingDetachCommandDecoder::decode (CborValue* iter, Message *msg) {
58
41
ThingDetachCmd * thingCommand = (ThingDetachCmd *) msg;
59
42
43
+ size_t dest_size = sizeof (thingCommand->params .thing_id );
44
+
45
+
60
46
// 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) {
62
51
return MessageDecoder::Status::Error;
63
52
}
64
53
@@ -125,33 +114,57 @@ MessageDecoder::Status LastValuesUpdateCommandDecoder::decode(CborValue* iter, M
125
114
}
126
115
127
116
MessageDecoder::Status OtaUpdateCommandDecoder::decode (CborValue* iter, Message *msg) {
128
- CborError error = CborNoError;
129
117
OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) msg;
118
+ size_t dest_size = sizeof (ota->params .id );
130
119
131
120
// 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) {
133
127
return MessageDecoder::Status::Error;
134
128
}
135
129
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
+ }
137
142
138
- if ((error != CborNoError) || !copyCBORStringToArray (iter, ota->params .url , sizeof (ota->params .url ))) {
143
+ // decoding parameter initialSha256
144
+ if (cbor_value_advance (iter) != CborNoError) {
139
145
return MessageDecoder::Status::Error;
140
146
}
141
147
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
+ }
143
156
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) {
147
159
return MessageDecoder::Status::Error;
148
160
}
149
161
150
- error = cbor_value_advance (iter );
162
+ dest_size = sizeof (ota-> params . finalSha256 );
151
163
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 )) {
155
168
return MessageDecoder::Status::Error;
156
169
}
157
170
0 commit comments