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