]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_FAT_SL_and_CLI_Windows_Simulator/UDPCommandServer.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_FAT_SL_and_CLI_Windows_Simulator / UDPCommandServer.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 #pragma comment( lib, "ws2_32.lib" )\r
68 \r
69 /* Win32 includes. */\r
70 #include <WinSock2.h>\r
71 \r
72 /* FreeRTOS includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 /* Standard includes. */\r
77 #include <stdint.h>\r
78 #include <stdio.h>\r
79 \r
80 /* FreeRTOS+CLI includes. */\r
81 #include "FreeRTOS_CLI.h"\r
82 \r
83 /* Dimensions the buffer into which input characters are placed. */\r
84 #define cmdMAX_INPUT_SIZE       60\r
85 \r
86 /* Dimensions the buffer into which string outputs can be placed. */\r
87 #define cmdMAX_OUTPUT_SIZE      1024\r
88 \r
89 /* Dimensions the buffer passed to the recvfrom() call. */\r
90 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
91 \r
92 /* DEL acts as a backspace. */\r
93 #define cmdASCII_DEL            ( 0x7F )\r
94 \r
95 /*\r
96  * Open and configure the UDP socket.\r
97  */\r
98 static SOCKET prvOpenUDPSocket( void );\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /*\r
103  * Task that provides the input and output for the FreeRTOS+CLI command\r
104  * interpreter.  In this case a WinSock UDP port is used for convenience as this\r
105  * demo runs in a simulated environment on a Windows PC.  See the URL in the\r
106  * comments within main.c for the location of the online documentation.\r
107  */\r
108 void vUDPCommandInterpreterTask( void *pvParameters )\r
109 {\r
110 long lBytes, lByte;\r
111 signed char cInChar, cInputIndex = 0;\r
112 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
113 BaseType_t xMoreDataToFollow;\r
114 volatile int iErrorCode = 0;\r
115 struct sockaddr_in xClient;\r
116 int xClientAddressLength = sizeof( struct sockaddr_in );\r
117 SOCKET xSocket;\r
118 \r
119         /* Just to prevent compiler warnings. */\r
120         ( void ) pvParameters;\r
121 \r
122         /* Attempt to open the socket. */\r
123         xSocket = prvOpenUDPSocket();\r
124 \r
125         if( xSocket != INVALID_SOCKET )\r
126         {\r
127                 for( ;; )\r
128                 {\r
129                         /* Wait for incoming data on the opened socket. */\r
130                         lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength );\r
131 \r
132                         if( lBytes == SOCKET_ERROR )\r
133                         {\r
134                                 /* Something went wrong, but it is not handled by this simple\r
135                                 example. */\r
136                                 iErrorCode = WSAGetLastError();\r
137                         }\r
138                         else\r
139                         {\r
140                                 /* Process each received byte in turn. */\r
141                                 lByte = 0;\r
142                                 while( lByte < lBytes )\r
143                                 {\r
144                                         /* The next character in the input buffer. */\r
145                                         cInChar = cLocalBuffer[ lByte ];\r
146                                         lByte++;\r
147 \r
148                                         /* Newline characters are taken as the end of the command\r
149                                         string. */\r
150                                         if( cInChar == '\n' )\r
151                                         {\r
152                                                 /* Process the input string received prior to the\r
153                                                 newline. */\r
154                                                 do\r
155                                                 {\r
156                                                         /* Pass the string to FreeRTOS+CLI. */\r
157                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
158 \r
159                                                         /* Send the output generated by the command's\r
160                                                         implementation. */\r
161                                                         sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
162 \r
163                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
164 \r
165                                                 /* All the strings generated by the command processing\r
166                                                 have been sent.  Clear the input string ready to receive\r
167                                                 the next command. */\r
168                                                 cInputIndex = 0;\r
169                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
170 \r
171                                                 /* Transmit a spacer, just to make the command console\r
172                                                 easier to read. */\r
173                                                 sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
174                                         }\r
175                                         else\r
176                                         {\r
177                                                 if( cInChar == '\r' )\r
178                                                 {\r
179                                                         /* Ignore the character.  Newlines are used to\r
180                                                         detect the end of the input string. */\r
181                                                 }\r
182                                                 else if( ( cInChar == '\b' ) || ( cInChar == cmdASCII_DEL ) )\r
183                                                 {\r
184                                                         /* Backspace was pressed.  Erase the last character\r
185                                                         in the string - if any. */\r
186                                                         if( cInputIndex > 0 )\r
187                                                         {\r
188                                                                 cInputIndex--;\r
189                                                                 cInputString[ cInputIndex ] = '\0';\r
190                                                         }\r
191                                                 }\r
192                                                 else\r
193                                                 {\r
194                                                         /* A character was entered.  Add it to the string\r
195                                                         entered so far.  When a \n is entered the complete\r
196                                                         string will be passed to the command interpreter. */\r
197                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
198                                                         {\r
199                                                                 cInputString[ cInputIndex ] = cInChar;\r
200                                                                 cInputIndex++;\r
201                                                         }\r
202                                                 }\r
203                                         }\r
204                                 }\r
205                         }\r
206                 }\r
207         }\r
208         else\r
209         {\r
210                 /* The socket could not be opened. */\r
211                 vTaskDelete( NULL );\r
212         }\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static SOCKET prvOpenUDPSocket( void )\r
217 {\r
218 WSADATA xWSAData;\r
219 WORD wVersionRequested;\r
220 struct sockaddr_in xServer;\r
221 SOCKET xSocket = INVALID_SOCKET;\r
222 \r
223         wVersionRequested = MAKEWORD( 2, 2 );\r
224 \r
225         /* Prepare to use WinSock. */\r
226         if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )\r
227         {\r
228                 fprintf( stderr, "Could not open Windows connection.\n" );\r
229         }\r
230         else\r
231         {\r
232                 xSocket = socket( AF_INET, SOCK_DGRAM, 0 );\r
233                 if( xSocket == INVALID_SOCKET)\r
234                 {\r
235                         fprintf( stderr, "Could not create socket.\n" );\r
236                         WSACleanup();\r
237                 }\r
238                 else\r
239                 {\r
240                         /* Zero out the server structure. */\r
241                         memset( ( void * ) &xServer, 0x00, sizeof( struct sockaddr_in ) );\r
242 \r
243                         /* Set family and port. */\r
244                         xServer.sin_family = AF_INET;\r
245                         xServer.sin_port = htons( configUDP_CLI_PORT_NUMBER );\r
246 \r
247                         /* Assign the loopback address */\r
248                         xServer.sin_addr.S_un.S_un_b.s_b1 = 127;\r
249                         xServer.sin_addr.S_un.S_un_b.s_b2 = 0;\r
250                         xServer.sin_addr.S_un.S_un_b.s_b3 = 0;\r
251                         xServer.sin_addr.S_un.S_un_b.s_b4 = 1;\r
252 \r
253                         /* Bind the address to the socket. */\r
254                         if( bind( xSocket, ( struct sockaddr * ) &xServer, sizeof( struct sockaddr_in ) ) == -1 )\r
255                         {\r
256                                 fprintf( stderr, "Could not socket to port %d.\n", configUDP_CLI_PORT_NUMBER );\r
257                                 closesocket( xSocket );\r
258                                 xSocket = INVALID_SOCKET;\r
259                                 WSACleanup();\r
260                         }\r
261                 }\r
262         }\r
263 \r
264         return xSocket;\r
265 }\r
266 \r
267 \r