]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/BasicWEB.c
e4b57544ad43d865f239fa1c976268e6edcdd6e6
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / BasicWEB.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29         Implements a simplistic WEB server.  Every time a connection is made and\r
30         data is received a dynamic page that shows the current TCP/IP statistics\r
31         is generated and returned.  The connection is then closed.\r
32 \r
33         This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
34         party.\r
35 */\r
36 \r
37 /*\r
38         Changes from V3.2.2\r
39 \r
40         + Changed the page returned by the lwIP WEB server demo to display the\r
41           task status table rather than the TCP/IP statistics.\r
42 */\r
43 \r
44 \r
45 /* Standard includes. */\r
46 #include <stdio.h>\r
47 #include <string.h>\r
48 \r
49 /* Scheduler includes. */\r
50 #include "FreeRTOS.h"\r
51 #include "task.h"\r
52 #include "semphr.h"\r
53 \r
54 /* Demo includes. */\r
55 #include "BasicWEB.h"\r
56 #include "SAM7_EMAC.h"\r
57 \r
58 /* lwIP includes. */\r
59 #include "lwip/api.h"\r
60 #include "lwip/tcpip.h"\r
61 #include "lwip/memp.h"\r
62 #include "lwip/stats.h"\r
63 #include "netif/loopif.h"\r
64 \r
65 /* The size of the buffer in which the dynamic WEB page is created. */\r
66 #define webMAX_PAGE_SIZE        2048\r
67 \r
68 /* Standard GET response. */\r
69 #define webHTTP_OK      "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
70 \r
71 /* The port on which we listen. */\r
72 #define webHTTP_PORT            ( 80 )\r
73 \r
74 /* Delay on close error. */\r
75 #define webSHORT_DELAY          ( 10 )\r
76 \r
77 /* Format of the dynamic page that is returned on each connection. */\r
78 #define webHTML_START \\r
79 "<html>\\r
80 <head>\\r
81 </head>\\r
82 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
83 \r\nPage Hits = "\r
84 \r
85 #define webHTML_END \\r
86 "\r\n</pre>\\r
87 \r\n</BODY>\\r
88 </html>"\r
89 \r
90 /*------------------------------------------------------------*/\r
91 \r
92 /*\r
93  * Process an incoming connection on port 80.\r
94  *\r
95  * This simply checks to see if the incoming data contains a GET request, and\r
96  * if so sends back a single dynamically created page.  The connection is then\r
97  * closed.  A more complete implementation could create a task for each\r
98  * connection.\r
99  */\r
100 static void vProcessConnection( struct netconn *pxNetCon );\r
101 \r
102 /*------------------------------------------------------------*/\r
103 \r
104 static void vProcessConnection( struct netconn *pxNetCon )\r
105 {\r
106 static char cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];\r
107 struct netbuf *pxRxBuffer;\r
108 char *pcRxString;\r
109 unsigned short usLength;\r
110 static unsigned long ulPageHits = 0;\r
111 \r
112         /* We expect to immediately get data. */\r
113         pxRxBuffer = netconn_recv( pxNetCon );\r
114 \r
115         if( pxRxBuffer != NULL )\r
116         {\r
117                 /* Where is the data? */\r
118                 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );\r
119 \r
120                 /* Is this a GET?  We don't handle anything else. */\r
121                 if( !strncmp( pcRxString, "GET", 3 ) )\r
122                 {\r
123                         pcRxString = cDynamicPage;\r
124 \r
125                         /* Update the hit count. */\r
126                         ulPageHits++;\r
127                         sprintf( cPageHits, "%lu", ulPageHits );\r
128 \r
129                         /* Write out the HTTP OK header. */\r
130             netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );\r
131 \r
132                         /* Generate the dynamic page...\r
133 \r
134                         ... First the page header. */\r
135                         strcpy( cDynamicPage, webHTML_START );\r
136                         /* ... Then the hit count... */\r
137                         strcat( cDynamicPage, cPageHits );\r
138                         strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack     #<br>************************************************<br>" );\r
139                         /* ... Then the list of tasks and their status... */\r
140                         vTaskList( cDynamicPage + strlen( cDynamicPage ) );\r
141                         /* ... Finally the page footer. */\r
142                         strcat( cDynamicPage, webHTML_END );\r
143 \r
144                         /* Write out the dynamically generated page. */\r
145                         netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );\r
146                 }\r
147 \r
148                 netbuf_delete( pxRxBuffer );\r
149         }\r
150 \r
151         netconn_close( pxNetCon );\r
152 }\r
153 /*------------------------------------------------------------*/\r
154 \r
155 void vlwIPInit( void )\r
156 {\r
157     /* Initialize lwIP and its interface layer. */\r
158         sys_init();\r
159         mem_init();\r
160         memp_init();\r
161         pbuf_init();\r
162         netif_init();\r
163         ip_init();\r
164         tcpip_init( NULL, NULL );\r
165 }\r
166 /*------------------------------------------------------------*/\r
167 \r
168 void vBasicWEBServer( void *pvParameters )\r
169 {\r
170 struct netconn *pxHTTPListener, *pxNewConnection;\r
171 struct ip_addr xIpAddr, xNetMast, xGateway;\r
172 extern err_t ethernetif_init( struct netif *netif );\r
173 static struct netif EMAC_if;\r
174 \r
175         /* Parameters are not used - suppress compiler error. */\r
176         ( void ) pvParameters;\r
177 \r
178 \r
179         /* Create and configure the EMAC interface. */\r
180         IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);\r
181         IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);\r
182         IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);\r
183         netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);\r
184 \r
185         /* make it the default interface */\r
186     netif_set_default(&EMAC_if);\r
187 \r
188         /* bring it up */\r
189     netif_set_up(&EMAC_if);\r
190 \r
191         /* Create a new tcp connection handle */\r
192 \r
193         pxHTTPListener = netconn_new( NETCONN_TCP );\r
194         netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );\r
195         netconn_listen( pxHTTPListener );\r
196 \r
197         /* Loop forever */\r
198         for( ;; )\r
199         {\r
200                 /* Wait for connection. */\r
201                 pxNewConnection = netconn_accept(pxHTTPListener);\r
202 \r
203                 if(pxNewConnection != NULL)\r
204                 {\r
205                         /* Service connection. */\r
206                         vProcessConnection( pxNewConnection );\r
207                         while( netconn_delete( pxNewConnection ) != ERR_OK )\r
208                         {\r
209                                 vTaskDelay( webSHORT_DELAY );\r
210                         }\r
211                 }\r
212         }\r
213 }\r
214 \r
215 \r