Skip to content

Commit 59cb642

Browse files
committed
enable compression by default. handle optional compression, and update uncompressed firmware checksum
1 parent 1088768 commit 59cb642

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/ThingerESP32OTA.h

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#ifndef THINGER_ESP32OTA_H
22
#define THINGER_ESP32OTA_H
33

4+
#ifndef THINGER_OTA_COMPRESSION
5+
#define THINGER_OTA_COMPRESSION 1
6+
#endif
7+
48
#include "ThingerOTA.h"
59
#include <Update.h>
610

7-
#if THINGER_ESP32OTA_COMPRESSION
11+
#if THINGER_OTA_COMPRESSION
812
#include "rom/miniz.h"
913
#endif
1014

@@ -28,11 +32,6 @@ class ThingerESP32OTA : public ThingerOTA{
2832

2933
// request a md5 checksum
3034
options["checksum"] = "md5";
31-
32-
// add gzip compression option if THINGER_ESP32OTA_UNCOMPRESSED is not defined
33-
#if THINGER_ESP32OTA_COMPRESSION
34-
options["compression"] = "zlib";
35-
#endif
3635
}
3736

3837
bool begin_ota(const char* firmware, const char* version, size_t size, pson& options, pson& state) override
@@ -61,19 +60,26 @@ class ThingerESP32OTA : public ThingerOTA{
6160
Update.setMD5(md5_checksum_);
6261
}
6362

64-
#if THINGER_ESP32OTA_COMPRESSION
65-
// initialize compression parameters (0 disabled)
66-
remaining_compressed_ = options["compressed_size"];
67-
if(init && remaining_compressed_){
68-
inflator_ = tinfl_decompressor{};
63+
#if THINGER_OTA_COMPRESSION
64+
if(is_compressed()){
65+
// initialize compression parameters (0 disabled)
66+
remaining_compressed_ = options["compressed_size"];
67+
if(init && remaining_compressed_){
68+
inflator_ = tinfl_decompressor{};
69+
}
6970
}
7071
#endif
7172

7273
return init;
7374
}
7475

75-
#if THINGER_ESP32OTA_COMPRESSION
76+
#if THINGER_OTA_COMPRESSION
7677
bool write_ota(uint8_t* buffer, size_t bytes, pson& state) override{
78+
if(!is_compressed()){
79+
// ensure the compression is enabled
80+
return do_write(buffer, bytes, state);
81+
}
82+
7783
static uint8_t out_buf[32768];
7884
static uint8_t *next_out = out_buf;
7985
int status = TINFL_STATUS_NEEDS_MORE_INPUT;
@@ -98,6 +104,7 @@ class ThingerESP32OTA : public ThingerOTA{
98104
size_t bytes_in_out_buf = next_out - out_buf;
99105
if (status == TINFL_STATUS_DONE || bytes_in_out_buf == sizeof(out_buf)) {
100106
if(!do_write(out_buf, bytes_in_out_buf, state)) return false;
107+
update_firmware_checksum(out_buf, bytes_in_out_buf);
101108
next_out = out_buf;
102109
}
103110
} // while
@@ -129,7 +136,13 @@ class ThingerESP32OTA : public ThingerOTA{
129136
return true;
130137
}
131138

132-
protected:
139+
protected:
140+
141+
#if THINGER_OTA_COMPRESSION
142+
const char* supported_compression() override{
143+
return "zlib";
144+
}
145+
#endif
133146

134147
bool do_write(uint8_t* buffer, size_t bytes, pson& state){
135148
if(Update.write(buffer, bytes) != bytes){
@@ -141,7 +154,7 @@ class ThingerESP32OTA : public ThingerOTA{
141154

142155
private:
143156
char md5_checksum_[33];
144-
#if THINGER_ESP32OTA_COMPRESSION
157+
#if THINGER_OTA_COMPRESSION
145158
size_t remaining_compressed_ = 0;
146159
tinfl_decompressor inflator_;
147160
#endif

0 commit comments

Comments
 (0)