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