2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\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
60 + Changed the page returned by the lwIP WEB server demo to display the
\r
61 task status table rather than the TCP/IP statistics.
\r
65 /* Standard includes. */
\r
69 /* Scheduler includes. */
\r
70 #include "FreeRTOS.h"
\r
74 /* Demo includes. */
\r
75 #include "BasicWEB.h"
\r
76 #include "SAM7_EMAC.h"
\r
78 /* lwIP includes. */
\r
79 #include "lwip/api.h"
\r
80 #include "lwip/tcpip.h"
\r
81 #include "lwip/memp.h"
\r
82 #include "lwip/stats.h"
\r
83 #include "netif/loopif.h"
\r
85 /* The size of the buffer in which the dynamic WEB page is created. */
\r
86 #define webMAX_PAGE_SIZE 2048
\r
88 /* Standard GET response. */
\r
89 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
91 /* The port on which we listen. */
\r
92 #define webHTTP_PORT ( 80 )
\r
94 /* Delay on close error. */
\r
95 #define webSHORT_DELAY ( 10 )
\r
97 /* Format of the dynamic page that is returned on each connection. */
\r
98 #define webHTML_START \
\r
102 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
105 #define webHTML_END \
\r
110 /*------------------------------------------------------------*/
\r
113 * Process an incoming connection on port 80.
\r
115 * This simply checks to see if the incoming data contains a GET request, and
\r
116 * if so sends back a single dynamically created page. The connection is then
\r
117 * closed. A more complete implementation could create a task for each
\r
120 static void vProcessConnection( struct netconn *pxNetCon );
\r
122 /*------------------------------------------------------------*/
\r
124 static void vProcessConnection( struct netconn *pxNetCon )
\r
126 static portCHAR cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
\r
127 struct netbuf *pxRxBuffer;
\r
128 portCHAR *pcRxString;
\r
129 unsigned portSHORT usLength;
\r
130 static unsigned portLONG ulPageHits = 0;
\r
132 /* We expect to immediately get data. */
\r
133 pxRxBuffer = netconn_recv( pxNetCon );
\r
135 if( pxRxBuffer != NULL )
\r
137 /* Where is the data? */
\r
138 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
\r
140 /* Is this a GET? We don't handle anything else. */
\r
141 if( !strncmp( pcRxString, "GET", 3 ) )
\r
143 pcRxString = cDynamicPage;
\r
145 /* Update the hit count. */
\r
147 sprintf( cPageHits, "%lu", ulPageHits );
\r
149 /* Write out the HTTP OK header. */
\r
150 netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );
\r
152 /* Generate the dynamic page...
\r
154 ... First the page header. */
\r
155 strcpy( cDynamicPage, webHTML_START );
\r
156 /* ... Then the hit count... */
\r
157 strcat( cDynamicPage, cPageHits );
\r
158 strcat( cDynamicPage, "<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
161 /* ... Finally the page footer. */
\r
162 strcat( cDynamicPage, webHTML_END );
\r
164 /* Write out the dynamically generated page. */
\r
165 netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );
\r
168 netbuf_delete( pxRxBuffer );
\r
171 netconn_close( pxNetCon );
\r
173 /*------------------------------------------------------------*/
\r
175 void vlwIPInit( void )
\r
177 /* Initialize lwIP and its interface layer. */
\r
184 tcpip_init( NULL, NULL );
\r
186 /*------------------------------------------------------------*/
\r
188 void vBasicWEBServer( void *pvParameters )
\r
190 struct netconn *pxHTTPListener, *pxNewConnection;
\r
191 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
192 extern err_t ethernetif_init( struct netif *netif );
\r
193 static struct netif EMAC_if;
\r
195 /* Parameters are not used - suppress compiler error. */
\r
196 ( void ) pvParameters;
\r
199 /* Create and configure the EMAC interface. */
\r
200 IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);
\r
201 IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);
\r
202 IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);
\r
203 netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);
\r
205 /* make it the default interface */
\r
206 netif_set_default(&EMAC_if);
\r
209 netif_set_up(&EMAC_if);
\r
211 /* Create a new tcp connection handle */
\r
213 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
214 netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
\r
215 netconn_listen( pxHTTPListener );
\r
220 /* Wait for connection. */
\r
221 pxNewConnection = netconn_accept(pxHTTPListener);
\r
223 if(pxNewConnection != NULL)
\r
225 /* Service connection. */
\r
226 vProcessConnection( pxNewConnection );
\r
227 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
229 vTaskDelay( webSHORT_DELAY );
\r