]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/DemoTasks/UDPCommandServer.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / DemoTasks / UDPCommandServer.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* Standard includes. */\r
66 #include <stdint.h>\r
67 #include <stdio.h>\r
68 \r
69 /* FreeRTOS includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 /* FreeRTOS+CLI includes. */\r
74 #include "FreeRTOS_CLI.h"\r
75 \r
76 /* FreeRTOS+UDP includes. */\r
77 #include "FreeRTOS_UDP_IP.h"\r
78 #include "FreeRTOS_Sockets.h"\r
79 \r
80 /* Demo app includes. */\r
81 #include "UDPCommandInterpreter.h"\r
82 \r
83 /* Dimensions the buffer into which input characters are placed. */\r
84 #define cmdMAX_INPUT_SIZE       60\r
85 \r
86 /* Dimensions the buffer into which string outputs can be placed. */\r
87 #define cmdMAX_OUTPUT_SIZE      1024\r
88 \r
89 /* Dimensions the buffer passed to the recvfrom() call. */\r
90 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
91 \r
92 /*\r
93  * The task that runs FreeRTOS+CLI.\r
94  */\r
95 void vUDPCommandInterpreterTask( void *pvParameters );\r
96 \r
97 /*\r
98  * Open and configure the UDP socket.\r
99  */\r
100 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort );\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, unsigned portBASE_TYPE uxPriority )\r
105 {\r
106         xTaskCreate( vUDPCommandInterpreterTask, ( signed char * ) "CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
107 }\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* \r
111  * Task that provides the input and output for the FreeRTOS+CLI command\r
112  * interpreter.  In this case a UDP port is used.  See the URL in the comments\r
113  * within main.c for the location of the online documentation.\r
114  */\r
115 void vUDPCommandInterpreterTask( void *pvParameters )\r
116 {\r
117 long lBytes, lByte;\r
118 signed char cInChar, cInputIndex = 0;\r
119 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
120 portBASE_TYPE xMoreDataToFollow;\r
121 struct freertos_sockaddr xClient;\r
122 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
123 xSocket_t xSocket;\r
124 extern const uint8_t ucIPAddress[ 4 ];\r
125 extern const uint8_t ucMACAddress[ 6 ];\r
126 \r
127         /* Just to prevent compiler warnings. */\r
128         ( void ) pvParameters;\r
129 \r
130         /* Attempt to open the socket.  The port number is passed in the task\r
131         parameter.  The strange casting is to remove compiler warnings on 32-bit\r
132         machines. */\r
133         xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );\r
134 \r
135         if( xSocket != FREERTOS_INVALID_SOCKET )\r
136         {\r
137                 for( ;; )\r
138                 {\r
139                         /* Wait for incoming data on the opened socket. */\r
140                         lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );\r
141 \r
142                         if( lBytes != FREERTOS_SOCKET_ERROR )\r
143                         {\r
144                                 /* Process each received byte in turn. */\r
145                                 lByte = 0;\r
146                                 while( lByte < lBytes )\r
147                                 {\r
148                                         /* The next character in the input buffer. */\r
149                                         cInChar = cLocalBuffer[ lByte ];\r
150                                         lByte++;\r
151 \r
152                                         /* Newline characters are taken as the end of the command\r
153                                         string. */\r
154                                         if( cInChar == '\n' )\r
155                                         {\r
156                                                 /* Process the input string received prior to the \r
157                                                 newline. */\r
158                                                 do\r
159                                                 {\r
160                                                         /* Pass the string to FreeRTOS+CLI. */\r
161                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
162                                                         \r
163                                                         /* Send the output generated by the command's\r
164                                                         implementation. */\r
165                                                         FreeRTOS_sendto( xSocket, cOutputString,  strlen( ( const char * ) cOutputString ), 0, &xClient, xClientAddressLength );\r
166 \r
167                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
168 \r
169                                                 /* All the strings generated by the command processing \r
170                                                 have been sent.  Clear the input string ready to receive \r
171                                                 the next command. */\r
172                                                 cInputIndex = 0;\r
173                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
174                                                 \r
175                                                 /* Transmit a spacer, just to make the command console\r
176                                                 easier to read. */\r
177                                                 FreeRTOS_sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, &xClient, xClientAddressLength );\r
178                                         }\r
179                                         else\r
180                                         {\r
181                                                 if( cInChar == '\r' )\r
182                                                 {\r
183                                                         /* Ignore the character.  Newlines are used to \r
184                                                         detect the end of the input string. */\r
185                                                 }\r
186                                                 else if( cInChar == '\b' )\r
187                                                 {\r
188                                                         /* Backspace was pressed.  Erase the last character \r
189                                                         in the string - if any. */\r
190                                                         if( cInputIndex > 0 )\r
191                                                         {\r
192                                                                 cInputIndex--;\r
193                                                                 cInputString[ cInputIndex ] = '\0';\r
194                                                         }\r
195                                                 }\r
196                                                 else\r
197                                                 {\r
198                                                         /* A character was entered.  Add it to the string\r
199                                                         entered so far.  When a \n is entered the complete\r
200                                                         string will be passed to the command interpreter. */\r
201                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
202                                                         {\r
203                                                                 cInputString[ cInputIndex ] = cInChar;\r
204                                                                 cInputIndex++;\r
205                                                         }\r
206                                                 }\r
207                                         }\r
208                                 }\r
209                         }\r
210                 } \r
211         }\r
212         else\r
213         {\r
214                 /* The socket could not be opened. */\r
215                 vTaskDelete( NULL );\r
216         }\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )\r
221 {\r
222 struct freertos_sockaddr xServer;\r
223 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;\r
224 \r
225         xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
226         if( xSocket != FREERTOS_INVALID_SOCKET)\r
227         {\r
228                 /* Zero out the server structure. */\r
229                 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );\r
230 \r
231                 /* Set family and port. */\r
232                 xServer.sin_port = FreeRTOS_htons( usPort );\r
233 \r
234                 /* Bind the address to the socket. */\r
235                 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )\r
236                 {                       \r
237                         FreeRTOS_closesocket( xSocket );\r
238                         xSocket = FREERTOS_INVALID_SOCKET;\r
239                 }\r
240         }\r
241 \r
242         return xSocket;\r
243 }\r
244 \r
245 \r