-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathRemoteDebug.cpp
2015 lines (1385 loc) · 42.1 KB
/
RemoteDebug.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
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
/*
* Libraries Arduino
* *****************
* Library : Remote debug - debug over telnet - for Esp8266 (NodeMCU) or ESP32
* Author : Joao Lopes
* Comments: Telnet server based on example of TelnetServer code in http: *www.rudiswiki.de/wiki9/WiFiTelnetServer
* Web socket server uses the arduinWebSockets library (https://github.com/Links2004/arduinoWebSockets)
* The author uses Eclise IDE (sloeber) to made this source
* License : See RemoteDebug.h
*
* Versions:
* ------ ---------- -----------------
* 3.0.5 2019-03-32 Ajustment on debugA macro, thanks @jetpax and @cmidgley to add this issue
* 3.0.4 2019-03-19 All public configurations (#defines) have moved to RemoteDebugCfg.h, to facilitate changes for anybody.
* Changed examples with warnings on change any #define in project,
* with workarounds, if it not work. (thanks to @22MarioZ for added this issue)
* 3.0.3 2019-03-18 Adjustments if web socket is disabled
* 3.0.2 2019-03-16 Adjustments in examples, added one for debugger
* 3.0.1 2019-03-13 Adjustments in silente mode
* Commands from RemoteDebugApp now is treated
* Adjusts to RemoteDebugger support connection by web sockets
* 3.0.0 2019-03-11 If not disabled, add a web socket server to comunicate with RemoteDebugApp (HTML5 web app)
* The standard telnet still working, to debug with internet offline
* Ajustment on debugA macro, thanks @jetpax to add this issue
* 2.1.2 2019-03-08 Add empty rprint* macros, if debug is disabled
* 2.1.1 2019-03-06 Create option DEBUG_DISABLE_AUTO_FUNC
* Create macros to be used for code converter: rprint and rprintln
* RemoteDebug now have an code converters to help migrate codes
*
* 2.1.0 2019-03-04 Create precompiler DEBUG_DISABLED to compile for production/release,
* equal that have in SerialDebug
* Adjustments in examples
*
* 2.0.2 2019-03-03 Just to do new release, to update other files
* 2.0.1 2019-03-01 Adjustments for the debugger: it still disable until dbg command, equal to SerialDebug
* The callback will to be called before print debug messages now
* And only if debugger is enabled in RemoteDebugger (command dbg)
* Changed handle debugger logic
*
* 2.0.0 2019-02-28 Added support to RemoteDebug addon library: the RemoteDebugger, an simple software debugger, based on SerialDebug
* New color system (uncomment COLOR_NEW_SYSTEM in remotedebug.h to return to old way)
* 1.5.9 2019-02-18 Bug> sometimes the processCommand is executed twice. Workaround> check time
* 1.5.8 2019-02-08 New macros to compatibility with SerialDebug (can use RemoteDebug or SerialDebug) thanks to @phrxmd
* 1.5.7 2018-11-03 Fixed bug for MAX_TIME_INACTIVE
* 1.5.6 2018-10-19 Adjustments based on pull request from @jeroenst (to allow serial output with telnet password and setPassword method)
* 1.5.5 ? Serial output is now not allowed if telnet password is enabled
* 1.5.4 ? Serial output not depending of telnet password (thanks @jeroenst for suggestion)
* 1.5.3 ? Serial output adjustments (due bug in password logic)
* 1.5.2 ? Correct rdebug macro (thanks @stritti)
* 1.5.1 ? New command: silence
* Added new rdebug?ln to put auto new line
* Auto function and core if (for ESP32) in rdebug macros
* Class destructor implemented
* 1.5.0 ? Port can be pass in begin method (thanks @PjotrekSE for suggestion)
* Few adjustments
* this kind of authentication will not be done now.
* Such as RemoteDebug now is not for production releases,
* Note: telnet use advanced authentication (kerberos, etc.)
* 1.4.0 ? A simple text password request, if enabled (thanks @jeroenst for suggestion)
* 1.3.1 ? Retired # from VARGS precompiler macros
* Few adjustments as ESP32 includes
* Port number can be modified in project Arduino (.ino file)
* 1.3.0 Aug 2018 Bug in write with latest ESP8266 SDK
* 1.2.1 ? Adjusts to not cause error in Arduino
* 1.2.0 ? Added shortcuts and buffering to avoid delays
* 1.1.1 2017-11-24 Added support for the pass through of commands, and default debug levels thanks B. Harville
* 1.1.0 Aug 2017 Support to ESP32
* New commands for CPU frequencies
* New level> profiler and auto-profiler
* 1.0.1 Aug 2017 New connection logic
* 1.0.0 Jan 2017 First RC
* 0.9.1 Oct 2016 Beta 2
* 0.9.0 Aug 2016 Beta 1
*
*/
/*
* TODO: - Page HTML for begin/stop Telnet server
* - Add support to another Arduino WiFi boards (if have demand on it)
*/
///// RemoteDebug configuration
#include "RemoteDebugCfg.h"
///// Debug disable for compile to production/release ?
///// as nothing of RemotedDebug is compiled, zero overhead :-)
#ifndef DEBUG_DISABLED
///// Defines
#define VERSION "3.0.5"
///// Includes
#include "stdint.h"
#if defined(ESP8266)
// ESP8266 SDK
extern "C" {
bool system_update_cpu_freq(uint8_t freq);
}
#endif
#include "Arduino.h"
#include "Print.h"
#ifdef SERIAL_DEBUG_H
// Cannot used with SerialDebug at same time
#error "RemoteDebug cannot be used with SerialDebug"
#endif
// ESP8266 or ESP32 ?
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#else
#error Only for ESP8266 or ESP32
#endif
#include "RemoteDebug.h" // This library
#ifdef ALPHA_VERSION // In test, not good yet
#include "telnet.h"
#endif
// Support to websocket connection with RemoteDebugApp
// Note: you must install the arduinoWebSocket library before
#ifndef WEBSOCKET_DISABLED // Only if Web socket enabled (RemoteDebugApp)
#include "RemoteDebugWS.h"
#endif
// Internal print macros for send messages to client
#ifndef WEBSOCKET_DISABLED // Only if Web socket enabled (RemoteDebugApp)
#define debugPrintf(fmt, ...) { \
if (_connected) TelnetClient.printf(fmt, ##__VA_ARGS__);\
else if (_connectedWS) DebugWS.printf(fmt, ##__VA_ARGS__);\
}
#define debugPrintln(str) { \
if (_connected) TelnetClient.println(str);\
else if (_connectedWS) DebugWS.println(str);\
}
#define debugPrint(str) { \
if (_connected) TelnetClient.print(str);\
else if (_connectedWS) DebugWS.print(str);\
}
#else // With web socket too
#define debugPrintf(fmt, ...) { \
if (_connected) TelnetClient.printf(fmt, ##__VA_ARGS__);\
}
#define debugPrintln(str) { \
if (_connected) TelnetClient.println(str);\
}
#define debugPrint(str) { \
if (_connected) TelnetClient.print(str);\
}
#endif
// Internal debug macro - recommended stay disable
#define D(fmt, ...) // Without this
//#define D(fmt, ...) Serial.printf("rd: " fmt "\n", ##__VA_ARGS__) // Serial debug
////// Variables
// Instance
static RemoteDebug* _instance;
// WiFi server (telnet)
static WiFiServer TelnetServer(TELNET_PORT); // @suppress("Abstract class cannot be instantiated")
static WiFiClient TelnetClient; // @suppress("Abstract class cannot be instantiated")
// Support to websocket connection with RemoteDebugApp
#ifndef WEBSOCKET_DISABLED
// Instance of RemoteDebugWS
static RemoteDebugWS DebugWS; // @suppress("Abstract class cannot be instantiated")
static boolean _connectedWS = false; // Connected :
// Callbacks
class MyRemoteDebugCallbacks: public RemoteDebugWSCallbacks {
void onConnect() {
// Web socket (app) connected
D("rd: onconnect");
_connectedWS = true;
// Is telnet connected -> disconnect it, due reduce overheads
if (_instance->isConnected()) {
_instance->disconnect(true);
}
// Call same routine that telnet
_instance->onConnection(true);
}
void onDisconnect() {
// Web socket (app) disconnected
D("rd: ondisconnect");
_connectedWS = false;
}
void onReceive(const char *message) {
// Receive a message
D("rd: onreceive");
_instance->wsOnReceive(message);
}
};
#endif // WEBSOCKET_DISABLED
////// Methods / routines
// Constructor
RemoteDebug::RemoteDebug() {
// Save the instance
_instance = this;
}
// Initialize the telnet server
bool RemoteDebug::begin(String hostName, uint8_t startingDebugLevel) {
return begin(hostName, TELNET_PORT, startingDebugLevel);
}
bool RemoteDebug::begin(String hostName, uint16_t port, uint8_t startingDebugLevel) {
// Initialize server telnet
if (port != TELNET_PORT) { // Bug: not more can use begin(port)..
return false;
}
TelnetServer.begin();
TelnetServer.setNoDelay(true);
#ifndef WEBSOCKET_DISABLED
// Initialize web socket (for RemoteDebugApp)
DebugWS.begin(new MyRemoteDebugCallbacks());
#endif
// Reserve space to buffer of print writes
_bufferPrint.reserve(BUFFER_PRINT);
#ifdef CLIENT_BUFFERING
// Reserve space to buffer of send
_bufferPrint.reserve(MAX_SIZE_SEND);
#endif
// Host name of this device
_hostName = hostName;
// Debug level
_clientDebugLevel = startingDebugLevel;
_lastDebugLevel = startingDebugLevel;
return true;
}
#ifdef DEBUGGER_ENABLED
// Simple software debugger - based on SerialDebug Library
void RemoteDebug::initDebugger(boolean (*callbackEnabled)(), void (*callbackHandle)(const boolean), String (*callbackGetHelp)(), void (*callbackProcessCmd)()) {
// Init callbacks for the debugger
_callbackDbgEnabled = callbackEnabled;
_callbackDbgHandle = callbackHandle;
_callbackDbgHelp = callbackGetHelp;
_callbackDbgProcessCmd = callbackProcessCmd;
}
WiFiClient* RemoteDebug::getTelnetClient() {
return &TelnetClient;
}
#endif
// Set the password for telnet - thanks @jeroenst for suggest thist method
void RemoteDebug::setPassword(String password) {
_password = password;
}
// Destructor
RemoteDebug::~RemoteDebug() {
// Flush
if (TelnetClient && TelnetClient.connected()) {
TelnetClient.flush();
}
// Stop
stop();
}
// Stop the server
void RemoteDebug::stop() {
// Stop Client
if (TelnetClient && TelnetClient.connected()) {
TelnetClient.stop();
}
// Stop server
TelnetServer.stop();
#ifndef WEBSOCKET_DISABLED
// Stop web socket (RemoteDebugApp)
DebugWS.stop(); // stop the websocket server
#endif
}
// Handle the connection (in begin of loop in sketch)
// TODO: optimize when loop not have a large delay
void RemoteDebug::handle() {
#ifdef ALPHA_VERSION // In test, not good yet
static uint32_t lastTime = millis();
#endif
#ifdef DEBUGGER_ENABLED
static uint32_t dbgTimeHandle = millis(); // To avoid call the handler desnecessary
static boolean dbgLastConnected = false; // Last is connected ?
#endif
// Silence timeout ?
if (_silence && _silenceTimeout > 0 && millis() >= _silenceTimeout) {
// Get out of silence mode
silence(false, true);
}
// Debug level is profiler -> set the level before
if (_clientDebugLevel == PROFILER) {
if (millis() > _levelProfilerDisable) {
_clientDebugLevel = _levelBeforeProfiler;
debugPrintln("* Debug level profile inactive now");
}
}
#ifdef ALPHA_VERSION // In test, not good yet
// Automatic change to profiler level if time between handles is greater than n millis
if (_autoLevelProfiler > 0 && _clientDebugLevel != PROFILER) {
uint32_t diff = (millis() - lastTime);
if (diff >= _autoLevelProfiler) {
_levelBeforeProfiler = _clientDebugLevel;
_clientDebugLevel = PROFILER;
_levelProfilerDisable = 1000; // Disable it at 1 sec
debugPrintf("* Debug level profile active now - time between handels: %u\r\n", diff);
}
lastTime = millis();
}
#endif
// look for Client connect trial
if (TelnetServer.hasClient()) {
// Old connection logic
// if (!TelnetClient || !TelnetClient.connected()) {
//
// if (TelnetClient) { // Close the last connect - only one supported
//
// TelnetClient.stop();
//
// }
// New connection logic - 10/08/17
if (TelnetClient && TelnetClient.connected()) {
// Verify if the IP is same than actual conection
WiFiClient newClient; // @suppress("Abstract class cannot be instantiated")
newClient = TelnetServer.available();
String ip = newClient.remoteIP().toString();
if (ip == TelnetClient.remoteIP().toString()) {
// Reconnect
TelnetClient.stop();
TelnetClient = newClient;
} else {
// Disconnect (not allow more than one connection)
newClient.stop();
return;
}
} else {
// New TCP client
TelnetClient = TelnetServer.available();
// Password request ? - 18/07/18
if (_password != "") {
#ifdef ALPHA_VERSION // In test, not good yet
// Send command to telnet client to not do local echos
// Experimental code !
sendTelnetCommand(TELNET_WONT, TELNET_ECHO);
#endif
}
}
if (!TelnetClient) { // No client yet ???
return;
}
// Set client
TelnetClient.setNoDelay(true); // More faster
TelnetClient.flush(); // clear input buffer, else you get strange characters
// Empty buffer
delay(100);
while (TelnetClient.available()) {
TelnetClient.read();
}
// Connection event
onConnection(true);
}
// Is client connected ? (to reduce overhead in active)
_connected = (TelnetClient && TelnetClient.connected());
// Get command over telnet
if (_connected) {
char last = ' '; // To avoid process two times the "\r\n"
while (TelnetClient.available()) { // get data from Client
// Get character
char character = TelnetClient.read();
// Newline (CR or LF) - once one time if (\r\n) - 26/07/17
if (isCRLF(character) == true) {
if (isCRLF(last) == false) {
// Process the command
if (_command.length() > 0) {
_lastCommand = _command; // Store the last command
processCommand();
}
}
_command = ""; // Init it for next command
} else if (isPrintable(character)) {
// Concat
_command.concat(character);
}
// Last char
last = character;
}
}
// Client connected ?
#ifndef WEBSOCKET_DISABLED // For web socket server (app)
boolean connected = (_connected || _connectedWS);
#else // By telnet
boolean connected = _connected;
#endif
if (connected) {
#ifdef CLIENT_BUFFERING
// Client buffering - send data in intervals to avoid delays or if its is too big
if ((millis() - _lastTimeSend) >= DELAY_TO_SEND || _sizeBufferSend >= MAX_SIZE_SEND) {
debugPrint(_bufferSend);
_bufferSend = "";
_sizeBufferSend = 0;
_lastTimeSend = millis();
}
#endif
#ifdef MAX_TIME_INACTIVE
#if MAX_TIME_INACTIVE > 0
// Inactivity - close connection if not received commands from user in telnet
// For reduce overheads
uint32_t maxTime = MAX_TIME_INACTIVE; // Normal
if (_password != "" && !_passwordOk) { // Request password - 18/08/08
maxTime = 60000; // One minute to password
}
if ((millis() - _lastTimeCommand) > maxTime) {
debugPrintln("* Closing session by inactivity");
// Disconnect
disconnect();
return;
}
#endif
#endif
}
#ifndef WEBSOCKET_DISABLED // For websocket server
// Web socket server handle
DebugWS.handle();
#endif
#ifdef DEBUGGER_ENABLED
// For Simple software debugger - based on SerialDebug Library
// Changed handle debugger logic - 2018-03-01
if (_callbackDbgEnabled && _callbackDbgHandle) { // Calbacks ok ?
boolean callHandle = false;
if (dbgLastConnected != connected) { // Change connection -> always call
dbgLastConnected = connected;
callHandle = true;
} else if (millis() >= dbgTimeHandle) {
if (_callbackDbgEnabled()) { // Only if it is enabled
callHandle = true;
}
}
if (callHandle) {
// Call the handle
_callbackDbgHandle(true);
// Save time
dbgTimeHandle = millis() + DEBUGGER_HANDLE_TIME;
}
}
#endif
//DV("*handle time: ", (millis() - timeBegin));
}
// Disconnect client
void RemoteDebug::disconnect(boolean onlyTelnetClient) {
// Disconnect
if (onlyTelnetClient) {
if (_connected) {
TelnetClient.println("* Closing client connection ..."); // this is to web app new conn not receive it
}
} else {
debugPrintln("* Closing client connection ...");
}
_silence = false;
_silenceTimeout = 0;
if (_connected) { // By telnet
TelnetClient.stop();
_connected = false;
}
#ifndef WEBSOCKET_DISABLED // For web socket server (app)
if (_connectedWS && !onlyTelnetClient) {
DebugWS.disconnect(); // Disconnect client
_connectedWS = false;
}
#endif
}
// Connection/disconnection event
void RemoteDebug::onConnection(boolean connected) {
// Clear variables
D("rd onconn %d", connected);
_bufferPrint = ""; // Clean buffer
_lastTimeCommand = millis(); // To mark time for inactivity
_command = ""; // Clear command
_lastCommand = ""; // Clear las command
_lastTimePrint = millis(); // Clear the time
_silence = false; // No silence
_silenceTimeout = 0;
#ifdef CLIENT_BUFFERING
// Client buffering - send data in intervals to avoid delays or if its is too big
_bufferSend = "";
_sizeBufferSend = 0;
_lastTimeSend = millis();
#endif
// Password request ? - 18/07/18
if (_password != "") {
_passwordOk = false;
#ifdef REMOTEDEBUG_PWD_ATTEMPTS
_passwordAttempt = 1;
#endif
}
// Save it
_connected = connected;
// Process
if (connected) { // Connected ?
// Callback
if (_callbackNewClient) {
_callbackNewClient();
}
// Show the initial message
#if SHOW_HELP
showHelp();
#endif
}
}
boolean RemoteDebug::isConnected() {
// Is connected
#ifndef WEBSOCKET_DISABLED // For web socket server (app)
return (_connected || _connectedWS);
#else
return _connected;
#endif
}
// Send to serial too (use only if need)
void RemoteDebug::setSerialEnabled(boolean enable) {
_serialEnabled = enable;
_showColors = false; // Disable it for Serial
}
// Allow ESP reset over telnet client
void RemoteDebug::setResetCmdEnabled(boolean enable) {
_resetCommandEnabled = enable;
}
// Show time in millis
void RemoteDebug::showTime(boolean show) {
_showTime = show;
}
// Show profiler - time in millis between messages of debug
void RemoteDebug::showProfiler(boolean show, uint32_t minTime) {
_showProfiler = show;
_minTimeShowProfiler = minTime;
}
#ifdef ALPHA_VERSION // In test, not good yet
// Automatic change to profiler level if time between handles is greater than n mills (0 - disable)
void RemoteDebug::autoProfilerLevel(uint32_t millisElapsed) {
_autoLevelProfiler = millisElapsed;
}
#endif
// Show debug level
void RemoteDebug::showDebugLevel(boolean show) {
_showDebugLevel = show;
}
// Show colors
void RemoteDebug::showColors(boolean show) {
if (_serialEnabled == false) {
_showColors = show;
} else {
_showColors = false; // Disable it for Serial
}
}
// Show in raw mode - only data ?
void RemoteDebug::showRaw(boolean show) {
_showRaw = show;
}
// Is active ? client telnet connected and level of debug equal or greater then set by user in telnet
boolean RemoteDebug::isActive(uint8_t debugLevel) {
// Active ->
// Not in silence (new)
// Debug level ok and
// Telnet connected or
// Serial enabled (use only if need)
// Password ok (if enabled) - 18/08/18
#ifndef WEBSOCKET_DISABLED // For web socket server (app)
boolean ret = (debugLevel >= _clientDebugLevel &&
!_silence &&
(_connected || _connectedWS || _serialEnabled));
#else // Telnet only
boolean ret = (debugLevel >= _clientDebugLevel &&
!_silence &&
(_connected || _serialEnabled));
#endif
if (ret) {
_lastDebugLevel = debugLevel;
}
return ret;
}
// Set help for commands over telnet set by sketch
void RemoteDebug::setHelpProjectsCmds(String help) {
_helpProjectCmds = help;
}
// Set callback of sketch function to process project messages
void RemoteDebug::setCallBackProjectCmds(void (*callback)()) {
_callbackProjectCmds = callback;
}
void RemoteDebug::setCallBackNewClient(void (*callback)()) {
_callbackNewClient = callback;
}
// Print
size_t RemoteDebug::write(const uint8_t *buffer, size_t size) {
// Process buffer
// Insert due a write bug w/ latest Esp8266 SDK - 17/08/18
for(size_t i=0; i<size; i++) {
write((uint8_t) buffer[i]);
}
return size;
}
size_t RemoteDebug::write(uint8_t character) {
// Write logic
uint32_t elapsed = 0;
size_t ret = 0;
#ifdef COLOR_NEW_SYSTEM
String colorLevel = "";
#endif
// Connected ?
#ifndef WEBSOCKET_DISABLED // For web socket server (app)
boolean connected = (_connected || _connectedWS);
#else
boolean connected = _connected;
#endif
// In silente mode now ?
if (_silence) {
return 0;
}
// New line writted before ?
if (_newLine ) {
#ifdef DEBUGGER_ENABLED
// For Simple software debugger - based on SerialDebug Library
// Changed handle debugger logic - 2018-02-29
if (!_showRaw) { // Not for raw mode
if (_callbackDbgEnabled && _callbackDbgEnabled()) { // Callbacks ok
if (connected && _callbackDbgEnabled()) { // Only call if is connected and debugger is enabled
// Call the handle
_callbackDbgHandle(false);
}
}
}
#endif
String show = "";
// Not in raw mode (only data)
if (!_showRaw) {
#ifdef COLOR_NEW_SYSTEM
// New color system
if (_showColors) {
switch (_lastDebugLevel) {
case VERBOSE:
show = COLOR_VERBOSE;
break;
case DEBUG:
show = COLOR_DEBUG;
break;
case INFO:
show = COLOR_INFO;
break;
case WARNING:
show = COLOR_WARNING;
break;
case ERROR:
show = COLOR_ERROR;
break;
}
colorLevel = show;
}
// Show debug level
if (_showDebugLevel) {
switch (_lastDebugLevel) {
case PROFILER:
show.concat("(P");
break;
case VERBOSE:
show.concat("(V");
break;
case DEBUG:
show.concat("(D");
break;
case INFO:
show.concat("(I");
break;
case WARNING:
show.concat("(W");
break;
case ERROR:
show.concat("(E");
break;
}
}
// Show time in millis
if (_showTime) {
if (show != "") {
show.concat(" ");
}
show.concat("t:");
show.concat(millis());
show.concat("ms");
}
// Show profiler (time between messages)
if (_showProfiler) {
elapsed = (millis() - _lastTimePrint);
boolean resetColors = false;
if (show != "") {
show.concat(" ");
}
if (_showColors) {
if (elapsed < 250) {
; // not color this
} else if (elapsed < 1000) {
show.concat(COLOR_BLACK);
show.concat(COLOR_BACKGROUND_GREEN);
resetColors = true;
} else if (elapsed < 3000) {
show.concat(COLOR_BLACK);
show.concat(COLOR_BACKGROUND_YELLOW);