Skip to content

Commit 188c39a

Browse files
authored
Merge pull request #132 from arduino-libraries/extract-property-container
Extracting property container functionality
2 parents 03bc13e + 221683c commit 188c39a

40 files changed

+630
-476
lines changed

Diff for: extras/test/CMakeLists.txt

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

1111
include_directories(include)
1212
include_directories(../../src/cbor)
13+
include_directories(../../src/property)
1314
include_directories(../../src/utility/ota)
1415
include_directories(external/catch/v2.12.1/include)
1516
include_directories(external/fakeit/v2.0.5/include)
@@ -51,8 +52,9 @@ set(TEST_DUT_SRCS
5152
../../src/utility/ota/crc.cpp
5253
../../src/utility/ota/OTALogic.cpp
5354

55+
../../src/property/Property.cpp
56+
../../src/property/PropertyContainer.cpp
5457
../../src/cbor/ArduinoCloudThing.cpp
55-
../../src/cbor/ArduinoCloudProperty.cpp
5658
../../src/cbor/lib/tinycbor/src/cborencoder.c
5759
../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c
5860
../../src/cbor/lib/tinycbor/src/cborerrorstrings.c

Diff for: extras/test/src/test_addPropertyReal.cpp

+18-17
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@
88

99
#include <catch.hpp>
1010

11-
#include <ArduinoCloudThing.h>
11+
#include <PropertyContainer.h>
12+
13+
#include <types/CloudInt.h>
14+
#include <types/CloudBool.h>
15+
#include <types/CloudFloat.h>
16+
#include <types/CloudString.h>
1217

1318
/**************************************************************************************
1419
TEST CODE
1520
**************************************************************************************/
1621

1722
SCENARIO("The same arduino cloud properties are added multiple times", "[ArduinoCloudThing::addPropertyReal]") {
1823
WHEN("The same bool property is added multiple times") {
19-
ArduinoCloudThing thing;
20-
thing.begin();
24+
PropertyContainer property_container;
2125

2226
CloudBool bool_property = false;
2327

24-
ArduinoCloudProperty * bool_property_ptr_1 = &thing.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
25-
ArduinoCloudProperty * bool_property_ptr_2 = &thing.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
28+
Property * bool_property_ptr_1 = &property_container.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
29+
Property * bool_property_ptr_2 = &property_container.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
2630
THEN("No new property is added and the first added property is returned instead of a new one") {
2731
REQUIRE(bool_property_ptr_1 == bool_property_ptr_2);
2832
}
@@ -31,13 +35,12 @@ SCENARIO("The same arduino cloud properties are added multiple times", "[Arduino
3135
/**************************************************************************************/
3236

3337
WHEN("the same int property is added multiple times") {
34-
ArduinoCloudThing thing;
35-
thing.begin();
38+
PropertyContainer property_container;
3639

3740
CloudInt int_property = 1;
3841

39-
ArduinoCloudProperty * int_property_ptr_1 = &thing.addPropertyReal(int_property, "int_property", Permission::ReadWrite);
40-
ArduinoCloudProperty * int_property_ptr_2 = &thing.addPropertyReal(int_property, "int_property", Permission::ReadWrite);
42+
Property * int_property_ptr_1 = &property_container.addPropertyReal(int_property, "int_property", Permission::ReadWrite);
43+
Property * int_property_ptr_2 = &property_container.addPropertyReal(int_property, "int_property", Permission::ReadWrite);
4144

4245
THEN("No new property is added and the first added property is returned instead of a new one") {
4346
REQUIRE(int_property_ptr_1 == int_property_ptr_2);
@@ -47,13 +50,12 @@ SCENARIO("The same arduino cloud properties are added multiple times", "[Arduino
4750
/**************************************************************************************/
4851

4952
WHEN("the same float property is added multiple times") {
50-
ArduinoCloudThing thing;
51-
thing.begin();
53+
PropertyContainer property_container;
5254

5355
CloudFloat float_property = 1.0f;
5456

55-
ArduinoCloudProperty * float_property_ptr_1 = &thing.addPropertyReal(float_property, "float_property", Permission::ReadWrite);
56-
ArduinoCloudProperty * float_property_ptr_2 = &thing.addPropertyReal(float_property, "float_property", Permission::ReadWrite);
57+
Property * float_property_ptr_1 = &property_container.addPropertyReal(float_property, "float_property", Permission::ReadWrite);
58+
Property * float_property_ptr_2 = &property_container.addPropertyReal(float_property, "float_property", Permission::ReadWrite);
5759

5860
THEN("No new property is added and the first added property is returned instead of a new one") {
5961
REQUIRE(float_property_ptr_1 == float_property_ptr_2);
@@ -63,13 +65,12 @@ SCENARIO("The same arduino cloud properties are added multiple times", "[Arduino
6365
/**************************************************************************************/
6466

6567
WHEN("the same String property is added multiple times") {
66-
ArduinoCloudThing thing;
67-
thing.begin();
68+
PropertyContainer property_container;
6869

6970
CloudString str_property;
7071

71-
ArduinoCloudProperty * str_property_ptr_1 = &thing.addPropertyReal(str_property, "str_property", Permission::ReadWrite);
72-
ArduinoCloudProperty * str_property_ptr_2 = &thing.addPropertyReal(str_property, "str_property", Permission::ReadWrite);
72+
Property * str_property_ptr_1 = &property_container.addPropertyReal(str_property, "str_property", Permission::ReadWrite);
73+
Property * str_property_ptr_2 = &property_container.addPropertyReal(str_property, "str_property", Permission::ReadWrite);
7374

7475
THEN("No new property is added and the first added property is returned instead of a new one") {
7576
REQUIRE(str_property_ptr_1 == str_property_ptr_2);

Diff for: extras/test/src/test_callback.cpp

+41-29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <util/CBORTestUtil.h>
1212
#include <ArduinoCloudThing.h>
13+
#include <PropertyContainer.h>
1314
#include "types/CloudWrapperBool.h"
1415

1516
/**************************************************************************************
@@ -39,11 +40,12 @@ SCENARIO("A callback is registered via 'onUpdate' to be called on property chang
3940
/************************************************************************************/
4041

4142
GIVEN("CloudProtocol::V2") {
43+
PropertyContainer property_container;
4244
ArduinoCloudThing thing;
43-
thing.begin();
45+
thing.begin(&property_container);
4446

4547
CloudInt test = 10;
46-
thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(externalCallbackV2);
48+
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(externalCallbackV2);
4749

4850
/* [{0: "test", 2: 7}] = 81 A2 00 64 74 65 73 74 02 07 */
4951
uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x07};
@@ -68,11 +70,12 @@ void switch_callback() {
6870

6971
SCENARIO("A (boolean) property is manipulated in the callback to its origin state", "[ArduinoCloudThing::decode]") {
7072
GIVEN("CloudProtocol::V2") {
73+
PropertyContainer property_container;
7174
ArduinoCloudThing thing;
72-
thing.begin();
75+
thing.begin(&property_container);
7376
cbor::encode(thing);
7477

75-
thing.addPropertyReal(switch_turned_on, "switch_turned_on", Permission::ReadWrite).onUpdate(switch_callback);
78+
property_container.addPropertyReal(switch_turned_on, "switch_turned_on", Permission::ReadWrite).onUpdate(switch_callback);
7679

7780
/* [{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 */
7881
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};
@@ -98,7 +101,7 @@ SCENARIO("A (boolean) property is manipulated in the callback to its origin stat
98101
static bool sync_callback_called = false;
99102
static bool change_callback_called = false;
100103

101-
void auto_sync_callback(ArduinoCloudProperty& property) {
104+
void auto_sync_callback(Property& property) {
102105
MOST_RECENT_WINS(property);
103106
sync_callback_called = true;
104107
}
@@ -113,10 +116,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
113116
sync_callback_called = false;
114117
change_callback_called = false;
115118

119+
PropertyContainer property_container;
116120
ArduinoCloudThing thing;
117-
thing.begin();
121+
thing.begin(&property_container);
118122

119-
thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
123+
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
120124

121125
test.setLastLocalChangeTimestamp(1550138809);
122126

@@ -140,10 +144,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
140144
sync_callback_called = false;
141145
change_callback_called = false;
142146

147+
PropertyContainer property_container;
143148
ArduinoCloudThing thing;
144-
thing.begin();
149+
thing.begin(&property_container);
145150

146-
thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
151+
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
147152
test = false;
148153
test.setLastLocalChangeTimestamp(1550138811);
149154

@@ -162,16 +167,17 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
162167
SCENARIO("Primitive property: After a connection/reconnection an incoming cbor payload is processed and the synchronization callback is executed. The sync callback applies the AUTO_SYNC policy (the most recent value between the local one and the cloud one is finally assigned to the property). The onUpdate function is called if the cloud value is the most recent one. In this scenario the most updated value is the cloud one.") {
163168
GIVEN("CloudProtocol::V2") {
164169
bool test = true;
165-
std::unique_ptr<ArduinoCloudProperty> p(new CloudWrapperBool(test));
170+
std::unique_ptr<Property> p(new CloudWrapperBool(test));
166171
sync_callback_called = false;
167172
change_callback_called = false;
168173

174+
PropertyContainer property_container;
169175
ArduinoCloudThing thing;
170-
thing.begin();
176+
thing.begin(&property_container);
171177

172-
thing.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
178+
property_container.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
173179
test = false;
174-
thing.updateTimestampOnLocallyChangedProperties();
180+
property_container.updateTimestampOnLocallyChangedProperties();
175181
//There is no RTC on test execution environment so we force the local timestamp
176182
p->setLastLocalChangeTimestamp(1550138809);
177183

@@ -192,16 +198,17 @@ SCENARIO("Primitive property: After a connection/reconnection an incoming cbor p
192198
SCENARIO("Primitive property: After a connection/reconnection an incoming cbor payload is processed and the synchronization callback is executed. The sync callback apply the AUTO_SYNC policy (the most recent value between the local one and the cloud one is finally assigned to the property). The onUpdate function is called if the cloud value is the most recent one. In this scenario the most updated value is the local one.") {
193199
GIVEN("CloudProtocol::V2") {
194200
bool test = true;
195-
std::unique_ptr<ArduinoCloudProperty> p(new CloudWrapperBool(test));
201+
std::unique_ptr<Property> p(new CloudWrapperBool(test));
196202
sync_callback_called = false;
197203
change_callback_called = false;
198204

205+
PropertyContainer property_container;
199206
ArduinoCloudThing thing;
200-
thing.begin();
207+
thing.begin(&property_container);
201208

202-
thing.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
209+
property_container.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
203210
test = false;
204-
thing.updateTimestampOnLocallyChangedProperties();
211+
property_container.updateTimestampOnLocallyChangedProperties();
205212
//There is no RTC on test execution environment so we force the local timestamp
206213
p->setLastLocalChangeTimestamp(1550138811);
207214

@@ -223,10 +230,11 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl
223230
sync_callback_called = false;
224231
change_callback_called = false;
225232

233+
PropertyContainer property_container;
226234
ArduinoCloudThing thing;
227-
thing.begin();
235+
thing.begin(&property_container);
228236

229-
thing.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
237+
property_container.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
230238
location_test.setLastLocalChangeTimestamp(1550138809);
231239

232240
/* [{-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*/
@@ -254,10 +262,11 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl
254262
sync_callback_called = false;
255263
change_callback_called = false;
256264

265+
PropertyContainer property_container;
257266
ArduinoCloudThing thing;
258-
thing.begin();
267+
thing.begin(&property_container);
259268

260-
thing.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
269+
property_container.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
261270
location_test.setLastLocalChangeTimestamp(1550138811);
262271

263272
/* [{-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*/
@@ -279,7 +288,7 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl
279288

280289
/**************************************************************************************/
281290

282-
void force_device_sync_callback(ArduinoCloudProperty& property) {
291+
void force_device_sync_callback(Property& property) {
283292
DEVICE_WINS(property);
284293
sync_callback_called = true;
285294
}
@@ -291,10 +300,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
291300
sync_callback_called = false;
292301
change_callback_called = false;
293302

303+
PropertyContainer property_container;
294304
ArduinoCloudThing thing;
295-
thing.begin();
305+
thing.begin(&property_container);
296306

297-
thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_device_sync_callback);
307+
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_device_sync_callback);
298308

299309
/* [{-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 */
300310
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
@@ -311,7 +321,7 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
311321

312322
/**************************************************************************************/
313323

314-
void force_cloud_sync_callback(ArduinoCloudProperty& property) {
324+
void force_cloud_sync_callback(Property& property) {
315325
CLOUD_WINS(property);
316326
sync_callback_called = true;
317327
}
@@ -323,10 +333,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
323333
sync_callback_called = false;
324334
change_callback_called = false;
325335

336+
PropertyContainer property_container;
326337
ArduinoCloudThing thing;
327-
thing.begin();
338+
thing.begin(&property_container);
328339

329-
thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_cloud_sync_callback);
340+
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_cloud_sync_callback);
330341

331342
/* [{-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 */
332343
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
@@ -349,10 +360,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed.
349360
sync_callback_called = false;
350361
change_callback_called = false;
351362

363+
PropertyContainer property_container;
352364
ArduinoCloudThing thing;
353-
thing.begin();
365+
thing.begin(&property_container);
354366

355-
thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback);
367+
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback);
356368

357369
/* [{-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 */
358370
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};

0 commit comments

Comments
 (0)