]> git.sur5r.net Git - freertos/blob - Demo/ColdFire_MCF52259_CodeWarrior/HTTPDemo.c
Update to V5.4.1
[freertos] / Demo / ColdFire_MCF52259_CodeWarrior / HTTPDemo.c
1 /*\r
2         FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /*\r
49     Implements a simplistic WEB server.  Every time a connection is made and\r
50     data is received a dynamic page that shows the current TCP/IP statistics\r
51     is generated and returned.  The connection is then closed.\r
52 \r
53     This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
54     party.\r
55 */\r
56 \r
57 /* ------------------------ System includes ------------------------------- */\r
58 \r
59 \r
60 /* ------------------------ FreeRTOS includes ----------------------------- */\r
61 #include "FreeRTOS.h"\r
62 #include "task.h"\r
63 #include "semphr.h"\r
64 \r
65 /* ------------------------ lwIP includes --------------------------------- */\r
66 #include "lwip/api.h"\r
67 #include "lwip/tcpip.h"\r
68 #include "lwip/ip.h"\r
69 #include "lwip/memp.h"\r
70 #include "lwip/stats.h"\r
71 #include "netif/loopif.h"\r
72 \r
73 /* ------------------------ Project includes ------------------------------ */\r
74 #include "common.h"\r
75 \r
76 #include "HTTPDemo.h"\r
77 \r
78 /* ------------------------ Defines --------------------------------------- */\r
79 /* The size of the buffer in which the dynamic WEB page is created. */\r
80 #define webMAX_PAGE_SIZE        ( 1024 ) /*FSL: buffer containing array*/\r
81 \r
82 /* Standard GET response. */\r
83 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
84 \r
85 /* The port on which we listen. */\r
86 #define webHTTP_PORT            ( 80 )\r
87 \r
88 /* Delay on close error. */\r
89 #define webSHORT_DELAY          ( 10 )\r
90 \r
91 /* Format of the dynamic page that is returned on each connection. */\r
92 #define webHTML_START \\r
93 "<html>\\r
94 <head>\\r
95 </head>\\r
96 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
97 \r\n\r\nPage Hits = "\r
98 \r
99 #define webHTML_END \\r
100 "\r\n" \\r
101 "</pre>\r\n" \\r
102 "</BODY>\r\n" \\r
103 "</html>"\r
104 \r
105 #if INCLUDE_uxTaskGetStackHighWaterMark\r
106         static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;\r
107 #endif\r
108 \r
109 /* ------------------------ Prototypes ------------------------------------ */\r
110 static void     vProcessConnection( struct netconn *pxNetCon );\r
111 \r
112 /*------------------------------------------------------------*/\r
113 \r
114 /*\r
115  * Process an incoming connection on port 80.\r
116  *\r
117  * This simply checks to see if the incoming data contains a GET request, and\r
118  * if so sends back a single dynamically created page.  The connection is then\r
119  * closed.  A more complete implementation could create a task for each\r
120  * connection.\r
121  */\r
122 static void vProcessConnection( struct netconn *pxNetCon )\r
123 {\r
124     static portCHAR cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];\r
125     struct netbuf  *pxRxBuffer;\r
126     portCHAR       *pcRxString;\r
127     unsigned portSHORT usLength;\r
128     static unsigned portLONG ulPageHits = 0;\r
129 \r
130     /* We expect to immediately get data. */\r
131     pxRxBuffer = netconn_recv( pxNetCon );\r
132 \r
133     if( pxRxBuffer != NULL )\r
134     {\r
135         /* Where is the data? */\r
136         netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );\r
137 \r
138         /* Is this a GET?  We don't handle anything else. */\r
139         if( !strncmp( pcRxString, "GET", 3 ) )\r
140         {\r
141             pcRxString = cDynamicPage;\r
142 \r
143             /* Update the hit count. */\r
144             ulPageHits++;\r
145             sprintf( cPageHits, "%d", (int)ulPageHits );\r
146 \r
147             /* Write out the HTTP OK header. */            \r
148             netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );\r
149 \r
150             /* Generate the dynamic page...\r
151             \r
152                ... First the page header. */\r
153             strcpy( cDynamicPage, webHTML_START );\r
154             /* ... Then the hit count... */\r
155             strcat( cDynamicPage, cPageHits );\r
156             \r
157             strcat( cDynamicPage,\r
158                     "<p><pre>Task          State  Priority  Stack      #<br>************************************************<br>" );\r
159             /* ... Then the list of tasks and their status... */\r
160             vTaskList( ( signed portCHAR * )cDynamicPage + strlen( cDynamicPage ) );            \r
161             \r
162             /* ... Finally the page footer. */\r
163             strcat( cDynamicPage, webHTML_END );\r
164 \r
165             /* Write out the dynamically generated page. */\r
166             netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );\r
167         }\r
168         netbuf_delete( pxRxBuffer );\r
169     }\r
170     netconn_close( pxNetCon );\r
171 }\r
172 \r
173 /*------------------------------------------------------------*/\r
174 \r
175 void vlwIPInit( void )\r
176 {\r
177     /* Initialize lwIP and its interface layer. */\r
178     tcpip_init( NULL, NULL );\r
179 }\r
180 \r
181 /*------------------------------------------------------------*/\r
182 \r
183 void vBasicWEBServer( void *pvParameters )\r
184 {\r
185     struct netconn *pxHTTPListener, *pxNewConnection;\r
186     struct ip_addr  xIpAddr, xNetMast, xGateway;\r
187     static struct netif fec523x_if;\r
188         extern err_t ethernetif_init(struct netif *netif);\r
189 \r
190     /* Parameters are not used - suppress compiler error. */\r
191     ( void )pvParameters;\r
192 \r
193         vlwIPInit();\r
194 \r
195     /* Create and configure the FEC interface. */\r
196     IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
197     IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
198     IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );\r
199     netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );\r
200 \r
201     /* make it the default interface */\r
202     netif_set_default( &fec523x_if );\r
203 \r
204     /* bring it up */\r
205     netif_set_up( &fec523x_if );\r
206 \r
207     /* Create a new tcp connection handle */\r
208     pxHTTPListener = netconn_new( NETCONN_TCP );\r
209     netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
210     netconn_listen( pxHTTPListener );\r
211 \r
212     /* Loop forever */\r
213     for( ;; )\r
214     {   \r
215         /* Wait for connection. */\r
216         pxNewConnection = netconn_accept( pxHTTPListener );\r
217 \r
218         if( pxNewConnection != NULL )\r
219         {\r
220             /* Service connection. */\r
221             vProcessConnection( pxNewConnection );\r
222             while( netconn_delete( pxNewConnection ) != ERR_OK )\r
223             {\r
224                 vTaskDelay( webSHORT_DELAY );\r
225             }\r
226         }\r
227     }\r
228 }\r