Skip to content

Commit 35fe1f7

Browse files
First commit
0 parents  commit 35fe1f7

File tree

6 files changed

+868
-0
lines changed

6 files changed

+868
-0
lines changed

SimpleTelnetServer.cpp

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*
2+
Telnet support for the ESP8266 Wifi.
3+
Copyright (c) 2016 Kenneth S. Davis, All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
Extends supports of TelnetServer base class:
20+
21+
RFC 1079 - TELNET TERMINAL SPEED OPTION
22+
*/
23+
24+
#include "SimpleTelnetServer.h"
25+
26+
SimpleTelnetServer::SimpleTelnetServer()
27+
{
28+
recvBufLen = 0;
29+
}
30+
31+
SimpleTelnetServer::~SimpleTelnetServer()
32+
{
33+
end();
34+
}
35+
36+
/*
37+
This example extends TelnetServer with additional RFC's.
38+
*/
39+
bool SimpleTelnetServer::_processOption(WiFiClient& client, ClientStruct& str)
40+
{
41+
// check with the base class first.....
42+
if (TelnetServer::_processOption(client, str))
43+
return true;
44+
45+
if (str.clientState == Normal)
46+
{
47+
if (str.echo)
48+
{
49+
str.buffer[str.bufferLen] = str.opt0;
50+
str.bufferLen++;
51+
}
52+
53+
recvBuffer[recvBufLen] = str.opt0;
54+
recvBufLen++;
55+
56+
return true;
57+
}
58+
59+
if (str.clientState == InTelnetOpt0)
60+
{
61+
switch (str.opt0)
62+
{
63+
case TELNET_AYT:
64+
{
65+
66+
// send a '.'
67+
str.buffer[str.bufferLen] = '.';
68+
str.bufferLen++;
69+
str.clientState = Normal;
70+
return true;
71+
}
72+
73+
case TELNET_BRK: // break cmd
74+
case TELNET_AO: // abort output
75+
case TELNET_NOP: // Nop
76+
case TELNET_DM: // Data mark
77+
case TELNET_GA: // Go-ahead flow-control
78+
{
79+
80+
#ifdef DEBUG_TELNET
81+
DEBUG_TELNET.print("Unsupported telnet option:");
82+
DEBUG_TELNET.println(str.opt0, HEX);
83+
#endif
84+
return false;
85+
}
86+
case TELNET_EC: // erase last character
87+
{
88+
if (str.echo)
89+
{
90+
if (str.buffer[str.bufferLen] > 0)
91+
{
92+
str.buffer[str.bufferLen] = 0;
93+
str.bufferLen--;
94+
}
95+
}
96+
97+
_clientStr.clientState = Normal;
98+
return true;
99+
}
100+
case TELNET_EL: // erase current line
101+
{
102+
#ifdef DEBUG_TELNET
103+
DEBUG_TELNET.print("Unsupported telnet option:");
104+
DEBUG_TELNET.println(str.opt0, HEX);
105+
#endif
106+
_clientStr.clientState = Normal;
107+
return true;
108+
}
109+
case TELNET_IP: // interrupt process ---- what to do...?
110+
{
111+
#ifdef DEBUG_TELNET
112+
DEBUG_TELNET.print("Unsupported telnet option:");
113+
DEBUG_TELNET.println(str.opt0, HEX);
114+
#endif
115+
_clientStr.clientState = Normal;
116+
return true;
117+
}
118+
default:
119+
{
120+
#ifdef DEBUG_TELNET
121+
DEBUG_TELNET.print("Unknown telnet option:");
122+
DEBUG_TELNET.println(str.opt0, HEX);
123+
#endif
124+
return false;
125+
}
126+
}
127+
}
128+
129+
if (str.clientState == InTelnetOpt1)
130+
{
131+
switch (str.opt1)
132+
{
133+
// extend for RFC 1079 - TELNET TERMINAL SPEED OPTION, also needed
134+
// to add subnegotiation support for this.
135+
case TELNET_OPTION_TERMINAL_SPEED:
136+
{
137+
if (str.opt0 == TELNET_WILL)
138+
{
139+
str.buffer[str.bufferLen] = TELNET_IAC;
140+
str.bufferLen++;
141+
str.buffer[str.bufferLen] = TELNET_DO;
142+
str.bufferLen++;
143+
str.buffer[str.bufferLen] = str.opt1;
144+
str.bufferLen++;
145+
}
146+
else if (_clientStr.opt0 == TELNET_DO)
147+
{
148+
str.buffer[str.bufferLen] = TELNET_IAC;
149+
str.bufferLen++;
150+
str.buffer[str.bufferLen] = TELNET_WILL;
151+
str.bufferLen++;
152+
str.buffer[str.bufferLen] = str.opt1;
153+
str.bufferLen++;
154+
}
155+
156+
str.clientState = Normal;
157+
return true;
158+
}
159+
160+
default:
161+
{
162+
return false;
163+
}
164+
}
165+
}
166+
167+
return false;
168+
}
169+
170+
bool SimpleTelnetServer::_processSubNegotiation(WiFiClient &client, struct ClientStruct &str)
171+
{
172+
// we have captured a sb block:
173+
// IAC SB [ captured ] IAC SE
174+
if (TelnetServer::_processSubNegotiation(client, str))
175+
return true;
176+
177+
switch (str.negoBuffer[0])
178+
{
179+
case TELNET_OPTION_TERMINAL_SPEED:
180+
{
181+
int txSpeed = 9600;
182+
int rxSpeed = 9600;
183+
184+
if (str.negoBuffer[1] == 1)
185+
{
186+
str.buffer[str.bufferLen++] = TELNET_IAC;
187+
str.buffer[str.bufferLen++] = TELNET_SB;
188+
str.buffer[str.bufferLen++] = TELNET_OPTION_TERMINAL_SPEED;
189+
str.buffer[str.bufferLen++] = 0;
190+
str.bufferLen += sprintf((char*)&str.buffer[str.bufferLen], "%d,%d", txSpeed, rxSpeed);
191+
str.buffer[str.bufferLen++] = TELNET_IAC;
192+
str.buffer[str.bufferLen] = TELNET_SE;
193+
194+
#ifdef DEBUG_TELNET
195+
DEBUG_TELNET.println("SB TERMINAL SPEED");
196+
for (int i = 0; i < str.bufferLen; i++)
197+
{
198+
DEBUG_TELNET.print(str.buffer[i], HEX);
199+
DEBUG_TELNET.print(" ");
200+
}
201+
DEBUG_TELNET.println();
202+
#endif
203+
str.negoBufferLen = 0;
204+
return true;
205+
}
206+
207+
break;
208+
}
209+
210+
default:
211+
return false;
212+
}
213+
214+
return false;
215+
}

