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