Skip to content

Commit 1d504ca

Browse files
committed
restored CBOR decode function
1 parent d6e90f5 commit 1d504ca

File tree

1 file changed

+63
-47
lines changed

1 file changed

+63
-47
lines changed

ArduinoCloudThing.cpp

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -185,47 +185,48 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
185185
int propId; String propType, propName;
186186

187187
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));
189191
return;
190192
}
191193

192194
// parse cbor data only if a cbor array is received.
193-
if (dataArray.type != CborArrayType)
195+
if(dataArray.type != CborArrayType)
194196
return;
195197

196198
// 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
199202
cbor_value_enter_container(&dataArray, &recursedMap);
203+
200204
CborType type = cbor_value_get_type(&recursedMap);
201205
if (type != CborMapType) {
202206
// 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);
207208
continue;
208-
} else {
209209

210+
} else {
211+
210212
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);
217217
continue;
218218
}
219219

220220
CborValue name;
221221
// chechk for the if the a property has a name, if yes Cbor value name will properly updated
222222
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
224225
if (name.type != CborTextStringType) {
225226
cbor_value_advance(&recursedMap);
226227
continue;
227228
}
228-
229+
229230
// get the property name from cbor map as char* string
230231
char *nameVal; size_t nameValSize;
231232
err = cbor_value_dup_text_string(&name, &nameVal, &nameValSize, NULL);
@@ -236,52 +237,67 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
236237
propName = String(nameVal);
237238
// used to avoid memory leaks (cbor_value_dup_text_string automatically perform a malloc)
238239
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
240242
propId = findPropertyByName(propName);
241243
// If property does not exist, skip it and do nothing.
242244
if (propId < 0) {
243245
cbor_value_advance(&recursedMap);
244246
continue;
245247
}
246-
248+
247249
ArduinoCloudPropertyGeneric* property = list.get(propId);
248250
// 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+
}
275291
}
276292
// If the property has been changed call its callback
277293
if (property->newData()) {
278294
if (property->callback != NULL) {
279295
property->callback();
280296
}
281-
}
297+
}
282298
// Continue to scan the cbor map
283299
cbor_value_advance(&recursedMap);
284-
}
300+
}
285301
}
286302
// Leave the current cbor object, and advance to the next one
287303
err = cbor_value_leave_container(&dataArray, &recursedMap);

0 commit comments

Comments
 (0)