2323 *
2424 */
2525
26-
2726#include " ESP8266httpUpdate.h"
27+ #include < StreamString.h>
2828
29+ extern " C" uint32_t _SPIFFS_start;
30+ extern " C" uint32_t _SPIFFS_end;
2931
3032ESP8266HTTPUpdate::ESP8266HTTPUpdate (void ) {
3133}
@@ -46,6 +48,19 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * cur
4648 return handleUpdate (&http, current_version);
4749}
4850
51+ /* *
52+ *
53+ * @param url const char *
54+ * @param current_version const char *
55+ * @param httpsFingerprint const char *
56+ * @return t_httpUpdate_return
57+ */
58+ t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint) {
59+ HTTPClient http;
60+ http.begin (url, httpsFingerprint);
61+ return handleUpdate (&http, current_version, false , true );
62+ }
63+
4964/* *
5065 *
5166 * @param host const char *
@@ -73,7 +88,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
7388 * @param current_version const char *
7489 * @return t_httpUpdate_return
7590 */
76- t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version) {
91+ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs ) {
7792
7893 t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
7994
@@ -85,6 +100,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
85100 http->addHeader (" x-ESP8266-chip-size" , String (ESP.getFlashChipRealSize ()));
86101 http->addHeader (" x-ESP8266-sdk-version" , ESP.getSdkVersion ());
87102
103+ if (spiffs) {
104+ http->addHeader (" x-ESP8266-mode" , " spiffs" );
105+ } else {
106+ http->addHeader (" x-ESP8266-mode" , " sketch" );
107+ }
108+
88109 if (current_version && current_version[0 ] != 0x00 ) {
89110 http->addHeader (" x-ESP8266-version" , current_version);
90111 }
@@ -99,6 +120,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
99120 int code = http->GET ();
100121 int len = http->getSize ();
101122
123+ if (code <= 0 ) {
124+ DEBUG_HTTP_UPDATE (" [httpUpdate] HTTP error: %s\n " , http->errorToString (code).c_str ());
125+ http->end ();
126+ return HTTP_UPDATE_FAILED;
127+ }
128+
102129 DEBUG_HTTP_UPDATE (" [httpUpdate] Header read fin.\n " );
103130 DEBUG_HTTP_UPDATE (" [httpUpdate] Server header:\n " );
104131 DEBUG_HTTP_UPDATE (" [httpUpdate] - code: %d\n " , code);
@@ -117,11 +144,24 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
117144 }
118145
119146 switch (code) {
120- case 200 : // /< OK (Start Update)
147+ case HTTP_CODE_OK : // /< OK (Start Update)
121148 if (len > 0 ) {
122- if (len > ESP.getFreeSketchSpace ()) {
149+ bool startUpdate = true ;
150+ if (spiffs) {
151+ size_t spiffsSize = ((size_t ) &_SPIFFS_end - (size_t ) &_SPIFFS_start);
152+ if (len > (int ) spiffsSize) {
153+ DEBUG_HTTP_UPDATE (" [httpUpdate] spiffsSize to low (%d) needed: %d\n " , spiffsSize, len);
154+ startUpdate = false ;
155+ }
156+ } else {
157+ if (len > (int ) ESP.getFreeSketchSpace ()) {
158+ DEBUG_HTTP_UPDATE (" [httpUpdate] FreeSketchSpace to low (%d) needed: %d\n " , ESP.getFreeSketchSpace (), len);
159+ startUpdate = false ;
160+ }
161+ }
162+
163+ if (!startUpdate) {
123164 ret = HTTP_UPDATE_FAILED;
124- DEBUG_HTTP_UPDATE (" [httpUpdate] FreeSketchSpace to low (%d) needed: %d\n " , ESP.getFreeSketchSpace (), len);
125165 } else {
126166
127167 WiFiClient * tcp = http->getStreamPtr ();
@@ -131,11 +171,25 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
131171
132172 delay (100 );
133173
134- if (runUpdate (*tcp, len, http->header (" x-MD5" ))) {
174+ int command;
175+
176+ if (spiffs) {
177+ command = U_SPIFFS;
178+ DEBUG_HTTP_UPDATE (" [httpUpdate] runUpdate spiffs...\n " );
179+ } else {
180+ command = U_FLASH;
181+ DEBUG_HTTP_UPDATE (" [httpUpdate] runUpdate flash...\n " );
182+ }
183+
184+ if (runUpdate (*tcp, len, http->header (" x-MD5" ), command)) {
135185 ret = HTTP_UPDATE_OK;
136186 DEBUG_HTTP_UPDATE (" [httpUpdate] Update ok\n " );
137187 http->end ();
138- ESP.restart ();
188+
189+ if (reboot) {
190+ ESP.restart ();
191+ }
192+
139193 } else {
140194 ret = HTTP_UPDATE_FAILED;
141195 DEBUG_HTTP_UPDATE (" [httpUpdate] Update failed\n " );
@@ -146,19 +200,18 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
146200 DEBUG_HTTP_UPDATE (" [httpUpdate] Content-Length is 0 or not set by Server?!\n " );
147201 }
148202 break ;
149- case 304 :
203+ case HTTP_CODE_NOT_MODIFIED :
150204 // /< Not Modified (No updates)
151205 ret = HTTP_UPDATE_NO_UPDATES;
152206 break ;
153- case 403 :
154- // /< Forbidden
155- // todo handle login
156207 default :
157208 ret = HTTP_UPDATE_FAILED;
158- DEBUG_HTTP_UPDATE (" [httpUpdate] Code is (%d)\n " , code);
209+ DEBUG_HTTP_UPDATE (" [httpUpdate] HTTP Code is (%d)\n " , code);
210+ // http->writeToStream(&Serial1);
159211 break ;
160212 }
161213
214+
162215 http->end ();
163216
164217 return ret;
@@ -171,10 +224,14 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
171224 * @param md5 String
172225 * @return true if Update ok
173226 */
174- bool ESP8266HTTPUpdate::runUpdate (Stream& in, uint32_t size, String md5) {
227+ bool ESP8266HTTPUpdate::runUpdate (Stream& in, uint32_t size, String md5, int command) {
228+
229+ StreamString error;
175230
176- if (!Update.begin (size)) {
177- DEBUG_HTTP_UPDATE (" [httpUpdate] Update.begin failed!\n " );
231+ if (!Update.begin (size, command)) {
232+ Update.printError (error);
233+ error.trim (); // remove line ending
234+ DEBUG_HTTP_UPDATE (" [httpUpdate] Update.begin failed! (%s)\n " , error.c_str ());
178235 return false ;
179236 }
180237
@@ -183,12 +240,16 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5) {
183240 }
184241
185242 if (Update.writeStream (in) != size) {
186- DEBUG_HTTP_UPDATE (" [httpUpdate] Update.writeStream failed!\n " );
243+ Update.printError (error);
244+ error.trim (); // remove line ending
245+ DEBUG_HTTP_UPDATE (" [httpUpdate] Update.writeStream failed! (%s)\n " , error.c_str ());
187246 return false ;
188247 }
189248
190249 if (!Update.end ()) {
191- DEBUG_HTTP_UPDATE (" [httpUpdate] Update.end failed!\n " );
250+ Update.printError (error);
251+ error.trim (); // remove line ending
252+ DEBUG_HTTP_UPDATE (" [httpUpdate] Update.end failed! (%s)\n " , error.c_str ());
192253 return false ;
193254 }
194255
0 commit comments