-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.cpp
526 lines (478 loc) · 16 KB
/
web.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright (c) 2021, 2022 - Schelte Bron
#include "otgwmcu.h"
#include "webserver.h"
#include "debug.h"
#include "otmon.h"
#include "version.h"
#include <LittleFS.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266HTTPUpdateServer.h>
WebServer httpd(80);
ESP8266HTTPUpdateServer httpUpdater(false);
// Bitmaps for subscriptions of web socket clients
static unsigned int ws_status, ws_otlog, ws_progress;
static unsigned int updays = 0;
static File fsUploadFile;
const char *hexheaders[] = {
"Last-Modified",
"X-Version"
};
static const char useragent[] PROGMEM = "OTGWMCU " VERSION;
unsigned int uptime() {
static unsigned int tstamp = 0;
unsigned int up, ms = millis();
up = ms - tstamp;
if (up >= 86400000) {
updays++;
tstamp += 86400000;
}
return up;
}
int running(char *buffer) {
unsigned int h, m, s, ms;
ms = uptime() % 86400000;
h = ms / 3600000;
ms = ms % 3600000;
m = ms / 60000;
ms = ms % 60000;
s = ms / 1000;
ms = ms % 1000;
return sprintf_P(buffer, PSTR("%lu days %02d:%02d:%02d.%03d"), updays, h, m, s, ms);
}
bool servefile(String path) {
if (path.endsWith("/")) {
path += "index.html";
}
String contentType;
contentType = mime::getContentType(path);
if (LittleFS.exists(path)) {
File file = LittleFS.open(path, "r");
httpd.streamFile(file, contentType);
file.close();
return true;
} else {
return false;
}
}
void listfiles() {
Dir dir = LittleFS.openDir("/");
httpd.chunkedResponseModeStart(200, "text/plain");
while (dir.next()) {
httpd.sendContent(dir.fileName() + "\n");
}
httpd.chunkedResponseFinalize();
}
void servefilesys() {
String uri = ESP8266WebServer::urlDecode(httpd.uri());
if (servefile(uri)) return;
if (uri.endsWith("/")) {
listfiles();
}
httpd.send(404, "text/plain", "Not found");
}
void mainpage() {
servefile("/index.html");
}
void filelist() {
char sep1 = '\0', sep2, s[400], version[16];
int n;
Dir dir;
File f;
httpd.chunkedResponseModeStart(200, "text/javascript");
n = sprintf_P(s, PSTR("var ls={"));
dir = LittleFS.openDir("/");
while (dir.next()) {
if (!dir.isDirectory()) continue;
// if (!dir.fileName().startsWith("pic16f")) continue;
if (sep1) {s[n++] = sep1;} else {sep1 = ',';}
n += sprintf_P(s + n, PSTR("%s:["), dir.fileName().c_str());
sep2 = '\0';
Dir subdir = LittleFS.openDir("/" + dir.fileName());
while (subdir.next()) {
if (subdir.fileName().endsWith(".hex")) {
String verfile = "/" + dir.fileName() + "/" + subdir.fileName();
verfile.replace(".hex", ".ver");
f = LittleFS.open(verfile, "r");
if (f) {
f.readBytesUntil('\n', version, 15);
f.close();
} else {
sprintf(version, "0.0");
}
if (sep2) {s[n++] = sep2;} else {sep2 = ',';}
n += sprintf_P(s + n, PSTR("{name:'%s',version:'%s',size:%d}"),
subdir.fileName().c_str(), version, subdir.fileSize());
}
if (n >= 300) {
httpd.sendContent(s, n);
n = 0;
}
}
s[n++] = ']';
}
n += sprintf_P(s + n,
PSTR("}\nvar processor = '%s', firmware = ['%s', '%s']\n"),
Pic.processorToString().c_str(),
Pic.firmwareToString().c_str(), Pic.firmwareVersion());
httpd.sendContent(s, n);
httpd.chunkedResponseFinalize();
}
void refresh(String filename, String version) {
WiFiClient client;
HTTPClient http;
String latest;
int code;
http.setUserAgent(FPSTR(useragent));
http.begin(client, "http://otgw.tclcode.com/download/" + filename);
http.collectHeaders(hexheaders, 2);
code = http.sendRequest("HEAD");
if (code == HTTP_CODE_OK) {
for (int i = 0; i< http.headers(); i++) {
debuglog(PSTR("%s: %s\n"), hexheaders[i], http.header(i).c_str());
}
latest = http.header(1);
if (latest != version) {
debuglog(PSTR("Update %s: %s -> %s\n"), filename.c_str(), version.c_str(), latest.c_str());
http.end();
http.begin(client, "http://otgw.tclcode.com/download/" + filename);
code = http.GET();
if (code == HTTP_CODE_OK) {
File f = LittleFS.open("/" + filename, "w");
if (f) {
http.writeToStream(&f);
f.close();
String verfile = "/" + filename;
verfile.replace(".hex", ".ver");
f = LittleFS.open(verfile, "w");
if (f) {
f.print(latest + "\n");
f.close();
debuglog(PSTR("Update successful\n"));
}
}
}
}
}
http.end();
}
void firmware() {
String action = httpd.arg("command");
String filename = httpd.arg("name");
debuglog(PSTR("Action: %s %s\n"), action.c_str(), filename.c_str());
if (action == "download") {
fwupgradestart(String("/" + filename).c_str());
} else if (action == "update") {
String version = httpd.arg("version");
refresh(filename, version);
} else if (action == "delete") {
String path = "/" + filename;
LittleFS.remove(path);
path.replace(".hex", ".ver");
LittleFS.remove(path);
}
httpd.sendHeader("Location", "firmware.html", true);
httpd.send(303, "text/html", "<a href='firmware.html'>Return</a>");
}
void httpcmd() {
String cmd = httpd.arg("cmd");
if (cmd) {
command(cmd.c_str());
}
httpd.send(200, "text/plain", "Command sent");
}
void debuginfo() {
FSInfo fsinfo;
char buffer[250];
int cnt;
httpd.chunkedResponseModeStart(200, "text/html");
httpd.sendContent_P(PSTR("<!DOCTYPE html>\n<html>\n<head>\n"
"<link rel=\"icon\" type=\"image/png\" href=\"favicon.png\">\n"
"<meta charset=\"utf-8\"/>\n<title>OTGWMCU</title>\n"
"<style>\nbody {\n background: white;\n}\n</style>\n"
"</head>\n<body>\n"));
// MAC address
sprintf_P(buffer, PSTR("MAC Address: %s<br>\n"), WiFi.macAddress().c_str());
httpd.sendContent(buffer);
// IP address
sprintf_P(buffer, PSTR("IP Address: %s<br>\n"), WiFi.localIP().toString().c_str());
httpd.sendContent(buffer);
// System uptime
cnt = sprintf_P(buffer, PSTR("Uptime: "));
cnt += running(buffer + cnt);
cnt += sprintf_P(buffer + cnt, PSTR("<br>\n"));
httpd.sendContent(buffer, cnt);
// Last reset reason
String str = ESP.getResetReason();
sprintf_P(buffer, PSTR("Reset reason: %s<br>\n"), str.c_str());
httpd.sendContent(buffer);
// File system usage
FSInfo fs;
LittleFS.info(fs);
sprintf_P(buffer, PSTR("File system: %d%% (%d/%d)<br>\n"), 100 * fs.usedBytes / fs.totalBytes, fs.usedBytes, fs.totalBytes);
httpd.sendContent(buffer);
// Sketch usage
const int sketchtotal = 1044464;
int sketchused = ESP.getSketchSize();
sprintf_P(buffer, PSTR("Program: %d%% (%d/%d)<br>\n"), 100 * sketchused / sketchtotal, sketchused, sketchtotal);
httpd.sendContent(buffer);
// Memory usage
const int memtotal = 81920;
int memused = memtotal - ESP.getFreeHeap();
sprintf_P(buffer, PSTR("Memory: %d%% (%d/%d)<br>\n"), 100 * memused / memtotal, memused, memtotal);
httpd.sendContent(buffer);
// Memory fragmentation
sprintf_P(buffer, PSTR("Fragmentation: %d%%<br>\n"), ESP.getHeapFragmentation());
httpd.sendContent(buffer);
// Platform versions
sprintf_P(buffer, PSTR("Core version: %s<br>SDK version: %s<br>\n"),
ESP.getCoreVersion().c_str(), ESP.getSdkVersion());
httpd.sendContent(buffer);
// ATtiny85 WDT chip
cnt = sprintf_P(buffer, PSTR("WDT chip: "));
cnt += dumpattiny(buffer + cnt);
cnt += sprintf_P(buffer + cnt, PSTR("<br>\n"));
httpd.sendContent(buffer, cnt);
httpd.sendContent_P(PSTR("</body>\n</html>\n"));
httpd.chunkedResponseFinalize();
}
// Web sockets
unsigned int websockdistribute(const char *str, unsigned int clients) {
unsigned int n, fail = 0;
uint8_t num;
for (n = clients, num = 0; n != 0; n >>= 1, num++) {
if (n & 1)
if (!httpd.sendTXT(num, str))
fail++;
}
return fail;
}
bool websocket(uint8_t num, WStype_t type, unsigned int *clientmap) {
switch (type) {
case WStype_CONNECTED: // if a new websocket connection is established
debuglog(PSTR("[%u] Connected!\n"), num);
if (clientmap) *clientmap |= 1 << num;
return true;
case WStype_DISCONNECTED: // if the websocket is disconnected
debuglog(PSTR("[%u] Disconnected!\n"), num);
if (clientmap) *clientmap &= ~(1 << num);
break;
default:
break;
}
return false;
}
void websocketsend(int num, char *str) {
httpd.sendTXT(num, str);
}
void websockreport(char *json) {
if (ws_status != 0) {
websockdistribute(json, ws_status);
}
}
void wsstatus(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
if (websocket(num, type, &ws_status)) {
initialreport(num);
}
}
void websockotmessage(char dir, unsigned message) {
char buffer[256];
int len;
if (ws_otlog != 0) {
len = otformat(buffer, dir, message);
websockdistribute(buffer, ws_otlog);
}
}
void wsotlog(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
websocket(num, type, &ws_otlog);
}
void httpflash() {
String confirm = httpd.arg("confirm");
if (confirm == "Start") {
flashpic();
} else if (confirm == "Verify") {
verifypic();
}
httpd.sendHeader("Location", "index.html", true);
httpd.send(303, "text/html", "<a href='index.html'>Return</a>");
}
void otainfo() {
WiFiClient client;
HTTPClient http;
char buffer[80];
String latest = "unknown";
// Don't spend too much time waiting for a response
http.setTimeout(1000);
http.setUserAgent(FPSTR(useragent));
if (http.begin(client, otaurl() + "/version.txt")) {
http.addHeader("x-ESP8266-STA-MAC", WiFi.macAddress());
int code = http.GET();
if (code == HTTP_CODE_OK) {
latest = http.getString();
}
http.end();
}
// Remove the newline at the end of the file
latest.trim();
int len = sprintf_P(buffer,
PSTR("{\"version\":\"" VERSION "\",\"latest\":\"%s\"}"), latest.c_str());
httpd.send(200, "application/json", buffer, len);
}
void websockprogress(const char *fmt, ...) {
if (ws_progress != 0) {
char buffer[256];
int len;
va_list argptr;
va_start(argptr, fmt);
len = vsnprintf_P(buffer, sizeof(buffer), fmt, argptr);
va_end(argptr);
websockdistribute(buffer, ws_progress);
}
}
void wsdownload(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
websocket(num, type, &ws_progress);
}
void httpota() {
const char *message = PSTR(
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<meta charset='utf-8'>\n"
"<title>OTA upgrade</title>\n"
"<style>\n"
"html {\n"
" background: white;\n"
"}\n"
"h1 {\n"
" color: #509e0e;\n"
"}\n"
"</style>\n"
"</head>\n"
"<body>\n"
"<h1>OTA upgrade</h1>\n"
"OTA upgrade started. Check the LED for progress and result.\n"
"</body>\n"
"</html>\n"
);
httpd.send_P(202, "text/html", message);
delay(200);
otaupgrade();
}
void uploadmain() {
if (fsUploadFile) {
const char *location = "upload.html";
String name = fsUploadFile.fullName();
fsUploadFile.close();
if (httpd.arg("pic")) {
String dir = httpd.arg("pic");
if (dir != "") {
bool result = LittleFS.rename(name, "/" + dir + name);
if (name.endsWith(".hex")) {
name.replace(".hex", ".ver");
fsUploadFile = LittleFS.open("/" + dir + name, "w");
if (fsUploadFile) {
fsUploadFile.printf("%s\n", httpd.arg("version").c_str());
fsUploadFile.close();
}
}
location = "firmware.html";
}
}
// Redirect the client to the success page
httpd.sendHeader("Location", location);
httpd.send(303);
} else {
httpd.send(500, "text/plain", "500: couldn't create file");
}
}
void uploadfile() {
String filename;
HTTPUpload& upload = httpd.upload();
switch (upload.status) {
case UPLOAD_FILE_START:
filename = upload.filename;
filename = httpd.arg("target") + "/" + filename;
debuglog(PSTR("handleFileUpload Name: %s\n"), filename.c_str());
// Open the file for writing (create if it doesn't exist)
fsUploadFile = LittleFS.open(filename, "w");
break;
case UPLOAD_FILE_WRITE:
// Write the received bytes to the file
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
break;
case UPLOAD_FILE_END:
// The file is closed by uploadmain()
debuglog(PSTR("handleFileUpload Size: %d\n"), upload.totalSize);
break;
}
}
void upgrademain() {
httpd.sendHeader("Connection", "close");
httpd.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
debuglog(PSTR("Rebooting...\n"));
delay(100);
httpd.client().stop();
ESP.restart();
}
void upgradefile() {
HTTPUpload& upload = httpd.upload();
if (upload.status == UPLOAD_FILE_START) {
// WiFiUDP::stopAll();
debuglog(PSTR("Update: %s\n"), upload.filename.c_str());
if (upload.name == "filesystem") {
//start with max available size
uint32_t fsSize = (uint32_t)&_FS_end - (uint32_t)&_FS_start;
debuglog(PSTR("Filesystem: %d\n"), fsSize);
close_all_fs();
if (!Update.begin(fsSize, U_FS)){
debuglog(PSTR("Update error %d\n"), Update.getError());
}
} else {
//start with max available size
uint32_t fwSize = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(fwSize)) {
debuglog(PSTR("Update error %d\n"), Update.getError());
}
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
debuglog(PSTR("Update error %d\n"), Update.getError());
} else {
debuglog(PSTR("Upload: %d bytes\n"), upload.totalSize);
wdtevent();
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
debuglog(PSTR("Update Success: %u\n"), upload.totalSize);
} else {
debuglog(PSTR("Update error %d\n"), Update.getError());
}
}
yield();
}
void websetup() {
// Serve files from flash file system
httpd.onNotFound(servefilesys);
// Special web pages
httpd.on("/", HTTP_GET, mainpage);
httpd.on("/filelist.js", HTTP_GET, filelist);
httpd.on("/firmware.html", HTTP_POST, firmware);
httpd.on("/command.cgi", HTTP_POST, httpcmd);
httpd.on("/debug.html", HTTP_GET, debuginfo);
// Web sockets
httpd.on("/status.ws", HTTP_GET, [](){httpd.upgrade(wsstatus);});
httpd.on("/otlog.ws", HTTP_GET, [](){httpd.upgrade(wsotlog);});
httpd.on("/download.ws", HTTP_GET, [](){httpd.upgrade(wsdownload);});
// Maintenance
httpd.on("/upload.html", HTTP_POST, uploadmain, uploadfile);
httpd.on("/upgrade.html", HTTP_POST, upgrademain, upgradefile);
httpd.on("/flash.html", HTTP_POST, httpflash);
httpd.on("/otainfo.json", HTTP_GET, otainfo);
httpd.on("/ota.html", HTTP_GET, httpota);
httpUpdater.setup(&httpd, "/update.html");
httpd.begin();
}
void webevent() {
httpd.handleClient();
uptime();
}