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