Skip to content

Fix OTA properties synchronization #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
#endif

#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
# include <algorithm>
# include "tls/utility/SHA256.h"
# include <stm32h7xx_hal_rtc_ex.h>
# include <WiFi.h>
#endif

#include "utility/ota/OTA.h"
#include "utility/ota/FlashSHA256.h"

#include <algorithm>
#include "cbor/CBOREncoder.h"

#include "utility/watchdog/Watchdog.h"
Expand Down Expand Up @@ -516,7 +515,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
/* Clear the request flag. */
_ota_req = false;
/* Transmit the cleared error and request flags to the cloud. */
sendPropertiesToCloud();
sendOTAPropertiesToCloud();
/* Call member function to handle OTA request. */
onOTARequest();
}
Expand Down Expand Up @@ -563,12 +562,12 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
}
}

void ArduinoIoTCloudTCP::sendPropertiesToCloud()
void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(PropertyContainer & property_container)
{
int bytes_encoded = 0;
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];

if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
if (CBOREncoder::encode(property_container, data, sizeof(data), bytes_encoded, false) == CborNoError)
if (bytes_encoded > 0)
{
/* If properties have been encoded store them in the back-up buffer
Expand All @@ -581,6 +580,29 @@ void ArduinoIoTCloudTCP::sendPropertiesToCloud()
}
}

void ArduinoIoTCloudTCP::sendPropertiesToCloud()
{
sendPropertyContainerToCloud(_property_container);
}

void ArduinoIoTCloudTCP::sendOTAPropertiesToCloud()
{
PropertyContainer ota_property_container;

std::list<String> const ota_property_list {"OTA_CAP", "OTA_ERROR", "OTA_SHA256", "OTA_URL", "OTA_REQ"};
std::for_each(ota_property_list.cbegin(),
ota_property_list.cend(),
[this, &ota_property_container ] (String const & name)
{
Property* p = getProperty(this->_property_container, name);
if(p != nullptr)
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
}
);

sendPropertyContainerToCloud(ota_property_container);
}

void ArduinoIoTCloudTCP::requestLastValue()
{
// Send the getLastValues CBOR message to the cloud
Expand Down
2 changes: 2 additions & 0 deletions src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass

static void onMessage(int length);
void handleMessage(int length);
void sendPropertyContainerToCloud(PropertyContainer & property_container);
void sendPropertiesToCloud();
void sendOTAPropertiesToCloud();
void requestLastValue();
int write(String const topic, byte const data[], int const length);

Expand Down