2 FreeRTOS.org V5.2.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
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\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
64 + Changed the page returned by the lwIP WEB server demo to display the
\r
65 task status table rather than the TCP/IP statistics.
\r
69 /* Standard includes. */
\r
73 /* Scheduler includes. */
\r
74 #include "FreeRTOS.h"
\r
78 /* Demo includes. */
\r
79 #include "BasicWEB.h"
\r
80 #include "SAM7_EMAC.h"
\r
82 /* lwIP includes. */
\r
83 #include "lwip/api.h"
\r
84 #include "lwip/tcpip.h"
\r
85 #include "lwip/memp.h"
\r
86 #include "lwip/stats.h"
\r
87 #include "netif/loopif.h"
\r
89 /* The size of the buffer in which the dynamic WEB page is created. */
\r
90 #define webMAX_PAGE_SIZE 2048
\r
92 /* Standard GET response. */
\r
93 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
95 /* The port on which we listen. */
\r
96 #define webHTTP_PORT ( 80 )
\r
98 /* Delay on close error. */
\r
99 #define webSHORT_DELAY ( 10 )
\r
101 /* Format of the dynamic page that is returned on each connection. */
\r
102 #define webHTML_START \
\r
106 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
109 #define webHTML_END \
\r
114 /*------------------------------------------------------------*/
\r
117 * Process an incoming connection on port 80.
\r
119 * This simply checks to see if the incoming data contains a GET request, and
\r
120 * if so sends back a single dynamically created page. The connection is then
\r
121 * closed. A more complete implementation could create a task for each
\r
124 static void vProcessConnection( struct netconn *pxNetCon );
\r
126 /*------------------------------------------------------------*/
\r
128 static void vProcessConnection( struct netconn *pxNetCon )
\r
130 static portCHAR cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
\r
131 struct netbuf *pxRxBuffer;
\r
132 portCHAR *pcRxString;
\r
133 unsigned portSHORT usLength;
\r
134 static unsigned portLONG ulPageHits = 0;
\r
136 /* We expect to immediately get data. */
\r
137 pxRxBuffer = netconn_recv( pxNetCon );
\r
139 if( pxRxBuffer != NULL )
\r
141 /* Where is the data? */
\r
142 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
\r
144 /* Is this a GET? We don't handle anything else. */
\r
145 if( !strncmp( pcRxString, "GET", 3 ) )
\r
147 pcRxString = cDynamicPage;
\r
149 /* Update the hit count. */
\r
151 sprintf( cPageHits, "%lu", ulPageHits );
\r
153 /* Write out the HTTP OK header. */
\r
154 netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );
\r
156 /* Generate the dynamic page...
\r
158 ... First the page header. */
\r
159 strcpy( cDynamicPage, webHTML_START );
\r
160 /* ... Then the hit count... */
\r
161 strcat( cDynamicPage, cPageHits );
\r
162 strcat( cDynamicPage, "<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
165 /* ... Finally the page footer. */
\r
166 strcat( cDynamicPage, webHTML_END );
\r
168 /* Write out the dynamically generated page. */
\r
169 netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );
\r
172 netbuf_delete( pxRxBuffer );
\r
175 netconn_close( pxNetCon );
\r
177 /*------------------------------------------------------------*/
\r
179 void vlwIPInit( void )
\r
181 /* Initialize lwIP and its interface layer. */
\r
188 tcpip_init( NULL, NULL );
\r
190 /*------------------------------------------------------------*/
\r
192 void vBasicWEBServer( void *pvParameters )
\r
194 struct netconn *pxHTTPListener, *pxNewConnection;
\r
195 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
196 extern err_t ethernetif_init( struct netif *netif );
\r
197 static struct netif EMAC_if;
\r
199 /* Parameters are not used - suppress compiler error. */
\r
200 ( void ) pvParameters;
\r
203 /* Create and configure the EMAC interface. */
\r
204 IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);
\r
205 IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);
\r
206 IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);
\r
207 netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);
\r
209 /* make it the default interface */
\r
210 netif_set_default(&EMAC_if);
\r
213 netif_set_up(&EMAC_if);
\r
215 /* Create a new tcp connection handle */
\r
217 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
218 netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
\r
219 netconn_listen( pxHTTPListener );
\r
224 /* Wait for connection. */
\r
225 pxNewConnection = netconn_accept(pxHTTPListener);
\r
227 if(pxNewConnection != NULL)
\r
229 /* Service connection. */
\r
230 vProcessConnection( pxNewConnection );
\r
231 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
233 vTaskDelay( webSHORT_DELAY );
\r