2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! 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
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
25 ***************************************************************************
\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
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\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
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
46 ***************************************************************************
\r
48 ***************************************************************************
\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
62 ***************************************************************************
\r
64 ***************************************************************************
\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
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\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
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
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
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
96 /* Standard includes. */
\r
100 /* FreeRTOS includes. */
\r
101 #include "FreeRTOS.h"
\r
104 /* FreeRTOS+CLI includes. */
\r
105 #include "FreeRTOS_CLI.h"
\r
107 /* FreeRTOS+UDP includes. */
\r
108 #include "FreeRTOS_UDP_IP.h"
\r
109 #include "FreeRTOS_Sockets.h"
\r
111 /* Demo app includes. */
\r
112 #include "UDPCommandInterpreter.h"
\r
114 /* Dimensions the buffer into which input characters are placed. */
\r
115 #define cmdMAX_INPUT_SIZE 60
\r
117 /* Dimensions the buffer into which string outputs can be placed. */
\r
118 #define cmdMAX_OUTPUT_SIZE 1024
\r
120 /* Dimensions the buffer passed to the recvfrom() call. */
\r
121 #define cmdSOCKET_INPUT_BUFFER_SIZE 60
\r
124 * The task that runs FreeRTOS+CLI.
\r
126 void vUDPCommandInterpreterTask( void *pvParameters );
\r
129 * Open and configure the UDP socket.
\r
131 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort );
\r
133 /*-----------------------------------------------------------*/
\r
135 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
\r
137 xTaskCreate( vUDPCommandInterpreterTask, "CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
139 /*-----------------------------------------------------------*/
\r
142 * Task that provides the input and output for the FreeRTOS+CLI command
\r
143 * interpreter. In this case a UDP port is used. See the URL in the comments
\r
144 * within main.c for the location of the online documentation.
\r
146 void vUDPCommandInterpreterTask( void *pvParameters )
\r
148 long lBytes, lByte;
\r
149 signed char cInChar, cInputIndex = 0;
\r
150 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];
\r
151 BaseType_t xMoreDataToFollow;
\r
152 struct freertos_sockaddr xClient;
\r
153 socklen_t xClientAddressLength = 0; /* This is required as a parameter to maintain the sendto() Berkeley sockets API - but it is not actually used so can take any value. */
\r
155 extern const uint8_t ucIPAddress[ 4 ];
\r
156 extern const uint8_t ucMACAddress[ 6 ];
\r
158 /* Just to prevent compiler warnings. */
\r
159 ( void ) pvParameters;
\r
161 /* Attempt to open the socket. The port number is passed in the task
\r
162 parameter. The strange casting is to remove compiler warnings on 32-bit
\r
164 xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );
\r
166 if( xSocket != FREERTOS_INVALID_SOCKET )
\r
170 /* Wait for incoming data on the opened socket. */
\r
171 lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );
\r
173 if( lBytes != FREERTOS_SOCKET_ERROR )
\r
175 /* Process each received byte in turn. */
\r
177 while( lByte < lBytes )
\r
179 /* The next character in the input buffer. */
\r
180 cInChar = cLocalBuffer[ lByte ];
\r
183 /* Newline characters are taken as the end of the command
\r
185 if( cInChar == '\n' )
\r
187 /* Process the input string received prior to the
\r
191 /* Pass the string to FreeRTOS+CLI. */
\r
192 xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );
\r
194 /* Send the output generated by the command's
\r
196 FreeRTOS_sendto( xSocket, cOutputString, strlen( cOutputString ), 0, &xClient, xClientAddressLength );
\r
198 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */
\r
200 /* All the strings generated by the command processing
\r
201 have been sent. Clear the input string ready to receive
\r
202 the next command. */
\r
204 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
206 /* Transmit a spacer, just to make the command console
\r
208 FreeRTOS_sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, &xClient, xClientAddressLength );
\r
212 if( cInChar == '\r' )
\r
214 /* Ignore the character. Newlines are used to
\r
215 detect the end of the input string. */
\r
217 else if( cInChar == '\b' )
\r
219 /* Backspace was pressed. Erase the last character
\r
220 in the string - if any. */
\r
221 if( cInputIndex > 0 )
\r
224 cInputString[ cInputIndex ] = '\0';
\r
229 /* A character was entered. Add it to the string
\r
230 entered so far. When a \n is entered the complete
\r
231 string will be passed to the command interpreter. */
\r
232 if( cInputIndex < cmdMAX_INPUT_SIZE )
\r
234 cInputString[ cInputIndex ] = cInChar;
\r
245 /* The socket could not be opened. */
\r
246 vTaskDelete( NULL );
\r
249 /*-----------------------------------------------------------*/
\r
251 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )
\r
253 struct freertos_sockaddr xServer;
\r
254 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;
\r
256 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
257 if( xSocket != FREERTOS_INVALID_SOCKET)
\r
259 /* Zero out the server structure. */
\r
260 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );
\r
262 /* Set family and port. */
\r
263 xServer.sin_port = FreeRTOS_htons( usPort );
\r
265 /* Bind the address to the socket. */
\r
266 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )
\r
268 FreeRTOS_closesocket( xSocket );
\r
269 xSocket = FREERTOS_INVALID_SOCKET;
\r