2 FreeRTOS.org V5.3.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 Implements a simplistic WEB server. Every time a connection is made and
\r
54 data is received a dynamic page that shows the current TCP/IP statistics
\r
55 is generated and returned. The connection is then closed.
\r
57 This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
\r
61 /* ------------------------ System includes ------------------------------- */
\r
64 /* ------------------------ FreeRTOS includes ----------------------------- */
\r
65 #include "FreeRTOS.h"
\r
69 /* ------------------------ lwIP includes --------------------------------- */
\r
70 #include "lwip/api.h"
\r
71 #include "lwip/tcpip.h"
\r
72 #include "lwip/ip.h"
\r
73 #include "lwip/memp.h"
\r
74 #include "lwip/stats.h"
\r
75 #include "netif/loopif.h"
\r
77 /* ------------------------ Project includes ------------------------------ */
\r
80 #include "HTTPDemo.h"
\r
82 /* ------------------------ Defines --------------------------------------- */
\r
83 /* The size of the buffer in which the dynamic WEB page is created. */
\r
84 #define webMAX_PAGE_SIZE ( 1024 ) /*FSL: buffer containing array*/
\r
86 /* Standard GET response. */
\r
87 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
89 /* The port on which we listen. */
\r
90 #define webHTTP_PORT ( 80 )
\r
92 /* Delay on close error. */
\r
93 #define webSHORT_DELAY ( 10 )
\r
95 /* Format of the dynamic page that is returned on each connection. */
\r
96 #define webHTML_START \
\r
100 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
101 \r\n\r\nPage Hits = "
\r
103 #define webHTML_END \
\r
109 #if INCLUDE_uxTaskGetStackHighWaterMark
\r
110 static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;
\r
113 /* ------------------------ Prototypes ------------------------------------ */
\r
114 static void vProcessConnection( struct netconn *pxNetCon );
\r
116 /*------------------------------------------------------------*/
\r
119 * Process an incoming connection on port 80.
\r
121 * This simply checks to see if the incoming data contains a GET request, and
\r
122 * if so sends back a single dynamically created page. The connection is then
\r
123 * closed. A more complete implementation could create a task for each
\r
126 static void vProcessConnection( struct netconn *pxNetCon )
\r
128 static portCHAR cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];
\r
129 struct netbuf *pxRxBuffer;
\r
130 portCHAR *pcRxString;
\r
131 unsigned portSHORT usLength;
\r
132 static unsigned portLONG ulPageHits = 0;
\r
134 /* We expect to immediately get data. */
\r
135 pxRxBuffer = netconn_recv( pxNetCon );
\r
137 if( pxRxBuffer != NULL )
\r
139 /* Where is the data? */
\r
140 netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );
\r
142 /* Is this a GET? We don't handle anything else. */
\r
143 if( !strncmp( pcRxString, "GET", 3 ) )
\r
145 pcRxString = cDynamicPage;
\r
147 /* Update the hit count. */
\r
149 sprintf( cPageHits, "%d", (int)ulPageHits );
\r
151 /* Write out the HTTP OK header. */
\r
152 netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );
\r
154 /* Generate the dynamic page...
\r
156 ... First the page header. */
\r
157 strcpy( cDynamicPage, webHTML_START );
\r
158 /* ... Then the hit count... */
\r
159 strcat( cDynamicPage, cPageHits );
\r
161 strcat( cDynamicPage,
\r
162 "<p><pre>Task State Priority Stack #<br>************************************************<br>" );
\r
163 /* ... Then the list of tasks and their status... */
\r
164 vTaskList( ( signed portCHAR * )cDynamicPage + strlen( cDynamicPage ) );
\r
166 /* ... Finally the page footer. */
\r
167 strcat( cDynamicPage, webHTML_END );
\r
169 /* Write out the dynamically generated page. */
\r
170 netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );
\r
172 netbuf_delete( pxRxBuffer );
\r
174 netconn_close( pxNetCon );
\r
177 /*------------------------------------------------------------*/
\r
179 void vlwIPInit( void )
\r
181 /* Initialize lwIP and its interface layer. */
\r
182 tcpip_init( NULL, NULL );
\r
185 /*------------------------------------------------------------*/
\r
187 void vBasicWEBServer( void *pvParameters )
\r
189 struct netconn *pxHTTPListener, *pxNewConnection;
\r
190 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
191 static struct netif fec523x_if;
\r
192 extern err_t ethernetif_init(struct netif *netif);
\r
194 /* Parameters are not used - suppress compiler error. */
\r
195 ( void )pvParameters;
\r
199 /* Create and configure the FEC interface. */
\r
200 IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
201 IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
202 IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );
\r
203 netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );
\r
205 /* make it the default interface */
\r
206 netif_set_default( &fec523x_if );
\r
209 netif_set_up( &fec523x_if );
\r
211 /* Create a new tcp connection handle */
\r
212 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
213 netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );
\r
214 netconn_listen( pxHTTPListener );
\r
219 /* Wait for connection. */
\r
220 pxNewConnection = netconn_accept( pxHTTPListener );
\r
222 if( pxNewConnection != NULL )
\r
224 /* Service connection. */
\r
225 vProcessConnection( pxNewConnection );
\r
226 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
228 vTaskDelay( webSHORT_DELAY );
\r