2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Standard includes. */
\r
74 /* lwIP core includes */
\r
75 #include "lwip/opt.h"
\r
76 #include "lwip/sockets.h"
\r
78 /* FreeRTOS includes. */
\r
79 #include "FreeRTOS.h"
\r
82 /* Utils includes. */
\r
83 #include "FreeRTOS_CLI.h"
\r
85 /* Dimensions the buffer into which input characters are placed. */
\r
86 #define cmdMAX_INPUT_SIZE 100
\r
88 /* Dimensions the buffer into which string outputs can be placed. */
\r
89 #define cmdMAX_OUTPUT_SIZE 1024
\r
91 /*-----------------------------------------------------------*/
\r
93 void vBasicSocketsCommandInterpreterTask( void *pvParameters )
\r
95 long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in ), lInputIndex;
\r
96 struct sockaddr_in sLocalAddr;
\r
97 struct sockaddr_in client_addr;
\r
98 const char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
100 static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ];
\r
101 portBASE_TYPE xReturned;
\r
102 extern void vRegisterSampleCLICommands( void );
\r
104 ( void ) pvParameters;
\r
106 /* Register the standard CLI commands. */
\r
107 vRegisterSampleCLICommands();
\r
109 lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);
\r
113 memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
\r
114 sLocalAddr.sin_family = AF_INET;
\r
115 sLocalAddr.sin_len = sizeof(sLocalAddr);
\r
116 sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
\r
117 sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) );
\r
119 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 )
\r
121 lwip_close( lSocket );
\r
122 vTaskDelete( NULL );
\r
125 if( lwip_listen( lSocket, 20 ) != 0 )
\r
127 lwip_close( lSocket );
\r
128 vTaskDelete( NULL );
\r
134 lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );
\r
136 if( lClientFd > 0L )
\r
138 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );
\r
141 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
145 lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );
\r
149 if( cInChar == '\n' )
\r
151 /* The input string has been terminated. Was the
\r
152 input a quit command? */
\r
153 if( strcmp( "quit", ( const char * ) cInputString ) == 0 )
\r
155 /* Set lBytes to 0 to close the connection. */
\r
160 /* The input string was not a quit command.
\r
161 Pass the string to the command interpreter. */
\r
164 /* Get the next output string from the command interpreter. */
\r
165 xReturned = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_INPUT_SIZE );
\r
166 lwip_send( lClientFd, cOutputString, strlen( ( const char * ) cOutputString ), 0 );
\r
168 } while( xReturned != pdFALSE );
\r
171 /* All the strings generated by the input
\r
172 command have been sent. Clear the input
\r
173 string ready to receive the next command. */
\r
175 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
176 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );
\r
181 if( cInChar == '\r' )
\r
183 /* Ignore the character. */
\r
185 else if( cInChar == '\b' )
\r
187 /* Backspace was pressed. Erase the last
\r
188 character in the string - if any. */
\r
189 if( lInputIndex > 0 )
\r
192 cInputString[ lInputIndex ] = '\0';
\r
197 /* A character was entered. Add it to the string
\r
198 entered so far. When a \n is entered the complete
\r
199 string will be passed to the command interpreter. */
\r
200 if( lInputIndex < cmdMAX_INPUT_SIZE )
\r
202 cInputString[ lInputIndex ] = cInChar;
\r
209 } while( lBytes > 0L );
\r
211 lwip_close( lClientFd );
\r
216 /* Will only get here if a listening socket could not be created. */
\r
217 vTaskDelete( NULL );
\r