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