]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_IoT_SDK/task_pool/main.c
9a93fcd3d7c4b3b9a2995f3f3306751a1dd5c92a
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_IoT_SDK / task_pool / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\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.\r
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * This project is a cut down version of the project described on the following\r
30  * link.  Only the simple UDP client and server and the TCP echo clients are\r
31  * included in the build:\r
32  * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
33  */\r
34 \r
35 /* Standard includes. */\r
36 #include <stdio.h>\r
37 #include <time.h>\r
38 \r
39 /* FreeRTOS includes. */\r
40 #include <FreeRTOS.h>\r
41 #include "task.h"\r
42 \r
43 /* Demo application includes. */\r
44 #include "FreeRTOS_IP.h"\r
45 #include "FreeRTOS_Sockets.h"\r
46 #include "SimpleUDPClientAndServer.h"\r
47 #include "demo_logging.h"\r
48 \r
49 /* Simple UDP client and server task parameters. */\r
50 #define mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY              ( tskIDLE_PRIORITY )\r
51 #define mainSIMPLE_UDP_CLIENT_SERVER_PORT                               ( 5005UL )\r
52 \r
53 /* Define a name that will be used for LLMNR and NBNS searches. */\r
54 #define mainHOST_NAME                           "RTOSDemo"\r
55 #define mainDEVICE_NICK_NAME            "windows_demo"\r
56 \r
57 /* Set the following constants to 1 or 0 to define which tasks to include and\r
58 exclude:\r
59 \r
60 mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS:  When set to 1 two UDP client tasks\r
61 and two UDP server tasks are created.  The clients talk to the servers.  One set\r
62 of tasks use the standard sockets interface, and the other the zero copy sockets\r
63 interface.  These tasks are self checking and will trigger a configASSERT() if\r
64 they detect a difference in the data that is received from that which was sent.\r
65 As these tasks use UDP, and can therefore loose packets, they will cause\r
66 configASSERT() to be called when they are run in a less than perfect networking\r
67 environment.\r
68 \r
69 mainCREATE_TCP_ECHO_TASKS_SINGLE:  When set to 1 a set of tasks are created that\r
70 send TCP echo requests to the standard echo port (port 7), then wait for and\r
71 verify the echo reply, from within the same task (Tx and Rx are performed in the\r
72 same RTOS task).  The IP address of the echo server must be configured using the\r
73 configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in\r
74 FreeRTOSConfig.h.\r
75 \r
76 mainCREATE_TCP_ECHO_SERVER_TASK:  When set to 1 a task is created that accepts\r
77 connections on the standard echo port (port 7), then echos back any data\r
78 received on that connection.\r
79 */\r
80 #define mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS       1\r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /*\r
84  * Just seeds the simple pseudo random number generator.\r
85  */\r
86 static void prvSRand( UBaseType_t ulSeed );\r
87 \r
88 /*\r
89  * Miscellaneous initialisation including preparing the logging and seeding the\r
90  * random number generator.\r
91  */\r
92 static void prvMiscInitialisation( void );\r
93 \r
94 /* The default IP and MAC address used by the demo.  The address configuration\r
95 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
96 1 but a DHCP server could not be contacted.  See the online documentation for\r
97 more information. */\r
98 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
99 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
100 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
101 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
102 \r
103 /* Set the following constant to pdTRUE to log using the method indicated by the\r
104 name of the constant, or pdFALSE to not log using the method indicated by the\r
105 name of the constant.  Options include to standard out (xLogToStdout), to a disk\r
106 file (xLogToFile), and to a UDP port (xLogToUDP).  If xLogToUDP is set to pdTRUE\r
107 then UDP messages are sent to the IP address configured as the echo server\r
108 address (see the configECHO_SERVER_ADDR0 definitions in FreeRTOSConfig.h) and\r
109 the port number set by configPRINT_PORT in FreeRTOSConfig.h. */\r
110 const BaseType_t xLogToStdout = pdTRUE, xLogToFile = pdFALSE, xLogToUDP = pdFALSE;\r
111 \r
112 /* Default MAC address configuration.  The demo creates a virtual network\r
113 connection that uses this MAC address by accessing the raw Ethernet data\r
114 to and from a real network connection on the host PC.  See the\r
115 configNETWORK_INTERFACE_TO_USE definition for information on how to configure\r
116 the real network connection to use. */\r
117 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
118 \r
119 /* Use by the pseudo random number generator. */\r
120 static UBaseType_t ulNextRand;\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 int main( void )\r
125 {\r
126 const uint32_t ulLongTime_ms = pdMS_TO_TICKS( 1000UL );\r
127 \r
128         /*\r
129          * Instructions for using this project are provided on:\r
130          * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
131          */\r
132 \r
133         /* Miscellaneous initialisation including preparing the logging and seeding\r
134         the random number generator. */\r
135         prvMiscInitialisation();\r
136 \r
137         /* Initialise the network interface.\r
138 \r
139         ***NOTE*** Tasks that use the network are created in the network event hook\r
140         when the network is connected and ready for use (see the definition of\r
141         vApplicationIPNetworkEventHook() below).  The address values passed in here\r
142         are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1\r
143         but a DHCP server cannot be     contacted. */\r
144         FreeRTOS_debug_printf( ( "FreeRTOS_IPInit\n" ) );\r
145         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
146 \r
147         /* Start the RTOS scheduler. */\r
148         FreeRTOS_debug_printf( ("vTaskStartScheduler\n") );\r
149         vTaskStartScheduler();\r
150 \r
151         /* If all is well, the scheduler will now be running, and the following\r
152         line will never be reached.  If the following line does execute, then\r
153         there was insufficient FreeRTOS heap memory available for the idle and/or\r
154         timer tasks     to be created.  See the memory management section on the\r
155         FreeRTOS web site for more details (this is standard text that is not not\r
156         really applicable to the Win32 simulator port). */\r
157         for( ;; )\r
158         {\r
159                 Sleep( ulLongTime_ms );\r
160         }\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vApplicationIdleHook( void )\r
165 {\r
166 const uint32_t ulMSToSleep = 1;\r
167 \r
168         /* This is just a trivial example of an idle hook.  It is called on each\r
169         cycle of the idle task if configUSE_IDLE_HOOK is set to 1 in\r
170         FreeRTOSConfig.h.  It must *NOT* attempt to block.  In this case the\r
171         idle task just sleeps to lower the CPU usage. */\r
172         Sleep( ulMSToSleep );\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void vAssertCalled( const char *pcFile, uint32_t ulLine )\r
177 {\r
178 const uint32_t ulLongSleep = 1000UL;\r
179 volatile uint32_t ulBlockVariable = 0UL;\r
180 volatile char *pcFileName = ( volatile char *  ) pcFile;\r
181 volatile uint32_t ulLineNumber = ulLine;\r
182 \r
183         ( void ) pcFileName;\r
184         ( void ) ulLineNumber;\r
185 \r
186         FreeRTOS_debug_printf( ( "vAssertCalled( %s, %ld\n", pcFile, ulLine ) );\r
187 \r
188         /* Setting ulBlockVariable to a non-zero value in the debugger will allow\r
189         this function to be exited. */\r
190         taskDISABLE_INTERRUPTS();\r
191         {\r
192                 while( ulBlockVariable == 0UL )\r
193                 {\r
194                         Sleep( ulLongSleep );\r
195                 }\r
196         }\r
197         taskENABLE_INTERRUPTS();\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 /* Called by FreeRTOS+TCP when the network connects or disconnects.  Disconnect\r
202 events are only received if implemented in the MAC driver. */\r
203 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
204 {\r
205 uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;\r
206 char cBuffer[ 16 ];\r
207 static BaseType_t xTasksAlreadyCreated = pdFALSE;\r
208 \r
209         /* If the network has just come up...*/\r
210         if( eNetworkEvent == eNetworkUp )\r
211         {\r
212                 /* Create the tasks that use the IP stack if they have not already been\r
213                 created. */\r
214                 if( xTasksAlreadyCreated == pdFALSE )\r
215                 {\r
216                         /* See the comments above the definitions of these pre-processor\r
217                         macros at the top of this file for a description of the individual\r
218                         demo tasks. */\r
219                         #if( mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS == 1 )\r
220                         {\r
221                                 vStartSimpleUDPClientServerTasks( configMINIMAL_STACK_SIZE, mainSIMPLE_UDP_CLIENT_SERVER_PORT, mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY );\r
222                         }\r
223                         #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */\r
224 \r
225                         #if( mainCREATE_TCP_ECHO_TASKS_SINGLE == 1 )\r
226                         {\r
227                                 vStartTCPEchoClientTasks_SingleTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
228                         }\r
229                         #endif /* mainCREATE_TCP_ECHO_TASKS_SINGLE */\r
230 \r
231                         #if( mainCREATE_TCP_ECHO_SERVER_TASK == 1 )\r
232                         {\r
233                                 vStartSimpleTCPServerTasks( mainECHO_SERVER_TASK_STACK_SIZE, mainECHO_SERVER_TASK_PRIORITY );\r
234                         }\r
235                         #endif\r
236 \r
237                         xTasksAlreadyCreated = pdTRUE;\r
238                 }\r
239 \r
240                 /* Print out the network configuration, which may have come from a DHCP\r
241                 server. */\r
242                 FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress );\r
243                 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );\r
244                 FreeRTOS_printf( ( "\r\n\r\nIP Address: %s\r\n", cBuffer ) );\r
245 \r
246                 FreeRTOS_inet_ntoa( ulNetMask, cBuffer );\r
247                 FreeRTOS_printf( ( "Subnet Mask: %s\r\n", cBuffer ) );\r
248 \r
249                 FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );\r
250                 FreeRTOS_printf( ( "Gateway Address: %s\r\n", cBuffer ) );\r
251 \r
252                 FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );\r
253                 FreeRTOS_printf( ( "DNS Server Address: %s\r\n\r\n\r\n", cBuffer ) );\r
254         }\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 void vApplicationMallocFailedHook( void )\r
259 {\r
260         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
261         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
262         internally by FreeRTOS API functions that create tasks, queues, software\r
263         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
264         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
265         vAssertCalled( __FILE__, __LINE__ );\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 UBaseType_t uxRand( void )\r
270 {\r
271 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;\r
272 \r
273         /* Utility function to generate a pseudo random number. */\r
274 \r
275         ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
276         return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void prvSRand( UBaseType_t ulSeed )\r
281 {\r
282         /* Utility function to seed the pseudo random number generator. */\r
283     ulNextRand = ulSeed;\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 static void prvMiscInitialisation( void )\r
288 {\r
289 time_t xTimeNow;\r
290 uint32_t ulLoggingIPAddress;\r
291 \r
292         ulLoggingIPAddress = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0, configECHO_SERVER_ADDR1, configECHO_SERVER_ADDR2, configECHO_SERVER_ADDR3 );\r
293         vLoggingInit( xLogToStdout, xLogToFile, xLogToUDP, ulLoggingIPAddress, configPRINT_PORT );\r
294 \r
295         /* Seed the random number generator. */\r
296         time( &xTimeNow );\r
297         FreeRTOS_debug_printf( ( "Seed for randomiser: %lu\n", xTimeNow ) );\r
298         prvSRand( ( uint32_t ) xTimeNow );\r
299         FreeRTOS_debug_printf( ( "Random numbers: %08X %08X %08X %08X\n", ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32() ) );\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME == 1 )\r
304 \r
305         const char *pcApplicationHostnameHook( void )\r
306         {\r
307                 /* Assign the name "FreeRTOS" to this network node.  This function will\r
308                 be called during the DHCP: the machine will be registered with an IP\r
309                 address plus this name. */\r
310                 return mainHOST_NAME;\r
311         }\r
312 \r
313 #endif\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )\r
317 \r
318         BaseType_t xApplicationDNSQueryHook( const char *pcName )\r
319         {\r
320         BaseType_t xReturn;\r
321 \r
322                 /* Determine if a name lookup is for this node.  Two names are given\r
323                 to this node: that returned by pcApplicationHostnameHook() and that set\r
324                 by mainDEVICE_NICK_NAME. */\r
325                 if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )\r
326                 {\r
327                         xReturn = pdPASS;\r
328                 }\r
329                 else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )\r
330                 {\r
331                         xReturn = pdPASS;\r
332                 }\r
333                 else\r
334                 {\r
335                         xReturn = pdFAIL;\r
336                 }\r
337 \r
338                 return xReturn;\r
339         }\r
340 \r
341 #endif\r
342 /*-----------------------------------------------------------*/\r
343 \r
344 /*\r
345  * Callback that provides the inputs necessary to generate a randomized TCP\r
346  * Initial Sequence Number per RFC 6528.  THIS IS ONLY A DUMMY IMPLEMENTATION\r
347  * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION\r
348  * SYSTEMS.\r
349  */\r
350 extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,\r
351                                                                                                         uint16_t usSourcePort,\r
352                                                                                                         uint32_t ulDestinationAddress,\r
353                                                                                                         uint16_t usDestinationPort )\r
354 {\r
355         ( void ) ulSourceAddress;\r
356         ( void ) usSourcePort;\r
357         ( void ) ulDestinationAddress;\r
358         ( void ) usDestinationPort;\r
359 \r
360         return uxRand();\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
365 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
366 used by the Idle task. */\r
367 void vApplicationGetIdleTaskMemory( StaticTask_t** ppxIdleTaskTCBBuffer, StackType_t** ppxIdleTaskStackBuffer, uint32_t* pulIdleTaskStackSize )\r
368 {\r
369         /* If the buffers to be provided to the Idle task are declared inside this\r
370         function then they must be declared static - otherwise they will be allocated on\r
371         the stack and so not exists after this function exits. */\r
372         static StaticTask_t xIdleTaskTCB;\r
373         static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE];\r
374 \r
375         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
376         state will be stored. */\r
377         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
378 \r
379         /* Pass out the array that will be used as the Idle task's stack. */\r
380         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
381 \r
382         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
383         Note that, as the array is necessarily of type StackType_t,\r
384         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
385         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
390 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
391 to provide the memory that is used by the Timer service task. */\r
392 void vApplicationGetTimerTaskMemory( StaticTask_t** ppxTimerTaskTCBBuffer, StackType_t** ppxTimerTaskStackBuffer, uint32_t* pulTimerTaskStackSize )\r
393 {\r
394         /* If the buffers to be provided to the Timer task are declared inside this\r
395         function then they must be declared static - otherwise they will be allocated on\r
396         the stack and so not exists after this function exits. */\r
397         static StaticTask_t xTimerTaskTCB;\r
398         static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH];\r
399 \r
400         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
401         task's state will be stored. */\r
402         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
403 \r
404         /* Pass out the array that will be used as the Timer task's stack. */\r
405         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
406 \r
407         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
408         Note that, as the array is necessarily of type StackType_t,\r
409         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
410         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
411 }\r
412 \r
413 \r