Skip to content

Commit

Permalink
Improvements to OtaNetwork Http upgrader (#2725)
Browse files Browse the repository at this point in the history
* Fetch one item at a time

* Enable Host for testing OTA

* Add check in case item list is empty

---------

Co-authored-by: mikee47 <[email protected]>
  • Loading branch information
mikee47 and mikee47 authored Mar 8, 2024
1 parent 1e03c46 commit 8ba44cb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
3 changes: 3 additions & 0 deletions Sming/Arch/Host/spiffs-two-roms.hw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "Two ROM slots with single SPIFFS",
"base_config": "spiffs",
"partitions": {
"rom0": {
"subtype": "ota_0"
},
"rom1": {
"address": "0x108000",
"size": "992K",
Expand Down
89 changes: 47 additions & 42 deletions Sming/Libraries/OtaNetwork/src/HttpUpgrader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,37 @@ namespace Network
{
void HttpUpgrader::start()
{
for(unsigned i = 0; i < items.count(); i++) {
auto& it = items[i];
debug_d("Download file:\r\n"
" (%u) %s -> %s @ 0x%X",
currentItem, it.url.c_str(), it.partition.name().c_str(), it.partition.address());

HttpRequest* request;
if(baseRequest != nullptr) {
request = baseRequest->clone();
request->setURL(it.url);
} else {
request = new HttpRequest(it.url);
}

request->setMethod(HTTP_GET);
request->setResponseStream(it.getStream());

if(i == items.count() - 1) {
request->onRequestComplete(RequestCompletedDelegate(&HttpUpgrader::updateComplete, this));
} else {
request->onRequestComplete(RequestCompletedDelegate(&HttpUpgrader::itemComplete, this));
}

if(!send(request)) {
debug_e("ERROR: Rejected sending new request.");
break;
}
fetchNextItem();
}

void HttpUpgrader::fetchNextItem()
{
if(currentItem >= items.count()) {
return;
}

auto& it = items[currentItem];
debug_d("Download file:\r\n"
" (%u) %s -> %s @ 0x%X",
currentItem, it.url.c_str(), it.partition.name().c_str(), it.partition.address());

HttpRequest* request;
if(baseRequest != nullptr) {
request = baseRequest->clone();
request->setURL(it.url);
} else {
request = new HttpRequest(it.url);
}

request->setMethod(HTTP_GET);
request->setResponseStream(it.getStream());

request->onRequestComplete(RequestCompletedDelegate(&HttpUpgrader::itemComplete, this));

if(!send(request)) {
debug_e("ERROR: Rejected sending new request.");
it.stream.release();
downloadFailed();
}
}

Expand All @@ -52,43 +56,44 @@ int HttpUpgrader::itemComplete(HttpConnection& client, bool success)
auto& it = items[currentItem];

if(!success) {
it.stream.release(); // Owned by HttpRequest
updateFailed();
it.stream.release();
downloadFailed();
return -1;
}

debug_d("Finished: URL: %s, Offset: 0x%X, Length: %u", it.url.c_str(), it.partition.address(),
it.stream->available());

it.size = it.stream->available();
debug_d("Finished: URL: %s, Offset: 0x%X, Length: %u", it.url.c_str(), it.partition.address(), it.size);

it.stream.release(); // the actual deletion will happen outside of this class
currentItem++;

if(currentItem < items.count()) {
fetchNextItem();
} else {
downloadComplete();
}

return 0;
}

int HttpUpgrader::updateComplete(HttpConnection& client, bool success)
void HttpUpgrader::downloadComplete()
{
int hasError = itemComplete(client, success);
if(hasError != 0) {
return hasError;
}

#if DEBUG_VERBOSE_LEVEL >= DBG
debug_d("\r\nFirmware download finished!");
for(unsigned i = 0; i < items.count(); i++) {
debug_d(" - item: %u, addr: 0x%X, url: %s", i, items[i].partition.address(), items[i].url.c_str());
auto& it = items[i];
debug_d(" - item: %u, addr: 0x%X, size: 0x%X, url: %s", i, it.partition.address(), it.size, it.url.c_str());
}
#endif

if(updateDelegate) {
updateDelegate(*this, true);
}

applyUpdate();

return 0;
}

void HttpUpgrader::updateFailed()
void HttpUpgrader::downloadFailed()
{
debug_e("\r\nFirmware download failed..");
if(updateDelegate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ class HttpUpgrader : protected HttpClient

protected:
void applyUpdate();
void updateFailed();
void downloadFailed();
void downloadComplete();
void fetchNextItem();

virtual int itemComplete(HttpConnection& client, bool success);
virtual int updateComplete(HttpConnection& client, bool success);
int itemComplete(HttpConnection& client, bool success);

protected:
ItemList items;
Expand Down
2 changes: 1 addition & 1 deletion samples/Basic_Ota/component.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
COMPONENT_SOC := esp*
COMPONENT_SOC := esp* host
COMPONENT_DEPENDS := OtaNetwork

HWCONFIG := ota
Expand Down

0 comments on commit 8ba44cb

Please sign in to comment.