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