Skip to content

Commit 2c41a33

Browse files
committed
Change CBOR library (this one uses maps)
decode() needs to be rewritten
1 parent 00cca22 commit 2c41a33

38 files changed

+1972
-2004
lines changed

ArduinoCloudThing.cpp

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ bool ArduinoCloudThing::connect() {
8484
return false;
8585
}
8686

87-
void ArduinoCloudThing::publish(CborDynamicOutput& output) {
87+
void ArduinoCloudThing::publish(CborObject& object) {
8888

8989
bool retained = false;
9090

91-
unsigned char *buf = output.getData();
91+
uint8_t data[1024];
92+
size_t size = object.encode(data, sizeof(data));
9293

9394
#ifdef TESTING_PROTOCOL
94-
decode(buf, output.getSize());
95+
decode(data, size);
9596
#endif
9697

97-
client->publish(GENERAL_TOPIC, (const char*)buf, output.getSize());
98+
#ifndef TESTING_PROTOCOL
99+
client->publish(GENERAL_TOPIC, (const char*)data, size);
100+
#endif
98101

99102
for (int i = 0; i < list.size(); i++) {
100103
ArduinoCloudPropertyGeneric *p = list.get(i);
@@ -115,11 +118,13 @@ int ArduinoCloudThing::poll() {
115118

116119
// check if backing storage and cloud has diverged
117120
int diff = 0;
118-
CborDynamicOutput output;
119-
diff = checkNewData(output);
121+
122+
diff = checkNewData();
120123
if (diff > 0) {
121-
compress(output, diff);
122-
publish(output);
124+
CborBuffer buffer(1024);
125+
CborObject object = CborObject(buffer);
126+
compress(object, buffer);
127+
publish(object);
123128
}
124129

125130
#ifdef DEBUG_MEMORY
@@ -129,21 +134,23 @@ int ArduinoCloudThing::poll() {
129134
return diff;
130135
}
131136

132-
void ArduinoCloudThing::compress(CborDynamicOutput& output, int howMany) {
137+
void ArduinoCloudThing::compress(CborObject& object, CborBuffer& buffer) {
133138

134-
CborWriter writer(output);
135-
136-
writer.writeArray(howMany);
139+
CborArray array = CborArray(buffer);
137140

138141
for (int i = 0; i < list.size(); i++) {
139142
ArduinoCloudPropertyGeneric *p = list.get(i);
140143
if (p->newData()) {
141-
p->append(writer);
144+
CborObject child = CborObject(buffer);
145+
p->append(child);
146+
CborVariant variant = CborVariant(buffer, child);
147+
array.add(variant);
142148
}
143149
}
150+
object.set("a", array);
144151
}
145152

146-
int ArduinoCloudThing::checkNewData(CborDynamicOutput& output) {
153+
int ArduinoCloudThing::checkNewData() {
147154
int counter = 0;
148155
for (int i = 0; i < list.size(); i++) {
149156
ArduinoCloudPropertyGeneric *p = list.get(i);
@@ -193,14 +200,23 @@ void ArduinoCloudThing::callback(MQTTClient *client, char topic[], char bytes[],
193200
}
194201

195202
void ArduinoCloudThing::decode(uint8_t * payload, size_t length) {
196-
CborInput input(payload, length);
203+
/*
204+
CborBuffer buffer(200);
205+
CborVariant variant = buffer.decode(payload, length);
206+
CborArray array = variant.asArray();
207+
208+
CborVariant obj = array.get(0);
209+
if (!obj.isValid()) {
210+
return;
211+
}
212+
if (obj.isString()) {
197213
198-
CborReader reader(input);
199-
CborPropertyListener listener(&list);
200-
reader.SetListener(listener);
201-
reader.Run();
214+
}
215+
*/
202216
}
203217

218+
/*
219+
204220
void CborPropertyListener::OnInteger(int32_t value) {
205221
if (currentListIndex < 0) {
206222
return;
@@ -267,4 +283,6 @@ void CborPropertyListener::OnSpecial(uint32_t code) {
267283
}
268284
269285
void CborPropertyListener::OnError(const char *error) {
270-
}
286+
}
287+
288+
*/

ArduinoCloudThing.h

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include <Stream.h>
66
#include "lib/MQTT/src/MQTTClient.h"
77
#include "lib/LinkedList/LinkedList.h"
8-
#include "lib/CBOR/CborDecoder.h"
9-
#include "lib/CBOR/CborEncoder.h"
8+
#include "lib/ArduinoCbor/src/ArduinoCbor.h"
109

1110
#ifndef MQTT_BUFFER_SIZE
1211
#define MQTT_BUFFER_SIZE 256
@@ -33,7 +32,7 @@ enum boolStatus {
3332
class ArduinoCloudPropertyGeneric
3433
{
3534
public:
36-
virtual void append(CborWriter &cbor) = 0;
35+
virtual void append(CborObject& object) = 0;
3736
virtual String& getName() = 0;
3837
virtual void setName(String _name) = 0;
3938
virtual void setPermission(permissionType _permission) = 0;
@@ -87,14 +86,12 @@ class ArduinoCloudProperty : public ArduinoCloudPropertyGeneric
8786
return permission;
8887
}
8988

90-
void appendValue(CborWriter &cbor);
89+
void appendValue(CborObject &cbor);
9190

92-
void append(CborWriter &cbor) {
93-
cbor.writeArray(4);
94-
cbor.writeTag(tag);
95-
cbor.writeString(name);
91+
void append(CborObject &cbor) {
92+
cbor.set("n", name.c_str());
9693
appendValue(cbor);
97-
cbor.writeSpecial(permission);
94+
cbor.set("p", permission);
9895
}
9996

10097
bool newData() {
@@ -115,29 +112,28 @@ class ArduinoCloudProperty : public ArduinoCloudPropertyGeneric
115112
};
116113

117114
template <>
118-
inline void ArduinoCloudProperty<int>::appendValue(CborWriter &cbor) {
119-
cbor.writeInt(property);
115+
inline void ArduinoCloudProperty<int>::appendValue(CborObject &cbor) {
116+
cbor.set("i", property);
120117
};
121118

122119
template <>
123-
inline void ArduinoCloudProperty<bool>::appendValue(CborWriter &cbor) {
124-
//cbor.writeBoolean(property);
125-
cbor.writeInt((int)property);
120+
inline void ArduinoCloudProperty<bool>::appendValue(CborObject &cbor) {
121+
cbor.set("b", property);
126122
};
127123

128124
template <>
129-
inline void ArduinoCloudProperty<float>::appendValue(CborWriter &cbor) {
130-
//cbor.writeFloat(property);
125+
inline void ArduinoCloudProperty<float>::appendValue(CborObject &cbor) {
126+
cbor.set("f", property);
131127
};
132128

133129
template <>
134-
inline void ArduinoCloudProperty<String>::appendValue(CborWriter &cbor) {
135-
cbor.writeString(property);
130+
inline void ArduinoCloudProperty<String>::appendValue(CborObject &cbor) {
131+
cbor.set("s", property.c_str());
136132
};
137133

138134
template <>
139-
inline void ArduinoCloudProperty<char*>::appendValue(CborWriter &cbor) {
140-
cbor.writeString(property);
135+
inline void ArduinoCloudProperty<char*>::appendValue(CborObject &cbor) {
136+
cbor.set("s", property);
141137
};
142138

143139
#ifndef addProperty
@@ -159,11 +155,11 @@ class ArduinoCloudThing {
159155
private:
160156
static void callback(MQTTClient *client, char topic[], char bytes[], int length);
161157
bool connect();
162-
void publish(CborDynamicOutput& output);
158+
void publish(CborObject& object);
163159

164160
void update();
165-
int checkNewData(CborDynamicOutput& output);
166-
void compress(CborDynamicOutput& output, int howMany);
161+
int checkNewData();
162+
void compress(CborObject& object, CborBuffer& buffer);
167163
void decode(uint8_t * payload, size_t length);
168164

169165
bool exists(String &name);
@@ -176,22 +172,4 @@ class ArduinoCloudThing {
176172
MQTTClient* client;
177173
};
178174

179-
class CborPropertyListener : public CborListener {
180-
public:
181-
CborPropertyListener(LinkedList<ArduinoCloudPropertyGeneric*> *_list) : list(_list) {}
182-
void OnInteger(int32_t value);
183-
void OnBytes(unsigned char *data, unsigned int size);
184-
void OnString(String &str);
185-
void OnArray(unsigned int size);
186-
void OnMap(unsigned int size) ;
187-
void OnTag(uint32_t tag);
188-
void OnSpecial(uint32_t code);
189-
void OnError(const char *error);
190-
LinkedList<ArduinoCloudPropertyGeneric*> *list;
191-
int currentListIndex;
192-
bool justStarted = true;
193-
int list_size = 0;
194-
bool newElement = false;
195-
};
196-
197175
#endif

lib/ArduinoCbor/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
gtest

lib/ArduinoCbor/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ArduinoCbor
2+
3+
ArduinoCbor is a C++ library to handle CBOR data structures.
4+
CBOR is a binary format based on the JSON data model.
5+
Internal the [cn-cbor](https://github.com/cabo/cn-cbor) C library is used.
6+
7+
## Examples
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <ArduinoCbor.h>
2+
3+
void setup() {
4+
Serial.begin(115200);
5+
6+
while(!Serial) {
7+
delay(10);
8+
}
9+
}
10+
11+
12+
void loop() {
13+
CborBuffer buffer(200);
14+
CborObject object = CborObject(buffer);
15+
16+
object.set("string", "value");
17+
object.set("integer", -1234);
18+
19+
CborObject child = CborObject(buffer);
20+
child.set("key", "value");
21+
object.set("object", child);
22+
23+
CborArray array = CborArray(buffer);
24+
array.add(-1234);
25+
object.set("array", array);
26+
27+
Serial.print("string value: ");
28+
Serial.println(object.get("string").asString());
29+
30+
Serial.print("integer value: ");
31+
Serial.println(object.get("integer").asInteger());
32+
33+
Serial.print("child string value: ");
34+
Serial.println(object.get("object").asObject().get("key").asString());
35+
36+
delay(1000);
37+
}

lib/ArduinoCbor/library.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "ArduinoCbor",
3+
"keywords": "cbor",
4+
"description": "CBOR library for Arduino",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/bergos/ArduinoCbor.git"
8+
},
9+
"frameworks": "arduino"
10+
}

lib/ArduinoCbor/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ArduinoCbor
2+
version=0.0.1
3+
author=Thomas Bergwinkl <[email protected]> (https://www.bergnet.org/people/bergi/card#me)
4+
maintainer=Thomas Bergwinkl <[email protected]> (https://www.bergnet.org/people/bergi/card#me)
5+
sentence=CBOR library for Arduino
6+
paragraph=CBOR library for Arduino
7+
category=Other
8+
url=https://github.com/bergos/ArduinoCbor
9+
architectures=*

lib/ArduinoCbor/makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
OBJS += \
2+
build/cn-cbor.o \
3+
build/cn-create.o \
4+
build/cn-encoder.o \
5+
build/cn-error.o \
6+
build/cn-get.o \
7+
build/CborArray.o \
8+
build/CborBuffer.o \
9+
build/CborObject.o \
10+
build/CborVariant.o \
11+
build/test.o
12+
13+
LIBS += \
14+
-lgtest \
15+
-lpthread
16+
17+
build/%.o: src/cn-cbor/%.c
18+
@mkdir -p build
19+
gcc $(CCFLAGS) -c -o "$@" "$<" -I src
20+
21+
build/%.o: src/%.cpp
22+
@mkdir -p build
23+
g++ $(CCFLAGS) -c -o "$@" "$<"
24+
25+
build/%.o: extra/test/%.cpp
26+
@mkdir -p build
27+
g++ $(CCFLAGS) -c -o "$@" "$<" -I src
28+
29+
all: test
30+
31+
test: $(OBJS)
32+
g++ -o gtest $(OBJS) $(LIBS)
33+
./gtest
34+
35+
clean:
36+
@rm -rf build
37+
@rm -f test

lib/ArduinoCbor/src/ArduinoCbor.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef ARDUINO_CBOR_H_
2+
#define ARDUINO_CBOR_H_
3+
4+
#define USE_CBOR_CONTEXT
5+
6+
#include "cn-cbor/cn-cbor.h"
7+
8+
#ifndef CBOR_INT_T
9+
#define CBOR_INT_T long
10+
#endif
11+
12+
class CborArray;
13+
class CborBuffer;
14+
class CborObject;
15+
class CborVariant;
16+
17+
#include "CborArray.h"
18+
#include "CborBuffer.h"
19+
#include "CborObject.h"
20+
#include "CborVariant.h"
21+
22+
#endif // ARDUINO_CBOR_H_

lib/ArduinoCbor/src/CborArray.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <string.h>
2+
#include <stdio.h>
3+
4+
#include "ArduinoCbor.h"
5+
6+
CborArray::CborArray(CborBuffer& buffer, cn_cbor* raw) : buffer(buffer) {
7+
this->raw = raw;
8+
9+
if (raw == 0) {
10+
cn_cbor_errback err;
11+
12+
this->raw = cn_cbor_array_create(&buffer.context, &err);
13+
}
14+
}
15+
16+
CborVariant CborArray::get(int index) {
17+
return CborVariant(buffer, cn_cbor_index(raw, index));
18+
}
19+
20+
void CborArray::add(CborVariant value) {
21+
cn_cbor_errback err;
22+
23+
cn_cbor_array_append(raw, value.raw, &err);
24+
}
25+
26+
void CborArray::add(const char* value) {
27+
add(CborVariant(buffer, value));
28+
}
29+
30+
void CborArray::add(CBOR_INT_T value) {
31+
add(CborVariant(buffer, value));
32+
}

0 commit comments

Comments
 (0)