2 * FreeRTOS Kernel V10.0.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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
33 /* lwIP core includes */
\r
34 #include "lwip/opt.h"
\r
35 #include "lwip/sockets.h"
\r
37 /* FreeRTOS includes. */
\r
38 #include "FreeRTOS.h"
\r
41 /* Utils includes. */
\r
42 #include "FreeRTOS_CLI.h"
\r
44 /* Dimensions the buffer into which input characters are placed. */
\r
45 #define cmdMAX_INPUT_SIZE 100
\r
47 /* Dimensions the buffer into which string outputs can be placed. */
\r
48 #define cmdMAX_OUTPUT_SIZE 1024
\r
50 /*-----------------------------------------------------------*/
\r
52 void vBasicSocketsCommandInterpreterTask( void *pvParameters )
\r
54 long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in ), lInputIndex;
\r
55 struct sockaddr_in sLocalAddr;
\r
56 struct sockaddr_in client_addr;
\r
57 const char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
59 static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ];
\r
60 portBASE_TYPE xReturned;
\r
61 extern void vRegisterSampleCLICommands( void );
\r
63 ( void ) pvParameters;
\r
65 /* Register the standard CLI commands. */
\r
66 vRegisterSampleCLICommands();
\r
68 lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);
\r
72 memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
\r
73 sLocalAddr.sin_family = AF_INET;
\r
74 sLocalAddr.sin_len = sizeof(sLocalAddr);
\r
75 sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
\r
76 sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) );
\r
78 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 )
\r
80 lwip_close( lSocket );
\r
81 vTaskDelete( NULL );
\r
84 if( lwip_listen( lSocket, 20 ) != 0 )
\r
86 lwip_close( lSocket );
\r
87 vTaskDelete( NULL );
\r
93 lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );
\r
95 if( lClientFd > 0L )
\r
97 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );
\r
100 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
104 lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );
\r
108 if( cInChar == '\n' )
\r
110 /* The input string has been terminated. Was the
\r
111 input a quit command? */
\r
112 if( strcmp( "quit", ( const char * ) cInputString ) == 0 )
\r
114 /* Set lBytes to 0 to close the connection. */
\r
119 /* The input string was not a quit command.
\r
120 Pass the string to the command interpreter. */
\r
123 /* Get the next output string from the command interpreter. */
\r
124 xReturned = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_INPUT_SIZE );
\r
125 lwip_send( lClientFd, cOutputString, strlen( ( const char * ) cOutputString ), 0 );
\r
127 } while( xReturned != pdFALSE );
\r
130 /* All the strings generated by the input
\r
131 command have been sent. Clear the input
\r
132 string ready to receive the next command. */
\r
134 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
135 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );
\r
140 if( cInChar == '\r' )
\r
142 /* Ignore the character. */
\r
144 else if( cInChar == '\b' )
\r
146 /* Backspace was pressed. Erase the last
\r
147 character in the string - if any. */
\r
148 if( lInputIndex > 0 )
\r
151 cInputString[ lInputIndex ] = '\0';
\r
154 else if( ( cInChar >= ' ' ) && ( cInChar <= 'z' ) )
\r
156 /* A character was entered. Add it to the string
\r
157 entered so far. When a \n is entered the complete
\r
158 string will be passed to the command interpreter. */
\r
159 if( lInputIndex < cmdMAX_INPUT_SIZE )
\r
161 cInputString[ lInputIndex ] = cInChar;
\r
168 } while( lBytes > 0L );
\r
170 lwip_close( lClientFd );
\r
175 /* Will only get here if a listening socket could not be created. */
\r
176 vTaskDelete( NULL );
\r