]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WizNET_DEMO_TERN_186/HTTPTask.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / WizNET_DEMO_TERN_186 / HTTPTask.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /* \r
70  * Very simple task that responds with a single WEB page to http requests.\r
71  *\r
72  * The WEB page displays task and system status.  A semaphore is used to \r
73  * wake the task when there is processing to perform as determined by the \r
74  * interrupts generated by the Ethernet interface.\r
75  */\r
76 \r
77 /* Standard includes. */\r
78 #include <string.h>\r
79 #include <stdio.h>\r
80 \r
81 /* Tern includes. */\r
82 #include "utils\system_common.h"\r
83 #include "i2chip_hw.h"\r
84 #include "socket.h"\r
85 \r
86 /* FreeRTOS.org includes. */\r
87 #include <FreeRTOS.h>\r
88 #include <task.h>\r
89 #include <semphr.h>\r
90 \r
91 /* The standard http port on which we are going to listen. */\r
92 #define httpPORT 80\r
93 \r
94 #define httpTX_WAIT 2\r
95 \r
96 /* Network address configuration. */\r
97 const unsigned char ucMacAddress[] =                    { 12, 128, 12, 34, 56, 78 };\r
98 const unsigned char ucGatewayAddress[] =                { 192, 168, 2, 1 };\r
99 const unsigned char ucIPAddress[] =                             { 172, 25, 218, 210 };\r
100 const unsigned char ucSubnetMask[] =                    { 255, 255, 255, 0 };\r
101 \r
102 /* The number of sockets this task is going to handle. */\r
103 #define httpSOCKET_NUM                       3\r
104 unsigned char ucConnection[ httpSOCKET_NUM ];\r
105 \r
106 /* The maximum data buffer size we can handle. */\r
107 #define httpSOCKET_BUFFER_SIZE  2048\r
108 \r
109 /* Standard HTTP response. */\r
110 #define httpOUTPUT_OK   "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"\r
111 \r
112 /* Hard coded HTML components.  Other data is generated dynamically. */\r
113 #define HTML_OUTPUT_BEGIN "\\r
114 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\\r
115 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\\r
116 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\\r
117 <HR>Task status table:\r\n\\r
118 <p><font face=\"courier\"><pre>Task          State  Priority  Stack     #<br>\\r
119 ************************************************<br>"\r
120 \r
121 #define HTML_OUTPUT_END   "\\r
122 </font></BODY></HTML>"\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * Initialise the data structures used to hold the socket status.\r
128  */\r
129 static void prvHTTPInit( void );\r
130 \r
131 /*\r
132  * Setup the Ethernet interface with the network addressing information.\r
133  */\r
134 static void prvNetifInit( void );\r
135 \r
136 /*\r
137  * Generate the dynamic components of the served WEB page and transmit the \r
138  * entire page through the socket.\r
139  */\r
140 static void prvTransmitHTTP( unsigned char socket );\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /* This variable is simply incremented by the idle task hook so the number of\r
144 iterations the idle task has performed can be displayed as part of the served\r
145 page. */\r
146 unsigned long ulIdleLoops = 0UL;\r
147 \r
148 /* Data buffer shared by sockets. */\r
149 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];\r
150 \r
151 /* The semaphore used by the Ethernet ISR to signal that the task should wake\r
152 and process whatever caused the interrupt. */\r
153 xSemaphoreHandle xTCPSemaphore = NULL;\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 void vHTTPTask( void * pvParameters )\r
157 {\r
158 short i, sLen;\r
159 unsigned char ucState;\r
160 \r
161         ( void ) pvParameters;\r
162 \r
163     /* Create the semaphore used to communicate between this task and the\r
164     WIZnet ISR. */\r
165     vSemaphoreCreateBinary( xTCPSemaphore );\r
166 \r
167         /* Make sure everything is setup before we start. */\r
168         prvNetifInit();\r
169         prvHTTPInit();\r
170 \r
171         for( ;; )\r
172         {\r
173                 /* Wait until the ISR tells us there is something to do. */\r
174         xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );\r
175 \r
176                 /* Check each socket. */\r
177                 for( i = 0; i < httpSOCKET_NUM; i++ )\r
178                 {\r
179                         ucState = select( i, SEL_CONTROL );\r
180 \r
181                         switch (ucState)\r
182                         {\r
183                                 case SOCK_ESTABLISHED :  /* new connection established. */\r
184 \r
185                                         if( ( sLen = select( i, SEL_RECV ) ) > 0 )\r
186                                         {\r
187                                                 if( sLen > httpSOCKET_BUFFER_SIZE ) \r
188                                                 {\r
189                                                         sLen = httpSOCKET_BUFFER_SIZE;\r
190                                                 }\r
191 \r
192                                                 disable();\r
193                                                 \r
194                                                 sLen = recv( i, ucSocketBuffer, sLen );    \r
195 \r
196                                                 if( ucConnection[ i ] == 1 )\r
197                                                 {       \r
198                                                         /* This is our first time processing a HTTP\r
199                                                          request on this connection. */\r
200                                                         prvTransmitHTTP( i );\r
201                                                         ucConnection[i] = 0;\r
202                                                 }\r
203                                                 enable();\r
204                                         }\r
205                                         break;\r
206 \r
207                                 case SOCK_CLOSE_WAIT :\r
208 \r
209                                         close(i);\r
210                                         break;\r
211 \r
212                                 case SOCK_CLOSED :\r
213 \r
214                                         ucConnection[i] = 1;\r
215                                         socket( i, SOCK_STREAM, 80, 0x00 );\r
216                                         NBlisten( i ); /* reinitialize socket. */\r
217                                         break;\r
218                         }\r
219                 }\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void prvHTTPInit( void )\r
225 {\r
226 unsigned char ucIndex;\r
227 \r
228         /* There are 4 total sockets available; we will claim 3 for HTTP. */\r
229         for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)\r
230         {\r
231                 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );\r
232                 NBlisten( ucIndex );\r
233                 ucConnection[ ucIndex ] = 1;\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvNetifInit( void )\r
239 {\r
240         i2chip_init();\r
241         initW3100A();\r
242 \r
243         setMACAddr( ( unsigned char * ) ucMacAddress );\r
244         setgateway( ( unsigned char * ) ucGatewayAddress );\r
245         setsubmask( ( unsigned char * ) ucSubnetMask );\r
246         setIP( ( unsigned char * ) ucIPAddress );\r
247 \r
248         /* See definition of 'sysinit' in socket.c\r
249          - 8 KB transmit buffer, and 8 KB receive buffer available.  These buffers\r
250            are shared by all 4 channels.\r
251          - (0x55, 0x55) configures the send and receive buffers at \r
252                 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */\r
253         sysinit( 0x55, 0x55 );\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 static void prvTransmitHTTP(unsigned char socket)\r
258 {\r
259 extern short usCheckStatus;\r
260 \r
261         /* Send the http and html headers. */\r
262         send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );\r
263         send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );\r
264 \r
265         /* Generate then send the table showing the status of each task. */\r
266         vTaskList( ucSocketBuffer );\r
267         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
268 \r
269         /* Send the number of times the idle task has looped. */\r
270     sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );\r
271         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
272 \r
273         /* Send the tick count. */\r
274     sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );\r
275         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
276 \r
277         /* Show a message indicating whether or not the check task has discovered \r
278         an error in any of the standard demo tasks. */\r
279     if( usCheckStatus == 0 )\r
280     {\r
281             sprintf( ucSocketBuffer, "No errors detected." );\r
282                 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
283     }\r
284     else\r
285     {\r
286             sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );\r
287                 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
288     }\r
289 \r
290         /* Finish the page off. */\r
291         send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));\r
292 \r
293         /* Must make sure the data is gone before closing the socket. */\r
294         while( !tx_empty( socket ) )\r
295     {\r
296         vTaskDelay( httpTX_WAIT );\r
297     }\r
298         close(socket);\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 void vApplicationIdleHook( void )\r
303 {\r
304         ulIdleLoops++;\r
305 }\r
306 \r
307 \r
308 \r
309 \r
310 \r
311 \r
312 \r
313 \r
314 \r
315 \r
316 \r
317 \r
318 \r
319 \r
320 \r
321 \r
322 \r
323 \r
324 \r
325 \r