Skip to content

Commit 38bdbc3

Browse files
fixup! Improving ESP32 ota to be inline with new IoTCloud implementation
1 parent 862cd25 commit 38bdbc3

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

Diff for: src/Arduino_ESP32_OTA.cpp

+23-24
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ int Arduino_ESP32_OTA::startDownload(const char * ota_url)
164164
}
165165
}
166166

167-
Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
167+
int Arduino_ESP32_OTA::progressDownload()
168168
{
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;
171171

172172
if(_http_client->available() == 0) {
173173
goto exit;
@@ -177,7 +177,7 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
177177

178178
if(http_res < 0) {
179179
DEBUG_VERBOSE("OTA ERROR: Download read error %d", http_res);
180-
res = OtaDownloadError;
180+
res = static_cast<int>(Error::OtaDownload);
181181
goto exit;
182182
}
183183

@@ -201,7 +201,7 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
201201

202202
if(_context->header.header.magic_number != _magic) {
203203
_context->downloadState = OtaDownloadMagicNumberMismatch;
204-
res = _context->downloadState;
204+
res = static_cast<int>(Error::OtaHeaderMagicNumber);
205205

206206
goto exit;
207207
}
@@ -224,20 +224,21 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
224224
// TODO there should be no more bytes available when the download is completed
225225
if(_context->downloadedSize == _http_client->contentLength()) {
226226
_context->downloadState = OtaDownloadCompleted;
227-
res = _context->downloadState;
227+
res = 1;
228228
}
229229

230230
if(_context->downloadedSize > _http_client->contentLength()) {
231231
_context->downloadState = OtaDownloadError;
232-
res = _context->downloadState;
232+
res = static_cast<int>(Error::OtaDownload);
233233
}
234234
// TODO fail if we exceed a timeout? and available is 0 (client is broken)
235235
break;
236236
case OtaDownloadCompleted:
237+
res = 1;
237238
goto exit;
238239
default:
239240
_context->downloadState = OtaDownloadError;
240-
res = _context->downloadState;
241+
res = static_cast<int>(Error::OtaDownload);
241242
goto exit;
242243
}
243244
}
@@ -264,7 +265,16 @@ Arduino_ESP32_OTA::OTADownloadState Arduino_ESP32_OTA::progressDownload()
264265

265266
int Arduino_ESP32_OTA::downloadProgress()
266267
{
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;
268278
}
269279

270280
int Arduino_ESP32_OTA::download(const char * ota_url)
@@ -275,22 +285,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
275285
return err;
276286
}
277287

278-
OTADownloadState res = OtaDownloadHeader;
279-
280-
while((res = progressDownload()) == OtaDownloadFile || res == OtaDownloadHeader);
288+
int res = 0;
289+
while((res = progressDownload()) <= 0);
281290

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;
294292
}
295293

296294
void Arduino_ESP32_OTA::clean()
@@ -368,6 +366,7 @@ Arduino_ESP32_OTA::Context::Context(
368366
, calculatedCrc32(0xFFFFFFFF)
369367
, headerCopiedBytes(0)
370368
, downloadedSize(0)
369+
, error(Error::None)
371370
, decoder(putc) {
372371
strcpy(this->url, url);
373372
}

0 commit comments

Comments
 (0)