]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/DemoTasks/UDPCommandServer.c
Update FreeRTOS+ version number ready for version 9 release candidate 1.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / DemoTasks / UDPCommandServer.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - 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 \r
74 /* FreeRTOS includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 \r
78 /* FreeRTOS+CLI includes. */\r
79 #include "FreeRTOS_CLI.h"\r
80 \r
81 /* FreeRTOS+UDP includes. */\r
82 #include "FreeRTOS_UDP_IP.h"\r
83 #include "FreeRTOS_Sockets.h"\r
84 \r
85 /* Demo app includes. */\r
86 #include "UDPCommandInterpreter.h"\r
87 \r
88 /* Dimensions the buffer into which input characters are placed. */\r
89 #define cmdMAX_INPUT_SIZE       60\r
90 \r
91 /* Dimensions the buffer into which string outputs can be placed. */\r
92 #define cmdMAX_OUTPUT_SIZE      1024\r
93 \r
94 /* Dimensions the buffer passed to the recvfrom() call. */\r
95 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
96 \r
97 /*\r
98  * The task that runs FreeRTOS+CLI.\r
99  */\r
100 void vUDPCommandInterpreterTask( void *pvParameters );\r
101 \r
102 /*\r
103  * Open and configure the UDP socket.\r
104  */\r
105 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort );\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )\r
110 {\r
111         xTaskCreate( vUDPCommandInterpreterTask, "CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Task that provides the input and output for the FreeRTOS+CLI command\r
117  * interpreter.  In this case a UDP port is used.  See the URL in the comments\r
118  * within main.c for the location of the online documentation.\r
119  */\r
120 void vUDPCommandInterpreterTask( void *pvParameters )\r
121 {\r
122 long lBytes, lByte;\r
123 signed char cInChar, cInputIndex = 0;\r
124 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
125 BaseType_t xMoreDataToFollow;\r
126 struct freertos_sockaddr xClient;\r
127 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
128 xSocket_t xSocket;\r
129 extern const uint8_t ucIPAddress[ 4 ];\r
130 extern const uint8_t ucMACAddress[ 6 ];\r
131 \r
132         /* Just to prevent compiler warnings. */\r
133         ( void ) pvParameters;\r
134 \r
135         /* Attempt to open the socket.  The port number is passed in the task\r
136         parameter.  The strange casting is to remove compiler warnings on 32-bit\r
137         machines. */\r
138         xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );\r
139 \r
140         if( xSocket != FREERTOS_INVALID_SOCKET )\r
141         {\r
142                 for( ;; )\r
143                 {\r
144                         /* Wait for incoming data on the opened socket. */\r
145                         lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );\r
146 \r
147                         if( lBytes != FREERTOS_SOCKET_ERROR )\r
148                         {\r
149                                 /* Process each received byte in turn. */\r
150                                 lByte = 0;\r
151                                 while( lByte < lBytes )\r
152                                 {\r
153                                         /* The next character in the input buffer. */\r
154                                         cInChar = cLocalBuffer[ lByte ];\r
155                                         lByte++;\r
156 \r
157                                         /* Newline characters are taken as the end of the command\r
158                                         string. */\r
159                                         if( cInChar == '\n' )\r
160                                         {\r
161                                                 /* Process the input string received prior to the\r
162                                                 newline. */\r
163                                                 do\r
164                                                 {\r
165                                                         /* Pass the string to FreeRTOS+CLI. */\r
166                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
167 \r
168                                                         /* Send the output generated by the command's\r
169                                                         implementation. */\r
170                                                         FreeRTOS_sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, &xClient, xClientAddressLength );\r
171 \r
172                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
173 \r
174                                                 /* All the strings generated by the command processing\r
175                                                 have been sent.  Clear the input string ready to receive\r
176                                                 the next command. */\r
177                                                 cInputIndex = 0;\r
178                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
179 \r
180                                                 /* Transmit a spacer, just to make the command console\r
181                                                 easier to read. */\r
182                                                 FreeRTOS_sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, &xClient, xClientAddressLength );\r
183                                         }\r
184                                         else\r
185                                         {\r
186                                                 if( cInChar == '\r' )\r
187                                                 {\r
188                                                         /* Ignore the character.  Newlines are used to\r
189                                                         detect the end of the input string. */\r
190                                                 }\r
191                                                 else if( cInChar == '\b' )\r
192                                                 {\r
193                                                         /* Backspace was pressed.  Erase the last character\r
194                                                         in the string - if any. */\r
195                                                         if( cInputIndex > 0 )\r
196                                                         {\r
197                                                                 cInputIndex--;\r
198                                                                 cInputString[ cInputIndex ] = '\0';\r
199                                                         }\r
200                                                 }\r
201                                                 else\r
202                                                 {\r
203                                                         /* A character was entered.  Add it to the string\r
204                                                         entered so far.  When a \n is entered the complete\r
205                                                         string will be passed to the command interpreter. */\r
206                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
207                                                         {\r
208                                                                 cInputString[ cInputIndex ] = cInChar;\r
209                                                                 cInputIndex++;\r
210                                                         }\r
211                                                 }\r
212                                         }\r
213                                 }\r
214                         }\r
215                 }\r
216         }\r
217         else\r
218         {\r
219                 /* The socket could not be opened. */\r
220                 vTaskDelete( NULL );\r
221         }\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )\r
226 {\r
227 struct freertos_sockaddr xServer;\r
228 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;\r
229 \r
230         xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
231         if( xSocket != FREERTOS_INVALID_SOCKET)\r
232         {\r
233                 /* Zero out the server structure. */\r
234                 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );\r
235 \r
236                 /* Set family and port. */\r
237                 xServer.sin_port = FreeRTOS_htons( usPort );\r
238 \r
239                 /* Bind the address to the socket. */\r
240                 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )\r
241                 {\r
242                         FreeRTOS_closesocket( xSocket );\r
243                         xSocket = FREERTOS_INVALID_SOCKET;\r
244                 }\r
245         }\r
246 \r
247         return xSocket;\r
248 }\r
249 \r
250 \r