Skip to content

Commit 3333846

Browse files
authored
Merge pull request #514 from pennam/ota-fix
Fix ota download byte count
2 parents b7f9b33 + 9412661 commit 3333846

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Diff for: src/ota/interface/OTAInterfaceDefault.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
159159
for(uint8_t* cursor=(uint8_t*)buffer; cursor<buffer+buf_len; ) {
160160
switch(context->downloadState) {
161161
case OtaDownloadHeader: {
162-
uint32_t copied = buf_len < sizeof(context->header.buf) ? buf_len : sizeof(context->header.buf);
163-
memcpy(context->header.buf+context->headerCopiedBytes, buffer, copied);
164-
cursor += copied;
165-
context->headerCopiedBytes += copied;
162+
const uint32_t headerLeft = context->headerCopiedBytes + buf_len <= sizeof(context->header.buf) ? buf_len : sizeof(context->header.buf) - context->headerCopiedBytes;
163+
memcpy(context->header.buf+context->headerCopiedBytes, buffer, headerLeft);
164+
cursor += headerLeft;
165+
context->headerCopiedBytes += headerLeft;
166166

167167
// when finished go to next state
168168
if(sizeof(context->header.buf) == context->headerCopiedBytes) {
@@ -178,22 +178,24 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
178178
context->downloadState = OtaDownloadMagicNumberMismatch;
179179
return;
180180
}
181+
context->downloadedSize += sizeof(context->header.buf);
181182
}
182183

183184
break;
184185
}
185186
case OtaDownloadFile: {
186-
uint32_t contentLength = http_client->contentLength();
187-
context->decoder.decompress(cursor, buf_len - (cursor-buffer)); // TODO verify return value
187+
const uint32_t contentLength = http_client->contentLength();
188+
const uint32_t dataLeft = buf_len - (cursor-buffer);
189+
context->decoder.decompress(cursor, dataLeft); // TODO verify return value
188190

189191
context->calculatedCrc32 = crc_update(
190192
context->calculatedCrc32,
191193
cursor,
192-
buf_len - (cursor-buffer)
194+
dataLeft
193195
);
194196

195-
cursor += buf_len - (cursor-buffer);
196-
context->downloadedSize += (cursor-buffer);
197+
cursor += dataLeft;
198+
context->downloadedSize += dataLeft;
197199

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

0 commit comments

Comments
 (0)