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