Skip to content

Commit 0e4aa69

Browse files
authored
Merge pull request #150 from arduino-libraries/cbor-encoder
Split ArduinoCloudThing into CBOREncoder and CBORDecoder and clean up superflous statements.
2 parents de0d21c + 7548be5 commit 0e4aa69

26 files changed

+534
-601
lines changed

extras/test/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ project(testArduinoIoTCloud)
99
##########################################################################
1010

1111
include_directories(include)
12+
include_directories(../../src)
1213
include_directories(../../src/cbor)
1314
include_directories(../../src/property)
1415
include_directories(../../src/utility/ota)
@@ -55,7 +56,8 @@ set(TEST_DUT_SRCS
5556

5657
../../src/property/Property.cpp
5758
../../src/property/PropertyContainer.cpp
58-
../../src/cbor/ArduinoCloudThing.cpp
59+
../../src/cbor/CBORDecoder.cpp
60+
../../src/cbor/CBOREncoder.cpp
5961
../../src/cbor/lib/tinycbor/src/cborencoder.c
6062
../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c
6163
../../src/cbor/lib/tinycbor/src/cborerrorstrings.c

extras/test/include/util/CBORTestUtil.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
INCLUDE
1010
**************************************************************************************/
1111

12-
#include <ArduinoCloudThing.h>
12+
#include <PropertyContainer.h>
1313

1414
#include <vector>
1515

@@ -24,7 +24,7 @@ namespace cbor
2424
PROTOTYPES
2525
**************************************************************************************/
2626

27-
std::vector<uint8_t> encode(ArduinoCloudThing & thing, bool lightPayload = false);
27+
std::vector<uint8_t> encode(PropertyContainer & property_container, bool lightPayload = false);
2828
void print(std::vector<uint8_t> const & vect);
2929

3030
/**************************************************************************************

extras/test/src/test_CloudColor.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <catch.hpp>
1010

1111
#include <util/CBORTestUtil.h>
12-
#include <ArduinoCloudThing.h>
12+
13+
#include <property/types/CloudColor.h>
1314

1415
/**************************************************************************************
1516
TEST CODE

extras/test/src/test_CloudLocation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <catch.hpp>
1010

11-
#include <ArduinoCloudThing.h>
11+
#include <property/types/CloudLocation.h>
1212

1313
/**************************************************************************************
1414
TEST CODE

extras/test/src/test_callback.cpp

+24-46
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <catch.hpp>
1010

1111
#include <util/CBORTestUtil.h>
12-
#include <ArduinoCloudThing.h>
12+
#include <CBORDecoder.h>
1313
#include <PropertyContainer.h>
1414
#include "types/CloudWrapperBool.h"
1515

@@ -41,16 +41,14 @@ SCENARIO("A callback is registered via 'onUpdate' to be called on property chang
4141

4242
GIVEN("CloudProtocol::V2") {
4343
PropertyContainer property_container;
44-
ArduinoCloudThing thing;
45-
thing.begin(&property_container);
46-
44+
4745
CloudInt test = 10;
4846
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).onUpdate(externalCallbackV2);
4947

5048
/* [{0: "test", 2: 7}] = 81 A2 00 64 74 65 73 74 02 07 */
5149
uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x07};
5250
int const payload_length = sizeof(payload) / sizeof(uint8_t);
53-
thing.decode(payload, payload_length);
51+
CBORDecoder::decode(property_container, payload, payload_length);
5452

