Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit e4140c3

Browse files
committed
Added comments to the code
1 parent 2b84cd1 commit e4140c3

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

src/ArduinoCloudProperty.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,18 @@ void ArduinoCloudProperty::appendAttributeReal(String value, String attributeNam
152152

153153
void ArduinoCloudProperty::appendAttributeName(String attributeName, std::function<void (CborEncoder& mapEncoder)>appendValue, CborEncoder *encoder) {
154154
if (attributeName != "") {
155+
// when the attribute name string is not empty, the attribute identifier is incremented in order to be encoded in the message if the _lightPayload flag is set
155156
_attributeIdentifier++;
156157
}
157158
CborEncoder mapEncoder;
158159
cbor_encoder_create_map(encoder, &mapEncoder, 2);
159160
cbor_encode_int(&mapEncoder, static_cast<int>(CborIntegerMapKey::Name));
160161

162+
// if _lightPayload is true, the property and attribute identifiers will be encoded instead of the property name
161163
if (_lightPayload) {
164+
// the most significant byte of the identifier to be encoded represent the property identifier
162165
int completeIdentifier = _attributeIdentifier * 256;
166+
// the least significant byte of the identifier to be encoded represent the attribute identifier
163167
completeIdentifier += _identifier;
164168
cbor_encode_int(&mapEncoder, completeIdentifier);
165169
} else {
@@ -211,12 +215,14 @@ void ArduinoCloudProperty::setAttributeReal(String attributeName, std::function<
211215
CborMapData *map = _map_data_list->get(i);
212216
if (map != nullptr) {
213217
if (map->light_payload.isSet() && map->light_payload.get()) {
218+
// if a light payload is detected, the attribute identifier is retrieved from the cbor map and the corresponding attribute is updated
214219
int attid = map->attribute_identifier.get();
215220
if (attid == _attributeIdentifier) {
216221
setValue(map);
217222
break;
218223
}
219224
} else {
225+
// if a normal payload is detected, the name of the attribute to be updated is extracted directly from the cbor map
220226
String an = map->attribute_name.get();
221227
if (an == attributeName) {
222228
setValue(map);

src/ArduinoCloudThing.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_Name(CborValue * val
328328
MapParserState next_state = MapParserState::Error;
329329

330330
if (cbor_value_is_text_string(value_iter)) {
331+
// if the value in the cbor message is a string, it corresponds to the name of the property to be updated (int the form [property_name]:[attribute_name])
331332
char * val = nullptr;
332333
size_t val_size = 0;
333334
if (cbor_value_dup_text_string(value_iter, &val, &val_size, value_iter) == CborNoError) {
@@ -343,6 +344,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_Name(CborValue * val
343344
next_state = MapParserState::MapKey;
344345
}
345346
} else if (cbor_value_is_integer(value_iter)) {
347+
// if the value in the cbor message is an integer, a light payload has been used and an integer identifier should be decode in order to retrieve the corresponding property and attribute name to be updated
346348
int val = 0;
347349
if (cbor_value_get_int(value_iter, &val) == CborNoError) {
348350
map_data->light_payload.set(true);

src/ArduinoCloudThing.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ class ArduinoCloudThing {
7777
ArduinoCloudThing();
7878

7979
void begin();
80-
80+
//if propertyIdentifier is different from -1, an integer identifier is associated to the added property to be use instead of the property name when the parameter lightPayload is true in the encode method
8181
ArduinoCloudProperty & addPropertyReal(ArduinoCloudProperty & property, String const & name, Permission const permission, int propertyIdentifier = -1);
8282

8383
/* encode return > 0 if a property has changed and encodes the changed properties in CBOR format into the provided buffer */
84+
/* if lightPayload is true the integer identifier of the property will be encoded in the message instead of the property name in order to reduce the size of the message payload*/
8485
int encode(uint8_t * data, size_t const size, bool lightPayload = false);
8586
/* decode a CBOR payload received from the cloud */
8687
void decode(uint8_t const * const payload, size_t const length, bool isSyncMessage = false);
@@ -142,6 +143,7 @@ class ArduinoCloudThing {
142143
if (propertyIdentifier != -1) {
143144
property_obj->setIdentifier(propertyIdentifier);
144145
} else {
146+
// if property identifier is -1, an incremental value will be assigned as identifier.
145147
property_obj->setIdentifier(_numProperties);
146148
}
147149
_property_list.add(property_obj);

test/src/test_decode.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ SCENARIO("Arduino Cloud Properties are decoded", "[ArduinoCloudThing::decode]")
5151
/************************************************************************************/
5252

5353
WHEN("A boolean property is changed via CBOR message - light payload") {
54+
/*An integer identifier has been encoded instead of the name of the property in order to have a shorter payload*/
5455
GIVEN("CloudProtocol::V2") {
5556
ArduinoCloudThing thing;
5657
thing.begin();
5758

5859
CloudBool test = true;
60+
/*The property is added with identifier 1 that will be used instead of the string "test" as property identifier*/
5961
thing.addPropertyReal(test, "test", Permission::ReadWrite, 1);
6062

6163
/* [{0: 1, 4: false}] = 81 A2 00 01 04 F4 */
@@ -186,12 +188,14 @@ SCENARIO("Arduino Cloud Properties are decoded", "[ArduinoCloudThing::decode]")
186188
/************************************************************************************/
187189

188190
WHEN("A Color property is changed via CBOR message - light payload") {
191+
/*An integer identifier has been encoded instead of the name of the property in order to have a shorter payload*/
189192
GIVEN("CloudProtocol::V2") {
190193
ArduinoCloudThing thing;
191194
thing.begin();
192195

193196
CloudColor color_test = CloudColor(0.0, 0.0, 0.0);
194197

198+
/*The property is added with identifier 1 that will be used instead of the string "test" as property identifier*/
195199
thing.addPropertyReal(color_test, "test", Permission::ReadWrite, 1);
196200

197201
/* [{0: 257, 2: 2.0},{0: 513, 2: 2.0},{0: 769, 2: 2.0}] = 83 A2 00 19 01 01 02 FA 40 00 00 00 A2 00 19 02 01 02 FA 40 00 00 00 A2 00 19 03 01 02 FA 40 00 00 00 */

test/src/test_encode.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode]")
4141
/************************************************************************************/
4242

4343
WHEN("A 'bool' property is added - light payload") {
44+
/*An integer identifier must be instead of the name of the property in order to have a shorter payload*/
4445
GIVEN("CloudProtocol::V2") {
4546
ArduinoCloudThing thing;
4647
thing.begin();
4748
encode(thing);
4849

4950
CloudBool test = true;
51+
/*The property is added with identifier 1 that will be used instead of the string "test" as property identifier*/
5052
thing.addPropertyReal(test, "test", Permission::ReadWrite, 1);
5153

5254
/* [{0: 1, 4: true}] = 9F A2 00 01 04 F5 FF*/
@@ -150,12 +152,14 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode]")
150152
/************************************************************************************/
151153

152154
WHEN("A 'Color' property is added - light payload") {
155+
/*An integer identifier must be encoded instead of the name of the property in order to have a shorter payload*/
153156
GIVEN("CloudProtocol::V2") {
154157
ArduinoCloudThing thing;
155158
thing.begin();
156159
encode(thing);
157160

158161
CloudColor color_test = CloudColor(2.0, 2.0, 2.0);
162+
/*The property is added with identifier 1 that will be used instead of the string "name" as property identifier */
159163
thing.addPropertyReal(color_test, "test", Permission::ReadWrite, 1);
160164

161165
/* [{0: 257, 2: 2.0},{0: 513, 2: 2.0},{0: 769, 2: 2.0}] = 9F A2 00 19 01 01 02 FA 40 00 00 00 A2 00 19 02 01 02 FA 40 00 00 00 A2 00 19 03 01 02 FA 40 00 00 00 FF*/

0 commit comments

Comments
 (0)