]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC-lwIP/lwIP_Apps/apps/BasicSocketCommandServer/BasicSocketCommandServer.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / WIN32-MSVC-lwIP / lwIP_Apps / apps / BasicSocketCommandServer / BasicSocketCommandServer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include "stdlib.h"\r
31 \r
32 /* lwIP core includes */\r
33 #include "lwip/opt.h"\r
34 #include "lwip/sockets.h"\r
35 \r
36 /* FreeRTOS includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 \r
40 /* Utils includes. */\r
41 #include "CommandInterpreter.h"\r
42 \r
43 /* Dimensions the buffer into which input characters are placed. */\r
44 #define cmdMAX_INPUT_SIZE       20\r
45 \r
46 /* Dimensions the buffer into which string outputs can be placed. */\r
47 #define cmdMAX_OUTPUT_SIZE      1024\r
48 \r
49 /*-----------------------------------------------------------*/\r
50 \r
51 void vBasicSocketsCommandInterpreterTask( void *pvParameters )\r
52 {\r
53 long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in );\r
54 struct sockaddr_in sLocalAddr;\r
55 struct sockaddr_in client_addr;\r
56 const signed char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
57 signed char cInChar, cInputIndex;\r
58 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ];\r
59 portBASE_TYPE xReturned;\r
60 \r
61         ( void ) pvParameters;\r
62 \r
63         lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);\r
64 \r
65         if( lSocket >= 0 )\r
66         {\r
67                 memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));\r
68                 sLocalAddr.sin_family = AF_INET;\r
69                 sLocalAddr.sin_len = sizeof(sLocalAddr);\r
70                 sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);\r
71                 sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) );\r
72 \r
73                 if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 ) \r
74                 {\r
75                         lwip_close( lSocket );\r
76                         vTaskDelete( NULL );\r
77                 }\r
78 \r
79                 if( lwip_listen( lSocket, 20 ) != 0 )\r
80                 {\r
81                         lwip_close( lSocket );\r
82                         vTaskDelete( NULL );\r
83                 }\r
84 \r
85                 for( ;; )\r
86                 {\r
87 \r
88                         lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen );\r
89 \r
90                         if( lClientFd > 0L )\r
91                         {\r
92                                 lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );\r
93 \r
94                                 cInputIndex = 0;\r
95                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
96 \r
97                                 do\r
98                                 {                                       \r
99                                         lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 );\r
100 \r
101                                         if( lBytes > 0L ) \r
102                                         {\r
103                                                 if( cInChar == '\n' )\r
104                                                 {\r
105                                                         /* The input string has been terminated.  Was the \r
106                                                         input a quit command? */\r
107                                                         if( strcmp( "quit", ( const char * ) cInputString ) == 0 )\r
108                                                         {\r
109                                                                 /* Set lBytes to 0 to close the connection. */\r
110                                                                 lBytes = 0L;\r
111                                                         }\r
112                                                         else\r
113                                                         {\r
114                                                                 /* The input string was not a quit command.  \r
115                                                                 Pass the string to the command interpreter. */\r
116                                                                 do\r
117                                                                 {\r
118                                                                         xReturned = xCmdIntProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
119                                                                         lwip_send( lClientFd, cOutputString, strlen( ( const char * ) cOutputString ), 0 );\r
120 \r
121                                                                 } while( xReturned != pdFALSE );\r
122 \r
123                                                                 /* All the strings generated by the input \r
124                                                                 command have been sent.  Clear the input\r
125                                                                 string ready to receive the next command. */\r
126                                                                 cInputIndex = 0;\r
127                                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
128                                                                 lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 );\r
129                                                         }\r
130                                                 }\r
131                                                 else\r
132                                                 {\r
133                                                         if( cInChar == '\r' )\r
134                                                         {\r
135                                                                 /* Ignore the character. */\r
136                                                         }\r
137                                                         else if( cInChar == '\b' )\r
138                                                         {\r
139                                                                 /* Backspace was pressed.  Erase the last \r
140                                                                 character in the string - if any. */\r
141                                                                 if( cInputIndex > 0 )\r
142                                                                 {\r
143                                                                         cInputIndex--;\r
144                                                                         cInputString[ cInputIndex ] = '\0';\r
145                                                                 }\r
146                                                         }\r
147                                                         else\r
148                                                         {\r
149                                                                 /* A character was entered.  Add it to the string\r
150                                                                 entered so far.  When a \n is entered the complete\r
151                                                                 string will be passed to the command interpreter. */\r
152                                                                 if( cInputIndex < cmdMAX_INPUT_SIZE )\r
153                                                                 {\r
154                                                                         cInputString[ cInputIndex ] = cInChar;\r
155                                                                         cInputIndex++;\r
156                                                                 }\r
157                                                         }\r
158                                                 }\r
159                                         }\r
160 \r
161                                 } while( lBytes > 0L );\r
162 \r
163                                  lwip_close( lClientFd );\r
164                         }\r
165                 } \r
166         }\r
167 \r
168         /* Will only get here if a listening socket could not be created. */\r
169         vTaskDelete( NULL );\r
170 }\r
171 \r