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
38 /* ------------------------ System includes ------------------------------- */
\r
41 /* ------------------------ FreeRTOS includes ----------------------------- */
\r
42 #include "FreeRTOS.h"
\r
46 /* ------------------------ lwIP includes --------------------------------- */
\r
47 #include "lwip/api.h"
\r
48 #include "lwip/tcpip.h"
\r
49 #include "lwip/ip.h"
\r
50 #include "lwip/memp.h"
\r
51 #include "lwip/stats.h"
\r
52 #include "netif/loopif.h"
\r
54 /* ------------------------ Project includes ------------------------------ */
\r
57 #include "HTTPDemo.h"
\r
59 /* ------------------------ Defines --------------------------------------- */
\r
60 /* The size of the buffer in which the dynamic WEB page is created. */
\r
61 #define webMAX_PAGE_SIZE ( 1024 ) /*FSL: buffer containing array*/
\r
63 /* Standard GET response. */
\r
64 #define webHTTP_OK "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
\r
66 /* The port on which we listen. */
\r
67 #define webHTTP_PORT ( 80 )
\r
69 /* Delay on close error. */
\r
70 #define webSHORT_DELAY ( 10 )
\r
72 /* Format of the dynamic page that is returned on each connection. */
\r
73 #define webHTML_START \
\r
77 <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\"bgcolor=\"#CCCCff\">\
\r
78 \r\n\r\nPage Hits = "
\r
80 #define webHTML_END \
\r
86 #if INCLUDE_uxTaskGetStackHighWaterMark
\r
87 static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;
\r
90 /* ------------------------ Prototypes ------------------------------------ */
\r
91 static void vProcessConnection( struct netconn *pxNetCon );
\r
93 /*------------------------------------------------------------*/
\r
96 * Process an incoming connection on port 80.
\r
98 * This simply checks to see if the incoming data contains a GET request, and
\r
99 * if so sends back a single dynamically created page. The connection is then
\r
100 * closed. A more complete implementation could create a task for each
\r
103 static void vProcessConnection( struct netconn *pxNetCon )
\r
105 static char cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];
\r
106 struct netbuf *pxRxBuffer;
\r
108 unsigned short usLength;
\r
109 static unsigned long ulPageHits = 0;
\r
111 /* We expect to immediately get data. */
\r
112 pxRxBuffer = netconn_recv( pxNetCon );
\r
114 if( pxRxBuffer != NULL )
\r
116 /* Where is the data? */
\r
117 netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );
\r
119 /* Is this a GET? We don't handle anything else. */
\r
120 if( !strncmp( pcRxString, "GET", 3 ) )
\r
122 pcRxString = cDynamicPage;
\r
124 /* Update the hit count. */
\r
126 sprintf( cPageHits, "%d", (int)ulPageHits );
\r
128 /* Write out the HTTP OK header. */
\r
129 netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );
\r
131 /* Generate the dynamic page...
\r
133 ... First the page header. */
\r
134 strcpy( cDynamicPage, webHTML_START );
\r
135 /* ... Then the hit count... */
\r
136 strcat( cDynamicPage, cPageHits );
\r
138 strcat( cDynamicPage,
\r
139 "<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
143 /* ... Finally the page footer. */
\r
144 strcat( cDynamicPage, webHTML_END );
\r
146 /* Write out the dynamically generated page. */
\r
147 netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );
\r
149 netbuf_delete( pxRxBuffer );
\r
151 netconn_close( pxNetCon );
\r
154 /*------------------------------------------------------------*/
\r
156 void vlwIPInit( void )
\r
158 /* Initialize lwIP and its interface layer. */
\r
159 tcpip_init( NULL, NULL );
\r
162 /*------------------------------------------------------------*/
\r
164 void vBasicWEBServer( void *pvParameters )
\r
166 struct netconn *pxHTTPListener, *pxNewConnection;
\r
167 struct ip_addr xIpAddr, xNetMast, xGateway;
\r
168 static struct netif fec523x_if;
\r
169 extern err_t ethernetif_init(struct netif *netif);
\r
171 /* Parameters are not used - suppress compiler error. */
\r
172 ( void )pvParameters;
\r
176 /* Create and configure the FEC interface. */
\r
177 IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
178 IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
179 IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );
\r
180 netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );
\r
182 /* make it the default interface */
\r
183 netif_set_default( &fec523x_if );
\r
186 netif_set_up( &fec523x_if );
\r
188 /* Create a new tcp connection handle */
\r
189 pxHTTPListener = netconn_new( NETCONN_TCP );
\r
190 netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );
\r
191 netconn_listen( pxHTTPListener );
\r
196 /* Wait for connection. */
\r
197 pxNewConnection = netconn_accept( pxHTTPListener );
\r
199 if( pxNewConnection != NULL )
\r
201 /* Service connection. */
\r
202 vProcessConnection( pxNewConnection );
\r
203 while( netconn_delete( pxNewConnection ) != ERR_OK )
\r
205 vTaskDelay( webSHORT_DELAY );
\r