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