Skip to content

Commit 8994d20

Browse files
committed
Simplify OTA header download timeout check
1 parent 6c4c09b commit 8994d20

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/Arduino_ESP32_OTA.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,31 +182,30 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
182182
DEBUG_VERBOSE("%s: Length of OTA binary according to HTTP header = %d bytes", __FUNCTION__, content_length_val);
183183

184184
/* Read the OTA header ... */
185-
bool is_http_data_timeout = false;
186-
for(int i = 0; i < sizeof(OtaHeader) && !is_http_data_timeout; i++)
185+
bool is_ota_header_timeout = false;
186+
unsigned long const start = millis();
187+
for (int i = 0; i < sizeof(OtaHeader);)
187188
{
188-
for(unsigned long const start = millis();;)
189+
is_ota_header_timeout = (millis() - start) > ARDUINO_ESP32_OTA_BINARY_HEADER_RECEIVE_TIMEOUT_ms;
190+
if (is_ota_header_timeout) break;
191+
192+
if (_client->available())
189193
{
190-
is_http_data_timeout = (millis() - start) > ARDUINO_ESP32_OTA_BINARY_BYTE_RECEIVE_TIMEOUT_ms;
191-
if (is_http_data_timeout)
192-
{
193-
DEBUG_ERROR("%s: timeout waiting data", __FUNCTION__);
194-
break;
195-
}
196-
if (_client->available())
197-
{
198-
_ota_header.buf[i] = _client->read();
199-
break;
200-
}
194+
_ota_header.buf[i++] = _client->read();
201195
}
202196
}
203-
//_client->read(_ota_header.buf, sizeof(OtaHeader));
204197

205-
/* ... and check first length ... */
198+
/* ... check for header download timeout ... */
199+
if (is_ota_header_timeout) {
200+
return static_cast<int>(Error::OtaHeaderTimeout);
201+
}
202+
203+
/* ... then check if OTA header length field matches HTTP content length... */
206204
if (_ota_header.header.len != (content_length_val - sizeof(_ota_header.header.len) - sizeof(_ota_header.header.crc32))) {
207205
return static_cast<int>(Error::OtaHeaderLength);
208206
}
209207

208+
/* ... and OTA magic number */
210209
if (_ota_header.header.magic_number != ARDUINO_ESP32_OTA_MAGIC)
211210
{
212211
return static_cast<int>(Error::OtaHeaterMagicNumber);

src/Arduino_ESP32_OTA.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
******************************************************************************/
4040

4141
static uint32_t const ARDUINO_ESP32_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms = 10000;
42+
static uint32_t const ARDUINO_ESP32_OTA_BINARY_HEADER_RECEIVE_TIMEOUT_ms = 10000;
4243
static uint32_t const ARDUINO_ESP32_OTA_BINARY_BYTE_RECEIVE_TIMEOUT_ms = 2000;
4344

4445
/******************************************************************************
@@ -71,6 +72,7 @@ class Arduino_ESP32_OTA
7172
OtaHeaderCrc = -10,
7273
OtaHeaterMagicNumber = -11,
7374
OtaDownload = -12,
75+
OtaHeaderTimeout = -13
7476
};
7577

7678
Arduino_ESP32_OTA();

0 commit comments

Comments
 (0)