From 613eae25e276e2be0b1b536b275134bef4fda657 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Wed, 24 Apr 2024 14:34:26 +0200 Subject: [PATCH] Ota default interface: making the download run at least a configurable amount of time --- src/ota/interface/OTAInterfaceDefault.cpp | 24 +++++++++++++---------- src/ota/interface/OTAInterfaceDefault.h | 4 ++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/ota/interface/OTAInterfaceDefault.cpp b/src/ota/interface/OTAInterfaceDefault.cpp index de698e66..d96442bc 100644 --- a/src/ota/interface/OTAInterfaceDefault.cpp +++ b/src/ota/interface/OTAInterfaceDefault.cpp @@ -91,20 +91,24 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::startOTA() { OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() { OTACloudProcessInterface::State res = Fetch; int http_res = 0; + uint32_t start = millis(); - if(http_client->available() == 0) { - goto exit; - } + do { + if(http_client->available() == 0) { + goto exit; + } - http_res = http_client->read(context->buffer, context->buf_len); + http_res = http_client->read(context->buffer, context->buf_len); - if(http_res < 0) { - DEBUG_VERBOSE("OTA ERROR: Download read error %d", http_res); - res = OtaDownloadFail; - goto exit; - } + if(http_res < 0) { + DEBUG_VERBOSE("OTA ERROR: Download read error %d", http_res); + res = OtaDownloadFail; + goto exit; + } - parseOta(context->buffer, http_res); + parseOta(context->buffer, http_res); + } while((context->downloadState == OtaDownloadFile || context->downloadState == OtaDownloadHeader) && + millis() - start < downloadTime); // TODO verify that the information present in the ota header match the info in context if(context->downloadState == OtaDownloadCompleted) { diff --git a/src/ota/interface/OTAInterfaceDefault.h b/src/ota/interface/OTAInterfaceDefault.h index ae68d53b..8f7aaf18 100644 --- a/src/ota/interface/OTAInterfaceDefault.h +++ b/src/ota/interface/OTAInterfaceDefault.h @@ -49,6 +49,10 @@ class OTADefaultCloudProcessInterface: public OTACloudProcessInterface { const char *username, *password; + // The amount of time that each iteration of Fetch has to take at least + // This mitigate the issues arising from tasks run in main loop that are using all the computing time + static constexpr uint32_t downloadTime = 100; + enum OTADownloadState: uint8_t { OtaDownloadHeader, OtaDownloadFile,