2 * Copyright (c) 2003, Adam Dunkels.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
29 * This file is part of the uIP TCP/IP stack.
31 * $Id: shell.c,v 1.1 2006/06/07 09:43:54 adam Exp $
39 #include "checksumm.h"
44 #include "CallbackStream.h"
47 //#define DEBUG_PRINTF(...)
48 #define DEBUG_PRINTF printf
52 void (* pfunc
)(char *str
, Shell
*sh
);
55 #define SHELL_PROMPT "> "
57 /*---------------------------------------------------------------------------*/
58 bool Shell::parse(register char *str
, struct ptentry
*t
)
61 for (p
= t
; p
->command_cs
!= 0; ++p
) {
62 if (get_checksum(str
) == p
->command_cs
) {
69 return p
->command_cs
!= 0;
71 /*---------------------------------------------------------------------------*/
72 static void help(char *str
, Shell
*sh
)
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");
81 /*---------------------------------------------------------------------------*/
82 static const char *states
[] = {
96 static void connections(char *str
, Shell
*sh
)
99 struct uip_conn
*connr
;
100 snprintf(istr
, sizeof(istr
), "Initial MSS: %d, MSS: %d\n", uip_initialmss(), uip_mss());
102 sh
->output("Current connections: \n");
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",
108 uip_ipaddr1(connr
->ripaddr
), uip_ipaddr2(connr
->ripaddr
), uip_ipaddr3(connr
->ripaddr
), uip_ipaddr4(connr
->ripaddr
),
110 states
[connr
->tcpstateflags
& UIP_TS_MASK
],
113 (uip_outstanding(connr
)) ? '*' : ' ',
114 (uip_stopped(connr
)) ? '!' : ' ');
121 static void quit(char *str
, Shell
*sh
)
127 static void test(char *str
, Shell
*sh
)
132 // u16_t ticks= CLOCK_SECOND*5;
133 // timer_set(&t, ticks);
134 // printf("Wait....\n");
135 // while(!timer_expired(&t)) {
140 const char *fn= "/sd/test6.txt";
141 uint16_t *buf= (uint16_t *)malloc(200*2);
144 for(int i=0;i<10;i++) {
145 fp= fopen(fn, i == 0 ? "w" : "a");
147 printf("failed to open file\n");
150 for (int x = 0; x < 200; ++x) {
154 int n= fwrite(buf, 2, 200, fp);
155 printf("wrote %d, %d\n", i, n);
161 printf("failed to open file for read\n");
164 printf("Opened file %s for read\n", fn);
166 int n= fread(buf, 2, 200, fp);
168 for(int x=0;x<n;x++) {
169 printf("%04X, ", buf[x]);
177 /*---------------------------------------------------------------------------*/
179 static void unknown(char *str
, Shell
*sh
)
181 // its some other command, so queue it for mainloop to find
182 if (strlen(str
) > 0) {
183 CommandQueue::getInstance()->add(str
, sh
->getStream());
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
},
197 /*---------------------------------------------------------------------------*/
198 // this callback gets the results of a command, line by line
199 // NULL means command completed
201 int Shell::command_result(const char *str
, void *p
)
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
;
207 // indicates command is complete
208 // only prompt when command is completed
209 sh
->telnet
->output_prompt(SHELL_PROMPT
);
213 if (sh
->telnet
->can_output()) {
214 if (sh
->telnet
->output(str
) == -1) return -1; // connection was closed
222 /*---------------------------------------------------------------------------*/
225 telnet
->output("Smoothie command shell\r\n> ");
228 int Shell::queue_size()
230 return CommandQueue::getInstance()->size();
232 /*---------------------------------------------------------------------------*/
233 void Shell::input(char *cmd
)
235 if (parse(cmd
, parsetab
)) {
236 telnet
->output_prompt(SHELL_PROMPT
);
239 /*---------------------------------------------------------------------------*/
241 int Shell::output(const char *str
)
243 return telnet
->output(str
);
251 void Shell::setConsole()
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
);
261 Shell::Shell(Telnetd
*telnet
)
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);
273 DEBUG_PRINTF("Shell: Removing stream from kernel streams\n");
274 THEKERNEL
->streams
->remove_stream(pstream
);
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);