2 * FreeRTOS Kernel V10.0.1
\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.
\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 #include "FreeRTOS.h"
\r
32 /* lwIP core includes */
\r
33 #include "lwip/opt.h"
\r
34 #include "lwip/tcpip.h"
\r
36 /* applications includes */
\r
37 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"
\r
39 /* include the port-dependent configuration */
\r
40 #include "lwipcfg_msvc.h"
\r
42 /* Dimensions the cTxBuffer array - which is itself used to hold replies from
\r
43 command line commands. cTxBuffer is a shared buffer, so protected by the
\r
44 xTxBufferMutex mutex. */
\r
45 #define lwipappsTX_BUFFER_SIZE 1024
\r
47 /* The maximum time to block waiting to obtain the xTxBufferMutex to become
\r
49 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ( 100 / portTICK_PERIOD_MS )
\r
51 /* Definitions of the various SSI callback functions within the pccSSITags
\r
52 array. If pccSSITags is updated, then these definitions must also be updated. */
\r
53 #define ssiTASK_STATS_INDEX 0
\r
54 #define ssiRUN_TIME_STATS_INDEX 1
\r
56 /*-----------------------------------------------------------*/
\r
59 * The function that implements the lwIP based sockets command interpreter
\r
62 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );
\r
65 * The SSI handler callback function passed to lwIP.
\r
67 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );
\r
69 /*-----------------------------------------------------------*/
\r
71 /* The SSI strings that are embedded in the served html files. If this array
\r
72 is changed, then the index position defined by the #defines such as
\r
73 ssiTASK_STATS_INDEX above must also be updated. */
\r
74 static const char *pccSSITags[] =
\r
80 /* Semaphore used to guard the Tx buffer. */
\r
81 static SemaphoreHandle_t xTxBufferMutex = NULL;
\r
83 /* The Tx buffer itself. This is used to hold the text generated by the
\r
84 execution of command line commands, and (hopefully) the execution of
\r
85 server side include callbacks. It is a shared buffer so protected by the
\r
86 xTxBufferMutex mutex. pcLwipAppsBlockingGetTxBuffer() and
\r
87 vLwipAppsReleaseTxBuffer() are provided to obtain and release the
\r
88 xTxBufferMutex respectively. pcLwipAppsBlockingGetTxBuffer() must be used with
\r
89 caution as it has the potential to block. */
\r
90 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];
\r
92 /*-----------------------------------------------------------*/
\r
95 /* Called from the TCP/IP thread. */
\r
96 void lwIPAppsInit( void *pvArgument )
\r
98 ip_addr_t xIPAddr, xNetMask, xGateway;
\r
99 extern err_t ethernetif_init( struct netif *xNetIf );
\r
100 static struct netif xNetIf;
\r
102 ( void ) pvArgument;
\r
104 /* Set up the network interface. */
\r
105 ip_addr_set_zero( &xGateway );
\r
106 ip_addr_set_zero( &xIPAddr );
\r
107 ip_addr_set_zero( &xNetMask );
\r
109 LWIP_PORT_INIT_GW(&xGateway);
\r
110 LWIP_PORT_INIT_IPADDR(&xIPAddr);
\r
111 LWIP_PORT_INIT_NETMASK(&xNetMask);
\r
112 printf("Starting lwIP, local interface IP is %s\n", ip_ntoa(&xIPAddr));
\r
114 netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, NULL, ethernetif_init, tcpip_input ) );
\r
115 netif_set_up( &xNetIf );
\r
117 /* Install the server side include handler. */
\r
118 http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );
\r
120 /* Create the mutex used to ensure mutual exclusive access to the Tx
\r
122 xTxBufferMutex = xSemaphoreCreateMutex();
\r
123 configASSERT( xTxBufferMutex );
\r
125 /* Create the httpd server from the standard lwIP code. This demonstrates
\r
126 use of the lwIP raw API. */
\r
129 /* Create the FreeRTOS defined basic command server. This demonstrates use
\r
130 of the lwIP sockets API. */
\r
131 xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 10, NULL, configMAX_PRIORITIES - 2, NULL );
\r
133 /*-----------------------------------------------------------*/
\r
135 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )
\r
137 static unsigned int uiUpdateCount = 0;
\r
138 static char cUpdateString[ 200 ];
\r
139 extern char *pcMainGetTaskStatusMessage( void );
\r
141 /* Unused parameter. */
\r
142 ( void ) iBufferLength;
\r
144 /* The SSI handler function that generates text depending on the index of
\r
145 the SSI tag encountered. */
\r
149 case ssiTASK_STATS_INDEX :
\r
150 vTaskList( pcBuffer );
\r
153 case ssiRUN_TIME_STATS_INDEX :
\r
154 vTaskGetRunTimeStats( pcBuffer );
\r
158 /* Include a count of the number of times an SSI function has been executed
\r
159 in the returned string. */
\r
161 sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );
\r
162 strcat( pcBuffer, cUpdateString );
\r
164 return strlen( pcBuffer );
\r
166 /*-----------------------------------------------------------*/
\r
168 signed char *pcLwipAppsBlockingGetTxBuffer( void )
\r
170 signed char *pcReturn;
\r
172 /* Attempt to obtain the semaphore that guards the Tx buffer. */
\r
173 if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )
\r
175 /* The semaphore could not be obtained before timing out. */
\r
180 /* The semaphore was obtained successfully. Return a pointer to the
\r
182 pcReturn = cTxBuffer;
\r
187 /*-----------------------------------------------------------*/
\r
189 void vLwipAppsReleaseTxBuffer( void )
\r
191 /* Finished with the Tx buffer. Return the mutex. */
\r
192 xSemaphoreGiveRecursive( xTxBufferMutex );
\r