]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF52259_CodeWarrior/HTTPDemo.c
f2289fd56810a354943de41b4498adfa069e09f8
[freertos] / FreeRTOS / Demo / ColdFire_MCF52259_CodeWarrior / HTTPDemo.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\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
32 \r
33     This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
34     party.\r
35 */\r
36 \r
37 /* ------------------------ System includes ------------------------------- */\r
38 \r
39 \r
40 /* ------------------------ FreeRTOS includes ----------------------------- */\r
41 #include "FreeRTOS.h"\r
42 #include "task.h"\r
43 #include "semphr.h"\r
44 \r
45 /* ------------------------ lwIP includes --------------------------------- */\r
46 #include "lwip/api.h"\r
47 #include "lwip/tcpip.h"\r
48 #include "lwip/ip.h"\r
49 #include "lwip/memp.h"\r
50 #include "lwip/stats.h"\r
51 #include "netif/loopif.h"\r
52 \r
53 /* ------------------------ Project includes ------------------------------ */\r
54 #include "common.h"\r
55 \r
56 #include "HTTPDemo.h"\r
57 \r
58 /* ------------------------ Defines --------------------------------------- */\r
59 /* The size of the buffer in which the dynamic WEB page is created. */\r
60 #define webMAX_PAGE_SIZE        ( 1024 ) /*FSL: buffer containing array*/\r
61 \r
62 /* Standard GET response. */\r
63 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
64 \r
65 /* The port on which we listen. */\r
66 #define webHTTP_PORT            ( 80 )\r
67 \r
68 /* Delay on close error. */\r
69 #define webSHORT_DELAY          ( 10 )\r
70 \r
71 /* Format of the dynamic page that is returned on each connection. */\r
72 #define webHTML_START \\r
73 "<html>\\r
74 <head>\\r
75 </head>\\r
76 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
77 \r\n\r\nPage Hits = "\r
78 \r
79 #define webHTML_END \\r
80 "\r\n" \\r
81 "</pre>\r\n" \\r
82 "</BODY>\r\n" \\r
83 "</html>"\r
84 \r
85 #if INCLUDE_uxTaskGetStackHighWaterMark\r
86         static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;\r
87 #endif\r
88 \r
89 /* ------------------------ Prototypes ------------------------------------ */\r
90 static void     vProcessConnection( struct netconn *pxNetCon );\r
91 \r
92 /*------------------------------------------------------------*/\r
93 \r
94 /*\r
95  * Process an incoming connection on port 80.\r
96  *\r
97  * This simply checks to see if the incoming data contains a GET request, and\r
98  * if so sends back a single dynamically created page.  The connection is then\r
99  * closed.  A more complete implementation could create a task for each\r
100  * connection.\r
101  */\r
102 static void vProcessConnection( struct netconn *pxNetCon )\r
103 {\r
104     static char cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];\r
105     struct netbuf  *pxRxBuffer;\r
106     char       *pcRxString;\r
107     unsigned short usLength;\r
108     static unsigned long ulPageHits = 0;\r
109 \r
110     /* We expect to immediately get data. */\r
111     pxRxBuffer = netconn_recv( pxNetCon );\r
112 \r
113     if( pxRxBuffer != NULL )\r
114     {\r
115         /* Where is the data? */\r
116         netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );\r
117 \r
118         /* Is this a GET?  We don't handle anything else. */\r
119         if( !strncmp( pcRxString, "GET", 3 ) )\r
120         {\r
121             pcRxString = cDynamicPage;\r
122 \r
123             /* Update the hit count. */\r
124             ulPageHits++;\r
125             sprintf( cPageHits, "%d", (int)ulPageHits );\r
126 \r
127             /* Write out the HTTP OK header. */\r
128             netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );\r
129 \r
130             /* Generate the dynamic page...\r
131 \r
132                ... First the page header. */\r
133             strcpy( cDynamicPage, webHTML_START );\r
134             /* ... Then the hit count... */\r
135             strcat( cDynamicPage, cPageHits );\r
136 \r
137             strcat( cDynamicPage,\r
138                     "<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 \r
142             /* ... Finally the page footer. */\r
143             strcat( cDynamicPage, webHTML_END );\r
144 \r
145             /* Write out the dynamically generated page. */\r
146             netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );\r
147         }\r
148         netbuf_delete( pxRxBuffer );\r
149     }\r
150     netconn_close( pxNetCon );\r
151 }\r
152 \r
153 /*------------------------------------------------------------*/\r
154 \r
155 void vlwIPInit( void )\r
156 {\r
157     /* Initialize lwIP and its interface layer. */\r
158     tcpip_init( NULL, NULL );\r
159 }\r
160 \r
161 /*------------------------------------------------------------*/\r
162 \r
163 void vBasicWEBServer( void *pvParameters )\r
164 {\r
165     struct netconn *pxHTTPListener, *pxNewConnection;\r
166     struct ip_addr  xIpAddr, xNetMast, xGateway;\r
167     static struct netif fec523x_if;\r
168         extern err_t ethernetif_init(struct netif *netif);\r
169 \r
170     /* Parameters are not used - suppress compiler error. */\r
171     ( void )pvParameters;\r
172 \r
173         vlwIPInit();\r
174 \r
175     /* Create and configure the FEC interface. */\r
176     IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
177     IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
178     IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );\r
179     netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );\r
180 \r
181     /* make it the default interface */\r
182     netif_set_default( &fec523x_if );\r
183 \r
184     /* bring it up */\r
185     netif_set_up( &fec523x_if );\r
186 \r
187     /* Create a new tcp connection handle */\r
188     pxHTTPListener = netconn_new( NETCONN_TCP );\r
189     netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
190     netconn_listen( pxHTTPListener );\r
191 \r
192     /* Loop forever */\r
193     for( ;; )\r
194     {\r
195         /* Wait for connection. */\r
196         pxNewConnection = netconn_accept( pxHTTPListener );\r
197 \r
198         if( pxNewConnection != NULL )\r
199         {\r
200             /* Service connection. */\r
201             vProcessConnection( pxNewConnection );\r
202             while( netconn_delete( pxNewConnection ) != ERR_OK )\r
203             {\r
204                 vTaskDelay( webSHORT_DELAY );\r
205             }\r
206         }\r
207     }\r
208 }\r