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