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

Commit 2489e77

Browse files
authored
Removing enum class CloudProtocol since the 'decode' function decodes both V1/V2 and encoding will only happen in V2 anymore. Decoding for V2 shall be removed at a later point in time (#19)
1 parent b598aad commit 2489e77

16 files changed

+96
-201
lines changed

.gitignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
build
21
*~
2+
3+
build
4+
bin
5+
CMakeFiles
6+
37
.project
48
.cproject
9+
10+
CMakeCache.txt
11+
cmake_install.cmake
12+
Makefile
13+

src/ArduinoCloudProperty.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434
* TYPEDEF
3535
******************************************************************************/
3636

37-
enum class CloudProtocol {
38-
V1, /* [{"n": "test", "vb": true}] */
39-
V2 /* [{0: "test", 4: true}] */
40-
};
41-
4237
enum class Permission {
4338
Read, Write, ReadWrite
4439
};
@@ -97,7 +92,7 @@ class ArduinoCloudProperty {
9792
bool shouldBeUpdated ();
9893
void execCallbackOnChange ();
9994

100-
void append (CborEncoder * encoder, CloudProtocol const cloud_protocol);
95+
void append (CborEncoder * encoder);
10196

10297
private:
10398

@@ -117,7 +112,7 @@ class ArduinoCloudProperty {
117112
unsigned long _last_updated_millis,
118113
_update_interval_millis;
119114

120-
void appendValue(CborEncoder * mapEncoder, CloudProtocol const cloud_protocol) const;
115+
void appendValue(CborEncoder * mapEncoder) const;
121116
bool isValueDifferent(T const lhs, T const rhs) const;
122117

123118
T getInitialMinDeltaPropertyValue() const;

src/ArduinoCloudProperty.ipp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,14 @@ void ArduinoCloudProperty<T>::execCallbackOnChange() {
103103
}
104104

105105
template <typename T>
106-
void ArduinoCloudProperty<T>::append(CborEncoder * encoder, CloudProtocol const cloud_protocol) {
106+
void ArduinoCloudProperty<T>::append(CborEncoder * encoder) {
107107
if (isReadableByCloud()) {
108108
CborEncoder mapEncoder;
109109

110-
cbor_encoder_create_map(encoder, &mapEncoder, CborIndefiniteLength);
111-
112-
if (cloud_protocol == CloudProtocol::V1) cbor_encode_text_stringz(&mapEncoder, "n");
113-
else if(cloud_protocol == CloudProtocol::V2) cbor_encode_int (&mapEncoder, static_cast<int>(CborIntegerMapKey::Name));
114-
cbor_encode_text_stringz(&mapEncoder, _name.c_str());
115-
appendValue(&mapEncoder, cloud_protocol);
110+
cbor_encoder_create_map (encoder, &mapEncoder, CborIndefiniteLength);
111+
cbor_encode_int (&mapEncoder, static_cast<int>(CborIntegerMapKey::Name));
112+
cbor_encode_text_stringz (&mapEncoder, _name.c_str());
113+
appendValue (&mapEncoder);
116114
cbor_encoder_close_container(encoder, &mapEncoder);
117115

118116
_shadow_property = _property;
@@ -126,30 +124,26 @@ void ArduinoCloudProperty<T>::append(CborEncoder * encoder, CloudProtocol const
126124
******************************************************************************/
127125

128126
template <>
129-
inline void ArduinoCloudProperty<bool>::appendValue(CborEncoder * mapEncoder, CloudProtocol const cloud_protocol) const {
130-
if (cloud_protocol == CloudProtocol::V1) cbor_encode_text_stringz(mapEncoder, "vb");
131-
else if(cloud_protocol == CloudProtocol::V2) cbor_encode_int (mapEncoder, static_cast<int>(CborIntegerMapKey::BooleanValue));
127+
inline void ArduinoCloudProperty<bool>::appendValue(CborEncoder * mapEncoder) const {
128+
cbor_encode_int (mapEncoder, static_cast<int>(CborIntegerMapKey::BooleanValue));
132129
cbor_encode_boolean(mapEncoder, _property);
133130
}
134131

135132
template <>
136-
inline void ArduinoCloudProperty<int>::appendValue(CborEncoder * mapEncoder, CloudProtocol const cloud_protocol) const {
137-
if (cloud_protocol == CloudProtocol::V1) cbor_encode_text_stringz(mapEncoder, "v");
138-
else if(cloud_protocol == CloudProtocol::V2) cbor_encode_int (mapEncoder, static_cast<int>(CborIntegerMapKey::Value));
133+
inline void ArduinoCloudProperty<int>::appendValue(CborEncoder * mapEncoder) const {
134+
cbor_encode_int(mapEncoder, static_cast<int>(CborIntegerMapKey::Value));
139135
cbor_encode_int(mapEncoder, _property);
140136
}
141137

142138
template <>
143-
inline void ArduinoCloudProperty<float>::appendValue(CborEncoder * mapEncoder, CloudProtocol const cloud_protocol) const {
144-
if (cloud_protocol == CloudProtocol::V1) cbor_encode_text_stringz(mapEncoder, "v");
145-
else if(cloud_protocol == CloudProtocol::V2) cbor_encode_int (mapEncoder, static_cast<int>(CborIntegerMapKey::Value));
139+
inline void ArduinoCloudProperty<float>::appendValue(CborEncoder * mapEncoder) const {
140+
cbor_encode_int (mapEncoder, static_cast<int>(CborIntegerMapKey::Value));
146141
cbor_encode_float(mapEncoder, _property);
147142
}
148143

149144
template <>
150-
inline void ArduinoCloudProperty<String>::appendValue(CborEncoder * mapEncoder, CloudProtocol const cloud_protocol) const {
151-
if (cloud_protocol == CloudProtocol::V1) cbor_encode_text_stringz(mapEncoder, "vs");
152-
else if(cloud_protocol == CloudProtocol::V2) cbor_encode_int (mapEncoder, static_cast<int>(CborIntegerMapKey::StringValue));
145+
inline void ArduinoCloudProperty<String>::appendValue(CborEncoder * mapEncoder) const {
146+
cbor_encode_int (mapEncoder, static_cast<int>(CborIntegerMapKey::StringValue));
153147
cbor_encode_text_stringz(mapEncoder, _property.c_str());
154148
}
155149

src/ArduinoCloudPropertyContainer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ int ArduinoCloudPropertyContainer::getNumOfChangedProperties() {
4444
return num_changes_properties;
4545
}
4646

47-
void ArduinoCloudPropertyContainer::appendChangedProperties(CborEncoder * arrayEncoder, CloudProtocol const cloud_protocol) {
48-
appendChangedProperties<bool> (_bool_property_list, arrayEncoder, cloud_protocol);
49-
appendChangedProperties<int> (_int_property_list, arrayEncoder, cloud_protocol);
50-
appendChangedProperties<float> (_float_property_list, arrayEncoder, cloud_protocol);
51-
appendChangedProperties<String>(_string_property_list, arrayEncoder, cloud_protocol);
47+
void ArduinoCloudPropertyContainer::appendChangedProperties(CborEncoder * arrayEncoder) {
48+
appendChangedProperties<bool> (_bool_property_list, arrayEncoder);
49+
appendChangedProperties<int> (_int_property_list, arrayEncoder);
50+
appendChangedProperties<float> (_float_property_list, arrayEncoder);
51+
appendChangedProperties<String>(_string_property_list, arrayEncoder);
5252
}

src/ArduinoCloudPropertyContainer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ArduinoCloudPropertyContainer {
3636

3737
bool isPropertyInContainer (Type const type, String const & name);
3838
int getNumOfChangedProperties();
39-
void appendChangedProperties (CborEncoder * arrayEncoder, CloudProtocol const cloud_protocol);
39+
void appendChangedProperties (CborEncoder * arrayEncoder);
4040

4141
inline ArduinoCloudProperty<bool> * getPropertyBool (String const & name) { return getProperty(_bool_property_list, name); }
4242
inline ArduinoCloudProperty<int> * getPropertyInt (String const & name) { return getProperty(_int_property_list, name); }
@@ -65,7 +65,7 @@ class ArduinoCloudPropertyContainer {
6565
int getNumOfChangedProperties(LinkedList<ArduinoCloudProperty<T> *> & list);
6666

6767
template <typename T>
68-
void appendChangedProperties(LinkedList<ArduinoCloudProperty<T> *> & list, CborEncoder * arrayEncoder, CloudProtocol const cloud_protocol);
68+
void appendChangedProperties(LinkedList<ArduinoCloudProperty<T> *> & list, CborEncoder * arrayEncoder);
6969

7070
};
7171

src/ArduinoCloudPropertyContainer.ipp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ int ArduinoCloudPropertyContainer::getNumOfChangedProperties(LinkedList<ArduinoC
5252
}
5353

5454
template <typename T>
55-
void ArduinoCloudPropertyContainer::appendChangedProperties(LinkedList<ArduinoCloudProperty<T> *> & list, CborEncoder * arrayEncoder, CloudProtocol const cloud_protocol) {
55+
void ArduinoCloudPropertyContainer::appendChangedProperties(LinkedList<ArduinoCloudProperty<T> *> & list, CborEncoder * arrayEncoder) {
5656
for (int i = 0; i < list.size(); i++) {
5757
ArduinoCloudProperty<T> * p = list.get(i);
5858
if (p->shouldBeUpdated() && p->isReadableByCloud()) {
59-
p->append(arrayEncoder, cloud_protocol);
59+
p->append(arrayEncoder);
6060
}
6161
}
6262
}

src/ArduinoCloudThing.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ static void utox8(uint32_t val, char* s) {
5656
* CTOR/DTOR
5757
******************************************************************************/
5858

59-
ArduinoCloudThing::ArduinoCloudThing(CloudProtocol const cloud_protocol)
60-
: _cloud_protocol(cloud_protocol) {
59+
ArduinoCloudThing::ArduinoCloudThing() {
6160
#ifdef ARDUINO_ARCH_SAMD
6261
#define SERIAL_NUMBER_WORD_0 *(volatile uint32_t*)(0x0080A00C)
6362
#define SERIAL_NUMBER_WORD_1 *(volatile uint32_t*)(0x0080A040)
@@ -100,7 +99,7 @@ int ArduinoCloudThing::encode(uint8_t * data, size_t const size) {
10099
return -1;
101100
}
102101

103-
_property_cont.appendChangedProperties(&arrayEncoder, _cloud_protocol);
102+
_property_cont.appendChangedProperties(&arrayEncoder);
104103

105104
err = cbor_encoder_close_container(&encoder, &arrayEncoder);
106105

@@ -224,10 +223,12 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_MapKey(CborValue * v
224223

225224
if(cbor_value_at_end(value_iter)) {
226225
next_state = MapParserState::LeaveMap;
227-
// If the key is a string means that the Map use the string keys (protocol V1)
228-
// https://tools.ietf.org/html/rfc8428#section-4.3
229-
// example [{"n": "temperature", "v": 25}]
230-
} else if(cbor_value_is_text_string(value_iter)) {
226+
}
227+
/* If the key is a string means that the Map use the string keys (protocol V1)
228+
* https://tools.ietf.org/html/rfc8428#section-4.3
229+
* Example [{"n": "temperature", "v": 25}]
230+
*/
231+
else if(cbor_value_is_text_string(value_iter)) {
231232
char * val = 0;
232233
size_t val_size = 0;
233234
if(cbor_value_dup_text_string(value_iter, &val, &val_size, value_iter) == CborNoError) {
@@ -242,9 +243,11 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_MapKey(CborValue * v
242243
else next_state = MapParserState::UndefinedKey;
243244
free(val);
244245
}
245-
// If the key is a number means that the Map use the CBOR Label (protocol V2)
246-
// example [{0: "temperature", 2: 25}]
247-
} else if (cbor_value_is_integer(value_iter)) {
246+
}
247+
/* If the key is a number means that the Map use the CBOR Label (protocol V2)
248+
* Example [{0: "temperature", 2: 25}]
249+
*/
250+
else if (cbor_value_is_integer(value_iter)) {
248251
int val = 0;
249252
if(cbor_value_get_int(value_iter, &val) == CborNoError) {
250253
if(cbor_value_advance(value_iter) == CborNoError) {

src/ArduinoCloudThing.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static long const DAYS = 86400;
4747
class ArduinoCloudThing {
4848

4949
public:
50-
ArduinoCloudThing(CloudProtocol const cloud_protocol = CloudProtocol::V2);
50+
ArduinoCloudThing();
5151

5252
void begin();
5353

@@ -64,7 +64,6 @@ class ArduinoCloudThing {
6464

6565
private:
6666

67-
CloudProtocol const _cloud_protocol;
6867
bool _status = OFF;
6968
char _uuid[33];
7069
ArduinoCloudPropertyContainer _property_cont;
@@ -96,7 +95,7 @@ class ArduinoCloudThing {
9695
inline T const get () const { return _entry; }
9796

9897
inline bool isSet() const { return _is_set; }
99-
inline bool reset() { _is_set = false; }
98+
inline void reset() { _is_set = false; }
10099

101100
private:
102101

test/src/test_callback.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SCENARIO("A callback is registered via 'onUpdate' to be called on property chang
3333

3434
GIVEN("CloudProtocol::V1")
3535
{
36-
ArduinoCloudThing thing(CloudProtocol::V1);
36+
ArduinoCloudThing thing;
3737
thing.begin();
3838

3939
int test = 10;
@@ -48,7 +48,7 @@ SCENARIO("A callback is registered via 'onUpdate' to be called on property chang
4848
}
4949
GIVEN("CloudProtocol::V2")
5050
{
51-
ArduinoCloudThing thing(CloudProtocol::V2);
51+
ArduinoCloudThing thing;
5252
thing.begin();
5353

5454
int test = 10;
@@ -78,16 +78,16 @@ void switch_callback()
7878

7979
SCENARIO("A (boolean) property is manipulated in the callback to its origin state", "[ArduinoCloudThing::decode]")
8080
{
81-
GIVEN("CloudProtocol::V1")
81+
GIVEN("CloudProtocol::V2")
8282
{
83-
ArduinoCloudThing thing(CloudProtocol::V1);
83+
ArduinoCloudThing thing;
8484
thing.begin();
8585
encode(thing);
8686

8787
thing.addPropertyReal(switch_turned_on, "switch_turned_on", Permission::ReadWrite).onUpdate(switch_callback);
8888

89-
/* [{"n": "switch_turned_on", "vb": true}] = 81 A2 61 6E 70 73 77 69 74 63 68 5F 74 75 72 6E 65 64 5F 6F 6E 62 76 62 F5 */
90-
uint8_t const payload[] = {0x81, 0xA2, 0x61, 0x6E, 0x70, 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x5F, 0x74, 0x75, 0x72, 0x6E, 0x65, 0x64, 0x5F, 0x6F, 0x6E, 0x62, 0x76, 0x62, 0xF5};
89+
/* [{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 */
90+
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};
9191
int const payload_length = sizeof(payload)/sizeof(uint8_t);
9292
thing.decode(payload, payload_length);
9393

@@ -98,8 +98,8 @@ SCENARIO("A (boolean) property is manipulated in the callback to its origin stat
9898
* the cloud.
9999
*/
100100

101-
/* [{"n": "switch_turned_on", "vb": false}] = 81 BF 61 6E 70 73 77 69 74 63 68 5F 74 75 72 6E 65 64 5F 6F 6E 62 76 62 F4 FF */
102-
std::vector<uint8_t> const expected = {0x81, 0xBF, 0x61, 0x6E, 0x70, 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x5F, 0x74, 0x75, 0x72, 0x6E, 0x65, 0x64, 0x5F, 0x6F, 0x6E, 0x62, 0x76, 0x62, 0xF4, 0xFF};
101+
/* [{0: "switch_turned_on", 4: false}] = 81 BF 00 70 73 77 69 74 63 68 5F 74 75 72 6E 65 64 5F 6F 6E 04 F4 FF */
102+
std::vector<uint8_t> const expected = {0x81, 0xBF, 0x00, 0x70, 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x5F, 0x74, 0x75, 0x72, 0x6E, 0x65, 0x64, 0x5F, 0x6F, 0x6E, 0x04, 0xF4, 0xFF};
103103
std::vector<uint8_t> const actual = encode(thing);
104104
REQUIRE(actual == expected);
105105
}

0 commit comments

Comments
 (0)