]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/BasicWEB.c
Ready for V5.2.0 release.
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / BasicWEB.c
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it \r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53         Implements a simplistic WEB server.  Every time a connection is made and\r
54         data is received a dynamic page that shows the current TCP/IP statistics\r
55         is generated and returned.  The connection is then closed.\r
56 \r
57         This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
58         party.\r
59 */\r
60 \r
61 /*\r
62         Changes from V3.2.2\r
63 \r
64         + Changed the page returned by the lwIP WEB server demo to display the \r
65           task status table rather than the TCP/IP statistics.\r
66 */\r
67 \r
68 \r
69 /* Standard includes. */\r
70 #include <stdio.h>\r
71 #include <string.h>\r
72 \r
73 /* Scheduler includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "semphr.h"\r
77 \r
78 /* Demo includes. */\r
79 #include "BasicWEB.h"\r
80 #include "SAM7_EMAC.h"\r
81 \r
82 /* lwIP includes. */\r
83 #include "lwip/api.h" \r
84 #include "lwip/tcpip.h"\r
85 #include "lwip/memp.h" \r
86 #include "lwip/stats.h"\r
87 #include "netif/loopif.h"\r
88 \r
89 /* The size of the buffer in which the dynamic WEB page is created. */\r
90 #define webMAX_PAGE_SIZE        2048\r
91 \r
92 /* Standard GET response. */\r
93 #define webHTTP_OK      "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
94 \r
95 /* The port on which we listen. */\r
96 #define webHTTP_PORT            ( 80 )\r
97 \r
98 /* Delay on close error. */\r
99 #define webSHORT_DELAY          ( 10 )\r
100 \r
101 /* Format of the dynamic page that is returned on each connection. */\r
102 #define webHTML_START \\r
103 "<html>\\r
104 <head>\\r
105 </head>\\r
106 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
107 \r\nPage Hits = "\r
108 \r
109 #define webHTML_END \\r
110 "\r\n</pre>\\r
111 \r\n</BODY>\\r
112 </html>"\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 /*------------------------------------------------------------*/\r
127 \r
128 static void vProcessConnection( struct netconn *pxNetCon )\r
129 {\r
130 static portCHAR cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];\r
131 struct netbuf *pxRxBuffer;\r
132 portCHAR *pcRxString;\r
133 unsigned portSHORT usLength;\r
134 static unsigned portLONG ulPageHits = 0;\r
135 \r
136         /* We expect to immediately get data. */\r
137         pxRxBuffer = netconn_recv( pxNetCon );\r
138 \r
139         if( pxRxBuffer != NULL )\r
140         {\r
141                 /* Where is the data? */\r
142                 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );      \r
143         \r
144                 /* Is this a GET?  We don't handle anything else. */\r
145                 if( !strncmp( pcRxString, "GET", 3 ) )\r
146                 {\r
147                         pcRxString = cDynamicPage;\r
148 \r
149                         /* Update the hit count. */\r
150                         ulPageHits++;\r
151                         sprintf( cPageHits, "%lu", ulPageHits );\r
152 \r
153                         /* Write out the HTTP OK header. */\r
154             netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );\r
155 \r
156                         /* Generate the dynamic page...\r
157 \r
158                         ... First the page header. */\r
159                         strcpy( cDynamicPage, webHTML_START );\r
160                         /* ... Then the hit count... */\r
161                         strcat( cDynamicPage, cPageHits );\r
162                         strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack     #<br>************************************************<br>" );\r
163                         /* ... Then the list of tasks and their status... */\r
164                         vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) );       \r
165                         /* ... Finally the page footer. */\r
166                         strcat( cDynamicPage, webHTML_END );\r
167 \r
168                         /* Write out the dynamically generated page. */\r
169                         netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );\r
170                 }\r
171  \r
172                 netbuf_delete( pxRxBuffer );\r
173         }\r
174 \r
175         netconn_close( pxNetCon );\r
176 }\r
177 /*------------------------------------------------------------*/\r
178 \r
179 void vlwIPInit( void )\r
180 {\r
181     /* Initialize lwIP and its interface layer. */\r
182         sys_init();\r
183         mem_init();                                                             \r
184         memp_init();\r
185         pbuf_init(); \r
186         netif_init();\r
187         ip_init();\r
188         tcpip_init( NULL, NULL );\r
189 }\r
190 /*------------------------------------------------------------*/\r
191 \r
192 void vBasicWEBServer( void *pvParameters )\r
193 {\r
194 struct netconn *pxHTTPListener, *pxNewConnection;\r
195 struct ip_addr xIpAddr, xNetMast, xGateway;\r
196 extern err_t ethernetif_init( struct netif *netif );\r
197 static struct netif EMAC_if;\r
198 \r
199         /* Parameters are not used - suppress compiler error. */\r
200         ( void ) pvParameters;\r
201 \r
202 \r
203         /* Create and configure the EMAC interface. */\r
204         IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);\r
205         IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);\r
206         IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);\r
207         netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);\r
208 \r
209         /* make it the default interface */\r
210     netif_set_default(&EMAC_if);\r
211 \r
212         /* bring it up */\r
213     netif_set_up(&EMAC_if);\r
214         \r
215         /* Create a new tcp connection handle */\r
216 \r
217         pxHTTPListener = netconn_new( NETCONN_TCP );\r
218         netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );\r
219         netconn_listen( pxHTTPListener );\r
220 \r
221         /* Loop forever */\r
222         for( ;; )\r
223         {\r
224                 /* Wait for connection. */\r
225                 pxNewConnection = netconn_accept(pxHTTPListener);\r
226 \r
227                 if(pxNewConnection != NULL)\r
228                 {\r
229                         /* Service connection. */\r
230                         vProcessConnection( pxNewConnection );\r
231                         while( netconn_delete( pxNewConnection ) != ERR_OK )\r
232                         {\r
233                                 vTaskDelay( webSHORT_DELAY );\r
234                         }\r
235                 }\r
236         }\r
237 }\r
238 \r
239 \r