]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/BasicWEB.c
7535797ea89ea1bb30dac30a381ba6ed9604ae59
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / BasicWEB.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67         Implements a simplistic WEB server.  Every time a connection is made and\r
68         data is received a dynamic page that shows the current TCP/IP statistics\r
69         is generated and returned.  The connection is then closed.\r
70 \r
71         This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
72         party.\r
73 */\r
74 \r
75 /*\r
76         Changes from V3.2.2\r
77 \r
78         + Changed the page returned by the lwIP WEB server demo to display the \r
79           task status table rather than the TCP/IP statistics.\r
80 */\r
81 \r
82 \r
83 /* Standard includes. */\r
84 #include <stdio.h>\r
85 #include <string.h>\r
86 \r
87 /* Scheduler includes. */\r
88 #include "FreeRTOS.h"\r
89 #include "task.h"\r
90 #include "semphr.h"\r
91 \r
92 /* Demo includes. */\r
93 #include "BasicWEB.h"\r
94 #include "SAM7_EMAC.h"\r
95 \r
96 /* lwIP includes. */\r
97 #include "lwip/api.h" \r
98 #include "lwip/tcpip.h"\r
99 #include "lwip/memp.h" \r
100 #include "lwip/stats.h"\r
101 #include "netif/loopif.h"\r
102 \r
103 /* The size of the buffer in which the dynamic WEB page is created. */\r
104 #define webMAX_PAGE_SIZE        2048\r
105 \r
106 /* Standard GET response. */\r
107 #define webHTTP_OK      "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
108 \r
109 /* The port on which we listen. */\r
110 #define webHTTP_PORT            ( 80 )\r
111 \r
112 /* Delay on close error. */\r
113 #define webSHORT_DELAY          ( 10 )\r
114 \r
115 /* Format of the dynamic page that is returned on each connection. */\r
116 #define webHTML_START \\r
117 "<html>\\r
118 <head>\\r
119 </head>\\r
120 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
121 \r\nPage Hits = "\r
122 \r
123 #define webHTML_END \\r
124 "\r\n</pre>\\r
125 \r\n</BODY>\\r
126 </html>"\r
127 \r
128 /*------------------------------------------------------------*/\r
129 \r
130 /* \r
131  * Process an incoming connection on port 80.\r
132  *\r
133  * This simply checks to see if the incoming data contains a GET request, and\r
134  * if so sends back a single dynamically created page.  The connection is then\r
135  * closed.  A more complete implementation could create a task for each \r
136  * connection. \r
137  */\r
138 static void vProcessConnection( struct netconn *pxNetCon );\r
139 \r
140 /*------------------------------------------------------------*/\r
141 \r
142 static void vProcessConnection( struct netconn *pxNetCon )\r
143 {\r
144 static char cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];\r
145 struct netbuf *pxRxBuffer;\r
146 char *pcRxString;\r
147 unsigned short usLength;\r
148 static unsigned long ulPageHits = 0;\r
149 \r
150         /* We expect to immediately get data. */\r
151         pxRxBuffer = netconn_recv( pxNetCon );\r
152 \r
153         if( pxRxBuffer != NULL )\r
154         {\r
155                 /* Where is the data? */\r
156                 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );      \r
157         \r
158                 /* Is this a GET?  We don't handle anything else. */\r
159                 if( !strncmp( pcRxString, "GET", 3 ) )\r
160                 {\r
161                         pcRxString = cDynamicPage;\r
162 \r
163                         /* Update the hit count. */\r
164                         ulPageHits++;\r
165                         sprintf( cPageHits, "%lu", ulPageHits );\r
166 \r
167                         /* Write out the HTTP OK header. */\r
168             netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );\r
169 \r
170                         /* Generate the dynamic page...\r
171 \r
172                         ... First the page header. */\r
173                         strcpy( cDynamicPage, webHTML_START );\r
174                         /* ... Then the hit count... */\r
175                         strcat( cDynamicPage, cPageHits );\r
176                         strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack     #<br>************************************************<br>" );\r
177                         /* ... Then the list of tasks and their status... */\r
178                         vTaskList( ( signed char * ) cDynamicPage + strlen( cDynamicPage ) );   \r
179                         /* ... Finally the page footer. */\r
180                         strcat( cDynamicPage, webHTML_END );\r
181 \r
182                         /* Write out the dynamically generated page. */\r
183                         netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );\r
184                 }\r
185  \r
186                 netbuf_delete( pxRxBuffer );\r
187         }\r
188 \r
189         netconn_close( pxNetCon );\r
190 }\r
191 /*------------------------------------------------------------*/\r
192 \r
193 void vlwIPInit( void )\r
194 {\r
195     /* Initialize lwIP and its interface layer. */\r
196         sys_init();\r
197         mem_init();                                                             \r
198         memp_init();\r
199         pbuf_init(); \r
200         netif_init();\r
201         ip_init();\r
202         tcpip_init( NULL, NULL );\r
203 }\r
204 /*------------------------------------------------------------*/\r
205 \r
206 void vBasicWEBServer( void *pvParameters )\r
207 {\r
208 struct netconn *pxHTTPListener, *pxNewConnection;\r
209 struct ip_addr xIpAddr, xNetMast, xGateway;\r
210 extern err_t ethernetif_init( struct netif *netif );\r
211 static struct netif EMAC_if;\r
212 \r
213         /* Parameters are not used - suppress compiler error. */\r
214         ( void ) pvParameters;\r
215 \r
216 \r
217         /* Create and configure the EMAC interface. */\r
218         IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);\r
219         IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);\r
220         IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);\r
221         netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);\r
222 \r
223         /* make it the default interface */\r
224     netif_set_default(&EMAC_if);\r
225 \r
226         /* bring it up */\r
227     netif_set_up(&EMAC_if);\r
228         \r
229         /* Create a new tcp connection handle */\r
230 \r
231         pxHTTPListener = netconn_new( NETCONN_TCP );\r
232         netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );\r
233         netconn_listen( pxHTTPListener );\r
234 \r
235         /* Loop forever */\r
236         for( ;; )\r
237         {\r
238                 /* Wait for connection. */\r
239                 pxNewConnection = netconn_accept(pxHTTPListener);\r
240 \r
241                 if(pxNewConnection != NULL)\r
242                 {\r
243                         /* Service connection. */\r
244                         vProcessConnection( pxNewConnection );\r
245                         while( netconn_delete( pxNewConnection ) != ERR_OK )\r
246                         {\r
247                                 vTaskDelay( webSHORT_DELAY );\r
248                         }\r
249                 }\r
250         }\r
251 }\r
252 \r
253 \r