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