2 FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * Very simple task that responds with a single WEB page to http requests.
\r
55 * The WEB page displays task and system status. A semaphore is used to
\r
56 * wake the task when there is processing to perform as determined by the
\r
57 * interrupts generated by the Ethernet interface.
\r
60 /* Standard includes. */
\r
64 /* Tern includes. */
\r
65 #include "utils\system_common.h"
\r
66 #include "i2chip_hw.h"
\r
69 /* FreeRTOS.org includes. */
\r
70 #include <FreeRTOS.h>
\r
74 /* The standard http port on which we are going to listen. */
\r
77 #define httpTX_WAIT 2
\r
79 /* Network address configuration. */
\r
80 const unsigned portCHAR ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
81 const unsigned portCHAR ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
82 const unsigned portCHAR ucIPAddress[] = { 172, 25, 218, 210 };
\r
83 const unsigned portCHAR ucSubnetMask[] = { 255, 255, 255, 0 };
\r
85 /* The number of sockets this task is going to handle. */
\r
86 #define httpSOCKET_NUM 3
\r
87 unsigned portCHAR ucConnection[ httpSOCKET_NUM ];
\r
89 /* The maximum data buffer size we can handle. */
\r
90 #define httpSOCKET_BUFFER_SIZE 2048
\r
92 /* Standard HTTP response. */
\r
93 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
95 /* Hard coded HTML components. Other data is generated dynamically. */
\r
96 #define HTML_OUTPUT_BEGIN "\
\r
97 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
98 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
99 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
100 <HR>Task status table:\r\n\
\r
101 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
102 ************************************************<br>"
\r
104 #define HTML_OUTPUT_END "\
\r
105 </font></BODY></HTML>"
\r
107 /*-----------------------------------------------------------*/
\r
110 * Initialise the data structures used to hold the socket status.
\r
112 static void prvHTTPInit( void );
\r
115 * Setup the Ethernet interface with the network addressing information.
\r
117 static void prvNetifInit( void );
\r
120 * Generate the dynamic components of the served WEB page and transmit the
\r
121 * entire page through the socket.
\r
123 static void prvTransmitHTTP( unsigned portCHAR socket );
\r
124 /*-----------------------------------------------------------*/
\r
126 /* This variable is simply incremented by the idle task hook so the number of
\r
127 iterations the idle task has performed can be displayed as part of the served
\r
129 unsigned portLONG ulIdleLoops = 0UL;
\r
131 /* Data buffer shared by sockets. */
\r
132 unsigned portCHAR ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
134 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
135 and process whatever caused the interrupt. */
\r
136 xSemaphoreHandle xTCPSemaphore = NULL;
\r
138 /*-----------------------------------------------------------*/
\r
139 void vHTTPTask( void * pvParameters )
\r
142 unsigned portCHAR ucState;
\r
144 ( void ) pvParameters;
\r
146 /* Create the semaphore used to communicate between this task and the
\r
148 vSemaphoreCreateBinary( xTCPSemaphore );
\r
150 /* Make sure everything is setup before we start. */
\r
156 /* Wait until the ISR tells us there is something to do. */
\r
157 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
159 /* Check each socket. */
\r
160 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
162 ucState = select( i, SEL_CONTROL );
\r
166 case SOCK_ESTABLISHED : /* new connection established. */
\r
168 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
170 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
172 sLen = httpSOCKET_BUFFER_SIZE;
\r
177 sLen = recv( i, ucSocketBuffer, sLen );
\r
179 if( ucConnection[ i ] == 1 )
\r
181 /* This is our first time processing a HTTP
\r
182 request on this connection. */
\r
183 prvTransmitHTTP( i );
\r
184 ucConnection[i] = 0;
\r
190 case SOCK_CLOSE_WAIT :
\r
197 ucConnection[i] = 1;
\r
198 socket( i, SOCK_STREAM, 80, 0x00 );
\r
199 NBlisten( i ); /* reinitialize socket. */
\r
205 /*-----------------------------------------------------------*/
\r
207 static void prvHTTPInit( void )
\r
209 unsigned portCHAR ucIndex;
\r
211 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
212 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
214 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
215 NBlisten( ucIndex );
\r
216 ucConnection[ ucIndex ] = 1;
\r
219 /*-----------------------------------------------------------*/
\r
221 static void prvNetifInit( void )
\r
226 setMACAddr( ( unsigned portCHAR * ) ucMacAddress );
\r
227 setgateway( ( unsigned portCHAR * ) ucGatewayAddress );
\r
228 setsubmask( ( unsigned portCHAR * ) ucSubnetMask );
\r
229 setIP( ( unsigned portCHAR * ) ucIPAddress );
\r
231 /* See definition of 'sysinit' in socket.c
\r
232 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
233 are shared by all 4 channels.
\r
234 - (0x55, 0x55) configures the send and receive buffers at
\r
235 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
236 sysinit( 0x55, 0x55 );
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvTransmitHTTP(unsigned portCHAR socket)
\r
242 extern portSHORT usCheckStatus;
\r
244 /* Send the http and html headers. */
\r
245 send( socket, ( unsigned portCHAR * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
246 send( socket, ( unsigned portCHAR * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
248 /* Generate then send the table showing the status of each task. */
\r
249 vTaskList( ucSocketBuffer );
\r
250 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
252 /* Send the number of times the idle task has looped. */
\r
253 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
254 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
256 /* Send the tick count. */
\r
257 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
258 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
260 /* Show a message indicating whether or not the check task has discovered
\r
261 an error in any of the standard demo tasks. */
\r
262 if( usCheckStatus == 0 )
\r
264 sprintf( ucSocketBuffer, "No errors detected." );
\r
265 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
269 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
270 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
273 /* Finish the page off. */
\r
274 send( socket, (unsigned portCHAR*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
276 /* Must make sure the data is gone before closing the socket. */
\r
277 while( !tx_empty( socket ) )
\r
279 vTaskDelay( httpTX_WAIT );
\r
283 /*-----------------------------------------------------------*/
\r
285 void vApplicationIdleHook( void )
\r