]> git.sur5r.net Git - freertos/blob - Demo/uIP_Demo_Rowley_ARM7/uip/telnetd-shell.c
Renamed the CORTEX_M4_ATSAM4S_AVR_Studio directory to the correct CORTEX_M4_ATSAM4S_A...
[freertos] / Demo / uIP_Demo_Rowley_ARM7 / uip / telnetd-shell.c
1 /**\r
2  * \addtogroup telnetd\r
3  * @{\r
4  */\r
5 \r
6 /**\r
7  * \file\r
8  * An example telnet server shell\r
9  * \author Adam Dunkels <adam@dunkels.com>\r
10  */\r
11 \r
12 /*\r
13  * Copyright (c) 2003, Adam Dunkels.\r
14  * All rights reserved. \r
15  *\r
16  * Redistribution and use in source and binary forms, with or without \r
17  * modification, are permitted provided that the following conditions \r
18  * are met: \r
19  * 1. Redistributions of source code must retain the above copyright \r
20  *    notice, this list of conditions and the following disclaimer. \r
21  * 2. Redistributions in binary form must reproduce the above copyright \r
22  *    notice, this list of conditions and the following disclaimer in the \r
23  *    documentation and/or other materials provided with the distribution. \r
24  * 3. The name of the author may not be used to endorse or promote\r
25  *    products derived from this software without specific prior\r
26  *    written permission.  \r
27  *\r
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
29  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
30  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
34  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
36  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  \r
39  *\r
40  * This file is part of the Contiki desktop OS.\r
41  *\r
42  * $Id: telnetd-shell.c,v 1.1.2.1 2003/10/06 22:56:22 adam Exp $\r
43  *\r
44  */\r
45 \r
46 #include "uip.h"\r
47 #include "telnetd.h"\r
48 #include <string.h>\r
49 \r
50 struct ptentry {\r
51   char c;\r
52   void (* pfunc)(struct telnetd_state *s, char *str);\r
53 };\r
54 \r
55 /*-----------------------------------------------------------------------------------*/\r
56 static void\r
57 parse(struct telnetd_state *s, register char *str, struct ptentry *t)\r
58 {\r
59   register struct ptentry *p;\r
60   char *sstr;\r
61 \r
62   sstr = str;\r
63   \r
64   /* Loop over the parse table entries in t in order to find one that\r
65      matches the first character in str. */\r
66   for(p = t; p->c != 0; ++p) {\r
67     if(*str == p->c) {\r
68       /* Skip rest of the characters up to the first space. */\r
69       while(*str != ' ') {\r
70         ++str;\r
71       }\r
72 \r
73       /* Skip all spaces.*/\r
74       while(*str == ' ') {\r
75         ++str;\r
76       }\r
77 \r
78       /* Call parse table entry function and return. */\r
79       p->pfunc(s, str);\r
80       return;\r
81     }\r
82   }\r
83 \r
84   /* Did not find matching entry in parse table. We just call the\r
85      default handler supplied by the caller and return. */\r
86   p->pfunc(s, str);\r
87 }\r
88 /*-----------------------------------------------------------------------------------*/\r
89 static void\r
90 exitt(struct telnetd_state *s, char *str)\r
91 {\r
92   telnetd_close(s);\r
93 }\r
94 /*-----------------------------------------------------------------------------------*/\r
95 static void\r
96 inttostr(register char *str, unsigned int i)\r
97 {\r
98   str[0] = '0' + i / 100;\r
99   if(str[0] == '0') {\r
100     str[0] = ' ';\r
101   }\r
102   str[1] = '0' + (i / 10) % 10;\r
103   if(str[1] == '0') {\r
104     str[1] = ' ';\r
105   }\r
106   str[2] = '0' + i % 10;\r
107   str[3] = ' ';\r
108   str[4] = 0;\r
109 }\r
110 /*-----------------------------------------------------------------------------------*/\r
111 static void\r
112 stats(struct telnetd_state *s, char *strr)\r
113 {\r
114   char str[10];\r
115 \r
116   inttostr(str, uip_stat.ip.recv);\r
117   telnetd_output(s, "IP packets received ", str);\r
118   inttostr(str, uip_stat.ip.sent);\r
119   telnetd_output(s, "IP packets sent ", str);\r
120   inttostr(str, uip_stat.ip.drop);\r
121   telnetd_output(s, "IP packets dropped ", str);\r
122 \r
123   inttostr(str, uip_stat.icmp.recv);\r
124   telnetd_output(s, "ICMP packets received ", str);\r
125   inttostr(str, uip_stat.icmp.sent);\r
126   telnetd_output(s, "ICMP packets sent ", str);\r
127   inttostr(str, uip_stat.icmp.drop);\r
128   telnetd_output(s, "ICMP packets dropped ", str);\r
129 \r
130   inttostr(str, uip_stat.tcp.recv);\r
131   telnetd_output(s, "TCP packets received ", str);\r
132   inttostr(str, uip_stat.tcp.sent);\r
133   telnetd_output(s, "TCP packets sent ", str);\r
134   inttostr(str, uip_stat.tcp.drop);\r
135   telnetd_output(s, "TCP packets dropped ", str);\r
136   inttostr(str, uip_stat.tcp.rexmit);\r
137   telnetd_output(s, "TCP packets retransmitted ", str);\r
138   inttostr(str, uip_stat.tcp.synrst);\r
139   telnetd_output(s, "TCP connection attempts ", str);\r
140 }\r
141 /*-----------------------------------------------------------------------------------*/\r
142 static void\r
143 help(struct telnetd_state *s, char *str)\r
144 {\r
145   telnetd_output(s, "Available commands:", "");\r
146   telnetd_output(s, "stats - show uIP statistics", "");\r
147   telnetd_output(s, "exit  - exit shell", "");  \r
148   telnetd_output(s, "?     - show this help", "");        \r
149 }\r
150 /*-----------------------------------------------------------------------------------*/\r
151 static void\r
152 none(struct telnetd_state *s, char *str)\r
153 {\r
154   if(strlen(str) > 0) {\r
155     telnetd_output(s, "Unknown command", "");\r
156   }\r
157 }\r
158 /*-----------------------------------------------------------------------------------*/\r
159 static struct ptentry configparsetab[] =\r
160   {{'s', stats},\r
161    {'e', exitt},\r
162    {'?', help},\r
163 \r
164    /* Default action */\r
165    {0, none}};\r
166 /*-----------------------------------------------------------------------------------*/\r
167 void\r
168 telnetd_connected(struct telnetd_state *s)\r
169 {\r
170   telnetd_output(s, "uIP command shell", "");\r
171   telnetd_output(s, "Type '?' for help", "");  \r
172   telnetd_prompt(s, "uIP-0.9> "); \r
173 }\r
174 /*-----------------------------------------------------------------------------------*/\r
175 void\r
176 telnetd_input(struct telnetd_state *s, char *cmd)\r
177 {\r
178   parse(s, cmd, configparsetab);\r
179   telnetd_prompt(s, "uIP-0.9> "); \r
180 }\r
181 /*-----------------------------------------------------------------------------------*/\r