1
1
#ifndef THINGER_ESP32OTA_H
2
2
#define THINGER_ESP32OTA_H
3
3
4
+ #ifndef THINGER_OTA_COMPRESSION
5
+ #define THINGER_OTA_COMPRESSION 1
6
+ #endif
7
+
4
8
#include " ThingerOTA.h"
5
9
#include < Update.h>
6
10
7
- #if THINGER_ESP32OTA_COMPRESSION
11
+ #if THINGER_OTA_COMPRESSION
8
12
#include " rom/miniz.h"
9
13
#endif
10
14
@@ -28,11 +32,6 @@ class ThingerESP32OTA : public ThingerOTA{
28
32
29
33
// request a md5 checksum
30
34
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
36
35
}
37
36
38
37
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{
61
60
Update.setMD5 (md5_checksum_);
62
61
}
63
62
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
+ }
69
70
}
70
71
#endif
71
72
72
73
return init;
73
74
}
74
75
75
- #if THINGER_ESP32OTA_COMPRESSION
76
+ #if THINGER_OTA_COMPRESSION
76
77
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
+
77
83
static uint8_t out_buf[32768 ];
78
84
static uint8_t *next_out = out_buf;
79
85
int status = TINFL_STATUS_NEEDS_MORE_INPUT;
@@ -98,6 +104,7 @@ class ThingerESP32OTA : public ThingerOTA{
98
104
size_t bytes_in_out_buf = next_out - out_buf;
99
105
if (status == TINFL_STATUS_DONE || bytes_in_out_buf == sizeof (out_buf)) {
100
106
if (!do_write (out_buf, bytes_in_out_buf, state)) return false ;
107
+ update_firmware_checksum (out_buf, bytes_in_out_buf);
101
108
next_out = out_buf;
102
109
}
103
110
} // while
@@ -129,7 +136,13 @@ class ThingerESP32OTA : public ThingerOTA{
129
136
return true ;
130
137
}
131
138
132
- protected:
139
+ protected:
140
+
141
+ #if THINGER_OTA_COMPRESSION
142
+ const char * supported_compression () override {
143
+ return " zlib" ;
144
+ }
145
+ #endif
133
146
134
147
bool do_write (uint8_t * buffer, size_t bytes, pson& state){
135
148
if (Update.write (buffer, bytes) != bytes){
@@ -141,7 +154,7 @@ class ThingerESP32OTA : public ThingerOTA{
141
154
142
155
private:
143
156
char md5_checksum_[33 ];
144
- #if THINGER_ESP32OTA_COMPRESSION
157
+ #if THINGER_OTA_COMPRESSION
145
158
size_t remaining_compressed_ = 0 ;
146
159
tinfl_decompressor inflator_;
147
160
#endif
0 commit comments