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