]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/Common/FreeRTOS_Plus_CLI_Demos/TCP-related-CLI-commands.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / Common / FreeRTOS_Plus_CLI_Demos / TCP-related-CLI-commands.c
1 /*\r
2     FreeRTOS V9.0.0 - 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 /* FreeRTOS includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 /* Standard includes. */\r
75 #include <stdint.h>\r
76 #include <stdio.h>\r
77 #include <stdlib.h>\r
78 \r
79 /* FreeRTOS+CLI includes. */\r
80 #include "FreeRTOS_CLI.h"\r
81 \r
82 /* FreeRTOS+TCP includes, just to make the stats available to the CLI\r
83 commands. */\r
84 #include "FreeRTOS_IP.h"\r
85 #include "FreeRTOS_Sockets.h"\r
86 \r
87 /*\r
88  * Defines a command that prints out IP address information.\r
89  */\r
90 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
91 \r
92 /*\r
93  * Defines a command that prints out the gathered demo debug stats.\r
94  */\r
95 #if( configINCLUDE_DEMO_DEBUG_STATS != 0 )\r
96         static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
97 #endif\r
98 \r
99 /*\r
100  * Defines a command that sends an ICMP ping request to an IP address.\r
101  */\r
102 #if( ipconfigSUPPORT_OUTGOING_PINGS == 1 )\r
103         static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
104 #endif\r
105 \r
106 /*\r
107  * Defines a command that calls FreeRTOS_netstat().\r
108  */\r
109 static BaseType_t prvNetStatCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
110 \r
111 /* Structure that defines the "ip-config" command line command. */\r
112 static const CLI_Command_Definition_t xIPConfig =\r
113 {\r
114         "ip-config",\r
115         "\r\nip-config:\r\n Displays IP address configuration\r\n",\r
116         prvDisplayIPConfig,\r
117         0\r
118 };\r
119 \r
120 #if( configINCLUDE_DEMO_DEBUG_STATS != 0 )\r
121         /* Structure that defines the "ip-debug-stats" command line command. */\r
122         static const CLI_Command_Definition_t xIPDebugStats =\r
123         {\r
124                 "ip-debug-stats", /* The command string to type. */\r
125                 "\r\nip-debug-stats:\r\n Shows some IP stack stats useful for debug - an example only.\r\n",\r
126                 prvDisplayIPDebugStats, /* The function to run. */\r
127                 0 /* No parameters are expected. */\r
128         };\r
129 #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
130 \r
131 #if( ipconfigSUPPORT_OUTGOING_PINGS == 1 )\r
132 \r
133         /* Structure that defines the "ping" command line command.  This takes an IP\r
134         address or host name and (optionally) the number of bytes to ping as\r
135         parameters. */\r
136         static const CLI_Command_Definition_t xPing =\r
137         {\r
138                 "ping",\r
139                 "\r\nping <ipaddress> <optional:bytes to send>:\r\n for example, ping 192.168.0.3 8, or ping www.example.com\r\n",\r
140                 prvPingCommand, /* The function to run. */\r
141                 -1 /* Ping can take either one or two parameter, so the number of parameters has to be determined by the ping command implementation. */\r
142         };\r
143 \r
144 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
145 \r
146 /* Structure that defines the "task-stats" command line command.  This generates\r
147 a table that gives information on each task in the system. */\r
148 static const CLI_Command_Definition_t xNetStats =\r
149 {\r
150         "net-stats", /* The command string to type. */\r
151         "\r\nnet-stats:\r\n Calls FreeRTOS_netstat()\r\n",\r
152         prvNetStatCommand, /* The function to run. */\r
153         0 /* No parameters are expected. */\r
154 };\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void vRegisterTCPCLICommands( void )\r
159 {\r
160         /* Register all the command line commands defined immediately above. */\r
161         FreeRTOS_CLIRegisterCommand( &xIPConfig );\r
162 \r
163         #if( configINCLUDE_DEMO_DEBUG_STATS == 1 )\r
164         {\r
165                 FreeRTOS_CLIRegisterCommand( &xIPDebugStats );\r
166         }\r
167         #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
168 \r
169         #if( ipconfigSUPPORT_OUTGOING_PINGS == 1 )\r
170         {\r
171                 FreeRTOS_CLIRegisterCommand( &xPing );\r
172         }\r
173         #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
174 \r
175         FreeRTOS_CLIRegisterCommand( &xNetStats );\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 #if( ipconfigSUPPORT_OUTGOING_PINGS == 1 )\r
180 \r
181         static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
182         {\r
183         char * pcParameter;\r
184         BaseType_t lParameterStringLength, xReturn = pdFALSE;\r
185         uint32_t ulIPAddress, ulBytesToPing;\r
186         const uint32_t ulDefaultBytesToPing = 8UL;\r
187         char cBuffer[ 16 ];\r
188 \r
189                 /* Remove compile time warnings about unused parameters, and check the\r
190                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
191                 write buffer length is adequate, so does not check for buffer overflows. */\r
192                 ( void ) pcCommandString;\r
193                 ( void ) xWriteBufferLen;\r
194                 configASSERT( pcWriteBuffer );\r
195 \r
196                 /* Start with an empty string. */\r
197                 pcWriteBuffer[ 0 ] = 0x00;\r
198 \r
199                 /* Obtain the number of bytes to ping. */\r
200                 pcParameter = ( char * ) FreeRTOS_CLIGetParameter\r
201                                                                 (\r
202                                                                         pcCommandString,                /* The command string itself. */\r
203                                                                         2,                                              /* Return the second parameter. */\r
204                                                                         &lParameterStringLength /* Store the parameter string length. */\r
205                                                                 );\r
206 \r
207                 if( pcParameter == NULL )\r
208                 {\r
209                         /* The number of bytes was not specified, so default it. */\r
210                         ulBytesToPing = ulDefaultBytesToPing;\r
211                 }\r
212                 else\r
213                 {\r
214                         ulBytesToPing = atol( pcParameter );\r
215                 }\r
216 \r
217                 /* Obtain the IP address string. */\r
218                 pcParameter = ( char * ) FreeRTOS_CLIGetParameter\r
219                                                                 (\r
220                                                                         pcCommandString,                /* The command string itself. */\r
221                                                                         1,                                              /* Return the first parameter. */\r
222                                                                         &lParameterStringLength /* Store the parameter string length. */\r
223                                                                 );\r
224 \r
225                 if( pcParameter != NULL )\r
226                 {\r
227                         /* Terminate the host name or IP address string. */\r
228                         pcParameter[ lParameterStringLength ] = 0x00;\r
229 \r
230                         /* Attempt to obtain the IP address.   If the first character is not a\r
231                         digit, assume the host name has been passed in. */\r
232                         if( ( *pcParameter >= '0' ) && ( *pcParameter <= '9' ) )\r
233                         {\r
234                                 ulIPAddress = FreeRTOS_inet_addr( pcParameter );\r
235                         }\r
236                         else\r
237                         {\r
238                                 /* Attempt to resolve host. */\r
239                                 ulIPAddress = FreeRTOS_gethostbyname( pcParameter );\r
240                         }\r
241 \r
242                         /* Convert IP address, which may have come from a DNS lookup, to string. */\r
243                         FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );\r
244 \r
245                         if( ulIPAddress != 0 )\r
246                         {\r
247                                 xReturn = FreeRTOS_SendPingRequest( ulIPAddress, ( uint16_t ) ulBytesToPing, portMAX_DELAY );\r
248                         }\r
249                 }\r
250 \r
251                 if( xReturn == pdFALSE )\r
252                 {\r
253                         sprintf( pcWriteBuffer, "%s", "Could not send ping request\r\n" );\r
254                 }\r
255                 else\r
256                 {\r
257                         sprintf( pcWriteBuffer, "Ping sent to %s with identifier %d\r\n", cBuffer, ( int ) xReturn );\r
258                 }\r
259 \r
260                 return pdFALSE;\r
261         }\r
262         /*-----------------------------------------------------------*/\r
263 \r
264 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
265 \r
266 #if( configINCLUDE_DEMO_DEBUG_STATS != 0 )\r
267 \r
268         static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
269         {\r
270         static BaseType_t xIndex = -1;\r
271         extern ExampleDebugStatEntry_t xIPTraceValues[];\r
272         BaseType_t xReturn;\r
273 \r
274                 /* Remove compile time warnings about unused parameters, and check the\r
275                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
276                 write buffer length is adequate, so does not check for buffer overflows. */\r
277                 ( void ) pcCommandString;\r
278                 ( void ) xWriteBufferLen;\r
279                 configASSERT( pcWriteBuffer );\r
280 \r
281                 xIndex++;\r
282 \r
283                 if( xIndex < xExampleDebugStatEntries() )\r
284                 {\r
285                         sprintf( pcWriteBuffer, "%s %d\r\n", ( char * ) xIPTraceValues[ xIndex ].pucDescription, ( int ) xIPTraceValues[ xIndex ].ulData );\r
286                         xReturn = pdPASS;\r
287                 }\r
288                 else\r
289                 {\r
290                         /* Reset the index for the next time it is called. */\r
291                         xIndex = -1;\r
292 \r
293                         /* Ensure nothing remains in the write buffer. */\r
294                         pcWriteBuffer[ 0 ] = 0x00;\r
295                         xReturn = pdFALSE;\r
296                 }\r
297 \r
298                 return xReturn;\r
299         }\r
300         /*-----------------------------------------------------------*/\r
301 \r
302 #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
303 \r
304 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
305 {\r
306 static BaseType_t xIndex = 0;\r
307 BaseType_t xReturn;\r
308 uint32_t ulAddress;\r
309 \r
310         /* Remove compile time warnings about unused parameters, and check the\r
311         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
312         write buffer length is adequate, so does not check for buffer overflows. */\r
313         ( void ) pcCommandString;\r
314         ( void ) xWriteBufferLen;\r
315         configASSERT( pcWriteBuffer );\r
316 \r
317         switch( xIndex )\r
318         {\r
319                 case 0 :\r
320                         FreeRTOS_GetAddressConfiguration( &ulAddress, NULL, NULL, NULL );\r
321                         sprintf( pcWriteBuffer, "\r\nIP address " );\r
322                         xReturn = pdTRUE;\r
323                         xIndex++;\r
324                         break;\r
325 \r
326                 case 1 :\r
327                         FreeRTOS_GetAddressConfiguration( NULL, &ulAddress, NULL, NULL );\r
328                         sprintf( pcWriteBuffer, "\r\nNet mask " );\r
329                         xReturn = pdTRUE;\r
330                         xIndex++;\r
331                         break;\r
332 \r
333                 case 2 :\r
334                         FreeRTOS_GetAddressConfiguration( NULL, NULL, &ulAddress, NULL );\r
335                         sprintf( pcWriteBuffer, "\r\nGateway address " );\r
336                         xReturn = pdTRUE;\r
337                         xIndex++;\r
338                         break;\r
339 \r
340                 case 3 :\r
341                         FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulAddress );\r
342                         sprintf( pcWriteBuffer, "\r\nDNS server address " );\r
343                         xReturn = pdTRUE;\r
344                         xIndex++;\r
345                         break;\r
346 \r
347                 default :\r
348                         ulAddress = 0;\r
349                         sprintf( pcWriteBuffer, "\r\n\r\n" );\r
350                         xReturn = pdFALSE;\r
351                         xIndex = 0;\r
352                         break;\r
353         }\r
354 \r
355         if( ulAddress != 0 )\r
356         {\r
357                 FreeRTOS_inet_ntoa( ulAddress, ( &( pcWriteBuffer[ strlen( pcWriteBuffer ) ] ) ) );\r
358         }\r
359 \r
360         return xReturn;\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 static BaseType_t prvNetStatCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
365 {\r
366         ( void ) pcWriteBuffer;\r
367         ( void ) xWriteBufferLen;\r
368         ( void ) pcCommandString;\r
369 \r
370         FreeRTOS_netstat();\r
371         snprintf( pcWriteBuffer, xWriteBufferLen, "FreeRTOS_netstat() called - output uses FreeRTOS_printf\r\n" );\r
372         return pdFALSE;\r
373 }\r
374 \r