5553
REQUIRE(callback_called_protocol_v2 == true);
5654
}
@@ -71,16 +69,14 @@ void switch_callback() {
7169
SCENARIO("A (boolean) property is manipulated in the callback to its origin state", "[ArduinoCloudThing::decode]") {
7270
GIVEN("CloudProtocol::V2") {
7371
PropertyContainer property_container;
74-
ArduinoCloudThing thing;
75-
thing.begin(&property_container);
76-
cbor::encode(thing);
72+
cbor::encode(property_container);
7773

7874
addPropertyToContainer(property_container, switch_turned_on, "switch_turned_on", Permission::ReadWrite).onUpdate(switch_callback);
7975

8076
/* [{0: "switch_turned_on", 4: true}] = 81 A2 00 70 73 77 69 74 63 68 5F 74 75 72 6E 65 64 5F 6F 6E 04 F5 */
8177
uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x70, 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x5F, 0x74, 0x75, 0x72, 0x6E, 0x65, 0x64, 0x5F, 0x6F, 0x6E, 0x04, 0xF5};
8278
int const payload_length = sizeof(payload) / sizeof(uint8_t);
83-
thing.decode(payload, payload_length);
79+
CBORDecoder::decode(property_container, payload, payload_length);
8480

8581
REQUIRE(switch_callback_called == true);
8682

@@ -91,7 +87,7 @@ SCENARIO("A (boolean) property is manipulated in the callback to its origin stat
9187

9288
/* [{0: "switch_turned_on", 4: false}] = 9F A2 00 70 73 77 69 74 63 68 5F 74 75 72 6E 65 64 5F 6F 6E 04 F4 FF*/
9389
std::vector<uint8_t> const expected = {0x9F, 0xA2, 0x00, 0x70, 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x5F, 0x74, 0x75, 0x72, 0x6E, 0x65, 0x64, 0x5F, 0x6F, 0x6E, 0x04, 0xF4, 0xFF};
94-
std::vector<uint8_t> const actual = cbor::encode(thing);
90+
std::vector<uint8_t> const actual = cbor::encode(property_container);
9591
REQUIRE(actual == expected);
9692
}
9793
}
@@ -117,17 +113,15 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
117113
change_callback_called = false;
118114

119115
PropertyContainer property_container;
120-
ArduinoCloudThing thing;
121-
thing.begin(&property_container);
122-
116+
123117
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
124118

125119
test.setLastLocalChangeTimestamp(1550138809);
126120

127121
/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
128122
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
129123
int const payload_length = sizeof(payload) / sizeof(uint8_t);
130-
thing.decode(payload, payload_length, true);
124+
CBORDecoder::decode(property_container, payload, payload_length, true);
131125

132126
REQUIRE(sync_callback_called == true);
133127
REQUIRE(change_callback_called == true);
@@ -145,17 +139,15 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
145139
change_callback_called = false;
146140

147141
PropertyContainer property_container;
148-
ArduinoCloudThing thing;
149-
thing.begin(&property_container);
150-
142+
151143
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
152144
test = false;
153145
test.setLastLocalChangeTimestamp(1550138811);
154146

155147
/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
156148
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
157149
int const payload_length = sizeof(payload) / sizeof(uint8_t);
158-
thing.decode(payload, payload_length, true);
150+
CBORDecoder::decode(property_container, payload, payload_length, true);
159151

160152
REQUIRE(sync_callback_called == true);
161153
REQUIRE(change_callback_called == false);
@@ -172,9 +164,7 @@ SCENARIO("Primitive property: After a connection/reconnection an incoming cbor p
172164
change_callback_called = false;
173165

174166
PropertyContainer property_container;
175-
ArduinoCloudThing thing;
176-
thing.begin(&property_container);
177-
167+
178168
addPropertyToContainer(property_container, *p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
179169
test = false;
180170
updateTimestampOnLocallyChangedProperties(property_container);
@@ -184,7 +174,7 @@ SCENARIO("Primitive property: After a connection/reconnection an incoming cbor p
184174
/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
185175
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
186176
int const payload_length = sizeof(payload) / sizeof(uint8_t);
187-
thing.decode(payload, payload_length, true);
177+
CBORDecoder::decode(property_container, payload, payload_length, true);
188178

189179
REQUIRE(sync_callback_called == true);
190180
REQUIRE(change_callback_called == true);
@@ -203,9 +193,7 @@ SCENARIO("Primitive property: After a connection/reconnection an incoming cbor p
203193
change_callback_called = false;
204194

205195
PropertyContainer property_container;
206-
ArduinoCloudThing thing;
207-
thing.begin(&property_container);
208-
196+
209197
addPropertyToContainer(property_container, *p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
210198
test = false;
211199
updateTimestampOnLocallyChangedProperties(property_container);
@@ -215,7 +203,7 @@ SCENARIO("Primitive property: After a connection/reconnection an incoming cbor p
215203
/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
216204
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
217205
int const payload_length = sizeof(payload) / sizeof(uint8_t);
218-
thing.decode(payload, payload_length, true);
206+
CBORDecoder::decode(property_container, payload, payload_length, true);
219207

220208
REQUIRE(sync_callback_called == true);
221209
REQUIRE(change_callback_called == false);
@@ -231,17 +219,15 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl
231219
change_callback_called = false;
232220

233221
PropertyContainer property_container;
234-
ArduinoCloudThing thing;
235-
thing.begin(&property_container);
236-
222+
237223
addPropertyToContainer(property_container, location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
238224
location_test.setLastLocalChangeTimestamp(1550138809);
239225

240226
/* [{-3: 1550138810.00, 0: "test:lat", 3: 2},{0: "test:lon", 3: 3}] = 82 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 68 74 65 73 74 3A 6C 61 74 02 02 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 03*/
241227
uint8_t const payload[] = { 0x82, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x61, 0x74, 0x02, 0x02, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x6F, 0x6E, 0x02, 0x03 };
242228

243229
int const payload_length = sizeof(payload) / sizeof(uint8_t);
244-
thing.decode(payload, payload_length, true);
230+
CBORDecoder::decode(property_container, payload, payload_length, true);
245231

246232
REQUIRE(sync_callback_called == true);
247233
REQUIRE(change_callback_called == true);
@@ -263,17 +249,15 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl
263249
change_callback_called = false;
264250

265251
PropertyContainer property_container;
266-
ArduinoCloudThing thing;
267-
thing.begin(&property_container);
268-
252+
269253
addPropertyToContainer(property_container, location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
270254
location_test.setLastLocalChangeTimestamp(1550138811);
271255

272256
/* [{-3: 1550138810.00, 0: "test:lat", 3: 2},{0: "test:lon", 3: 3}] = 82 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 68 74 65 73 74 3A 6C 61 74 02 02 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 03*/
273257
uint8_t const payload[] = { 0x82, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x61, 0x74, 0x02, 0x02, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x6F, 0x6E, 0x02, 0x03 };
274258

275259
int const payload_length = sizeof(payload) / sizeof(uint8_t);
276-
thing.decode(payload, payload_length, true);
260+
CBORDecoder::decode(property_container, payload, payload_length, true);
277261

278262
REQUIRE(sync_callback_called == true);
279263
REQUIRE(change_callback_called == false);
@@ -301,15 +285,13 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
301285
change_callback_called = false;
302286

303287
PropertyContainer property_container;
304-
ArduinoCloudThing thing;
305-
thing.begin(&property_container);
306-
288+
307289
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_device_sync_callback);
308290

309291
/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
310292
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
311293
int const payload_length = sizeof(payload) / sizeof(uint8_t);
312-
thing.decode(payload, payload_length, true);
294+
CBORDecoder::decode(property_container, payload, payload_length, true);
313295

314296
REQUIRE(sync_callback_called == true);
315297
REQUIRE(change_callback_called == false);
@@ -334,15 +316,13 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
334316
change_callback_called = false;
335317

336318
PropertyContainer property_container;
337-
ArduinoCloudThing thing;
338-
thing.begin(&property_container);
339-
319+
340320
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_cloud_sync_callback);
341321

342322
/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
343323
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
344324
int const payload_length = sizeof(payload) / sizeof(uint8_t);
345-
thing.decode(payload, payload_length, true);
325+
CBORDecoder::decode(property_container, payload, payload_length, true);
346326

347327
REQUIRE(sync_callback_called == true);
348328
REQUIRE(change_callback_called == true);
@@ -361,15 +341,13 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed.
361341
change_callback_called = false;
362342

363343
PropertyContainer property_container;
364-
ArduinoCloudThing thing;
365-
thing.begin(&property_container);
366-
344+
367345
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).onUpdate(change_callback);
368346

369347
/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
370348
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
371349
int const payload_length = sizeof(payload) / sizeof(uint8_t);
372-
thing.decode(payload, payload_length, true);
350+
CBORDecoder::decode(property_container, payload, payload_length, true);
373351

374352
REQUIRE(sync_callback_called == false);
375353
REQUIRE(change_callback_called == false);

0 commit comments

Comments
 (0)