Skip to content

Commit c2934f3

Browse files
authored
Merge pull request #23 from arduino-libraries/unor4
Allow subclassing
2 parents 8d6958e + 3525b0f commit c2934f3

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

Diff for: src/Arduino_ESP32_OTA.cpp

+49-33
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,6 @@
2626
#include "decompress/utility.h"
2727
#include "esp_ota_ops.h"
2828

29-
/* Used to bind local module function to actual class instance */
30-
static Arduino_ESP32_OTA * _esp_ota_obj_ptr = 0;
31-
32-
/******************************************************************************
33-
LOCAL MODULE FUNCTIONS
34-
******************************************************************************/
35-
36-
static uint8_t read_byte() {
37-
if(_esp_ota_obj_ptr) {
38-
return _esp_ota_obj_ptr->read_byte_from_network();
39-
}
40-
return -1;
41-
}
42-
43-
static void write_byte(uint8_t data) {
44-
if(_esp_ota_obj_ptr) {
45-
_esp_ota_obj_ptr->write_byte_to_flash(data);
46-
}
47-
}
48-
4929
/******************************************************************************
5030
CTOR/DTOR
5131
******************************************************************************/
@@ -57,6 +37,7 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
5737
,_crc32(0)
5838
,_ca_cert{amazon_root_ca}
5939
,_ca_cert_bundle{nullptr}
40+
,_magic(0)
6041
{
6142

6243
}
@@ -65,22 +46,22 @@ Arduino_ESP32_OTA::Arduino_ESP32_OTA()
6546
PUBLIC MEMBER FUNCTIONS
6647
******************************************************************************/
6748

68-
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin()
49+
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::begin(uint32_t magic)
6950
{
70-
_esp_ota_obj_ptr = this;
51+
/* initialize private variables */
52+
otaInit();
7153

7254
/* ... initialize CRC ... */
73-
_crc32 = 0xFFFFFFFF;
55+
crc32Init();
56+
57+
/* ... configure board Magic number */
58+
setMagic(magic);
7459

7560
if(!isCapable()) {
7661
DEBUG_ERROR("%s: board is not capable to perform OTA", __FUNCTION__);
7762
return Error::NoOtaStorage;
7863
}
7964

80-
/* initialize private variables */
81-
_ota_size = 0;
82-
_ota_header = {0};
83-
8465
if(Update.isRunning()) {
8566
Update.abort();
8667
DEBUG_DEBUG("%s: Aborting running update", __FUNCTION__);
@@ -107,6 +88,11 @@ void Arduino_ESP32_OTA::setCACertBundle (const uint8_t * bundle)
10788
}
10889
}
10990

91+
void Arduino_ESP32_OTA::setMagic(uint32_t magic)
92+
{
93+
_magic = magic;
94+
}
95+
11096
uint8_t Arduino_ESP32_OTA::read_byte_from_network()
11197
{
11298
bool is_http_data_timeout = false;
@@ -119,7 +105,7 @@ uint8_t Arduino_ESP32_OTA::read_byte_from_network()
119105
}
120106
if (_client->available()) {
121107
const uint8_t data = _client->read();
122-
_crc32 = crc_update(_crc32, &data, 1);
108+
crc32Update(data);
123109
return data;
124110
}
125111
}
@@ -262,7 +248,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
262248
}
263249

