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