-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSBrowser.cpp
701 lines (573 loc) · 18.6 KB
/
FSBrowser.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
FSWebServer - Example WebServer with SPIFFS backend for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the ESP8266WebServer library for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
upload the contents of the data folder with MkSPIFFS Tool ("ESP8266 Sketch Data Upload" in Tools menu in Arduino IDE)
or you can upload the contents of a folder if you CD in that folder and run the following command:
for file in `ls -A1`; do curl -F "file=@$PWD/$file" esp8266fs.local/edit; done
access the sample web page at http://esp8266fs.local
edit the page by going to http://esp8266fs.local/edit
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#include <ArduinoJson.h>
#define htonl(x) (((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24))
#define DBG_OUTPUT_PORT Serial
typedef struct wifiMultEntry {
String ssid;
String password;
} WifiMultiEntry;
typedef struct wifiConfig {
String ApHostname;
String ApPassword;
std::vector <WifiMultiEntry> multiEntries;
String mdnsHostname;
} WifiConfig;
WifiConfig wifiConf;
ESP8266WiFiMulti wifiMulti;
char host[30];
int timeZone_;
ESP8266WebServer server(80);
//holds the current upload
File fsUploadFile;
//format bytes
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
String getContentType(String filename) {
if (server.hasArg("download"))
return "application/octet-stream";
else if (filename.endsWith(".htm"))
return "text/html";
else if (filename.endsWith(".html"))
return "text/html";
else if (filename.endsWith(".css"))
return "text/css";
else if (filename.endsWith(".js"))
return "application/javascript";
else if (filename.endsWith(".png"))
return "image/png";
else if (filename.endsWith(".gif"))
return "image/gif";
else if (filename.endsWith(".jpg"))
return "image/jpeg";
else if (filename.endsWith(".ico"))
return "image/x-icon";
else if (filename.endsWith(".xml"))
return "text/xml";
else if (filename.endsWith(".pdf"))
return "application/x-pdf";
else if (filename.endsWith(".zip"))
return "application/x-zip";
else if (filename.endsWith(".gz"))
return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path) {
static uint32_t apSubnet = 0;
uint32_t clIp = server.client().remoteIP();
bool apClient = false;
if (apSubnet == 0)
{
apSubnet = WiFi.softAPIP();
apSubnet = htonl(apSubnet) & 0xffffff00;
}
DBG_OUTPUT_PORT.println("handleFileRead: " + path);
// this says the remote is in the 192.168.4.0 subnet
if ((htonl(clIp) & 0xffffff00) == apSubnet) {
DBG_OUTPUT_PORT.println("From an AP client");
apClient = true;
}
if (path.endsWith("/"))
path += "index.htm";
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (apClient)
{
/* if we are talking to an AP client maybe give him different files if they exist.
* prepend an '/ap' to the path and try with that. The '/ap' isn't really a directory
* it is just the name of the file.
* If there is no file try without.
*/
if (SPIFFS.exists("/ap" + pathWithGz) || SPIFFS.exists("/ap" + path)) {
pathWithGz = "/ap" + pathWithGz;
path = "/ap" + path;
}
}
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if (SPIFFS.exists(pathWithGz))
path += ".gz";
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
void handleFileUpload() {
if (server.uri() != "/edit")
return;
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/"))
filename = "/" + filename;
DBG_OUTPUT_PORT.print("handleFileUpload Name: ");
DBG_OUTPUT_PORT.println(filename);
fsUploadFile = SPIFFS.open(filename, "w");
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
//DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile)
fsUploadFile.close();
DBG_OUTPUT_PORT.print("handleFileUpload Size: ");
DBG_OUTPUT_PORT.println(upload.totalSize);
}
}
void handleFileDelete() {
if (server.args() == 0)
return server.send(500, "text/plain", "BAD ARGS");
String path = server.arg(0);
DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
if (path == "/")
return server.send(500, "text/plain", "BAD PATH");
if (!SPIFFS.exists(path))
return server.send(404, "text/plain", "FileNotFound");
SPIFFS.remove(path);
server.send(200, "text/plain", "");
path = String();
}
void handleFileCreate() {
if (server.args() == 0)
return server.send(500, "text/plain", "BAD ARGS");
String path = server.arg(0);
DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
if (path == "/")
return server.send(500, "text/plain", "BAD PATH");
if (SPIFFS.exists(path))
return server.send(500, "text/plain", "FILE EXISTS");
File file = SPIFFS.open(path, "w");
if (file)
file.close();
else
return server.send(500, "text/plain", "CREATE FAILED");
server.send(200, "text/plain", "");
path = String();
}
void handleFileList() {
if (!server.hasArg("dir")) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
String path = server.arg("dir");
DBG_OUTPUT_PORT.println("handleFileList: " + path);
Dir dir = SPIFFS.openDir(path);
path = String();
String output = "[";
while (dir.next()) {
File entry = dir.openFile("r");
if (output != "[")
output += ',';
bool isDir = false;
output += "{\"type\":\"";
output += (isDir) ? "dir" : "file";
output += "\",\"name\":\"";
output += String(entry.name()).substring(1);
output += "\"}";
entry.close();
}
output += "]";
server.send(200, "text/json", output);
}
void handleSsidGet(void) {
String output = "{\"SSID\":\"";
output += WiFi.SSID();
output += "\"}";
server.send(200, "text/json", output);
}
void addMac(String &hostName, uint8_t *mac) {
size_t index = 0;
int x;
for (x = 0 ; x < 6 ; x++)
{
char tmpBuf[10];
char hexBuf[10];
index = 0;
sprintf(tmpBuf, "\%m%d", x);
sprintf(hexBuf, "%02x", mac[x]);
/* Make the replacement. */
hostName.replace(tmpBuf, hexBuf);
}
}
void setupAp(void) {
String ApName;
uint8_t mac[6];
// I should have done this before so that we can support AP and STA mode together
// WiFi.mode(WIFI_AP);
WiFi.softAPmacAddress(mac);
if (wifiConf.ApHostname == "")
{
ApName = String("espLights-%m4%m5");
}
else
{
ApName = wifiConf.ApHostname;
}
addMac(ApName, mac);
if (wifiConf.ApPassword != "")
{
DBG_OUTPUT_PORT.print("Setting up AP ");
DBG_OUTPUT_PORT.print(ApName);
DBG_OUTPUT_PORT.print(" password ");
DBG_OUTPUT_PORT.println(wifiConf.ApPassword);
WiFi.softAP(ApName.c_str(), wifiConf.ApPassword.c_str());
}
else
{
WiFi.softAP(ApName.c_str());
}
}
static const char *configFilename = "/wifi.json";
static const char *tmpConfigFilename = "/wifi.tmp";
static const char *configBackFilename = "/wifi.bak.json";
bool readWifiConfig(WifiConfig *pConfig) {
bool status = false;
int configSize = 0;
if (!SPIFFS.exists(configFilename)) {
// if it doesn't exist then return false
DBG_OUTPUT_PORT.print("Config file ");
DBG_OUTPUT_PORT.print(configFilename);
DBG_OUTPUT_PORT.println(" doesn't exist");
return false;
}
// file exists, open it
File configFile = SPIFFS.open(configFilename, "r");
if (!configFile) {
// if we can't open it then return false
DBG_OUTPUT_PORT.print("Config file ");
DBG_OUTPUT_PORT.print(configFilename);
DBG_OUTPUT_PORT.println(" open failed");
return false;
}
// get the size of the file
configSize = configFile.size();
DynamicJsonBuffer jsonBuffer;
// allocate space for the file
char *configJson = (char *) calloc(configSize, 1);
// read in the file
if (configFile.readBytes(configJson, configSize) == 0) {
DBG_OUTPUT_PORT.print("Config file ");
DBG_OUTPUT_PORT.print(configFilename);
DBG_OUTPUT_PORT.print(" read failed size ");
DBG_OUTPUT_PORT.println(configSize);
goto cleanup;
} else {
// we have hopefully a JSON string in memory, we need to parse it
JsonObject& wifiJson = jsonBuffer.parseObject(configJson);
if (wifiJson.success()) {
// we should be able to do cool things here. But for now let's print it
wifiJson.prettyPrintTo(Serial);
Serial.println();
// see if there is an ApHostname
if (wifiJson.containsKey("ApHostname"))
{
// read it in.
pConfig->ApHostname = wifiJson["ApHostname"].asString();
}
if (wifiJson.containsKey("ApPassword"))
{
// read it in.
pConfig->ApPassword = wifiJson["ApPassword"].asString();
}
JsonArray& hosts = wifiJson["WifiMulti"];
for (auto& jhost : hosts) {
WifiMultiEntry entry;
DBG_OUTPUT_PORT.print("The ssid = ");
entry.ssid = jhost["ssid"].asString();
DBG_OUTPUT_PORT.print(entry.ssid);
DBG_OUTPUT_PORT.print(" and the password = ");
entry.password = jhost["password"].asString();
DBG_OUTPUT_PORT.println(entry.password);
pConfig->multiEntries.push_back(entry);
}
if (wifiJson.containsKey("mdnsHostname"))
{
// read it in.
pConfig->mdnsHostname = wifiJson["mdnsHostname"].asString();
}
if (wifiJson.containsKey("timeZone"))
{
// read it in.
timeZone_ = (int) (wifiJson["timeZone"]) * 3600;
}
// we parsed it and stored it in the passed in struct, return true
status = true;
goto cleanup;
}
else {
// failed to parse, return false
DBG_OUTPUT_PORT.print("Config file ");
DBG_OUTPUT_PORT.print(configFilename);
DBG_OUTPUT_PORT.println(" failed to parse JSON");
goto cleanup;
}
}
cleanup:
configFile.close();
free(configJson);
return status;
}
bool writeWifiConfig(WifiConfig *pConfig) {
// so now we have to build a jsonObject
DynamicJsonBuffer jsonBuffer;
JsonObject& wifiJson = jsonBuffer.createObject();
if (pConfig->ApHostname != "")
{
wifiJson["ApHostname"] = pConfig->ApHostname;
if (pConfig->ApPassword != "")
{
wifiJson["ApPassword"] = pConfig->ApPassword;
}
}
if (pConfig->multiEntries.size() != 0)
{
JsonArray& wifiMulti = wifiJson.createNestedArray("WifiMulti");
for (auto entry : pConfig->multiEntries)
{
JsonObject& multiJson = wifiMulti.createNestedObject();
multiJson["ssid"] = entry.ssid;
multiJson["password"] = entry.password;
}
}
if (pConfig->mdnsHostname != "")
{
wifiJson["mdnsHostname"] = pConfig->mdnsHostname;
}
wifiJson["timeZone"] = timeZone_ / 3600;
wifiJson.prettyPrintTo(Serial);
Serial.println("");
// so now we should write this out to a file
// then rename the original to name.bak
// then rename our file to name
// create the file or truncate it
File configFile = SPIFFS.open(tmpConfigFilename, "w");
if (!configFile) {
// if we can't open it then return false
DBG_OUTPUT_PORT.print("Write Config file ");
DBG_OUTPUT_PORT.print(tmpConfigFilename);
DBG_OUTPUT_PORT.println(" open failed");
return false;
}
// write to the file
wifiJson.printTo(configFile);
configFile.close();
// now I want to move the old file out of the way
SPIFFS.rename(configFilename, configBackFilename);
SPIFFS.rename(tmpConfigFilename, configFilename);
DBG_OUTPUT_PORT.println("Write Config file OK");
return true;
}
const char* updateIndex =
"<form method='POST' action='/update' enctype='multipart/form-data'><input type='file'"
"name='update'><input type='submit' value='Update'></form>";
void FSBsetup(void) {
DBG_OUTPUT_PORT.print("\n");
DBG_OUTPUT_PORT.setDebugOutput(true);
//SPIFFS.begin();
{
Dir dir = SPIFFS.openDir("/");
DBG_OUTPUT_PORT.printf("about to read dir\n");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(),
formatBytes(fileSize).c_str());
}
DBG_OUTPUT_PORT.printf("\n");
}
if (readWifiConfig(&wifiConf) != true) {
// I am not sure what I want to do here, I expect that going into AP mode
// and letting the system be managed by the webpage at 192.168.4.1 is ok.
DBG_OUTPUT_PORT.println("AP mode.");
WiFi.mode(WIFI_AP);
setupAp();
}
else
{
// look at what wifi mode we want
if (wifiConf.ApHostname != "")
{
if (wifiConf.multiEntries.size() != 0)
{
// we are doing both
DBG_OUTPUT_PORT.println("AP_STA mode.");
WiFi.mode(WIFI_AP_STA);
}
else
{
// seems we only want AP mode
DBG_OUTPUT_PORT.println("AP mode.");
WiFi.mode(WIFI_AP);
}
}
else if (wifiConf.multiEntries.size() != 0) {
// seems we only want STA mode
DBG_OUTPUT_PORT.println("STA mode.");
WiFi.mode(WIFI_STA);
}
if (wifiConf.multiEntries.size() != 0) {
uint32_t waitTime;
// process the multiEntries
if (wifiConf.ApHostname != "")
{
// We also want an AP
setupAp();
}
for( auto entry : wifiConf.multiEntries ) {
// add each entry into the WiFi.multi
wifiMulti.addAP(entry.ssid.c_str(), entry.password.c_str());
}
DBG_OUTPUT_PORT.println("Connecting Wifi...");
waitTime = millis() + (45 * 1000);
while (wifiMulti.run() != WL_CONNECTED) {
delay(500);
DBG_OUTPUT_PORT.print(".");
// how much time is enough 45 seconds
if (millis() >= waitTime)
break;
}
if (WiFi.status() == WL_CONNECTED)
{
DBG_OUTPUT_PORT.print("Connected to SSID ");
DBG_OUTPUT_PORT.println(WiFi.SSID());
DBG_OUTPUT_PORT.print("IP address: ");
DBG_OUTPUT_PORT.println(WiFi.localIP());
String mdnsName;
if (wifiConf.mdnsHostname == "")
{
mdnsName = "pyroSphere";
}
else
{
mdnsName = wifiConf.mdnsHostname;
}
uint8_t mac[6];
WiFi.macAddress(mac);
addMac(mdnsName, mac);
if (MDNS.begin(mdnsName.c_str())) {
MDNS.addService("http", "tcp", 80);
}
DBG_OUTPUT_PORT.print("Open http://");
DBG_OUTPUT_PORT.print(mdnsName);
DBG_OUTPUT_PORT.println(".local/edit to see the file browser");
}
else
{
// try to clean up, we did not connect.
WiFi.scanDelete();
WiFi.disconnect();
}
}
else {
// we have no SSIDs to connect to, What to do? I know setup in Ap mode
DBG_OUTPUT_PORT.println("No SSIDs where in the config, starting as an AP");
setupAp();
}
}
// either way we should either have an AP or an STA that can do web stuff
//SERVER INIT
//list directory
server.on("/list", HTTP_GET, handleFileList);
//load editor
server.on("/edit", HTTP_GET,
[]() {
if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
});
//create file
server.on("/edit", HTTP_PUT, handleFileCreate);
//delete file
server.on("/edit", HTTP_DELETE, handleFileDelete);
//first callback is called after the request has ended with all parsed arguments
//second callback handles file uploads at that location
server.on("/edit", HTTP_POST, []() {server.send(200, "text/plain", "");},
handleFileUpload);
server.on("/ssid", HTTP_GET, handleSsidGet);
//called when the url is not defined here
//use it to load content from SPIFFS
server.onNotFound([]() {
if(!handleFileRead(server.uri()))
server.send(404, "text/plain", "FileNotFound");
});
server.on("/restart", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", "OK");
ESP.restart();
});
// setup to get program updates as well.
server.on("/up", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/html", updateIndex);
});
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
ESP.restart();
}, []() {
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
Update.printError(Serial);
}
} else if(upload.status == UPLOAD_FILE_WRITE) {
if(Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if(upload.status == UPLOAD_FILE_END) {
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);
}
yield();
});
server.begin();
DBG_OUTPUT_PORT.println("HTTP server started");
}
void FSBloop(void) {
server.handleClient();
}