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