Skip to content

Commit 98484d2

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

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 34 additions & 3 deletions
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,38 @@ 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 to_property_container;
589+
PropertyContainer from_property_container = _property_container;
590+
591+
std::list<String> ota_property_list {"OTA_CAP", "OTA_ERROR", "OTA_SHA256", "OTA_URL", "OTA_REQ"};
592+
std::for_each(ota_property_list.begin(),
593+
ota_property_list.end(),
594+
[&from_property_container, &to_property_container ] (String const & name)
595+
{
596+
Property* p = getProperty(from_property_container, name);
597+
if(p != nullptr)
598+
addPropertyToContainer(to_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
599+
}
600+
);
601+
602+
if (CBOREncoder::encode(to_property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
603+
if (bytes_encoded > 0)
604+
{
605+
/* If properties have been encoded store them in the back-up buffer
606+
* in order to allow retransmission in case of failure.
607+
*/
608+
_mqtt_data_len = bytes_encoded;
609+
memcpy(_mqtt_data_buf, data, _mqtt_data_len);
610+
/* Transmit the properties to the MQTT broker */
611+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
612+
}
613+
}
614+
584615
void ArduinoIoTCloudTCP::requestLastValue()
585616
{
586617
// Send the getLastValues CBOR message to the cloud

src/ArduinoIoTCloudTCP.h

Lines changed: 1 addition & 0 deletions
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)