Skip to content

Commit dfcc74a

Browse files
committed
Extracting CBOR encode functionality into static class CBOREncoder
1 parent de0d21c commit dfcc74a

File tree

6 files changed

+104
-29
lines changed

6 files changed

+104
-29
lines changed

src/ArduinoIoTCloudLPWAN.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include<ArduinoIoTCloudLPWAN.h>
2727

28+
#include "cbor/CBOREncoder.h"
29+
2830
/******************************************************************************
2931
CONSTANTS
3032
******************************************************************************/
@@ -126,7 +128,7 @@ void ArduinoIoTCloudLPWAN::disconnect()
126128
void ArduinoIoTCloudLPWAN::sendPropertiesToCloud()
127129
{
128130
uint8_t data[CBOR_LORA_MSG_MAX_SIZE];
129-
int const length = _thing.encode(data, sizeof(data), true);
131+
int const length = CBOREncoder::encode(_property_container, data, sizeof(data), true);
130132
if (length > 0) {
131133
writeProperties(data, length);
132134
}

src/ArduinoIoTCloudTCP.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "tls/utility/CryptoUtil.h"
3030
#endif
3131

32+
#include "cbor/CBOREncoder.h"
33+
3234
/******************************************************************************
3335
GLOBAL VARIABLES
3436
******************************************************************************/
@@ -284,7 +286,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
284286
void ArduinoIoTCloudTCP::sendPropertiesToCloud()
285287
{
286288
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
287-
int const length = _thing.encode(data, sizeof(data));
289+
int const length = CBOREncoder::encode(_property_container, data, sizeof(data));
288290
if (length > 0)
289291
{
290292
/* If properties have been encoded store them in the back-up buffer

src/cbor/ArduinoCloudThing.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,6 @@ void ArduinoCloudThing::begin(PropertyContainer * property_container)
4848
_property_container = property_container;
4949
}
5050

51-
int ArduinoCloudThing::encode(uint8_t * data, size_t const size, bool lightPayload) {
52-
53-
// check if backing storage and cloud has diverged
54-
// time interval may be elapsed or property may be changed
55-
CborEncoder encoder, arrayEncoder;
56-
57-
cbor_encoder_init(&encoder, data, size, 0);
58-
59-
if (cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength) != CborNoError) {
60-
return -1;
61-
}
62-
63-
if (appendChangedProperties(*_property_container, &arrayEncoder, lightPayload) < 1) {
64-
return -1;
65-
}
66-
67-
if (cbor_encoder_close_container(&encoder, &arrayEncoder) != CborNoError) {
68-
return -1;
69-
}
70-
71-
int const bytes_encoded = cbor_encoder_get_buffer_size(&encoder, data);
72-
return bytes_encoded;
73-
}
74-
7551
void ArduinoCloudThing::decode(uint8_t const * const payload, size_t const length, bool isSyncMessage) {
7652
_isSyncMessage = isSyncMessage;
7753

src/cbor/ArduinoCloudThing.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ class ArduinoCloudThing {
8181

8282
void begin(PropertyContainer * property_container);
8383

84-
/* encode return > 0 if a property has changed and encodes the changed properties in CBOR format into the provided buffer */
85-
/* 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*/
86-
int encode(uint8_t * data, size_t const size, bool lightPayload = false);
8784
/* decode a CBOR payload received from the cloud */
8885
void decode(uint8_t const * const payload, size_t const length, bool isSyncMessage = false);
8986

src/cbor/CBOREncoder.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
22+
#include "CBOREncoder.h"
23+
24+
#include "lib/tinycbor/cbor-lib.h"
25+
26+
/******************************************************************************
27+
* PUBLIC MEMBER FUNCTIONS
28+
******************************************************************************/
29+
30+
int CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, bool lightPayload)
31+
{
32+
CborEncoder encoder, arrayEncoder;
33+
34+
cbor_encoder_init(&encoder, data, size, 0);
35+
36+
if (cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength) != CborNoError)
37+
return -1;
38+
39+
/* Check if backing storage and cloud has diverged
40+
* time interval may be elapsed or property may be changed
41+
* and if that's the case encode the property into the CBOR.
42+
*/
43+
if (appendChangedProperties(property_container, &arrayEncoder, lightPayload) < 1)
44+
return -1;
45+
46+
if (cbor_encoder_close_container(&encoder, &arrayEncoder) != CborNoError)
47+
return -1;
48+
49+
int const bytes_encoded = cbor_encoder_get_buffer_size(&encoder, data);
50+
return bytes_encoded;
51+
}

src/cbor/CBOREncoder.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_CBOR_CBOR_ENCODER_H_
19+
#define ARDUINO_CBOR_CBOR_ENCODER_H_
20+
21+
/******************************************************************************
22+
* INCLUDE
23+
******************************************************************************/
24+
25+
#include "../property/PropertyContainer.h"
26+
27+
/******************************************************************************
28+
* CLASS DECLARATION
29+
******************************************************************************/
30+
31+
class CBOREncoder
32+
{
33+
34+
public:
35+
36+
/* encode return > 0 if a property has changed and encodes the changed properties in CBOR format into the provided buffer */
37+
/* 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*/
38+
static int encode(PropertyContainer & property_container, uint8_t * data, size_t const size, bool lightPayload = false);
39+
40+
private:
41+
42+
CBOREncoder() { }
43+
CBOREncoder(CborEncoder const &) { }
44+
45+
};
46+
47+
#endif /* ARDUINO_CBOR_CBOR_ENCODER_H_ */

0 commit comments

Comments
 (0)