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