]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/UDP-Related-CLI-commands.c
6be8639b5830ba987c53b70398f7aa70e97151f8
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / UDP-Related-CLI-commands.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66  /******************************************************************************\r
67  *\r
68  * See the following URL for information on the commands defined in this file:\r
69  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
70  *\r
71  ******************************************************************************/\r
72 \r
73 \r
74 /* FreeRTOS includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 \r
78 /* Standard includes. */\r
79 #include <stdint.h>\r
80 #include <stdio.h>\r
81 #include <stdlib.h>\r
82 \r
83 /* FreeRTOS+CLI includes. */\r
84 #include "FreeRTOS_CLI.h"\r
85 \r
86 /* FreeRTOS+UDP includes, just to make the stats available to the CLI\r
87 commands. */\r
88 #include "FreeRTOS_UDP_IP.h"\r
89 #include "FreeRTOS_Sockets.h"\r
90 \r
91 /*\r
92  * Defines a command that prints out IP address information.\r
93  */\r
94 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
95 \r
96 /*\r
97  * Defines a command that prints out the gathered demo debug stats.\r
98  */\r
99 static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
100 \r
101 /*\r
102  * Defines a command that sends an ICMP ping request to an IP address.\r
103  */\r
104 static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
105 \r
106 /* Structure that defines the "ip-config" command line command. */\r
107 static const CLI_Command_Definition_t xIPConfig =\r
108 {\r
109         "ip-config",\r
110         "ip-config:\r\n Displays IP address configuration\r\n\r\n",\r
111         prvDisplayIPConfig,\r
112         0\r
113 };\r
114 \r
115 #if configINCLUDE_DEMO_DEBUG_STATS != 0\r
116         /* Structure that defines the "ip-debug-stats" command line command. */\r
117         static const CLI_Command_Definition_t xIPDebugStats =\r
118         {\r
119                 "ip-debug-stats", /* The command string to type. */\r
120                 "ip-debug-stats:\r\n Shows some IP stack stats useful for debug - an example only.\r\n\r\n",\r
121                 prvDisplayIPDebugStats, /* The function to run. */\r
122                 0 /* No parameters are expected. */\r
123         };\r
124 #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
125 \r
126 #if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
127 \r
128         /* Structure that defines the "ping" command line command.  This takes an IP\r
129         address or host name and (optionally) the number of bytes to ping as\r
130         parameters. */\r
131         static const CLI_Command_Definition_t xPing =\r
132         {\r
133                 "ping",\r
134                 "ping <ipaddress> <optional:bytes to send>:\r\n for example, ping 192.168.0.3 8, or ping www.example.com\r\n\r\n",\r
135                 prvPingCommand, /* The function to run. */\r
136                 -1 /* Ping can take either one or two parameter, so the number of parameters has to be determined by the ping command implementation. */\r
137         };\r
138 \r
139 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
140 \r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 void vRegisterUDPCLICommands( void )\r
145 {\r
146         /* Register all the command line commands defined immediately above. */\r
147         FreeRTOS_CLIRegisterCommand( &xIPConfig );\r
148 \r
149         #if configINCLUDE_DEMO_DEBUG_STATS == 1\r
150         {\r
151                 FreeRTOS_CLIRegisterCommand( &xIPDebugStats );\r
152         }\r
153         #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
154 \r
155         #if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
156         {\r
157                 FreeRTOS_CLIRegisterCommand( &xPing );\r
158         }\r
159         #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 #if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
164 \r
165         static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
166         {\r
167         char * pcParameter;\r
168         BaseType_t lParameterStringLength, xReturn;\r
169         uint32_t ulIPAddress, ulBytesToPing;\r
170         const uint32_t ulDefaultBytesToPing = 8UL;\r
171         char cBuffer[ 16 ];\r
172 \r
173                 /* Remove compile time warnings about unused parameters, and check the\r
174                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
175                 write buffer length is adequate, so does not check for buffer overflows. */\r
176                 ( void ) pcCommandString;\r
177                 ( void ) xWriteBufferLen;\r
178                 configASSERT( pcWriteBuffer );\r
179 \r
180                 /* Start with an empty string. */\r
181                 pcWriteBuffer[ 0 ] = 0x00;\r
182 \r
183                 /* Obtain the number of bytes to ping. */\r
184                 pcParameter = ( char * ) FreeRTOS_CLIGetParameter\r
185                                                                 (\r
186                                                                         pcCommandString,                /* The command string itself. */\r
187                                                                         2,                                              /* Return the second parameter. */\r
188                                                                         &lParameterStringLength /* Store the parameter string length. */\r
189                                                                 );\r
190 \r
191                 if( pcParameter == NULL )\r
192                 {\r
193                         /* The number of bytes was not specified, so default it. */\r
194                         ulBytesToPing = ulDefaultBytesToPing;\r
195                 }\r
196                 else\r
197                 {\r
198                         ulBytesToPing = atol( pcParameter );\r
199                 }\r
200 \r
201                 /* Obtain the IP address string. */\r
202                 pcParameter = ( char * ) FreeRTOS_CLIGetParameter\r
203                                                                 (\r
204                                                                         pcCommandString,                /* The command string itself. */\r
205                                                                         1,                                              /* Return the first parameter. */\r
206                                                                         &lParameterStringLength /* Store the parameter string length. */\r
207                                                                 );\r
208 \r
209                 /* Sanity check something was returned. */\r
210                 configASSERT( pcParameter );\r
211 \r
212                 /* Attempt to obtain the IP address.   If the first character is not a\r
213                 digit, assume the host name has been passed in. */\r
214                 if( ( *pcParameter >= '0' ) && ( *pcParameter <= '9' ) )\r
215                 {\r
216                         ulIPAddress = FreeRTOS_inet_addr( pcParameter );\r
217                 }\r
218                 else\r
219                 {\r
220                         /* Terminate the host name. */\r
221                         pcParameter[ lParameterStringLength ] = 0x00;\r
222 \r
223                         /* Attempt to resolve host. */\r
224                         ulIPAddress = FreeRTOS_gethostbyname( pcParameter );\r
225                 }\r
226 \r
227                 /* Convert IP address, which may have come from a DNS lookup, to string. */\r
228                 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );\r
229 \r
230                 if( ulIPAddress != 0 )\r
231                 {\r
232                         xReturn = FreeRTOS_SendPingRequest( ulIPAddress, ( uint16_t ) ulBytesToPing, portMAX_DELAY );\r
233                 }\r
234                 else\r
235                 {\r
236                         xReturn = pdFALSE;\r
237                 }\r
238 \r
239                 if( xReturn == pdFALSE )\r
240                 {\r
241                         sprintf( pcWriteBuffer, "%s", "Could not send ping request\r\n" );\r
242                 }\r
243                 else\r
244                 {\r
245                         sprintf( pcWriteBuffer, "Ping sent to %s with identifier %d\r\n", cBuffer, ( int ) xReturn );\r
246                 }\r
247 \r
248                 return pdFALSE;\r
249         }\r
250         /*-----------------------------------------------------------*/\r
251 \r
252 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
253 \r
254 #if configINCLUDE_DEMO_DEBUG_STATS != 0\r
255 \r
256         static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
257         {\r
258         static BaseType_t xIndex = -1;\r
259         extern xExampleDebugStatEntry_t xIPTraceValues[];\r
260         BaseType_t xReturn;\r
261 \r
262                 /* Remove compile time warnings about unused parameters, and check the\r
263                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
264                 write buffer length is adequate, so does not check for buffer overflows. */\r
265                 ( void ) pcCommandString;\r
266                 ( void ) xWriteBufferLen;\r
267                 configASSERT( pcWriteBuffer );\r
268 \r
269                 xIndex++;\r
270 \r
271                 if( xIndex < xExampleDebugStatEntries() )\r
272                 {\r
273                         sprintf( pcWriteBuffer, "%s %d\r\n", ( char * ) xIPTraceValues[ xIndex ].pucDescription, ( int ) xIPTraceValues[ xIndex ].ulData );\r
274                         xReturn = pdPASS;\r
275                 }\r
276                 else\r
277                 {\r
278                         /* Reset the index for the next time it is called. */\r
279                         xIndex = -1;\r
280 \r
281                         /* Ensure nothing remains in the write buffer. */\r
282                         pcWriteBuffer[ 0 ] = 0x00;\r
283                         xReturn = pdFALSE;\r
284                 }\r
285 \r
286                 return xReturn;\r
287         }\r
288         /*-----------------------------------------------------------*/\r
289 \r
290 #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
291 \r
292 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
293 {\r
294 static BaseType_t xIndex = 0;\r
295 BaseType_t xReturn;\r
296 uint32_t ulAddress;\r
297 \r
298         /* Remove compile time warnings about unused parameters, and check the\r
299         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
300         write buffer length is adequate, so does not check for buffer overflows. */\r
301         ( void ) pcCommandString;\r
302         ( void ) xWriteBufferLen;\r
303         configASSERT( pcWriteBuffer );\r
304 \r
305         switch( xIndex )\r
306         {\r
307                 case 0 :\r
308                         FreeRTOS_GetAddressConfiguration( &ulAddress, NULL, NULL, NULL );\r
309                         sprintf( pcWriteBuffer, "\r\nIP address " );\r
310                         xReturn = pdTRUE;\r
311                         xIndex++;\r
312                         break;\r
313 \r
314                 case 1 :\r
315                         FreeRTOS_GetAddressConfiguration( NULL, &ulAddress, NULL, NULL );\r
316                         sprintf( pcWriteBuffer, "\r\nNet mask " );\r
317                         xReturn = pdTRUE;\r
318                         xIndex++;\r
319                         break;\r
320 \r
321                 case 2 :\r
322                         FreeRTOS_GetAddressConfiguration( NULL, NULL, &ulAddress, NULL );\r
323                         sprintf( pcWriteBuffer, "\r\nGateway address " );\r
324                         xReturn = pdTRUE;\r
325                         xIndex++;\r
326                         break;\r
327 \r
328                 case 3 :\r
329                         FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulAddress );\r
330                         sprintf( pcWriteBuffer, "\r\nDNS server address " );\r
331                         xReturn = pdTRUE;\r
332                         xIndex++;\r
333                         break;\r
334 \r
335                 default :\r
336                         ulAddress = 0;\r
337                         sprintf( pcWriteBuffer, "\r\n\r\n" );\r
338                         xReturn = pdFALSE;\r
339                         xIndex = 0;\r
340                         break;\r
341         }\r
342 \r
343         if( ulAddress != 0 )\r
344         {\r
345                 FreeRTOS_inet_ntoa( ulAddress, ( &( pcWriteBuffer[ strlen( pcWriteBuffer ) ] ) ) );\r
346         }\r
347 \r
348         return xReturn;\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r