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