Skip to content

Commit e388049

Browse files
committed
Fix encoding
1 parent 86f53b7 commit e388049

File tree

4 files changed

+20
-85
lines changed

4 files changed

+20
-85
lines changed

ArduinoCloudThing.cpp

Lines changed: 12 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ static void utox8(uint32_t val, char* s) {
2222
#endif
2323

2424
#ifdef USE_ARDUINO_CLOUD
25-
2625
char MQTT_SERVER[] = "10.130.22.94";
2726
int MQTT_PORT = 1883;
2827
char GENERAL_TOPIC[] = "/main";
2928
char MQTT_USER[] = "";
3029
char MQTT_PASSWORD[] = "";
3130
char LWT_TOPIC[] = "";
3231
char LWT_MESSAGE[] = "";
32+
#endif
3333

34+
#ifdef ARDUINO_ARCH_MRAA
35+
#define Serial DebugSerial
3436
#endif
3537

3638
ArduinoCloudThing::ArduinoCloudThing() {
@@ -84,7 +86,7 @@ bool ArduinoCloudThing::connect() {
8486
return false;
8587
}
8688

87-
void ArduinoCloudThing::publish(CborObject& object) {
89+
void ArduinoCloudThing::publish(CborArray& object) {
8890

8991
bool retained = false;
9092

@@ -122,7 +124,7 @@ int ArduinoCloudThing::poll() {
122124
diff = checkNewData();
123125
if (diff > 0) {
124126
CborBuffer buffer(1024);
125-
CborObject object = CborObject(buffer);
127+
CborArray object = CborArray(buffer);
126128
compress(object, buffer);
127129
publish(object);
128130
}
@@ -134,20 +136,17 @@ int ArduinoCloudThing::poll() {
134136
return diff;
135137
}
136138

137-
void ArduinoCloudThing::compress(CborObject& object, CborBuffer& buffer) {
138-
139-
CborArray array = CborArray(buffer);
139+
void ArduinoCloudThing::compress(CborArray& object, CborBuffer& buffer) {
140140

141141
for (int i = 0; i < list.size(); i++) {
142142
ArduinoCloudPropertyGeneric *p = list.get(i);
143143
if (p->shouldBeUpdated()) {
144144
CborObject child = CborObject(buffer);
145145
p->append(child);
146146
CborVariant variant = CborVariant(buffer, child);
147-
array.add(variant);
147+
object.add(variant);
148148
}
149149
}
150-
object.set("a", array);
151150
}
152151

153152
int ArduinoCloudThing::checkNewData() {
@@ -205,6 +204,7 @@ void ArduinoCloudThing::callback(MQTTClient *client, char topic[], char bytes[],
205204
void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
206205
CborBuffer buffer(200);
207206
CborVariant total = buffer.decode(payload, length);
207+
208208
CborArray array = total.asArray();
209209

210210
for (int i=0; ;i++) {
@@ -214,6 +214,7 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
214214
break;
215215
}
216216

217+
217218
CborObject object = variant.asObject();
218219

219220
String name = "";
@@ -227,15 +228,13 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
227228
break;
228229
}
229230
if (idx == list.size()) {
230-
Serial.println("Property not found, skipping");
231231
currentListIndex = -1;
232232
}
233233
}
234234
}
235235

236236
if (object.get("t").isValid()) {
237237
int tag = object.get("t").asInteger();
238-
239238
if (name != "") {
240239
list.get(currentListIndex)->setTag(tag);
241240
} else {
@@ -281,79 +280,9 @@ void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
281280

282281
if (list.get(currentListIndex)->newData()) {
283282
// call onUpdate()
284-
list.get(currentListIndex)->callback();
285-
}
286-
}
287-
}
288-
289-
/*
290-
291-
void CborPropertyListener::OnInteger(int32_t value) {
292-
if (currentListIndex < 0) {
293-
return;
294-
}
295-
reinterpret_cast<ArduinoCloudProperty<int>*>(list->get(currentListIndex))->write(value);
296-
}
297-
298-
void CborPropertyListener::OnBytes(unsigned char *data, unsigned int size) {
299-
printf("bytes with size: %d", size);
300-
}
301-
302-
void CborPropertyListener::OnString(String &str) {
303-
// if tag arrived, search a string with the same name in the list
304-
if (newElement == true) {
305-
newElement = false;
306-
for (int i = 0; i < list->size(); i++) {
307-
ArduinoCloudPropertyGeneric *p = list->get(i);
308-
if (p->getName() == str) {
309-
currentListIndex = i;
310-
break;
311-
}
312-
if (i == list->size()) {
313-
Serial.println("Property not found, skipping");
314-
currentListIndex = -1;
283+
if (list.get(currentListIndex)->callback != NULL) {
284+
list.get(currentListIndex)->callback();
315285
}
316286
}
317-
} else {
318-
if (currentListIndex < 0) {
319-
return;
320-
}
321-
reinterpret_cast<ArduinoCloudProperty<String>*>(list->get(currentListIndex))->write(str);
322-
}
323-
}
324-
325-
void CborPropertyListener::OnArray(unsigned int size) {
326-
327-
// prepare for new properties to arrive
328-
if (justStarted == true) {
329-
list_size = size;
330-
justStarted = false;
331-
}
332-
}
333-
334-
void CborPropertyListener::OnMap(unsigned int size) {
335-
}
336-
337-
void CborPropertyListener::OnTag(uint32_t tag) {
338-
newElement = true;
339-
list_size--;
340-
if (list_size < 0) {
341-
Serial.println("problem, we got more properties than advertised");
342-
}
343-
}
344-
345-
void CborPropertyListener::OnSpecial(uint32_t code) {
346-
347-
if (currentListIndex < 0) {
348-
return;
349-
}
350-
if (list->get(currentListIndex)->getPermission() != code) {
351-
Serial.println("permission changed, updating");
352-
list->get(currentListIndex)->setPermission((permissionType)code);
353287
}
354-
}
355-
356-
void CborPropertyListener::OnError(const char *error) {
357-
}
358-
359-
*/
288+
}

ArduinoCloudThing.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ class ArduinoCloudThing {
198198
private:
199199
static void callback(MQTTClient *client, char topic[], char bytes[], int length);
200200
bool connect();
201-
void publish(CborObject& object);
201+
void publish(CborArray& object);
202202

203203
void update();
204204
int checkNewData();
205-
void compress(CborObject& object, CborBuffer& buffer);
205+
void compress(CborArray& object, CborBuffer& buffer);
206206
void decode(uint8_t * payload, size_t length);
207207

208208
bool exists(String &name);

lib/ArduinoCbor/src/CborArray.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ void CborArray::add(const char* value) {
3030
void CborArray::add(CBOR_INT_T value) {
3131
add(CborVariant(buffer, value));
3232
}
33+
34+
size_t CborArray::encode(uint8_t* data, size_t size) {
35+
return cn_cbor_encoder_write(data, 0, size, raw);
36+
}

lib/ArduinoCbor/src/CborArray.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class CborArray {
1515
void add(const char* value);
1616
void add(CBOR_INT_T value);
1717

18+
size_t encode(uint8_t* data, size_t size);
19+
1820
protected:
1921
CborBuffer& buffer;
2022
cn_cbor* raw;

0 commit comments

Comments
 (0)