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