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 (strcasecmp(str
, p
->command
) == 0) {
71 return p
->command
!= 0;
73 /*---------------------------------------------------------------------------*/
74 static void help(char *str
, Shell
*sh
)
76 sh
->output("Available telnet commands: All others are passed to the command handler\n");
77 sh
->output("netstat - show network info\n");
78 sh
->output("h - show network help\n");
79 sh
->output("? - show current query status\n");
80 sh
->output("help - show command help\n");
81 sh
->output("exit, quit - exit shell\n");
84 static void query(char *str
, Shell
*sh
)
86 sh
->output(THEKERNEL
->get_query_string().c_str());
89 /*---------------------------------------------------------------------------*/
90 static const char *states
[] = {
104 static void connections(char *str
, Shell
*sh
)
107 struct uip_conn
*connr
;
108 snprintf(istr
, sizeof(istr
), "Initial MSS: %d, MSS: %d\n", uip_initialmss(), uip_mss());
110 sh
->output("Current connections: \n");
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",
116 uip_ipaddr1(connr
->ripaddr
), uip_ipaddr2(connr
->ripaddr
), uip_ipaddr3(connr
->ripaddr
), uip_ipaddr4(connr
->ripaddr
),
118 states
[connr
->tcpstateflags
& UIP_TS_MASK
],
121 (uip_outstanding(connr
)) ? '*' : ' ',
122 (uip_stopped(connr
)) ? '!' : ' ');
129 static void quit(char *str
, Shell
*sh
)
135 static void ntest(char *str
, Shell
*sh
)
140 // u16_t ticks= CLOCK_SECOND*5;
141 // timer_set(&t, ticks);
142 // printf("Wait....\n");
143 // while(!timer_expired(&t)) {
148 const char *fn= "/sd/test6.txt";
149 uint16_t *buf= (uint16_t *)malloc(200*2);
152 for(int i=0;i<10;i++) {
153 fp= fopen(fn, i == 0 ? "w" : "a");
155 printf("failed to open file\n");
158 for (int x = 0; x < 200; ++x) {
162 int n= fwrite(buf, 2, 200, fp);
163 printf("wrote %d, %d\n", i, n);
169 printf("failed to open file for read\n");
172 printf("Opened file %s for read\n", fn);
174 int n= fread(buf, 2, 200, fp);
176 for(int x=0;x<n;x++) {
177 printf("%04X, ", buf[x]);
185 /*---------------------------------------------------------------------------*/
187 static void unknown(char *str
, Shell
*sh
)
189 // its some other command, so queue it for mainloop to find
190 if (strlen(str
) > 0) {
191 CommandQueue::getInstance()->add(str
, sh
->getStream());
194 /*---------------------------------------------------------------------------*/
195 static const struct ptentry parsetab
[] = {
196 {"netstat", connections
},
206 /*---------------------------------------------------------------------------*/
207 // this callback gets the results of a command, line by line
208 // NULL means command completed
210 int Shell::command_result(const char *str
, void *p
)
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
;
216 // indicates command is complete
217 // only prompt when command is completed
218 sh
->telnet
->output_prompt(SHELL_PROMPT
);
222 if (sh
->telnet
->can_output()) {
223 if (sh
->telnet
->output(str
) == -1) return -1; // connection was closed
231 /*---------------------------------------------------------------------------*/
233 { // add it to the kernels output stream
234 DEBUG_PRINTF("Shell: Adding stream to kernel streams\n");
235 THEKERNEL
->streams
->append_stream(pstream
);
236 telnet
->output("Smoothie command shell\r\n");
237 telnet
->output_prompt(SHELL_PROMPT
);
240 int Shell::queue_size()
242 return CommandQueue::getInstance()->size();
244 /*---------------------------------------------------------------------------*/
245 void Shell::input(char *cmd
)
247 if (parse(cmd
, parsetab
)) {
248 telnet
->output_prompt(SHELL_PROMPT
);
251 /*---------------------------------------------------------------------------*/
253 int Shell::output(const char *str
)
255 return telnet
->output(str
);
263 void Shell::setConsole()
268 Shell::Shell(Telnetd
*telnet
)
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);
279 DEBUG_PRINTF("Shell: Removing stream from kernel streams\n");
280 THEKERNEL
->streams
->remove_stream(pstream
);
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);