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
29 /* Standard includes. */
\r
32 /* FreeRTOS includes. */
\r
33 #include "FreeRTOS.h"
\r
37 /* lwIP core includes */
\r
38 #include "lwip/opt.h"
\r
39 #include "lwip/tcpip.h"
\r
40 #include "lwip/inet.h"
\r
41 #include "lwip/dhcp.h"
\r
43 /* applications includes */
\r
44 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"
\r
46 /* include the port-dependent configuration */
\r
47 #include "lwipcfg_msvc.h"
\r
49 /* Dimensions the cTxBuffer array - which is itself used to hold replies from
\r
50 command line commands. cTxBuffer is a shared buffer, so protected by the
\r
51 xTxBufferMutex mutex. */
\r
52 #define lwipappsTX_BUFFER_SIZE 1024
\r
54 /* The maximum time to block waiting to obtain the xTxBufferMutex to become
\r
56 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ( 100 / portTICK_RATE_MS )
\r
58 /* Definitions of the various SSI callback functions within the pccSSITags
\r
59 array. If pccSSITags is updated, then these definitions must also be updated. */
\r
60 #define ssiTASK_STATS_INDEX 0
\r
61 #define ssiRUN_TIME_STATS_INDEX 1
\r
63 /*-----------------------------------------------------------*/
\r
66 * The function that implements the lwIP based sockets command interpreter
\r
69 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );
\r
72 * The SSI handler callback function passed to lwIP.
\r
74 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );
\r
76 /*-----------------------------------------------------------*/
\r
78 /* The SSI strings that are embedded in the served html files. If this array
\r
79 is changed, then the index position defined by the #defines such as
\r
80 ssiTASK_STATS_INDEX above must also be updated. */
\r
81 static const char *pccSSITags[] =
\r
87 /* Semaphore used to guard the Tx buffer. */
\r
88 static xSemaphoreHandle xTxBufferMutex = NULL;
\r
90 /* The Tx buffer itself. This is used to hold the text generated by the
\r
91 execution of command line commands, and (hopefully) the execution of
\r
92 server side include callbacks. It is a shared buffer so protected by the
\r
93 xTxBufferMutex mutex. pcLwipAppsBlockingGetTxBuffer() and
\r
94 vLwipAppsReleaseTxBuffer() are provided to obtain and release the
\r
95 xTxBufferMutex respectively. pcLwipAppsBlockingGetTxBuffer() must be used with
\r
96 caution as it has the potential to block. */
\r
97 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];
\r
99 /*-----------------------------------------------------------*/
\r
101 void vStatusCallback( struct netif *pxNetIf )
\r
103 char pcMessage[20];
\r
104 struct in_addr* pxIPAddress;
\r
106 if( netif_is_up( pxNetIf ) != 0 )
\r
108 strcpy( pcMessage, "IP=" );
\r
109 pxIPAddress = ( struct in_addr* ) &( pxNetIf->ip_addr );
\r
110 strcat( pcMessage, inet_ntoa( ( *pxIPAddress ) ) );
\r
111 xil_printf( pcMessage );
\r
115 xil_printf( "Network is down" );
\r
119 /* Called from the TCP/IP thread. */
\r
120 void lwIPAppsInit( void *pvArgument )
\r
122 ip_addr_t xIPAddr, xNetMask, xGateway;
\r
123 extern err_t xemacpsif_init( struct netif *netif );
\r
124 extern void xemacif_input_thread( void *netif );
\r
125 static struct netif xNetIf;
\r
127 ( void ) pvArgument;
\r
129 /* Set up the network interface. */
\r
130 ip_addr_set_zero( &xGateway );
\r
131 ip_addr_set_zero( &xIPAddr );
\r
132 ip_addr_set_zero( &xNetMask );
\r
134 LWIP_PORT_INIT_GW(&xGateway);
\r
135 LWIP_PORT_INIT_IPADDR( &xIPAddr );
\r
136 LWIP_PORT_INIT_NETMASK(&xNetMask);
\r
138 /* Set mac address */
\r
139 xNetIf.hwaddr_len = 6;
\r
140 xNetIf.hwaddr[ 0 ] = configMAC_ADDR0;
\r
141 xNetIf.hwaddr[ 1 ] = configMAC_ADDR1;
\r
142 xNetIf.hwaddr[ 2 ] = configMAC_ADDR2;
\r
143 xNetIf.hwaddr[ 3 ] = configMAC_ADDR3;
\r
144 xNetIf.hwaddr[ 4 ] = configMAC_ADDR4;
\r
145 xNetIf.hwaddr[ 5 ] = configMAC_ADDR5;
\r
147 netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, ( void * ) XPAR_XEMACPS_0_BASEADDR, xemacpsif_init, tcpip_input ) );
\r
148 netif_set_status_callback( &xNetIf, vStatusCallback );
\r
151 dhcp_start( &xNetIf );
\r
155 netif_set_up( &xNetIf );
\r
159 /* Install the server side include handler. */
\r
160 http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );
\r
162 /* Create the mutex used to ensure mutual exclusive access to the Tx
\r
164 xTxBufferMutex = xSemaphoreCreateMutex();
\r
165 configASSERT( xTxBufferMutex );
\r
167 /* Create the httpd server from the standard lwIP code. This demonstrates
\r
168 use of the lwIP raw API. */
\r
171 sys_thread_new( "lwIP_In", xemacif_input_thread, &xNetIf, configMINIMAL_STACK_SIZE, configMAC_INPUT_TASK_PRIORITY );
\r
173 /* Create the FreeRTOS defined basic command server. This demonstrates use
\r
174 of the lwIP sockets API. */
\r
175 xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 5, NULL, configCLI_TASK_PRIORITY, NULL );
\r
177 /*-----------------------------------------------------------*/
\r
179 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )
\r
181 static unsigned int uiUpdateCount = 0;
\r
182 static char cUpdateString[ 200 ];
\r
183 extern char *pcMainGetTaskStatusMessage( void );
\r
185 /* Unused parameter. */
\r
186 ( void ) iBufferLength;
\r
188 /* The SSI handler function that generates text depending on the index of
\r
189 the SSI tag encountered. */
\r
193 case ssiTASK_STATS_INDEX :
\r
194 vTaskList( pcBuffer );
\r
197 case ssiRUN_TIME_STATS_INDEX :
\r
198 vTaskGetRunTimeStats( pcBuffer );
\r
202 /* Include a count of the number of times an SSI function has been executed
\r
203 in the returned string. */
\r
205 sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );
\r
206 strcat( pcBuffer, cUpdateString );
\r
208 return strlen( pcBuffer );
\r
210 /*-----------------------------------------------------------*/
\r
212 signed char *pcLwipAppsBlockingGetTxBuffer( void )
\r
214 signed char *pcReturn;
\r
216 /* Attempt to obtain the semaphore that guards the Tx buffer. */
\r
217 if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )
\r
219 /* The semaphore could not be obtained before timing out. */
\r
224 /* The semaphore was obtained successfully. Return a pointer to the
\r
226 pcReturn = cTxBuffer;
\r
231 /*-----------------------------------------------------------*/
\r
233 void vLwipAppsReleaseTxBuffer( void )
\r
235 /* Finished with the Tx buffer. Return the mutex. */
\r
236 xSemaphoreGiveRecursive( xTxBufferMutex );
\r