]> begriffs open source - freertos/blob - Demo/WIN32-MSVC-lwIP/lwIP_Apps/apps/BasicSocketCommandServer/BasicSocketCommandServer.c
Move the MSVC/lwIP code into the main line.
[freertos] / Demo / WIN32-MSVC-lwIP / lwIP_Apps / apps / BasicSocketCommandServer / BasicSocketCommandServer.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Standard includes. */\r
55 #include "stdlib.h"\r
56 \r
57 /* lwIP core includes */\r
58 #include "lwip/opt.h"\r
59 #include "lwip/sockets.h"\r
60 \r
61 /* FreeRTOS includes. */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 \r
65 /* Utils includes. */\r
66 #include "CommandInterpreter.h"\r
67 \r
68 #define cmdMAX_INPUT_SIZE       20\r
69 /*-----------------------------------------------------------*/\r
70 \r
71 void vBasicTelnetServer( void *pvParameters )\r
72 {\r
73 long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in );\r
74 struct sockaddr_in sLocalAddr;\r
75 struct sockaddr_in client_addr;\r
76 const signed char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
77 const signed char *pcString;\r
78 signed char cInChar, cInputIndex;\r
79 signed char cInputString[ cmdMAX_INPUT_SIZE ];\r
80 \r
81         ( void ) pvParameters;\r
82 \r
83         lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);\r
84 \r
85         if( lSocket >= 0 )\r
86         {\r
87                 memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));\r
88                 sLocalAddr.sin_family = AF_INET;\r
89                 sLocalAddr.sin_len = sizeof(sLocalAddr);\r
90                 sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);\r
91                 sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) );\r
92 \r
93                 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 ) \r
94                 {\r
95                         lwip_close( lSocket );\r
96                         vTaskDelete( NULL );\r
97                 }\r
98 \r
99                 if( lwip_listen( lSocket, 20 ) != 0 )\r
100                 {\r
101                         lwip_close( lSocket );\r
102                         vTaskDelete( NULL );\r
103                 }\r
104 \r
105                 for( ;; )\r
106                 {\r
107 \r
108                         lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );\r
109 \r
110                         if( lClientFd > 0L )\r
111                         {\r
112                                 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );\r
113 \r
114                                 cInputIndex = 0;\r
115                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
116 \r
117                                 do\r
118                                 {                                       \r
119                                         lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );\r
120 \r
121                                         if( lBytes > 0L ) \r
122                                         {\r
123                                                 if( cInChar == '\n' )\r
124                                                 {\r
125                                                         /* The input string has been terminated.  Was the \r
126                                                         input a quit command? */\r
127                                                         if( strcmp( "quit", ( const char * ) cInputString ) == 0 )\r
128                                                         {\r
129                                                                 /* Set lBytes to 0 to close the connection. */\r
130                                                                 lBytes = 0L;\r
131                                                         }\r
132                                                         else\r
133                                                         {\r
134                                                                 /* The input string was not a quit command.  \r
135                                                                 Pass the string to the command interpreter. */\r
136                                                                 while( ( pcString = pcProcessCommand( cInputString ) ) != NULL )\r
137                                                                 {\r
138                                                                         /* A string has been generated by the \r
139                                                                         command interpreter.  Send it. */\r
140                                                                         lwip_send( lClientFd, pcString, strlen( ( const char * ) pcString ), 0 );\r
141                                                                 }\r
142 \r
143                                                                 /* All the strings generated by the input \r
144                                                                 command have been sent.  Clear the input\r
145                                                                 string ready to receive the next command. */\r
146                                                                 cInputIndex = 0;\r
147                                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
148                                                                 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );\r
149                                                         }\r
150                                                 }\r
151                                                 else\r
152                                                 {\r
153                                                         if( cInChar == '\r' )\r
154                                                         {\r
155                                                                 /* Ignore the character. */\r
156                                                         }\r
157                                                         else if( cInChar == '\b' )\r
158                                                         {\r
159                                                                 /* Backspace was pressed.  Erase the last \r
160                                                                 character in the string - if any. */\r
161                                                                 if( cInputIndex > 0 )\r
162                                                                 {\r
163                                                                         cInputIndex--;\r
164                                                                         cInputString[ cInputIndex ] = '\0';\r
165                                                                 }\r
166                                                         }\r
167                                                         else\r
168                                                         {\r
169                                                                 /* A character was entered.  Add it to the string\r
170                                                                 entered so far.  When a \n is entered the complete\r
171                                                                 string will be passed to the command interpreter. */\r
172                                                                 if( cInputIndex < cmdMAX_INPUT_SIZE )\r
173                                                                 {\r
174                                                                         cInputString[ cInputIndex ] = cInChar;\r
175                                                                         cInputIndex++;\r
176                                                                 }\r
177                                                         }\r
178                                                 }\r
179                                         }\r
180 \r
181                                 } while( lBytes > 0L );\r
182 \r
183                                  lwip_close( lClientFd );\r
184                         }\r
185                 } \r
186         }\r
187 }\r
188 \r