2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /******************************************************************************
\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
33 ******************************************************************************/
\r
36 /* FreeRTOS includes. */
\r
37 #include "FreeRTOS.h"
\r
40 /* Standard includes. */
\r
45 /* FreeRTOS+CLI includes. */
\r
46 #include "FreeRTOS_CLI.h"
\r
48 /* FreeRTOS+UDP includes, just to make the stats available to the CLI
\r
50 #include "FreeRTOS_UDP_IP.h"
\r
51 #include "FreeRTOS_Sockets.h"
\r
54 * Defines a command that prints out IP address information.
\r
56 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
59 * Defines a command that prints out the gathered demo debug stats.
\r
61 static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
64 * Defines a command that sends an ICMP ping request to an IP address.
\r
66 static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
68 /* Structure that defines the "ip-config" command line command. */
\r
69 static const CLI_Command_Definition_t xIPConfig =
\r
72 "ip-config:\r\n Displays IP address configuration\r\n\r\n",
\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
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
86 #endif /* configINCLUDE_DEMO_DEBUG_STATS */
\r
88 #if ipconfigSUPPORT_OUTGOING_PINGS == 1
\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
93 static const CLI_Command_Definition_t xPing =
\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
101 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
\r
104 /*-----------------------------------------------------------*/
\r
106 void vRegisterUDPCLICommands( void )
\r
108 /* Register all the command line commands defined immediately above. */
\r
109 FreeRTOS_CLIRegisterCommand( &xIPConfig );
\r
111 #if configINCLUDE_DEMO_DEBUG_STATS == 1
\r
113 FreeRTOS_CLIRegisterCommand( &xIPDebugStats );
\r
115 #endif /* configINCLUDE_DEMO_DEBUG_STATS */
\r
117 #if ipconfigSUPPORT_OUTGOING_PINGS == 1
\r
119 FreeRTOS_CLIRegisterCommand( &xPing );
\r
121 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
\r
123 /*-----------------------------------------------------------*/
\r
125 #if ipconfigSUPPORT_OUTGOING_PINGS == 1
\r
127 static BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\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
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
142 /* Start with an empty string. */
\r
143 pcWriteBuffer[ 0 ] = 0x00;
\r
145 /* Obtain the number of bytes to ping. */
\r
146 pcParameter = ( char * ) FreeRTOS_CLIGetParameter
\r
148 pcCommandString, /* The command string itself. */
\r
149 2, /* Return the second parameter. */
\r
150 &lParameterStringLength /* Store the parameter string length. */
\r
153 if( pcParameter == NULL )
\r
155 /* The number of bytes was not specified, so default it. */
\r
156 ulBytesToPing = ulDefaultBytesToPing;
\r
160 ulBytesToPing = atol( pcParameter );
\r
163 /* Obtain the IP address string. */
\r
164 pcParameter = ( char * ) FreeRTOS_CLIGetParameter
\r
166 pcCommandString, /* The command string itself. */
\r
167 1, /* Return the first parameter. */
\r
168 &lParameterStringLength /* Store the parameter string length. */
\r
171 /* Sanity check something was returned. */
\r
172 configASSERT( pcParameter );
\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
178 ulIPAddress = FreeRTOS_inet_addr( pcParameter );
\r
182 /* Terminate the host name. */
\r
183 pcParameter[ lParameterStringLength ] = 0x00;
\r
185 /* Attempt to resolve host. */
\r
186 ulIPAddress = FreeRTOS_gethostbyname( pcParameter );
\r
189 /* Convert IP address, which may have come from a DNS lookup, to string. */
\r
190 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );
\r
192 if( ulIPAddress != 0 )
\r
194 xReturn = FreeRTOS_SendPingRequest( ulIPAddress, ( uint16_t ) ulBytesToPing, portMAX_DELAY );
\r
201 if( xReturn == pdFALSE )
\r
203 sprintf( pcWriteBuffer, "%s", "Could not send ping request\r\n" );
\r
207 sprintf( pcWriteBuffer, "Ping sent to %s with identifier %d\r\n", cBuffer, ( int ) xReturn );
\r
212 /*-----------------------------------------------------------*/
\r
214 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
\r
216 #if configINCLUDE_DEMO_DEBUG_STATS != 0
\r
218 static BaseType_t prvDisplayIPDebugStats( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
220 static BaseType_t xIndex = -1;
\r
221 extern xExampleDebugStatEntry_t xIPTraceValues[];
\r
222 BaseType_t xReturn;
\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
233 if( xIndex < xExampleDebugStatEntries() )
\r
235 sprintf( pcWriteBuffer, "%s %d\r\n", ( char * ) xIPTraceValues[ xIndex ].pucDescription, ( int ) xIPTraceValues[ xIndex ].ulData );
\r
240 /* Reset the index for the next time it is called. */
\r
243 /* Ensure nothing remains in the write buffer. */
\r
244 pcWriteBuffer[ 0 ] = 0x00;
\r
250 /*-----------------------------------------------------------*/
\r
252 #endif /* configINCLUDE_DEMO_DEBUG_STATS */
\r
254 static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
256 static BaseType_t xIndex = 0;
\r
257 BaseType_t xReturn;
\r
258 uint32_t ulAddress;
\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
270 FreeRTOS_GetAddressConfiguration( &ulAddress, NULL, NULL, NULL );
\r
271 sprintf( pcWriteBuffer, "\r\nIP address " );
\r
277 FreeRTOS_GetAddressConfiguration( NULL, &ulAddress, NULL, NULL );
\r
278 sprintf( pcWriteBuffer, "\r\nNet mask " );
\r
284 FreeRTOS_GetAddressConfiguration( NULL, NULL, &ulAddress, NULL );
\r
285 sprintf( pcWriteBuffer, "\r\nGateway address " );
\r
291 FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulAddress );
\r
292 sprintf( pcWriteBuffer, "\r\nDNS server address " );
\r
299 sprintf( pcWriteBuffer, "\r\n\r\n" );
\r
305 if( ulAddress != 0 )
\r
307 FreeRTOS_inet_ntoa( ulAddress, ( &( pcWriteBuffer[ strlen( pcWriteBuffer ) ] ) ) );
\r
312 /*-----------------------------------------------------------*/
\r