2 FreeRTOS V5.4.2 - 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 Implements a simplistic WEB server. Every time a connection is made and
\r
50 data is received a dynamic page that shows the current TCP/IP statistics
\r
51 is generated and returned. The connection is then closed.
\r
53 This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
\r
57 /* ------------------------ System includes ------------------------------- */
\r
60 /* ------------------------ FreeRTOS includes ----------------------------- */
\r
61 #include "FreeRTOS.h"
\r
65 /* ------------------------ lwIP includes --------------------------------- */
\r
66 #include "lwip/api.h"
\r
67 #include "lwip/tcpip.h"
\r
68 #include "lwip/ip.h"
\r
69 #include "lwip/memp.h"
\r
70 #include "lwip/stats.h"
\r
71 #include "netif/loopif.h"
\r
73 /* ------------------------ Project includes ------------------------------ */
\r
76 #include "HTTPDemo.h"
\r
78 /* ------------------------ Defines --------------------------------------- */
\r
79 /* The size of the buffer in which the dynamic WEB page is created. */
\r
80 #define webMAX_PAGE_SIZE ( 1024 ) /*FSL: buffer containing array*/
\r
82 /* Standard GET response. */
\r
83 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
85 /* The port on which we listen. */
\r
86 #define webHTTP_PORT ( 80 )
\r
88 /* Delay on close error. */
\r
89 #define webSHORT_DELAY ( 10 )
\r
91 /* Format of the dynamic page that is returned on each connection. */
\r
92 #define webHTML_START \
\r
96 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
97 \r\n\r\nPage Hits = "
\r
99 #define webHTML_END \
\r
105 #if INCLUDE_uxTaskGetStackHighWaterMark
\r
106 static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;
\r
109 /* ------------------------ Prototypes ------------------------------------ */
\r
110 static void vProcessConnection( struct netconn *pxNetCon );
\r
112 /*------------------------------------------------------------*/
\r
115 * Process an incoming connection on port 80.
\r
117 * This simply checks to see if the incoming data contains a GET request, and
\r
118 * if so sends back a single dynamically created page. The connection is then
\r
119 * closed. A more complete implementation could create a task for each
\r
122 static void vProcessConnection( struct netconn *pxNetCon )
\r
124 static portCHAR cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];
\r
125 struct netbuf *pxRxBuffer;
\r
126 portCHAR *pcRxString;
\r
127 unsigned portSHORT usLength;
\r
128 static unsigned portLONG ulPageHits = 0;
\r
130 /* We expect to immediately get data. */
\r
131 pxRxBuffer = netconn_recv( pxNetCon );
\r
133 if( pxRxBuffer != NULL )
\r
135 /* Where is the data? */
\r
136 netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );
\r
138 /* Is this a GET? We don't handle anything else. */
\r
139 if( !strncmp( pcRxString, "GET", 3 ) )
\r
141 pcRxString = cDynamicPage;
\r
143 /* Update the hit count. */
\r
145 sprintf( cPageHits, "%d", (int)ulPageHits );
\r
147 /* Write out the HTTP OK header. */
\r
148 netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );
\r
150 /* Generate the dynamic page...
\r
152 ... First the page header. */
\r
153 strcpy( cDynamicPage, webHTML_START );
\r
154 /* ... Then the hit count... */
\r
155 strcat( cDynamicPage, cPageHits );
\r
157 strcat( cDynamicPage,
\r
158 "<p><pre>Task State Priority Stack #<br>************************************************<br>" );
\r
159 /* ... Then the list of tasks and their status... */
\r
160 vTaskList( ( signed portCHAR * )cDynamicPage + strlen( cDynamicPage ) );
\r
162 /* ... Finally the page footer. */
\r
163 strcat( cDynamicPage, webHTML_END );
\r
165 /* Write out the dynamically generated page. */
\r
166 netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );
\r
168 netbuf_delete( pxRxBuffer );
\r
170 netconn_close( pxNetCon );
\r
173 /*------------------------------------------------------------*/
\r
175 void vlwIPInit( void )
\r
177 /* Initialize lwIP and its interface layer. */
\r
178 tcpip_init( NULL, NULL );
\r
181 /*------------------------------------------------------------*/
\r
183 void vBasicWEBServer( void *pvParameters )
\r
185 struct netconn *pxHTTPListener, *pxNewConnection;
\r
186 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
187 static struct netif fec523x_if;
\r
188 extern err_t ethernetif_init(struct netif *netif);
\r
190 /* Parameters are not used - suppress compiler error. */
\r
191 ( void )pvParameters;
\r
195 /* Create and configure the FEC interface. */
\r
196 IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
197 IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
198 IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );
\r
199 netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );
\r
201 /* make it the default interface */
\r
202 netif_set_default( &fec523x_if );
\r
205 netif_set_up( &fec523x_if );
\r
207 /* Create a new tcp connection handle */
\r
208 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
209 netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );
\r
210 netconn_listen( pxHTTPListener );
\r
215 /* Wait for connection. */
\r
216 pxNewConnection = netconn_accept( pxHTTPListener );
\r
218 if( pxNewConnection != NULL )
\r
220 /* Service connection. */
\r
221 vProcessConnection( pxNewConnection );
\r
222 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
224 vTaskDelay( webSHORT_DELAY );
\r