]> git.sur5r.net Git - freertos/blob - Demo/lwIP_MCF5235_GCC/web.c
Update version numbers to V4.8.0
[freertos] / Demo / lwIP_MCF5235_GCC / web.c
1 /*\r
2     FreeRTOS V4.6.1 - 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!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\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 #include <stdio.h>\r
60 #include <string.h>\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/memp.h"\r
71 #include "lwip/stats.h"\r
72 #include "netif/loopif.h"\r
73 \r
74 /* ------------------------ Project includes ------------------------------ */\r
75 #include "mcf5xxx.h"\r
76 #include "mcf523x.h"\r
77 #include "netif/fec.h"\r
78 \r
79 #include "web.h"\r
80 \r
81 /* ------------------------ Defines --------------------------------------- */\r
82 /* The size of the buffer in which the dynamic WEB page is created. */\r
83 #define webMAX_PAGE_SIZE        ( 2048 )\r
84 \r
85 /* Standard GET response. */\r
86 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
87 \r
88 /* The port on which we listen. */\r
89 #define webHTTP_PORT            ( 80 )\r
90 \r
91 /* Delay on close error. */\r
92 #define webSHORT_DELAY          ( 10 )\r
93 \r
94 /* Format of the dynamic page that is returned on each connection. */\r
95 #define webHTML_START \\r
96 "<html>\\r
97 <head>\\r
98 </head>\\r
99 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
100 \r\nPage Hits = "\r
101 \r
102 #define webHTML_END \\r
103 "\r\n" \\r
104 "FreeRTOS MCF5235 port (c) 2006 by Christian Walter &lt;wolti@sil.at&gt;\r\n" \\r
105 "</pre>\r\n" \\r
106 "</BODY>\r\n" \\r
107 "</html>"\r
108 \r
109 /* ------------------------ Prototypes ------------------------------------ */\r
110 static void     vProcessConnection( struct netconn *pxNetCon );\r
111 \r
112 /*------------------------------------------------------------*/\r
113 \r
114 /*\r
115  * Process an incoming connection on port 80.\r
116  *\r
117  * This simply checks to see if the incoming data contains a GET request, and\r
118  * if so sends back a single dynamically created page.  The connection is then\r
119  * closed.  A more complete implementation could create a task for each\r
120  * connection.\r
121  */\r
122 static void\r
123 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, "%lu", 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             strcat( cDynamicPage,\r
158                     "<p><pre>Task          State  Priority  Stack #<br>************************************************<br>" );\r
159             /* ... Then the list of tasks and their status... */\r
160             vTaskList( ( signed portCHAR * )cDynamicPage + strlen( cDynamicPage ) );\r
161             /* ... Finally the page footer. */\r
162             strcat( cDynamicPage, webHTML_END );\r
163 \r
164             /* Write out the dynamically generated page. */\r
165             netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );\r
166         }\r
167 \r
168         netbuf_delete( pxRxBuffer );\r
169     }\r
170 \r
171     netconn_close( pxNetCon );\r
172 }\r
173 \r
174 /*------------------------------------------------------------*/\r
175 \r
176 void\r
177 vlwIPInit( void )\r
178 {\r
179     /* Initialize lwIP and its interface layer. */\r
180     sys_init(  );\r
181     mem_init(  );\r
182     memp_init(  );\r
183     pbuf_init(  );\r
184     netif_init(  );\r
185     ip_init(  );\r
186     tcpip_init( NULL, NULL );\r
187 }\r
188 \r
189 /*------------------------------------------------------------*/\r
190 \r
191 void\r
192 vBasicWEBServer( void *pvParameters )\r
193 {\r
194     struct netconn *pxHTTPListener, *pxNewConnection;\r
195     struct ip_addr  xIpAddr, xNetMast, xGateway;\r
196     static struct netif fec523x_if;\r
197 \r
198     /* Parameters are not used - suppress compiler error. */\r
199     ( void )pvParameters;\r
200 \r
201     /* Create and configure the EMAC interface. */\r
202     IP4_ADDR( &xIpAddr, 10, 0, 10, 2 );\r
203     IP4_ADDR( &xNetMast, 255, 255, 255, 0 );\r
204     IP4_ADDR( &xGateway, 10, 0, 10, 1 );\r
205     netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, mcf523xfec_init, tcpip_input );\r
206 \r
207     /* make it the default interface */\r
208     netif_set_default( &fec523x_if );\r
209 \r
210     /* bring it up */\r
211     netif_set_up( &fec523x_if );\r
212 \r
213     /* Create a new tcp connection handle */\r
214     pxHTTPListener = netconn_new( NETCONN_TCP );\r
215     netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
216     netconn_listen( pxHTTPListener );\r
217 \r
218     /* Loop forever */\r
219     for( ;; )\r
220     {\r
221         /* Wait for connection. */\r
222         pxNewConnection = netconn_accept( pxHTTPListener );\r
223 \r
224         if( pxNewConnection != NULL )\r
225         {\r
226             /* Service connection. */\r
227             vProcessConnection( pxNewConnection );\r
228             while( netconn_delete( pxNewConnection ) != ERR_OK )\r
229             {\r
230                 vTaskDelay( webSHORT_DELAY );\r
231             }\r
232         }\r
233     }\r
234 }\r