-
Notifications
You must be signed in to change notification settings - Fork 4
/
nmea_bridge.ino
1498 lines (1291 loc) · 44.7 KB
/
nmea_bridge.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
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Arduino.h>
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WebSocketsServer.h>
#include <DNSServer.h>
extern "C" {
#include "user_interface.h"
uint16 readvdd33(void);
}
// global macros utils
#define DEBUG
#define DEBUG_SERIAL Serial
#ifdef DEBUG
#define DEBUG_PRINT(x) DEBUG_SERIAL.print(x)
#define DEBUG_PRINTLN(x) DEBUG_SERIAL.println(x)
#else
#define DEBUG_PRINT(x) {}
#define DEBUG_PRINTLN(x) {}
#endif
#define min(x, y) ((x < y) ? x : y)
// hardware layout
#define CONFIG_EEPROM_ADDRESS 100
#define WIFI_SIGNAL_POWER 20.5 // min: 0 - max: 20.5 dBm
#define BOOT_MODE_BUTTON_PIN 0
#define INDICATOR_LED_PIN 2
#define INDICATOR_LED_ON 0
#define INDICATOR_LED_OFF 1
#define RESET_CONFIG_THRESHOLD 5000 // in milliseconds
// configuration
#define MAX_WIFI_SSID_SIZE 33
#define MAX_WIFI_PASSWORD_SIZE 33
#define MAX_MDNS_HOSTNAME_SIZE 33
#define ENABLE_WEBSOCKET_LOG
enum WifiMode: uint8_t {
station = 1,
access_point = 2,
};
enum TransmitMode: uint8_t {
broadcast = 1,
multicast = 2,
unicast = 3,
};
#define BAUDRATE_OPTION_COUNT 4
uint32_t baudrate_options[BAUDRATE_OPTION_COUNT + 1] = {
0, // not an option, used to distinguish from undefined
4800,
9600,
38400,
115200
};
/*
** Serial on the ESP8266
**
** The ESP has two hardware serial ports.
** Serial: uart0
** TX: gpio1 gpio15
** RX: gpio3 gpio13
** By default, the first set of pins is used.
** Calling Serial.swap() changes these to the other set.
**
** Serial1: uart1
** TX: gpio2
** This one can only transmit, not receive.
**
**
** Source:
** https://github.com/esp8266/Arduino/blob/master/doc/reference.rst#serial
*/
// The serial device on which this device receives NMEA sentences.
#define SERIAL_RX Serial
// The serial device on which this device transmits NMEA sentences.
#define SERIAL_TX Serial1
// Set to 1 if the RX and TX serial ports are the same one.
#define SERIAL_SAME 0 // TODO automatic
struct Configuration {
WifiMode wifi_mode;
char wifi_ssid[MAX_WIFI_SSID_SIZE];
char wifi_password[MAX_WIFI_PASSWORD_SIZE];
IPAddress static_ip_address;
char mdns_hostname[MAX_MDNS_HOSTNAME_SIZE];
TransmitMode tx_mode;
IPAddress tx_address;
uint16_t tx_port;
uint16_t rx_port;
uint8_t tx_baudrate;
uint8_t rx_baudrate;
} config;
const char* display_wifi_mode(WifiMode mode) {
if (mode == WifiMode::station) {
return "Station";
} else {
return "Access Point";
}
}
const char* display_tx_mode(TransmitMode mode) {
if (mode == TransmitMode::broadcast) {
return "Broadcast";
} else if (mode == TransmitMode::multicast) {
return "Multicast";
} else {
return "Unicast";
}
}
struct Configuration default_config = {
// wifi_mode
WifiMode::access_point,
// wifi_ssid
"NMEA Bridge",
// wifi_password
"nmeabridgeesp8266",
// static_ip_address
INADDR_ANY,
// mdns_hostname
"nmea_bridge",
// tx_mode
TransmitMode::broadcast,
// tx_address
INADDR_ANY,
// tx_port
10110,
// rx_port
10110,
// tx_baudrate (4800)
1,
// rx_baudrate (4800)
1,
};
// global values
#define HTTP_PORT 80
#define WEBSOCKET_PORT 81
#define DNS_PORT 53
#define WIFI_ACCESS_POINT_CHANNEL 6
#define WIFI_ACCESS_POINT_MAX_CONNECTIONS 4 // max 8
#define WIFI_ACCESS_POINT_DEVICE_IP IPAddress(10, 1, 1, 1)
#define WIFI_ACCESS_POINT_GATEWAY_IP IPAddress(10, 1, 1, 1)
#define WIFI_ACCESS_POINT_SUBNET_IP IPAddress(255, 255, 255, 0)
bool inConfigMode = false;
uint8_t indicator_led_state;
WifiMode effective_wifi_mode;
char effective_wifi_ssid[MAX_WIFI_SSID_SIZE];
IPAddress effective_tx_address;
uint16_t nmea_sentences_received = 0;
uint16_t nmea_sentences_sent = 0;
bool is_network_config_changed = false;
#define UDP_SENTENCE_BUFFER_SIZE 64
#define HTTP_RESPONSE_BUFFER_SIZE 4000
char http_response_buffer[HTTP_RESPONSE_BUFFER_SIZE + 1];
uint16_t http_response_buffer_filled = 0;
#define HTTP_RESPONSE_BUFFER_REMAINING \
(HTTP_RESPONSE_BUFFER_SIZE - http_response_buffer_filled)
/* The buffer should be long enough for about two sentences
** so we can handle the case where a (single) newline is lost.
*/
#define NMEA_SENTENCE_BUFFER_SIZE 100
char nmea_sentence_buffer[NMEA_SENTENCE_BUFFER_SIZE + 1];
uint8_t nmea_sentence_buffer_filled = 0; // number of chars in buffer
ESP8266WebServer http_server(HTTP_PORT);
WiFiUDP udp_server;
DNSServer dns_server;
#ifdef ENABLE_WEBSOCKET_LOG
WebSocketsServer websocket_server(WEBSOCKET_PORT);
#endif
// cache static files "forever" (about a year)
#define STATIC_FILE_CACHE_TIME 31536000 // in seconds
#define JS_CONTENT_TYPE "application/javascript"
#define CSS_CONTENT_TYPE "text/css"
/* Create a request handler for static files.
*/
#define STATIC_FILE_REQUEST_HANDLER(content_type, content) \
{ \
if (captive_portal()) {return;} \
http_server.sendHeader( \
"Cache-Control", \
String("max-age=") + STATIC_FILE_CACHE_TIME, \
true); \
http_server.send(200, content_type, content); \
}
// forward declarations
void send_info_page_response();
void send_config_form_response();
void nmea_log_page_response();
void send_css_style_response();
void send_404_not_found_response();
void update_mdns_hostname();
void update_tx_config();
void update_rx_config();
void update_tx_baudrate();
void update_rx_baudrate();
void set_indicator_led_on();
void set_indicator_led_off();
void force_indicator_led_on();
void force_indicator_led_off();
void toggle_indicator_led();
void long_indicator_blink();
void update_indicator_slow_blink();
void update_indicator_double_blink();
bool blink_while_pressing_mode_button();
void blink_while_connecting_wifi();
String get_uptime_display();
IPAddress get_device_ip_address();
IPAddress gateway_address(IPAddress addr);
IPAddress subnet_address(IPAddress addr);
void load_config_from_eeprom(Configuration &new_config);
void store_config_to_eeprom(Configuration new_config);
void fix_config();
#include "html_fragments.cpp" // TODO inherit WEBSOCKET_PORT
#include "css_fragments.cpp"
#include "js_fragments.cpp"
extern const String html_start;
extern const String html_end;
extern const String html_log_content;
extern const String css_style;
extern const String js_log_script;
/**********************
** MAIN EVENT LOOP **
**********************/
void setup() {
EEPROM.begin(512);
load_config_from_eeprom(config);
fix_config();
nmea_sentence_buffer_filled = 0;
SERIAL_RX.begin(baudrate_options[config.rx_baudrate]);
#if !SERIAL_SAME
SERIAL_TX.begin(baudrate_options[config.tx_baudrate]);
#endif
#ifdef DEBUG
DEBUG_SERIAL.setDebugOutput(true);
#endif
// wait for serial to connect
delay(10);
// give the user a chance to press the boot-mode button after reset
delay(1000);
pinMode(INDICATOR_LED_PIN, OUTPUT);
force_indicator_led_off();
effective_wifi_mode = config.wifi_mode;
pinMode(BOOT_MODE_BUTTON_PIN, INPUT_PULLUP);
if (digitalRead(BOOT_MODE_BUTTON_PIN) == LOW) {
bool exceeded_threshold = blink_while_pressing_mode_button();
if (exceeded_threshold) {
DEBUG_PRINTLN("Resetting configuration to default");
long_indicator_blink();
config = default_config;
store_config_to_eeprom(config);
// change state to match new config
update_tx_baudrate();
update_rx_baudrate();
effective_wifi_mode = config.wifi_mode;
} else {
DEBUG_PRINTLN("Forcing access point mode");
effective_wifi_mode = WifiMode::access_point;
inConfigMode = true;
}
}
// WiFi.hostname(config.mdns_hostname);
#ifdef WIFI_SIGNAL_POWER
WiFi.setOutputPower(WIFI_SIGNAL_POWER);
#endif
strlcpy(effective_wifi_ssid, config.wifi_ssid, MAX_WIFI_SSID_SIZE);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
if (effective_wifi_mode == WifiMode::access_point) {
DEBUG_PRINT("starting new wifi network: ");
DEBUG_PRINTLN(effective_wifi_ssid);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(
(config.static_ip_address.isSet() ?
config.static_ip_address : WIFI_ACCESS_POINT_DEVICE_IP),
WIFI_ACCESS_POINT_GATEWAY_IP,
WIFI_ACCESS_POINT_SUBNET_IP);
WiFi.softAP(effective_wifi_ssid, config.wifi_password, WIFI_ACCESS_POINT_CHANNEL, WIFI_ACCESS_POINT_MAX_CONNECTIONS);
// wait for the ip address to be available
delay(500);
// setup dns for captive portal
dns_server.setErrorReplyCode(DNSReplyCode::NoError);
dns_server.start(DNS_PORT, "*", WiFi.softAPIP());
} else {
DEBUG_PRINT("connecting to existing wifi network: ");
DEBUG_PRINTLN(effective_wifi_ssid);
WiFi.mode(WIFI_STA);
if (config.static_ip_address.isSet()) {
WiFi.config(config.static_ip_address, gateway_address(config.static_ip_address), subnet_address(config.static_ip_address));
} else {
// use DHCP, which is the default
}
WiFi.begin(effective_wifi_ssid, config.wifi_password);
blink_while_connecting_wifi();
}
DEBUG_PRINTLN("done setting up wifi");
// start udp server
udp_server.begin(config.rx_port);
// start mDNS server
if (config.mdns_hostname[0] != '\0') {
// TODO mDNS doesnt work in station mode
DEBUG_PRINT("starting MDNS: http://");
DEBUG_PRINT(config.mdns_hostname);
DEBUG_PRINTLN(".local/");
MDNS.begin(config.mdns_hostname);
}
// start web server
DEBUG_PRINTLN("starting http server");
http_response_buffer[HTTP_RESPONSE_BUFFER_SIZE] = '\0';
http_server.on("/", send_info_page_response);
http_server.on("/config", send_config_form_response);
#ifdef ENABLE_WEBSOCKET_LOG
http_server.on("/log", nmea_log_page_response);
http_server.on("/log_script.js", send_js_log_script);
#endif
http_server.on("/style.css", send_css_style_response);
http_server.onNotFound(send_404_not_found_response);
http_server.begin();
#ifdef ENABLE_WEBSOCKET_LOG
DEBUG_PRINTLN("starting websocket server");
websocket_server.begin();
websocket_server.onEvent(handle_websocket_event);
#endif
// determine transmit address
update_tx_config();
DEBUG_PRINT("transmit: udp://");
DEBUG_PRINT(effective_tx_address.toString());
DEBUG_PRINT(":");
DEBUG_PRINT(config.tx_port);
DEBUG_PRINT("/ (");
DEBUG_PRINT(display_tx_mode(config.tx_mode));
DEBUG_PRINTLN(")");
DEBUG_PRINT("receive: udp://");
DEBUG_PRINT(get_device_ip_address().toString());
DEBUG_PRINT(":");
DEBUG_PRINT(config.rx_port);
DEBUG_PRINTLN("/");
}
void loop() {
handle_serial_event();
yield();
handle_udp_event();
yield();
http_server.handleClient();
yield();
#ifdef ENABLE_WEBSOCKET_LOG
websocket_server.loop();
yield();
#endif
MDNS.update();
yield();
dns_server.processNextRequest();
yield();
// use indicator led to show connection status
if (effective_wifi_mode == WifiMode::access_point) {
update_indicator_double_blink();
} else {
if (!WiFi.isConnected()) {
update_indicator_slow_blink();
} else {
set_indicator_led_on();
}
}
return;
}
/*********************
** EVENT HANDLERS **
*********************/
void handle_serial_event() {
// on incoming data over the hardware serial port
int serial_character_int;
char serial_character;
while (1) {
serial_character_int = SERIAL_RX.read();
if (serial_character_int < 0) {
// no more data available
return;
}
serial_character = (char)serial_character_int;
// TODO sometimes this gives a large number of zero bytes
// TODO then we also don't get the extra newline
nmea_sentence_buffer[nmea_sentence_buffer_filled] = serial_character;
nmea_sentence_buffer_filled ++;
if (serial_character == '\n') {
} else if (nmea_sentence_buffer_filled >= NMEA_SENTENCE_BUFFER_SIZE) {
DEBUG_PRINTLN("nmea sentence buffer overflow");
// A newline must have been lost since at least two sentences
// should fit in the buffer, so send all buffered data immedately
// to let the client decide how to handle this.
nmea_sentence_buffer[NMEA_SENTENCE_BUFFER_SIZE] = '\n';
nmea_sentence_buffer_filled = NMEA_SENTENCE_BUFFER_SIZE + 1;
} else {
continue;
}
handle_outgoing_sentence(
nmea_sentence_buffer, nmea_sentence_buffer_filled);
nmea_sentence_buffer_filled = 0;
}
}
void handle_udp_event() {
// on incoming data over the network udp connection
int packet_size = udp_server.parsePacket();
char buffer[UDP_SENTENCE_BUFFER_SIZE + 2];
if (packet_size) {
while (1) {
size_t length = udp_server.read(buffer, UDP_SENTENCE_BUFFER_SIZE);
if (length <= 0) {
return;
}
if (buffer[length - 1] != '\n') {
// Add newline terminator if not not yet present.
buffer[length] = '\n';
length ++;
}
handle_incoming_sentence(buffer, length, "udp");
}
}
}
#ifdef ENABLE_WEBSOCKET_LOG
void handle_websocket_event(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
// on incoming data over the network websocket connection
// TODO handle text fragments, although nmea sentences are very short
// https://github.com/Links2004/arduinoWebSockets/blob/master/examples/esp8266/WebSocketServerFragmentation/WebSocketServerFragmentation.ino
if (type == WStype_TEXT) {
if ((char)payload[length - 1] != '\n') {
// Add newline terminator if not not yet present.
String sentence = (char *)payload; // TODO not pretty
sentence += '\n';
payload = (uint8_t*)sentence.c_str();
length ++;
}
handle_incoming_sentence((char *)payload, length, "ws");
}
}
#endif
void handle_incoming_sentence(char *sentence, size_t length, char *source) {
/* data is sent into the nmea device
** sentence: always includes the newline and must be newline terminted
*/
transmit_incoming_over_serial(sentence, length);
#ifdef ENABLE_WEBSOCKET_LOG
transmit_incoming_over_websocket(sentence, length, source);
#endif
nmea_sentences_sent ++;
}
void handle_outgoing_sentence(char *sentence, size_t length) {
/* data is received from the nmea device
** sentence: always includes the newline and must be newline terminted
*/
transmit_outgoing_over_udp(sentence, length);
#ifdef ENABLE_WEBSOCKET_LOG
transmit_outgoing_over_websocket(sentence, length);
#endif
nmea_sentences_received ++;
}
void transmit_incoming_over_serial(char *sentence, size_t length) {
SERIAL_TX.write(sentence, length);
}
void transmit_outgoing_over_udp(char *sentence, size_t length) {
if (config.tx_mode == TransmitMode::multicast) {
int result = udp_server.beginPacketMulticast(
effective_tx_address, config.tx_port,
get_device_ip_address());
} else {
int result = udp_server.beginPacket(
effective_tx_address, config.tx_port);
}
udp_server.write(sentence, length);
udp_server.endPacket();
}
#ifdef ENABLE_WEBSOCKET_LOG
void transmit_incoming_over_websocket(char *sentence, size_t length, char *source) {
send_websocket_message(sentence, length, source);
}
void transmit_outgoing_over_websocket(char *sentence, size_t length) {
send_websocket_message(sentence, length, "serial");
}
bool JSONEscapeRequired(const char c) {
return (c == '"' || c == '\\' || ('\x00' <= c && c <= '\x1f'));
}
void send_websocket_message(char *data, size_t length, char *source) {
write_to_http_response_buffer("{\"source\":\"");
write_to_http_response_buffer(source);
write_to_http_response_buffer("\",\"sentence\":\"");
// escape data
for (size_t n = 0; n < length; n ++) {
char c = data[n];
if (JSONEscapeRequired(c)) {
write_format_to_http_response_buffer("\\u%04x", int(c));
} else {
write_to_http_response_buffer(c);
}
}
write_to_http_response_buffer("\"}");
websocket_server.broadcastTXT(
http_response_buffer, http_response_buffer_filled);
clear_http_response_buffer();
}
#endif
/******************************
** HTTP RESPONSE CALLBACKS **
******************************/
bool isThisHost(String host) {
String ip = get_device_ip_address().toString();
String name = String(config.mdns_hostname) + ".local";
return ((host == ip)
|| (host == name)
|| (host == ip + String(":") + String(HTTP_PORT))
|| (host == name + String(":") + String(HTTP_PORT)));
}
bool captive_portal() {
if (!isThisHost(http_server.hostHeader())) {
http_server.sendHeader(
"Location",
String("http://") + get_device_ip_address().toString(),
true);
http_server.send(302, "text/plain", "");
return true;
}
return false;
}
void clear_http_response_buffer() {
http_response_buffer_filled = 0;
}
void write_to_http_response_buffer(const char *data, uint16_t length) {
char *buffer_start = &http_response_buffer[http_response_buffer_filled];
uint16_t remaining = HTTP_RESPONSE_BUFFER_REMAINING;
if (length > remaining) {
DEBUG_PRINTLN("response buffer overflow\n");
length = remaining;
}
memcpy(buffer_start, data, length);
http_response_buffer_filled += length;
}
void write_to_http_response_buffer(const char *data) {
char *buffer_start = &http_response_buffer[http_response_buffer_filled];
uint16_t remaining = HTTP_RESPONSE_BUFFER_REMAINING;
// Add one for the zero byte at the end.
remaining += 1;
size_t result = strlcpy(buffer_start, data, remaining);
http_response_buffer_filled += min(result, remaining - 1);;
if (result >= remaining) {
DEBUG_PRINTLN("response buffer overflow");
}
}
void write_to_http_response_buffer(char c) {
if (HTTP_RESPONSE_BUFFER_REMAINING >= 1) {
http_response_buffer[http_response_buffer_filled] = c;
http_response_buffer_filled ++;
}
}
void write_to_http_response_buffer(String str) {
write_to_http_response_buffer(str.c_str(), str.length());
}
void write_format_to_http_response_buffer(char *format, ...) {
char *buffer_start = &http_response_buffer[http_response_buffer_filled];
uint16_t remaining = HTTP_RESPONSE_BUFFER_REMAINING;
// Add one for the zero byte at the end.
remaining += 1;
va_list args;
va_start(args, format);
size_t result = vsnprintf(buffer_start, remaining, format, args);
va_end(args);
http_response_buffer_filled += min(result, remaining - 1);;
if (result >= HTTP_RESPONSE_BUFFER_REMAINING + 1) {
DEBUG_PRINTLN("response buffer overflow\n");
}
}
void send_http_response_buffer() {
http_response_buffer[http_response_buffer_filled] = '\0';
String html = http_response_buffer; // TODO avoid the copy?
http_server.send(200, "text/html", html);
clear_http_response_buffer();
}
const String html_link(String url, String text) {
return String("<a href=\"") + url + String("\">") + text + String("</a>");
}
void write_info_html_section_start(const char *title) {
write_to_http_response_buffer("<h2>");
write_to_http_response_buffer(title);
write_to_http_response_buffer("</h2><table>");
}
void write_info_html_field(const char *name, String value) {
write_to_http_response_buffer("<tr><th>");
write_to_http_response_buffer(name);
write_to_http_response_buffer("</th><td>");
write_to_http_response_buffer(value);
write_to_http_response_buffer("</td></tr>");
}
void write_info_html_section_end() {
write_to_http_response_buffer("</table>");
}
void send_info_page_response() {
/* Request handler for the info page.
*/
if (captive_portal()) {return;}
String device_ip = get_device_ip_address().toString();
write_to_http_response_buffer(html_start);
write_info_html_section_start("Network");
if (is_network_config_changed) {
write_to_http_response_buffer(
"<p class=\"note\">"
"The network configuration has been updated "
"since the device started. "
"Changes will be applied after restart."
"</p>");
}
write_info_html_field(
"Wifi mode",
display_wifi_mode(effective_wifi_mode));
write_info_html_field(
"Wifi SSID",
effective_wifi_ssid);
write_info_html_field(
"IP Address",
device_ip);
write_info_html_field(
"Hostname",
config.mdns_hostname[0] == '\0' ?
"undefined" :
html_link(
String("http://") + config.mdns_hostname + String(".local/"),
config.mdns_hostname));
write_info_html_field(
"MAC Address",
WiFi.macAddress());
if (effective_wifi_mode == WifiMode::access_point) {
write_info_html_field(
"Connected Stations",
String(WiFi.softAPgetStationNum()));
}
write_info_html_section_end();
write_info_html_section_start("Connection");
write_info_html_field(
"Transmit Mode",
display_tx_mode(config.tx_mode));
String tx_addr = (
String("udp://") + effective_tx_address.toString()
+ String(":") + String(config.tx_port));
write_info_html_field(
"Transmit",
html_link(tx_addr, tx_addr));
String rx_addr = (
String("udp://") + device_ip + String(":") + String(config.rx_port));
write_info_html_field(
"Receive",
html_link(rx_addr, rx_addr));
write_info_html_section_end();
write_info_html_section_start("Device");
#if SERIAL_SAME
write_info_html_field(
"Baudrate",
String(baudrate_options[config.rx_baudrate]));
#else
write_info_html_field(
"RX Baudrate",
String(baudrate_options[config.rx_baudrate]));
write_info_html_field(
"TX Baudrate",
String(baudrate_options[config.tx_baudrate]));
#endif
write_info_html_field(
"RX Counter",
String(nmea_sentences_received));
write_info_html_field(
"TX Counter",
String(nmea_sentences_sent));
write_info_html_field(
"Voltage",
String(readvdd33() / 1000.0, 2) + String("V"));
write_info_html_field(
"Uptime",
get_uptime_display());
write_info_html_field(
"Device ID",
String(system_get_chip_id()));
write_info_html_section_end();
write_to_http_response_buffer(html_end);
send_http_response_buffer();
}
void write_form_html_field_start(const char *title, const char *tag) {
write_to_http_response_buffer("<");
write_to_http_response_buffer(tag);
write_to_http_response_buffer(" class=\"field\"><div>");
write_to_http_response_buffer(title);
write_to_http_response_buffer("</div>");
}
void write_form_html_help(const char *help) {
if (help[0] != '\0') {
write_to_http_response_buffer("<p class=\"help\">");
write_to_http_response_buffer(help);
write_to_http_response_buffer("</p>");
}
}
void write_form_html_heading(String title) {
write_to_http_response_buffer("<h2>");
write_to_http_response_buffer(title);
write_to_http_response_buffer("</h2>");
}
void write_form_html_field(const char *type, const char *name,
String value, const char *title,
const char *help, const char *attrs) {
write_form_html_field_start(title, "label");
write_form_html_help(help);
write_to_http_response_buffer("<input type=\"");
write_to_http_response_buffer(type);
write_to_http_response_buffer("\" name=\"");
write_to_http_response_buffer(name);
write_to_http_response_buffer("\" value=\"");
write_to_http_response_buffer(value);
write_to_http_response_buffer("\"");
write_to_http_response_buffer(attrs);
write_to_http_response_buffer("/></label>");
}
void write_form_html_options_start(const char *title, const char *help) {
write_form_html_field_start(title, "div");
write_form_html_help(help);
write_to_http_response_buffer("<div class=\"optgroup\">");
}
void write_form_html_options_item(const char *name, const char *value,
const char *title, const char *attrs,
bool checked) {
write_to_http_response_buffer("<label class=\"option\">");
write_to_http_response_buffer("<input name=\"");
write_to_http_response_buffer(name);
write_to_http_response_buffer("\" value=\"");
write_to_http_response_buffer(value);
write_to_http_response_buffer("\" type=\"radio\"");
if (checked) {
write_to_http_response_buffer(" checked");
}
write_to_http_response_buffer(attrs);
write_to_http_response_buffer("/><p>");
write_to_http_response_buffer(title);
write_to_http_response_buffer("</p></label>");
}
void write_form_html_options_end() {
write_to_http_response_buffer("</div></div>");
}
void write_form_html_select_start(const char *name, const char *title,
const char *help, const char *attrs) {
write_form_html_field_start(title, "label");
write_form_html_help(help);
write_to_http_response_buffer("<select name=\"");
write_to_http_response_buffer(name);
write_to_http_response_buffer("\"");
write_to_http_response_buffer(attrs);
write_to_http_response_buffer(">");
}
void write_form_html_select_item(String value, String title, bool selected) {
write_to_http_response_buffer("<option value=\"");
write_to_http_response_buffer(value);
write_to_http_response_buffer("\"");
if (selected) {
write_to_http_response_buffer(" selected");
}
write_to_http_response_buffer(">");
write_to_http_response_buffer(title);
write_to_http_response_buffer("</option>");
}
void write_form_html_select_end() {
write_to_http_response_buffer("</select></label>");
}
String ip_address_form_value(IPAddress addr) {
if (addr.isSet()) {
return addr.toString();
} else {
return "";
}
}
void write_form_html_baudrate_items(uint8_t selected_baudrate) {
for (int n = 1; n <= BAUDRATE_OPTION_COUNT; n ++) {
write_form_html_select_item(
String(n),
String(baudrate_options[n]),
selected_baudrate == n);
}
}
void send_config_form_response() {
/* Request handler for the config form page.
*/
if (captive_portal()) {return;}
if (http_server.method() == HTTP_POST) {
send_config_form_post_response();
return;
}
write_to_http_response_buffer(html_start);
write_to_http_response_buffer(
"<form method=\"post\"><h1>Configure</h1>");
write_form_html_heading("Network");
write_form_html_options_start("Wifi Mode", "");
write_form_html_options_item(
"1", "1", "Connect to existing access point", " required",
config.wifi_mode == WifiMode::station);
write_form_html_options_item(
"1", "2", "Create a new wifi access point", " required",
config.wifi_mode == WifiMode::access_point);
write_form_html_options_end();
write_form_html_field("text", "2", config.wifi_ssid, "Wifi SSID", "", " required");
write_form_html_field("text", "3", config.wifi_password, "Wifi password", "", " required");
write_form_html_field("text", "4", ip_address_form_value(config.static_ip_address), "IP Address", "Leave empty to get an address by DHCP.", "");
write_form_html_field("text", "5", config.mdns_hostname, "mDNS Hostname", "Leave empty to skip setting up mDNS.", "");
write_form_html_heading("Connection");
write_form_html_options_start("Transmit Mode", "");
write_form_html_options_item(
"6", "1", "Unicast", " required",
config.tx_mode == TransmitMode::unicast);
write_form_html_options_item(
"6", "2", "Multicast", " required",
config.tx_mode == TransmitMode::multicast);
write_form_html_options_item(
"6", "3", "Broadcast", " required",
config.tx_mode == TransmitMode::broadcast);
write_form_html_options_end();
write_form_html_field("text", "7", ip_address_form_value(config.tx_address), "UDP Transmit Address", "In broadcast mode, only the global broadcast address is used.<br>In unicast mode, this must be a valid unicast address.", "");
write_form_html_field("number", "8", String(config.tx_port), "UDP Transmit Port", "", " min=\"1\" max=\"65535\" required");
write_form_html_field("number", "9", String(config.rx_port), "UDP Receive Port", "", " min=\"1\" max=\"65535\" required");
write_form_html_heading("Device");
#if SERIAL_SAME
write_form_html_select_start("a", "Baudrate", "", " required");
write_form_html_baudrate_items(config.rx_baudrate);
write_form_html_select_end();
#else
write_form_html_select_start("a", "Receive Baudrate", "", " required");
write_form_html_baudrate_items(config.rx_baudrate);
write_form_html_select_end();
write_form_html_select_start("b", "Transmit Baudrate", "", " required");
write_form_html_baudrate_items(config.tx_baudrate);
write_form_html_select_end();
#endif
write_to_http_response_buffer(
"<button type=\"submit\">Save</button></form>");
write_to_http_response_buffer(html_end);
send_http_response_buffer();
}
void send_config_form_post_response() {
/* Request handler for the config form POST.
*/
Configuration new_config = config; // copy config
IPAddress addr;
uint16_t port;
WifiMode wifi_mode;