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