1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

ESP8266httpUpdate: decouple HTTPS overloads

This commit is contained in:
Ivan Grokhotkov 2016-04-05 01:56:29 +03:00
parent cae4039225
commit c450023a32
2 changed files with 489 additions and 449 deletions

View File

@ -1,365 +1,383 @@
/** /**
* *
* @file ESP8266HTTPUpdate.cpp * @file ESP8266HTTPUpdate.cpp
* @date 21.06.2015 * @date 21.06.2015
* @author Markus Sattler * @author Markus Sattler
* *
* Copyright (c) 2015 Markus Sattler. All rights reserved. * Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the ESP8266 Http Updater. * This file is part of the ESP8266 Http Updater.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * 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, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*/ */
#include "ESP8266httpUpdate.h" #include "ESP8266httpUpdate.h"
#include <StreamString.h> #include <StreamString.h>
extern "C" uint32_t _SPIFFS_start; extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _SPIFFS_end; extern "C" uint32_t _SPIFFS_end;
ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) { ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
} }
ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) { ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) {
} }
/** t_httpUpdate_return ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
* const String& httpsFingerprint, bool reboot)
* @param url const char * {
* @param current_version const char * rebootOnUpdate(reboot);
* @param httpsFingerprint const char * return update(url, currentVersion, httpsFingerprint);
* @return t_httpUpdate_return }
*/
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * current_version, const char * httpsFingerprint, bool reboot) { t_httpUpdate_return ESP8266HTTPUpdate::update(const String& url, const String& currentVersion)
HTTPClient http; {
http.begin(url, httpsFingerprint); HTTPClient http;
return handleUpdate(&http, current_version, reboot, false); http.begin(url);
} return handleUpdate(http, currentVersion, false);
}
/**
* t_httpUpdate_return ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
* @param url const char * const String& httpsFingerprint)
* @param current_version const char * {
* @param httpsFingerprint const char * HTTPClient http;
* @return t_httpUpdate_return http.begin(url, httpsFingerprint);
*/ return handleUpdate(http, currentVersion, false);
t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint, bool reboot) { }
HTTPClient http;
http.begin(url, httpsFingerprint);
return handleUpdate(&http, current_version, reboot, true); t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint)
} {
HTTPClient http;
/** http.begin(url, httpsFingerprint);
* return handleUpdate(http, currentVersion, true);
* @param host const char * }
* @param port uint16_t
* @param url const char * t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion)
* @param current_version const char * {
* @param httpsFingerprint const char * HTTPClient http;
* @return http.begin(url);
*/ return handleUpdate(http, currentVersion, true);
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version, bool https, const char * httpsFingerprint, bool reboot) { }
HTTPClient http;
http.begin(host, port, url, https, httpsFingerprint); t_httpUpdate_return ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri, const String& currentVersion,
return handleUpdate(&http, current_version, reboot, false); bool https, const String& httpsFingerprint, bool reboot)
} {
rebootOnUpdate(reboot);
t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version, bool https, String httpsFingerprint, bool reboot) { if (httpsFingerprint.length() == 0) {
HTTPClient http; return update(host, port, uri, currentVersion);
http.begin(host, port, url, https, httpsFingerprint); }
return handleUpdate(&http, current_version.c_str(), reboot, false); else {
} return update(host, port, uri, currentVersion, httpsFingerprint);
}
/** }
* return error code as int
* @return int error code t_httpUpdate_return ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri,
*/ const String& currentVersion)
int ESP8266HTTPUpdate::getLastError(void){ {
return lastError; HTTPClient http;
} http.begin(host, port, uri);
return handleUpdate(http, currentVersion, false);
/** }
* return error code as String t_httpUpdate_return ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& url,
* @return String error const String& currentVersion, const String& httpsFingerprint)
*/ {
String ESP8266HTTPUpdate::getLastErrorString(void) { HTTPClient http;
http.begin(host, port, url, httpsFingerprint);
if(lastError == 0) { return handleUpdate(http, currentVersion, false);
return String(); // no error
} }
// error from Update class /**
if(lastError > 0) { * return error code as int
StreamString error; * @return int error code
Update.printError(error); */
error.trim(); // remove line ending int ESP8266HTTPUpdate::getLastError(void){
return "Update error: " + error; return _lastError;
} }
// error from http client /**
if(lastError > -100) { * return error code as String
return "HTTP error: " + HTTPClient::errorToString(lastError); * @return String error
} */
String ESP8266HTTPUpdate::getLastErrorString(void) {
switch(lastError) {
case HTTP_UE_TOO_LESS_SPACE: if(_lastError == 0) {
return String("To less space"); return String(); // no error
case HTTP_UE_SERVER_NOT_REPORT_SIZE: }
return String("Server not Report Size");
case HTTP_UE_SERVER_FILE_NOT_FOUND: // error from Update class
return String("File not Found (404)"); if(_lastError > 0) {
case HTTP_UE_SERVER_FORBIDDEN: StreamString error;
return String("Forbidden (403)"); Update.printError(error);
case HTTP_UE_SERVER_WRONG_HTTP_CODE: error.trim(); // remove line ending
return String("Wrong HTTP code"); return "Update error: " + error;
case HTTP_UE_SERVER_FAULTY_MD5: }
return String("Faulty MD5");
case HTTP_UE_BIN_VERIFY_HEADER_FAILED: // error from http client
return String("Verify bin header failed"); if(_lastError > -100) {
case HTTP_UE_BIN_FOR_WRONG_FLASH: return "HTTP error: " + HTTPClient::errorToString(_lastError);
return String("bin for wrong flash size"); }
}
switch(_lastError) {
return String(); case HTTP_UE_TOO_LESS_SPACE:
} return String("To less space");
case HTTP_UE_SERVER_NOT_REPORT_SIZE:
return String("Server not Report Size");
/** case HTTP_UE_SERVER_FILE_NOT_FOUND:
* return String("File not Found (404)");
* @param http HTTPClient * case HTTP_UE_SERVER_FORBIDDEN:
* @param current_version const char * return String("Forbidden (403)");
* @return t_httpUpdate_return case HTTP_UE_SERVER_WRONG_HTTP_CODE:
*/ return String("Wrong HTTP code");
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs) { case HTTP_UE_SERVER_FAULTY_MD5:
return String("Faulty MD5");
t_httpUpdate_return ret = HTTP_UPDATE_FAILED; case HTTP_UE_BIN_VERIFY_HEADER_FAILED:
return String("Verify bin header failed");
// use HTTP/1.0 for update since the update handler not support any transfer Encoding case HTTP_UE_BIN_FOR_WRONG_FLASH:
http->useHTTP10(true); return String("bin for wrong flash size");
http->setTimeout(8000); }
http->setUserAgent("ESP8266-http-Update");
http->addHeader("x-ESP8266-STA-MAC", WiFi.macAddress()); return String();
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()); *
* @param http HTTPClient *
if(spiffs) { * @param currentVersion const char *
http->addHeader("x-ESP8266-mode", "spiffs"); * @return t_httpUpdate_return
} else { */
http->addHeader("x-ESP8266-mode", "sketch"); t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs) {
}
t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
if(current_version && current_version[0] != 0x00) {
http->addHeader("x-ESP8266-version", current_version); // use HTTP/1.0 for update since the update handler not support any transfer Encoding
} http.useHTTP10(true);
http.setTimeout(8000);
const char * headerkeys[] = { "x-MD5" }; http.setUserAgent("ESP8266-http-Update");
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); http.addHeader("x-ESP8266-STA-MAC", WiFi.macAddress());
http.addHeader("x-ESP8266-AP-MAC", WiFi.softAPmacAddress());
// track these headers http.addHeader("x-ESP8266-free-space", String(ESP.getFreeSketchSpace()));
http->collectHeaders(headerkeys, headerkeyssize); 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());
int code = http->GET();
int len = http->getSize(); if(spiffs) {
http.addHeader("x-ESP8266-mode", "spiffs");
if(code <= 0) { } else {
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str()); http.addHeader("x-ESP8266-mode", "sketch");
lastError = code; }
http->end();
return HTTP_UPDATE_FAILED; if(currentVersion && currentVersion[0] != 0x00) {
} http.addHeader("x-ESP8266-version", currentVersion);
}
DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n"); const char * headerkeys[] = { "x-MD5" };
DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n"); size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len); // track these headers
http.collectHeaders(headerkeys, headerkeyssize);
if(http->hasHeader("x-MD5")) {
DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http->header("x-MD5").c_str());
} int code = http.GET();
int len = http.getSize();
DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace()); if(code <= 0) {
DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize()); DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http.errorToString(code).c_str());
_lastError = code;
if(current_version && current_version[0] != 0x00) { http.end();
DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version); return HTTP_UPDATE_FAILED;
} }
switch(code) {
case HTTP_CODE_OK: ///< OK (Start Update) DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n");
if(len > 0) { DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n");
bool startUpdate = true; DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
if(spiffs) { DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len);
size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start);
if(len > (int) spiffsSize) { if(http.hasHeader("x-MD5")) {
DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", spiffsSize, len); DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http.header("x-MD5").c_str());
startUpdate = false; }
}
} else { DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
if(len > (int) ESP.getFreeSketchSpace()) { DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace());
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize());
startUpdate = false;
} if(currentVersion && currentVersion[0] != 0x00) {
} DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", currentVersion);
}
if(!startUpdate) {
lastError = HTTP_UE_TOO_LESS_SPACE; switch(code) {
ret = HTTP_UPDATE_FAILED; case HTTP_CODE_OK: ///< OK (Start Update)
} else { if(len > 0) {
bool startUpdate = true;
WiFiClient * tcp = http->getStreamPtr(); if(spiffs) {
size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start);
WiFiUDP::stopAll(); if(len > (int) spiffsSize) {
WiFiClient::stopAllExcept(tcp); DEBUG_HTTP_UPDATE("[httpUpdate] spiffsSize to low (%d) needed: %d\n", spiffsSize, len);
startUpdate = false;
delay(100); }
} else {
int command; if(len > (int) ESP.getFreeSketchSpace()) {
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
if(spiffs) { startUpdate = false;
command = U_SPIFFS; }
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate spiffs...\n"); }
} else {
command = U_FLASH; if(!startUpdate) {
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate flash...\n"); _lastError = HTTP_UE_TOO_LESS_SPACE;
} ret = HTTP_UPDATE_FAILED;
} else {
if(!spiffs) {
uint8_t buf[4]; WiFiClient * tcp = http.getStreamPtr();
if(tcp->peekBytes(&buf[0], 4) != 4) {
DEBUG_HTTP_UPDATE("[httpUpdate] peekBytes magic header failed\n"); WiFiUDP::stopAll();
lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED; WiFiClient::stopAllExcept(tcp);
http->end();
return HTTP_UPDATE_FAILED; delay(100);
}
int command;
// check for valid first magic byte
if(buf[0] != 0xE9) { if(spiffs) {
DEBUG_HTTP_UPDATE("[httpUpdate] magic header not starts with 0xE9\n"); command = U_SPIFFS;
lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate spiffs...\n");
http->end(); } else {
return HTTP_UPDATE_FAILED; command = U_FLASH;
DEBUG_HTTP_UPDATE("[httpUpdate] runUpdate flash...\n");
} }
uint32_t bin_flash_size = ESP.magicFlashChipSize((buf[3] & 0xf0) >> 4); if(!spiffs) {
uint8_t buf[4];
// check if new bin fits to SPI flash if(tcp->peekBytes(&buf[0], 4) != 4) {
if(bin_flash_size > ESP.getFlashChipRealSize()) { DEBUG_HTTP_UPDATE("[httpUpdate] peekBytes magic header failed\n");
DEBUG_HTTP_UPDATE("[httpUpdate] magic header, new bin not fits SPI Flash\n"); _lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
lastError = HTTP_UE_BIN_FOR_WRONG_FLASH; http.end();
http->end(); return HTTP_UPDATE_FAILED;
return HTTP_UPDATE_FAILED; }
}
} // check for valid first magic byte
if(buf[0] != 0xE9) {
if(runUpdate(*tcp, len, http->header("x-MD5"), command)) { DEBUG_HTTP_UPDATE("[httpUpdate] magic header not starts with 0xE9\n");
ret = HTTP_UPDATE_OK; _lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n"); http.end();
http->end(); return HTTP_UPDATE_FAILED;
if(reboot) { }
ESP.restart();
} uint32_t bin_flash_size = ESP.magicFlashChipSize((buf[3] & 0xf0) >> 4);
} else { // check if new bin fits to SPI flash
ret = HTTP_UPDATE_FAILED; if(bin_flash_size > ESP.getFlashChipRealSize()) {
DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n"); DEBUG_HTTP_UPDATE("[httpUpdate] magic header, new bin not fits SPI Flash\n");
} _lastError = HTTP_UE_BIN_FOR_WRONG_FLASH;
} http.end();
} else { return HTTP_UPDATE_FAILED;
lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE; }
ret = HTTP_UPDATE_FAILED; }
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
} if(runUpdate(*tcp, len, http.header("x-MD5"), command)) {
break; ret = HTTP_UPDATE_OK;
case HTTP_CODE_NOT_MODIFIED: DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
///< Not Modified (No updates) http.end();
ret = HTTP_UPDATE_NO_UPDATES;
break; if(_rebootOnUpdate) {
case HTTP_CODE_NOT_FOUND: ESP.restart();
lastError = HTTP_UE_SERVER_FILE_NOT_FOUND; }
ret = HTTP_UPDATE_FAILED;
break; } else {
case HTTP_CODE_FORBIDDEN: ret = HTTP_UPDATE_FAILED;
lastError = HTTP_UE_SERVER_FORBIDDEN; DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n");
ret = HTTP_UPDATE_FAILED; }
break; }
default: } else {
lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE; _lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE;
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code); DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
//http->writeToStream(&Serial1); }
break; break;
} case HTTP_CODE_NOT_MODIFIED:
///< Not Modified (No updates)
http->end(); ret = HTTP_UPDATE_NO_UPDATES;
return ret; break;
} case HTTP_CODE_NOT_FOUND:
_lastError = HTTP_UE_SERVER_FILE_NOT_FOUND;
/** ret = HTTP_UPDATE_FAILED;
* write Update to flash break;
* @param in Stream& case HTTP_CODE_FORBIDDEN:
* @param size uint32_t _lastError = HTTP_UE_SERVER_FORBIDDEN;
* @param md5 String ret = HTTP_UPDATE_FAILED;
* @return true if Update ok break;
*/ default:
bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) { _lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE;
ret = HTTP_UPDATE_FAILED;
StreamString error; DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code);
//http.writeToStream(&Serial1);
if(!Update.begin(size, command)) { break;
lastError = Update.getError(); }
Update.printError(error);
error.trim(); // remove line ending http.end();
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str()); return ret;
return false; }
}
/**
if(md5.length()) { * write Update to flash
if(!Update.setMD5(md5.c_str())) { * @param in Stream&
lastError = HTTP_UE_SERVER_FAULTY_MD5; * @param size uint32_t
DEBUG_HTTP_UPDATE("[httpUpdate] Update.setMD5 failed! (%s)\n", md5.c_str()); * @param md5 String
return false; * @return true if Update ok
} */
} bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) {
if(Update.writeStream(in) != size) { StreamString error;
lastError = Update.getError();
Update.printError(error); if(!Update.begin(size, command)) {
error.trim(); // remove line ending _lastError = Update.getError();
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str()); Update.printError(error);
return false; error.trim(); // remove line ending
} DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str());
return false;
if(!Update.end()) { }
lastError = Update.getError();
Update.printError(error); if(md5.length()) {
error.trim(); // remove line ending if(!Update.setMD5(md5.c_str())) {
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str()); _lastError = HTTP_UE_SERVER_FAULTY_MD5;
return false; DEBUG_HTTP_UPDATE("[httpUpdate] Update.setMD5 failed! (%s)\n", md5.c_str());
} return false;
}
return true; }
}
if(Update.writeStream(in) != size) {
_lastError = Update.getError();
Update.printError(error);
ESP8266HTTPUpdate ESPhttpUpdate; error.trim(); // remove line ending
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str());
return false;
}
if(!Update.end()) {
_lastError = Update.getError();
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;

View File

@ -1,84 +1,106 @@
/** /**
* *
* @file ESP8266HTTPUpdate.h * @file ESP8266HTTPUpdate.h
* @date 21.06.2015 * @date 21.06.2015
* @author Markus Sattler * @author Markus Sattler
* *
* Copyright (c) 2015 Markus Sattler. All rights reserved. * Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the ESP8266 Http Updater. * This file is part of the ESP8266 Http Updater.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * 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, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*/ */
#ifndef ESP8266HTTPUPDATE_H_ #ifndef ESP8266HTTPUPDATE_H_
#define ESP8266HTTPUPDATE_H_ #define ESP8266HTTPUPDATE_H_
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#ifdef DEBUG_ESP_HTTP_UPDATE #ifdef DEBUG_ESP_HTTP_UPDATE
#ifdef DEBUG_ESP_PORT #ifdef DEBUG_ESP_PORT
#define DEBUG_HTTP_UPDATE(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ ) #define DEBUG_HTTP_UPDATE(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
#endif #endif
#endif #endif
#ifndef DEBUG_HTTP_UPDATE #ifndef DEBUG_HTTP_UPDATE
#define DEBUG_HTTP_UPDATE(...) #define DEBUG_HTTP_UPDATE(...)
#endif #endif
/// note we use HTTP client errors too so we start at 100 /// note we use HTTP client errors too so we start at 100
#define HTTP_UE_TOO_LESS_SPACE (-100) #define HTTP_UE_TOO_LESS_SPACE (-100)
#define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101) #define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101)
#define HTTP_UE_SERVER_FILE_NOT_FOUND (-102) #define HTTP_UE_SERVER_FILE_NOT_FOUND (-102)
#define HTTP_UE_SERVER_FORBIDDEN (-103) #define HTTP_UE_SERVER_FORBIDDEN (-103)
#define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104) #define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104)
#define HTTP_UE_SERVER_FAULTY_MD5 (-105) #define HTTP_UE_SERVER_FAULTY_MD5 (-105)
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106) #define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107) #define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
typedef enum { enum HTTPUpdateResult {
HTTP_UPDATE_FAILED, HTTP_UPDATE_FAILED,
HTTP_UPDATE_NO_UPDATES, HTTP_UPDATE_NO_UPDATES,
HTTP_UPDATE_OK HTTP_UPDATE_OK
} t_httpUpdate_return; };
class ESP8266HTTPUpdate { typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility
public:
ESP8266HTTPUpdate(void); class ESP8266HTTPUpdate {
~ESP8266HTTPUpdate(void); public:
ESP8266HTTPUpdate(void);
t_httpUpdate_return update(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = true); ~ESP8266HTTPUpdate(void);
t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = "", bool reboot = true);
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = "", bool reboot = true); void rebootOnUpdate(bool reboot) { _rebootOnUpdate = reboot; }
t_httpUpdate_return updateSpiffs(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = false); // This function is deprecated, use rebootOnUpdate and the next one instead
t_httpUpdate_return update(const String& url, const String& currentVersion,
int getLastError(void); const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
String getLastErrorString(void); t_httpUpdate_return update(const String& url, const String& currentVersion = "");
t_httpUpdate_return update(const String& url, const String& currentVersion,
protected: const String& httpsFingerprint);
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false);
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH); // This function is deprecated, use one of the overloads below along with rebootOnUpdate
t_httpUpdate_return update(const String& host, uint16_t port, const String& uri, const String& currentVersion,
int lastError; bool https, const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
};
t_httpUpdate_return update(const String& host, uint16_t port, const String& uri = "/",
extern ESP8266HTTPUpdate ESPhttpUpdate; const String& currentVersion = "");
t_httpUpdate_return update(const String& host, uint16_t port, const String& url,
#endif /* ESP8266HTTPUPDATE_H_ */ const String& currentVersion, const String& httpsFingerprint);
// This function is deprecated, use rebootOnUpdate and the next one instead
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion,
const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion = "");
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint);
int getLastError(void);
String getLastErrorString(void);
protected:
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
int _lastError;
bool _rebootOnUpdate = true;
};
extern ESP8266HTTPUpdate ESPhttpUpdate;
#endif /* ESP8266HTTPUPDATE_H_ */