@@ -76,7 +76,16 @@ Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin()
76
76
DEBUG_ERROR (" %s: board is not capable to perform OTA" , __FUNCTION__);
77
77
return Error::NoOtaStorage;
78
78
}
79
-
79
+
80
+ /* initialize private variables */
81
+ _ota_size = 0 ;
82
+ _ota_header = {0 };
83
+
84
+ if (Update.isRunning ()) {
85
+ Update.abort ();
86
+ DEBUG_DEBUG (" %s: Aborting running update" , __FUNCTION__);
87
+ }
88
+
80
89
if (!Update.begin (UPDATE_SIZE_UNKNOWN)) {
81
90
DEBUG_ERROR (" %s: failed to initialize flash update" , __FUNCTION__);
82
91
return Error::OtaStorageInit;
@@ -147,6 +156,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
147
156
if (!_client->connect (url.host_ .c_str (), port))
148
157
{
149
158
DEBUG_ERROR (" %s: Connection failure with OTA storage server %s" , __FUNCTION__, url.host_ .c_str ());
159
+ delete _client;
160
+ _client = nullptr ;
150
161
return static_cast <int >(Error::ServerConnectError);
151
162
}
152
163
@@ -177,6 +188,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
177
188
if (!is_header_complete)
178
189
{
179
190
DEBUG_ERROR (" %s: Error receiving HTTP header %s" , __FUNCTION__, is_http_header_timeout ? " (timeout)" :" " );
191
+ delete _client;
192
+ _client = nullptr ;
180
193
return static_cast <int >(Error::HttpHeaderError);
181
194
}
182
195
@@ -207,6 +220,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
207
220
if (!content_length_ptr)
208
221
{
209
222
DEBUG_ERROR (" %s: Failure to extract content length from http header" , __FUNCTION__);
223
+ delete _client;
224
+ _client = nullptr ;
210
225
return static_cast <int >(Error::ParseHttpHeader);
211
226
}
212
227
/* Find start of numerical value. */
@@ -234,17 +249,23 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
234
249
235
250
/* ... check for header download timeout ... */
236
251
if (is_ota_header_timeout) {
252
+ delete _client;
253
+ _client = nullptr ;
237
254
return static_cast <int >(Error::OtaHeaderTimeout);
238
255
}
239
256
240
257
/* ... then check if OTA header length field matches HTTP content length... */
241
258
if (_ota_header.header .len != (content_length_val - sizeof (_ota_header.header .len ) - sizeof (_ota_header.header .crc32 ))) {
259
+ delete _client;
260
+ _client = nullptr ;
242
261
return static_cast <int >(Error::OtaHeaderLength);
243
262
}
244
263
245
264
/* ... and OTA magic number */
246
265
if (_ota_header.header .magic_number != ARDUINO_ESP32_OTA_MAGIC)
247
266
{
267
+ delete _client;
268
+ _client = nullptr ;
248
269
return static_cast <int >(Error::OtaHeaterMagicNumber);
249
270
}
250
271
@@ -256,9 +277,13 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
256
277
257
278
if (_ota_size <= content_length_val - sizeof (_ota_header))
258
279
{
280
+ delete _client;
281
+ _client = nullptr ;
259
282
return static_cast <int >(Error::OtaDownload);
260
283
}
261
284
285
+ delete _client;
286
+ _client = nullptr ;
262
287
return _ota_size;
263
288
}
264
289
0 commit comments