Skip to content

Commit 4d1a0d6

Browse files
committed
Fix ota properties synchronization ensuring all ota properties are sent together in a single message.
1 parent 4c3af93 commit 4d1a0d6

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/ArduinoIoTCloudTCP.cpp

+33-3
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@
3434
#endif
3535

3636
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
37-
# include <algorithm>
3837
# include "tls/utility/SHA256.h"
3938
# include <stm32h7xx_hal_rtc_ex.h>
4039
# include <WiFi.h>
4140
#endif
4241

4342
#include "utility/ota/OTA.h"
4443
#include "utility/ota/FlashSHA256.h"
45-
44+
#include <algorithm>
4645
#include "cbor/CBOREncoder.h"
4746

4847
#include "utility/watchdog/Watchdog.h"
@@ -516,7 +515,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
516515
/* Clear the request flag. */
517516
_ota_req = false;
518517
/* Transmit the cleared error and request flags to the cloud. */
519-
sendPropertiesToCloud();
518+
sendOTAPropertiesToCloud();
520519
/* Call member function to handle OTA request. */
521520
onOTARequest();
522521
}
@@ -581,6 +580,37 @@ void ArduinoIoTCloudTCP::sendPropertiesToCloud()
581580
}
582581
}
583582

583+
void ArduinoIoTCloudTCP::sendOTAPropertiesToCloud()
584+
{
585+
int bytes_encoded = 0;
586+
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
587+
588+
PropertyContainer ota_property_container;
589+
590+
std::list<String> const ota_property_list {"OTA_CAP", "OTA_ERROR", "OTA_SHA256", "OTA_URL", "OTA_REQ"};
591+
std::for_each(ota_property_list.cbegin(),
592+
ota_property_list.cend(),
593+
[this, &ota_property_container ] (String const & name)
594+
{
595+
Property* p = getProperty(this->_property_container, name);
596+
if(p != nullptr)
597+
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
598+
}
599+
);
600+
601+
if (CBOREncoder::encode(ota_property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
602+
if (bytes_encoded > 0)
603+
{
604+
/* If properties have been encoded store them in the back-up buffer
605+
* in order to allow retransmission in case of failure.
606+
*/
607+
_mqtt_data_len = bytes_encoded;
608+
memcpy(_mqtt_data_buf, data, _mqtt_data_len);
609+
/* Transmit the properties to the MQTT broker */
610+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
611+
}
612+
}
613+
584614
void ArduinoIoTCloudTCP::requestLastValue()
585615
{
586616
// Send the getLastValues CBOR message to the cloud

src/ArduinoIoTCloudTCP.h

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
165165
static void onMessage(int length);
166166
void handleMessage(int length);
167167
void sendPropertiesToCloud();
168+
void sendOTAPropertiesToCloud();
168169
void requestLastValue();
169170
int write(String const topic, byte const data[], int const length);
170171

0 commit comments

Comments
 (0)