]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC-lwIP/lwIP_Apps/apps/BasicSocketCommandServer/BasicSocketCommandServer.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Demo / WIN32-MSVC-lwIP / lwIP_Apps / apps / BasicSocketCommandServer / BasicSocketCommandServer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include "stdlib.h"\r
30 \r
31 /* lwIP core includes */\r
32 #include "lwip/opt.h"\r
33 #include "lwip/sockets.h"\r
34 \r
35 /* FreeRTOS includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 \r
39 /* Utils includes. */\r
40 #include "CommandInterpreter.h"\r
41 \r
42 /* Dimensions the buffer into which input characters are placed. */\r
43 #define cmdMAX_INPUT_SIZE       20\r
44 \r
45 /* Dimensions the buffer into which string outputs can be placed. */\r
46 #define cmdMAX_OUTPUT_SIZE      1024\r
47 \r
48 /*-----------------------------------------------------------*/\r
49 \r
50 void vBasicSocketsCommandInterpreterTask( void *pvParameters )\r
51 {\r
52 long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in );\r
53 struct sockaddr_in sLocalAddr;\r
54 struct sockaddr_in client_addr;\r
55 const signed char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
56 signed char cInChar, cInputIndex;\r
57 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ];\r
58 portBASE_TYPE xReturned;\r
59 \r
60         ( void ) pvParameters;\r
61 \r
62         lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);\r
63 \r
64         if( lSocket >= 0 )\r
65         {\r
66                 memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));\r
67                 sLocalAddr.sin_family = AF_INET;\r
68                 sLocalAddr.sin_len = sizeof(sLocalAddr);\r
69                 sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);\r
70                 sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) );\r
71 \r
72                 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 ) \r
73                 {\r
74                         lwip_close( lSocket );\r
75                         vTaskDelete( NULL );\r
76                 }\r
77 \r
78                 if( lwip_listen( lSocket, 20 ) != 0 )\r
79                 {\r
80                         lwip_close( lSocket );\r
81                         vTaskDelete( NULL );\r
82                 }\r
83 \r
84                 for( ;; )\r
85                 {\r
86 \r
87                         lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );\r
88 \r
89                         if( lClientFd > 0L )\r
90                         {\r
91                                 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );\r
92 \r
93                                 cInputIndex = 0;\r
94                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
95 \r
96                                 do\r
97                                 {                                       \r
98                                         lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );\r
99 \r
100                                         if( lBytes > 0L ) \r
101                                         {\r
102                                                 if( cInChar == '\n' )\r
103                                                 {\r
104                                                         /* The input string has been terminated.  Was the \r
105                                                         input a quit command? */\r
106                                                         if( strcmp( "quit", ( const char * ) cInputString ) == 0 )\r
107                                                         {\r
108                                                                 /* Set lBytes to 0 to close the connection. */\r
109                                                                 lBytes = 0L;\r
110                                                         }\r
111                                                         else\r
112                                                         {\r
113                                                                 /* The input string was not a quit command.  \r
114                                                                 Pass the string to the command interpreter. */\r
115                                                                 do\r
116                                                                 {\r
117                                                                         xReturned = xCmdIntProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
118                                                                         lwip_send( lClientFd, cOutputString, strlen( ( const char * ) cOutputString ), 0 );\r
119 \r
120                                                                 } while( xReturned != pdFALSE );\r
121 \r
122                                                                 /* All the strings generated by the input \r
123                                                                 command have been sent.  Clear the input\r
124                                                                 string ready to receive the next command. */\r
125                                                                 cInputIndex = 0;\r
126                                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
127                                                                 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );\r
128                                                         }\r
129                                                 }\r
130                                                 else\r
131                                                 {\r
132                                                         if( cInChar == '\r' )\r
133                                                         {\r
134                                                                 /* Ignore the character. */\r
135                                                         }\r
136                                                         else if( cInChar == '\b' )\r
137                                                         {\r
138                                                                 /* Backspace was pressed.  Erase the last \r
139                                                                 character in the string - if any. */\r
140                                                                 if( cInputIndex > 0 )\r
141                                                                 {\r
142                                                                         cInputIndex--;\r
143                                                                         cInputString[ cInputIndex ] = '\0';\r
144                                                                 }\r
145                                                         }\r
146                                                         else\r
147                                                         {\r
148                                                                 /* A character was entered.  Add it to the string\r
149                                                                 entered so far.  When a \n is entered the complete\r
150                                                                 string will be passed to the command interpreter. */\r
151                                                                 if( cInputIndex < cmdMAX_INPUT_SIZE )\r
152                                                                 {\r
153                                                                         cInputString[ cInputIndex ] = cInChar;\r
154                                                                         cInputIndex++;\r
155                                                                 }\r
156                                                         }\r
157                                                 }\r
158                                         }\r
159 \r
160                                 } while( lBytes > 0L );\r
161 \r
162                                  lwip_close( lClientFd );\r
163                         }\r
164                 } \r
165         }\r
166 \r
167         /* Will only get here if a listening socket could not be created. */\r
168         vTaskDelete( NULL );\r
169 }\r
170 \r