]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF52259_CodeWarrior/HTTPDemo.c
Change version numbers in preparation for V7.6.0 release.
[freertos] / FreeRTOS / Demo / ColdFire_MCF52259_CodeWarrior / HTTPDemo.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 /* ------------------------ System includes ------------------------------- */\r
76 \r
77 \r
78 /* ------------------------ FreeRTOS includes ----------------------------- */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "semphr.h"\r
82 \r
83 /* ------------------------ lwIP includes --------------------------------- */\r
84 #include "lwip/api.h"\r
85 #include "lwip/tcpip.h"\r
86 #include "lwip/ip.h"\r
87 #include "lwip/memp.h"\r
88 #include "lwip/stats.h"\r
89 #include "netif/loopif.h"\r
90 \r
91 /* ------------------------ Project includes ------------------------------ */\r
92 #include "common.h"\r
93 \r
94 #include "HTTPDemo.h"\r
95 \r
96 /* ------------------------ Defines --------------------------------------- */\r
97 /* The size of the buffer in which the dynamic WEB page is created. */\r
98 #define webMAX_PAGE_SIZE        ( 1024 ) /*FSL: buffer containing array*/\r
99 \r
100 /* Standard GET response. */\r
101 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
102 \r
103 /* The port on which we listen. */\r
104 #define webHTTP_PORT            ( 80 )\r
105 \r
106 /* Delay on close error. */\r
107 #define webSHORT_DELAY          ( 10 )\r
108 \r
109 /* Format of the dynamic page that is returned on each connection. */\r
110 #define webHTML_START \\r
111 "<html>\\r
112 <head>\\r
113 </head>\\r
114 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
115 \r\n\r\nPage Hits = "\r
116 \r
117 #define webHTML_END \\r
118 "\r\n" \\r
119 "</pre>\r\n" \\r
120 "</BODY>\r\n" \\r
121 "</html>"\r
122 \r
123 #if INCLUDE_uxTaskGetStackHighWaterMark\r
124         static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;\r
125 #endif\r
126 \r
127 /* ------------------------ Prototypes ------------------------------------ */\r
128 static void     vProcessConnection( struct netconn *pxNetCon );\r
129 \r
130 /*------------------------------------------------------------*/\r
131 \r
132 /*\r
133  * Process an incoming connection on port 80.\r
134  *\r
135  * This simply checks to see if the incoming data contains a GET request, and\r
136  * if so sends back a single dynamically created page.  The connection is then\r
137  * closed.  A more complete implementation could create a task for each\r
138  * connection.\r
139  */\r
140 static void vProcessConnection( struct netconn *pxNetCon )\r
141 {\r
142     static portCHAR cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];\r
143     struct netbuf  *pxRxBuffer;\r
144     portCHAR       *pcRxString;\r
145     unsigned portSHORT usLength;\r
146     static unsigned portLONG ulPageHits = 0;\r
147 \r
148     /* We expect to immediately get data. */\r
149     pxRxBuffer = netconn_recv( pxNetCon );\r
150 \r
151     if( pxRxBuffer != NULL )\r
152     {\r
153         /* Where is the data? */\r
154         netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );\r
155 \r
156         /* Is this a GET?  We don't handle anything else. */\r
157         if( !strncmp( pcRxString, "GET", 3 ) )\r
158         {\r
159             pcRxString = cDynamicPage;\r
160 \r
161             /* Update the hit count. */\r
162             ulPageHits++;\r
163             sprintf( cPageHits, "%d", (int)ulPageHits );\r
164 \r
165             /* Write out the HTTP OK header. */            \r
166             netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );\r
167 \r
168             /* Generate the dynamic page...\r
169             \r
170                ... First the page header. */\r
171             strcpy( cDynamicPage, webHTML_START );\r
172             /* ... Then the hit count... */\r
173             strcat( cDynamicPage, cPageHits );\r
174             \r
175             strcat( cDynamicPage,\r
176                     "<p><pre>Task          State  Priority  Stack      #<br>************************************************<br>" );\r
177             /* ... Then the list of tasks and their status... */\r
178             vTaskList( ( signed portCHAR * )cDynamicPage + strlen( cDynamicPage ) );            \r
179             \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         netbuf_delete( pxRxBuffer );\r
187     }\r
188     netconn_close( pxNetCon );\r
189 }\r
190 \r
191 /*------------------------------------------------------------*/\r
192 \r
193 void vlwIPInit( void )\r
194 {\r
195     /* Initialize lwIP and its interface layer. */\r
196     tcpip_init( NULL, NULL );\r
197 }\r
198 \r
199 /*------------------------------------------------------------*/\r
200 \r
201 void vBasicWEBServer( void *pvParameters )\r
202 {\r
203     struct netconn *pxHTTPListener, *pxNewConnection;\r
204     struct ip_addr  xIpAddr, xNetMast, xGateway;\r
205     static struct netif fec523x_if;\r
206         extern err_t ethernetif_init(struct netif *netif);\r
207 \r
208     /* Parameters are not used - suppress compiler error. */\r
209     ( void )pvParameters;\r
210 \r
211         vlwIPInit();\r
212 \r
213     /* Create and configure the FEC interface. */\r
214     IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
215     IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
216     IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );\r
217     netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );\r
218 \r
219     /* make it the default interface */\r
220     netif_set_default( &fec523x_if );\r
221 \r
222     /* bring it up */\r
223     netif_set_up( &fec523x_if );\r
224 \r
225     /* Create a new tcp connection handle */\r
226     pxHTTPListener = netconn_new( NETCONN_TCP );\r
227     netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
228     netconn_listen( pxHTTPListener );\r
229 \r
230     /* Loop forever */\r
231     for( ;; )\r
232     {   \r
233         /* Wait for connection. */\r
234         pxNewConnection = netconn_accept( pxHTTPListener );\r
235 \r
236         if( pxNewConnection != NULL )\r
237         {\r
238             /* Service connection. */\r
239             vProcessConnection( pxNewConnection );\r
240             while( netconn_delete( pxNewConnection ) != ERR_OK )\r
241             {\r
242                 vTaskDelay( webSHORT_DELAY );\r
243             }\r
244         }\r
245     }\r
246 }\r