]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/Common/FreeRTOS_Plus_CLI_Demos/UDPCommandConsole.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / Common / FreeRTOS_Plus_CLI_Demos / UDPCommandConsole.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdint.h>\r
72 #include <stdio.h>\r
73 #include <stdarg.h>\r
74 \r
75 /* FreeRTOS includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 #include "semphr.h"\r
79 \r
80 /* FreeRTOS+CLI includes. */\r
81 #include "FreeRTOS_CLI.h"\r
82 \r
83 /* FreeRTOS+TCP includes. */\r
84 #include "FreeRTOS_IP.h"\r
85 #include "FreeRTOS_Sockets.h"\r
86 \r
87 /* Demo app includes. */\r
88 #include "UDPCommandConsole.h"\r
89 \r
90 /* Dimensions the buffer into which input characters are placed. */\r
91 #define cmdMAX_INPUT_SIZE       60\r
92 \r
93 /* Dimensions the buffer into which string outputs can be placed. */\r
94 #define cmdMAX_OUTPUT_SIZE      1024\r
95 \r
96 /* Dimensions the buffer passed to the recvfrom() call. */\r
97 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
98 \r
99 /* DEL acts as a backspace. */\r
100 #define cmdASCII_DEL            ( 0x7F )\r
101 \r
102 /* The socket used by the CLI and debug print messages. */\r
103 static Socket_t xSocket = FREERTOS_INVALID_SOCKET;\r
104 \r
105 /*\r
106  * The task that runs FreeRTOS+CLI.\r
107  */\r
108 static void prvUDPCommandInterpreterTask( void *pvParameters );\r
109 \r
110 /*\r
111  * Open and configure the UDP socket.\r
112  */\r
113 static Socket_t prvOpenUDPServerSocket( uint16_t usPort );\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /* This is required as a parameter to maintain the sendto() Berkeley sockets\r
118 API - but it is not actually used so can take any value. */\r
119 static socklen_t xClientAddressLength = 0;\r
120 \r
121 /* Const messages output by the command console. */\r
122 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
123 static const char * const pcNewLine = "\r\n";\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )\r
128 {\r
129         xTaskCreate( prvUDPCommandInterpreterTask, "UDP CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 void prvUDPCommandInterpreterTask( void *pvParameters )\r
134 {\r
135 int32_t lBytes, lByte;\r
136 char cRxedChar, cInputIndex = 0;\r
137 static char cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
138 static char cLastInputString[ cmdMAX_INPUT_SIZE ], cInputString[ cmdMAX_INPUT_SIZE ];\r
139 BaseType_t xMoreDataToFollow;\r
140 struct freertos_sockaddr xClient;\r
141 \r
142         memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
143 \r
144         /* Attempt to open the socket.  The port number is passed in the task\r
145         parameter.  The strange casting is to remove compiler warnings on 32-bit\r
146         machines. */\r
147         xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );\r
148 \r
149         if( xSocket != FREERTOS_INVALID_SOCKET )\r
150         {\r
151                 for( ;; )\r
152                 {\r
153                         /* Wait for incoming data on the opened socket. */\r
154                         lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );\r
155 \r
156                         if( lBytes > 0 )\r
157                         {\r
158                                 /* Process each received byte in turn. */\r
159                                 lByte = 0;\r
160                                 while( lByte < lBytes )\r
161                                 {\r
162                                         /* The next character in the input buffer. */\r
163                                         cRxedChar = cLocalBuffer[ lByte ];\r
164                                         lByte++;\r
165 \r
166                                         /* Newline characters are taken as the end of the command\r
167                                         string. */\r
168                                         if( cRxedChar == '\n' )\r
169                                         {\r
170                                                 /* Just to space the output from the input. */\r
171                                                 FreeRTOS_sendto( xSocket, pcNewLine,  strlen( pcNewLine ), 0, &xClient, xClientAddressLength );\r
172 \r
173                                                 /* See if the command is empty, indicating that the last command\r
174                                                 is to be executed again. */\r
175                                                 if( cInputIndex == 0 )\r
176                                                 {\r
177                                                         /* Copy the last command back into the input string. */\r
178                                                         strcpy( cInputString, cLastInputString );\r
179                                                 }\r
180 \r
181                                                 /* Process the input string received prior to the\r
182                                                 newline. */\r
183                                                 do\r
184                                                 {\r
185                                                         /* Pass the string to FreeRTOS+CLI. */\r
186                                                         cOutputString[ 0 ] = 0x00;\r
187                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
188 \r
189                                                         /* Send the output generated by the command's\r
190                                                         implementation. */\r
191                                                         FreeRTOS_sendto( xSocket, cOutputString,  strlen( ( const char * ) cOutputString ), 0, &xClient, xClientAddressLength );\r
192 \r
193                                                   /* Until the command does not generate any more output. */\r
194                                                 } while( xMoreDataToFollow != pdFALSE );\r
195 \r
196                                                 /* All the strings generated by the command processing\r
197                                                 have been sent.  Clear the input string ready to receive\r
198                                                 the next command.  Remember the previous command so it\r
199                                                 can be repeated if the user just presses the ENTER key. */\r
200                                                 strcpy( cLastInputString, cInputString );\r
201                                                 cInputIndex = 0;\r
202                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
203 \r
204                                                 /* Transmit a spacer to make the console easier to\r
205                                                 read. */\r
206                                                 FreeRTOS_sendto( xSocket, ( void * ) pcEndOfOutputMessage,  strlen( pcEndOfOutputMessage ), 0, &xClient, xClientAddressLength );\r
207                                         }\r
208                                         else\r
209                                         {\r
210                                                 if( cRxedChar == '\r' )\r
211                                                 {\r
212                                                         /* Ignore the character.  Newlines are used to\r
213                                                         detect the end of the input string. */\r
214                                                 }\r
215                                                 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )\r
216                                                 {\r
217                                                         /* Backspace was pressed.  Erase the last character\r
218                                                         in the string - if any. */\r
219                                                         if( cInputIndex > 0 )\r
220                                                         {\r
221                                                                 cInputIndex--;\r
222                                                                 cInputString[ ( int ) cInputIndex ] = '\0';\r
223                                                         }\r
224                                                 }\r
225                                                 else\r
226                                                 {\r
227                                                         /* A character was entered.  Add it to the string\r
228                                                         entered so far.  When a \n is entered the complete\r
229                                                         string will be passed to the command interpreter. */\r
230                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
231                                                         {\r
232                                                                 cInputString[ ( int ) cInputIndex ] = cRxedChar;\r
233                                                                 cInputIndex++;\r
234                                                         }\r
235                                                 }\r
236                                         }\r
237                                 }\r
238                         }\r
239                 }\r
240         }\r
241         else\r
242         {\r
243                 /* The socket could not be opened. */\r
244                 vTaskDelete( NULL );\r
245         }\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static Socket_t prvOpenUDPServerSocket( uint16_t usPort )\r
250 {\r
251 struct freertos_sockaddr xServer;\r
252 Socket_t xServerSocket = FREERTOS_INVALID_SOCKET;\r
253 TickType_t xSendTimeOut = 0;\r
254 \r
255         xServerSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
256         if( xServerSocket != FREERTOS_INVALID_SOCKET)\r
257         {\r
258                 /* Set to non-blocking sends with a timeout of zero as the socket might\r
259                 also be used for debug prints which should not block. */\r
260                 FreeRTOS_setsockopt( xServerSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );\r
261 \r
262                 /* Zero out the server structure. */\r
263                 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );\r
264 \r
265                 /* Set family and port. */\r
266                 xServer.sin_port = FreeRTOS_htons( usPort );\r
267 \r
268                 /* Bind the address to the socket. */\r
269                 if( FreeRTOS_bind( xServerSocket, &xServer, sizeof( xServer ) ) == -1 )\r
270                 {\r
271                         FreeRTOS_closesocket( xServerSocket );\r
272                         xServerSocket = FREERTOS_INVALID_SOCKET;\r
273                 }\r
274         }\r
275 \r
276         return xServerSocket;\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r