forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathESP8266httpUpdate.cpp
261 lines (216 loc) · 8.6 KB
/
ESP8266httpUpdate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
*
* @file ESP8266HTTPUpdate.cpp
* @date 21.06.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the ESP8266 Http Updater.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "ESP8266httpUpdate.h"
#include <StreamString.h>
extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _SPIFFS_end;
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
}
ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) {
}
/**
*
* @param url const char *
* @param current_version const char *
* @param httpsFingerprint const char *
* @return t_httpUpdate_return
*/
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * current_version, const char * httpsFingerprint) {
HTTPClient http;
http.begin(url, httpsFingerprint);
return handleUpdate(&http, current_version);
}
/**
*
* @param url const char *
* @param current_version const char *
* @param httpsFingerprint const char *
* @return t_httpUpdate_return
*/
t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint) {
HTTPClient http;
http.begin(url, httpsFingerprint);
return handleUpdate(&http, current_version, false, true);
}
/**
*
* @param host const char *
* @param port uint16_t
* @param url const char *
* @param current_version const char *
* @param httpsFingerprint const char *
* @return
*/
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version, bool https, const char * httpsFingerprint) {
HTTPClient http;
http.begin(host, port, url, https, httpsFingerprint);
return handleUpdate(&http, current_version);
}
t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version, bool https, String httpsFingerprint) {
HTTPClient http;
http.begin(host, port, url, https, httpsFingerprint);
return handleUpdate(&http, current_version.c_str());
}
/**
*
* @param http HTTPClient *
* @param current_version const char *
* @return t_httpUpdate_return
*/
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs) {
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
http->setUserAgent("ESP8266-http-Update");
http->addHeader("x-ESP8266-STA-MAC", WiFi.macAddress());
http->addHeader("x-ESP8266-AP-MAC", WiFi.softAPmacAddress());
http->addHeader("x-ESP8266-free-space", String(ESP.getFreeSketchSpace()));
http->addHeader("x-ESP8266-sketch-size", String(ESP.getSketchSize()));
http->addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize()));
http->addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion());
if(spiffs) {
http->addHeader("x-ESP8266-mode", "spiffs");
} else {
http->addHeader("x-ESP8266-mode", "sketch");
}
if(current_version && current_version[0] != 0x00) {
http->addHeader("x-ESP8266-version", current_version);
}
const char * headerkeys[] = { "x-MD5" };
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
// track these headers
http->collectHeaders(headerkeys, headerkeyssize);
int code = http->GET();
int len = http->getSize();
if(code <= 0) {
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str());
http->end();
return HTTP_UPDATE_FAILED;
}
DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n");
DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n");
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len);
if(http->hasHeader("x-MD5")) {
DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http->header("x-MD5").c_str());
}
DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace());
DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize());
if(current_version && current_version[0] != 0x00) {
DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version);
}
switch(code) {
case HTTP_CODE_OK: ///< OK (Start Update)
if(len > 0) {
bool startUpdate = true;
if(spiffs) {
size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start);
if(len > (int) spiffsSize) {
DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", spiffsSize, len);
startUpdate = false;
}
} else {
if(len > (int) ESP.getFreeSketchSpace()) {
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
startUpdate = false;
}
}
if(!startUpdate) {
ret = HTTP_UPDATE_FAILED;
} else {
WiFiClient * tcp = http->getStreamPtr();
WiFiUDP::stopAll();
WiFiClient::stopAllExcept(tcp);
delay(100);
int command;
if(spiffs) {
command = U_SPIFFS;
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate spiffs...\n");
} else {
command = U_FLASH;
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate flash...\n");
}
if(runUpdate(*tcp, len, http->header("x-MD5"), command)) {
ret = HTTP_UPDATE_OK;
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
http->end();
if(reboot) {
ESP.restart();
}
} else {
ret = HTTP_UPDATE_FAILED;
DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n");
}
}
} else {
ret = HTTP_UPDATE_FAILED;
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
}
break;
case HTTP_CODE_NOT_MODIFIED:
///< Not Modified (No updates)
ret = HTTP_UPDATE_NO_UPDATES;
break;
default:
ret = HTTP_UPDATE_FAILED;
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code);
//http->writeToStream(&Serial1);
break;
}
http->end();
return ret;
}
/**
* write Update to flash
* @param in Stream&
* @param size uint32_t
* @param md5 String
* @return true if Update ok
*/
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) {
StreamString error;
if(!Update.begin(size, command)) {
Update.printError(error);
error.trim(); // remove line ending
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str());
return false;
}
if(md5.length()) {
Update.setMD5(md5.c_str());
}
if(Update.writeStream(in) != size) {
Update.printError(error);
error.trim(); // remove line ending
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str());
return false;
}
if(!Update.end()) {
Update.printError(error);
error.trim(); // remove line ending
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str());
return false;
}
return true;
}
ESP8266HTTPUpdate ESPhttpUpdate;