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