2 * FreeRTOS Kernel V10.0.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
28 /* Standard includes. */
\r
32 /* FreeRTOS includes. */
\r
33 #include "FreeRTOS.h"
\r
36 /* FreeRTOS+CLI includes. */
\r
37 #include "FreeRTOS_CLI.h"
\r
39 /* FreeRTOS+UDP includes. */
\r
40 #include "FreeRTOS_UDP_IP.h"
\r
41 #include "FreeRTOS_Sockets.h"
\r
43 /* Demo app includes. */
\r
44 #include "UDPCommandInterpreter.h"
\r
46 /* Dimensions the buffer into which input characters are placed. */
\r
47 #define cmdMAX_INPUT_SIZE 60
\r
49 /* Dimensions the buffer into which string outputs can be placed. */
\r
50 #define cmdMAX_OUTPUT_SIZE 1250
\r
52 /* Dimensions the buffer passed to the recvfrom() call. */
\r
53 #define cmdSOCKET_INPUT_BUFFER_SIZE 60
\r
56 * The task that runs FreeRTOS+CLI.
\r
58 void vUDPCommandInterpreterTask( void *pvParameters );
\r
61 * Open and configure the UDP socket.
\r
63 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort );
\r
65 /*-----------------------------------------------------------*/
\r
67 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
\r
69 xTaskCreate( vUDPCommandInterpreterTask, "CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
71 /*-----------------------------------------------------------*/
\r
74 * Task that provides the input and output for the FreeRTOS+CLI command
\r
75 * interpreter. In this case a UDP port is used. See the URL in the comments
\r
76 * within main.c for the location of the online documentation.
\r
78 void vUDPCommandInterpreterTask( void *pvParameters )
\r
81 signed char cInChar, cInputIndex = 0;
\r
82 static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];
\r
83 BaseType_t xMoreDataToFollow;
\r
84 struct freertos_sockaddr xClient;
\r
85 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
88 /* Just to prevent compiler warnings. */
\r
89 ( void ) pvParameters;
\r
91 /* Attempt to open the socket. The port number is passed in the task
\r
92 parameter. The strange casting is to remove compiler warnings on 32-bit
\r
94 xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );
\r
96 if( xSocket != FREERTOS_INVALID_SOCKET )
\r
100 /* Wait for incoming data on the opened socket. */
\r
101 lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );
\r
103 if( lBytes != FREERTOS_SOCKET_ERROR )
\r
105 /* Process each received byte in turn. */
\r
107 while( lByte < lBytes )
\r
109 /* The next character in the input buffer. */
\r
110 cInChar = cLocalBuffer[ lByte ];
\r
113 /* Newline characters are taken as the end of the command
\r
115 if( cInChar == '\n' )
\r
117 /* Process the input string received prior to the
\r
121 /* Pass the string to FreeRTOS+CLI. */
\r
122 xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );
\r
124 /* Send the output generated by the command's
\r
126 FreeRTOS_sendto( xSocket, cOutputString, strlen( cOutputString ), 0, &xClient, xClientAddressLength );
\r
128 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */
\r
130 /* All the strings generated by the command processing
\r
131 have been sent. Clear the input string ready to receive
\r
132 the next command. */
\r
134 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
136 /* Transmit a spacer, just to make the command console
\r
138 FreeRTOS_sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, &xClient, xClientAddressLength );
\r
142 if( cInChar == '\r' )
\r
144 /* Ignore the character. Newlines are used to
\r
145 detect the end of the input string. */
\r
147 else if( cInChar == '\b' )
\r
149 /* Backspace was pressed. Erase the last character
\r
150 in the string - if any. */
\r
151 if( cInputIndex > 0 )
\r
154 cInputString[ cInputIndex ] = '\0';
\r
159 /* A character was entered. Add it to the string
\r
160 entered so far. When a \n is entered the complete
\r
161 string will be passed to the command interpreter. */
\r
162 if( cInputIndex < cmdMAX_INPUT_SIZE )
\r
164 cInputString[ cInputIndex ] = cInChar;
\r
175 /* The socket could not be opened. */
\r
176 vTaskDelete( NULL );
\r
179 /*-----------------------------------------------------------*/
\r
181 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )
\r
183 struct freertos_sockaddr xServer;
\r
184 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;
\r
186 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
187 if( xSocket != FREERTOS_INVALID_SOCKET)
\r
189 /* Zero out the server structure. */
\r
190 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );
\r
192 /* Set family and port. */
\r
193 xServer.sin_port = FreeRTOS_htons( usPort );
\r
195 /* Bind the address to the socket. */
\r
196 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )
\r
198 FreeRTOS_closesocket( xSocket );
\r
199 xSocket = FREERTOS_INVALID_SOCKET;
\r