]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/lwIP_Demo/lwIP_Apps/apps/BasicSocketCommandServer/BasicSocketCommandServer.c
f77b2fe61010159039dd1e16f0a3ecdb4cefe788
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / lwIP_Demo / lwIP_Apps / apps / BasicSocketCommandServer / BasicSocketCommandServer.c
1 /*\r
2     FreeRTOS V7.0.2 - 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 #include "string.h"\r
57 \r
58 /* lwIP core includes */\r
59 #include "lwip/opt.h"\r
60 #include "lwip/sockets.h"\r
61 \r
62 /* FreeRTOS includes. */\r
63 #include "FreeRTOS.h"\r
64 #include "task.h"\r
65 \r
66 /* Utils includes. */\r
67 #include "FreeRTOS_CLI.h"\r
68 \r
69 /* Dimensions the buffer into which input characters are placed. */\r
70 #define cmdMAX_INPUT_SIZE       100\r
71 \r
72 /* Dimensions the buffer into which string outputs can be placed. */\r
73 #define cmdMAX_OUTPUT_SIZE      1024\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 void vBasicSocketsCommandInterpreterTask( void *pvParameters )\r
78 {\r
79 long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in ), lInputIndex;\r
80 struct sockaddr_in sLocalAddr;\r
81 struct sockaddr_in client_addr;\r
82 const char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
83 char cInChar;\r
84 static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ];\r
85 portBASE_TYPE xReturned;\r
86 extern void vRegisterSampleCLICommands( void );\r
87 \r
88         ( void ) pvParameters;\r
89 \r
90         /* Register the standard CLI commands. */\r
91         vRegisterSampleCLICommands();\r
92 \r
93         lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);\r
94 \r
95         if( lSocket >= 0 )\r
96         {\r
97                 memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));\r
98                 sLocalAddr.sin_family = AF_INET;\r
99                 sLocalAddr.sin_len = sizeof(sLocalAddr);\r
100                 sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);\r
101                 sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) );\r
102 \r
103                 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 )\r
104                 {\r
105                         lwip_close( lSocket );\r
106                         vTaskDelete( NULL );\r
107                 }\r
108 \r
109                 if( lwip_listen( lSocket, 20 ) != 0 )\r
110                 {\r
111                         lwip_close( lSocket );\r
112                         vTaskDelete( NULL );\r
113                 }\r
114 \r
115                 for( ;; )\r
116                 {\r
117 \r
118                         lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );\r
119 \r
120                         if( lClientFd > 0L )\r
121                         {\r
122                                 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );\r
123 \r
124                                 lInputIndex = 0;\r
125                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
126 \r
127                                 do\r
128                                 {\r
129                                         lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );\r
130 \r
131                                         if( lBytes > 0L )\r
132                                         {\r
133                                                 if( cInChar == '\n' )\r
134                                                 {\r
135                                                         /* The input string has been terminated.  Was the\r
136                                                         input a quit command? */\r
137                                                         if( strcmp( "quit", ( const char * ) cInputString ) == 0 )\r
138                                                         {\r
139                                                                 /* Set lBytes to 0 to close the connection. */\r
140                                                                 lBytes = 0L;\r
141                                                         }\r
142                                                         else\r
143                                                         {\r
144                                                                 /* The input string was not a quit command.\r
145                                                                 Pass the string to the command interpreter. */\r
146                                                                 do\r
147                                                                 {\r
148                                                                         /* Get the next output string from the command interpreter. */\r
149                                                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_INPUT_SIZE );\r
150                                                                         lwip_send( lClientFd, cOutputString, strlen( ( const char * ) cOutputString ), 0 );\r
151 \r
152                                                                 } while( xReturned != pdFALSE );\r
153 \r
154 \r
155                                                                 /* All the strings generated by the input\r
156                                                                 command have been sent.  Clear the input\r
157                                                                 string ready to receive the next command. */\r
158                                                                 lInputIndex = 0;\r
159                                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
160                                                                 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );\r
161                                                         }\r
162                                                 }\r
163                                                 else\r
164                                                 {\r
165                                                         if( cInChar == '\r' )\r
166                                                         {\r
167                                                                 /* Ignore the character. */\r
168                                                         }\r
169                                                         else if( cInChar == '\b' )\r
170                                                         {\r
171                                                                 /* Backspace was pressed.  Erase the last\r
172                                                                 character in the string - if any. */\r
173                                                                 if( lInputIndex > 0 )\r
174                                                                 {\r
175                                                                         lInputIndex--;\r
176                                                                         cInputString[ lInputIndex ] = '\0';\r
177                                                                 }\r
178                                                         }\r
179                                                         else\r
180                                                         {\r
181                                                                 /* A character was entered.  Add it to the string\r
182                                                                 entered so far.  When a \n is entered the complete\r
183                                                                 string will be passed to the command interpreter. */\r
184                                                                 if( lInputIndex < cmdMAX_INPUT_SIZE )\r
185                                                                 {\r
186                                                                         cInputString[ lInputIndex ] = cInChar;\r
187                                                                         lInputIndex++;\r
188                                                                 }\r
189                                                         }\r
190                                                 }\r
191                                         }\r
192 \r
193                                 } while( lBytes > 0L );\r
194 \r
195                                  lwip_close( lClientFd );\r
196                         }\r
197                 }\r
198         }\r
199 \r
200         /* Will only get here if a listening socket could not be created. */\r
201         vTaskDelete( NULL );\r
202 }\r
203 \r