]> git.sur5r.net Git - freertos/blob
b6f395b56e847c796c32e9f085e9f05e862a4824
[freertos] /
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \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
19 \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
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
31      *                                                                       *\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
36      *                                                                       *\r
37     ***************************************************************************\r
38 \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
42 \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
46 \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
51 \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
55 \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
58 \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
62 \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
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include "stdlib.h"\r
72 #include "string.h"\r
73 \r
74 /* lwIP core includes */\r
75 #include "lwip/opt.h"\r
76 #include "lwip/sockets.h"\r
77 \r
78 /* FreeRTOS includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 \r
82 /* Utils includes. */\r
83 #include "FreeRTOS_CLI.h"\r
84 \r
85 /* Dimensions the buffer into which input characters are placed. */\r
86 #define cmdMAX_INPUT_SIZE       100\r
87 \r
88 /* Dimensions the buffer into which string outputs can be placed. */\r
89 #define cmdMAX_OUTPUT_SIZE      1024\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void vBasicSocketsCommandInterpreterTask( void *pvParameters )\r
94 {\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
99 char cInChar;\r
100 static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ];\r
101 portBASE_TYPE xReturned;\r
102 extern void vRegisterSampleCLICommands( void );\r
103 \r
104         ( void ) pvParameters;\r
105 \r
106         /* Register the standard CLI commands. */\r
107         vRegisterSampleCLICommands();\r
108 \r
109         lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);\r
110 \r
111         if( lSocket >= 0 )\r
112         {\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
118 \r
119                 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 )\r
120                 {\r
121                         lwip_close( lSocket );\r
122                         vTaskDelete( NULL );\r
123                 }\r
124 \r
125                 if( lwip_listen( lSocket, 20 ) != 0 )\r
126                 {\r
127                         lwip_close( lSocket );\r
128                         vTaskDelete( NULL );\r
129                 }\r
130 \r
131                 for( ;; )\r
132                 {\r
133 \r
134                         lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );\r
135 \r
136                         if( lClientFd > 0L )\r
137                         {\r
138                                 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );\r
139 \r
140                                 lInputIndex = 0;\r
141                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
142 \r
143                                 do\r
144                                 {\r
145                                         lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );\r
146 \r
147                                         if( lBytes > 0L )\r
148                                         {\r
149                                                 if( cInChar == '\n' )\r
150                                                 {\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
154                                                         {\r
155                                                                 /* Set lBytes to 0 to close the connection. */\r
156                                                                 lBytes = 0L;\r
157                                                         }\r
158                                                         else\r
159                                                         {\r
160                                                                 /* The input string was not a quit command.\r
161                                                                 Pass the string to the command interpreter. */\r
162                                                                 do\r
163                                                                 {\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
167 \r
168                                                                 } while( xReturned != pdFALSE );\r
169 \r
170 \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
174                                                                 lInputIndex = 0;\r
175                                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
176                                                                 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );\r
177                                                         }\r
178                                                 }\r
179                                                 else\r
180                                                 {\r
181                                                         if( cInChar == '\r' )\r
182                                                         {\r
183                                                                 /* Ignore the character. */\r
184                                                         }\r
185                                                         else if( cInChar == '\b' )\r
186                                                         {\r
187                                                                 /* Backspace was pressed.  Erase the last\r
188                                                                 character in the string - if any. */\r
189                                                                 if( lInputIndex > 0 )\r
190                                                                 {\r
191                                                                         lInputIndex--;\r
192                                                                         cInputString[ lInputIndex ] = '\0';\r
193                                                                 }\r
194                                                         }\r
195                                                         else if( ( cInChar >= ' ' ) && ( cInChar <= 'z' ) )\r
196                                                         {\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
201                                                                 {\r
202                                                                         cInputString[ lInputIndex ] = cInChar;\r
203                                                                         lInputIndex++;\r
204                                                                 }\r
205                                                         }\r
206                                                 }\r
207                                         }\r
208 \r
209                                 } while( lBytes > 0L );\r
210 \r
211                                  lwip_close( lClientFd );\r
212                         }\r
213                 }\r
214         }\r
215 \r
216         /* Will only get here if a listening socket could not be created. */\r
217         vTaskDelete( NULL );\r
218 }\r
219 \r