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