]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WizNET_DEMO_TERN_186/HTTPTask.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / WizNET_DEMO_TERN_186 / HTTPTask.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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  * Very simple task that responds with a single WEB page to http requests.\r
30  *\r
31  * The WEB page displays task and system status.  A semaphore is used to\r
32  * wake the task when there is processing to perform as determined by the\r
33  * interrupts generated by the Ethernet interface.\r
34  */\r
35 \r
36 /* Standard includes. */\r
37 #include <string.h>\r
38 #include <stdio.h>\r
39 \r
40 /* Tern includes. */\r
41 #include "utils\system_common.h"\r
42 #include "i2chip_hw.h"\r
43 #include "socket.h"\r
44 \r
45 /* FreeRTOS.org includes. */\r
46 #include <FreeRTOS.h>\r
47 #include <task.h>\r
48 #include <semphr.h>\r
49 \r
50 /* The standard http port on which we are going to listen. */\r
51 #define httpPORT 80\r
52 \r
53 #define httpTX_WAIT 2\r
54 \r
55 /* Network address configuration. */\r
56 const unsigned char ucMacAddress[] =                    { 12, 128, 12, 34, 56, 78 };\r
57 const unsigned char ucGatewayAddress[] =                { 192, 168, 2, 1 };\r
58 const unsigned char ucIPAddress[] =                             { 172, 25, 218, 210 };\r
59 const unsigned char ucSubnetMask[] =                    { 255, 255, 255, 0 };\r
60 \r
61 /* The number of sockets this task is going to handle. */\r
62 #define httpSOCKET_NUM                       3\r
63 unsigned char ucConnection[ httpSOCKET_NUM ];\r
64 \r
65 /* The maximum data buffer size we can handle. */\r
66 #define httpSOCKET_BUFFER_SIZE  2048\r
67 \r
68 /* Standard HTTP response. */\r
69 #define httpOUTPUT_OK   "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"\r
70 \r
71 /* Hard coded HTML components.  Other data is generated dynamically. */\r
72 #define HTML_OUTPUT_BEGIN "\\r
73 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\\r
74 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\\r
75 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\\r
76 <HR>Task status table:\r\n\\r
77 <p><font face=\"courier\"><pre>Task          State  Priority  Stack     #<br>\\r
78 ************************************************<br>"\r
79 \r
80 #define HTML_OUTPUT_END   "\\r
81 </font></BODY></HTML>"\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /*\r
86  * Initialise the data structures used to hold the socket status.\r
87  */\r
88 static void prvHTTPInit( void );\r
89 \r
90 /*\r
91  * Setup the Ethernet interface with the network addressing information.\r
92  */\r
93 static void prvNetifInit( void );\r
94 \r
95 /*\r
96  * Generate the dynamic components of the served WEB page and transmit the\r
97  * entire page through the socket.\r
98  */\r
99 static void prvTransmitHTTP( unsigned char socket );\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /* This variable is simply incremented by the idle task hook so the number of\r
103 iterations the idle task has performed can be displayed as part of the served\r
104 page. */\r
105 unsigned long ulIdleLoops = 0UL;\r
106 \r
107 /* Data buffer shared by sockets. */\r
108 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];\r
109 \r
110 /* The semaphore used by the Ethernet ISR to signal that the task should wake\r
111 and process whatever caused the interrupt. */\r
112 SemaphoreHandle_t xTCPSemaphore = NULL;\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 void vHTTPTask( void * pvParameters )\r
116 {\r
117 short i, sLen;\r
118 unsigned char ucState;\r
119 \r
120         ( void ) pvParameters;\r
121 \r
122     /* Create the semaphore used to communicate between this task and the\r
123     WIZnet ISR. */\r
124     vSemaphoreCreateBinary( xTCPSemaphore );\r
125 \r
126         /* Make sure everything is setup before we start. */\r
127         prvNetifInit();\r
128         prvHTTPInit();\r
129 \r
130         for( ;; )\r
131         {\r
132                 /* Wait until the ISR tells us there is something to do. */\r
133         xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );\r
134 \r
135                 /* Check each socket. */\r
136                 for( i = 0; i < httpSOCKET_NUM; i++ )\r
137                 {\r
138                         ucState = select( i, SEL_CONTROL );\r
139 \r
140                         switch (ucState)\r
141                         {\r
142                                 case SOCK_ESTABLISHED :  /* new connection established. */\r
143 \r
144                                         if( ( sLen = select( i, SEL_RECV ) ) > 0 )\r
145                                         {\r
146                                                 if( sLen > httpSOCKET_BUFFER_SIZE )\r
147                                                 {\r
148                                                         sLen = httpSOCKET_BUFFER_SIZE;\r
149                                                 }\r
150 \r
151                                                 disable();\r
152 \r
153                                                 sLen = recv( i, ucSocketBuffer, sLen );\r
154 \r
155                                                 if( ucConnection[ i ] == 1 )\r
156                                                 {\r
157                                                         /* This is our first time processing a HTTP\r
158                                                          request on this connection. */\r
159                                                         prvTransmitHTTP( i );\r
160                                                         ucConnection[i] = 0;\r
161                                                 }\r
162                                                 enable();\r
163                                         }\r
164                                         break;\r
165 \r
166                                 case SOCK_CLOSE_WAIT :\r
167 \r
168                                         close(i);\r
169                                         break;\r
170 \r
171                                 case SOCK_CLOSED :\r
172 \r
173                                         ucConnection[i] = 1;\r
174                                         socket( i, SOCK_STREAM, 80, 0x00 );\r
175                                         NBlisten( i ); /* reinitialize socket. */\r
176                                         break;\r
177                         }\r
178                 }\r
179         }\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvHTTPInit( void )\r
184 {\r
185 unsigned char ucIndex;\r
186 \r
187         /* There are 4 total sockets available; we will claim 3 for HTTP. */\r
188         for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)\r
189         {\r
190                 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );\r
191                 NBlisten( ucIndex );\r
192                 ucConnection[ ucIndex ] = 1;\r
193         }\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static void prvNetifInit( void )\r
198 {\r
199         i2chip_init();\r
200         initW3100A();\r
201 \r
202         setMACAddr( ( unsigned char * ) ucMacAddress );\r
203         setgateway( ( unsigned char * ) ucGatewayAddress );\r
204         setsubmask( ( unsigned char * ) ucSubnetMask );\r
205         setIP( ( unsigned char * ) ucIPAddress );\r
206 \r
207         /* See definition of 'sysinit' in socket.c\r
208          - 8 KB transmit buffer, and 8 KB receive buffer available.  These buffers\r
209            are shared by all 4 channels.\r
210          - (0x55, 0x55) configures the send and receive buffers at\r
211                 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */\r
212         sysinit( 0x55, 0x55 );\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void prvTransmitHTTP(unsigned char socket)\r
217 {\r
218 extern short usCheckStatus;\r
219 \r
220         /* Send the http and html headers. */\r
221         send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );\r
222         send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );\r
223 \r
224         /* Generate then send the table showing the status of each task. */\r
225         vTaskList( ( char * ) ucSocketBuffer );\r
226         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
227 \r
228         /* Send the number of times the idle task has looped. */\r
229     sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );\r
230         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
231 \r
232         /* Send the tick count. */\r
233     sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );\r
234         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
235 \r
236         /* Show a message indicating whether or not the check task has discovered\r
237         an error in any of the standard demo tasks. */\r
238     if( usCheckStatus == 0 )\r
239     {\r
240             sprintf( ucSocketBuffer, "No errors detected." );\r
241                 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
242     }\r
243     else\r
244     {\r
245             sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );\r
246                 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
247     }\r
248 \r
249         /* Finish the page off. */\r
250         send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));\r
251 \r
252         /* Must make sure the data is gone before closing the socket. */\r
253         while( !tx_empty( socket ) )\r
254     {\r
255         vTaskDelay( httpTX_WAIT );\r
256     }\r
257         close(socket);\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 void vApplicationIdleHook( void )\r
262 {\r
263         ulIdleLoops++;\r
264 }\r
265 \r
266 \r
267 \r
268 \r
269 \r
270 \r
271 \r
272 \r
273 \r
274 \r
275 \r
276 \r
277 \r
278 \r
279 \r
280 \r
281 \r
282 \r
283 \r
284 \r