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