2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
70 #pragma comment( lib, "ws2_32.lib" )
\r
72 /* Win32 includes. */
\r
73 #include <WinSock2.h>
\r
76 /* FreeRTOS includes. */
\r
77 #include "FreeRTOS.h"
\r
80 /* FreeRTOS+CLI includes. */
\r
81 #include "FreeRTOS_CLI.h"
\r
83 /* Dimensions the buffer into which input characters are placed. */
\r
84 #define cmdMAX_INPUT_SIZE 60
\r
86 /* Dimensions the buffer into which string outputs can be placed. */
\r
87 #define cmdMAX_OUTPUT_SIZE 1024
\r
89 /* Dimensions the buffer passed to the recvfrom() call. */
\r
90 #define cmdSOCKET_INPUT_BUFFER_SIZE 60
\r
93 * Open and configure the UDP socket.
\r
95 static SOCKET prvOpenUDPSocket( void );
\r
97 /*-----------------------------------------------------------*/
\r
100 * Task that provides the input and output for the FreeRTOS+CLI command
\r
101 * interpreter. In this case a UDP port is used. See the URL in the comments
\r
102 * within main.c for the location of the online documentation.
\r
104 void vUDPCommandInterpreterTask( void *pvParameters )
\r
106 long lBytes, lByte;
\r
107 signed char cInChar, cInputIndex = 0;
\r
108 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];
\r
109 BaseType_t xMoreDataToFollow;
\r
110 volatile int iErrorCode = 0;
\r
111 struct sockaddr_in xClient;
\r
112 int xClientAddressLength = sizeof( struct sockaddr_in );
\r
115 /* Just to prevent compiler warnings. */
\r
116 ( void ) pvParameters;
\r
118 /* Attempt to open the socket. */
\r
119 xSocket = prvOpenUDPSocket();
\r
121 if( xSocket != INVALID_SOCKET )
\r
125 /* Wait for incoming data on the opened socket. */
\r
126 lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength );
\r
128 if( lBytes == SOCKET_ERROR )
\r
130 /* Something went wrong, but it is not handled by this simple
\r
132 iErrorCode = WSAGetLastError();
\r
136 /* Process each received byte in turn. */
\r
138 while( lByte < lBytes )
\r
140 /* The next character in the input buffer. */
\r
141 cInChar = cLocalBuffer[ lByte ];
\r
144 /* Newline characters are taken as the end of the command
\r
146 if( cInChar == '\n' )
\r
148 /* Process the input string received prior to the
\r
152 /* Pass the string to FreeRTOS+CLI. */
\r
153 xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );
\r
155 /* Send the output generated by the command's
\r
157 sendto( xSocket, cOutputString, strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );
\r
159 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */
\r
161 /* All the strings generated by the command processing
\r
162 have been sent. Clear the input string ready to receive
\r
163 the next command. */
\r
165 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
167 /* Transmit a spacer, just to make the command console
\r
169 sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );
\r
173 if( cInChar == '\r' )
\r
175 /* Ignore the character. Newlines are used to
\r
176 detect the end of the input string. */
\r
178 else if( cInChar == '\b' )
\r
180 /* Backspace was pressed. Erase the last character
\r
181 in the string - if any. */
\r
182 if( cInputIndex > 0 )
\r
185 cInputString[ cInputIndex ] = '\0';
\r
190 /* A character was entered. Add it to the string
\r
191 entered so far. When a \n is entered the complete
\r
192 string will be passed to the command interpreter. */
\r
193 if( cInputIndex < cmdMAX_INPUT_SIZE )
\r
195 cInputString[ cInputIndex ] = cInChar;
\r
206 /* The socket could not be opened. */
\r
207 vTaskDelete( NULL );
\r
210 /*-----------------------------------------------------------*/
\r
212 static SOCKET prvOpenUDPSocket( void )
\r
215 WORD wVersionRequested;
\r
216 struct sockaddr_in xServer;
\r
217 SOCKET xSocket = INVALID_SOCKET;
\r
219 wVersionRequested = MAKEWORD( 2, 2 );
\r
221 /* Prepare to use WinSock. */
\r
222 if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )
\r
224 fprintf( stderr, "Could not open Windows connection.\n" );
\r
228 xSocket = socket( AF_INET, SOCK_DGRAM, 0 );
\r
229 if( xSocket == INVALID_SOCKET)
\r
231 fprintf( stderr, "Could not create socket.\n" );
\r
236 /* Zero out the server structure. */
\r
237 memset( ( void * ) &xServer, 0x00, sizeof( struct sockaddr_in ) );
\r
239 /* Set family and port. */
\r
240 xServer.sin_family = AF_INET;
\r
241 xServer.sin_port = htons( configUDP_CLI_PORT_NUMBER );
\r
243 /* Assign the loopback address */
\r
244 xServer.sin_addr.S_un.S_un_b.s_b1 = 127;
\r
245 xServer.sin_addr.S_un.S_un_b.s_b2 = 0;
\r
246 xServer.sin_addr.S_un.S_un_b.s_b3 = 0;
\r
247 xServer.sin_addr.S_un.S_un_b.s_b4 = 1;
\r
249 /* Bind the address to the socket. */
\r
250 if( bind( xSocket, ( struct sockaddr * ) &xServer, sizeof( struct sockaddr_in ) ) == -1 )
\r
252 fprintf( stderr, "Could not socket to port %d.\n", configUDP_CLI_PORT_NUMBER );
\r
253 closesocket( xSocket );
\r
254 xSocket = INVALID_SOCKET;
\r