2 * FreeRTOS Kernel V10.0.1
\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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 Implements a simplistic WEB server. Every time a connection is made and
\r
30 data is received a dynamic page that shows the current TCP/IP statistics
\r
31 is generated and returned. The connection is then closed.
\r
33 This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
\r
40 + Changed the page returned by the lwIP WEB server demo to display the
\r
41 task status table rather than the TCP/IP statistics.
\r
45 /* Standard includes. */
\r
49 /* Scheduler includes. */
\r
50 #include "FreeRTOS.h"
\r
54 /* Demo includes. */
\r
55 #include "BasicWEB.h"
\r
56 #include "SAM7_EMAC.h"
\r
58 /* lwIP includes. */
\r
59 #include "lwip/api.h"
\r
60 #include "lwip/tcpip.h"
\r
61 #include "lwip/memp.h"
\r
62 #include "lwip/stats.h"
\r
63 #include "netif/loopif.h"
\r
65 /* The size of the buffer in which the dynamic WEB page is created. */
\r
66 #define webMAX_PAGE_SIZE 2048
\r
68 /* Standard GET response. */
\r
69 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
71 /* The port on which we listen. */
\r
72 #define webHTTP_PORT ( 80 )
\r
74 /* Delay on close error. */
\r
75 #define webSHORT_DELAY ( 10 )
\r
77 /* Format of the dynamic page that is returned on each connection. */
\r
78 #define webHTML_START \
\r
82 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
85 #define webHTML_END \
\r
90 /*------------------------------------------------------------*/
\r
93 * Process an incoming connection on port 80.
\r
95 * This simply checks to see if the incoming data contains a GET request, and
\r
96 * if so sends back a single dynamically created page. The connection is then
\r
97 * closed. A more complete implementation could create a task for each
\r
100 static void vProcessConnection( struct netconn *pxNetCon );
\r
102 /*------------------------------------------------------------*/
\r
104 static void vProcessConnection( struct netconn *pxNetCon )
\r
106 static char cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
\r
107 struct netbuf *pxRxBuffer;
\r
109 unsigned short usLength;
\r
110 static unsigned long ulPageHits = 0;
\r
112 /* We expect to immediately get data. */
\r
113 pxRxBuffer = netconn_recv( pxNetCon );
\r
115 if( pxRxBuffer != NULL )
\r
117 /* Where is the data? */
\r
118 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
\r
120 /* Is this a GET? We don't handle anything else. */
\r
121 if( !strncmp( pcRxString, "GET", 3 ) )
\r
123 pcRxString = cDynamicPage;
\r
125 /* Update the hit count. */
\r
127 sprintf( cPageHits, "%lu", ulPageHits );
\r
129 /* Write out the HTTP OK header. */
\r
130 netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );
\r
132 /* Generate the dynamic page...
\r
134 ... First the page header. */
\r
135 strcpy( cDynamicPage, webHTML_START );
\r
136 /* ... Then the hit count... */
\r
137 strcat( cDynamicPage, cPageHits );
\r
138 strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" );
\r
139 /* ... Then the list of tasks and their status... */
\r
140 vTaskList( cDynamicPage + strlen( cDynamicPage ) );
\r
141 /* ... Finally the page footer. */
\r
142 strcat( cDynamicPage, webHTML_END );
\r
144 /* Write out the dynamically generated page. */
\r
145 netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );
\r
148 netbuf_delete( pxRxBuffer );
\r
151 netconn_close( pxNetCon );
\r
153 /*------------------------------------------------------------*/
\r
155 void vlwIPInit( void )
\r
157 /* Initialize lwIP and its interface layer. */
\r
164 tcpip_init( NULL, NULL );
\r
166 /*------------------------------------------------------------*/
\r
168 void vBasicWEBServer( void *pvParameters )
\r
170 struct netconn *pxHTTPListener, *pxNewConnection;
\r
171 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
172 extern err_t ethernetif_init( struct netif *netif );
\r
173 static struct netif EMAC_if;
\r
175 /* Parameters are not used - suppress compiler error. */
\r
176 ( void ) pvParameters;
\r
179 /* Create and configure the EMAC interface. */
\r
180 IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);
\r
181 IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);
\r
182 IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);
\r
183 netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);
\r
185 /* make it the default interface */
\r
186 netif_set_default(&EMAC_if);
\r
189 netif_set_up(&EMAC_if);
\r
191 /* Create a new tcp connection handle */
\r
193 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
194 netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
\r
195 netconn_listen( pxHTTPListener );
\r
200 /* Wait for connection. */
\r
201 pxNewConnection = netconn_accept(pxHTTPListener);
\r
203 if(pxNewConnection != NULL)
\r
205 /* Service connection. */
\r
206 vProcessConnection( pxNewConnection );
\r
207 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
209 vTaskDelay( webSHORT_DELAY );
\r