2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 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 /* Standard includes. */
\r
32 /* lwIP core includes */
\r
33 #include "lwip/opt.h"
\r
34 #include "lwip/sockets.h"
\r
36 /* FreeRTOS includes. */
\r
37 #include "FreeRTOS.h"
\r
40 /* Utils includes. */
\r
41 #include "FreeRTOS_CLI.h"
\r
43 /* Dimensions the buffer into which input characters are placed. */
\r
44 #define cmdMAX_INPUT_SIZE 100
\r
46 /* Dimensions the buffer into which string outputs can be placed. */
\r
47 #define cmdMAX_OUTPUT_SIZE 1024
\r
49 /*-----------------------------------------------------------*/
\r
51 void vBasicSocketsCommandInterpreterTask( void *pvParameters )
\r
53 long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in ), lInputIndex;
\r
54 struct sockaddr_in sLocalAddr;
\r
55 struct sockaddr_in client_addr;
\r
56 const char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
58 static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ];
\r
59 portBASE_TYPE xReturned;
\r
60 extern void vRegisterSampleCLICommands( void );
\r
62 ( void ) pvParameters;
\r
64 /* Register the standard CLI commands. */
\r
65 vRegisterSampleCLICommands();
\r
67 lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);
\r
71 memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
\r
72 sLocalAddr.sin_family = AF_INET;
\r
73 sLocalAddr.sin_len = sizeof(sLocalAddr);
\r
74 sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
\r
75 sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) );
\r
77 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 )
\r
79 lwip_close( lSocket );
\r
80 vTaskDelete( NULL );
\r
83 if( lwip_listen( lSocket, 20 ) != 0 )
\r
85 lwip_close( lSocket );
\r
86 vTaskDelete( NULL );
\r
92 lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );
\r
94 if( lClientFd > 0L )
\r
96 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );
\r
99 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
103 lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );
\r
107 if( cInChar == '\n' )
\r
109 /* The input string has been terminated. Was the
\r
110 input a quit command? */
\r
111 if( strcmp( "quit", ( const char * ) cInputString ) == 0 )
\r
113 /* Set lBytes to 0 to close the connection. */
\r
118 /* The input string was not a quit command.
\r
119 Pass the string to the command interpreter. */
\r
122 /* Get the next output string from the command interpreter. */
\r
123 xReturned = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_INPUT_SIZE );
\r
124 lwip_send( lClientFd, cOutputString, strlen( ( const char * ) cOutputString ), 0 );
\r
126 } while( xReturned != pdFALSE );
\r
129 /* All the strings generated by the input
\r
130 command have been sent. Clear the input
\r
131 string ready to receive the next command. */
\r
133 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
134 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );
\r
139 if( cInChar == '\r' )
\r
141 /* Ignore the character. */
\r
143 else if( cInChar == '\b' )
\r
145 /* Backspace was pressed. Erase the last
\r
146 character in the string - if any. */
\r
147 if( lInputIndex > 0 )
\r
150 cInputString[ lInputIndex ] = '\0';
\r
155 /* A character was entered. Add it to the string
\r
156 entered so far. When a \n is entered the complete
\r
157 string will be passed to the command interpreter. */
\r
158 if( lInputIndex < cmdMAX_INPUT_SIZE )
\r
160 cInputString[ lInputIndex ] = cInChar;
\r
167 } while( lBytes > 0L );
\r
169 lwip_close( lClientFd );
\r
174 /* Will only get here if a listening socket could not be created. */
\r
175 vTaskDelete( NULL );
\r