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