2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Very simple task that responds with a single WEB page to http requests.
\r
32 * The WEB page displays task and system status. A semaphore is used to
\r
33 * wake the task when there is processing to perform as determined by the
\r
34 * interrupts generated by the Ethernet interface.
\r
37 /* Standard includes. */
\r
41 /* Tern includes. */
\r
42 #include "utils\system_common.h"
\r
43 #include "i2chip_hw.h"
\r
46 /* FreeRTOS.org includes. */
\r
47 #include <FreeRTOS.h>
\r
51 /* The standard http port on which we are going to listen. */
\r
54 #define httpTX_WAIT 2
\r
56 /* Network address configuration. */
\r
57 const unsigned char ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
58 const unsigned char ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
59 const unsigned char ucIPAddress[] = { 172, 25, 218, 210 };
\r
60 const unsigned char ucSubnetMask[] = { 255, 255, 255, 0 };
\r
62 /* The number of sockets this task is going to handle. */
\r
63 #define httpSOCKET_NUM 3
\r
64 unsigned char ucConnection[ httpSOCKET_NUM ];
\r
66 /* The maximum data buffer size we can handle. */
\r
67 #define httpSOCKET_BUFFER_SIZE 2048
\r
69 /* Standard HTTP response. */
\r
70 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
72 /* Hard coded HTML components. Other data is generated dynamically. */
\r
73 #define HTML_OUTPUT_BEGIN "\
\r
74 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
75 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
76 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
77 <HR>Task status table:\r\n\
\r
78 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
79 ************************************************<br>"
\r
81 #define HTML_OUTPUT_END "\
\r
82 </font></BODY></HTML>"
\r
84 /*-----------------------------------------------------------*/
\r
87 * Initialise the data structures used to hold the socket status.
\r
89 static void prvHTTPInit( void );
\r
92 * Setup the Ethernet interface with the network addressing information.
\r
94 static void prvNetifInit( void );
\r
97 * Generate the dynamic components of the served WEB page and transmit the
\r
98 * entire page through the socket.
\r
100 static void prvTransmitHTTP( unsigned char socket );
\r
101 /*-----------------------------------------------------------*/
\r
103 /* This variable is simply incremented by the idle task hook so the number of
\r
104 iterations the idle task has performed can be displayed as part of the served
\r
106 unsigned long ulIdleLoops = 0UL;
\r
108 /* Data buffer shared by sockets. */
\r
109 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
111 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
112 and process whatever caused the interrupt. */
\r
113 SemaphoreHandle_t xTCPSemaphore = NULL;
\r
115 /*-----------------------------------------------------------*/
\r
116 void vHTTPTask( void * pvParameters )
\r
119 unsigned char ucState;
\r
121 ( void ) pvParameters;
\r
123 /* Create the semaphore used to communicate between this task and the
\r
125 vSemaphoreCreateBinary( xTCPSemaphore );
\r
127 /* Make sure everything is setup before we start. */
\r
133 /* Wait until the ISR tells us there is something to do. */
\r
134 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
136 /* Check each socket. */
\r
137 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
139 ucState = select( i, SEL_CONTROL );
\r
143 case SOCK_ESTABLISHED : /* new connection established. */
\r
145 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
147 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
149 sLen = httpSOCKET_BUFFER_SIZE;
\r
154 sLen = recv( i, ucSocketBuffer, sLen );
\r
156 if( ucConnection[ i ] == 1 )
\r
158 /* This is our first time processing a HTTP
\r
159 request on this connection. */
\r
160 prvTransmitHTTP( i );
\r
161 ucConnection[i] = 0;
\r
167 case SOCK_CLOSE_WAIT :
\r
174 ucConnection[i] = 1;
\r
175 socket( i, SOCK_STREAM, 80, 0x00 );
\r
176 NBlisten( i ); /* reinitialize socket. */
\r
182 /*-----------------------------------------------------------*/
\r
184 static void prvHTTPInit( void )
\r
186 unsigned char ucIndex;
\r
188 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
189 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
191 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
192 NBlisten( ucIndex );
\r
193 ucConnection[ ucIndex ] = 1;
\r
196 /*-----------------------------------------------------------*/
\r
198 static void prvNetifInit( void )
\r
203 setMACAddr( ( unsigned char * ) ucMacAddress );
\r
204 setgateway( ( unsigned char * ) ucGatewayAddress );
\r
205 setsubmask( ( unsigned char * ) ucSubnetMask );
\r
206 setIP( ( unsigned char * ) ucIPAddress );
\r
208 /* See definition of 'sysinit' in socket.c
\r
209 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
210 are shared by all 4 channels.
\r
211 - (0x55, 0x55) configures the send and receive buffers at
\r
212 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
213 sysinit( 0x55, 0x55 );
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvTransmitHTTP(unsigned char socket)
\r
219 extern short usCheckStatus;
\r
221 /* Send the http and html headers. */
\r
222 send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
223 send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
225 /* Generate then send the table showing the status of each task. */
\r
226 vTaskList( ( char * ) ucSocketBuffer );
\r
227 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
229 /* Send the number of times the idle task has looped. */
\r
230 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
231 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
233 /* Send the tick count. */
\r
234 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
235 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
237 /* Show a message indicating whether or not the check task has discovered
\r
238 an error in any of the standard demo tasks. */
\r
239 if( usCheckStatus == 0 )
\r
241 sprintf( ucSocketBuffer, "No errors detected." );
\r
242 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
246 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
247 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
250 /* Finish the page off. */
\r
251 send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
253 /* Must make sure the data is gone before closing the socket. */
\r
254 while( !tx_empty( socket ) )
\r
256 vTaskDelay( httpTX_WAIT );
\r
260 /*-----------------------------------------------------------*/
\r
262 void vApplicationIdleHook( void )
\r