Skip to content

Commit 8a38a4c

Browse files
committed
Changing signature of CBOREncoder::encode to allow the propagation of internal errors to the outside
1 parent bc8d6e3 commit 8a38a4c

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

src/ArduinoIoTCloudLPWAN.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ void ArduinoIoTCloudLPWAN::disconnect()
126126

127127
void ArduinoIoTCloudLPWAN::sendPropertiesToCloud()
128128
{
129+
int bytes_encoded = 0;
129130
uint8_t data[CBOR_LORA_MSG_MAX_SIZE];
130-
int const length = CBOREncoder::encode(_property_container, data, sizeof(data), true);
131-
if (length > 0) {
132-
writeProperties(data, length);
133-
}
131+
132+
if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, true) == CborNoError)
133+
if (bytes_encoded > 0)
134+
writeProperties(data, bytes_encoded);
134135
}
135136

136137
int ArduinoIoTCloudLPWAN::writeProperties(const byte data[], int length)

src/ArduinoIoTCloudTCP.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,20 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
283283

284284
void ArduinoIoTCloudTCP::sendPropertiesToCloud()
285285
{
286+
int bytes_encoded = 0;
286287
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
287-
int const length = CBOREncoder::encode(_property_container, data, sizeof(data));
288-
if (length > 0)
289-
{
290-
/* If properties have been encoded store them in the back-up buffer
291-
* in order to allow retransmission in case of failure.
292-
*/
293-
_mqtt_data_len = length;
294-
memcpy(_mqtt_data_buf, data, _mqtt_data_len);
295-
/* Transmit the properties to the MQTT broker */
296-
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
297-
}
288+
289+
if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
290+
if (bytes_encoded > 0)
291+
{
292+
/* If properties have been encoded store them in the back-up buffer
293+
* in order to allow retransmission in case of failure.
294+
*/
295+
_mqtt_data_len = bytes_encoded;
296+
memcpy(_mqtt_data_buf, data, _mqtt_data_len);
297+
/* Transmit the properties to the MQTT broker */
298+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
299+
}
298300
}
299301

300302
void ArduinoIoTCloudTCP::requestLastValue()

src/cbor/CBOREncoder.cpp

+23-14
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,48 @@
3030
/******************************************************************************
3131
* PUBLIC MEMBER FUNCTIONS
3232
******************************************************************************/
33-
34-
int CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, bool lightPayload)
33+
#include <iostream>
34+
CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, bool lightPayload)
3535
{
36+
CborError error = CborNoError;
3637
CborEncoder encoder, arrayEncoder;
3738

3839
cbor_encoder_init(&encoder, data, size, 0);
3940

40-
if (cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength) != CborNoError)
41-
return -1;
41+
error = cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength);
42+
if (CborNoError != error)
43+
return error;
4244

4345
/* Check if backing storage and cloud has diverged
4446
* time interval may be elapsed or property may be changed
4547
* and if that's the case encode the property into the CBOR.
4648
*/
47-
CborError err = CborNoError;
49+
int num_encoded_properties = 0;
4850
std::for_each(property_container.begin(),
4951
property_container.end(),
50-
[lightPayload, &arrayEncoder, &err](Property * p)
52+
[lightPayload, &arrayEncoder, &error, &num_encoded_properties](Property * p)
5153
{
5254
if (p->shouldBeUpdated() && p->isReadableByCloud())
5355
{
54-
err = p->append(&arrayEncoder, lightPayload);
55-
if(err != CborNoError)
56+
error = p->append(&arrayEncoder, lightPayload);
57+
if(error == CborNoError)
58+
num_encoded_properties++;
59+
else
5660
return;
5761
}
5862
});
59-
if ((err != CborNoError) && (err != CborErrorOutOfMemory))
60-
return -1;
63+
if ((CborNoError != error) &&
64+
(CborErrorOutOfMemory != error))
65+
return error;
6166

67+
error = cbor_encoder_close_container(&encoder, &arrayEncoder);
68+
if (CborNoError != error)
69+
return error;
6270

63-
if (cbor_encoder_close_container(&encoder, &arrayEncoder) != CborNoError)
64-
return -1;
71+
if (num_encoded_properties > 0)
72+
bytes_encoded = cbor_encoder_get_buffer_size(&encoder, data);
73+
else
74+
bytes_encoded = 0;
6575

66-
int const bytes_encoded = cbor_encoder_get_buffer_size(&encoder, data);
67-
return bytes_encoded;
76+
return CborNoError;
6877
}

src/cbor/CBOREncoder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CBOREncoder
3535

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

4040
private:
4141

0 commit comments

Comments
 (0)