Skip to content

Commit ab527a1

Browse files
committed
Move last_property index out of CBOREncoder::encode function and fix unit tests
1 parent eb41da8 commit ab527a1

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

extras/test/src/util/CBORTestUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ namespace cbor
2727
std::vector<uint8_t> encode(PropertyContainer & property_container, bool lightPayload)
2828
{
2929
int bytes_encoded = 0;
30+
unsigned int starting_property_index = 0;
3031
uint8_t buf[200] = {0};
3132

32-
if (CBOREncoder::encode(property_container, buf, 200, bytes_encoded, lightPayload) == CborNoError)
33+
if (CBOREncoder::encode(property_container, buf, 200, bytes_encoded, starting_property_index, lightPayload) == CborNoError)
3334
return std::vector<uint8_t>(buf, buf + bytes_encoded);
3435
else
3536
return std::vector<uint8_t>();

src/ArduinoIoTCloudLPWAN.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ void ArduinoIoTCloudLPWAN::sendPropertiesToCloud()
149149
{
150150
int bytes_encoded = 0;
151151
uint8_t data[CBOR_LORA_MSG_MAX_SIZE];
152+
static unsigned int last_checked_property_index = 0;
152153

153-
if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, true) == CborNoError)
154+
if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, last_checked_property_index, true) == CborNoError)
154155
if (bytes_encoded > 0)
155156
writeProperties(data, bytes_encoded);
156157
}

src/ArduinoIoTCloudTCP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,9 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(PropertyContainer & proper
580580
{
581581
int bytes_encoded = 0;
582582
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
583+
static unsigned int last_checked_property_index = 0;
583584

584-
if (CBOREncoder::encode(property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
585+
if (CBOREncoder::encode(property_container, data, sizeof(data), bytes_encoded, last_checked_property_index, false) == CborNoError)
585586
if (bytes_encoded > 0)
586587
{
587588
/* If properties have been encoded store them in the back-up buffer

src/cbor/CBOREncoder.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* PUBLIC MEMBER FUNCTIONS
3333
******************************************************************************/
3434

35-
CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, bool lightPayload)
35+
CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, unsigned int & current_property_index, bool lightPayload)
3636
{
3737
CborEncoder encoder, arrayEncoder;
3838

@@ -47,13 +47,12 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
4747
CborError error = CborNoError;
4848
int num_encoded_properties = 0;
4949
int num_checked_properties = 0;
50-
static unsigned int last_property_index = 0;
5150

52-
if(last_property_index >= property_container.size())
53-
last_property_index = 0;
51+
if(current_property_index >= property_container.size())
52+
current_property_index = 0;
5453

5554
PropertyContainer::iterator iter = property_container.begin();
56-
std::advance(iter, last_property_index);
55+
std::advance(iter, current_property_index);
5756

5857
std::for_each(iter,
5958
property_container.end(),
@@ -73,7 +72,7 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
7372
}
7473
});
7574

76-
last_property_index += num_checked_properties;
75+
current_property_index += num_checked_properties;
7776

7877
if ((CborNoError != error) &&
7978
(CborErrorOutOfMemory != error))

src/cbor/CBOREncoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CBOREncoder
4545

4646
/* encode return > 0 if a property has changed and encodes the changed properties in CBOR format into the provided buffer */
4747
/* 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*/
48-
static CborError encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, bool lightPayload = false);
48+
static CborError encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, unsigned int & current_property_index, bool lightPayload = false);
4949

5050
private:
5151

0 commit comments

Comments
 (0)