Skip to content

Commit 9f0fdd5

Browse files
committed
update ESP8266HTTPUpdate to use httpClient class
add support for MD5 check
1 parent ce5720d commit 9f0fdd5

File tree

3 files changed

+110
-82
lines changed

3 files changed

+110
-82
lines changed

doc/ota_updates/ota_updates.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ function sendFile($path) {
340340
header('Content-Type: application/octet-stream', true);
341341
header('Content-Disposition: attachment; filename='.basename($path));
342342
header('Content-Length: '.filesize($path), true);
343+
header('x-MD5: '.md5_file($path), true);
343344
readfile($path);
344345
}
345346

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

Lines changed: 100 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -23,106 +23,95 @@
2323
*
2424
*/
2525

26+
2627
#include "ESP8266httpUpdate.h"
2728

28-
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
2929

30+
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
3031
}
3132

3233
ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) {
34+
}
3335

36+
/**
37+
*
38+
* @param url const char *
39+
* @param current_version const char *
40+
* @param httpsFingerprint const char *
41+
* @return t_httpUpdate_return
42+
*/
43+
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * current_version, const char * httpsFingerprint) {
44+
httpClient http;
45+
http.begin(url, httpsFingerprint);
46+
return handleUpdate(&http, current_version);
3447
}
3548

36-
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version) {
37-
WiFiClient tcp;
38-
DEBUG_HTTP_UPDATE("[httpUpdate] connected to %s:%u %s .... ", host, port, url);
49+
/**
50+
*
51+
* @param host const char *
52+
* @param port uint16_t
53+
* @param url const char *
54+
* @param current_version const char *
55+
* @param httpsFingerprint const char *
56+
* @return
57+
*/
58+
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version, bool https, const char * httpsFingerprint) {
59+
httpClient http;
60+
http.begin(host, port, url, https, httpsFingerprint);
61+
return handleUpdate(&http, current_version);
62+
}
3963

40-
if(!tcp.connect(host, port)) {
41-
DEBUG_HTTP_UPDATE("failed.\n");
42-
return HTTP_UPDATE_FAILED;
43-
}
44-
DEBUG_HTTP_UPDATE("ok.\n");
45-
return update(tcp, host, url, current_version);
64+
t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version, bool https, String httpsFingerprint) {
65+
httpClient http;
66+
http.begin(host, port, url, https, httpsFingerprint);
67+
return handleUpdate(&http, current_version.c_str());
4668
}
4769

48-
t_httpUpdate_return ESP8266HTTPUpdate::update(WiFiClient& tcp, const char* host, const char* url, const char * current_version) {
70+
/**
71+
*
72+
* @param http httpClient *
73+
* @param current_version const char *
74+
* @return t_httpUpdate_return
75+
*/
76+
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(httpClient * http, const char * current_version) {
77+
4978
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
50-
// set Timeout for readBytesUntil and readStringUntil
51-
tcp.setTimeout(2000);
52-
tcp.setNoDelay(true);
53-
54-
String req = "GET ";
55-
56-
req += url;
57-
req += " HTTP/1.1\r\n"
58-
"Host: ";
59-
req += host;
60-
req += "\r\n"
61-
"Connection: close\r\n"
62-
"User-Agent: ESP8266-http-Update\r\n"
63-
"x-ESP8266-STA-MAC: ";
64-
req += WiFi.macAddress();
65-
req += "\r\n"
66-
"x-ESP8266-AP-MAC: ";
67-
req += WiFi.softAPmacAddress();
68-
req += "\r\n"
69-
"x-ESP8266-free-space: ";
70-
req += ESP.getFreeSketchSpace();
71-
req += "\r\n"
72-
"x-ESP8266-sketch-size: ";
73-
req += ESP.getSketchSize();
74-
req += "\r\n"
75-
"x-ESP8266-chip-size: ";
76-
req += ESP.getFlashChipRealSize();
77-
req += "\r\n"
78-
"x-ESP8266-sdk-version: ";
79-
req += ESP.getSdkVersion();
80-
81-
if(current_version[0] != 0x00) {
82-
req += "\r\n"
83-
"x-ESP8266-version: ";
84-
req += current_version;
85-
}
8679

87-
req += "\r\n"
88-
"\r\n";
80+
http->setUserAgent("ESP8266-http-Update");
81+
http->addHeader("x-ESP8266-STA-MAC", WiFi.macAddress());
82+
http->addHeader("x-ESP8266-AP-MAC", WiFi.softAPmacAddress());
83+
http->addHeader("x-ESP8266-free-space", String(ESP.getFreeSketchSpace()));
84+
http->addHeader("x-ESP8266-sketch-size", String(ESP.getSketchSize()));
85+
http->addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize()));
86+
http->addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion());
8987

90-
tcp.write(req.c_str(), req.length());
88+
if(current_version && current_version[0] != 0x00) {
89+
http->addHeader("x-ESP8266-version", current_version);
90+
}
9191

