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