]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF52259_CodeWarrior/HTTPDemo.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / ColdFire_MCF52259_CodeWarrior / HTTPDemo.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30     Implements a simplistic WEB server.  Every time a connection is made and\r
31     data is received a dynamic page that shows the current TCP/IP statistics\r
32     is generated and returned.  The connection is then closed.\r
33 \r
34     This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
35     party.\r
36 */\r
37 \r
38 /* ------------------------ System includes ------------------------------- */\r
39 \r
40 \r
41 /* ------------------------ FreeRTOS includes ----------------------------- */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 #include "semphr.h"\r
45 \r
46 /* ------------------------ lwIP includes --------------------------------- */\r
47 #include "lwip/api.h"\r
48 #include "lwip/tcpip.h"\r
49 #include "lwip/ip.h"\r
50 #include "lwip/memp.h"\r
51 #include "lwip/stats.h"\r
52 #include "netif/loopif.h"\r
53 \r
54 /* ------------------------ Project includes ------------------------------ */\r
55 #include "common.h"\r
56 \r
57 #include "HTTPDemo.h"\r
58 \r
59 /* ------------------------ Defines --------------------------------------- */\r
60 /* The size of the buffer in which the dynamic WEB page is created. */\r
61 #define webMAX_PAGE_SIZE        ( 1024 ) /*FSL: buffer containing array*/\r
62 \r
63 /* Standard GET response. */\r
64 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
65 \r
66 /* The port on which we listen. */\r
67 #define webHTTP_PORT            ( 80 )\r
68 \r
69 /* Delay on close error. */\r
70 #define webSHORT_DELAY          ( 10 )\r
71 \r
72 /* Format of the dynamic page that is returned on each connection. */\r
73 #define webHTML_START \\r
74 "<html>\\r
75 <head>\\r
76 </head>\\r
77 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
78 \r\n\r\nPage Hits = "\r
79 \r
80 #define webHTML_END \\r
81 "\r\n" \\r
82 "</pre>\r\n" \\r
83 "</BODY>\r\n" \\r
84 "</html>"\r
85 \r
86 #if INCLUDE_uxTaskGetStackHighWaterMark\r
87         static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;\r
88 #endif\r
89 \r
90 /* ------------------------ Prototypes ------------------------------------ */\r
91 static void     vProcessConnection( struct netconn *pxNetCon );\r
92 \r
93 /*------------------------------------------------------------*/\r
94 \r
95 /*\r
96  * Process an incoming connection on port 80.\r
97  *\r
98  * This simply checks to see if the incoming data contains a GET request, and\r
99  * if so sends back a single dynamically created page.  The connection is then\r
100  * closed.  A more complete implementation could create a task for each\r
101  * connection.\r
102  */\r
103 static void vProcessConnection( struct netconn *pxNetCon )\r
104 {\r
105     static char cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];\r
106     struct netbuf  *pxRxBuffer;\r
107     char       *pcRxString;\r
108     unsigned short usLength;\r
109     static unsigned long ulPageHits = 0;\r
110 \r
111     /* We expect to immediately get data. */\r
112     pxRxBuffer = netconn_recv( pxNetCon );\r
113 \r
114     if( pxRxBuffer != NULL )\r
115     {\r
116         /* Where is the data? */\r
117         netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );\r
118 \r
119         /* Is this a GET?  We don't handle anything else. */\r
120         if( !strncmp( pcRxString, "GET", 3 ) )\r
121         {\r
122             pcRxString = cDynamicPage;\r
123 \r
124             /* Update the hit count. */\r
125             ulPageHits++;\r
126             sprintf( cPageHits, "%d", (int)ulPageHits );\r
127 \r
128             /* Write out the HTTP OK header. */\r
129             netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );\r
130 \r
131             /* Generate the dynamic page...\r
132 \r
133                ... First the page header. */\r
134             strcpy( cDynamicPage, webHTML_START );\r
135             /* ... Then the hit count... */\r
136             strcat( cDynamicPage, cPageHits );\r
137 \r
138             strcat( cDynamicPage,\r
139                     "<p><pre>Task          State  Priority  Stack      #<br>************************************************<br>" );\r
140             /* ... Then the list of tasks and their status... */\r
141             vTaskList( cDynamicPage + strlen( cDynamicPage ) );\r
142 \r
143             /* ... Finally the page footer. */\r
144             strcat( cDynamicPage, webHTML_END );\r
145 \r
146             /* Write out the dynamically generated page. */\r
147             netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );\r
148         }\r
149         netbuf_delete( pxRxBuffer );\r
150     }\r
151     netconn_close( pxNetCon );\r
152 }\r
153 \r
154 /*------------------------------------------------------------*/\r
155 \r
156 void vlwIPInit( void )\r
157 {\r
158     /* Initialize lwIP and its interface layer. */\r
159     tcpip_init( NULL, NULL );\r
160 }\r
161 \r
162 /*------------------------------------------------------------*/\r
163 \r
164 void vBasicWEBServer( void *pvParameters )\r
165 {\r
166     struct netconn *pxHTTPListener, *pxNewConnection;\r
167     struct ip_addr  xIpAddr, xNetMast, xGateway;\r
168     static struct netif fec523x_if;\r
169         extern err_t ethernetif_init(struct netif *netif);\r
170 \r
171     /* Parameters are not used - suppress compiler error. */\r
172     ( void )pvParameters;\r
173 \r
174         vlwIPInit();\r
175 \r
176     /* Create and configure the FEC interface. */\r
177     IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
178     IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
179     IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );\r
180     netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );\r
181 \r
182     /* make it the default interface */\r
183     netif_set_default( &fec523x_if );\r
184 \r
185     /* bring it up */\r
186     netif_set_up( &fec523x_if );\r
187 \r
188     /* Create a new tcp connection handle */\r
189     pxHTTPListener = netconn_new( NETCONN_TCP );\r
190     netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
191     netconn_listen( pxHTTPListener );\r
192 \r
193     /* Loop forever */\r
194     for( ;; )\r
195     {\r
196         /* Wait for connection. */\r
197         pxNewConnection = netconn_accept( pxHTTPListener );\r
198 \r
199         if( pxNewConnection != NULL )\r
200         {\r
201             /* Service connection. */\r
202             vProcessConnection( pxNewConnection );\r
203             while( netconn_delete( pxNewConnection ) != ERR_OK )\r
204             {\r
205                 vTaskDelay( webSHORT_DELAY );\r
206             }\r
207         }\r
208     }\r
209 }\r