92-
uint32_t code = 0;
93-
size_t len = 0;
92+
const char * headerkeys[] = { "x-MD5" };
93+
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
9494

95-
while(true) {
96-
String headerLine = tcp.readStringUntil('\n');
97-
headerLine.trim(); // remove \r
95+
// track these headers
96+
http->collectHeaders(headerkeys, headerkeyssize);
9897

99-
if(headerLine.length() > 0) {
100-
DEBUG_HTTP_UPDATE("[httpUpdate][Header] RX: %s\n", headerLine.c_str());
101-
if(headerLine.startsWith("HTTP/1.")) {
102-
// 9 = lenght of "HTTP/1.x "
103-
code = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
104-
} else if(headerLine.startsWith("Content-Length: ")) {
105-
// 16 = lenght of "Content-Length: "
106-
len = headerLine.substring(16).toInt();
107-
}
108-
} else {
109-
break;
110-
}
111-
}
98+
99+
int code = http->GET();
100+
int len = http->getSize();
112101

113102
DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n");
114103
DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n");
115104
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
116105
DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len);
117106

107+
if(http->hasHeader("x-MD5")) {
108+
DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http->header("x-MD5").c_str());
109+
}
110+
118111
DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
119112
DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace());
120113
DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize());
121114

122-
if(current_version[0] != 0x00) {
123-
DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version);
124-
}
125-
126115
switch(code) {
127116
case 200: ///< OK (Start Update)
128117
if(len > 0) {
@@ -131,15 +120,17 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(WiFiClient& tcp, const char* host,
131120
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
132121
} else {
133122

123+
WiFiClient * tcp = http->getStreamPtr();
124+
134125
WiFiUDP::stopAll();
135-
WiFiClient::stopAllExcept(&tcp);
126+
WiFiClient::stopAllExcept(tcp);
136127

137128
delay(100);
138129

139-
if(ESP.updateSketch(tcp, len, false, false)) {
130+
if(runUpdate(*tcp, len, http->header("x-MD5"))) {
140131
ret = HTTP_UPDATE_OK;
141132
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
142-
tcp.stop();
133+
http->end();
143134
ESP.restart();
144135
} else {
145136
ret = HTTP_UPDATE_FAILED;
@@ -148,7 +139,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(WiFiClient& tcp, const char* host,
148139
}
149140
} else {
150141
ret = HTTP_UPDATE_FAILED;
151-
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0?!\n");
142+
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or net set by Server?!\n");
152143
}
153144
break;
154145
case 304:
@@ -164,11 +155,42 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(WiFiClient& tcp, const char* host,
164155
break;
165156
}
166157

158+
http->end();
159+
167160
return ret;
168161
}
169162

170-
t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version) {
171-
return update(host.c_str(), port, url.c_str(), current_version.c_str());
163+
/**
164+
* write Update to flash
165+
* @param in Stream&
166+
* @param size uint32_t
167+
* @param md5 String
168+
* @return true if Update ok
169+
*/
170+
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5) {
171+
172+
if(!Update.begin(size)) {
173+
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed!\n");
174+
return false;
175+
}
176+
177+
if(md5.length()) {
178+
Update.setMD5(md5.c_str());
179+
}
180+
181+
if(Update.writeStream(in) != size) {
182+
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed!\n");
183+
return false;
184+
}
185+
186+
if(!Update.end()) {
187+
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed!\n");
188+
return false;
189+
}
190+
191+
return true;
172192
}
173193

194+
195+
174196
ESP8266HTTPUpdate ESPhttpUpdate;

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828

2929
#include <Arduino.h>
3030
#include <ESP8266WiFi.h>
31-
#include <WiFiUdp.h>
3231
#include <WiFiClient.h>
32+
#include <WiFiUdp.h>
33+
#include <ESP8266httpClient.h>
3334

3435
//#define DEBUG_HTTP_UPDATE(...) Serial1.printf( __VA_ARGS__ )
3536

@@ -48,9 +49,13 @@ class ESP8266HTTPUpdate {
4849
ESP8266HTTPUpdate(void);
4950
~ESP8266HTTPUpdate(void);
5051

51-
t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "");
52-
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "");
53-
t_httpUpdate_return update(WiFiClient& client, const char* host, const char* url = "/", const char * current_version = "");
52+
t_httpUpdate_return update(const char * url, const char * current_version = "", const char * httpsFingerprint = "");
53+
t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = "");
54+
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = "");
55+
56+
protected:
57+
t_httpUpdate_return handleUpdate(httpClient * http, const char * current_version);
58+
bool runUpdate(Stream& in, uint32_t size, String md5);
5459
};
5560

5661
extern ESP8266HTTPUpdate ESPhttpUpdate;

0 commit comments

Comments
 (0)