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