@@ -98,13 +98,14 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
98
98
goto exit ;
99
99
}
100
100
101
- if (http_client->available () == 0 ) {
101
+ uint32_t available = http_client->available ();
102
+ if (available == 0 ) {
102
103
/* Avoid tight loop and allow yield */
103
104
delay (1 );
104
105
continue ;
105
106
}
106
107
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 );
108
109
109
110
if (http_res < 0 ) {
110
111
DEBUG_VERBOSE (" OTA ERROR: Download read error %d" , http_res);
@@ -159,7 +160,7 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
159
160
for (uint8_t * cursor=(uint8_t *)buffer; cursor<buffer+buf_len; ) {
160
161
switch (context->downloadState ) {
161
162
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 ;
163
164
memcpy (context->header .buf +context->headerCopiedBytes , buffer, copied);
164
165
cursor += copied;
165
166
context->headerCopiedBytes += copied;
@@ -178,22 +179,25 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
178
179
context->downloadState = OtaDownloadMagicNumberMismatch;
179
180
return ;
180
181
}
182
+ context->downloadedSize += sizeof (context->header .buf );
181
183
}
182
184
183
185
break ;
184
186
}
185
187
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
188
191
189
192
context->calculatedCrc32 = crc_update (
190
193
context->calculatedCrc32 ,
191
194
cursor,
192
- buf_len - (cursor-buffer)
195
+ dataLeft
193
196
);
194
197
195
- cursor += buf_len - (cursor-buffer);
196
- context->downloadedSize += (cursor-buffer);
198
+ cursor += dataLeft;
199
+ context->downloadedSize += dataLeft;
200
+
197
201
198
202
if ((millis () - context->lastReportTime ) > 10000 ) { // Report the download progress each X millisecond
199
203
DEBUG_VERBOSE (" OTA Download Progress %d/%d" , context->downloadedSize , contentLength);
0 commit comments