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