Skip to content

Commit 1c57514

Browse files
committed
Merge pull request #1235 from Links2004/httpUpdate
HTTPupdate add flash size check + better error output
2 parents e75c3d8 + f57ab60 commit 1c57514

File tree

9 files changed

+118
-12
lines changed

9 files changed

+118
-12
lines changed

cores/esp8266/Esp.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ uint32_t EspClass::getFlashChipSizeByChipId(void) {
298298
}
299299
}
300300

301+
/**
302+
* check the Flash settings from IDE against the Real flash size
303+
* @param needsEquals (return only true it equals)
304+
* @return ok or not
305+
*/
306+
bool EspClass::checkFlashConfig(bool needsEquals) {
307+
if(needsEquals) {
308+
if(getFlashChipRealSize() == getFlashChipSize()) {
309+
return true;
310+
}
311+
} else {
312+
if(getFlashChipRealSize() >= getFlashChipSize()) {
313+
return true;
314+
}
315+
}
316+
return false;
317+
}
318+
301319
String EspClass::getResetInfo(void) {
302320
if(resetInfo.reason != 0) {
303321
char buff[200];

cores/esp8266/Esp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class EspClass {
117117
FlashMode_t getFlashChipMode();
118118
uint32_t getFlashChipSizeByChipId();
119119

120+
bool checkFlashConfig(bool needsEquals = false);
121+
120122
bool flashEraseSector(uint32_t sector);
121123
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
122124
bool flashRead(uint32_t offset, uint32_t *data, size_t size);

cores/esp8266/Updater.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ bool UpdaterClass::begin(size_t size, int command) {
5757
return false;
5858
}
5959

60+
if(ESP.checkFlashConfig(false)) {
61+
_error = UPDATE_ERROR_FLASH_CONFIG;
62+
#ifdef DEBUG_UPDATER
63+
printError(DEBUG_UPDATER);
64+
#endif
65+
return false;
66+
}
67+
6068
_reset();
6169
_error = 0;
6270

@@ -278,6 +286,8 @@ void UpdaterClass::printError(Stream &out){
278286
out.println("Stream Read Timeout");
279287
} else if(_error == UPDATE_ERROR_MD5){
280288
out.println("MD5 Check Failed");
289+
} else if(_error == UPDATE_ERROR_FLASH_CONFIG){
290+
out.printf("Flash config wrong real: %d IDE: %d\n", ESP.getFlashChipRealSize(), ESP.getFlashChipSize());
281291
} else {
282292
out.println("UNKNOWN");
283293
}

cores/esp8266/Updater.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
#include "flash_utils.h"
66
#include "MD5Builder.h"
77

8-
#define UPDATE_ERROR_OK 0
9-
#define UPDATE_ERROR_WRITE 1
10-
#define UPDATE_ERROR_ERASE 2
11-
#define UPDATE_ERROR_SPACE 3
12-
#define UPDATE_ERROR_SIZE 4
13-
#define UPDATE_ERROR_STREAM 5
14-
#define UPDATE_ERROR_MD5 6
8+
#define UPDATE_ERROR_OK (0)
9+
#define UPDATE_ERROR_WRITE (1)
10+
#define UPDATE_ERROR_ERASE (2)
11+
#define UPDATE_ERROR_SPACE (3)
12+
#define UPDATE_ERROR_SIZE (4)
13+
#define UPDATE_ERROR_STREAM (5)
14+
#define UPDATE_ERROR_MD5 (6)
15+
#define UPDATE_ERROR_FLASH_CONFIG (7)
16+
1517

1618
#define U_FLASH 0
1719
#define U_SPIFFS 100

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class HTTPClient {
153153
int writeToStream(Stream * stream);
154154
String getString(void);
155155

156-
String errorToString(int error);
156+
static String errorToString(int error);
157157

158158
protected:
159159

libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void loop() {
4545

4646
switch(ret) {
4747
case HTTP_UPDATE_FAILED:
48-
USE_SERIAL.println("HTTP_UPDATE_FAILD");
48+
USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
4949
break;
5050

5151
case HTTP_UPDATE_NO_UPDATES:

libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void loop() {
4848

4949
switch(ret) {
5050
case HTTP_UPDATE_FAILED:
51-
USE_SERIAL.println("HTTP_UPDATE_FAILD");
51+
USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
5252
break;
5353

5454
case HTTP_UPDATE_NO_UPDATES:

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,54 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
8282
return handleUpdate(&http, current_version.c_str(), reboot, false);
8383
}
8484

85+
/**
86+
* return error code as int
87+
* @return int error code
88+
*/
89+
int ESP8266HTTPUpdate::getLastError(void){
90+
return lastError;
91+
}
92+
93+
/**
94+
* return error code as String
95+
* @return String error
96+
*/
97+
String ESP8266HTTPUpdate::getLastErrorString(void) {
98+
99+
if(lastError == 0) {
100+
return String(); // no error
101+
}
102+
103+
// error from Update class
104+
if(lastError > 0) {
105+
StreamString error;
106+
Update.printError(error);
107+
error.trim(); // remove line ending
108+
return "Update error: " + error;
109+
}
110+
111+
// error from http client
112+
if(lastError > -100) {
113+
return "HTTP error: " + HTTPClient::errorToString(lastError);
114+
}
115+
116+
switch(lastError) {
117+
case HTTP_UE_TOO_LESS_SPACE:
118+
return String("To less space");
119+
case HTTP_UE_SERVER_NOT_REPORT_SIZE:
120+
return String("Server not Report Size");
121+
case HTTP_UE_SERVER_FILE_NOT_FOUND:
122+
return String("File not Found (404)");
123+
case HTTP_UE_SERVER_FORBIDDEN:
124+
return String("Forbidden (403)");
125+
case HTTP_UE_SERVER_WRONG_HTTP_CODE:
126+
return String("Wrong HTTP code");
127+
}
128+
129+
return String();
130+
}
131+
132+
85133
/**
86134
*
87135
* @param http HTTPClient *
@@ -122,10 +170,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
122170

123171
if(code <= 0) {
124172
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str());
173+
lastError = code;
125174
http->end();
126175
return HTTP_UPDATE_FAILED;
127176
}
128177

178+
129179
DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n");
130180
DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n");
131181
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
@@ -161,6 +211,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
161211
}
162212

163213
if(!startUpdate) {
214+
lastError = HTTP_UE_TOO_LESS_SPACE;
164215
ret = HTTP_UPDATE_FAILED;
165216
} else {
166217

@@ -196,6 +247,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
196247
}
197248
}
198249
} else {
250+
lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE;
199251
ret = HTTP_UPDATE_FAILED;
200252
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
201253
}
@@ -204,16 +256,23 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
204256
///< Not Modified (No updates)
205257
ret = HTTP_UPDATE_NO_UPDATES;
206258
break;
259+
case HTTP_CODE_NOT_FOUND:
260+
lastError = HTTP_UE_SERVER_FILE_NOT_FOUND;
261+
ret = HTTP_UPDATE_FAILED;
262+
break;
263+
case HTTP_CODE_FORBIDDEN:
264+
lastError = HTTP_UE_SERVER_FORBIDDEN;
265+
ret = HTTP_UPDATE_FAILED;
266+
break;
207267
default:
268+
lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE;
208269
ret = HTTP_UPDATE_FAILED;
209270
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code);
210271
//http->writeToStream(&Serial1);
211272
break;
212273
}
213274

214-
215275
http->end();
216-
217276
return ret;
218277
}
219278

@@ -229,6 +288,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
229288
StreamString error;
230289

231290
if(!Update.begin(size, command)) {
291+
lastError = Update.getError();
232292
Update.printError(error);
233293
error.trim(); // remove line ending
234294
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str());
@@ -240,13 +300,15 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
240300
}
241301

242302
if(Update.writeStream(in) != size) {
303+
lastError = Update.getError();
243304
Update.printError(error);
244305
error.trim(); // remove line ending
245306
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str());
246307
return false;
247308
}
248309

249310
if(!Update.end()) {
311+
lastError = Update.getError();
250312
Update.printError(error);
251313
error.trim(); // remove line ending
252314
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str());

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
#define DEBUG_HTTP_UPDATE(...)
3939
#endif
4040

41+
/// note we use HTTP client errors too so we start at 100
42+
#define HTTP_UE_TOO_LESS_SPACE (-100)
43+
#define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101)
44+
#define HTTP_UE_SERVER_FILE_NOT_FOUND (-102)
45+
#define HTTP_UE_SERVER_FORBIDDEN (-103)
46+
#define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104)
47+
4148
typedef enum {
4249
HTTP_UPDATE_FAILED,
4350
HTTP_UPDATE_NO_UPDATES,
@@ -55,9 +62,14 @@ class ESP8266HTTPUpdate {
5562

5663
t_httpUpdate_return updateSpiffs(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = false);
5764

65+
int getLastError(void);
66+
String getLastErrorString(void);
67+
5868
protected:
5969
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false);
6070
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
71+
72+
int lastError;
6173
};
6274

6375
extern ESP8266HTTPUpdate ESPhttpUpdate;

0 commit comments

Comments
 (0)