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 $
40 #include "checksumm.h"
45 #include "CallbackStream.h"
46 #include "StreamOutputPool.h"
47 #include "CommandQueue.h"
49 //#define DEBUG_PRINTF(...)
50 #define DEBUG_PRINTF printf
54 void (* pfunc
)(char *str
, Shell
*sh
);
57 #define SHELL_PROMPT "> "
59 /*---------------------------------------------------------------------------*/
60 bool Shell::parse(register char *str
, const struct ptentry
*t
)
62 const struct ptentry
*p
;
63 for (p
= t
; p
->command
!= 0; ++p
) {
64 if (strncasecmp(str
, p
->command
, strlen(p
->command
)) == 0) {
71 return p
->command
!= 0;
73 /*---------------------------------------------------------------------------*/
74 static void help(char *str
, Shell
*sh
)
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");
83 /*---------------------------------------------------------------------------*/
84 static const char *states
[] = {
98 static void connections(char *str
, Shell
*sh
)
101 struct uip_conn
*connr
;
102 snprintf(istr
, sizeof(istr
), "Initial MSS: %d, MSS: %d\n", uip_initialmss(), uip_mss());
104 sh
->output("Current connections: \n");
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",
110 uip_ipaddr1(connr
->ripaddr
), uip_ipaddr2(connr
->ripaddr
), uip_ipaddr3(connr
->ripaddr
), uip_ipaddr4(connr
->ripaddr
),
112 states
[connr
->tcpstateflags
& UIP_TS_MASK
],
115 (uip_outstanding(connr
)) ? '*' : ' ',
116 (uip_stopped(connr
)) ? '!' : ' ');
123 static void quit(char *str
, Shell
*sh
)
129 static void test(char *str
, Shell
*sh
)
134 // u16_t ticks= CLOCK_SECOND*5;
135 // timer_set(&t, ticks);
136 // printf("Wait....\n");
137 // while(!timer_expired(&t)) {
142 const char *fn= "/sd/test6.txt";
143 uint16_t *buf= (uint16_t *)malloc(200*2);
146 for(int i=0;i<10;i++) {
147 fp= fopen(fn, i == 0 ? "w" : "a");
149 printf("failed to open file\n");
152 for (int x = 0; x < 200; ++x) {
156 int n= fwrite(buf, 2, 200, fp);
157 printf("wrote %d, %d\n", i, n);
163 printf("failed to open file for read\n");
166 printf("Opened file %s for read\n", fn);
168 int n= fread(buf, 2, 200, fp);
170 for(int x=0;x<n;x++) {
171 printf("%04X, ", buf[x]);
179 /*---------------------------------------------------------------------------*/
181 static void unknown(char *str
, Shell
*sh
)
183 // its some other command, so queue it for mainloop to find
184 if (strlen(str
) > 0) {
185 CommandQueue::getInstance()->add(str
, sh
->getStream());
188 /*---------------------------------------------------------------------------*/
189 static const struct ptentry parsetab
[] = {
190 {"netstat", connections
},
199 /*---------------------------------------------------------------------------*/
200 // this callback gets the results of a command, line by line
201 // NULL means command completed
203 int Shell::command_result(const char *str
, void *p
)
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
;
209 // indicates command is complete
210 // only prompt when command is completed
211 sh
->telnet
->output_prompt(SHELL_PROMPT
);
215 if (sh
->telnet
->can_output()) {
216 if (sh
->telnet
->output(str
) == -1) return -1; // connection was closed
224 /*---------------------------------------------------------------------------*/
227 telnet
->output("Smoothie command shell\r\n> ");
230 int Shell::queue_size()
232 return CommandQueue::getInstance()->size();
234 /*---------------------------------------------------------------------------*/
235 void Shell::input(char *cmd
)
237 if (parse(cmd
, parsetab
)) {
238 telnet
->output_prompt(SHELL_PROMPT
);
241 /*---------------------------------------------------------------------------*/
243 int Shell::output(const char *str
)
245 return telnet
->output(str
);
253 void Shell::setConsole()
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
);
263 Shell::Shell(Telnetd
*telnet
)
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);
275 DEBUG_PRINTF("Shell: Removing stream from kernel streams\n");
276 THEKERNEL
->streams
->remove_stream(pstream
);
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);