@@ -98,13 +98,14 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
9898 goto exit;
9999 }
100100
101- if (http_client->available () == 0 ) {
101+ const uint32_t available = http_client->available ();
102+ if (available == 0 ) {
102103 /* Avoid tight loop and allow yield */
103104 delay (1 );
104105 continue ;
105106 }
106107
107- http_res = http_client->read (context->buffer , context->buf_len );
108+ http_res = http_client->read (context->buffer , (available > context->buf_len ) ? context-> buf_len : available );
108109
109110 if (http_res < 0 ) {
110111 DEBUG_VERBOSE (" OTA ERROR: Download read error %d" , http_res);
@@ -159,7 +160,7 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
159160 for (uint8_t * cursor=(uint8_t *)buffer; cursor<buffer+buf_len; ) {
160161 switch (context->downloadState ) {
161162 case OtaDownloadHeader: {
162- uint32_t copied = buf_len < sizeof (context->header .buf ) ? buf_len : sizeof (context->header .buf );
163+ const uint32_t copied = context-> headerCopiedBytes + buf_len < sizeof (context->header .buf ) ? buf_len : sizeof (context->header .buf ) - context-> headerCopiedBytes ;
163164 memcpy (context->header .buf +context->headerCopiedBytes , buffer, copied);
164165 cursor += copied;
165166 context->headerCopiedBytes += copied;
@@ -178,22 +179,25 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
178179 context->downloadState = OtaDownloadMagicNumberMismatch;
179180 return ;
180181 }
182+ context->downloadedSize += sizeof (context->header .buf );
181183 }
182184
183185 break ;
184186 }
185187 case OtaDownloadFile: {
186- uint32_t contentLength = http_client->contentLength ();
187- context->decoder .decompress (cursor, buf_len - (cursor-buffer)); // TODO verify return value
188+ const uint32_t contentLength = http_client->contentLength ();
189+ const uint32_t dataLeft = buf_len - (cursor-buffer);
190+ context->decoder .decompress (cursor, dataLeft); // TODO verify return value
188191
189192 context->calculatedCrc32 = crc_update (
190193 context->calculatedCrc32 ,
191194 cursor,
192- buf_len - (cursor-buffer)
195+ dataLeft
193196 );
194197
195- cursor += buf_len - (cursor-buffer);
196- context->downloadedSize += (cursor-buffer);
198+ cursor += dataLeft;
199+ context->downloadedSize += dataLeft;
200+
197201
198202 if ((millis () - context->lastReportTime ) > 10000 ) { // Report the download progress each X millisecond
199203 DEBUG_VERBOSE (" OTA Download Progress %d/%d" , context->downloadedSize , contentLength);
0 commit comments