Skip to content

Reset OTA error before new OTA download #201

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
Sep 24, 2020
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
71 changes: 38 additions & 33 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
addPropertyReal(_ota_error, "OTA_ERROR", Permission::Read);
addPropertyReal(_ota_img_sha256, "OTA_SHA256", Permission::Read);
addPropertyReal(_ota_url, "OTA_URL", Permission::ReadWrite).onSync(DEVICE_WINS);
addPropertyReal(_ota_req, "OTA_REQ", Permission::ReadWrite).onSync(DEVICE_WINS).onUpdate(ArduinoIoTCloudTCP::on_OTA_REQ_Update);
addPropertyReal(_ota_req, "OTA_REQ", Permission::ReadWrite).onSync(DEVICE_WINS);
#endif /* OTA_ENABLED */

#if OTA_STORAGE_SNU
Expand Down Expand Up @@ -301,6 +301,23 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
*/
sendPropertiesToCloud();

#if OTA_ENABLED
/* Request a OTA download if the hidden property
* OTA request has been set.
*/
if (_ota_req)
{
/* Clear the error flag. */
_ota_error = static_cast<int>(OTAError::None);
/* Transmit the cleared error flag to the cloud. */
sendPropertiesToCloud();
/* Clear the request flag. */
_ota_req = false;
/* Call member function to handle OTA request. */
onOTARequest();
}
#endif /* OTA_ENABLED */

return State::Connected;
}
}
Expand Down Expand Up @@ -374,48 +391,36 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
}

#if OTA_ENABLED
void ArduinoIoTCloudTCP::on_OTA_REQ_Update()
{
ArduinoCloud.onOTARequest();
}

void ArduinoIoTCloudTCP::onOTARequest()
{
DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s _ota_req = %s"), __FUNCTION__, _ota_req ? "true" : "false");
DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s _ota_url = %s"), __FUNCTION__, _ota_url.c_str());

if (_ota_req)
{
/* Clear the request flag. */
_ota_req = false;

/* Status flag to prevent the reset from being executed
* when HTTPS download is not supported.
*/
bool ota_download_success = false;
/* Status flag to prevent the reset from being executed
* when HTTPS download is not supported.
*/
bool ota_download_success = false;

#if OTA_STORAGE_SNU
/* Just to be safe delete any remains from previous updates. */
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS");
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");
/* Just to be safe delete any remains from previous updates. */
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS");
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");

/* Trigger direct download to nina module. */
uint8_t nina_ota_err_code = 0;
if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code))
{
DBG_ERROR(F("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code);
_ota_error = static_cast<int>(OTAError::DownloadFailed);
return;
}
/* Trigger direct download to nina module. */
uint8_t nina_ota_err_code = 0;
if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code))
{
DBG_ERROR(F("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code);
_ota_error = static_cast<int>(OTAError::DownloadFailed);
return;
}

/* The download was a success. */
ota_download_success = true;
/* The download was a success. */
ota_download_success = true;
#endif /* OTA_STORAGE_SNU */

/* Perform the reset to reboot to SxU. */
if (ota_download_success)
NVIC_SystemReset();
}
/* Perform the reset to reboot to SxU. */
if (ota_download_success)
NVIC_SystemReset();
}
#endif

Expand Down
1 change: 0 additions & 1 deletion src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
int write(String const topic, byte const data[], int const length);

#if OTA_ENABLED
static void on_OTA_REQ_Update();
void onOTARequest();
#endif
};
Expand Down