-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelnet_cmd.h
145 lines (123 loc) · 4.12 KB
/
telnet_cmd.h
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
#ifndef telnet_cmd_h
#define telnet_cmd_h
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#include <serialcmd.h>
#include <motor.h>
bool DEBUG_telnet_cmd = true;
int telnetport = 23;
// ansi stuff, could always use printf instead of concat
String ansiPRE = "\033"; // escape code
String ansiHOME = "\033[H"; // cursor home
String ansiESC = "\033[2J"; // esc
String ansiCLC = "\033[?25l"; // invisible cursor
String ansiCLS = "\033c\e[3J";
String ansiEND = "\033[0m"; // closing tag for styles
String ansiBOLD = "\033[1m";
String ansiEL = "\033[K"; // erase line
String ansiRED = "\033[41m"; // red background
String ansiGRN = "\033[42m"; // green background
String ansiBLU = "\033[44m"; // blue background
String ansiREDF = "\033[31m"; // red foreground
String ansiGRNF = "\033[34m"; // green foreground
String ansiBLUF = "\033[32m"; // blue foreground
String BELL = "\a";
// declare telnet server (do NOT put in setup())
WiFiServer TelnetServer(telnetport);
WiFiClient Telnet;
void println(String msg){
Serial.println(msg);
Telnet.println(msg);
}
void init_telnet(){
TelnetServer.begin();
TelnetServer.setNoDelay(true);
Serial.println("[TELNET] Starting telnet server on port " + (String)telnetport);
}
void telnet_status(){
// control panel sample
// Telnet.print(ansiCLS); // clear screen buffer
// Telnet.print(ansiCLC); // hide cursor
// Telnet.println("\n\n"); // 2 empty lines for loop
// Telnet.print(ansiHOME);
Telnet.println("uptime: "+ansiRED + ansiBOLD + " " + (String)millis() + " " + ansiEND + ansiEND+" ms");
Telnet.print(ansiGRNF + ansiBOLD);
// do RSSI display
int32_t rssi = WiFi.RSSI();
if(rssi < -96) Telnet.println("No Signal");
else{
int count = (int)map(rssi,-1,-96,60,1);
for(int i=0;i<80;i++){
if(i<count) Telnet.print("|");
// else Telnet.print(" ");
}
Telnet.print(ansiEND+ansiEND);
Telnet.print(" ");
Telnet.print((String)rssi + " dB");
}
Telnet.print(ansiEL);
Telnet.print("\n\n");
Telnet.flush(); // clear input buffer, else you get strange characters
emptyBuffer();
delay(20); // to fast might crash terminals
}
// new client connected
void handleClientConnect(){
telnet_status();
}
void handleTelnet(){
// only allows 1 client, use serverClients[MAX_SRV_CLIENTS] and loop them for more
if (TelnetServer.hasClient()){
// client is connected
if (!Telnet || !Telnet.connected()){
if(Telnet){
Telnet.stop(); // client disconnected
if(DEBUG_telnet_cmd)Serial.println("[TELNET] Client has disconnected");
} else {
if(DEBUG_telnet_cmd)Serial.println("[TELNET] Client disappeared");
}
Telnet = TelnetServer.available(); // ready for new client
} else {
TelnetServer.available().stop(); // have client, block new conections
}
if(DEBUG_telnet_cmd)Serial.println("[TELNET] Client Connected");
Telnet.flush(); // clear input buffer, else you get strange characters
handleClientConnect();
}
if (Telnet && Telnet.connected() && Telnet.available()){
// client input processing
while(Telnet.available()){
// Serial.println(Serial.peek());
readSerial(Telnet);
// inputString = Serial.readStringUntil('\n');
// parseSerial();
delay(0);
}
// if(echo)
// Serial.write(Telnet.read()); // pass through
// do other stuff with client input here
}
}
// void doTelnet(){
// if (TelnetServer.hasClient()){
// // client is connected
// if (!Telnet || !Telnet.connected()){
// if(Telnet) Telnet.stop(); // client disconnected
// Telnet = TelnetServer.available(); // ready for new client
// } else {
// TelnetServer.available().stop(); // have client, block new conections
// }
// }
// // BUG Telnet.available() crashes wifi on 2.0.4 ????
// if (Telnet && Telnet.connected() && Telnet.available()){
// // client input processing
// while(Telnet.available())
// Serial.write(Telnet.read()); // pass through
// // do other stuff with client input here
// }
// }
void process_telnet() {
handleTelnet();
}
#endif