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