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