23
23
*
24
24
*/
25
25
26
-
27
26
#include " ESP8266httpUpdate.h"
27
+ #include < StreamString.h>
28
28
29
+ extern " C" uint32_t _SPIFFS_start;
30
+ extern " C" uint32_t _SPIFFS_end;
29
31
30
32
ESP8266HTTPUpdate::ESP8266HTTPUpdate (void ) {
31
33
}
@@ -46,6 +48,19 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * cur
46
48
return handleUpdate (&http, current_version);
47
49
}
48
50
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
+
49
64
/* *
50
65
*
51
66
* @param host const char *
@@ -73,7 +88,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
73
88
* @param current_version const char *
74
89
* @return t_httpUpdate_return
75
90
*/
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 ) {
77
92
78
93
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
79
94
@@ -85,6 +100,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
85
100
http->addHeader (" x-ESP8266-chip-size" , String (ESP.getFlashChipRealSize ()));
86
101
http->addHeader (" x-ESP8266-sdk-version" , ESP.getSdkVersion ());
87
102
103
+ if (spiffs) {
104
+ http->addHeader (" x-ESP8266-mode" , " spiffs" );
105
+ } else {
106
+ http->addHeader (" x-ESP8266-mode" , " sketch" );
107
+ }
108
+
88
109
if (current_version && current_version[0 ] != 0x00 ) {
89
110
http->addHeader (" x-ESP8266-version" , current_version);
90
111
}
@@ -99,6 +120,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
99
120
int code = http->GET ();
100
121
int len = http->getSize ();
101
122
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
+
102
129
DEBUG_HTTP_UPDATE (" [httpUpdate] Header read fin.\n " );
103
130
DEBUG_HTTP_UPDATE (" [httpUpdate] Server header:\n " );
104
131
DEBUG_HTTP_UPDATE (" [httpUpdate] - code: %d\n " , code);
@@ -117,11 +144,24 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
117
144
}
118
145
119
146
switch (code) {
120
- case 200 : // /< OK (Start Update)
147
+ case HTTP_CODE_OK : // /< OK (Start Update)
121
148
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) {
123
164
ret = HTTP_UPDATE_FAILED;
124
- DEBUG_HTTP_UPDATE (" [httpUpdate] FreeSketchSpace to low (%d) needed: %d\n " , ESP.getFreeSketchSpace (), len);
125
165
} else {
126
166
127
167
WiFiClient * tcp = http->getStreamPtr ();
@@ -131,11 +171,25 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
131
171
132
172
delay (100 );
133
173
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)) {
135
185
ret = HTTP_UPDATE_OK;
136
186
DEBUG_HTTP_UPDATE (" [httpUpdate] Update ok\n " );
137
187
http->end ();
138
- ESP.restart ();
188
+
189
+ if (reboot ) {
190
+ ESP.restart ();
191
+ }
192
+
139
193
} else {
140
194
ret = HTTP_UPDATE_FAILED;
141
195
DEBUG_HTTP_UPDATE (" [httpUpdate] Update failed\n " );
@@ -146,19 +200,18 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
146
200
DEBUG_HTTP_UPDATE (" [httpUpdate] Content-Length is 0 or not set by Server?!\n " );
147
201
}
148
202
break ;
149
- case 304 :
203
+ case HTTP_CODE_NOT_MODIFIED :
150
204
// /< Not Modified (No updates)
151
205
ret = HTTP_UPDATE_NO_UPDATES;
152
206
break ;
153
- case 403 :
154
- // /< Forbidden
155
- // todo handle login
156
207
default :
157
208
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);
159
211
break ;
160
212
}
161
213
214
+
162
215
http->end ();
163
216
164
217
return ret;
@@ -171,10 +224,14 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
171
224
* @param md5 String
172
225
* @return true if Update ok
173
226
*/
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;
175
230
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 ());
178
235
return false ;
179
236
}
180
237
@@ -183,12 +240,16 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5) {
183
240
}
184
241
185
242
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 ());
187
246
return false ;
188
247
}
189
248
190
249
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 ());
192
253
return false ;
193
254
}
194
255
0 commit comments