-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMMtelnet.c
165 lines (156 loc) · 8.02 KB
/
MMtelnet.c
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
/***********************************************************************************************************************
PicoMite MMBasic
custom.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
//#define DEBUG_printf printf
#define DEBUG_printf
static char Telnetbuff[256]={0};
static int Telnetpos=0;
static const uint8_t telnet_init_options[] =
{
// TELNET_CHAR_IAC, TELNET_CHAR_WILL, TELNET_OPT_SUPPRESS_GO_AHEAD,
255,251,3,
// TELNET_CHAR_IAC, TELNET_CHAR_DO, TELNET_OPT_SUPPRESS_GO_AHEAD,
255,253,3,
// TELNET_CHAR_IAC, TELNET_CHAR_WILL, TELNET_OPT_ECHO,
255,251,1,
//
255,253,34,
255,254,34,
0
};
static err_t tcp_telnet_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) {
TCP_SERVER_T *state = (TCP_SERVER_T*)arg;
// DEBUG_printf("telnet_server_sent %u\n", len);
state->sent_len[state->telnet_pcb_no] = len;
return ERR_OK;
}
void TelnetPutCommand(int command, int option){
Telnetbuff[Telnetpos]=255;
Telnetpos++;
Telnetbuff[Telnetpos]=command;
Telnetpos++;
if(option){
Telnetbuff[Telnetpos]=255;
Telnetpos++;
}
}
void __not_in_flash_func(TelnetPutC)(int c,int flush){
TCP_SERVER_T *state = (TCP_SERVER_T*)TCPstate;
if(state->telnet_pcb_no==99 || !WIFIconnected )return;
if(!(flush==-1)){
Telnetbuff[Telnetpos]=c;
Telnetpos++;
if(c==255){
Telnetbuff[Telnetpos]=c;
Telnetpos++;
}
if(c==13){
Telnetbuff[Telnetpos]=0;
Telnetpos++;
}
}
if(Telnetpos>=sizeof(Telnetbuff-4) || (flush==-1 && Telnetpos)){
int pcb=state->telnet_pcb_no;
state->to_send[pcb]=Telnetpos;
state->buffer_sent[state->telnet_pcb_no]=(uint8_t *)Telnetbuff;
if(state->client_pcb[pcb]){
// cyw43_arch_lwip_check();
tcp_server_send_data(state, state->client_pcb[pcb], pcb);
}
Telnetpos=0;
}
}
err_t tcp_telnet_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
static int lastchar=-1;
// TCP_SERVER_T *state = (TCP_SERVER_T*)arg;
// int pcb=state->telnet_pcb_no;
if (!p) {
return ERR_OK;
}
// cyw43_arch_lwip_check();
if (p->tot_len > 0) {
tcp_recved(tpcb, p->tot_len);
if(((char *)p->payload)[0]==255){
// for(int i=0;i<p->tot_len;i++)DEBUG_printf("%d,",((char *)p->payload)[i]);
// DEBUG_printf("\r\n");
} else {
for(int j=0;j<p->tot_len;j++){
ConsoleRxBuf[ConsoleRxBufHead] = ((char *)p->payload)[j];
if((lastchar==13 && ConsoleRxBuf[ConsoleRxBufHead]==0) ||
(lastchar==255 && ConsoleRxBuf[ConsoleRxBufHead]==255)){
lastchar=-1;
continue;
}
if(BreakKey && ConsoleRxBuf[ConsoleRxBufHead] == BreakKey) {// if the user wants to stop the progran
MMAbort = true; // set the flag for the interpreter to see
ConsoleRxBufHead = ConsoleRxBufTail; // empty the buffer
} else if(ConsoleRxBuf[ConsoleRxBufHead] ==keyselect && KeyInterrupt!=NULL){
Keycomplete=1;
} else {
lastchar=ConsoleRxBuf[ConsoleRxBufHead];
ConsoleRxBufHead = (ConsoleRxBufHead + 1) % CONSOLE_RX_BUF_SIZE; // advance the head of the queue
if(ConsoleRxBufHead == ConsoleRxBufTail) { // if the buffer has overflowed
ConsoleRxBufTail = (ConsoleRxBufTail + 1) % CONSOLE_RX_BUF_SIZE; // throw away the oldest char
}
}
}
}
}
pbuf_free(p);
return ERR_OK;
}
void tcp_telnet_err(void *arg, err_t err) {
TCP_SERVER_T *state = (TCP_SERVER_T*)arg;
// if (err != ERR_ABRT) {
// char buff[STRINGSIZE]={0};
// DEBUG_printf("Telnet disconnected %d\r\n", err);
tcp_server_close(arg,state->telnet_pcb_no);
state->telnet_pcb_no=99;
// if(!CurrentLinePtr) longjmp(mark, 1);
// else longjmp(ErrNext,1) ;
// }
}
/*static err_t tcp_telnet_poll(void *arg, struct tcp_pcb *tpcb) {
TCP_SERVER_T *state = (TCP_SERVER_T*)arg;
// cyw43_arch_lwip_check();
int i=0;
while(state->client_pcb[i]!=tpcb && i<=MaxPcb)i++;
if(i==MaxPcb)error("Internal TCP receive error");
state->write_pcb=i;
DEBUG_printf("tcp_server_poll_fn\r\n");
return tcp_server_close(argstate->telnet_pcb_no); // no activity so close the connection
}*/
void starttelnet(struct tcp_pcb *client_pcb, int pcb, void *arg){
TCP_SERVER_T *state = (TCP_SERVER_T*)arg;
// DEBUG_printf("Telnet Client connected %x on pcb %d\r\n",(uint32_t)client_pcb,pcb);
tcp_arg(client_pcb, state);
tcp_sent(client_pcb, tcp_telnet_sent);
tcp_recv(client_pcb, tcp_telnet_recv);
tcp_err(client_pcb, tcp_telnet_err);
state->telnet_pcb_no=pcb;
state->keepalive[pcb]=1;
err_t err = tcp_write(client_pcb, telnet_init_options, sizeof(telnet_init_options), 0);
if (err != ERR_OK) {
tcp_server_close(state,pcb);
}
}