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