|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file ESP8266httpUpdate.cpp |
| 4 | + * @date 21.06.2015 |
| 5 | + * @author Markus Sattler |
| 6 | + * |
| 7 | + * Copyright (c) 2015 Markus Sattler. All rights reserved. |
| 8 | + * This file is part of the ESP8266 Http Updater. |
| 9 | + * |
| 10 | + * This library is free software; you can redistribute it and/or |
| 11 | + * modify it under the terms of the GNU Lesser General Public |
| 12 | + * License as published by the Free Software Foundation; either |
| 13 | + * version 2.1 of the License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This library is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + * Lesser General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Lesser General Public |
| 21 | + * License along with this library; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +#include "ESP8266httpUpdate.h" |
| 27 | + |
| 28 | +ESP8266httpUpdate::ESP8266httpUpdate(void) { |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +ESP8266httpUpdate::~ESP8266httpUpdate(void) { |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +t_httpUpdate_return ESP8266httpUpdate::update(const char *host, uint16_t port, const char * url, const char *current_version) { |
| 37 | + |
| 38 | + t_httpUpdate_return ret = HTTP_UPDATE_FAILD; |
| 39 | + WiFiClient tcp; |
| 40 | + DEBUG_HTTP_UPDATE("[httpUpdate] connected to %s:%u %s .... ", host, port, url); |
| 41 | + |
| 42 | + if(!tcp.connect(host, port)) { |
| 43 | + DEBUG_HTTP_UPDATE("faild.\n"); |
| 44 | + return ret; |
| 45 | + } |
| 46 | + DEBUG_HTTP_UPDATE("ok.\n"); |
| 47 | + |
| 48 | + // set Timeout for readBytesUntil and readStringUntil |
| 49 | + tcp.setTimeout(2000); |
| 50 | + tcp.setNoDelay(true); |
| 51 | + |
| 52 | + String req = "GET "; |
| 53 | + |
| 54 | + req += url; |
| 55 | + req += " HTTP/1.1\r\n" |
| 56 | + "Host: "; |
| 57 | + req += host; |
| 58 | + req += "\r\n" |
| 59 | + "Connection: close\r\n" |
| 60 | + "User-Agent: ESP8266-http-Update\r\n" |
| 61 | + "x-ESP8266-STA-MAC: "; |
| 62 | + req += WiFi.macAddress(); |
| 63 | + req += "\r\n" |
| 64 | + "x-ESP8266-AP-MAC: "; |
| 65 | + req += WiFi.softAPmacAddress(); |
| 66 | + req += "\r\n" |
| 67 | + "x-ESP8266-free-space: "; |
| 68 | + req += ESP.getFreeSketchSpace(); |
| 69 | + req += "\r\n" |
| 70 | + "x-ESP8266-sketch-size: "; |
| 71 | + req += ESP.getSketchSize(); |
| 72 | + req += "\r\n" |
| 73 | + "x-ESP8266-chip-size: "; |
| 74 | + req += ESP.getFlashChipRealSize(); |
| 75 | + |
| 76 | + if(current_version[0] != 0x00) { |
| 77 | + req += "\r\n" |
| 78 | + "x-ESP8266-version: "; |
| 79 | + req += current_version; |
| 80 | + } |
| 81 | + |
| 82 | + req += "\r\n" |
| 83 | + "\r\n"; |
| 84 | + |
| 85 | + tcp.write(req.c_str(), req.length()); |
| 86 | + |
| 87 | + uint32_t code = 0; |
| 88 | + size_t len = 0; |
| 89 | + |
| 90 | + while(true) { |
| 91 | + String headerLine = tcp.readStringUntil('\n'); |
| 92 | + headerLine.trim(); // remove \r |
| 93 | + |
| 94 | + if(headerLine.length() > 0) { |
| 95 | + DEBUG_HTTP_UPDATE("[httpUpdate][Header] RX: %s\n", headerLine.c_str()); |
| 96 | + if(headerLine.startsWith("HTTP/1.")) { |
| 97 | + // 9 = lenght of "HTTP/1.x " |
| 98 | + code = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt(); |
| 99 | + } else if(headerLine.startsWith("Content-Length: ")) { |
| 100 | + // 16 = lenght of "Content-Length: " |
| 101 | + len = headerLine.substring(16).toInt(); |
| 102 | + } |
| 103 | + } else { |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n"); |
| 109 | + DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n"); |
| 110 | + DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code); |
| 111 | + DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len); |
| 112 | + |
| 113 | + DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n"); |
| 114 | + DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace()); |
| 115 | + DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize()); |
| 116 | + |
| 117 | + if(current_version[0] != 0x00) { |
| 118 | + DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version); |
| 119 | + } |
| 120 | + |
| 121 | + switch(code) { |
| 122 | + case 200: ///< OK (Start Update) |
| 123 | + if(len > 0) { |
| 124 | + if(len > ESP.getFreeSketchSpace()) { |
| 125 | + ret = HTTP_UPDATE_FAILD; |
| 126 | + DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); |
| 127 | + } else { |
| 128 | + if(ESP.updateSketch(tcp, len)) { |
| 129 | + // may never reached! |
| 130 | + ret = HTTP_UPDATE_OK; |
| 131 | + DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n"); |
| 132 | + } else { |
| 133 | + ret = HTTP_UPDATE_FAILD; |
| 134 | + DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n"); |
| 135 | + } |
| 136 | + } |
| 137 | + } else { |
| 138 | + ret = HTTP_UPDATE_FAILD; |
| 139 | + DEBUG_HTTP_UPDATE("[httpUpdate]Content-Length is 0?!\n"); |
| 140 | + } |
| 141 | + break; |
| 142 | + case 304: |
| 143 | + ///< Not Modified (No updates) |
| 144 | + ret = HTTP_UPDATE_NO_UPDATES; |
| 145 | + break; |
| 146 | + case 403: |
| 147 | + ///< Forbidden |
| 148 | + // todo handle login |
| 149 | + default: |
| 150 | + ret = HTTP_UPDATE_FAILD; |
| 151 | + DEBUG_HTTP_UPDATE("[httpUpdate] Code is (%d)\n", code); |
| 152 | + break; |
| 153 | + } |
| 154 | + |
| 155 | + return ret; |
| 156 | +} |
| 157 | + |
| 158 | +t_httpUpdate_return ESP8266httpUpdate::update(String host, uint16_t port, String url, String current_version) { |
| 159 | + return update(host.c_str(), port, url.c_str(), current_version.c_str()); |
| 160 | +} |
| 161 | + |
| 162 | +ESP8266httpUpdate ESPhttpUpdate; |
0 commit comments