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