]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/BasicWEB.c
Update to V4.7.1
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / BasicWEB.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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\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.org 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.org; 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.org, 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 /*\r
53         Changes from V3.2.2\r
54 \r
55         + Changed the page returned by the lwIP WEB server demo to display the \r
56           task status table rather than the TCP/IP statistics.\r
57 */\r
58 \r
59 \r
60 /* Standard includes. */\r
61 #include <stdio.h>\r
62 #include <string.h>\r
63 \r
64 /* Scheduler includes. */\r
65 #include "FreeRTOS.h"\r
66 #include "task.h"\r
67 #include "semphr.h"\r
68 \r
69 /* Demo includes. */\r
70 #include "BasicWEB.h"\r
71 #include "SAM7_EMAC.h"\r
72 \r
73 /* lwIP includes. */\r
74 #include "lwip/api.h" \r
75 #include "lwip/tcpip.h"\r
76 #include "lwip/memp.h" \r
77 #include "lwip/stats.h"\r
78 #include "netif/loopif.h"\r
79 \r
80 /* The size of the buffer in which the dynamic WEB page is created. */\r
81 #define webMAX_PAGE_SIZE        2048\r
82 \r
83 /* Standard GET response. */\r
84 #define webHTTP_OK      "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
85 \r
86 /* The port on which we listen. */\r
87 #define webHTTP_PORT            ( 80 )\r
88 \r
89 /* Delay on close error. */\r
90 #define webSHORT_DELAY          ( 10 )\r
91 \r
92 /* Format of the dynamic page that is returned on each connection. */\r
93 #define webHTML_START \\r
94 "<html>\\r
95 <head>\\r
96 </head>\\r
97 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
98 \r\nPage Hits = "\r
99 \r
100 #define webHTML_END \\r
101 "\r\n</pre>\\r
102 \r\n</BODY>\\r
103 </html>"\r
104 \r
105 /*------------------------------------------------------------*/\r
106 \r
107 /* \r
108  * Process an incoming connection on port 80.\r
109  *\r
110  * This simply checks to see if the incoming data contains a GET request, and\r
111  * if so sends back a single dynamically created page.  The connection is then\r
112  * closed.  A more complete implementation could create a task for each \r
113  * connection. \r
114  */\r
115 static void vProcessConnection( struct netconn *pxNetCon );\r
116 \r
117 /*------------------------------------------------------------*/\r
118 \r
119 static void vProcessConnection( struct netconn *pxNetCon )\r
120 {\r
121 static portCHAR cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];\r
122 struct netbuf *pxRxBuffer;\r
123 portCHAR *pcRxString;\r
124 unsigned portSHORT usLength;\r
125 static unsigned portLONG ulPageHits = 0;\r
126 \r
127         /* We expect to immediately get data. */\r
128         pxRxBuffer = netconn_recv( pxNetCon );\r
129 \r
130         if( pxRxBuffer != NULL )\r
131         {\r
132                 /* Where is the data? */\r
133                 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );      \r
134         \r
135                 /* Is this a GET?  We don't handle anything else. */\r
136                 if( !strncmp( pcRxString, "GET", 3 ) )\r
137                 {\r
138                         pcRxString = cDynamicPage;\r
139 \r
140                         /* Update the hit count. */\r
141                         ulPageHits++;\r
142                         sprintf( cPageHits, "%lu", ulPageHits );\r
143 \r
144                         /* Write out the HTTP OK header. */\r
145             netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );\r
146 \r
147                         /* Generate the dynamic page...\r
148 \r
149                         ... First the page header. */\r
150                         strcpy( cDynamicPage, webHTML_START );\r
151                         /* ... Then the hit count... */\r
152                         strcat( cDynamicPage, cPageHits );\r
153                         strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack     #<br>************************************************<br>" );\r
154                         /* ... Then the list of tasks and their status... */\r
155                         vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) );       \r
156                         /* ... Finally the page footer. */\r
157                         strcat( cDynamicPage, webHTML_END );\r
158 \r
159                         /* Write out the dynamically generated page. */\r
160                         netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );\r
161                 }\r
162  \r
163                 netbuf_delete( pxRxBuffer );\r
164         }\r
165 \r
166         netconn_close( pxNetCon );\r
167 }\r
168 /*------------------------------------------------------------*/\r
169 \r
170 void vlwIPInit( void )\r
171 {\r
172     /* Initialize lwIP and its interface layer. */\r
173         sys_init();\r
174         mem_init();                                                             \r
175         memp_init();\r
176         pbuf_init(); \r
177         netif_init();\r
178         ip_init();\r
179         tcpip_init( NULL, NULL );\r
180 }\r
181 /*------------------------------------------------------------*/\r
182 \r
183 void vBasicWEBServer( void *pvParameters )\r
184 {\r
185 struct netconn *pxHTTPListener, *pxNewConnection;\r
186 struct ip_addr xIpAddr, xNetMast, xGateway;\r
187 extern err_t ethernetif_init( struct netif *netif );\r
188 static struct netif EMAC_if;\r
189 \r
190         /* Parameters are not used - suppress compiler error. */\r
191         ( void ) pvParameters;\r
192 \r
193 \r
194         /* Create and configure the EMAC interface. */\r
195         IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);\r
196         IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);\r
197         IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);\r
198         netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);\r
199 \r
200         /* make it the default interface */\r
201     netif_set_default(&EMAC_if);\r
202 \r
203         /* bring it up */\r
204     netif_set_up(&EMAC_if);\r
205         \r
206         /* Create a new tcp connection handle */\r
207 \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
229 \r
230 \r