]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/UDP-Related-CLI-commands.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / UDP-Related-CLI-commands.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29  /******************************************************************************\r
30  *\r
31  * See the following URL for information on the commands defined in this file:\r
32  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
33  *\r
34  ******************************************************************************/\r
35 \r
36 \r
37 /* FreeRTOS includes. */\r
38 #include "FreeRTOS.h"\r
39 #include "task.h"\r
40 \r
41 /* Standard includes. */\r
42 #include <stdint.h>\r
43 #include <stdio.h>\r
44 #include <stdlib.h>\r
45 \r
46 /* FreeRTOS+CLI includes. */\r
47 #include "FreeRTOS_CLI.h"\r
48 \r
49 /* FreeRTOS+UDP includes, just to make the stats available to the CLI\r
50 commands. */\r
51 #include "FreeRTOS_UDP_IP.h"\r
52 #include "FreeRTOS_Sockets.h"\r
53 \r
54 /*\r
55  * Defines a command that prints out IP address information.\r
56  */\r
57 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
58 \r
59 /*\r
60  * Defines a command that prints out the gathered demo debug stats.\r
61  */\r
62 static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
63 \r
64 /*\r
65  * Defines a command that sends an ICMP ping request to an IP address.\r
66  */\r
67 static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
68 \r
69 /* Structure that defines the "ip-config" command line command. */\r
70 static const CLI_Command_Definition_t xIPConfig =\r
71 {\r
72         "ip-config",\r
73         "ip-config:\r\n Displays IP address configuration\r\n\r\n",\r
74         prvDisplayIPConfig,\r
75         0\r
76 };\r
77 \r
78 #if configINCLUDE_DEMO_DEBUG_STATS != 0\r
79         /* Structure that defines the "ip-debug-stats" command line command. */\r
80         static const CLI_Command_Definition_t xIPDebugStats =\r
81         {\r
82                 "ip-debug-stats", /* The command string to type. */\r
83                 "ip-debug-stats:\r\n Shows some IP stack stats useful for debug - an example only.\r\n\r\n",\r
84                 prvDisplayIPDebugStats, /* The function to run. */\r
85                 0 /* No parameters are expected. */\r
86         };\r
87 #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
88 \r
89 #if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
90 \r
91         /* Structure that defines the "ping" command line command.  This takes an IP\r
92         address or host name and (optionally) the number of bytes to ping as\r
93         parameters. */\r
94         static const CLI_Command_Definition_t xPing =\r
95         {\r
96                 "ping",\r
97                 "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
98                 prvPingCommand, /* The function to run. */\r
99                 -1 /* Ping can take either one or two parameter, so the number of parameters has to be determined by the ping command implementation. */\r
100         };\r
101 \r
102 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
103 \r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vRegisterUDPCLICommands( void )\r
108 {\r
109         /* Register all the command line commands defined immediately above. */\r
110         FreeRTOS_CLIRegisterCommand( &xIPConfig );\r
111 \r
112         #if configINCLUDE_DEMO_DEBUG_STATS == 1\r
113         {\r
114                 FreeRTOS_CLIRegisterCommand( &xIPDebugStats );\r
115         }\r
116         #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
117 \r
118         #if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
119         {\r
120                 FreeRTOS_CLIRegisterCommand( &xPing );\r
121         }\r
122         #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 #if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
127 \r
128         static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
129         {\r
130         char * pcParameter;\r
131         BaseType_t lParameterStringLength, xReturn;\r
132         uint32_t ulIPAddress, ulBytesToPing;\r
133         const uint32_t ulDefaultBytesToPing = 8UL;\r
134         char cBuffer[ 16 ];\r
135 \r
136                 /* Remove compile time warnings about unused parameters, and check the\r
137                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
138                 write buffer length is adequate, so does not check for buffer overflows. */\r
139                 ( void ) pcCommandString;\r
140                 ( void ) xWriteBufferLen;\r
141                 configASSERT( pcWriteBuffer );\r
142 \r
143                 /* Start with an empty string. */\r
144                 pcWriteBuffer[ 0 ] = 0x00;\r
145 \r
146                 /* Obtain the number of bytes to ping. */\r
147                 pcParameter = ( char * ) FreeRTOS_CLIGetParameter\r
148                                                                 (\r
149                                                                         pcCommandString,                /* The command string itself. */\r
150                                                                         2,                                              /* Return the second parameter. */\r
151                                                                         &lParameterStringLength /* Store the parameter string length. */\r
152                                                                 );\r
153 \r
154                 if( pcParameter == NULL )\r
155                 {\r
156                         /* The number of bytes was not specified, so default it. */\r
157                         ulBytesToPing = ulDefaultBytesToPing;\r
158                 }\r
159                 else\r
160                 {\r
161                         ulBytesToPing = atol( pcParameter );\r
162                 }\r
163 \r
164                 /* Obtain the IP address string. */\r
165                 pcParameter = ( char * ) FreeRTOS_CLIGetParameter\r
166                                                                 (\r
167                                                                         pcCommandString,                /* The command string itself. */\r
168                                                                         1,                                              /* Return the first parameter. */\r
169                                                                         &lParameterStringLength /* Store the parameter string length. */\r
170                                                                 );\r
171 \r
172                 /* Sanity check something was returned. */\r
173                 configASSERT( pcParameter );\r
174 \r
175                 /* Attempt to obtain the IP address.   If the first character is not a\r
176                 digit, assume the host name has been passed in. */\r
177                 if( ( *pcParameter >= '0' ) && ( *pcParameter <= '9' ) )\r
178                 {\r
179                         ulIPAddress = FreeRTOS_inet_addr( pcParameter );\r
180                 }\r
181                 else\r
182                 {\r
183                         /* Terminate the host name. */\r
184                         pcParameter[ lParameterStringLength ] = 0x00;\r
185 \r
186                         /* Attempt to resolve host. */\r
187                         ulIPAddress = FreeRTOS_gethostbyname( pcParameter );\r
188                 }\r
189 \r
190                 /* Convert IP address, which may have come from a DNS lookup, to string. */\r
191                 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );\r
192 \r
193                 if( ulIPAddress != 0 )\r
194                 {\r
195                         xReturn = FreeRTOS_SendPingRequest( ulIPAddress, ( uint16_t ) ulBytesToPing, portMAX_DELAY );\r
196                 }\r
197                 else\r
198                 {\r
199                         xReturn = pdFALSE;\r
200                 }\r
201 \r
202                 if( xReturn == pdFALSE )\r
203                 {\r
204                         sprintf( pcWriteBuffer, "%s", "Could not send ping request\r\n" );\r
205                 }\r
206                 else\r
207                 {\r
208                         sprintf( pcWriteBuffer, "Ping sent to %s with identifier %d\r\n", cBuffer, ( int ) xReturn );\r
209                 }\r
210 \r
211                 return pdFALSE;\r
212         }\r
213         /*-----------------------------------------------------------*/\r
214 \r
215 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
216 \r
217 #if configINCLUDE_DEMO_DEBUG_STATS != 0\r
218 \r
219         static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
220         {\r
221         static BaseType_t xIndex = -1;\r
222         extern xExampleDebugStatEntry_t xIPTraceValues[];\r
223         BaseType_t xReturn;\r
224 \r
225                 /* Remove compile time warnings about unused parameters, and check the\r
226                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
227                 write buffer length is adequate, so does not check for buffer overflows. */\r
228                 ( void ) pcCommandString;\r
229                 ( void ) xWriteBufferLen;\r
230                 configASSERT( pcWriteBuffer );\r
231 \r
232                 xIndex++;\r
233 \r
234                 if( xIndex < xExampleDebugStatEntries() )\r
235                 {\r
236                         sprintf( pcWriteBuffer, "%s %d\r\n", ( char * ) xIPTraceValues[ xIndex ].pucDescription, ( int ) xIPTraceValues[ xIndex ].ulData );\r
237                         xReturn = pdPASS;\r
238                 }\r
239                 else\r
240                 {\r
241                         /* Reset the index for the next time it is called. */\r
242                         xIndex = -1;\r
243 \r
244                         /* Ensure nothing remains in the write buffer. */\r
245                         pcWriteBuffer[ 0 ] = 0x00;\r
246                         xReturn = pdFALSE;\r
247                 }\r
248 \r
249                 return xReturn;\r
250         }\r
251         /*-----------------------------------------------------------*/\r
252 \r
253 #endif /* configINCLUDE_DEMO_DEBUG_STATS */\r
254 \r
255 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
256 {\r
257 static BaseType_t xIndex = 0;\r
258 BaseType_t xReturn;\r
259 uint32_t ulAddress;\r
260 \r
261         /* Remove compile time warnings about unused parameters, and check the\r
262         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
263         write buffer length is adequate, so does not check for buffer overflows. */\r
264         ( void ) pcCommandString;\r
265         ( void ) xWriteBufferLen;\r
266         configASSERT( pcWriteBuffer );\r
267 \r
268         switch( xIndex )\r
269         {\r
270                 case 0 :\r
271                         FreeRTOS_GetAddressConfiguration( &ulAddress, NULL, NULL, NULL );\r
272                         sprintf( pcWriteBuffer, "\r\nIP address " );\r
273                         xReturn = pdTRUE;\r
274                         xIndex++;\r
275                         break;\r
276 \r
277                 case 1 :\r
278                         FreeRTOS_GetAddressConfiguration( NULL, &ulAddress, NULL, NULL );\r
279                         sprintf( pcWriteBuffer, "\r\nNet mask " );\r
280                         xReturn = pdTRUE;\r
281                         xIndex++;\r
282                         break;\r
283 \r
284                 case 2 :\r
285                         FreeRTOS_GetAddressConfiguration( NULL, NULL, &ulAddress, NULL );\r
286                         sprintf( pcWriteBuffer, "\r\nGateway address " );\r
287                         xReturn = pdTRUE;\r
288                         xIndex++;\r
289                         break;\r
290 \r
291                 case 3 :\r
292                         FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulAddress );\r
293                         sprintf( pcWriteBuffer, "\r\nDNS server address " );\r
294                         xReturn = pdTRUE;\r
295                         xIndex++;\r
296                         break;\r
297 \r
298                 default :\r
299                         ulAddress = 0;\r
300                         sprintf( pcWriteBuffer, "\r\n\r\n" );\r
301                         xReturn = pdFALSE;\r
302                         xIndex = 0;\r
303                         break;\r
304         }\r
305 \r
306         if( ulAddress != 0 )\r
307         {\r
308                 FreeRTOS_inet_ntoa( ulAddress, ( &( pcWriteBuffer[ strlen( pcWriteBuffer ) ] ) ) );\r
309         }\r
310 \r
311         return xReturn;\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r