264250
/* ... and OTA magic number */
265-
if (_ota_header.header.magic_number != ARDUINO_ESP32_OTA_MAGIC)
251+
if (_ota_header.header.magic_number != _magic)
266252
{
267253
delete _client;
268254
_client = nullptr;
@@ -273,7 +259,7 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
273259
_crc32 = crc_update(_crc32, &_ota_header.header.magic_number, 12);
274260

275261
/* Download and decode OTA file */
276-
_ota_size = lzss_download(read_byte, write_byte, content_length_val - sizeof(_ota_header));
262+
_ota_size = lzss_download(this, content_length_val - sizeof(_ota_header));
277263

278264
if(_ota_size <= content_length_val - sizeof(_ota_header))
279265
{
@@ -289,10 +275,10 @@ int Arduino_ESP32_OTA::download(const char * ota_url)
289275

290276
Arduino_ESP32_OTA::Error Arduino_ESP32_OTA::update()
291277
{
292-
/* ... then finalise ... */
293-
_crc32 ^= 0xFFFFFFFF;
278+
/* ... then finalize ... */
279+
crc32Finalize();
294280

295-
if(_crc32 != _ota_header.header.crc32) {
281+
if(!crc32Verify()) {
296282
DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__);
297283
return Error::OtaHeaderCrc;
298284
}
@@ -316,3 +302,33 @@ bool Arduino_ESP32_OTA::isCapable()
316302
const esp_partition_t * ota_1 = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
317303
return ((ota_0 != nullptr) && (ota_1 != nullptr));
318304
}
305+
306+
/******************************************************************************
307+
PROTECTED MEMBER FUNCTIONS
308+
******************************************************************************/
309+
310+
void Arduino_ESP32_OTA::otaInit()
311+
{
312+
_ota_size = 0;
313+
_ota_header = {0};
314+
}
315+
316+
void Arduino_ESP32_OTA::crc32Init()
317+
{
318+
_crc32 = 0xFFFFFFFF;
319+
}
320+
321+
void Arduino_ESP32_OTA::crc32Update(const uint8_t data)
322+
{
323+
_crc32 = crc_update(_crc32, &data, 1);
324+
}
325+
326+
void Arduino_ESP32_OTA::crc32Finalize()
327+
{
328+
_crc32 ^= 0xFFFFFFFF;
329+
}
330+
331+
bool Arduino_ESP32_OTA::crc32Verify()
332+
{
333+
return (_crc32 == _ota_header.header.crc32);
334+
}

Diff for: src/Arduino_ESP32_OTA.h

+14-11
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ static uint32_t const ARDUINO_ESP32_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms = 10000;
4242
static uint32_t const ARDUINO_ESP32_OTA_BINARY_HEADER_RECEIVE_TIMEOUT_ms = 10000;
4343
static uint32_t const ARDUINO_ESP32_OTA_BINARY_BYTE_RECEIVE_TIMEOUT_ms = 2000;
4444

45-
/******************************************************************************
46-
* TYPEDEF
47-
******************************************************************************/
48-
49-
typedef uint8_t(*ArduinoEsp32OtaReadByteFuncPointer)(void);
50-
typedef void(*ArduinoEsp32OtaWriteByteFuncPointer)(uint8_t);
51-
5245
/******************************************************************************
5346
* CLASS DECLARATION
5447
******************************************************************************/
@@ -79,24 +72,34 @@ class Arduino_ESP32_OTA
7972
Arduino_ESP32_OTA();
8073
virtual ~Arduino_ESP32_OTA() { }
8174

82-
Arduino_ESP32_OTA::Error begin();
83-
void setCACert (const char *rootCA);
75+
Arduino_ESP32_OTA::Error begin(uint32_t magic = ARDUINO_ESP32_OTA_MAGIC);
76+
void setMagic(uint32_t magic);
77+
void setCACert(const char *rootCA);
8478
void setCACertBundle(const uint8_t * bundle);
8579
int download(const char * ota_url);
8680
uint8_t read_byte_from_network();
87-
void write_byte_to_flash(uint8_t data);
81+
virtual void write_byte_to_flash(uint8_t data);
8882
Arduino_ESP32_OTA::Error update();
8983
void reset();
9084
static bool isCapable();
9185

92-
private:
86+
protected:
87+
88+
void otaInit();
89+
void crc32Init();
90+
void crc32Update(const uint8_t data);
91+
void crc32Finalize();
92+
bool crc32Verify();
9393

94+
private:
9495
Client * _client;
9596
OtaHeader _ota_header;
9697
size_t _ota_size;
9798
uint32_t _crc32;
9899
const char * _ca_cert;
99100
const uint8_t * _ca_cert_bundle;
101+
uint32_t _magic;
102+
100103
};
101104

102105
#endif /* ARDUINO_ESP32_OTA_H_ */

Diff for: src/decompress/lzss.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
GLOBAL VARIABLES
2323
**************************************************************************************/
2424

25+
/* Used to bind local module function to actual class instance */
26+
static Arduino_ESP32_OTA * esp_ota_obj_ptr = 0;
27+
2528
static size_t LZSS_FILE_SIZE = 0;
26-
static ArduinoEsp32OtaReadByteFuncPointer read_byte_fptr = 0;
27-
static ArduinoEsp32OtaWriteByteFuncPointer write_byte_fptr = 0;
2829

2930
int bit_buffer = 0, bit_mask = 128;
3031
unsigned char buffer[N * 2];
@@ -38,7 +39,7 @@ static size_t bytes_read_fgetc = 0;
3839

3940
void lzss_fputc(int const c)
4041
{
41-
write_byte_fptr((uint8_t)c);
42+
esp_ota_obj_ptr->write_byte_to_flash((uint8_t)c);
4243

4344
/* write byte callback */
4445
bytes_written_fputc++;
@@ -56,7 +57,7 @@ int lzss_fgetc()
5657
return LZSS_EOF;
5758

5859
/* read byte callback */
59-
uint8_t const c = read_byte_fptr();
60+
uint8_t const c = esp_ota_obj_ptr->read_byte_from_network();
6061
bytes_read_fgetc++;
6162

6263
return c;
@@ -157,10 +158,9 @@ void lzss_decode(void)
157158
PUBLIC FUNCTIONS
158159
**************************************************************************************/
159160

160-
int lzss_download(ArduinoEsp32OtaReadByteFuncPointer read_byte, ArduinoEsp32OtaWriteByteFuncPointer write_byte, size_t const lzss_file_size)
161+
int lzss_download(Arduino_ESP32_OTA * instance, size_t const lzss_file_size)
161162
{
162-
read_byte_fptr = read_byte;
163-
write_byte_fptr = write_byte;
163+
esp_ota_obj_ptr = instance;
164164
LZSS_FILE_SIZE = lzss_file_size;
165165
bytes_written_fputc = 0;
166166
bytes_read_fgetc = 0;

Diff for: src/decompress/lzss.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
FUNCTION DEFINITION
1212
**************************************************************************************/
1313

14-
int lzss_download(ArduinoEsp32OtaReadByteFuncPointer read_byte, ArduinoEsp32OtaWriteByteFuncPointer write_byte, size_t const lzss_file_size);
14+
int lzss_download(Arduino_ESP32_OTA * instance, size_t const lzss_file_size);
1515

1616
#endif /* SSU_LZSS_H_ */

0 commit comments

Comments
 (0)