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