2 FreeRTOS V6.0.0 - 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 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 Implements a simplistic WEB server. Every time a connection is made and
\r
51 data is received a dynamic page that shows the current TCP/IP statistics
\r
52 is generated and returned. The connection is then closed.
\r
54 This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
\r
58 /* ------------------------ System includes ------------------------------- */
\r
61 /* ------------------------ FreeRTOS includes ----------------------------- */
\r
62 #include "FreeRTOS.h"
\r
66 /* ------------------------ lwIP includes --------------------------------- */
\r
67 #include "lwip/api.h"
\r
68 #include "lwip/tcpip.h"
\r
69 #include "lwip/ip.h"
\r
70 #include "lwip/memp.h"
\r
71 #include "lwip/stats.h"
\r
72 #include "netif/loopif.h"
\r
74 /* ------------------------ Project includes ------------------------------ */
\r
77 #include "HTTPDemo.h"
\r
79 /* ------------------------ Defines --------------------------------------- */
\r
80 /* The size of the buffer in which the dynamic WEB page is created. */
\r
81 #define webMAX_PAGE_SIZE ( 1024 ) /*FSL: buffer containing array*/
\r
83 /* Standard GET response. */
\r
84 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
86 /* The port on which we listen. */
\r
87 #define webHTTP_PORT ( 80 )
\r
89 /* Delay on close error. */
\r
90 #define webSHORT_DELAY ( 10 )
\r
92 /* Format of the dynamic page that is returned on each connection. */
\r
93 #define webHTML_START \
\r
97 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
98 \r\n\r\nPage Hits = "
\r
100 #define webHTML_END \
\r
106 #if INCLUDE_uxTaskGetStackHighWaterMark
\r
107 static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;
\r
110 /* ------------------------ Prototypes ------------------------------------ */
\r
111 static void vProcessConnection( struct netconn *pxNetCon );
\r
113 /*------------------------------------------------------------*/
\r
116 * Process an incoming connection on port 80.
\r
118 * This simply checks to see if the incoming data contains a GET request, and
\r
119 * if so sends back a single dynamically created page. The connection is then
\r
120 * closed. A more complete implementation could create a task for each
\r
123 static void vProcessConnection( struct netconn *pxNetCon )
\r
125 static portCHAR cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];
\r
126 struct netbuf *pxRxBuffer;
\r
127 portCHAR *pcRxString;
\r
128 unsigned portSHORT usLength;
\r
129 static unsigned portLONG ulPageHits = 0;
\r
131 /* We expect to immediately get data. */
\r
132 pxRxBuffer = netconn_recv( pxNetCon );
\r
134 if( pxRxBuffer != NULL )
\r
136 /* Where is the data? */
\r
137 netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );
\r
139 /* Is this a GET? We don't handle anything else. */
\r
140 if( !strncmp( pcRxString, "GET", 3 ) )
\r
142 pcRxString = cDynamicPage;
\r
144 /* Update the hit count. */
\r
146 sprintf( cPageHits, "%d", (int)ulPageHits );
\r
148 /* Write out the HTTP OK header. */
\r
149 netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );
\r
151 /* Generate the dynamic page...
\r
153 ... First the page header. */
\r
154 strcpy( cDynamicPage, webHTML_START );
\r
155 /* ... Then the hit count... */
\r
156 strcat( cDynamicPage, cPageHits );
\r
158 strcat( cDynamicPage,
\r
159 "<p><pre>Task State Priority Stack #<br>************************************************<br>" );
\r
160 /* ... Then the list of tasks and their status... */
\r
161 vTaskList( ( signed portCHAR * )cDynamicPage + strlen( cDynamicPage ) );
\r
163 /* ... Finally the page footer. */
\r
164 strcat( cDynamicPage, webHTML_END );
\r
166 /* Write out the dynamically generated page. */
\r
167 netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );
\r
169 netbuf_delete( pxRxBuffer );
\r
171 netconn_close( pxNetCon );
\r
174 /*------------------------------------------------------------*/
\r
176 void vlwIPInit( void )
\r
178 /* Initialize lwIP and its interface layer. */
\r
179 tcpip_init( NULL, NULL );
\r
182 /*------------------------------------------------------------*/
\r
184 void vBasicWEBServer( void *pvParameters )
\r
186 struct netconn *pxHTTPListener, *pxNewConnection;
\r
187 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
188 static struct netif fec523x_if;
\r
189 extern err_t ethernetif_init(struct netif *netif);
\r
191 /* Parameters are not used - suppress compiler error. */
\r
192 ( void )pvParameters;
\r
196 /* Create and configure the FEC interface. */
\r
197 IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
198 IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
199 IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );
\r
200 netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );
\r
202 /* make it the default interface */
\r
203 netif_set_default( &fec523x_if );
\r
206 netif_set_up( &fec523x_if );
\r
208 /* Create a new tcp connection handle */
\r
209 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
210 netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );
\r
211 netconn_listen( pxHTTPListener );
\r
216 /* Wait for connection. */
\r
217 pxNewConnection = netconn_accept( pxHTTPListener );
\r
219 if( pxNewConnection != NULL )
\r
221 /* Service connection. */
\r
222 vProcessConnection( pxNewConnection );
\r
223 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
225 vTaskDelay( webSHORT_DELAY );
\r