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