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
28 /* Standard includes. */
\r
31 /* FreeRTOS includes. */
\r
32 #include "FreeRTOS.h"
\r
36 /* lwIP core includes */
\r
37 #include "lwip/opt.h"
\r
38 #include "lwip/tcpip.h"
\r
39 #include "lwip/inet.h"
\r
40 #include "lwip/dhcp.h"
\r
42 /* applications includes */
\r
43 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"
\r
45 /* include the port-dependent configuration */
\r
46 #include "lwipcfg_msvc.h"
\r
48 /* Dimensions the cTxBuffer array - which is itself used to hold replies from
\r
49 command line commands. cTxBuffer is a shared buffer, so protected by the
\r
50 xTxBufferMutex mutex. */
\r
51 #define lwipappsTX_BUFFER_SIZE 1024
\r
53 /* The maximum time to block waiting to obtain the xTxBufferMutex to become
\r
55 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ( 100 / portTICK_RATE_MS )
\r
57 /* Definitions of the various SSI callback functions within the pccSSITags
\r
58 array. If pccSSITags is updated, then these definitions must also be updated. */
\r
59 #define ssiTASK_STATS_INDEX 0
\r
60 #define ssiRUN_TIME_STATS_INDEX 1
\r
62 /*-----------------------------------------------------------*/
\r
65 * The function that implements the lwIP based sockets command interpreter
\r
68 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );
\r
71 * The SSI handler callback function passed to lwIP.
\r
73 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );
\r
75 /*-----------------------------------------------------------*/
\r
77 /* The SSI strings that are embedded in the served html files. If this array
\r
78 is changed, then the index position defined by the #defines such as
\r
79 ssiTASK_STATS_INDEX above must also be updated. */
\r
80 static const char *pccSSITags[] =
\r
86 /* Semaphore used to guard the Tx buffer. */
\r
87 static xSemaphoreHandle xTxBufferMutex = NULL;
\r
89 /* The Tx buffer itself. This is used to hold the text generated by the
\r
90 execution of command line commands, and (hopefully) the execution of
\r
91 server side include callbacks. It is a shared buffer so protected by the
\r
92 xTxBufferMutex mutex. pcLwipAppsBlockingGetTxBuffer() and
\r
93 vLwipAppsReleaseTxBuffer() are provided to obtain and release the
\r
94 xTxBufferMutex respectively. pcLwipAppsBlockingGetTxBuffer() must be used with
\r
95 caution as it has the potential to block. */
\r
96 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];
\r
98 /*-----------------------------------------------------------*/
\r
100 void vStatusCallback( struct netif *pxNetIf )
\r
102 char pcMessage[20];
\r
103 struct in_addr* pxIPAddress;
\r
105 if( netif_is_up( pxNetIf ) != 0 )
\r
107 strcpy( pcMessage, "IP=" );
\r
108 pxIPAddress = ( struct in_addr* ) &( pxNetIf->ip_addr );
\r
109 strcat( pcMessage, inet_ntoa( ( *pxIPAddress ) ) );
\r
110 xil_printf( pcMessage );
\r
114 xil_printf( "Network is down" );
\r
118 /* Called from the TCP/IP thread. */
\r
119 void lwIPAppsInit( void *pvArgument )
\r
121 ip_addr_t xIPAddr, xNetMask, xGateway;
\r
122 extern err_t xemacliteif_init(struct netif *netif);
\r
123 extern void xemacif_input_thread( void *netif );
\r
124 static struct netif xNetIf;
\r
126 ( void ) pvArgument;
\r
128 /* Set up the network interface. */
\r
129 ip_addr_set_zero( &xGateway );
\r
130 ip_addr_set_zero( &xIPAddr );
\r
131 ip_addr_set_zero( &xNetMask );
\r
133 LWIP_PORT_INIT_GW(&xGateway);
\r
134 LWIP_PORT_INIT_IPADDR( &xIPAddr );
\r
135 LWIP_PORT_INIT_NETMASK(&xNetMask);
\r
137 /* Set mac address */
\r
138 xNetIf.hwaddr_len = 6;
\r
139 xNetIf.hwaddr[ 0 ] = configMAC_ADDR0;
\r
140 xNetIf.hwaddr[ 1 ] = configMAC_ADDR1;
\r
141 xNetIf.hwaddr[ 2 ] = configMAC_ADDR2;
\r
142 xNetIf.hwaddr[ 3 ] = configMAC_ADDR3;
\r
143 xNetIf.hwaddr[ 4 ] = configMAC_ADDR4;
\r
144 xNetIf.hwaddr[ 5 ] = configMAC_ADDR5;
\r
146 netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, ( void * ) XPAR_AXI_ETHERNETLITE_0_BASEADDR, xemacliteif_init, tcpip_input ) );
\r
147 netif_set_status_callback( &xNetIf, vStatusCallback );
\r
150 dhcp_start( &xNetIf );
\r
154 netif_set_up( &xNetIf );
\r
158 /* Install the server side include handler. */
\r
159 http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );
\r
161 /* Create the mutex used to ensure mutual exclusive access to the Tx
\r
163 xTxBufferMutex = xSemaphoreCreateMutex();
\r
164 configASSERT( xTxBufferMutex );
\r
166 /* Create the httpd server from the standard lwIP code. This demonstrates
\r
167 use of the lwIP raw API. */
\r
170 sys_thread_new( "lwIP_In", xemacif_input_thread, &xNetIf, configMINIMAL_STACK_SIZE, configMAC_INPUT_TASK_PRIORITY );
\r
172 /* Create the FreeRTOS defined basic command server. This demonstrates use
\r
173 of the lwIP sockets API. */
\r
174 xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 5, NULL, configCLI_TASK_PRIORITY, NULL );
\r
176 /*-----------------------------------------------------------*/
\r
178 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )
\r
180 static unsigned int uiUpdateCount = 0;
\r
181 static char cUpdateString[ 200 ];
\r
182 extern char *pcMainGetTaskStatusMessage( void );
\r
184 /* Unused parameter. */
\r
185 ( void ) iBufferLength;
\r
187 /* The SSI handler function that generates text depending on the index of
\r
188 the SSI tag encountered. */
\r
192 case ssiTASK_STATS_INDEX :
\r
193 vTaskList( pcBuffer );
\r
196 case ssiRUN_TIME_STATS_INDEX :
\r
197 vTaskGetRunTimeStats( pcBuffer );
\r
201 /* Include a count of the number of times an SSI function has been executed
\r
202 in the returned string. */
\r
204 sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );
\r
205 strcat( pcBuffer, cUpdateString );
\r
207 return strlen( pcBuffer );
\r
209 /*-----------------------------------------------------------*/
\r
211 signed char *pcLwipAppsBlockingGetTxBuffer( void )
\r
213 signed char *pcReturn;
\r
215 /* Attempt to obtain the semaphore that guards the Tx buffer. */
\r
216 if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )
\r
218 /* The semaphore could not be obtained before timing out. */
\r
223 /* The semaphore was obtained successfully. Return a pointer to the
\r
225 pcReturn = cTxBuffer;
\r
230 /*-----------------------------------------------------------*/
\r
232 void vLwipAppsReleaseTxBuffer( void )
\r
234 /* Finished with the Tx buffer. Return the mutex. */
\r
235 xSemaphoreGiveRecursive( xTxBufferMutex );
\r