]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/lwIP_Demo/lwIP_Apps/lwIP_Apps.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / lwIP_Demo / lwIP_Apps / lwIP_Apps.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include <string.h>\r
31 \r
32 /* FreeRTOS includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 #include "semphr.h"\r
36 \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
42 \r
43 /* applications includes */\r
44 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"\r
45 \r
46 /* include the port-dependent configuration */\r
47 #include "lwipcfg_msvc.h"\r
48 \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
53 \r
54 /* The maximum time to block waiting to obtain the xTxBufferMutex to become\r
55 available. */\r
56 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS       ( 100 / portTICK_RATE_MS )\r
57 \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
62 \r
63 /*-----------------------------------------------------------*/\r
64 \r
65 /*\r
66  * The function that implements the lwIP based sockets command interpreter\r
67  * server.\r
68  */\r
69 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );\r
70 \r
71 /*\r
72  * The SSI handler callback function passed to lwIP.\r
73  */\r
74 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \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
82 {\r
83         "rtos_stats",\r
84         "run_stats"\r
85 };\r
86 \r
87 /* Semaphore used to guard the Tx buffer. */\r
88 static xSemaphoreHandle xTxBufferMutex = NULL;\r
89 \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
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 void vStatusCallback( struct netif *pxNetIf )\r
102 {\r
103 char pcMessage[20];\r
104 struct in_addr* pxIPAddress;\r
105 \r
106         if( netif_is_up( pxNetIf ) != 0 )\r
107         {\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
112         }\r
113         else\r
114         {\r
115                 xil_printf( "Network is down" );\r
116         }\r
117 }\r
118 \r
119 /* Called from the TCP/IP thread. */\r
120 void lwIPAppsInit( void *pvArgument )\r
121 {\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
126 \r
127         ( void ) pvArgument;\r
128 \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
133 \r
134         LWIP_PORT_INIT_GW(&xGateway);\r
135         LWIP_PORT_INIT_IPADDR( &xIPAddr );\r
136         LWIP_PORT_INIT_NETMASK(&xNetMask);\r
137 \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
146 \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
149         #if LWIP_DHCP\r
150         {\r
151                 dhcp_start( &xNetIf );\r
152         }\r
153         #else\r
154         {\r
155                 netif_set_up( &xNetIf );\r
156         }\r
157         #endif\r
158 \r
159         /* Install the server side include handler. */\r
160         http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );\r
161 \r
162         /* Create the mutex used to ensure mutual exclusive access to the Tx \r
163         buffer. */\r
164         xTxBufferMutex = xSemaphoreCreateMutex();\r
165         configASSERT( xTxBufferMutex );\r
166 \r
167         /* Create the httpd server from the standard lwIP code.  This demonstrates\r
168         use of the lwIP raw API. */\r
169         httpd_init();\r
170 \r
171         sys_thread_new( "lwIP_In", xemacif_input_thread, &xNetIf, configMINIMAL_STACK_SIZE, configMAC_INPUT_TASK_PRIORITY );\r
172 \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
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )\r
180 {\r
181 static unsigned int uiUpdateCount = 0;\r
182 static char cUpdateString[ 200 ];\r
183 extern char *pcMainGetTaskStatusMessage( void );\r
184 \r
185         /* Unused parameter. */\r
186         ( void ) iBufferLength;\r
187 \r
188         /* The SSI handler function that generates text depending on the index of\r
189         the SSI tag encountered. */\r
190         \r
191         switch( iIndex )\r
192         {\r
193                 case ssiTASK_STATS_INDEX :\r
194                         vTaskList( pcBuffer );\r
195                         break;\r
196 \r
197                 case ssiRUN_TIME_STATS_INDEX :\r
198                         vTaskGetRunTimeStats( pcBuffer );\r
199                         break;\r
200         }\r
201 \r
202         /* Include a count of the number of times an SSI function has been executed\r
203         in the returned string. */\r
204         uiUpdateCount++;\r
205         sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );\r
206         strcat( pcBuffer, cUpdateString );\r
207 \r
208         return strlen( pcBuffer );\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 signed char *pcLwipAppsBlockingGetTxBuffer( void )\r
213 {\r
214 signed char *pcReturn;\r
215 \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
218         {\r
219                 /* The semaphore could not be obtained before timing out. */\r
220                 pcReturn = NULL;\r
221         }\r
222         else\r
223         {\r
224                 /* The semaphore was obtained successfully.  Return a pointer to the\r
225                 Tx buffer. */\r
226                 pcReturn = cTxBuffer;\r
227         }\r
228 \r
229         return pcReturn;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 void vLwipAppsReleaseTxBuffer( void )\r
234 {\r
235         /* Finished with the Tx buffer.  Return the mutex. */\r
236         xSemaphoreGiveRecursive( xTxBufferMutex );\r
237 }\r
238 \r
239 \r
240 \r