@@ -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);
0 commit comments