@@ -98,7 +98,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
98
98
goto exit ;
99
99
}
100
100
101
- if (http_client->available () == 0 ) {
101
+ if (! http_client->available ()) {
102
102
/* Avoid tight loop and allow yield */
103
103
delay (1 );
104
104
continue ;
@@ -159,10 +159,10 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
159
159
for (uint8_t * cursor=(uint8_t *)buffer; cursor<buffer+buf_len; ) {
160
160
switch (context->downloadState ) {
161
161
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 ;
166
166
167
167
// when finished go to next state
168
168
if (sizeof (context->header .buf ) == context->headerCopiedBytes ) {
@@ -178,22 +178,25 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
178
178
context->downloadState = OtaDownloadMagicNumberMismatch;
179
179
return ;
180
180
}
181
+ context->downloadedSize += sizeof (context->header .buf );
181
182
}
182
183
183
184
break ;
184
185
}
185
186
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
188
190
189
191
context->calculatedCrc32 = crc_update (
190
192
context->calculatedCrc32 ,
191
193
cursor,
192
- buf_len - (cursor-buffer)
194
+ dataLeft
193
195
);
194
196
195
- cursor += buf_len - (cursor-buffer);
196
- context->downloadedSize += (cursor-buffer);
197
+ cursor += dataLeft;
198
+ context->downloadedSize += dataLeft;
199
+
197
200
198
201
if ((millis () - context->lastReportTime ) > 10000 ) { // Report the download progress each X millisecond
199
202
DEBUG_VERBOSE (" OTA Download Progress %d/%d" , context->downloadedSize , contentLength);
0 commit comments