2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 #pragma comment( lib, "ws2_32.lib" )
\r
31 /* Win32 includes. */
\r
32 #include <WinSock2.h>
\r
34 /* FreeRTOS includes. */
\r
35 #include "FreeRTOS.h"
\r
38 /* Standard includes. */
\r
42 /* FreeRTOS+CLI includes. */
\r
43 #include "FreeRTOS_CLI.h"
\r
45 /* Dimensions the buffer into which input characters are placed. */
\r
46 #define cmdMAX_INPUT_SIZE 60
\r
48 /* Dimensions the buffer into which string outputs can be placed. */
\r
49 #define cmdMAX_OUTPUT_SIZE 1024
\r
51 /* Dimensions the buffer passed to the recvfrom() call. */
\r
52 #define cmdSOCKET_INPUT_BUFFER_SIZE 60
\r
54 /* DEL acts as a backspace. */
\r
55 #define cmdASCII_DEL ( 0x7F )
\r
58 * Open and configure the UDP socket.
\r
60 static SOCKET prvOpenUDPSocket( void );
\r
62 /*-----------------------------------------------------------*/
\r
65 * Task that provides the input and output for the FreeRTOS+CLI command
\r
66 * interpreter. In this case a WinSock UDP port is used for convenience as this
\r
67 * demo runs in a simulated environment on a Windows PC. See the URL in the
\r
68 * comments within main.c for the location of the online documentation.
\r
70 void vUDPCommandInterpreterTask( void *pvParameters )
\r
73 signed char cInChar, cInputIndex = 0;
\r
74 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];
\r
75 BaseType_t xMoreDataToFollow;
\r
76 volatile int iErrorCode = 0;
\r
77 struct sockaddr_in xClient;
\r
78 int xClientAddressLength = sizeof( struct sockaddr_in );
\r
81 /* Just to prevent compiler warnings. */
\r
82 ( void ) pvParameters;
\r
84 /* Attempt to open the socket. */
\r
85 xSocket = prvOpenUDPSocket();
\r
87 if( xSocket != INVALID_SOCKET )
\r
91 /* Wait for incoming data on the opened socket. */
\r
92 lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength );
\r
94 if( lBytes == SOCKET_ERROR )
\r
96 /* Something went wrong, but it is not handled by this simple
\r
98 iErrorCode = WSAGetLastError();
\r
102 /* Process each received byte in turn. */
\r
104 while( lByte < lBytes )
\r
106 /* The next character in the input buffer. */
\r
107 cInChar = cLocalBuffer[ lByte ];
\r
110 /* Newline characters are taken as the end of the command
\r
112 if( cInChar == '\n' )
\r
114 /* Process the input string received prior to the
\r
118 /* Pass the string to FreeRTOS+CLI. */
\r
119 xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );
\r
121 /* Send the output generated by the command's
\r
123 sendto( xSocket, cOutputString, strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );
\r
125 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */
\r
127 /* All the strings generated by the command processing
\r
128 have been sent. Clear the input string ready to receive
\r
129 the next command. */
\r
131 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
133 /* Transmit a spacer, just to make the command console
\r
135 sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );
\r
139 if( cInChar == '\r' )
\r
141 /* Ignore the character. Newlines are used to
\r
142 detect the end of the input string. */
\r
144 else if( ( cInChar == '\b' ) || ( cInChar == cmdASCII_DEL ) )
\r
146 /* Backspace was pressed. Erase the last character
\r
147 in the string - if any. */
\r
148 if( cInputIndex > 0 )
\r
151 cInputString[ cInputIndex ] = '\0';
\r
156 /* A character was entered. Add it to the string
\r
157 entered so far. When a \n is entered the complete
\r
158 string will be passed to the command interpreter. */
\r
159 if( cInputIndex < cmdMAX_INPUT_SIZE )
\r
161 cInputString[ cInputIndex ] = cInChar;
\r
172 /* The socket could not be opened. */
\r
173 vTaskDelete( NULL );
\r
176 /*-----------------------------------------------------------*/
\r
178 static SOCKET prvOpenUDPSocket( void )
\r
181 WORD wVersionRequested;
\r
182 struct sockaddr_in xServer;
\r
183 SOCKET xSocket = INVALID_SOCKET;
\r
185 wVersionRequested = MAKEWORD( 2, 2 );
\r
187 /* Prepare to use WinSock. */
\r
188 if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )
\r
190 fprintf( stderr, "Could not open Windows connection.\n" );
\r
194 xSocket = socket( AF_INET, SOCK_DGRAM, 0 );
\r
195 if( xSocket == INVALID_SOCKET)
\r
197 fprintf( stderr, "Could not create socket.\n" );
\r
202 /* Zero out the server structure. */
\r
203 memset( ( void * ) &xServer, 0x00, sizeof( struct sockaddr_in ) );
\r
205 /* Set family and port. */
\r
206 xServer.sin_family = AF_INET;
\r
207 xServer.sin_port = htons( configUDP_CLI_PORT_NUMBER );
\r
209 /* Assign the loopback address */
\r
210 xServer.sin_addr.S_un.S_un_b.s_b1 = 127;
\r
211 xServer.sin_addr.S_un.S_un_b.s_b2 = 0;
\r
212 xServer.sin_addr.S_un.S_un_b.s_b3 = 0;
\r
213 xServer.sin_addr.S_un.S_un_b.s_b4 = 1;
\r
215 /* Bind the address to the socket. */
\r
216 if( bind( xSocket, ( struct sockaddr * ) &xServer, sizeof( struct sockaddr_in ) ) == -1 )
\r
218 fprintf( stderr, "Could not socket to port %d.\n", configUDP_CLI_PORT_NUMBER );
\r
219 closesocket( xSocket );
\r
220 xSocket = INVALID_SOCKET;
\r