forked from DustinWatts/FreeTouchDeck
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebserver.cpp
596 lines (534 loc) · 23 KB
/
Webserver.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "Webserver.h"
#include "globals.hpp"
#include "Storage.h"
#include "ConfigLoad.h"
#include "ConfigHelper.h"
namespace FreeTouchDeck
{
extern cJSON * MenusToJsonObject(bool withSystem);
using namespace fs;
String resultHeader;
String resultText;
String resultFiles = "";
cJSON * handleFileList(const char * path)
{
File root = ftdfs->open(path,FILE_READ);
cJSON * doc = cJSON_CreateArray();
cJSON * fileEntry = NULL;
String fileName;
if (root.isDirectory())
{
File file = root.openNextFile();
while (file)
{
fileName = file.name();
fileName.substring(fileName.lastIndexOf('/'));
fileEntry= cJSON_CreateObject();
cJSON_AddStringToObject(fileEntry,"name",fileName.c_str());
cJSON_AddNumberToObject(fileEntry,"size",file.size());
cJSON_AddStringToObject(fileEntry,"type",file.isDirectory()?"D":"F");
cJSON_AddItemToArray(doc,fileEntry);
file = root.openNextFile();
}
file.close();
}
root.close();
return doc;
}
bool RespondWithJSON(AsyncWebServerRequest *request, cJSON * doc)
{
char * result = AllocPrintJson(doc);
if (result)
{
request->send(200, "application/json", result);
free(result);
}
else
{
request->send(500, "Error Generating JSON structure");
}
}
cJSON * AllocGetInfoJson()
{
cJSON * infoDoc = cJSON_CreateArray();
float freemem = ftdfs->totalBytes() - ftdfs->usedBytes();
cJSON * element = cJSON_CreateObject();
cJSON_AddStringToObject(element,"Version",versionnumber);
cJSON_AddItemToArray(infoDoc,element);
element = cJSON_CreateObject();
cJSON_AddNumberToObject(element,"Free Space",freemem / 1000);
cJSON_AddItemToArray(infoDoc,element);
element = cJSON_CreateObject();
cJSON_AddStringToObject(element,"BLE Keyboard Version",BLE_KEYBOARD_VERSION);
cJSON_AddItemToArray(infoDoc,element);
element = cJSON_CreateObject();
cJSON_AddStringToObject(element,"TFT_eSPI Version",TFT_ESPI_VERSION);
cJSON_AddItemToArray(infoDoc,element);
element = cJSON_CreateObject();
cJSON_AddStringToObject(element,"ESP-IDF",esp_get_idf_version());
cJSON_AddItemToArray(infoDoc,element);
element = cJSON_CreateObject();
cJSON_AddStringToObject(element,"WiFi Mode",wificonfig.wifimode);
cJSON_AddItemToArray(infoDoc,element);
#ifdef touchInterruptPin
element = cJSON_CreateObject();
cJSON_AddStringToObject(element,"Sleep",generalconfig.sleepenable?"Enabled":"Disabled");
cJSON_AddItemToArray(infoDoc,element);
element = cJSON_CreateObject();
cJSON_AddNumberToObject(element,"Sleep Timer",generalconfig.sleeptimer);
cJSON_AddItemToArray(infoDoc,element);
#else
element = cJSON_CreateObject();
cJSON_AddStringToObject(element,"Sleep","Disabled");
cJSON_AddItemToArray(infoDoc,element);
#endif
return infoDoc;
}
String errorCode;
String errorText;
String processor(const String &var)
{
if (var == "ERROR_CODE")
{
return errorCode;
}
if (var == "ERROR_TEXT")
{
return errorText;
}
return String();
}
void handleJSONUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
if (filename != "menu1.json" && filename != "menu2.json" && filename != "menu3.json" && filename != "menu4.json" && filename != "menu5.json" && filename != "colors.json" && filename != "homescreen.json")
{
LOC_LOGI(module, "JSON has invalid name: %s\n", filename.c_str());
errorCode = "102";
errorText = "JSON file has an invalid name. You can only upload JSON files with the following file names:";
errorText += "<ul><li>menu1.json</li><li>menu2.json</li><li>menu3.json</li><li>menu4.json</li><li>menu5.json</li>";
errorText += "<li>colors.json</li><li>homescreen.json</li></ul>";
request->send((ftdfs->fileSystem), "/error.htm", String(), false, processor);
return;
}
if (!index)
{
LOC_LOGI(module, "JSON Upload Start: %s\n", filename.c_str());
filename = "/config/" + filename; // TODO: Does the config directory need to be hardcoded?
// Open the file on first call and store the file handle in the request object
request->_tempFile = ftdfs->stopen(filename, FILE_WRITE);
}
if (len)
{
// Stream the incoming chunk to the opened file
request->_tempFile.write(data, len);
}
if (final)
{
LOC_LOGI(module, "JSON Uploaded: %s\n", filename.c_str());
// Close the file handle as the upload is now done
request->_tempFile.close();
request->send((ftdfs->fileSystem), "/upload.htm");
}
}
void handleAPIUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
if (!index)
{
LOC_LOGI(module, "API file Upload Start: %s\n", filename.c_str());
filename = "/uploads/" + filename; // TODO: Does the uploads directory need to be hardcoded?
// Open the file on first call and store the file handle in the request object
request->_tempFile = ftdfs->stopen(filename, "w");
}
if (len)
{
// Stream the incoming chunk to the opened file
request->_tempFile.write(data, len);
}
if (final)
{
LOC_LOGI(module, "API file Uploaded: %s\n", filename.c_str());
// Close the file handle as the upload is now done
request->_tempFile.close();
request->send((ftdfs->fileSystem), "/upload.htm");
}
}
/* --------------- Checking for free space on storage ----------------
Purpose: This checks if the free memory on the storage is bigger then a set threshold
Input : none
Output : boolean
Note : none
*/
bool spaceLeft()
{
float minmem = 100000.00; // Always leave 100 kB free pace on storage
float freeMemory = ftdfs->totalBytes() - ftdfs->usedBytes();
LOC_LOGI(module, "Free storage: %f bytes\n", freeMemory);
if (freeMemory < minmem)
{
return false;
}
return true;
}
/**
* @brief This function handles a file upload used by the Webserver
*
* @param *request
* @param filename String
* @param index size_t
* @param *data uint8_t
* @param len size_t
* @param final boolean
*
* @return none
*
* @note The reason the file is first uploaded and then deleted if there is not enough free space, is that
if the request is not handled, the ESP32 craches. So we have to accept the upload but
can delete it.
*/
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
if (!index)
{
LOC_LOGI(module, "File Upload Start: %s\n", filename.c_str());
filename = "/logos/" + filename;
// Open the file on first call and store the file handle in the request object
request->_tempFile = ftdfs->stopen(filename, "w");
}
if (len)
{
// Stream the incoming chunk to the opened file
request->_tempFile.write(data, len);
}
if (final)
{
LOC_LOGI(module, "File Uploaded: %s\n", filename.c_str());
// Close the file handle as the upload is now done
request->_tempFile.close();
// If there is not enough space left, we have to delete the recently uploaded file
if (!spaceLeft())
{
LOC_LOGD(module, "Not enough free space left");
errorCode = "103";
errorText = "There is not enough free space left to upload data. Please delete unused logos and try again.";
request->send((ftdfs->fileSystem), "/error.htm", String(), false, processor);
// Remove the recently uploaded file
String fileToDelete = "/logos/";
fileToDelete += filename;
ftdfs->stremove(fileToDelete);
LOC_LOGD(module, "File removed to keep enough free space");
return;
}
else
{
request->send((ftdfs->fileSystem), "/upload.htm");
}
}
}
/**
* @brief This function handles delete.htm template processing.
*
* @param &var const String
*
* @return String
*
* @note Only need to call this once! This is also where the saving of config files is done.
*/
String deleteProcessor(const String &var)
{
if (var == "RESULT")
{
return resultHeader;
}
if (var == "TEXT")
{
return resultText;
}
if (var == "FILES")
{
return resultFiles;
}
return String();
}
/**
* @brief This function adds all the handlers we need to the webserver.
*
* @param none
*
* @return none
*
* @note Only need to call this once! This is also where the saving of config files is done.
*/
void handlerSetup()
{
webserver.serveStatic("/", ftdfs->fileSystem, "/").setDefaultFile("index.htm");
//----------- index.htm handler -----------------
webserver.on("/index.htm", HTTP_POST, [](AsyncWebServerRequest *request)
{ request->send((ftdfs->fileSystem), "/index.htm"); });
webserver.on("/index2.htm", HTTP_POST, [](AsyncWebServerRequest *request)
{ request->send((ftdfs->fileSystem), "/index2.htm"); });
//----------- saveconfig handler -----------------
webserver.on("/saveconfig", HTTP_POST, [](AsyncWebServerRequest *request)
{
if (request->hasParam("save", true))
{
AsyncWebParameter *p = request->getParam("save", true);
String savemode = p->value().c_str();
if (savemode == "general")
{
AsyncWebParameter *value = request->getParam("menubuttoncolor", true);
if (value)
{
generalconfig.menuButtonColour = convertRGB888ToRGB565(convertHTMLtoRGB888(value->value().c_str()));
}
value = request->getParam("functionbuttoncolor", true);
if (value)
{
generalconfig.functionButtonColour = convertRGB888ToRGB565(convertHTMLtoRGB888(value->value().c_str()));
}
value = request->getParam("latchcolor", true);
if (value)
{
generalconfig.latchedColour = convertRGB888ToRGB565(convertHTMLtoRGB888(value->value().c_str()));
}
value = request->getParam("backgroundcolor", true);
if (value)
{
generalconfig.backgroundColour = convertRGB888ToRGB565(convertHTMLtoRGB888(value->value().c_str()));
}
value = request->getParam("flip_touch_axis", true);
if (value)
{
generalconfig.flip_touch_axis = value->value() == "true";
}
value = request->getParam("reverse_x_touch", true);
if (value)
{
generalconfig.reverse_x_touch = value->value() == "true";
}
value = request->getParam("reverse_y_touch", true);
if (value)
{
generalconfig.reverse_y_touch = value->value() == "true";
}
value = request->getParam("rotation", true);
if (value)
{
uint8_t rot = value->value().toInt();
generalconfig.screenrotation = rot >= 0 && rot <= 3 ? rot : generalconfig.screenrotation;
}
value = request->getParam("sleepenable", true);
if (value)
{
generalconfig.sleepenable = value->value() == "true";
}
value = request->getParam("beep", true);
if (value)
{
generalconfig.beep = value->value() == "true";
}
value = request->getParam("sleeptimer", true);
if (value)
{
generalconfig.sleeptimer = value->value().toInt();
}
value = request->getParam("helperdelay", true);
if (value)
{
generalconfig.helperdelay = value->value().toInt();
}
value = request->getParam("rowscount", true);
if (value)
{
generalconfig.rowscount = value->value().toInt();
}
value = request->getParam("colscount", true);
if (value)
{
generalconfig.colscount = value->value().toInt();
}
saveConfig(false);
}
else if (savemode == "wifi")
{
// --- Saving wifi config
LOC_LOGI(module, "Saving Wifi Config");
ftdfs->remove("/config/wificonfig.json");
File file = ftdfs->open("/config/wificonfig.json", "w");
if (!file)
{
LOC_LOGD(module, "Failed to create file");
return;
}
EraseWifiConfig(&wificonfig);
AsyncWebParameter *ssid = request->getParam("ssid", true);
wificonfig.ssid = ps_strdup(ssid->value().c_str());
AsyncWebParameter *password = request->getParam("password", true);
wificonfig.password = ps_strdup(password->value().c_str());
AsyncWebParameter *wifimode = request->getParam("wifimode", true);
wificonfig.wifimode = ps_strdup(wifimode->value().c_str());
AsyncWebParameter *hostname = request->getParam("wifihostname", true);
wificonfig.hostname = ps_strdup(hostname->value().c_str());
AsyncWebParameter *attempts = request->getParam("attempts", true);
wificonfig.attempts=attempts->value().toInt();
AsyncWebParameter *attemptdelay = request->getParam("attemptdelay", true);
wificonfig.attemptdelay=attemptdelay->value().toInt();
Commit(&wificonfig);
}
request->send((ftdfs->fileSystem), "/saveconfig.htm");
}
});
//----------- File list handler -----------------
webserver.on("/list", HTTP_GET, [](AsyncWebServerRequest *request)
{
if (request->hasParam("dir"))
{
AsyncWebParameter *p = request->getParam("dir");
RespondWithJSON(request, handleFileList(p->value().c_str())) ;
}
});
webserver.on("/menus.json", HTTP_GET, [](AsyncWebServerRequest *request){RespondWithJSON(request,MenusToJsonObject(false));});
webserver.on("/useractions.json", HTTP_GET, [](AsyncWebServerRequest *request){RespondWithJSON(request,UserActionsJson());});
webserver.on("/keynames.json", HTTP_GET, [](AsyncWebServerRequest *request){RespondWithJSON(request,KeyNamesJson());});
webserver.on("/info", HTTP_GET, [](AsyncWebServerRequest *request) { RespondWithJSON(request, AllocGetInfoJson()) ; });
//----------- 404 handler -----------------
webserver.onNotFound([](AsyncWebServerRequest *request)
{
Serial.printf("NOT_FOUND: ");
if (request->method() == HTTP_GET)
Serial.printf("GET");
else if (request->method() == HTTP_POST)
Serial.printf("POST");
else if (request->method() == HTTP_DELETE)
Serial.printf("DELETE");
else if (request->method() == HTTP_PUT)
Serial.printf("PUT");
else if (request->method() == HTTP_PATCH)
Serial.printf("PATCH");
else if (request->method() == HTTP_HEAD)
Serial.printf("HEAD");
else if (request->method() == HTTP_OPTIONS)
Serial.printf("OPTIONS");
else
Serial.printf("UNKNOWN");
Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
if (request->contentLength())
{
Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength());
}
int headers = request->headers();
int i;
for (i = 0; i < headers; i++)
{
AsyncWebHeader *h = request->getHeader(i);
Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
}
int params = request->params();
for (i = 0; i < params; i++)
{
AsyncWebParameter *p = request->getParam(i);
if (p->isFile())
{
Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
}
else if (p->isPost())
{
Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
else
{
Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(404);
});
webserver.onFileUpload([](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
{
if (!index)
Serial.printf("UploadStart: %s\n", filename.c_str());
Serial.printf("%s", (const char *)data);
if (final)
Serial.printf("UploadEnd: %s (%u)\n", filename.c_str(), index + len);
});
webserver.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
{
if (!index)
Serial.printf("BodyStart: %u\n", total);
Serial.printf("%s", (const char *)data);
if (index + len == total)
Serial.printf("BodyEnd: %u\n", total);
});
webserver.on(
"/upload", HTTP_POST, [](AsyncWebServerRequest *request)
{ request->send((ftdfs->fileSystem), "/upload.htm"); },
handleUpload);
webserver.on("/restart", HTTP_POST, [](AsyncWebServerRequest *request)
{
// First send some text to the browser otherwise an ugly browser error shows up
request->send(200, "text/plain", "FreeTouchDeck is restarting...");
// Then restart the ESP
LOC_LOGD(module, "Restarting");
ESP.restart();
});
// ----------------------------- Error Handle ---------------------------------
webserver.on("/error", HTTP_GET, [](AsyncWebServerRequest *request)
{
errorCode = "123";
errorText = "Nothing went wrong, all is good. This is just a test";
request->send((ftdfs->fileSystem), "/error.htm", String(), false, processor);
});
// ----------------------------- Editor Handle ---------------------------------
webserver.on("/editor", HTTP_POST, [](AsyncWebServerRequest *request)
{
int params = request->params();
int i;
int filecount = 0;
for (i = 0; i < params; i++)
{
AsyncWebParameter *p = request->getParam(i);
LOC_LOGI(module, "Deleting file: %s\n", p->value().c_str());
String filename = "/logos/";
filename += p->value().c_str();
if (ftdfs->stexists(filename))
{
ftdfs->stremove(filename);
}
resultFiles += p->value().c_str();
resultFiles += "<br>";
filecount++;
}
if (filecount > 0)
{
resultHeader = "Succes!";
}
else
{
resultHeader = "Fail!";
}
resultText = String(filecount);
request->send((ftdfs->fileSystem), "/editor.htm", String(), false, deleteProcessor);
resultFiles = "";
});
// ----------------------------- JSON Download Handle ---------------------------------
webserver.on("/download", HTTP_GET, [](AsyncWebServerRequest *request)
{
AsyncWebParameter *p = request->getParam("file");
String filerequest = p->value().c_str();
LOC_LOGI(module, "Requested file: %s\n", filerequest.c_str());
String downloadfile = "/config/" + filerequest;
LOC_LOGI(module, "Full path: %s\n", downloadfile.c_str());
if (ftdfs->stexists(downloadfile))
{
LOC_LOGI(module, "Download file %s\n", downloadfile.c_str());
request->send((ftdfs->fileSystem), downloadfile, String(), true);
}
else
{
LOC_LOGI(module, "Download file %s doesn't exits!\n", downloadfile.c_str());
}
});
// ----------------------------- JSON Upload Handle ---------------------------------
webserver.on(
"/uploadJSON", HTTP_POST, [](AsyncWebServerRequest *request) {}, handleJSONUpload);
}
}