2 FreeRTOS.org V4.0.5 - Copyright (C) 2003-2006 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
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
30 ***************************************************************************
\r
34 * Very simple task that responds with a single WEB page to http requests.
\r
36 * The WEB page displays task and system status. A semaphore is used to
\r
37 * wake the task when there is processing to perform as determined by the
\r
38 * interrupts generated by the Ethernet interface.
\r
41 /* Standard includes. */
\r
45 /* Tern includes. */
\r
46 #include "utils\system_common.h"
\r
47 #include "i2chip_hw.h"
\r
50 /* FreeRTOS.org includes. */
\r
51 #include <FreeRTOS.h>
\r
55 /* The standard http port on which we are going to listen. */
\r
58 #define httpTX_WAIT 2
\r
60 /* Network address configuration. */
\r
61 const unsigned portCHAR ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
62 const unsigned portCHAR ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
63 const unsigned portCHAR ucIPAddress[] = { 172, 25, 218, 210 };
\r
64 const unsigned portCHAR ucSubnetMask[] = { 255, 255, 255, 0 };
\r
66 /* The number of sockets this task is going to handle. */
\r
67 #define httpSOCKET_NUM 3
\r
68 unsigned portCHAR ucConnection[ httpSOCKET_NUM ];
\r
70 /* The maximum data buffer size we can handle. */
\r
71 #define httpSOCKET_BUFFER_SIZE 2048
\r
73 /* Standard HTTP response. */
\r
74 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
76 /* Hard coded HTML components. Other data is generated dynamically. */
\r
77 #define HTML_OUTPUT_BEGIN "\
\r
78 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
79 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
80 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
81 <HR>Task status table:\r\n\
\r
82 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
83 ************************************************<br>"
\r
85 #define HTML_OUTPUT_END "\
\r
86 </font></BODY></HTML>"
\r
88 /*-----------------------------------------------------------*/
\r
91 * Initialise the data structures used to hold the socket status.
\r
93 static void prvHTTPInit( void );
\r
96 * Setup the Ethernet interface with the network addressing information.
\r
98 static void prvNetifInit( void );
\r
101 * Generate the dynamic components of the served WEB page and transmit the
\r
102 * entire page through the socket.
\r
104 static void prvTransmitHTTP( unsigned portCHAR socket );
\r
105 /*-----------------------------------------------------------*/
\r
107 /* This variable is simply incremented by the idle task hook so the number of
\r
108 iterations the idle task has performed can be displayed as part of the served
\r
110 unsigned portLONG ulIdleLoops = 0UL;
\r
112 /* Data buffer shared by sockets. */
\r
113 unsigned portCHAR ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
115 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
116 and process whatever caused the interrupt. */
\r
117 xSemaphoreHandle xTCPSemaphore = NULL;
\r
119 /*-----------------------------------------------------------*/
\r
120 void vHTTPTask( void * pvParameters )
\r
123 unsigned portCHAR ucState;
\r
125 ( void ) pvParameters;
\r
127 /* Create the semaphore used to communicate between this task and the
\r
129 vSemaphoreCreateBinary( xTCPSemaphore );
\r
131 /* Make sure everything is setup before we start. */
\r
137 /* Wait until the ISR tells us there is something to do. */
\r
138 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
140 /* Check each socket. */
\r
141 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
143 ucState = select( i, SEL_CONTROL );
\r
147 case SOCK_ESTABLISHED : /* new connection established. */
\r
149 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
151 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
153 sLen = httpSOCKET_BUFFER_SIZE;
\r
158 sLen = recv( i, ucSocketBuffer, sLen );
\r
160 if( ucConnection[ i ] == 1 )
\r
162 /* This is our first time processing a HTTP
\r
163 request on this connection. */
\r
164 prvTransmitHTTP( i );
\r
165 ucConnection[i] = 0;
\r
171 case SOCK_CLOSE_WAIT :
\r
178 ucConnection[i] = 1;
\r
179 socket( i, SOCK_STREAM, 80, 0x00 );
\r
180 NBlisten( i ); /* reinitialize socket. */
\r
186 /*-----------------------------------------------------------*/
\r
188 static void prvHTTPInit( void )
\r
190 unsigned portCHAR ucIndex;
\r
192 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
193 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
195 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
196 NBlisten( ucIndex );
\r
197 ucConnection[ ucIndex ] = 1;
\r
200 /*-----------------------------------------------------------*/
\r
202 static void prvNetifInit( void )
\r
207 setMACAddr( ( unsigned portCHAR * ) ucMacAddress );
\r
208 setgateway( ( unsigned portCHAR * ) ucGatewayAddress );
\r
209 setsubmask( ( unsigned portCHAR * ) ucSubnetMask );
\r
210 setIP( ( unsigned portCHAR * ) ucIPAddress );
\r
212 /* See definition of 'sysinit' in socket.c
\r
213 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
214 are shared by all 4 channels.
\r
215 - (0x55, 0x55) configures the send and receive buffers at
\r
216 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
217 sysinit( 0x55, 0x55 );
\r
219 /*-----------------------------------------------------------*/
\r
221 static void prvTransmitHTTP(unsigned portCHAR socket)
\r
223 extern portSHORT usCheckStatus;
\r
225 /* Send the http and html headers. */
\r
226 send( socket, ( unsigned portCHAR * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
227 send( socket, ( unsigned portCHAR * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
229 /* Generate then send the table showing the status of each task. */
\r
230 vTaskList( ucSocketBuffer );
\r
231 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
233 /* Send the number of times the idle task has looped. */
\r
234 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
235 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
237 /* Send the tick count. */
\r
238 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
239 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
241 /* Show a message indicating whether or not the check task has discovered
\r
242 an error in any of the standard demo tasks. */
\r
243 if( usCheckStatus == 0 )
\r
245 sprintf( ucSocketBuffer, "No errors detected." );
\r
246 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
250 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
251 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
254 /* Finish the page off. */
\r
255 send( socket, (unsigned portCHAR*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
257 /* Must make sure the data is gone before closing the socket. */
\r
258 while( !tx_empty( socket ) )
\r
260 vTaskDelay( httpTX_WAIT );
\r
264 /*-----------------------------------------------------------*/
\r
266 void vApplicationIdleHook( void )
\r