26
26
#include " HTTPUpdate.h"
27
27
#include < StreamString.h>
28
28
29
+ #include < esp_partition.h>
30
+ #include < esp_ota_ops.h> // get running partition
31
+
29
32
// To do extern "C" uint32_t _SPIFFS_start;
30
33
// To do extern "C" uint32_t _SPIFFS_end;
31
34
@@ -121,6 +124,32 @@ String HTTPUpdate::getLastErrorString(void)
121
124
}
122
125
123
126
127
+ String getSketchSHA256 () {
128
+ const size_t HASH_LEN = 32 ; // SHA-256 digest length
129
+
130
+ uint8_t sha_256[HASH_LEN] = { 0 };
131
+
132
+ // get sha256 digest for running partition
133
+ if (esp_partition_get_sha256 (esp_ota_get_running_partition (), sha_256) == 0 ) {
134
+ char buffer[2 * HASH_LEN + 1 ];
135
+
136
+ for (size_t index = 0 ; index < HASH_LEN; index ++) {
137
+ uint8_t nibble = (sha_256[index ] & 0xf0 ) >> 4 ;
138
+ buffer[2 * index ] = nibble < 10 ? char (nibble + ' 0' ) : char (nibble - 10 + ' A' );
139
+
140
+ nibble = sha_256[index ] & 0x0f ;
141
+ buffer[2 * index + 1 ] = nibble < 10 ? char (nibble + ' 0' ) : char (nibble - 10 + ' A' );
142
+ }
143
+
144
+ buffer[2 * HASH_LEN] = ' \0 ' ;
145
+
146
+ return String (buffer);
147
+ } else {
148
+
149
+ return String ();
150
+ }
151
+ }
152
+
124
153
/* *
125
154
*
126
155
* @param http HTTPClient *
@@ -139,10 +168,15 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
139
168
http.addHeader (F (" Cache-Control" ), F (" no-cache" ));
140
169
http.addHeader (F (" x-ESP32-STA-MAC" ), WiFi.macAddress ());
141
170
http.addHeader (F (" x-ESP32-AP-MAC" ), WiFi.softAPmacAddress ());
142
- // To do http.addHeader(F("x-ESP32-free-space"), String(ESP.getFreeSketchSpace()));
143
- // To do http.addHeader(F("x-ESP32-sketch-size"), String(ESP.getSketchSize()));
171
+ http.addHeader (F (" x-ESP32-free-space" ), String (ESP.getFreeSketchSpace ()));
172
+ http.addHeader (F (" x-ESP32-sketch-size" ), String (ESP.getSketchSize ()));
144
173
// To do http.addHeader(F("x-ESP32-sketch-md5"), String(ESP.getSketchMD5()));
145
- // To do http.addHeader(F("x-ESP32-chip-size"), String(ESP.getFlashChipRealSize()));
174
+ // Sketch MD5 is not supported by the core, but SHA256 is, so add a SHA256 instead
175
+ String sketchSHA256 = getSketchSHA256 ();
176
+ if (sketchSHA256.length () != 0 ) {
177
+ http.addHeader (F (" x-ESP32-sketch-sha256" ), sketchSHA256);
178
+ }
179
+ http.addHeader (F (" x-ESP32-chip-size" ), String (ESP.getFlashChipSize ()));
146
180
http.addHeader (F (" x-ESP32-sdk-version" ), ESP.getSdkVersion ());
147
181
148
182
if (spiffs) {
@@ -183,8 +217,8 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
183
217
}
184
218
185
219
log_d (" ESP32 info:\n " );
186
- // To do log_d(" - free Space: %d\n", ESP.getFreeSketchSpace());
187
- // To do log_d(" - current Sketch Size: %d\n", ESP.getSketchSize());
220
+ log_d (" - free Space: %d\n " , ESP.getFreeSketchSpace ());
221
+ log_d (" - current Sketch Size: %d\n " , ESP.getSketchSize ());
188
222
189
223
if (currentVersion && currentVersion[0 ] != 0x00 ) {
190
224
log_d (" - current version: %s\n " , currentVersion.c_str () );
@@ -201,10 +235,10 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
201
235
// To do startUpdate = false;
202
236
// To do }
203
237
} else {
204
- // To do if(len > (int) ESP.getFreeSketchSpace()) {
205
- // To do log_e("FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
206
- // To do startUpdate = false;
207
- // To do }
238
+ if (len > (int ) ESP.getFreeSketchSpace ()) {
239
+ log_e (" FreeSketchSpace to low (%d) needed: %d\n " , ESP.getFreeSketchSpace (), len);
240
+ startUpdate = false ;
241
+ }
208
242
}
209
243
210
244
if (!startUpdate) {
0 commit comments