forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESPUpdateServer.cpp
More file actions
435 lines (381 loc) · 15.7 KB
/
Copy pathESPUpdateServer.cpp
File metadata and controls
435 lines (381 loc) · 15.7 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
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
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WebServer.h>
#include <FS.h>
#if UseLittleFS
#include <LittleFS.h>
#else
#include <SPIFFS.h>
#endif
#include "ESP32HTTPUpdateServer.h"
#include "EepromAccess.h"
#include "EepromManager.h"
#endif
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include "Config.h"
#include "ExternalData.h"
#include "BPLSettings.h"
#define EXTERNALDATA_ON_SYNC_SERVER false
#if SerialDebug == true
#define DBG_PRINT(...) DebugPort.print(__VA_ARGS__)
#define DBG_PRINTLN(...) DebugPort.println(__VA_ARGS__)
#else
#define DBG_PRINT(...)
#define DBG_PRINTLN(...)
#endif
#if (DEVELOPMENT_OTA == true) || (DEVELOPMENT_FILEMANAGER == true)
#if defined(ESP32)
static WebServer server(UPDATE_SERVER_PORT);
#else
static ESP8266WebServer server(UPDATE_SERVER_PORT);
#endif
#endif
#if DEVELOPMENT_OTA == true
#if ESP8266
static ESP8266HTTPUpdateServer httpUpdater;
#elif ESP32
static ESP32HTTPUpdateServer httpUpdater;
#endif
#endif
#if DEVELOPMENT_FILEMANAGER == true
#include "edit_html_gz.h"
#define SPIFFS_FORMAT_PATH "/format-spiffs"
#define SPIFFS_FORMATTING_PATH "/exeformat-spiffs"
static const char PROGMEM spiffsformat_html[] = { 0x1f, 0x8b, 0x08, 0x08, 0x17, 0xcf, 0x09, 0x5a, 0x02, 0x03, 0x66, 0x6f,
0x72, 0x6d, 0x61, 0x74, 0x66, 0x73, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x00,
0x55, 0x53, 0xcb, 0x6e, 0xdb, 0x30, 0x10, 0xfc, 0x95, 0x2d, 0x2f, 0x22,
0x11, 0x45, 0xb6, 0xfb, 0x48, 0x1b, 0xe8, 0x71, 0x08, 0x90, 0x00, 0xb9,
0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x27, 0xcf, 0x73, 0x12, 0x28,
0xa0, 0xf4, 0xb7, 0x9d, 0x8f, 0x60, 0x95, 0x64, 0x65, 0x5f, 0xfc, 0xe4,
0xd2, 0x86, 0x0c, 0xdc, 0xac, 0x61, 0x40, 0xd1, 0xe9, 0x6a, 0x98, 0xc3,
0xe5, 0x90, 0x7f, 0xef, 0x19, 0xb6, 0xf6, 0xcf, 0x02, 0xc3, 0x71, 0xb0,
0xb8, 0x4f, 0xb2, 0x50, 0x82, 0x4e, 0x0f, 0x63, 0xb9, 0x97, 0x36, 0x27,
0x67, 0xbb, 0xdf, 0x6d, 0xa1, 0x8c, 0x00, 0x9f, 0x9d, 0xca, 0x09, 0xbe,
0xe1, 0x82, 0x73, 0x3d, 0x18, 0x59, 0xd7, 0x03, 0x29, 0x32, 0xa9, 0xcd,
0x68, 0xc1, 0x1e, 0x0d, 0xe6, 0x64, 0x76, 0xa9, 0xec, 0xde, 0x08, 0xc8,
0x2a, 0x27, 0xa1, 0x99, 0x14, 0xf0, 0x08, 0xc1, 0x14, 0xe0, 0x5e, 0x6b,
0x60, 0x1d, 0x80, 0xeb, 0x0a, 0x2a, 0x6e, 0xf9, 0x45, 0xff, 0xae, 0xd3,
0x38, 0x6b, 0xff, 0x0f, 0x6c, 0x91, 0x43, 0xc0, 0x8f, 0xfd, 0xe8, 0xc3,
0xb3, 0x0f, 0xde, 0xaa, 0xa0, 0xc0, 0x2f, 0xb3, 0x57, 0xde, 0xb8, 0xf0,
0x17, 0xfd, 0x01, 0xb3, 0x36, 0xe9, 0x11, 0x4c, 0x03, 0x00, 0x00 };
static const char PROGMEM spiffsformating_html[] = {
0x1f, 0x8b, 0x08, 0x08, 0xd7, 0xce, 0x09, 0x5a, 0x02, 0x03, 0x66, 0x6f,
0x72, 0x6d, 0x61, 0x74, 0x66, 0x73, 0x5f, 0x65, 0x78, 0x65, 0x2e, 0x68,
0x74, 0x6d, 0x6c, 0x00, 0x4d, 0x53, 0x6b, 0x6f, 0xda, 0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2, 0x28, 0x73,
0xf0, 0x2b, 0x03, 0x00, 0x00
};
//holds the current upload
static File fsUploadFile;
extern String getContentType(String filename);
String getResponseContentType(String filename){
if(server.hasArg("download")) return "application/octet-stream";
return getContentType(filename);
}
static bool handleFileRead(String path){
DBG_PRINTLN("handleFileRead: " + path);
if(path.endsWith("/")) path += "index.htm";
String contentType = getResponseContentType(path);
String pathWithGz = path + ".gz";
if(FileSystem.exists(pathWithGz) || FileSystem.exists(path)){
if(FileSystem.exists(pathWithGz))
path += ".gz";
File file = FileSystem.open(path, "r");
/*size_t sent = */ server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
static void handleFileUpload(void){
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_PRINT("handleFileUpload Name: "); DBG_PRINTLN(filename);
fsUploadFile = FileSystem.open(filename, "w");
filename = String();
} else if(upload.status == UPLOAD_FILE_WRITE){
//DBG_PRINT("handleFileUpload Data: "); DBG_PRINTLN(upload.currentSize);
if(fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
} else if(upload.status == UPLOAD_FILE_END){
if(fsUploadFile)
fsUploadFile.close();
DBG_PRINT("handleFileUpload Size: "); DBG_PRINTLN(upload.totalSize);
}
}
static void handleFileDelete(void){
if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
String path = server.arg(0);
DBG_PRINTLN("handleFileDelete: " + path);
if(path == "/")
return server.send(500, "text/plain", "BAD PATH");
if(!FileSystem.exists(path))
return server.send(404, "text/plain", "FileNotFound");
FileSystem.remove(path);
server.send(200, "text/plain", "");
path = String();
}
static void handleFileCreate(void){
if(server.args() == 0)
return server.send(500, "text/plain", "BAD ARGS");
String path = server.arg(0);
DBG_PRINTLN("handleFileCreate: " + path);
if(path == "/")
return server.send(500, "text/plain", "BAD PATH");
if(FileSystem.exists(path))
return server.send(500, "text/plain", "FILE EXISTS");
File file = FileSystem.open(path, "w");
if(file)
file.close();
else
return server.send(500, "text/plain", "CREATE FAILED");
server.send(200, "text/plain", "");
path = String();
}
static void handleFileList(void) {
if(!server.hasArg("dir")) {server.send(500, "text/plain", "BAD ARGS"); return;}
String path = server.arg("dir");
DBG_PRINTLN("handleFileList: " + path);
// linked list(queue) is needed.
// avoid recursive call, which might open too many directories
#if ESP32
File dir = FileSystem.open(path);
// DBG_PRINTF("length: %d",exlen);
#else
Dir dir = FileSystem.openDir(path);
#endif
//path = String();
String output = "[";
#if ESP32
File entry = dir.openNextFile();
while(entry){
#else
while(dir.next()){
File entry = dir.openFile("r");
#endif
if (output != "[") output += ',';
#if UseLittleFS
bool isDir = entry.isDirectory();
#else
bool isDir = false;
#endif
DBG_PRINTF(" %s [%s]\n ", entry.name(),isDir? "DIR":"FILE");
output += "{\"type\":\"";
output += (isDir)? "dir":"file";
output += "\",\"name\":\"";
//DBG_PRINTF("%s, %s, cmp:%d, ", entry.name(),path.c_str(), strncmp(entry.name(),path.c_str(),path.length()));
String name=(entry.name());
if(name.startsWith(path)){
int exlen =path.length();
if(path.charAt(exlen - 1) != '/') exlen ++;
output += name.substring(exlen);
}else{
if(entry.name()[0] == '/'){
output += name.substring(1);
}else{
output += name;
}
}
#if 0
#if UseLittleFS
#if ESP32
// the name is so complicated.
//
output += String(entry.name()).substring(exlen);
#else
output += entry.name();
#endif
#else
output += String(entry.name()).substring(1);
#endif
#endif
output += "\"}";
entry.close();
#if defined(ESP32)
entry = dir.openNextFile();
#endif
}
output += "]";
DBG_PRINTF(" ret: %s \n",output.c_str());
server.send(200, "text/json", output);
}
#endif
#if (DEVELOPMENT_OTA == true) || (DEVELOPMENT_FILEMANAGER == true)
void ESPUpdateServer_setup(const char* user, const char* pass){
#if DEVELOPMENT_FILEMANAGER == true
//SERVER INIT
//list directory
server.on("/list", HTTP_GET, handleFileList);
//load editor
server.on(FILE_MANAGEMENT_PATH, HTTP_GET, [](){
server.sendHeader("Content-Encoding", "gzip");
server.send_P(200,"text/html",edit_htm_gz,edit_htm_gz_len);
});
//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);
//called when the url is not defined here
//use it to load content from FileSystem
server.onNotFound([](){
if(!handleFileRead(server.uri()))
server.send(404, "text/plain", "FileNotFound");
});
//get heap status, analog input value and all GPIO statuses in one json call
#if 0
server.on("/all", HTTP_GET, [](){
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
json += ", \"analog\":"+String(analogRead(A0));
json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "}";
server.send(200, "text/json", json);
json = String();
});
#endif
server.on(SPIFFS_FORMAT_PATH,HTTP_GET, [](){
server.sendHeader("Content-Encoding", "gzip");
server.send_P(200,"text/html",spiffsformat_html,sizeof(spiffsformat_html));
});
server.on(SPIFFS_FORMATTING_PATH,HTTP_GET, [](){
server.sendHeader("Content-Encoding", "gzip");
server.send_P(200,"text/html",spiffsformating_html,sizeof(spiffsformating_html));
theSettings.preFormat();
#if ESP32
FileSystem.format();
#else
FileSystem.format();
#endif
theSettings.postFormat();
#if ESP32
eepromAccess.saveDeviceDefinition();
eepromManager.storeTempConstantsAndSettings();
#endif
});
#endif
#if EXTERNALDATA_ON_SYNC_SERVER
server.on("/gravity",HTTP_POST, [](){
if (server.hasArg("plain")== false){ //Check if body received
server.send(200, "text/plain", "");
return;
}
uint8_t error;
String json=server.arg("plain");
char *data=(char*)malloc(json.length() +1);
if(!data){
server.send(500);
return;
}
strcpy(data,json.c_str());
if(externalData.processGravityReport(data,json.length(),false,error)){
server.send(200,"application/json","{}");
}else{
server.send(500);
}
});
#endif
#if DEVELOPMENT_OTA == true
// Flash update server
#if ESP8266
httpUpdater.setup(&server,SYSTEM_UPDATE_PATH,user,pass);
#elif ESP32
httpUpdater.setup(server,SYSTEM_UPDATE_PATH,user,pass);
#endif
#endif
server.begin();
DBG_PRINTLN("HTTP Update server started\n");
}
void ESPUpdateServer_loop(void){
server.handleClient();
}
#endif