2 FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 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 without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 * Very simple task that responds with a single WEB page to http requests.
\r
51 * The WEB page displays task and system status. A semaphore is used to
\r
52 * wake the task when there is processing to perform as determined by the
\r
53 * interrupts generated by the Ethernet interface.
\r
56 /* Standard includes. */
\r
60 /* Tern includes. */
\r
61 #include "utils\system_common.h"
\r
62 #include "i2chip_hw.h"
\r
65 /* FreeRTOS.org includes. */
\r
66 #include <FreeRTOS.h>
\r
70 /* The standard http port on which we are going to listen. */
\r
73 #define httpTX_WAIT 2
\r
75 /* Network address configuration. */
\r
76 const unsigned portCHAR ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
77 const unsigned portCHAR ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
78 const unsigned portCHAR ucIPAddress[] = { 172, 25, 218, 210 };
\r
79 const unsigned portCHAR ucSubnetMask[] = { 255, 255, 255, 0 };
\r
81 /* The number of sockets this task is going to handle. */
\r
82 #define httpSOCKET_NUM 3
\r
83 unsigned portCHAR ucConnection[ httpSOCKET_NUM ];
\r
85 /* The maximum data buffer size we can handle. */
\r
86 #define httpSOCKET_BUFFER_SIZE 2048
\r
88 /* Standard HTTP response. */
\r
89 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
91 /* Hard coded HTML components. Other data is generated dynamically. */
\r
92 #define HTML_OUTPUT_BEGIN "\
\r
93 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
94 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
95 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
96 <HR>Task status table:\r\n\
\r
97 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
98 ************************************************<br>"
\r
100 #define HTML_OUTPUT_END "\
\r
101 </font></BODY></HTML>"
\r
103 /*-----------------------------------------------------------*/
\r
106 * Initialise the data structures used to hold the socket status.
\r
108 static void prvHTTPInit( void );
\r
111 * Setup the Ethernet interface with the network addressing information.
\r
113 static void prvNetifInit( void );
\r
116 * Generate the dynamic components of the served WEB page and transmit the
\r
117 * entire page through the socket.
\r
119 static void prvTransmitHTTP( unsigned portCHAR socket );
\r
120 /*-----------------------------------------------------------*/
\r
122 /* This variable is simply incremented by the idle task hook so the number of
\r
123 iterations the idle task has performed can be displayed as part of the served
\r
125 unsigned portLONG ulIdleLoops = 0UL;
\r
127 /* Data buffer shared by sockets. */
\r
128 unsigned portCHAR ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
130 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
131 and process whatever caused the interrupt. */
\r
132 xSemaphoreHandle xTCPSemaphore = NULL;
\r
134 /*-----------------------------------------------------------*/
\r
135 void vHTTPTask( void * pvParameters )
\r
138 unsigned portCHAR ucState;
\r
140 ( void ) pvParameters;
\r
142 /* Create the semaphore used to communicate between this task and the
\r
144 vSemaphoreCreateBinary( xTCPSemaphore );
\r
146 /* Make sure everything is setup before we start. */
\r
152 /* Wait until the ISR tells us there is something to do. */
\r
153 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
155 /* Check each socket. */
\r
156 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
158 ucState = select( i, SEL_CONTROL );
\r
162 case SOCK_ESTABLISHED : /* new connection established. */
\r
164 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
166 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
168 sLen = httpSOCKET_BUFFER_SIZE;
\r
173 sLen = recv( i, ucSocketBuffer, sLen );
\r
175 if( ucConnection[ i ] == 1 )
\r
177 /* This is our first time processing a HTTP
\r
178 request on this connection. */
\r
179 prvTransmitHTTP( i );
\r
180 ucConnection[i] = 0;
\r
186 case SOCK_CLOSE_WAIT :
\r
193 ucConnection[i] = 1;
\r
194 socket( i, SOCK_STREAM, 80, 0x00 );
\r
195 NBlisten( i ); /* reinitialize socket. */
\r
201 /*-----------------------------------------------------------*/
\r
203 static void prvHTTPInit( void )
\r
205 unsigned portCHAR ucIndex;
\r
207 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
208 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
210 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
211 NBlisten( ucIndex );
\r
212 ucConnection[ ucIndex ] = 1;
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvNetifInit( void )
\r
222 setMACAddr( ( unsigned portCHAR * ) ucMacAddress );
\r
223 setgateway( ( unsigned portCHAR * ) ucGatewayAddress );
\r
224 setsubmask( ( unsigned portCHAR * ) ucSubnetMask );
\r
225 setIP( ( unsigned portCHAR * ) ucIPAddress );
\r
227 /* See definition of 'sysinit' in socket.c
\r
228 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
229 are shared by all 4 channels.
\r
230 - (0x55, 0x55) configures the send and receive buffers at
\r
231 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
232 sysinit( 0x55, 0x55 );
\r
234 /*-----------------------------------------------------------*/
\r
236 static void prvTransmitHTTP(unsigned portCHAR socket)
\r
238 extern portSHORT usCheckStatus;
\r
240 /* Send the http and html headers. */
\r
241 send( socket, ( unsigned portCHAR * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
242 send( socket, ( unsigned portCHAR * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
244 /* Generate then send the table showing the status of each task. */
\r
245 vTaskList( ucSocketBuffer );
\r
246 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
248 /* Send the number of times the idle task has looped. */
\r
249 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
250 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
252 /* Send the tick count. */
\r
253 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
254 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
256 /* Show a message indicating whether or not the check task has discovered
\r
257 an error in any of the standard demo tasks. */
\r
258 if( usCheckStatus == 0 )
\r
260 sprintf( ucSocketBuffer, "No errors detected." );
\r
261 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
265 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
266 send( socket, ( unsigned portCHAR * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
269 /* Finish the page off. */
\r
270 send( socket, (unsigned portCHAR*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
272 /* Must make sure the data is gone before closing the socket. */
\r
273 while( !tx_empty( socket ) )
\r
275 vTaskDelay( httpTX_WAIT );
\r
279 /*-----------------------------------------------------------*/
\r
281 void vApplicationIdleHook( void )
\r