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