Skip to content

Commit f35fe82

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

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
576576
}
577577
}
578578

579-
void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(PropertyContainer & property_container)
579+
void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(PropertyContainer & property_container, unsigned int & current_property_index)
580580
{
581581
int bytes_encoded = 0;
582582
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
583583

584-
if (CBOREncoder::encode(property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
584+
if (CBOREncoder::encode(property_container, data, sizeof(data), bytes_encoded, current_property_index, false) == CborNoError)
585585
if (bytes_encoded > 0)
586586
{
587587
/* If properties have been encoded store them in the back-up buffer
@@ -596,13 +596,15 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(PropertyContainer & proper
596596

597597
void ArduinoIoTCloudTCP::sendPropertiesToCloud()
598598
{
599-
sendPropertyContainerToCloud(_property_container);
599+
static unsigned int last_checked_property_index = 0;
600+
sendPropertyContainerToCloud(_property_container, last_checked_property_index);
600601
}
601602

602603
#if OTA_ENABLED
603604
void ArduinoIoTCloudTCP::sendOTAPropertiesToCloud()
604605
{
605606
PropertyContainer ota_property_container;
607+
unsigned int last_ota_property_index = 0;
606608

607609
std::list<String> const ota_property_list {"OTA_CAP", "OTA_ERROR", "OTA_SHA256", "OTA_URL", "OTA_REQ"};
608610
std::for_each(ota_property_list.cbegin(),
@@ -615,7 +617,7 @@ void ArduinoIoTCloudTCP::sendOTAPropertiesToCloud()
615617
}
616618
);
617619

618-
sendPropertyContainerToCloud(ota_property_container);
620+
sendPropertyContainerToCloud(ota_property_container, last_ota_property_index);
619621
}
620622
#endif
621623

src/ArduinoIoTCloudTCP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
164164

165165
static void onMessage(int length);
166166
void handleMessage(int length);
167-
void sendPropertyContainerToCloud(PropertyContainer & property_container);
167+
void sendPropertyContainerToCloud(PropertyContainer & property_container, unsigned int & current_property_index);
168168
void sendPropertiesToCloud();
169169
void requestLastValue();
170170
int write(String const topic, byte const data[], int const length);

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)