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 /* Standard includes. */
\r
33 /* FreeRTOS includes. */
\r
34 #include "FreeRTOS.h"
\r
37 /* FreeRTOS+CLI includes. */
\r
38 #include "FreeRTOS_CLI.h"
\r
40 /* FreeRTOS+UDP includes. */
\r
41 #include "FreeRTOS_UDP_IP.h"
\r
42 #include "FreeRTOS_Sockets.h"
\r
44 /* Demo app includes. */
\r
45 #include "UDPCommandInterpreter.h"
\r
47 /* Dimensions the buffer into which input characters are placed. */
\r
48 #define cmdMAX_INPUT_SIZE 60
\r
50 /* Dimensions the buffer into which string outputs can be placed. */
\r
51 #define cmdMAX_OUTPUT_SIZE 1024
\r
53 /* Dimensions the buffer passed to the recvfrom() call. */
\r
54 #define cmdSOCKET_INPUT_BUFFER_SIZE 60
\r
57 * The task that runs FreeRTOS+CLI.
\r
59 void vUDPCommandInterpreterTask( void *pvParameters );
\r
62 * Open and configure the UDP socket.
\r
64 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort );
\r
66 /*-----------------------------------------------------------*/
\r
68 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
\r
70 xTaskCreate( vUDPCommandInterpreterTask, "CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
72 /*-----------------------------------------------------------*/
\r
75 * Task that provides the input and output for the FreeRTOS+CLI command
\r
76 * interpreter. In this case a UDP port is used. See the URL in the comments
\r
77 * within main.c for the location of the online documentation.
\r
79 void vUDPCommandInterpreterTask( void *pvParameters )
\r
82 signed char cInChar, cInputIndex = 0;
\r
83 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];
\r
84 BaseType_t xMoreDataToFollow;
\r
85 struct freertos_sockaddr xClient;
\r
86 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 extern const uint8_t ucIPAddress[ 4 ];
\r
89 extern const uint8_t ucMACAddress[ 6 ];
\r
91 /* Just to prevent compiler warnings. */
\r
92 ( void ) pvParameters;
\r
94 /* Attempt to open the socket. The port number is passed in the task
\r
95 parameter. The strange casting is to remove compiler warnings on 32-bit
\r
97 xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );
\r
99 if( xSocket != FREERTOS_INVALID_SOCKET )
\r
103 /* Wait for incoming data on the opened socket. */
\r
104 lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );
\r
106 if( lBytes != FREERTOS_SOCKET_ERROR )
\r
108 /* Process each received byte in turn. */
\r
110 while( lByte < lBytes )
\r
112 /* The next character in the input buffer. */
\r
113 cInChar = cLocalBuffer[ lByte ];
\r
116 /* Newline characters are taken as the end of the command
\r
118 if( cInChar == '\n' )
\r
120 /* Process the input string received prior to the
\r
124 /* Pass the string to FreeRTOS+CLI. */
\r
125 xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );
\r
127 /* Send the output generated by the command's
\r
129 FreeRTOS_sendto( xSocket, cOutputString, strlen( cOutputString ), 0, &xClient, xClientAddressLength );
\r
131 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */
\r
133 /* All the strings generated by the command processing
\r
134 have been sent. Clear the input string ready to receive
\r
135 the next command. */
\r
137 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
139 /* Transmit a spacer, just to make the command console
\r
141 FreeRTOS_sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, &xClient, xClientAddressLength );
\r
145 if( cInChar == '\r' )
\r
147 /* Ignore the character. Newlines are used to
\r
148 detect the end of the input string. */
\r
150 else if( cInChar == '\b' )
\r
152 /* Backspace was pressed. Erase the last character
\r
153 in the string - if any. */
\r
154 if( cInputIndex > 0 )
\r
157 cInputString[ cInputIndex ] = '\0';
\r
162 /* A character was entered. Add it to the string
\r
163 entered so far. When a \n is entered the complete
\r
164 string will be passed to the command interpreter. */
\r
165 if( cInputIndex < cmdMAX_INPUT_SIZE )
\r
167 cInputString[ cInputIndex ] = cInChar;
\r
178 /* The socket could not be opened. */
\r
179 vTaskDelete( NULL );
\r
182 /*-----------------------------------------------------------*/
\r
184 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )
\r
186 struct freertos_sockaddr xServer;
\r
187 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;
\r
189 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
190 if( xSocket != FREERTOS_INVALID_SOCKET)
\r
192 /* Zero out the server structure. */
\r
193 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );
\r
195 /* Set family and port. */
\r
196 xServer.sin_port = FreeRTOS_htons( usPort );
\r
198 /* Bind the address to the socket. */
\r
199 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )
\r
201 FreeRTOS_closesocket( xSocket );
\r
202 xSocket = FREERTOS_INVALID_SOCKET;
\r