]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC-lwIP/lwIP_Apps/lwIP_Apps.c
Start to remove unnecessary 'signed char *' casts from strings that are now just...
[freertos] / FreeRTOS / Demo / WIN32-MSVC-lwIP / lwIP_Apps / lwIP_Apps.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "semphr.h"\r
78 \r
79 /* lwIP core includes */\r
80 #include "lwip/opt.h"\r
81 #include "lwip/tcpip.h"\r
82 \r
83 /* applications includes */\r
84 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"\r
85 \r
86 /* include the port-dependent configuration */\r
87 #include "lwipcfg_msvc.h"\r
88 \r
89 /* Dimensions the cTxBuffer array - which is itself used to hold replies from\r
90 command line commands.  cTxBuffer is a shared buffer, so protected by the\r
91 xTxBufferMutex mutex. */\r
92 #define lwipappsTX_BUFFER_SIZE  1024\r
93 \r
94 /* The maximum time to block waiting to obtain the xTxBufferMutex to become\r
95 available. */\r
96 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS       ( 100 / portTICK_RATE_MS )\r
97 \r
98 /* Definitions of the various SSI callback functions within the pccSSITags\r
99 array.  If pccSSITags is updated, then these definitions must also be updated. */\r
100 #define ssiTASK_STATS_INDEX                     0\r
101 #define ssiRUN_TIME_STATS_INDEX         1\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /*\r
106  * The function that implements the lwIP based sockets command interpreter\r
107  * server.\r
108  */\r
109 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );\r
110 \r
111 /*\r
112  * The SSI handler callback function passed to lwIP.\r
113  */\r
114 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /* The SSI strings that are embedded in the served html files.  If this array\r
119 is changed, then the index position defined by the #defines such as\r
120 ssiTASK_STATS_INDEX above must also be updated. */\r
121 static const char *pccSSITags[] =\r
122 {\r
123         "rtos_stats",\r
124         "run_stats"\r
125 };\r
126 \r
127 /* Semaphore used to guard the Tx buffer. */\r
128 static xSemaphoreHandle xTxBufferMutex = NULL;\r
129 \r
130 /* The Tx buffer itself.  This is used to hold the text generated by the\r
131 execution of command line commands, and (hopefully) the execution of\r
132 server side include callbacks.  It is a shared buffer so protected by the\r
133 xTxBufferMutex mutex.  pcLwipAppsBlockingGetTxBuffer() and\r
134 vLwipAppsReleaseTxBuffer() are provided to obtain and release the\r
135 xTxBufferMutex respectively.  pcLwipAppsBlockingGetTxBuffer() must be used with\r
136 caution as it has the potential to block. */\r
137 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 \r
142 /* Called from the TCP/IP thread. */\r
143 void lwIPAppsInit( void *pvArgument )\r
144 {\r
145 ip_addr_t xIPAddr, xNetMask, xGateway;\r
146 extern err_t ethernetif_init( struct netif *xNetIf );\r
147 static struct netif xNetIf;\r
148 \r
149         ( void ) pvArgument;\r
150 \r
151         /* Set up the network interface. */\r
152         ip_addr_set_zero( &xGateway );\r
153         ip_addr_set_zero( &xIPAddr );\r
154         ip_addr_set_zero( &xNetMask );\r
155 \r
156         LWIP_PORT_INIT_GW(&xGateway);\r
157         LWIP_PORT_INIT_IPADDR(&xIPAddr);\r
158         LWIP_PORT_INIT_NETMASK(&xNetMask);\r
159         printf("Starting lwIP, local interface IP is %s\n", ip_ntoa(&xIPAddr));\r
160 \r
161         netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, NULL, ethernetif_init, tcpip_input ) );\r
162         netif_set_up( &xNetIf );\r
163 \r
164         /* Install the server side include handler. */\r
165         http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );\r
166 \r
167         /* Create the mutex used to ensure mutual exclusive access to the Tx\r
168         buffer. */\r
169         xTxBufferMutex = xSemaphoreCreateMutex();\r
170         configASSERT( xTxBufferMutex );\r
171 \r
172         /* Create the httpd server from the standard lwIP code.  This demonstrates\r
173         use of the lwIP raw API. */\r
174         httpd_init();\r
175 \r
176         /* Create the FreeRTOS defined basic command server.  This demonstrates use\r
177         of the lwIP sockets API. */\r
178         xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 10, NULL, configMAX_PRIORITIES - 2, NULL );\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )\r
183 {\r
184 static unsigned int uiUpdateCount = 0;\r
185 static char cUpdateString[ 200 ];\r
186 extern char *pcMainGetTaskStatusMessage( void );\r
187 \r
188         /* Unused parameter. */\r
189         ( void ) iBufferLength;\r
190 \r
191         /* The SSI handler function that generates text depending on the index of\r
192         the SSI tag encountered. */\r
193 \r
194         switch( iIndex )\r
195         {\r
196                 case ssiTASK_STATS_INDEX :\r
197                         vTaskList( pcBuffer );\r
198                         break;\r
199 \r
200                 case ssiRUN_TIME_STATS_INDEX :\r
201                         vTaskGetRunTimeStats( pcBuffer );\r
202                         break;\r
203         }\r
204 \r
205         /* Include a count of the number of times an SSI function has been executed\r
206         in the returned string. */\r
207         uiUpdateCount++;\r
208         sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );\r
209         strcat( pcBuffer, cUpdateString );\r
210 \r
211         return strlen( pcBuffer );\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 signed char *pcLwipAppsBlockingGetTxBuffer( void )\r
216 {\r
217 signed char *pcReturn;\r
218 \r
219         /* Attempt to obtain the semaphore that guards the Tx buffer. */\r
220         if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )\r
221         {\r
222                 /* The semaphore could not be obtained before timing out. */\r
223                 pcReturn = NULL;\r
224         }\r
225         else\r
226         {\r
227                 /* The semaphore was obtained successfully.  Return a pointer to the\r
228                 Tx buffer. */\r
229                 pcReturn = cTxBuffer;\r
230         }\r
231 \r
232         return pcReturn;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vLwipAppsReleaseTxBuffer( void )\r
237 {\r
238         /* Finished with the Tx buffer.  Return the mutex. */\r
239         xSemaphoreGiveRecursive( xTxBufferMutex );\r
240 }\r
241 \r
242 \r
243 \r