@@ -164,10 +164,10 @@ int Arduino_ESP32_OTA::startDownload(const char * ota_url)
164
164
}
165
165
}
166
166
167
- Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload ()
167
+ int Arduino_ESP32_OTA::progressDownload ()
168
168
{
169
- int http_res = 0 ;
170
- Arduino_ESP32_OTA::OTADownloadState res = OtaDownloadHeader ;
169
+ int http_res = static_cast < int >(Error::None); ;
170
+ int res = 0 ;
171
171
172
172
if (_http_client->available () == 0 ) {
173
173
goto exit ;
@@ -177,7 +177,7 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
177
177
178
178
if (http_res < 0 ) {
179
179
DEBUG_VERBOSE (" OTA ERROR: Download read error %d" , http_res);
180
- res = OtaDownloadError ;
180
+ res = static_cast < int >(Error::OtaDownload) ;
181
181
goto exit ;
182
182
}
183
183
@@ -201,7 +201,7 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
201
201
202
202
if (_context->header .header .magic_number != _magic) {
203
203
_context->downloadState = OtaDownloadMagicNumberMismatch;
204
- res = _context-> downloadState ;
204
+ res = static_cast < int >(Error::OtaHeaderMagicNumber) ;
205
205
206
206
goto exit ;
207
207
}
@@ -224,20 +224,21 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
224
224
// TODO there should be no more bytes available when the download is completed
225
225
if (_context->downloadedSize == _http_client->contentLength ()) {
226
226
_context->downloadState = OtaDownloadCompleted;
227
- res = _context-> downloadState ;
227
+ res = 1 ;
228
228
}
229
229
230
230
if (_context->downloadedSize > _http_client->contentLength ()) {
231
231
_context->downloadState = OtaDownloadError;
232
- res = _context-> downloadState ;
232
+ res = static_cast < int >(Error::OtaDownload) ;
233
233
}
234
234
// TODO fail if we exceed a timeout? and available is 0 (client is broken)
235
235
break ;
236
236
case OtaDownloadCompleted:
237
+ res = 1 ;
237
238
goto exit ;
238
239
default :
239
240
_context->downloadState = OtaDownloadError;
240
- res = _context-> downloadState ;
241
+ res = static_cast < int >(Error::OtaDownload) ;
241
242
goto exit ;
242
243
}
243
244
}
@@ -264,7 +265,16 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
264
265
265
266
int Arduino_ESP32_OTA::downloadProgress ()
266
267
{
267
- return _context->downloadedSize ;
268
+ if (_context->error != Error::None) {
269
+ return static_cast <int >(_context->error );
270
+ } else {
271
+ return _context->downloadedSize ;
272
+ }
273
+ }
274
+
275
+ size_t Arduino_ESP32_OTA::downloadSize ()
276
+ {
277
+ return _http_client!=nullptr ? _http_client->contentLength () : 0 ;
268
278
}
269
279
270
280
int Arduino_ESP32_OTA::download (const char * ota_url)
@@ -275,22 +285,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
275
285
return err;
276
286
}
277
287
278
- OTADownloadState res = OtaDownloadHeader;
279
-
280
- while ((res = progressDownload ()) == OtaDownloadFile || res == OtaDownloadHeader);
288
+ int res = 0 ;
289
+ while ((res = progressDownload ()) <= 0 );
281
290
282
-
283
- if (res == OtaDownloadCompleted) {
284
- return _context->writtenBytes ;
285
- } else {
286
- switch (res) {
287
- case OtaDownloadMagicNumberMismatch:
288
- return static_cast <int >(Error::OtaHeaderMagicNumber);
289
- case OtaDownloadError:
290
- default :
291
- return static_cast <int >(Error::OtaDownload);
292
- }
293
- }
291
+ return res == 1 ? _context->writtenBytes : res;
294
292
}
295
293
296
294
void Arduino_ESP32_OTA::clean ()
@@ -368,6 +366,7 @@ Arduino_ESP32_OTA::Context::Context(
368
366
, calculatedCrc32(0xFFFFFFFF )
369
367
, headerCopiedBytes(0 )
370
368
, downloadedSize(0 )
369
+ , error(Error::None)
371
370
, decoder(putc) {
372
371
strcpy (this ->url , url);
373
372
}
0 commit comments