Skip to content

Commit

Permalink
fix ota download byte count
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Nov 8, 2024
1 parent b7f9b33 commit 84cda53
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/ota/interface/OTAInterfaceDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
goto exit;
}

if(http_client->available() == 0) {
const uint32_t available = http_client->available();
if(available == 0) {
/* Avoid tight loop and allow yield */
delay(1);
continue;
}

http_res = http_client->read(context->buffer, context->buf_len);
http_res = http_client->read(context->buffer, (available > context->buf_len) ? context->buf_len : available);

if(http_res < 0) {
DEBUG_VERBOSE("OTA ERROR: Download read error %d", http_res);
Expand Down Expand Up @@ -159,7 +160,7 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
for(uint8_t* cursor=(uint8_t*)buffer; cursor<buffer+buf_len; ) {
switch(context->downloadState) {
case OtaDownloadHeader: {
uint32_t copied = buf_len < sizeof(context->header.buf) ? buf_len : sizeof(context->header.buf);
const uint32_t copied = context->headerCopiedBytes + buf_len < sizeof(context->header.buf) ? buf_len : sizeof(context->header.buf) - context->headerCopiedBytes;
memcpy(context->header.buf+context->headerCopiedBytes, buffer, copied);
cursor += copied;
context->headerCopiedBytes += copied;
Expand All @@ -178,22 +179,25 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
context->downloadState = OtaDownloadMagicNumberMismatch;
return;
}
context->downloadedSize += sizeof(context->header.buf);
}

break;
}
case OtaDownloadFile: {
uint32_t contentLength = http_client->contentLength();
context->decoder.decompress(cursor, buf_len - (cursor-buffer)); // TODO verify return value
const uint32_t contentLength = http_client->contentLength();
const uint32_t dataLeft = buf_len - (cursor-buffer);
context->decoder.decompress(cursor, dataLeft); // TODO verify return value

context->calculatedCrc32 = crc_update(
context->calculatedCrc32,
cursor,
buf_len - (cursor-buffer)
dataLeft
);

cursor += buf_len - (cursor-buffer);
context->downloadedSize += (cursor-buffer);
cursor += dataLeft;
context->downloadedSize += dataLeft;


if((millis() - context->lastReportTime) > 10000) { // Report the download progress each X millisecond
DEBUG_VERBOSE("OTA Download Progress %d/%d", context->downloadedSize, contentLength);
Expand Down

0 comments on commit 84cda53

Please sign in to comment.