@@ -185,47 +185,48 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
185
185
int propId; String propType, propName;
186
186
187
187
err = cbor_parser_init (payload, length, 0 , &parser, &dataArray);
188
- if (err) {
188
+ if (err) {
189
+ // Serial.println("Error in the parser creation.");
190
+ // Serial.println(cbor_error_string(err));
189
191
return ;
190
192
}
191
193
192
194
// parse cbor data only if a cbor array is received.
193
- if (dataArray.type != CborArrayType)
195
+ if (dataArray.type != CborArrayType)
194
196
return ;
195
197
196
198
// main loop through the cbor array elements
197
- while (!cbor_value_at_end (&dataArray)) {
198
- // parse cbor object
199
+ while (!cbor_value_at_end (&dataArray)) {
200
+
201
+ // parse cbor object
199
202
cbor_value_enter_container (&dataArray, &recursedMap);
203
+
200
204
CborType type = cbor_value_get_type (&recursedMap);
201
205
if (type != CborMapType) {
202
206
// stop the decode when 1st item thai is not a cbor map is found.
203
- CborError err = cbor_value_advance (&dataArray);
204
- if (err != CborNoError) {
205
- break ;
206
- }
207
+ cbor_value_advance (&dataArray);
207
208
continue ;
208
- } else {
209
209
210
+ } else {
211
+
210
212
while (!cbor_value_at_end (&recursedMap)) {
211
- // if the current element is not a cbor object as expected, skip it and go ahead.
212
- if (cbor_value_get_type (&recursedMap) != CborMapType) {
213
- CborError err = cbor_value_advance (&recursedMap);
214
- if (err != CborNoError) {
215
- break ;
216
- }
213
+
214
+ // if the current element is not a cbor object as expected, skip it and go ahead.
215
+ if (cbor_value_get_type (&recursedMap) != CborMapType) {
216
+ cbor_value_advance (&recursedMap);
217
217
continue ;
218
218
}
219
219
220
220
CborValue name;
221
221
// chechk for the if the a property has a name, if yes Cbor value name will properly updated
222
222
cbor_value_map_find_value (&recursedMap, " n" , &name);
223
- // check if a property has a name, of string type, if not do nothin and skip curtrent property
223
+
224
+ // check if a property has a name, of string type, if not do nothin and skip curtrent property
224
225
if (name.type != CborTextStringType) {
225
226
cbor_value_advance (&recursedMap);
226
227
continue ;
227
228
}
228
-
229
+
229
230
// get the property name from cbor map as char* string
230
231
char *nameVal; size_t nameValSize;
231
232
err = cbor_value_dup_text_string (&name, &nameVal, &nameValSize, NULL );
@@ -236,52 +237,67 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
236
237
propName = String (nameVal);
237
238
// used to avoid memory leaks (cbor_value_dup_text_string automatically perform a malloc)
238
239
free (nameVal);
239
- // Search for the index of the device property with that name
240
+
241
+ // Search for the index of the device property with that name
240
242
propId = findPropertyByName (propName);
241
243
// If property does not exist, skip it and do nothing.
242
244
if (propId < 0 ) {
243
245
cbor_value_advance (&recursedMap);
244
246
continue ;
245
247
}
246
-
248
+
247
249
ArduinoCloudPropertyGeneric* property = list.get (propId);
248
250
// Check for the property type, write method internally check for the permission
249
-
250
- cbor_value_map_find_value (&recursedMap, " v" , &propValue);
251
-
252
- if (propValue.type == CborDoubleType) {
253
- double val;
254
- // get the value of the property as a double
255
- cbor_value_get_double (&propValue, &val);
256
- reinterpret_cast <ArduinoCloudProperty<float >*>(property)->write ((float )val);
257
- }
258
- // if no key proper key was found, do nothing
259
- if (propValue.type == CborIntegerType) {
260
- int val;
261
- cbor_value_get_int (&propValue, &val);
262
- reinterpret_cast <ArduinoCloudProperty<int >*>(property)->write (val);
263
- }
264
- if (propValue.type == CborBooleanType) {
265
- bool val;
266
- cbor_value_get_boolean (&propValue, &val);
267
- reinterpret_cast <ArduinoCloudProperty<bool >*>(property)->write (val);
268
- }
269
- if (propValue.type == CborTextStringType) {
270
- char *val; size_t valSize;
271
- err = cbor_value_dup_text_string (&propValue, &val, &valSize, &propValue);
272
- // Char* string transformed into array
273
- reinterpret_cast <ArduinoCloudProperty<String>*>(property)->write (String ((char *)val));
274
- free (val);
251
+ propType = property->getType ();
252
+
253
+ if (propType == " FLOAT" && !cbor_value_map_find_value (&recursedMap, " v" , &propValue)) {
254
+ if (propValue.type == CborDoubleType) {
255
+ double val;
256
+ // get the value of the property as a double
257
+ cbor_value_get_double (&propValue, &val);
258
+ ArduinoCloudProperty<float >* p = (ArduinoCloudProperty<float >*) property;
259
+ p->write ((float )val);
260
+ }
261
+ } else if (propType == " INT" && !cbor_value_map_find_value (&recursedMap, " v" , &propValue)) {
262
+ // if no key proper key was found, do nothing
263
+ if (propValue.type == CborIntegerType) {
264
+ int val;
265
+ cbor_value_get_int (&propValue, &val);
266
+ ArduinoCloudProperty<int >* p = (ArduinoCloudProperty<int >*) property;
267
+ p->write (val);
268
+ } else if (propValue.type == CborDoubleType) {
269
+ // If a double value is received, a cast to int is performed(so it is still accepted)
270
+ double val;
271
+ cbor_value_get_double (&propValue, &val);
272
+ ArduinoCloudProperty<int >* p = (ArduinoCloudProperty<int >*) property;
273
+ p->write ((int )val);
274
+ }
275
+ } else if (propType == " BOOL" && !cbor_value_map_find_value (&recursedMap, " vb" , &propValue)) {
276
+ if (propValue.type == CborBooleanType) {
277
+ bool val;
278
+ cbor_value_get_boolean (&propValue, &val);
279
+ ArduinoCloudProperty<bool >* p = (ArduinoCloudProperty<bool >*) property;
280
+ p->write (val);
281
+ }
282
+ } else if (propType == " STRING" && !cbor_value_map_find_value (&recursedMap, " vs" , &propValue)){
283
+ if (propValue.type == CborTextStringType) {
284
+ char *val; size_t valSize;
285
+ err = cbor_value_dup_text_string (&propValue, &val, &valSize, &propValue);
286
+ ArduinoCloudProperty<String>* p = (ArduinoCloudProperty<String>*) property;
287
+ // Char* string transformed into array
288
+ p->write (String ((char *)val));
289
+ free (val);
290
+ }
275
291
}
276
292
// If the property has been changed call its callback
277
293
if (property->newData ()) {
278
294
if (property->callback != NULL ) {
279
295
property->callback ();
280
296
}
281
- }
297
+ }
282
298
// Continue to scan the cbor map
283
299
cbor_value_advance (&recursedMap);
284
- }
300
+ }
285
301
}
286
302
// Leave the current cbor object, and advance to the next one
287
303
err = cbor_value_leave_container (&dataArray, &recursedMap);
0 commit comments