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