fix typo
[clinton/Smoothieware.git] / src / libs / Network / uip / telnetd / shell.cpp
CommitLineData
d4ee6ee2
JM
1/*
2* Copyright (c) 2003, Adam Dunkels.
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions
7* are met:
8* 1. Redistributions of source code must retain the above copyright
9* notice, this list of conditions and the following disclaimer.
10* 2. Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* 3. The name of the author may not be used to endorse or promote
14* products derived from this software without specific prior
15* written permission.
16*
17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*
29* This file is part of the uIP TCP/IP stack.
30*
31* $Id: shell.c,v 1.1 2006/06/07 09:43:54 adam Exp $
32*
33*/
34
61134a65 35#include "Kernel.h"
d4ee6ee2
JM
36#include "stdlib.h"
37#include "shell.h"
38#include "uip.h"
39#include <string.h>
40#include "checksumm.h"
41#include "utils.h"
42#include "stdio.h"
43#include "stdlib.h"
44#include "telnetd.h"
45#include "CallbackStream.h"
61134a65 46#include "StreamOutputPool.h"
0d9c5e2a 47#include "CommandQueue.h"
d4ee6ee2 48
75eae806
JM
49#define DEBUG_PRINTF(...)
50//#define DEBUG_PRINTF printf
d4ee6ee2
JM
51
52struct ptentry {
7e81f138 53 const char *command;
d4ee6ee2
JM
54 void (* pfunc)(char *str, Shell *sh);
55};
56
57#define SHELL_PROMPT "> "
58
59/*---------------------------------------------------------------------------*/
7e81f138 60bool Shell::parse(register char *str, const struct ptentry *t)
d4ee6ee2 61{
7e81f138
JM
62 const struct ptentry *p;
63 for (p = t; p->command != 0; ++p) {
80dd1a8e 64 if (strcasecmp(str, p->command) == 0) {
d4ee6ee2
JM
65 break;
66 }
67 }
68
69 p->pfunc(str, this);
70
7e81f138 71 return p->command != 0;
d4ee6ee2
JM
72}
73/*---------------------------------------------------------------------------*/
74static void help(char *str, Shell *sh)
75{
abc3e4bd 76 sh->output("Available telnet commands: All others are passed to the command handler\n");
d4ee6ee2 77 sh->output("netstat - show network info\n");
7cbf5b2c 78 sh->output("h - show network help\n");
80dd1a8e 79 sh->output("? - show current query status\n");
d4ee6ee2
JM
80 sh->output("help - show command help\n");
81 sh->output("exit, quit - exit shell\n");
82}
83
80dd1a8e
JM
84static void query(char *str, Shell *sh)
85{
86 sh->output(THEKERNEL->get_query_string().c_str());
87}
88
d4ee6ee2
JM
89/*---------------------------------------------------------------------------*/
90static const char *states[] = {
91 "CLOSED",
92 "SYN_RCVD",
93 "SYN_SENT",
94 "ESTABLISHED",
95 "FIN_WAIT_1",
96 "FIN_WAIT_2",
97 "CLOSING",
98 "TIME_WAIT",
99 "LAST_ACK",
100 "NONE",
101 "RUNNING",
102 "CALLED"
103};
104static void connections(char *str, Shell *sh)
105{
106 char istr[128];
107 struct uip_conn *connr;
108 snprintf(istr, sizeof(istr), "Initial MSS: %d, MSS: %d\n", uip_initialmss(), uip_mss());
109 sh->output(istr);
110 sh->output("Current connections: \n");
111
112 for (connr = &uip_conns[0]; connr <= &uip_conns[UIP_CONNS - 1]; ++connr) {
113 if(connr->tcpstateflags != UIP_CLOSED) {
114 snprintf(istr, sizeof(istr), "%d, %u.%u.%u.%u:%u, %s, %u, %u, %c %c\n",
115 HTONS(connr->lport),
116 uip_ipaddr1(connr->ripaddr), uip_ipaddr2(connr->ripaddr), uip_ipaddr3(connr->ripaddr), uip_ipaddr4(connr->ripaddr),
117 HTONS(connr->rport),
118 states[connr->tcpstateflags & UIP_TS_MASK],
119 connr->nrtx,
120 connr->timer,
121 (uip_outstanding(connr)) ? '*' : ' ',
122 (uip_stopped(connr)) ? '!' : ' ');
123
124 sh->output(istr);
125 }
126 }
127}
128
129static void quit(char *str, Shell *sh)
130{
131 sh->close();
132}
133
134//#include "clock.h"
7cbf5b2c 135static void ntest(char *str, Shell *sh)
d4ee6ee2
JM
136{
137 printf("In Test\n");
138
139 // struct timer t;
140 // u16_t ticks= CLOCK_SECOND*5;
141 // timer_set(&t, ticks);
142 // printf("Wait....\n");
143 // while(!timer_expired(&t)) {
144
145 // }
146 // printf("Done\n");
147 /*
148 const char *fn= "/sd/test6.txt";
149 uint16_t *buf= (uint16_t *)malloc(200*2);
150 int cnt= 0;
151 FILE *fp;
152 for(int i=0;i<10;i++) {
153 fp= fopen(fn, i == 0 ? "w" : "a");
154 if(fp == NULL) {
155 printf("failed to open file\n");
156 return;
157 }
158 for (int x = 0; x < 200; ++x) {
159 buf[x]= x+cnt;
160 }
161 cnt+=200;
162 int n= fwrite(buf, 2, 200, fp);
163 printf("wrote %d, %d\n", i, n);
164 fclose(fp);
165 }
166
167 fp= fopen(fn, "r");
168 if(fp == NULL) {
169 printf("failed to open file for read\n");
170 return;
171 }
172 printf("Opened file %s for read\n", fn);
173 do {
174 int n= fread(buf, 2, 200, fp);
175 if(n <= 0) break;
176 for(int x=0;x<n;x++) {
177 printf("%04X, ", buf[x]);
178 }
179 }while(1);
180 fclose(fp);
181 free(buf);
182 */
183}
184
185/*---------------------------------------------------------------------------*/
186
187static void unknown(char *str, Shell *sh)
188{
189 // its some other command, so queue it for mainloop to find
190 if (strlen(str) > 0) {
191 CommandQueue::getInstance()->add(str, sh->getStream());
192 }
193}
194/*---------------------------------------------------------------------------*/
7e81f138
JM
195static const struct ptentry parsetab[] = {
196 {"netstat", connections},
197 {"exit", quit},
198 {"quit", quit},
7cbf5b2c 199 {"ntest", ntest},
80dd1a8e 200 {"?", query},
7cbf5b2c 201 {"h", help},
d4ee6ee2
JM
202
203 /* Default action */
204 {0, unknown}
205};
206/*---------------------------------------------------------------------------*/
207// this callback gets the results of a command, line by line
208// NULL means command completed
209// static
210int Shell::command_result(const char *str, void *p)
211{
212 // FIXME problem when shell is deleted and this gets called from slow command
213 // need a way to know this shell was closed or deleted
214 Shell *sh = (Shell *)p;
215 if (str == NULL) {
216 // indicates command is complete
217 // only prompt when command is completed
218 sh->telnet->output_prompt(SHELL_PROMPT);
219 return 0;
220
221 } else {
222 if (sh->telnet->can_output()) {
223 if (sh->telnet->output(str) == -1) return -1; // connection was closed
224 return 1;
225 }
226 // we are stalled
227 return 0;
228 }
229}
230
231/*---------------------------------------------------------------------------*/
232void Shell::start()
bc53ce10
JM
233{ // add it to the kernels output stream
234 DEBUG_PRINTF("Shell: Adding stream to kernel streams\n");
235 THEKERNEL->streams->append_stream(pstream);
48a0fdb6
JM
236 telnet->output("Smoothie command shell\r\n");
237 telnet->output_prompt(SHELL_PROMPT);
d4ee6ee2
JM
238}
239
240int Shell::queue_size()
241{
242 return CommandQueue::getInstance()->size();
243}
244/*---------------------------------------------------------------------------*/
245void Shell::input(char *cmd)
246{
247 if (parse(cmd, parsetab)) {
248 telnet->output_prompt(SHELL_PROMPT);
249 }
250}
251/*---------------------------------------------------------------------------*/
252
253int Shell::output(const char *str)
254{
255 return telnet->output(str);
256}
257
258void Shell::close()
259{
260 telnet->close();
261}
262
263void Shell::setConsole()
264{
d4ee6ee2
JM
265 isConsole= true;
266}
267
268Shell::Shell(Telnetd *telnet)
269{
270 DEBUG_PRINTF("Shell: ctor %p - %p\n", this, telnet);
271 this->telnet= telnet;
272 // create a callback StreamOutput for this connection
273 pstream = new CallbackStream(command_result, this);
274 isConsole= false;
275}
276
277Shell::~Shell()
278{
bc53ce10
JM
279 DEBUG_PRINTF("Shell: Removing stream from kernel streams\n");
280 THEKERNEL->streams->remove_stream(pstream);
281
d4ee6ee2
JM
282 // we cannot delete this stream until it is no longer in any command queue entries
283 // so mark it as closed, and allow it to delete itself when it is no longer being used
284 static_cast<CallbackStream*>(pstream)->mark_closed(); // mark the stream as closed so we do not get any callbacks
285 DEBUG_PRINTF("Shell: dtor %p\n", this);
286}