Merge Network code with webserver etc
[clinton/Smoothieware.git] / src / libs / Network / uip / telnetd / shell.cpp
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
35 #include "stdlib.h"
36 #include "shell.h"
37 #include "uip.h"
38 #include <string.h>
39 #include "checksumm.h"
40 #include "utils.h"
41 #include "stdio.h"
42 #include "stdlib.h"
43 #include "telnetd.h"
44 #include "CallbackStream.h"
45 #include "Kernel.h"
46
47 //#define DEBUG_PRINTF(...)
48 #define DEBUG_PRINTF printf
49
50 struct ptentry {
51 uint16_t command_cs;
52 void (* pfunc)(char *str, Shell *sh);
53 };
54
55 #define SHELL_PROMPT "> "
56
57 /*---------------------------------------------------------------------------*/
58 bool Shell::parse(register char *str, struct ptentry *t)
59 {
60 struct ptentry *p;
61 for (p = t; p->command_cs != 0; ++p) {
62 if (get_checksum(str) == p->command_cs) {
63 break;
64 }
65 }
66
67 p->pfunc(str, this);
68
69 return p->command_cs != 0;
70 }
71 /*---------------------------------------------------------------------------*/
72 static void help(char *str, Shell *sh)
73 {
74 sh->output("Available commands: All others are passed on\n");
75 sh->output("netstat - show network info\n");
76 sh->output("? - show network help\n");
77 sh->output("help - show command help\n");
78 sh->output("exit, quit - exit shell\n");
79 }
80
81 /*---------------------------------------------------------------------------*/
82 static const char *states[] = {
83 "CLOSED",
84 "SYN_RCVD",
85 "SYN_SENT",
86 "ESTABLISHED",
87 "FIN_WAIT_1",
88 "FIN_WAIT_2",
89 "CLOSING",
90 "TIME_WAIT",
91 "LAST_ACK",
92 "NONE",
93 "RUNNING",
94 "CALLED"
95 };
96 static void connections(char *str, Shell *sh)
97 {
98 char istr[128];
99 struct uip_conn *connr;
100 snprintf(istr, sizeof(istr), "Initial MSS: %d, MSS: %d\n", uip_initialmss(), uip_mss());
101 sh->output(istr);
102 sh->output("Current connections: \n");
103
104 for (connr = &uip_conns[0]; connr <= &uip_conns[UIP_CONNS - 1]; ++connr) {
105 if(connr->tcpstateflags != UIP_CLOSED) {
106 snprintf(istr, sizeof(istr), "%d, %u.%u.%u.%u:%u, %s, %u, %u, %c %c\n",
107 HTONS(connr->lport),
108 uip_ipaddr1(connr->ripaddr), uip_ipaddr2(connr->ripaddr), uip_ipaddr3(connr->ripaddr), uip_ipaddr4(connr->ripaddr),
109 HTONS(connr->rport),
110 states[connr->tcpstateflags & UIP_TS_MASK],
111 connr->nrtx,
112 connr->timer,
113 (uip_outstanding(connr)) ? '*' : ' ',
114 (uip_stopped(connr)) ? '!' : ' ');
115
116 sh->output(istr);
117 }
118 }
119 }
120
121 static void quit(char *str, Shell *sh)
122 {
123 sh->close();
124 }
125
126 //#include "clock.h"
127 static void test(char *str, Shell *sh)
128 {
129 printf("In Test\n");
130
131 // struct timer t;
132 // u16_t ticks= CLOCK_SECOND*5;
133 // timer_set(&t, ticks);
134 // printf("Wait....\n");
135 // while(!timer_expired(&t)) {
136
137 // }
138 // printf("Done\n");
139 /*
140 const char *fn= "/sd/test6.txt";
141 uint16_t *buf= (uint16_t *)malloc(200*2);
142 int cnt= 0;
143 FILE *fp;
144 for(int i=0;i<10;i++) {
145 fp= fopen(fn, i == 0 ? "w" : "a");
146 if(fp == NULL) {
147 printf("failed to open file\n");
148 return;
149 }
150 for (int x = 0; x < 200; ++x) {
151 buf[x]= x+cnt;
152 }
153 cnt+=200;
154 int n= fwrite(buf, 2, 200, fp);
155 printf("wrote %d, %d\n", i, n);
156 fclose(fp);
157 }
158
159 fp= fopen(fn, "r");
160 if(fp == NULL) {
161 printf("failed to open file for read\n");
162 return;
163 }
164 printf("Opened file %s for read\n", fn);
165 do {
166 int n= fread(buf, 2, 200, fp);
167 if(n <= 0) break;
168 for(int x=0;x<n;x++) {
169 printf("%04X, ", buf[x]);
170 }
171 }while(1);
172 fclose(fp);
173 free(buf);
174 */
175 }
176
177 /*---------------------------------------------------------------------------*/
178
179 static void unknown(char *str, Shell *sh)
180 {
181 // its some other command, so queue it for mainloop to find
182 if (strlen(str) > 0) {
183 CommandQueue::getInstance()->add(str, sh->getStream());
184 }
185 }
186 /*---------------------------------------------------------------------------*/
187 static struct ptentry parsetab[] = {
188 {CHECKSUM("netstat"), connections},
189 {CHECKSUM("exit"), quit},
190 {CHECKSUM("quit"), quit},
191 {CHECKSUM("test"), test},
192 {CHECKSUM("?"), help},
193
194 /* Default action */
195 {0, unknown}
196 };
197 /*---------------------------------------------------------------------------*/
198 // this callback gets the results of a command, line by line
199 // NULL means command completed
200 // static
201 int Shell::command_result(const char *str, void *p)
202 {
203 // FIXME problem when shell is deleted and this gets called from slow command
204 // need a way to know this shell was closed or deleted
205 Shell *sh = (Shell *)p;
206 if (str == NULL) {
207 // indicates command is complete
208 // only prompt when command is completed
209 sh->telnet->output_prompt(SHELL_PROMPT);
210 return 0;
211
212 } else {
213 if (sh->telnet->can_output()) {
214 if (sh->telnet->output(str) == -1) return -1; // connection was closed
215 return 1;
216 }
217 // we are stalled
218 return 0;
219 }
220 }
221
222 /*---------------------------------------------------------------------------*/
223 void Shell::start()
224 {
225 telnet->output("Smoothie command shell\r\n> ");
226 }
227
228 int Shell::queue_size()
229 {
230 return CommandQueue::getInstance()->size();
231 }
232 /*---------------------------------------------------------------------------*/
233 void Shell::input(char *cmd)
234 {
235 if (parse(cmd, parsetab)) {
236 telnet->output_prompt(SHELL_PROMPT);
237 }
238 }
239 /*---------------------------------------------------------------------------*/
240
241 int Shell::output(const char *str)
242 {
243 return telnet->output(str);
244 }
245
246 void Shell::close()
247 {
248 telnet->close();
249 }
250
251 void Shell::setConsole()
252 {
253 // add it to the kernels output stream if we are a console
254 // TODO do we do this for all connections? so pronterface will get file done when playing from M24?
255 // then we need to turn it off for the streaming app
256 DEBUG_PRINTF("Shell: Adding stream to kernel streams\n");
257 THEKERNEL->streams->append_stream(pstream);
258 isConsole= true;
259 }
260
261 Shell::Shell(Telnetd *telnet)
262 {
263 DEBUG_PRINTF("Shell: ctor %p - %p\n", this, telnet);
264 this->telnet= telnet;
265 // create a callback StreamOutput for this connection
266 pstream = new CallbackStream(command_result, this);
267 isConsole= false;
268 }
269
270 Shell::~Shell()
271 {
272 if(isConsole) {
273 DEBUG_PRINTF("Shell: Removing stream from kernel streams\n");
274 THEKERNEL->streams->remove_stream(pstream);
275 }
276 // we cannot delete this stream until it is no longer in any command queue entries
277 // so mark it as closed, and allow it to delete itself when it is no longer being used
278 static_cast<CallbackStream*>(pstream)->mark_closed(); // mark the stream as closed so we do not get any callbacks
279 DEBUG_PRINTF("Shell: dtor %p\n", this);
280 }