-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEspSerialBridge.ino
596 lines (488 loc) · 16.3 KB
/
EspSerialBridge.ino
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
#include <Arduino.h>
#define _DEBUG
#ifdef _DEBUG
// #define _DEBUG_TRAFFIC
// #define _DEBUG_HEAP
// #define _DEBUG_WIFI_SETTINGS // enable WiFi.setAutoConnect and .printDiag on debug console
// #define _DEBUG_ESP // enable ESP.reset on debug console
#endif
//#define _DEBUG_HTTP
// wifi options
#define _ESP_WIFI_UDP_MULTICAST_DISABLED
#define _ESPSERIALBRIDGE_SUPPORT
#ifdef _ESPSERIALBRIDGE_SUPPORT
// #define _OTA_ATMEGA328_SERIAL // enable atmega328 OTA
// #define _TARGET_ESP_01 // no gpio15 for serial use
#endif
#ifdef _OTA_ATMEGA328_SERIAL
#include "IntelHexFormatParser.h"
#include "FlashATMega328Serial.h"
IntelHexFormatParser *intelHexFormatParser = NULL;
#endif
#include "EspConfig.h"
#include "EspDebug.h"
#include "EspSerialBridgeImpl.h"
#include "EspWifi.h"
#include "HelperHTML.h"
#define PROGNAME "EspSerialBridge"
#define PROGVERS "0.3"
#define PROGBUILD String(__DATE__) + " " + String(__TIME__)
bool httpRequestProcessed = false;
bool optionsChanged = false;
EspConfig espConfig(PROGNAME);
EspWiFi espWiFi;
EspSerialBridge espSerialBridge;
EspDebug espDebug;
// prototypes
// **********************************************
// * class EspSerialBridgeRequestHandler
// **********************************************
class EspSerialBridgeRequestHandler : public EspWiFiRequestHandler {
public:
bool canHandle(HTTPMethod method, String uri) override;
bool canUpload(String uri);
#ifdef ESP8266
bool handle(ESP8266WebServer& server, HTTPMethod method, String uri) override;
void upload(ESP8266WebServer& server, String uri, HTTPUpload& upload);
#endif
#ifdef ESP32
bool handle(WebServer& server, HTTPMethod method, String uri) override;
void upload(WebServer& server, String uri, HTTPUpload& upload);
#endif
protected:
#ifdef ESP8266
bool canHandle(ESP8266WebServer& server) override;
#endif
#ifdef ESP32
bool canHandle(WebServer& server) override;
#endif
String menuHtml() override;
uint8_t menuIdentifiers() override;
String menuIdentifiers(uint8_t identifier) override;
String menuIdentifierSerial() { return "serial"; };
String menuIdentifierOtaAddon() { return "ota-addon"; };
String getDevicesUri() { return "/devices"; };
String getOtaAtMegaUri() { return "/ota/atmega328.bin"; };
String handleDeviceList();
#ifdef ESP8266
String handleDeviceConfig(ESP8266WebServer& server, uint16_t *resultCode);
#endif
#ifdef ESP32
String handleDeviceConfig(WebServer& server, uint16_t *resultCode);
#endif
#ifdef _OTA_ATMEGA328_SERIAL
void clearParser();
#ifdef ESP8266
void httpHandleOTAatmega328(ESP8266WebServer& server);
void httpHandleOTAatmega328Data(ESP8266WebServer& server);
#endif
#ifdef ESP32
void httpHandleOTAatmega328(WebServer& server);
void httpHandleOTAatmega328Data(WebServer& server);
#endif
String otaFileName;
File otaFile;
bool initOtaFile(String filename, String mode);
void clearOtaFile();
#endif // _OTA_ATMEGA328_SERIAL
} espSerialBridgeRequestHandler;
void setup() {
setupEspTools();
espConfig.setup();
espWiFi.setup();
espWiFi.registerExternalRequestHandler(&espSerialBridgeRequestHandler);
espSerialBridge.begin();
espDebug.begin();
espDebug.registerInputCallback(handleInputStream);
}
void loop(void) {
// handle wifi
espWiFi.loop();
// tools
loopEspTools();
espSerialBridge.loop();
// send debug data
espDebug.loop();
}
// required EspWifi
String getDictionary() {
return "";
}
// other
void printHeapFree() {
#ifdef _DEBUG_HEAP
DBG_PRINTLN((String)F("heap: ") + (String)(ESP.getFreeHeap()));
#endif
}
void handleInput(char r, bool hasValue, unsigned long value, bool hasValue2, unsigned long value2) {
switch (r) {
#ifdef _DEBUG_WIFI_SETTINGS
case 'a':
WiFi.setAutoConnect(true);
break;
case 'd':
WiFi.printDiag(espDebug);
break;
#endif
#ifdef _DEBUG_ESP
case 'R':
ESP.reset();
break;
#endif
case 'u':
DBG_PRINTLN("uptime: " + uptime());
printHeapFree();
break;
case 'v':
DBG_PRINTF("[%s.%s] compiled at %s\n", String(PROGNAME).c_str(), String(PROGVERS).c_str(), String(PROGBUILD).c_str());
break;
case ' ':
case '\n':
case '\r':
break;
default:
break;
}
}
void handleInputStream(Stream *input) {
if (input->available() <= 0)
return;
static long value, value2;
bool hasValue, hasValue2;
char r = input->read();
// reset variables
value = 0; hasValue = false;
value2 = 0; hasValue2 = false;
byte sign = 0;
// char is a number
if ((r >= '0' && r <= '9') || r == '-'){
byte delays = 2;
while ((r >= '0' && r <= '9') || r == ',' || r == '-') {
if (r == '-') {
sign = 1;
} else {
// check value separator
if (r == ',') {
if (!hasValue || hasValue2) {
print_warning(2, "format");
return;
}
hasValue2 = true;
if (sign == 0) {
value = value * -1;
sign = 0;
}
} else {
if (!hasValue || !hasValue2) {
value = value * 10 + (r - '0');
hasValue = true;
} else {
value2 = value2 * 10 + (r - '0');
hasValue2 = true;
}
}
}
// wait a little bit for more input
while (input->available() <= 0 && delays > 0) {
delay(20);
delays--;
}
// more input available
if (delays == 0 && input->available() <= 0) {
return;
}
r = input->read();
}
}
// Vorzeichen
if (sign == 1) {
if (hasValue && !hasValue2)
value = value * -1;
if (hasValue && hasValue2)
value2 = value2 * -1;
}
handleInput(r, hasValue, value, hasValue2, value2);
}
// helper
void print_config() {
String blank = F(" ");
DBG_PRINT(F("config:"));
DBG_PRINTLN();
}
void print_warning(byte type, String msg) {
return;
DBG_PRINT(F("\nwarning: "));
if (type == 1)
DBG_PRINT(F("skipped incomplete command "));
if (type == 2)
DBG_PRINT(F("wrong parameter "));
if (type == 3)
DBG_PRINT(F("failed: "));
DBG_PRINTLN(msg);
}
// **********************************************
// * class EspSerialBridgeRequestHandler
// **********************************************
bool EspSerialBridgeRequestHandler::canHandle(HTTPMethod method, String uri) {
if (method == HTTP_POST && canUpload(uri))
return true;
return false;
}
bool EspSerialBridgeRequestHandler::canUpload(String uri) {
return uri == getOtaAtMegaUri();
}
#ifdef ESP8266
bool EspSerialBridgeRequestHandler::handle(ESP8266WebServer& server, HTTPMethod method, String uri) {
#endif
#ifdef ESP32
bool EspSerialBridgeRequestHandler::handle(WebServer& server, HTTPMethod method, String uri) {
#endif
if (method == HTTP_POST && uri == getConfigUri() && server.hasArg(menuIdentifierSerial())) {
uint16_t resultCode;
String html = handleDeviceConfig(server, &resultCode);
if (html != "") {
server.client().setNoDelay(true);
server.send(resultCode, "text/plain", html);
return (httpRequestProcessed = true);
}
}
#ifdef _OTA_ATMEGA328_SERIAL
if (method == HTTP_POST && uri == getConfigUri() && server.hasArg(menuIdentifierOtaAddon())) {
String action = getOtaAtMegaUri();
String html = F("<h4>OTA-AddOn</h4>");
html += htmlInput("file", "file", "", 0) + htmlNewLine();
server.client().setNoDelay(true);
server.send(200, "text/plain", htmlForm(html, action, "post", "submitForm", "multipart/form-data"));
return (httpRequestProcessed = true);
}
if (method == HTTP_POST && uri == getOtaAtMegaUri()) {
httpHandleOTAatmega328(server);
return httpRequestProcessed;
}
#endif // _OTA_ATMEGA328_SERIAL
return false;
}
#ifdef ESP8266
void EspSerialBridgeRequestHandler::upload(ESP8266WebServer& server, String uri, HTTPUpload& upload) {
#endif
#ifdef ESP32
void EspSerialBridgeRequestHandler::upload(WebServer& server, String uri, HTTPUpload& upload) {
#endif
#ifdef _OTA_ATMEGA328_SERIAL
httpHandleOTAatmega328Data(server);
#endif // _OTA_ATMEGA328_SERIAL
}
#ifdef ESP8266
bool EspSerialBridgeRequestHandler::canHandle(ESP8266WebServer& server) {
#endif
#ifdef ESP32
bool EspSerialBridgeRequestHandler::canHandle(WebServer& server) {
#endif
if (canHandle(server.method(), server.uri()))
return true;
if (server.method() == HTTP_POST && server.uri() == getConfigUri()) {
if (server.hasArg(menuIdentifierSerial()) && (server.arg(menuIdentifierSerial()) == "" || server.arg(menuIdentifierSerial()) == "config"))
return true;
if (server.hasArg(menuIdentifierOtaAddon()) && server.arg(menuIdentifierOtaAddon()) == "")
return true;
}
return false;
}
String EspSerialBridgeRequestHandler::menuHtml() {
return htmlMenuItem(menuIdentifierSerial(), "Serial");
}
uint8_t EspSerialBridgeRequestHandler::menuIdentifiers() {
return 2;
}
String EspSerialBridgeRequestHandler::menuIdentifiers(uint8_t identifier) {
switch(identifier) {
case 0: return menuIdentifierSerial();break;
case 1: return menuIdentifierOtaAddon();break;
}
return "";
}
#ifdef ESP8266
String EspSerialBridgeRequestHandler::handleDeviceConfig(ESP8266WebServer& server, uint16_t *resultCode) {
#endif
#ifdef ESP32
String EspSerialBridgeRequestHandler::handleDeviceConfig(WebServer& server, uint16_t *resultCode) {
#endif
String result = "";
String reqAction = server.arg(F("action"));
if (reqAction != F("form") && reqAction != F("submit"))
return result;
if (reqAction == F("form")) {
String action = F("/config?ChipID=");
action += getChipID();
action += F("&serial=config");
action += F("&action=submit");
String html = "", options = "";
uint32_t baud = espSerialBridge.getBaud();
html += htmlLabel(F("baud"), F("Baud: "));
options = htmlOption(F("9600"), F("9600"), baud == 9600);
options += htmlOption(F("19200"), F("19200"), baud == 19200);
options += htmlOption(F("38400"), F("38400"), baud == 38400);
options += htmlOption(F("57600"), F("57600"), baud == 57600);
options += htmlOption(F("74880"), F("74880"), baud == 74880);
options += htmlOption(F("115200"), F("115200"), baud == 115200);
html += htmlSelect(F("baud"), options, "") + htmlNewLine();
action += F("&baud=");
SerialConfig curr = espSerialBridge.getSerialConfig();
html += htmlLabel(F("data"), F("Data: "));
options = htmlOption(String(UART_NB_BIT_8), F("8"), (curr & UART_NB_BIT_MASK) == UART_NB_BIT_8);
options += htmlOption(String(UART_NB_BIT_7), F("7"), (curr & UART_NB_BIT_MASK) == UART_NB_BIT_7);
options += htmlOption(String(UART_NB_BIT_6), F("6"), (curr & UART_NB_BIT_MASK) == UART_NB_BIT_6);
options += htmlOption(String(UART_NB_BIT_5), F("5"), (curr & UART_NB_BIT_MASK) == UART_NB_BIT_5);
html += htmlSelect(String(F("data")), options, "") + htmlNewLine();
action += F("&data=");
html += htmlLabel(F("parity"), F("Parity: "));
options = htmlOption(String(UART_PARITY_NONE), F("None"), (curr & UART_PARITY_MASK) == UART_PARITY_NONE);
options += htmlOption(String(UART_PARITY_EVEN), F("Even"), (curr & UART_PARITY_MASK) == UART_PARITY_EVEN);
options += htmlOption(String(UART_PARITY_ODD), F("Odd"), (curr & UART_PARITY_MASK) == UART_PARITY_ODD);
html += htmlSelect(String(F("parity")), options, "") + htmlNewLine();
action += F("&parity=");
html += htmlLabel(F("stop"), F("Stop: "));
options = htmlOption(String(UART_NB_STOP_BIT_1), F("1"), (curr & UART_NB_STOP_BIT_MASK) == UART_NB_STOP_BIT_1);
options += htmlOption(String(UART_NB_STOP_BIT_2), F("2"), (curr & UART_NB_STOP_BIT_MASK) == UART_NB_STOP_BIT_2);
html += htmlSelect(String(F("stop")), options, "") + htmlNewLine();
action += F("&stop=");
uint8_t tx_pin = espSerialBridge.getTxPin();
html += htmlLabel("pins", F("TX/RX: "));
options = htmlOption(F("normal"), F("normal (1/3)"), tx_pin == 1);
#ifndef _TARGET_ESP_01
options += htmlOption(F("swapped"), F("swapped (15/13)"), tx_pin == 15);
#endif
html += htmlSelect(String(F("pins")), options, "") + htmlNewLine();
action += F("&pins=");
#ifdef _OTA_ATMEGA328_SERIAL
html = htmlFieldSet(html, htmlMenuItem(menuIdentifierOtaAddon(), "OTA"));
#else
html = htmlFieldSet(html, F("Settings"));
#endif
if (html != "") {
*resultCode = 200;
html = "<h4>Serial</h4>" + html;
result = htmlForm(html, action, F("post"), F("configForm"), "", "");
}
}
if (reqAction == F("submit")) {
EspDeviceConfig deviceConfig = espSerialBridge.getDeviceConfig();
deviceConfig.setValue("baud", server.arg("baud"));
deviceConfig.setValue("tx", String(server.arg("pins") == "normal" ? 1 : 15));
uint8_t dps = 0;
dps |= (server.arg("data").toInt() & UART_NB_BIT_MASK);
dps |= (server.arg("parity").toInt() & UART_PARITY_MASK);
dps |= (server.arg("stop").toInt() & UART_NB_STOP_BIT_MASK);
deviceConfig.setValue("dps", String(dps));
if (deviceConfig.hasChanged()) {
deviceConfig.saveToFile();
espSerialBridge.readDeviceConfig();
}
*resultCode = 200;
result = F("ok");
}
return result;
}
#ifdef _OTA_ATMEGA328_SERIAL
#ifdef ESP8266
void EspSerialBridgeRequestHandler::httpHandleOTAatmega328(ESP8266WebServer& server) {
#endif
#ifdef ESP32
void EspSerialBridgeRequestHandler::httpHandleOTAatmega328(WebServer& server) {
#endif
String message = "\n\nhttpHandleOTAatmega328: ";
bool doUpdate = false;
if (SPIFFS.exists(otaFileName) && initOtaFile(otaFileName, "r")) {
message += otaFile.name();
message += + " (";
message += otaFile.size();
message += " Bytes) received!";
doUpdate = true;
} else
message += "file doesn't exists (maybe wrong IntelHEX format parsed!)";
DBG_PRINTLN(message);
if (doUpdate) {
DBG_PRINT("starting Update: ");
DBG_FORCE_OUTPUT();
uint8_t txPin = 1;
#ifdef _ESPSERIALBRIDGE_SUPPORT
espSerialBridge.enableClientConnect(false);
txPin = espSerialBridge.getTxPin();
#endif
FlashATmega328 flashATmega328(2, txPin);
flashATmega328.flashFile(&otaFile);
#ifdef _ESPSERIALBRIDGE_SUPPORT
espSerialBridge.enableClientConnect();
#endif
clearOtaFile();
}
server.client().setNoDelay(true);
server.sendHeader("Location", "/");
server.send(303, "text/plain", "See Other");
httpRequestProcessed = true;
}
#ifdef ESP8266
void EspSerialBridgeRequestHandler::httpHandleOTAatmega328Data(ESP8266WebServer& server) {
#endif
#ifdef ESP32
void EspSerialBridgeRequestHandler::httpHandleOTAatmega328Data(WebServer& server) {
#endif
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
DBG_PRINT("httpHandleOTAatmega328Data: " + upload.filename);
DBG_FORCE_OUTPUT();
} else if (upload.status == UPLOAD_FILE_WRITE) {
// first block with data
if (upload.totalSize == 0) {
initOtaFile("/ota/atmega328.bin", "w");
intelHexFormatParser = new IntelHexFormatParser(&otaFile);
}
if (intelHexFormatParser == NULL)
return;
DBG_PRINT(".");
if ((upload.totalSize % HTTP_UPLOAD_BUFLEN) == 20)
DBG_PRINTLN("\n");
if (!intelHexFormatParser->parse(upload.buf, upload.currentSize)) {
DBG_PRINTLN("\nwriting file " + otaFileName + " failed!");
DBG_FORCE_OUTPUT();
clearParser();
clearOtaFile();
}
} else if (upload.status == UPLOAD_FILE_END) {
if (otaFile) {
bool uploadComplete = (otaFile.size() == intelHexFormatParser->sizeBinaryData() && intelHexFormatParser->isEOF());
DBG_PRINTF("\nend: %s (%d Bytes)\n", otaFile.name(), otaFile.size());
DBG_FORCE_OUTPUT();
otaFile.close();
clearParser();
if (!uploadComplete)
clearOtaFile();
}
} else if (upload.status == UPLOAD_FILE_ABORTED) {
DBG_PRINTF("\naborted\n");
DBG_FORCE_OUTPUT();
clearParser();
clearOtaFile();
}
}
bool EspSerialBridgeRequestHandler::initOtaFile(String filename, String mode) {
SPIFFS.begin();
otaFile = SPIFFS.open(filename, mode.c_str());
if (otaFile)
otaFileName = filename;
return otaFile;
}
void EspSerialBridgeRequestHandler::clearOtaFile() {
if (otaFile)
otaFile.close();
if (SPIFFS.exists(otaFileName))
SPIFFS.remove(otaFileName);
otaFileName = "";
}
void EspSerialBridgeRequestHandler::clearParser() {
if (intelHexFormatParser != NULL) {
free(intelHexFormatParser);
intelHexFormatParser = NULL;
}
}
#endif // _OTA_ATMEGA328_SERIAL