Commit | Line | Data |
---|---|---|
d4ee6ee2 JM |
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 | ||
61134a65 | 35 | #include "Kernel.h" |
d4ee6ee2 JM |
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" | |
61134a65 | 46 | #include "StreamOutputPool.h" |
0d9c5e2a | 47 | #include "CommandQueue.h" |
d4ee6ee2 JM |
48 | |
49 | //#define DEBUG_PRINTF(...) | |
50 | #define DEBUG_PRINTF printf | |
51 | ||
52 | struct ptentry { | |
7e81f138 | 53 | const char *command; |
d4ee6ee2 JM |
54 | void (* pfunc)(char *str, Shell *sh); |
55 | }; | |
56 | ||
57 | #define SHELL_PROMPT "> " | |
58 | ||
59 | /*---------------------------------------------------------------------------*/ | |
7e81f138 | 60 | bool Shell::parse(register char *str, const struct ptentry *t) |
d4ee6ee2 | 61 | { |
7e81f138 JM |
62 | const struct ptentry *p; |
63 | for (p = t; p->command != 0; ++p) { | |
64 | if (strncasecmp(str, p->command, strlen(p->command)) == 0) { | |
d4ee6ee2 JM |
65 | break; |
66 | } | |
67 | } | |
68 | ||
69 | p->pfunc(str, this); | |
70 | ||
7e81f138 | 71 | return p->command != 0; |
d4ee6ee2 JM |
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"); | |
7cbf5b2c | 78 | sh->output("h - show network help\n"); |
d4ee6ee2 JM |
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" | |
7cbf5b2c | 129 | static void ntest(char *str, Shell *sh) |
d4ee6ee2 JM |
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 | /*---------------------------------------------------------------------------*/ | |
7e81f138 JM |
189 | static const struct ptentry parsetab[] = { |
190 | {"netstat", connections}, | |
191 | {"exit", quit}, | |
192 | {"quit", quit}, | |
7cbf5b2c JM |
193 | {"ntest", ntest}, |
194 | {"h", help}, | |
d4ee6ee2 JM |
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() | |
bc53ce10 JM |
226 | { // add it to the kernels output stream |
227 | DEBUG_PRINTF("Shell: Adding stream to kernel streams\n"); | |
228 | THEKERNEL->streams->append_stream(pstream); | |
48a0fdb6 JM |
229 | telnet->output("Smoothie command shell\r\n"); |
230 | telnet->output_prompt(SHELL_PROMPT); | |
d4ee6ee2 JM |
231 | } |
232 | ||
233 | int Shell::queue_size() | |
234 | { | |
235 | return CommandQueue::getInstance()->size(); | |
236 | } | |
237 | /*---------------------------------------------------------------------------*/ | |
238 | void Shell::input(char *cmd) | |
239 | { | |
240 | if (parse(cmd, parsetab)) { | |
241 | telnet->output_prompt(SHELL_PROMPT); | |
242 | } | |
243 | } | |
244 | /*---------------------------------------------------------------------------*/ | |
245 | ||
246 | int Shell::output(const char *str) | |
247 | { | |
248 | return telnet->output(str); | |
249 | } | |
250 | ||
251 | void Shell::close() | |
252 | { | |
253 | telnet->close(); | |
254 | } | |
255 | ||
256 | void Shell::setConsole() | |
257 | { | |
d4ee6ee2 JM |
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 | { | |
bc53ce10 JM |
272 | DEBUG_PRINTF("Shell: Removing stream from kernel streams\n"); |
273 | THEKERNEL->streams->remove_stream(pstream); | |
274 | ||
d4ee6ee2 JM |
275 | // we cannot delete this stream until it is no longer in any command queue entries |
276 | // so mark it as closed, and allow it to delete itself when it is no longer being used | |
277 | static_cast<CallbackStream*>(pstream)->mark_closed(); // mark the stream as closed so we do not get any callbacks | |
278 | DEBUG_PRINTF("Shell: dtor %p\n", this); | |
279 | } |