]> git.sur5r.net Git - freertos/blob
b4b83369d7524115a19ff09591aa4abbf0c05ac9
[freertos] /
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 #pragma comment( lib, "ws2_32.lib" )\r
68 \r
69 /* Win32 includes. */\r
70 #include <WinSock2.h>\r
71 \r
72 /* FreeRTOS includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 /* FreeRTOS+CLI includes. */\r
77 #include "FreeRTOS_CLI.h"\r
78 \r
79 /* Dimensions the buffer into which input characters are placed. */\r
80 #define cmdMAX_INPUT_SIZE       60\r
81 \r
82 /* Dimensions the buffer into which string outputs can be placed. */\r
83 #define cmdMAX_OUTPUT_SIZE      1024\r
84 \r
85 /* Dimensions the buffer passed to the recvfrom() call. */\r
86 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
87 \r
88 /*\r
89  * Open and configure the UDP socket.\r
90  */\r
91 static SOCKET prvOpenUDPSocket( void );\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* \r
96  * Task that provides the input and output for the FreeRTOS+CLI command\r
97  * interpreter.  In this case a UDP port is used.  See the URL in the comments\r
98  * within main.c for the location of the online documentation.\r
99  */\r
100 void vUDPCommandInterpreterTask( void *pvParameters )\r
101 {\r
102 long lBytes, lByte;\r
103 signed char cInChar, cInputIndex = 0;\r
104 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
105 portBASE_TYPE xMoreDataToFollow;\r
106 volatile int iErrorCode = 0;\r
107 struct sockaddr_in xClient;\r
108 int xClientAddressLength = sizeof( struct sockaddr_in );\r
109 SOCKET xSocket;\r
110 \r
111         /* Just to prevent compiler warnings. */\r
112         ( void ) pvParameters;\r
113 \r
114         /* Attempt to open the socket. */\r
115         xSocket = prvOpenUDPSocket();\r
116 \r
117         if( xSocket != INVALID_SOCKET )\r
118         {\r
119                 for( ;; )\r
120                 {\r
121                         /* Wait for incoming data on the opened socket. */\r
122                         lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength );\r
123 \r
124                         if( lBytes == SOCKET_ERROR )\r
125                         {\r
126                                 /* Something went wrong, but it is not handled by this simple\r
127                                 example. */\r
128                                 iErrorCode = WSAGetLastError();\r
129                         }\r
130                         else\r
131                         {                               \r
132                                 /* Process each received byte in turn. */\r
133                                 lByte = 0;\r
134                                 while( lByte < lBytes )\r
135                                 {\r
136                                         /* The next character in the input buffer. */\r
137                                         cInChar = cLocalBuffer[ lByte ];\r
138                                         lByte++;\r
139 \r
140                                         /* Newline characters are taken as the end of the command\r
141                                         string. */\r
142                                         if( cInChar == '\n' )\r
143                                         {\r
144                                                 /* Process the input string received prior to the \r
145                                                 newline. */\r
146                                                 do\r
147                                                 {\r
148                                                         /* Pass the string to FreeRTOS+CLI. */\r
149                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
150                                                         \r
151                                                         /* Send the output generated by the command's\r
152                                                         implementation. */\r
153                                                         sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
154 \r
155                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
156 \r
157                                                 /* All the strings generated by the command processing \r
158                                                 have been sent.  Clear the input string ready to receive \r
159                                                 the next command. */\r
160                                                 cInputIndex = 0;\r
161                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
162                                                 \r
163                                                 /* Transmit a spacer, just to make the command console\r
164                                                 easier to read. */\r
165                                                 sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
166                                         }\r
167                                         else\r
168                                         {\r
169                                                 if( cInChar == '\r' )\r
170                                                 {\r
171                                                         /* Ignore the character.  Newlines are used to \r
172                                                         detect the end of the input string. */\r
173                                                 }\r
174                                                 else if( cInChar == '\b' )\r
175                                                 {\r
176                                                         /* Backspace was pressed.  Erase the last character \r
177                                                         in the string - if any. */\r
178                                                         if( cInputIndex > 0 )\r
179                                                         {\r
180                                                                 cInputIndex--;\r
181                                                                 cInputString[ cInputIndex ] = '\0';\r
182                                                         }\r
183                                                 }\r
184                                                 else\r
185                                                 {\r
186                                                         /* A character was entered.  Add it to the string\r
187                                                         entered so far.  When a \n is entered the complete\r
188                                                         string will be passed to the command interpreter. */\r
189                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
190                                                         {\r
191                                                                 cInputString[ cInputIndex ] = cInChar;\r
192                                                                 cInputIndex++;\r
193                                                         }\r
194                                                 }\r
195                                         }\r
196                                 }\r
197                         }\r
198                 } \r
199         }\r
200         else\r
201         {\r
202                 /* The socket could not be opened. */\r
203                 vTaskDelete( NULL );\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static SOCKET prvOpenUDPSocket( void )\r
209 {\r
210 WSADATA xWSAData;\r
211 WORD wVersionRequested;\r
212 struct sockaddr_in xServer;\r
213 SOCKET xSocket = INVALID_SOCKET;\r
214 \r
215         wVersionRequested = MAKEWORD( 2, 2 );\r
216 \r
217         /* Prepare to use WinSock. */\r
218         if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )\r
219         {\r
220                 fprintf( stderr, "Could not open Windows connection.\n" );\r
221         }\r
222         else\r
223         {\r
224                 xSocket = socket( AF_INET, SOCK_DGRAM, 0 );\r
225                 if( xSocket == INVALID_SOCKET)\r
226                 {\r
227                         fprintf( stderr, "Could not create socket.\n" );\r
228                         WSACleanup();\r
229                 }\r
230                 else\r
231                 {\r
232                         /* Zero out the server structure. */\r
233                         memset( ( void * ) &xServer, 0x00, sizeof( struct sockaddr_in ) );\r
234 \r
235                         /* Set family and port. */\r
236                         xServer.sin_family = AF_INET;\r
237                         xServer.sin_port = htons( configUDP_CLI_PORT_NUMBER );\r
238 \r
239                         /* Assign the loopback address */\r
240                         xServer.sin_addr.S_un.S_un_b.s_b1 = 127;\r
241                         xServer.sin_addr.S_un.S_un_b.s_b2 = 0;\r
242                         xServer.sin_addr.S_un.S_un_b.s_b3 = 0;\r
243                         xServer.sin_addr.S_un.S_un_b.s_b4 = 1;\r
244 \r
245                         /* Bind the address to the socket. */\r
246                         if( bind( xSocket, ( struct sockaddr * ) &xServer, sizeof( struct sockaddr_in ) ) == -1 )\r
247                         {\r
248                                 fprintf( stderr, "Could not socket to port %d.\n", configUDP_CLI_PORT_NUMBER );\r
249                                 closesocket( xSocket );\r
250                                 xSocket = INVALID_SOCKET;\r
251                                 WSACleanup();\r
252                         }\r
253                 }\r
254         }\r
255 \r
256         return xSocket;\r
257 }\r
258 \r
259 \r