SimpleTelnetServer.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Telnet support for the ESP8266 Wifi.
3+
Copyright (c) 2016 Kenneth S. Davis, All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
Extends supports of TelnetServer base class:
20+
21+
RFC 857 - TELNET ECHO OPTION
22+
RFC 858 - TELNET SUPPRESS GO AHEAD OPTION
23+
RFC 1079 - TELNET TERMINAL SPEED OPTION
24+
*/
25+
26+
27+
28+
#ifndef _SIMPLETELNETSERVER_h
29+
#define _SIMPLETELNETSERVER_h
30+
31+
#if defined(ARDUINO) && ARDUINO >= 100
32+
#include "arduino.h"
33+
#else
34+
#include "WProgram.h"
35+
#endif
36+
37+
#include "Telnet.h"
38+
39+
// telnet options
40+
#define TELNET_OPTION_TERMINAL_SPEED 32
41+
42+
// Future work
43+
//#define TELNET_OPTION_COM_PORT 44
44+
45+
/*
46+
// Future work
47+
#define TELNET_COM_PORT_OPTION_SIG
48+
#define TELNET_COM_PORT_SET_BAUDRATE "1"
49+
#define TELNET_COM_PORT_SET_DATASIZE "2"
50+
#define TELNET_COM_PORT_SET_PARITY "3"
51+
#define TELNET_COM_PORT_SET_STOPSIZE "4"
52+
#define TELNET_COM_PORT_SET_CONTROL "5"
53+
#define TELNET_COM_PORT_NOTFY_LINESTATE "6"
54+
#define TELNET_COM_PORT_NOTFY_MODEMSTATE "7"
55+
#define TELNET_COM_PORT_FLOW_SUSPEND "8"
56+
#define TELNET_COM_PORT_FLOW_RESUME "9"
57+
#define TELNET_COM_PORT_LINE_MASK "10"
58+
#define TELNET_COM_PORT_MODEM_MASK "11"
59+
#define TELNET_COM_PORT_PURGE "12"
60+
*/
61+
62+
class SimpleTelnetServer : public TelnetServer
63+
{
64+
public:
65+
66+
SimpleTelnetServer();
67+
68+
virtual ~SimpleTelnetServer();
69+
70+
uint8_t recvBuffer[1024];
71+
uint8_t recvBufLen;
72+
73+
protected:
74+
75+
virtual bool _processSubNegotiation(WiFiClient &client, struct ClientStruct &str);
76+
77+
virtual bool _processOption(WiFiClient &client, struct ClientStruct &str);
78+
};
79+
80+
#endif
81+

0 commit comments

Comments
 (0)