-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.h
More file actions
93 lines (89 loc) · 2.61 KB
/
update.h
File metadata and controls
93 lines (89 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#define TAR_SILENT
#include <untar.h>
#include "StreamBuf.h"
#define FWNAME "firmware.bin"
Tar<FS> tar(&SPIFFS);
StreamBuf sb;
bool isFW = false;
// TAR Callback. Called on every file.
bool tarFile(char* b) {
char* fwfile = FWNAME;
if (strcmp(b, fwfile) == 0) {
isFW = true;
return false;
}
return true;
}
// TAR Callback. Called on each data block.
void tarData(char* b, size_t s) {
if(isFW) {
Update.write((uint8_t*)b, s);
// if(Update.write((uint8_t*)b, s) != s){
// Update.printError(Serial);
// }
}
}
// TAR Callback. Called on each file end.
void tarEof() {
isFW = false;
}
uint32_t initUpdate(){
sb.setTimeout(1); // Minimize read delay
tar.onFile(tarFile);
tar.onData(tarData);
tar.onEof(tarEof);
tar.dest("/");
server.on("/update", HTTP_POST, [](){
#ifdef UPLOADPASS
if(!server.authenticate(UPLOADUSER, UPLOADPASS)) {
return server.requestAuthentication();
}
#endif
server.sendHeader("Connection", "close");
server.sendHeader("Refresh", "5; url=/list");
if (Update.hasError()) {
server.send_P(500, "text/plain", PSTR("Update failed."));
} else {
server.send_P(200, "text/plain", PSTR("Update OK. Rebooting..."));
taskAddWithDelay(restartESP, 500);
}
},[](){
#ifdef UPLOADPASS
if(!server.authenticate(UPLOADUSER, UPLOADPASS)) {
return server.requestAuthentication();
}
#endif
HTTPUpload& upload = server.upload();
if(upload.status == UPLOAD_FILE_START){
//Serial.setDebugOutput(true);
WiFiUDP::stopAll();
//Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if(!Update.begin(maxSketchSpace)){//start with max available size
//Serial.println(maxSketchSpace);
//Update.printError(Serial);
}
tar.open((Stream*)&sb);
} else if(upload.status == UPLOAD_FILE_WRITE){
//Serial.print("Block: ");
//Serial.println(upload.currentSize);
//Serial.print(".");
sb.open((uint8_t*)&upload.buf, upload.currentSize);
tar.extract();
//Serial.print("o");
}
if(upload.status == UPLOAD_FILE_END){
Update.end(true);
/*
if(Update.end(true)){ //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
*/
}
});
return RUN_DELETE;
}