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