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