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