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