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