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