2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Very simple task that responds with a single WEB page to http requests.
\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
36 /* Standard includes. */
\r
40 /* Tern includes. */
\r
41 #include "utils\system_common.h"
\r
42 #include "i2chip_hw.h"
\r
45 /* FreeRTOS.org includes. */
\r
46 #include <FreeRTOS.h>
\r
50 /* The standard http port on which we are going to listen. */
\r
53 #define httpTX_WAIT 2
\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
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
65 /* The maximum data buffer size we can handle. */
\r
66 #define httpSOCKET_BUFFER_SIZE 2048
\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
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
80 #define HTML_OUTPUT_END "\
\r
81 </font></BODY></HTML>"
\r
83 /*-----------------------------------------------------------*/
\r
86 * Initialise the data structures used to hold the socket status.
\r
88 static void prvHTTPInit( void );
\r
91 * Setup the Ethernet interface with the network addressing information.
\r
93 static void prvNetifInit( void );
\r
96 * Generate the dynamic components of the served WEB page and transmit the
\r
97 * entire page through the socket.
\r
99 static void prvTransmitHTTP( unsigned char socket );
\r
100 /*-----------------------------------------------------------*/
\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
105 unsigned long ulIdleLoops = 0UL;
\r
107 /* Data buffer shared by sockets. */
\r
108 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\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
114 /*-----------------------------------------------------------*/
\r
115 void vHTTPTask( void * pvParameters )
\r
118 unsigned char ucState;
\r
120 ( void ) pvParameters;
\r
122 /* Create the semaphore used to communicate between this task and the
\r
124 vSemaphoreCreateBinary( xTCPSemaphore );
\r
126 /* Make sure everything is setup before we start. */
\r
132 /* Wait until the ISR tells us there is something to do. */
\r
133 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
135 /* Check each socket. */
\r
136 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
138 ucState = select( i, SEL_CONTROL );
\r
142 case SOCK_ESTABLISHED : /* new connection established. */
\r
144 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
146 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
148 sLen = httpSOCKET_BUFFER_SIZE;
\r
153 sLen = recv( i, ucSocketBuffer, sLen );
\r
155 if( ucConnection[ i ] == 1 )
\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
166 case SOCK_CLOSE_WAIT :
\r
173 ucConnection[i] = 1;
\r
174 socket( i, SOCK_STREAM, 80, 0x00 );
\r
175 NBlisten( i ); /* reinitialize socket. */
\r
181 /*-----------------------------------------------------------*/
\r
183 static void prvHTTPInit( void )
\r
185 unsigned char ucIndex;
\r
187 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
188 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
190 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
191 NBlisten( ucIndex );
\r
192 ucConnection[ ucIndex ] = 1;
\r
195 /*-----------------------------------------------------------*/
\r
197 static void prvNetifInit( void )
\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
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
214 /*-----------------------------------------------------------*/
\r
216 static void prvTransmitHTTP(unsigned char socket)
\r
218 extern short usCheckStatus;
\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
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
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
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
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
240 sprintf( ucSocketBuffer, "No errors detected." );
\r
241 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\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
249 /* Finish the page off. */
\r
250 send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
252 /* Must make sure the data is gone before closing the socket. */
\r
253 while( !tx_empty( socket ) )
\r
255 vTaskDelay( httpTX_WAIT );
\r
259 /*-----------------------------------------------------------*/
\r
261 void vApplicationIdleHook( void )
\r