2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 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
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
38 Implements a simplistic WEB server. Every time a connection is made and
\r
39 data is received a dynamic page that shows the current TCP/IP statistics
\r
40 is generated and returned. The connection is then closed.
\r
42 This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
\r
49 + Changed the page returned by the lwIP WEB server demo to display the
\r
50 task status table rather than the TCP/IP statistics.
\r
54 /* Standard includes. */
\r
58 /* Scheduler includes. */
\r
59 #include "FreeRTOS.h"
\r
63 /* Demo includes. */
\r
64 #include "BasicWEB.h"
\r
65 #include "SAM7_EMAC.h"
\r
67 /* lwIP includes. */
\r
68 #include "lwip/api.h"
\r
69 #include "lwip/tcpip.h"
\r
70 #include "lwip/memp.h"
\r
71 #include "lwip/stats.h"
\r
72 #include "netif/loopif.h"
\r
74 /* The size of the buffer in which the dynamic WEB page is created. */
\r
75 #define webMAX_PAGE_SIZE 2048
\r
77 /* Standard GET response. */
\r
78 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
80 /* The port on which we listen. */
\r
81 #define webHTTP_PORT ( 80 )
\r
83 /* Delay on close error. */
\r
84 #define webSHORT_DELAY ( 10 )
\r
86 /* Format of the dynamic page that is returned on each connection. */
\r
87 #define webHTML_START \
\r
91 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
94 #define webHTML_END \
\r
99 /*------------------------------------------------------------*/
\r
102 * Process an incoming connection on port 80.
\r
104 * This simply checks to see if the incoming data contains a GET request, and
\r
105 * if so sends back a single dynamically created page. The connection is then
\r
106 * closed. A more complete implementation could create a task for each
\r
109 static void vProcessConnection( struct netconn *pxNetCon );
\r
111 /*------------------------------------------------------------*/
\r
113 static void vProcessConnection( struct netconn *pxNetCon )
\r
115 static portCHAR cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
\r
116 struct netbuf *pxRxBuffer;
\r
117 portCHAR *pcRxString;
\r
118 unsigned portSHORT usLength;
\r
119 static unsigned portLONG ulPageHits = 0;
\r
121 /* We expect to immediately get data. */
\r
122 pxRxBuffer = netconn_recv( pxNetCon );
\r
124 if( pxRxBuffer != NULL )
\r
126 /* Where is the data? */
\r
127 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
\r
129 /* Is this a GET? We don't handle anything else. */
\r
130 if( !strncmp( pcRxString, "GET", 3 ) )
\r
132 pcRxString = cDynamicPage;
\r
134 /* Update the hit count. */
\r
136 sprintf( cPageHits, "%lu", ulPageHits );
\r
138 /* Write out the HTTP OK header. */
\r
139 netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );
\r
141 /* Generate the dynamic page...
\r
143 ... First the page header. */
\r
144 strcpy( cDynamicPage, webHTML_START );
\r
145 /* ... Then the hit count... */
\r
146 strcat( cDynamicPage, cPageHits );
\r
147 strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" );
\r
148 /* ... Then the list of tasks and their status... */
\r
149 vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) );
\r
150 /* ... Finally the page footer. */
\r
151 strcat( cDynamicPage, webHTML_END );
\r
153 /* Write out the dynamically generated page. */
\r
154 netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );
\r
157 netbuf_delete( pxRxBuffer );
\r
160 netconn_close( pxNetCon );
\r
162 /*------------------------------------------------------------*/
\r
164 void vlwIPInit( void )
\r
166 /* Initialize lwIP and its interface layer. */
\r
173 tcpip_init( NULL, NULL );
\r
175 /*------------------------------------------------------------*/
\r
177 void vBasicWEBServer( void *pvParameters )
\r
179 struct netconn *pxHTTPListener, *pxNewConnection;
\r
180 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
181 extern err_t ethernetif_init( struct netif *netif );
\r
182 static struct netif EMAC_if;
\r
184 /* Parameters are not used - suppress compiler error. */
\r
185 ( void ) pvParameters;
\r
188 /* Create and configure the EMAC interface. */
\r
189 IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);
\r
190 IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);
\r
191 IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);
\r
192 netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);
\r
194 /* make it the default interface */
\r
195 netif_set_default(&EMAC_if);
\r
198 netif_set_up(&EMAC_if);
\r
200 /* Create a new tcp connection handle */
\r
202 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
203 netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
\r
204 netconn_listen( pxHTTPListener );
\r
209 /* Wait for connection. */
\r
210 pxNewConnection = netconn_accept(pxHTTPListener);
\r
212 if(pxNewConnection != NULL)
\r
214 /* Service connection. */
\r
215 vProcessConnection( pxNewConnection );
\r
216 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
218 vTaskDelay( webSHORT_DELAY );
\r