2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 Implements a simplistic WEB server. Every time a connection is made and
\r
31 data is received a dynamic page that shows the current TCP/IP statistics
\r
32 is generated and returned. The connection is then closed.
\r
34 This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
\r
41 + Changed the page returned by the lwIP WEB server demo to display the
\r
42 task status table rather than the TCP/IP statistics.
\r
46 /* Standard includes. */
\r
50 /* Scheduler includes. */
\r
51 #include "FreeRTOS.h"
\r
55 /* Demo includes. */
\r
56 #include "BasicWEB.h"
\r
57 #include "SAM7_EMAC.h"
\r
59 /* lwIP includes. */
\r
60 #include "lwip/api.h"
\r
61 #include "lwip/tcpip.h"
\r
62 #include "lwip/memp.h"
\r
63 #include "lwip/stats.h"
\r
64 #include "netif/loopif.h"
\r
66 /* The size of the buffer in which the dynamic WEB page is created. */
\r
67 #define webMAX_PAGE_SIZE 2048
\r
69 /* Standard GET response. */
\r
70 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
72 /* The port on which we listen. */
\r
73 #define webHTTP_PORT ( 80 )
\r
75 /* Delay on close error. */
\r
76 #define webSHORT_DELAY ( 10 )
\r
78 /* Format of the dynamic page that is returned on each connection. */
\r
79 #define webHTML_START \
\r
83 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
86 #define webHTML_END \
\r
91 /*------------------------------------------------------------*/
\r
94 * Process an incoming connection on port 80.
\r
96 * This simply checks to see if the incoming data contains a GET request, and
\r
97 * if so sends back a single dynamically created page. The connection is then
\r
98 * closed. A more complete implementation could create a task for each
\r
101 static void vProcessConnection( struct netconn *pxNetCon );
\r
103 /*------------------------------------------------------------*/
\r
105 static void vProcessConnection( struct netconn *pxNetCon )
\r
107 static char cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
\r
108 struct netbuf *pxRxBuffer;
\r
110 unsigned short usLength;
\r
111 static unsigned long ulPageHits = 0;
\r
113 /* We expect to immediately get data. */
\r
114 pxRxBuffer = netconn_recv( pxNetCon );
\r
116 if( pxRxBuffer != NULL )
\r
118 /* Where is the data? */
\r
119 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
\r
121 /* Is this a GET? We don't handle anything else. */
\r
122 if( !strncmp( pcRxString, "GET", 3 ) )
\r
124 pcRxString = cDynamicPage;
\r
126 /* Update the hit count. */
\r
128 sprintf( cPageHits, "%lu", ulPageHits );
\r
130 /* Write out the HTTP OK header. */
\r
131 netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );
\r
133 /* Generate the dynamic page...
\r
135 ... First the page header. */
\r
136 strcpy( cDynamicPage, webHTML_START );
\r
137 /* ... Then the hit count... */
\r
138 strcat( cDynamicPage, cPageHits );
\r
139 strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" );
\r
140 /* ... Then the list of tasks and their status... */
\r
141 vTaskList( cDynamicPage + strlen( cDynamicPage ) );
\r
142 /* ... Finally the page footer. */
\r
143 strcat( cDynamicPage, webHTML_END );
\r
145 /* Write out the dynamically generated page. */
\r
146 netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );
\r
149 netbuf_delete( pxRxBuffer );
\r
152 netconn_close( pxNetCon );
\r
154 /*------------------------------------------------------------*/
\r
156 void vlwIPInit( void )
\r
158 /* Initialize lwIP and its interface layer. */
\r
165 tcpip_init( NULL, NULL );
\r
167 /*------------------------------------------------------------*/
\r
169 void vBasicWEBServer( void *pvParameters )
\r
171 struct netconn *pxHTTPListener, *pxNewConnection;
\r
172 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
173 extern err_t ethernetif_init( struct netif *netif );
\r
174 static struct netif EMAC_if;
\r
176 /* Parameters are not used - suppress compiler error. */
\r
177 ( void ) pvParameters;
\r
180 /* Create and configure the EMAC interface. */
\r
181 IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);
\r
182 IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);
\r
183 IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);
\r
184 netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);
\r
186 /* make it the default interface */
\r
187 netif_set_default(&EMAC_if);
\r
190 netif_set_up(&EMAC_if);
\r
192 /* Create a new tcp connection handle */
\r
194 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
195 netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
\r
196 netconn_listen( pxHTTPListener );
\r
201 /* Wait for connection. */
\r
202 pxNewConnection = netconn_accept(pxHTTPListener);
\r
204 if(pxNewConnection != NULL)
\r
206 /* Service connection. */
\r
207 vProcessConnection( pxNewConnection );
\r
208 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
210 vTaskDelay( webSHORT_DELAY );
\r