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