]> git.sur5r.net Git - freertos/blob - Demo/ColdFire_MCF52259_CodeWarrior/HTTPDemo.c
e82002302d10d8150002c81a4d9b068988667f65
[freertos] / Demo / ColdFire_MCF52259_CodeWarrior / HTTPDemo.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     This file is part of the FreeRTOS distribution.\r
5 \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
14     license details.\r
15 \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
19     more details.\r
20 \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
24 \r
25 \r
26     ***************************************************************************\r
27     *                                                                         *\r
28     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 /*\r
50     Implements a simplistic WEB server.  Every time a connection is made and\r
51     data is received a dynamic page that shows the current TCP/IP statistics\r
52     is generated and returned.  The connection is then closed.\r
53 \r
54     This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
55     party.\r
56 */\r
57 \r
58 /* ------------------------ System includes ------------------------------- */\r
59 \r
60 \r
61 /* ------------------------ FreeRTOS includes ----------------------------- */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 #include "semphr.h"\r
65 \r
66 /* ------------------------ lwIP includes --------------------------------- */\r
67 #include "lwip/api.h"\r
68 #include "lwip/tcpip.h"\r
69 #include "lwip/ip.h"\r
70 #include "lwip/memp.h"\r
71 #include "lwip/stats.h"\r
72 #include "netif/loopif.h"\r
73 \r
74 /* ------------------------ Project includes ------------------------------ */\r
75 #include "common.h"\r
76 \r
77 #include "HTTPDemo.h"\r
78 \r
79 /* ------------------------ Defines --------------------------------------- */\r
80 /* The size of the buffer in which the dynamic WEB page is created. */\r
81 #define webMAX_PAGE_SIZE        ( 1024 ) /*FSL: buffer containing array*/\r
82 \r
83 /* Standard GET response. */\r
84 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
85 \r
86 /* The port on which we listen. */\r
87 #define webHTTP_PORT            ( 80 )\r
88 \r
89 /* Delay on close error. */\r
90 #define webSHORT_DELAY          ( 10 )\r
91 \r
92 /* Format of the dynamic page that is returned on each connection. */\r
93 #define webHTML_START \\r
94 "<html>\\r
95 <head>\\r
96 </head>\\r
97 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
98 \r\n\r\nPage Hits = "\r
99 \r
100 #define webHTML_END \\r
101 "\r\n" \\r
102 "</pre>\r\n" \\r
103 "</BODY>\r\n" \\r
104 "</html>"\r
105 \r
106 #if INCLUDE_uxTaskGetStackHighWaterMark\r
107         static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;\r
108 #endif\r
109 \r
110 /* ------------------------ Prototypes ------------------------------------ */\r
111 static void     vProcessConnection( struct netconn *pxNetCon );\r
112 \r
113 /*------------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Process an incoming connection on port 80.\r
117  *\r
118  * This simply checks to see if the incoming data contains a GET request, and\r
119  * if so sends back a single dynamically created page.  The connection is then\r
120  * closed.  A more complete implementation could create a task for each\r
121  * connection.\r
122  */\r
123 static void vProcessConnection( struct netconn *pxNetCon )\r
124 {\r
125     static portCHAR cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];\r
126     struct netbuf  *pxRxBuffer;\r
127     portCHAR       *pcRxString;\r
128     unsigned portSHORT usLength;\r
129     static unsigned portLONG ulPageHits = 0;\r
130 \r
131     /* We expect to immediately get data. */\r
132     pxRxBuffer = netconn_recv( pxNetCon );\r
133 \r
134     if( pxRxBuffer != NULL )\r
135     {\r
136         /* Where is the data? */\r
137         netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );\r
138 \r
139         /* Is this a GET?  We don't handle anything else. */\r
140         if( !strncmp( pcRxString, "GET", 3 ) )\r
141         {\r
142             pcRxString = cDynamicPage;\r
143 \r
144             /* Update the hit count. */\r
145             ulPageHits++;\r
146             sprintf( cPageHits, "%d", (int)ulPageHits );\r
147 \r
148             /* Write out the HTTP OK header. */            \r
149             netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );\r
150 \r
151             /* Generate the dynamic page...\r
152             \r
153                ... First the page header. */\r
154             strcpy( cDynamicPage, webHTML_START );\r
155             /* ... Then the hit count... */\r
156             strcat( cDynamicPage, cPageHits );\r
157             \r
158             strcat( cDynamicPage,\r
159                     "<p><pre>Task          State  Priority  Stack      #<br>************************************************<br>" );\r
160             /* ... Then the list of tasks and their status... */\r
161             vTaskList( ( signed portCHAR * )cDynamicPage + strlen( cDynamicPage ) );            \r
162             \r
163             /* ... Finally the page footer. */\r
164             strcat( cDynamicPage, webHTML_END );\r
165 \r
166             /* Write out the dynamically generated page. */\r
167             netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );\r
168         }\r
169         netbuf_delete( pxRxBuffer );\r
170     }\r
171     netconn_close( pxNetCon );\r
172 }\r
173 \r
174 /*------------------------------------------------------------*/\r
175 \r
176 void vlwIPInit( void )\r
177 {\r
178     /* Initialize lwIP and its interface layer. */\r
179     tcpip_init( NULL, NULL );\r
180 }\r
181 \r
182 /*------------------------------------------------------------*/\r
183 \r
184 void vBasicWEBServer( void *pvParameters )\r
185 {\r
186     struct netconn *pxHTTPListener, *pxNewConnection;\r
187     struct ip_addr  xIpAddr, xNetMast, xGateway;\r
188     static struct netif fec523x_if;\r
189         extern err_t ethernetif_init(struct netif *netif);\r
190 \r
191     /* Parameters are not used - suppress compiler error. */\r
192     ( void )pvParameters;\r
193 \r
194         vlwIPInit();\r
195 \r
196     /* Create and configure the FEC interface. */\r
197     IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
198     IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
199     IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );\r
200     netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );\r
201 \r
202     /* make it the default interface */\r
203     netif_set_default( &fec523x_if );\r
204 \r
205     /* bring it up */\r
206     netif_set_up( &fec523x_if );\r
207 \r
208     /* Create a new tcp connection handle */\r
209     pxHTTPListener = netconn_new( NETCONN_TCP );\r
210     netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
211     netconn_listen( pxHTTPListener );\r
212 \r
213     /* Loop forever */\r
214     for( ;; )\r
215     {   \r
216         /* Wait for connection. */\r
217         pxNewConnection = netconn_accept( pxHTTPListener );\r
218 \r
219         if( pxNewConnection != NULL )\r
220         {\r
221             /* Service connection. */\r
222             vProcessConnection( pxNewConnection );\r
223             while( netconn_delete( pxNewConnection ) != ERR_OK )\r
224             {\r
225                 vTaskDelay( webSHORT_DELAY );\r
226             }\r
227         }\r
228     }\r
229 }\r