Skip to content

Commit 8d6958e

Browse files
authored
Merge pull request #21 from pennam/restartable
Add the possibility to restart OTA multiple times
2 parents 5ccb160 + c644a2f commit 8d6958e

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Diff for: src/Arduino_ESP32_OTA.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin()
7676
DEBUG_ERROR("%s: board is not capable to perform OTA", __FUNCTION__);
7777
return Error::NoOtaStorage;
7878
}
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+
8089
if(!Update.begin(UPDATE_SIZE_UNKNOWN)) {
8190
DEBUG_ERROR("%s: failed to initialize flash update", __FUNCTION__);
8291
return Error::OtaStorageInit;
@@ -147,6 +156,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
147156
if (!_client->connect(url.host_.c_str(), port))
148157
{
149158
DEBUG_ERROR("%s: Connection failure with OTA storage server %s", __FUNCTION__, url.host_.c_str());
159+
delete _client;
160+
_client = nullptr;
150161
return static_cast<int>(Error::ServerConnectError);
151162
}
152163

@@ -177,6 +188,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
177188
if (!is_header_complete)
178189
{
179190
DEBUG_ERROR("%s: Error receiving HTTP header %s", __FUNCTION__, is_http_header_timeout ? "(timeout)":"");
191+
delete _client;
192+
_client = nullptr;
180193
return static_cast<int>(Error::HttpHeaderError);
181194
}
182195

@@ -207,6 +220,8 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
207220
if (!content_length_ptr)
208221
{
209222
DEBUG_ERROR("%s: Failure to extract content length from http header", __FUNCTION__);
223+
delete _client;
224+
_client = nullptr;
210225
return static_cast<int>(Error::ParseHttpHeader);
211226
}
212227
/* Find start of numerical value. */
@@ -234,17 +249,23 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
234249

235250
/* ... check for header download timeout ... */
236251
if (is_ota_header_timeout) {
252+
delete _client;
253+
_client = nullptr;
237254
return static_cast<int>(Error::OtaHeaderTimeout);
238255
}
239256

240257
/* ... then check if OTA header length field matches HTTP content length... */
241258
if (_ota_header.header.len != (content_length_val - sizeof(_ota_header.header.len) - sizeof(_ota_header.header.crc32))) {
259+
delete _client;
260+
_client = nullptr;
242261
return static_cast<int>(Error::OtaHeaderLength);
243262
}
244263

245264
/* ... and OTA magic number */
246265
if (_ota_header.header.magic_number != ARDUINO_ESP32_OTA_MAGIC)
247266
{
267+
delete _client;
268+
_client = nullptr;
248269
return static_cast<int>(Error::OtaHeaterMagicNumber);
249270
}
250271

@@ -256,9 +277,13 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
256277

257278
if(_ota_size <= content_length_val - sizeof(_ota_header))
258279
{
280+
delete _client;
281+
_client = nullptr;
259282
return static_cast<int>(Error::OtaDownload);
260283
}
261284

285+
delete _client;
286+
_client = nullptr;
262287
return _ota_size;
263288
}
264289

Diff for: src/decompress/lzss.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int bit_buffer = 0, bit_mask = 128;
3030
unsigned char buffer[N * 2];
3131

3232
static size_t bytes_written_fputc = 0;
33+
static size_t bytes_read_fgetc = 0;
3334

3435
/**************************************************************************************
3536
PRIVATE FUNCTIONS
@@ -45,8 +46,6 @@ void lzss_fputc(int const c)
4546

4647
int lzss_fgetc()
4748
{
48-
static size_t bytes_read_fgetc = 0;
49-
5049
/* lzss_file_size is set within SSUBoot:main
5150
* and contains the size of the LZSS file. Once
5251
* all those bytes have been read its time to return
@@ -163,6 +162,8 @@ int lzss_download(ArduinoEsp32OtaReadByteFuncPointer read_byte, ArduinoEsp32OtaW
163162
read_byte_fptr = read_byte;
164163
write_byte_fptr = write_byte;
165164
LZSS_FILE_SIZE = lzss_file_size;
165+
bytes_written_fputc = 0;
166+
bytes_read_fgetc = 0;
166167
lzss_decode();
167168
return bytes_written_fputc;
168169
}

0 commit comments

Comments
 (0)