2 * FreeRTOS Kernel V10.0.0
\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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 #pragma comment( lib, "ws2_32.lib" )
\r
31 /* Win32 includes. */
\r
32 #include <WinSock2.h>
\r
35 /* FreeRTOS includes. */
\r
36 #include "FreeRTOS.h"
\r
39 /* FreeRTOS+CLI includes. */
\r
40 #include "FreeRTOS_CLI.h"
\r
42 /* Dimensions the buffer into which input characters are placed. */
\r
43 #define cmdMAX_INPUT_SIZE 60
\r
45 /* Dimensions the buffer into which string outputs can be placed. */
\r
46 #define cmdMAX_OUTPUT_SIZE 1024
\r
48 /* Dimensions the buffer passed to the recvfrom() call. */
\r
49 #define cmdSOCKET_INPUT_BUFFER_SIZE 60
\r
52 * Open and configure the UDP socket.
\r
54 static SOCKET prvOpenUDPSocket( void );
\r
56 /*-----------------------------------------------------------*/
\r
59 * Task that provides the input and output for the FreeRTOS+CLI command
\r
60 * interpreter. In this case a UDP port is used. See the URL in the comments
\r
61 * within main.c for the location of the online documentation.
\r
63 void vUDPCommandInterpreterTask( void *pvParameters )
\r
66 signed char cInChar, cInputIndex = 0;
\r
67 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];
\r
68 BaseType_t xMoreDataToFollow;
\r
69 volatile int iErrorCode = 0;
\r
70 struct sockaddr_in xClient;
\r
71 int xClientAddressLength = sizeof( struct sockaddr_in );
\r
74 /* Just to prevent compiler warnings. */
\r
75 ( void ) pvParameters;
\r
77 /* Attempt to open the socket. */
\r
78 xSocket = prvOpenUDPSocket();
\r
80 if( xSocket != INVALID_SOCKET )
\r
84 /* Wait for incoming data on the opened socket. */
\r
85 lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength );
\r
87 if( lBytes == SOCKET_ERROR )
\r
89 /* Something went wrong, but it is not handled by this simple
\r
91 iErrorCode = WSAGetLastError();
\r
95 /* Process each received byte in turn. */
\r
97 while( lByte < lBytes )
\r
99 /* The next character in the input buffer. */
\r
100 cInChar = cLocalBuffer[ lByte ];
\r
103 /* Newline characters are taken as the end of the command
\r
105 if( cInChar == '\n' )
\r
107 /* Process the input string received prior to the
\r
111 /* Pass the string to FreeRTOS+CLI. */
\r
112 xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );
\r
114 /* Send the output generated by the command's
\r
116 sendto( xSocket, cOutputString, strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );
\r
118 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */
\r
120 /* All the strings generated by the command processing
\r
121 have been sent. Clear the input string ready to receive
\r
122 the next command. */
\r
124 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
126 /* Transmit a spacer, just to make the command console
\r
128 sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );
\r
132 if( cInChar == '\r' )
\r
134 /* Ignore the character. Newlines are used to
\r
135 detect the end of the input string. */
\r
137 else if( cInChar == '\b' )
\r
139 /* Backspace was pressed. Erase the last character
\r
140 in the string - if any. */
\r
141 if( cInputIndex > 0 )
\r
144 cInputString[ cInputIndex ] = '\0';
\r
149 /* A character was entered. Add it to the string
\r
150 entered so far. When a \n is entered the complete
\r
151 string will be passed to the command interpreter. */
\r
152 if( cInputIndex < cmdMAX_INPUT_SIZE )
\r
154 cInputString[ cInputIndex ] = cInChar;
\r
165 /* The socket could not be opened. */
\r
166 vTaskDelete( NULL );
\r
169 /*-----------------------------------------------------------*/
\r
171 static SOCKET prvOpenUDPSocket( void )
\r
174 WORD wVersionRequested;
\r
175 struct sockaddr_in xServer;
\r
176 SOCKET xSocket = INVALID_SOCKET;
\r
178 wVersionRequested = MAKEWORD( 2, 2 );
\r
180 /* Prepare to use WinSock. */
\r
181 if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )
\r
183 fprintf( stderr, "Could not open Windows connection.\n" );
\r
187 xSocket = socket( AF_INET, SOCK_DGRAM, 0 );
\r
188 if( xSocket == INVALID_SOCKET)
\r
190 fprintf( stderr, "Could not create socket.\n" );
\r
195 /* Zero out the server structure. */
\r
196 memset( ( void * ) &xServer, 0x00, sizeof( struct sockaddr_in ) );
\r
198 /* Set family and port. */
\r
199 xServer.sin_family = AF_INET;
\r
200 xServer.sin_port = htons( configUDP_CLI_PORT_NUMBER );
\r
202 /* Assign the loopback address */
\r
203 xServer.sin_addr.S_un.S_un_b.s_b1 = 127;
\r
204 xServer.sin_addr.S_un.S_un_b.s_b2 = 0;
\r
205 xServer.sin_addr.S_un.S_un_b.s_b3 = 0;
\r
206 xServer.sin_addr.S_un.S_un_b.s_b4 = 1;
\r
208 /* Bind the address to the socket. */
\r
209 if( bind( xSocket, ( struct sockaddr * ) &xServer, sizeof( struct sockaddr_in ) ) == -1 )
\r
211 fprintf( stderr, "Could not socket to port %d.\n", configUDP_CLI_PORT_NUMBER );
\r
212 closesocket( xSocket );
\r
213 xSocket = INVALID_SOCKET;
\r