]> git.sur5r.net Git - freertos/blob - Demo/WizNET_DEMO_TERN_186/HTTPTask.c
Prepare for V5.3.0 release.
[freertos] / Demo / WizNET_DEMO_TERN_186 / HTTPTask.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* \r
53  * Very simple task that responds with a single WEB page to http requests.\r
54  *\r
55  * The WEB page displays task and system status.  A semaphore is used to \r
56  * wake the task when there is processing to perform as determined by the \r
57  * interrupts generated by the Ethernet interface.\r
58  */\r
59 \r
60 /* Standard includes. */\r
61 #include <string.h>\r
62 #include <stdio.h>\r
63 \r
64 /* Tern includes. */\r
65 #include "utils\system_common.h"\r
66 #include "i2chip_hw.h"\r
67 #include "socket.h"\r
68 \r
69 /* FreeRTOS.org includes. */\r
70 #include <FreeRTOS.h>\r
71 #include <task.h>\r
72 #include <semphr.h>\r
73 \r
74 /* The standard http port on which we are going to listen. */\r
75 #define httpPORT 80\r
76 \r
77 #define httpTX_WAIT 2\r
78 \r
79 /* Network address configuration. */\r
80 const unsigned portCHAR ucMacAddress[] =                        { 12, 128, 12, 34, 56, 78 };\r
81 const unsigned portCHAR ucGatewayAddress[] =            { 192, 168, 2, 1 };\r
82 const unsigned portCHAR ucIPAddress[] =                         { 172, 25, 218, 210 };\r
83 const unsigned portCHAR ucSubnetMask[] =                        { 255, 255, 255, 0 };\r
84 \r
85 /* The number of sockets this task is going to handle. */\r
86 #define httpSOCKET_NUM                       3\r
87 unsigned portCHAR ucConnection[ httpSOCKET_NUM ];\r
88 \r
89 /* The maximum data buffer size we can handle. */\r
90 #define httpSOCKET_BUFFER_SIZE  2048\r
91 \r
92 /* Standard HTTP response. */\r
93 #define httpOUTPUT_OK   "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"\r
94 \r
95 /* Hard coded HTML components.  Other data is generated dynamically. */\r
96 #define HTML_OUTPUT_BEGIN "\\r
97 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\\r
98 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\\r
99 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\\r
100 <HR>Task status table:\r\n\\r
101 <p><font face=\"courier\"><pre>Task          State  Priority  Stack     #<br>\\r
102 ************************************************<br>"\r
103 \r
104 #define HTML_OUTPUT_END   "\\r
105 </font></BODY></HTML>"\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /*\r
110  * Initialise the data structures used to hold the socket status.\r
111  */\r
112 static void prvHTTPInit( void );\r
113 \r
114 /*\r
115  * Setup the Ethernet interface with the network addressing information.\r
116  */\r
117 static void prvNetifInit( void );\r
118 \r
119 /*\r
120  * Generate the dynamic components of the served WEB page and transmit the \r
121  * entire page through the socket.\r
122  */\r
123 static void prvTransmitHTTP( unsigned portCHAR socket );\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /* This variable is simply incremented by the idle task hook so the number of\r
127 iterations the idle task has performed can be displayed as part of the served\r
128 page. */\r
129 unsigned portLONG ulIdleLoops = 0UL;\r
130 \r
131 /* Data buffer shared by sockets. */\r
132 unsigned portCHAR ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];\r
133 \r
134 /* The semaphore used by the Ethernet ISR to signal that the task should wake\r
135 and process whatever caused the interrupt. */\r
136 xSemaphoreHandle xTCPSemaphore = NULL;\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 void vHTTPTask( void * pvParameters )\r
140 {\r
141 portSHORT i, sLen;\r
142 unsigned portCHAR ucState;\r
143 \r
144         ( void ) pvParameters;\r
145 \r
146     /* Create the semaphore used to communicate between this task and the\r
147     WIZnet ISR. */\r
148     vSemaphoreCreateBinary( xTCPSemaphore );\r
149 \r
150         /* Make sure everything is setup before we start. */\r
151         prvNetifInit();\r
152         prvHTTPInit();\r
153 \r
154         for( ;; )\r
155         {\r
156                 /* Wait until the ISR tells us there is something to do. */\r
157         xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );\r
158 \r
159                 /* Check each socket. */\r
160                 for( i = 0; i < httpSOCKET_NUM; i++ )\r
161                 {\r
162                         ucState = select( i, SEL_CONTROL );\r
163 \r
164                         switch (ucState)\r
165                         {\r
166                                 case SOCK_ESTABLISHED :  /* new connection established. */\r
167 \r
168                                         if( ( sLen = select( i, SEL_RECV ) ) > 0 )\r
169                                         {\r
170                                                 if( sLen > httpSOCKET_BUFFER_SIZE ) \r
171                                                 {\r
172                                                         sLen = httpSOCKET_BUFFER_SIZE;\r
173                                                 }\r
174 \r
175                                                 disable();\r
176                                                 \r
177                                                 sLen = recv( i, ucSocketBuffer, sLen );    \r
178 \r
179                                                 if( ucConnection[ i ] == 1 )\r
180                                                 {       \r
181                                                         /* This is our first time processing a HTTP\r
182                                                          request on this connection. */\r
183                                                         prvTransmitHTTP( i );\r
184                                                         ucConnection[i] = 0;\r
185                                                 }\r
186                                                 enable();\r
187                                         }\r
188                                         break;\r
189 \r
190                                 case SOCK_CLOSE_WAIT :\r
191 \r
192                                         close(i);\r
193                                         break;\r
194 \r
195                                 case SOCK_CLOSED :\r
196 \r
197                                         ucConnection[i] = 1;\r
198                                         socket( i, SOCK_STREAM, 80, 0x00 );\r
199                                         NBlisten( i ); /* reinitialize socket. */\r
200                                         break;\r
201                         }\r
202                 }\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void prvHTTPInit( void )\r
208 {\r
209 unsigned portCHAR ucIndex;\r
210 \r
211         /* There are 4 total sockets available; we will claim 3 for HTTP. */\r
212         for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)\r
213         {\r
214                 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );\r
215                 NBlisten( ucIndex );\r
216                 ucConnection[ ucIndex ] = 1;\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvNetifInit( void )\r
222 {\r
223         i2chip_init();\r
224         initW3100A();\r
225 \r
226         setMACAddr( ( unsigned portCHAR * ) ucMacAddress );\r
227         setgateway( ( unsigned portCHAR * ) ucGatewayAddress );\r
228         setsubmask( ( unsigned portCHAR * ) ucSubnetMask );\r
229         setIP( ( unsigned portCHAR * ) ucIPAddress );\r
230 \r
231         /* See definition of 'sysinit' in socket.c\r
232          - 8 KB transmit buffer, and 8 KB receive buffer available.  These buffers\r
233            are shared by all 4 channels.\r
234          - (0x55, 0x55) configures the send and receive buffers at \r
235                 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */\r
236         sysinit( 0x55, 0x55 );\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 static void prvTransmitHTTP(unsigned portCHAR socket)\r
241 {\r
242 extern portSHORT usCheckStatus;\r
243 \r
244         /* Send the http and html headers. */\r
245         send( socket, ( unsigned portCHAR * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );\r
246         send( socket, ( unsigned portCHAR * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );\r
247 \r
248         /* Generate then send the table showing the status of each task. */\r
249         vTaskList( ucSocketBuffer );\r
250         send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
251 \r
252         /* Send the number of times the idle task has looped. */\r
253     sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );\r
254         send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
255 \r
256         /* Send the tick count. */\r
257     sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );\r
258         send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
259 \r
260         /* Show a message indicating whether or not the check task has discovered \r
261         an error in any of the standard demo tasks. */\r
262     if( usCheckStatus == 0 )\r
263     {\r
264             sprintf( ucSocketBuffer, "No errors detected." );\r
265                 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
266     }\r
267     else\r
268     {\r
269             sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );\r
270                 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
271     }\r
272 \r
273         /* Finish the page off. */\r
274         send( socket, (unsigned portCHAR*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));\r
275 \r
276         /* Must make sure the data is gone before closing the socket. */\r
277         while( !tx_empty( socket ) )\r
278     {\r
279         vTaskDelay( httpTX_WAIT );\r
280     }\r
281         close(socket);\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 void vApplicationIdleHook( void )\r
286 {\r
287         ulIdleLoops++;\r
288 }\r
289 \r
290 \r
291 \r
292 \r
293 \r
294 \r
295 \r
296 \r
297 \r
298 \r
299 \r
300 \r
301 \r
302 \r
303 \r
304 \r
305 \r
306 \r
307 \r
308 \r