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