23
23
*
24
24
*/
25
25
26
-
27
26
#include " ESP8266httpUpdate.h"
28
27
28
+ extern " C" uint32_t _SPIFFS_start;
29
+ extern " C" uint32_t _SPIFFS_end;
29
30
30
31
ESP8266HTTPUpdate::ESP8266HTTPUpdate (void ) {
31
32
}
@@ -46,6 +47,19 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * cur
46
47
return handleUpdate (&http, current_version);
47
48
}
48
49
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
+
49
63
/* *
50
64
*
51
65
* @param host const char *
@@ -73,7 +87,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
73
87
* @param current_version const char *
74
88
* @return t_httpUpdate_return
75
89
*/
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 ) {
77
91
78
92
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
79
93
@@ -85,6 +99,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
85
99
http->addHeader (" x-ESP8266-chip-size" , String (ESP.getFlashChipRealSize ()));
86
100
http->addHeader (" x-ESP8266-sdk-version" , ESP.getSdkVersion ());
87
101
102
+ if (spiffs) {
103
+ http->addHeader (" x-ESP8266-mode" , " spiffs" );
104
+ } else {
105
+ http->addHeader (" x-ESP8266-mode" , " sketch" );
106
+ }
107
+
88
108
if (current_version && current_version[0 ] != 0x00 ) {
89
109
http->addHeader (" x-ESP8266-version" , current_version);
90
110
}
@@ -119,9 +139,20 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
119
139
switch (code) {
120
140
case 200 : // /< OK (Start Update)
121
141
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) {
123
155
ret = HTTP_UPDATE_FAILED;
124
- DEBUG_HTTP_UPDATE (" [httpUpdate] FreeSketchSpace to low (%d) needed: %d\n " , ESP.getFreeSketchSpace (), len);
125
156
} else {
126
157
127
158
WiFiClient * tcp = http->getStreamPtr ();
@@ -131,11 +162,25 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
131
162
132
163
delay (100 );
133
164
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)) {
135
176
ret = HTTP_UPDATE_OK;
136
177
DEBUG_HTTP_UPDATE (" [httpUpdate] Update ok\n " );
137
178
http->end ();
138
- ESP.restart ();
179
+
180
+ if (reboot) {
181
+ ESP.restart ();
182
+ }
183
+
139
184
} else {
140
185
ret = HTTP_UPDATE_FAILED;
141
186
DEBUG_HTTP_UPDATE (" [httpUpdate] Update failed\n " );
@@ -171,9 +216,9 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
171
216
* @param md5 String
172
217
* @return true if Update ok
173
218
*/
174
- bool ESP8266HTTPUpdate::runUpdate (Stream& in, uint32_t size, String md5) {
219
+ bool ESP8266HTTPUpdate::runUpdate (Stream& in, uint32_t size, String md5, int command ) {
175
220
176
- if (!Update.begin (size)) {
221
+ if (!Update.begin (size, command )) {
177
222
DEBUG_HTTP_UPDATE (" [httpUpdate] Update.begin failed!\n " );
178
223
return false ;
179
224
}
0 commit comments