]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_Reliance_Edge_and_CLI_Windows_Simulator/UDPCommandServer.c
565982363f4d226c9cdcb2da4bc25b5c5050472d
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_Reliance_Edge_and_CLI_Windows_Simulator / UDPCommandServer.c
1 /*\r
2  * FreeRTOS Kernel V10.2.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 \r
29 #pragma comment( lib, "ws2_32.lib" )\r
30 \r
31 /* Win32 includes. */\r
32 #include <WinSock2.h>\r
33 \r
34 /* FreeRTOS includes. */\r
35 #include "FreeRTOS.h"\r
36 #include "task.h"\r
37 \r
38 /* Standard includes. */\r
39 #include <stdint.h>\r
40 #include <stdio.h>\r
41 \r
42 /* FreeRTOS+CLI includes. */\r
43 #include "FreeRTOS_CLI.h"\r
44 \r
45 /* Dimensions the buffer into which input characters are placed. */\r
46 #define cmdMAX_INPUT_SIZE       60\r
47 \r
48 /* Dimensions the buffer into which string outputs can be placed. */\r
49 #define cmdMAX_OUTPUT_SIZE      1024\r
50 \r
51 /* Dimensions the buffer passed to the recvfrom() call. */\r
52 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
53 \r
54 /* DEL acts as a backspace. */\r
55 #define cmdASCII_DEL            ( 0x7F )\r
56 \r
57 /*\r
58  * Open and configure the UDP socket.\r
59  */\r
60 static SOCKET prvOpenUDPSocket( void );\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /*\r
65  * Task that provides the input and output for the FreeRTOS+CLI command\r
66  * interpreter.  In this case a WinSock UDP port is used for convenience as this\r
67  * demo runs in a simulated environment on a Windows PC.  See the URL in the\r
68  * comments within main.c for the location of the online documentation.\r
69  */\r
70 void vUDPCommandInterpreterTask( void *pvParameters )\r
71 {\r
72 long lBytes, lByte;\r
73 signed char cInChar, cInputIndex = 0;\r
74 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
75 BaseType_t xMoreDataToFollow;\r
76 volatile int iErrorCode = 0;\r
77 struct sockaddr_in xClient;\r
78 int xClientAddressLength = sizeof( struct sockaddr_in );\r
79 SOCKET xSocket;\r
80 \r
81         /* Just to prevent compiler warnings. */\r
82         ( void ) pvParameters;\r
83 \r
84         /* Attempt to open the socket. */\r
85         xSocket = prvOpenUDPSocket();\r
86 \r
87         if( xSocket != INVALID_SOCKET )\r
88         {\r
89                 for( ;; )\r
90                 {\r
91                         /* Wait for incoming data on the opened socket. */\r
92                         lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength );\r
93 \r
94                         if( lBytes == SOCKET_ERROR )\r
95                         {\r
96                                 /* Something went wrong, but it is not handled by this simple\r
97                                 example. */\r
98                                 iErrorCode = WSAGetLastError();\r
99                         }\r
100                         else\r
101                         {\r
102                                 /* Process each received byte in turn. */\r
103                                 lByte = 0;\r
104                                 while( lByte < lBytes )\r
105                                 {\r
106                                         /* The next character in the input buffer. */\r
107                                         cInChar = cLocalBuffer[ lByte ];\r
108                                         lByte++;\r
109 \r
110                                         /* Newline characters are taken as the end of the command\r
111                                         string. */\r
112                                         if( cInChar == '\n' )\r
113                                         {\r
114                                                 /* Process the input string received prior to the\r
115                                                 newline. */\r
116                                                 do\r
117                                                 {\r
118                                                         /* Pass the string to FreeRTOS+CLI. */\r
119                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
120 \r
121                                                         /* Send the output generated by the command's\r
122                                                         implementation. */\r
123                                                         sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
124 \r
125                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
126 \r
127                                                 /* All the strings generated by the command processing\r
128                                                 have been sent.  Clear the input string ready to receive\r
129                                                 the next command. */\r
130                                                 cInputIndex = 0;\r
131                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
132 \r
133                                                 /* Transmit a spacer, just to make the command console\r
134                                                 easier to read. */\r
135                                                 sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
136                                         }\r
137                                         else\r
138                                         {\r
139                                                 if( cInChar == '\r' )\r
140                                                 {\r
141                                                         /* Ignore the character.  Newlines are used to\r
142                                                         detect the end of the input string. */\r
143                                                 }\r
144                                                 else if( ( cInChar == '\b' ) || ( cInChar == cmdASCII_DEL ) )\r
145                                                 {\r
146                                                         /* Backspace was pressed.  Erase the last character\r
147                                                         in the string - if any. */\r
148                                                         if( cInputIndex > 0 )\r
149                                                         {\r
150                                                                 cInputIndex--;\r
151                                                                 cInputString[ cInputIndex ] = '\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( cInputIndex < cmdMAX_INPUT_SIZE )\r
160                                                         {\r
161                                                                 cInputString[ cInputIndex ] = cInChar;\r
162                                                                 cInputIndex++;\r
163                                                         }\r
164                                                 }\r
165                                         }\r
166                                 }\r
167                         }\r
168                 }\r
169         }\r
170         else\r
171         {\r
172                 /* The socket could not be opened. */\r
173                 vTaskDelete( NULL );\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static SOCKET prvOpenUDPSocket( void )\r
179 {\r
180 WSADATA xWSAData;\r
181 WORD wVersionRequested;\r
182 struct sockaddr_in xServer;\r
183 SOCKET xSocket = INVALID_SOCKET;\r
184 \r
185         wVersionRequested = MAKEWORD( 2, 2 );\r
186 \r
187         /* Prepare to use WinSock. */\r
188         if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )\r
189         {\r
190                 fprintf( stderr, "Could not open Windows connection.\n" );\r
191         }\r
192         else\r
193         {\r
194                 xSocket = socket( AF_INET, SOCK_DGRAM, 0 );\r
195                 if( xSocket == INVALID_SOCKET)\r
196                 {\r
197                         fprintf( stderr, "Could not create socket.\n" );\r
198                         WSACleanup();\r
199                 }\r
200                 else\r
201                 {\r
202                         /* Zero out the server structure. */\r
203                         memset( ( void * ) &xServer, 0x00, sizeof( struct sockaddr_in ) );\r
204 \r
205                         /* Set family and port. */\r
206                         xServer.sin_family = AF_INET;\r
207                         xServer.sin_port = htons( configUDP_CLI_PORT_NUMBER );\r
208 \r
209                         /* Assign the loopback address */\r
210                         xServer.sin_addr.S_un.S_un_b.s_b1 = 127;\r
211                         xServer.sin_addr.S_un.S_un_b.s_b2 = 0;\r
212                         xServer.sin_addr.S_un.S_un_b.s_b3 = 0;\r
213                         xServer.sin_addr.S_un.S_un_b.s_b4 = 1;\r
214 \r
215                         /* Bind the address to the socket. */\r
216                         if( bind( xSocket, ( struct sockaddr * ) &xServer, sizeof( struct sockaddr_in ) ) == -1 )\r
217                         {\r
218                                 fprintf( stderr, "Could not socket to port %d.\n", configUDP_CLI_PORT_NUMBER );\r
219                                 closesocket( xSocket );\r
220                                 xSocket = INVALID_SOCKET;\r
221                                 WSACleanup();\r
222                         }\r
223                 }\r
224         }\r
225 \r
226         return xSocket;\r
227 }\r
228 \r
229 \r