ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
upload-updatefw.cpp
Go to the documentation of this file.
1 /*
2  upload-updatefw.cpp - ESP3D http handle
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../../include/esp3d_config.h"
21 #if defined (HTTP_FEATURE) && defined(WEB_UPDATE_FEATURE)
22 #include "../http_server.h"
23 #if defined (ARDUINO_ARCH_ESP32)
24 #include <WebServer.h>
25 #define UPDATE_SIZE
26 #include <Update.h>
27 #endif //ARDUINO_ARCH_ESP32
28 #if defined (ARDUINO_ARCH_ESP8266)
29 #include <ESP8266WebServer.h>
30 #define UPDATE_SIZE ESP_FileSystem::max_update_size()
31 #endif //ARDUINO_ARCH_ESP8266
32 #include "../../filesystem/esp_filesystem.h"
33 #include "../../authentication/authentication_service.h"
34 #include "../../../core/esp3doutput.h"
35 //File upload for Web update
36 void HTTP_Server::WebUpdateUpload ()
37 {
38  static size_t last_upload_update;
39  static uint32_t downloadsize = 0;
41  //only admin can update FW
43  _upload_status = UPLOAD_STATUS_FAILED;
44  pushError(ESP_ERROR_AUTHENTICATION, "Upload rejected", 401);
45  output.printERROR("Update rejected!",401);
46  } else {
47  //get current file ID
48  HTTPUpload& upload = _webserver->upload();
49  if ((_upload_status != UPLOAD_STATUS_FAILED) || (upload.status == UPLOAD_FILE_START)) {
50  //Upload start
51  if(upload.status == UPLOAD_FILE_START) {
52  output.printMSG("Update Firmware");
53  _upload_status= UPLOAD_STATUS_ONGOING;
54  String sizeargname = upload.filename + "S";
55  if (_webserver->hasArg (sizeargname.c_str()) ) {
56  downloadsize = _webserver->arg (sizeargname).toInt();
57  } else {
58  downloadsize = 0;
59  }
60  if (downloadsize > ESP_FileSystem::max_update_size()) {
61  _upload_status=UPLOAD_STATUS_FAILED;
62  output.printERROR("Update rejected!",500);
63  pushError(ESP_ERROR_NOT_ENOUGH_SPACE, "Upload rejected");
64  }
65  last_upload_update = 0;
66  if (_upload_status != UPLOAD_STATUS_FAILED) {
67  if(!Update.begin(UPDATE_SIZE)) { //start with unknown = max available size
68  _upload_status=UPLOAD_STATUS_FAILED;
69  output.printERROR("Update rejected!",500);
70  pushError(ESP_ERROR_NOT_ENOUGH_SPACE, "Upload rejected");
71  } else {
72  output.printMSG("Update 0%");
73  }
74  }
75  //Upload write
76  } else if(upload.status == UPLOAD_FILE_WRITE) {
77  //check if no error
78  if (_upload_status == UPLOAD_STATUS_ONGOING) {
79  //we do not know the total file size yet but we know the available space so let's use it
80  if (downloadsize != 0) {
81  if ( ((100 * upload.totalSize) / downloadsize) !=last_upload_update) {
82  if ( downloadsize > 0) {
83  last_upload_update = (100 * upload.totalSize) / downloadsize;
84  } else {
85  last_upload_update = upload.totalSize;
86  }
87  String s = "Update ";
88  s+= String(last_upload_update);
89  s+="%";
90  output.printMSG(s.c_str());
91  }
92  }
93  if(Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
94  _upload_status=UPLOAD_STATUS_FAILED;
95  output.printERROR("Update write failed!",500);
96  pushError(ESP_ERROR_FILE_WRITE, "File write failed");
97  }
98  }
99  //Upload end
100 
101  } else if(upload.status == UPLOAD_FILE_END) {
102  if ((downloadsize!=0) && (downloadsize < upload.totalSize)) {
103  _upload_status=UPLOAD_STATUS_FAILED;
104  output.printERROR("Update write failed!",500);
105  pushError(ESP_ERROR_FILE_WRITE, "File write failed");
106  }
107  if (_upload_status == UPLOAD_STATUS_ONGOING) {
108  if(Update.end(true)) { //true to set the size to the current progress
109  //Now Reboot
110  output.printMSG("Update 100%");
111  _upload_status=UPLOAD_STATUS_SUCCESSFUL;
112  } else {
113  output.printERROR("Update failed!",500);
114  _upload_status=UPLOAD_STATUS_FAILED;
115  pushError(ESP_ERROR_FILE_CLOSE, "File close failed");
116  }
117  } else {
118  _upload_status=UPLOAD_STATUS_FAILED;
119  output.printERROR("Update failed!", 500);
120  pushError(ESP_ERROR_FILE_CLOSE, "File close failed");
121  }
122  } else {
123  output.printERROR("Update failed!",500);
124  _upload_status=UPLOAD_STATUS_FAILED;
125  }
126  }
127  }
128  if(_upload_status == UPLOAD_STATUS_FAILED) {
129  cancelUpload();
130  Update.end();
131  }
132 }
133 #endif //HTTP_FEATURE && WEB_UPDATE_FEATURE
ESP_PRINTER_LCD_CLIENT
#define ESP_PRINTER_LCD_CLIENT
Definition: esp3doutput.h:26
ESP_ERROR_FILE_WRITE
#define ESP_ERROR_FILE_WRITE
Definition: defines.h:107
ESP_ERROR_FILE_CLOSE
#define ESP_ERROR_FILE_CLOSE
Definition: defines.h:111
AuthenticationService::authenticated_level
static level_authenticate_type authenticated_level(const char *pwd=nullptr)
Definition: authentication_service.cpp:61
UPLOAD_STATUS_FAILED
@ UPLOAD_STATUS_FAILED
Definition: http_server.h:39
UPLOAD_STATUS_ONGOING
@ UPLOAD_STATUS_ONGOING
Definition: http_server.h:42
ESP_ERROR_NOT_ENOUGH_SPACE
#define ESP_ERROR_NOT_ENOUGH_SPACE
Definition: defines.h:109
ESP_FileSystem::max_update_size
static size_t max_update_size()
Definition: esp_filesystem.cpp:60
ESP_ERROR_AUTHENTICATION
#define ESP_ERROR_AUTHENTICATION
Definition: defines.h:105
LEVEL_ADMIN
@ LEVEL_ADMIN
Definition: authentication_service.h:28
ESP3DOutput
Definition: esp3doutput.h:48
UPLOAD_STATUS_SUCCESSFUL
@ UPLOAD_STATUS_SUCCESSFUL
Definition: http_server.h:41