Skip to content

Commit ecce35d

Browse files
committed
allow SPIFFS
1 parent 1368a82 commit ecce35d

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
*
2424
*/
2525

26-
2726
#include "ESP8266httpUpdate.h"
2827

28+
extern "C" uint32_t _SPIFFS_start;
29+
extern "C" uint32_t _SPIFFS_end;
2930

3031
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
3132
}
@@ -46,6 +47,19 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * cur
4647
return handleUpdate(&http, current_version);
4748
}
4849

50+
/**
51+
*
52+
* @param url const char *
53+
* @param current_version const char *
54+
* @param httpsFingerprint const char *
55+
* @return t_httpUpdate_return
56+
*/
57+
t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint) {
58+
HTTPClient http;
59+
http.begin(url, httpsFingerprint);
60+
return handleUpdate(&http, current_version, false, true);
61+
}
62+
4963
/**
5064
*
5165
* @param host const char *
@@ -73,7 +87,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
7387
* @param current_version const char *
7488
* @return t_httpUpdate_return
7589
*/
76-
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version) {
90+
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs) {
7791

7892
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
7993

@@ -85,6 +99,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
8599
http->addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize()));
86100
http->addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion());
87101

102+
if(spiffs) {
103+
http->addHeader("x-ESP8266-mode", "spiffs");
104+
} else {
105+
http->addHeader("x-ESP8266-mode", "sketch");
106+
}
107+
88108
if(current_version && current_version[0] != 0x00) {
89109
http->addHeader("x-ESP8266-version", current_version);
90110
}
@@ -119,9 +139,20 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
119139
switch(code) {
120140
case 200: ///< OK (Start Update)
121141
if(len > 0) {
122-
if(len > ESP.getFreeSketchSpace()) {
142+
bool startUpdate = false;
143+
if(spiffs) {
144+
size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start);
145+
if(len > (int) spiffsSize) {
146+
DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
147+
}
148+
} else {
149+
if(len > (int) ESP.getFreeSketchSpace()) {
150+
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
151+
}
152+
}
153+
154+
if(!startUpdate) {
123155
ret = HTTP_UPDATE_FAILED;
124-
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
125156
} else {
126157

127158
WiFiClient * tcp = http->getStreamPtr();
@@ -131,11 +162,25 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
131162

132163
delay(100);
133164

134-
if(runUpdate(*tcp, len, http->header("x-MD5"))) {
165+
int command;
166+
167+
if(spiffs) {
168+
command = U_SPIFFS;
169+
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate spiffs...\n");
170+
} else {
171+
command = U_FLASH;
172+
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate flash...\n");
173+
}
174+
175+
if(runUpdate(*tcp, len, http->header("x-MD5"), command)) {
135176
ret = HTTP_UPDATE_OK;
136177
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
137178
http->end();
138-
ESP.restart();
179+
180+
if(reboot) {
181+
ESP.restart();
182+
}
183+
139184
} else {
140185
ret = HTTP_UPDATE_FAILED;
141186
DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n");
@@ -171,9 +216,9 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
171216
* @param md5 String
172217
* @return true if Update ok
173218
*/
174-
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5) {
219+
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) {
175220

176-
if(!Update.begin(size)) {
221+
if(!Update.begin(size, command)) {
177222
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed!\n");
178223
return false;
179224
}

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class ESP8266HTTPUpdate {
5353
t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = "");
5454
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = "");
5555

56+
t_httpUpdate_return updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint);
57+
5658
protected:
57-
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version);
58-
bool runUpdate(Stream& in, uint32_t size, String md5);
59+
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false);
60+
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
5961
};
6062

6163
extern ESP8266HTTPUpdate ESPhttpUpdate;

0 commit comments

Comments
 (0)