]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Demo/FreeRTOS_IoT_Libraries/task_pool/DemoTasks/SimpleUDPClientAndServer.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_IoT_Libraries / task_pool / DemoTasks / SimpleUDPClientAndServer.c
diff --git a/FreeRTOS-Plus/Demo/FreeRTOS_IoT_Libraries/task_pool/DemoTasks/SimpleUDPClientAndServer.c b/FreeRTOS-Plus/Demo/FreeRTOS_IoT_Libraries/task_pool/DemoTasks/SimpleUDPClientAndServer.c
deleted file mode 100644 (file)
index 54143c3..0000000
+++ /dev/null
@@ -1,355 +0,0 @@
-/*\r
- * FreeRTOS Kernel V10.2.1\r
- * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
- * this software and associated documentation files (the "Software"), to deal in\r
- * the Software without restriction, including without limitation the rights to\r
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
- * the Software, and to permit persons to whom the Software is furnished to do so,\r
- * subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in all\r
- * copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- *\r
- * http://www.FreeRTOS.org\r
- * http://aws.amazon.com/freertos\r
- *\r
- * 1 tab == 4 spaces!\r
- */\r
-\r
-/*\r
- * Creates two transmitting tasks and two receiving tasks.  The transmitting\r
- * tasks send values that are received by the receiving tasks.  One set of tasks\r
- * uses the standard API.  The other set of tasks uses the zero copy API.\r
- *\r
- * See the following web page for essential demo usage and configuration\r
- * details:\r
- * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
- */\r
-\r
-/* Standard includes. */\r
-#include <stdint.h>\r
-#include <stdio.h>\r
-\r
-/* FreeRTOS includes. */\r
-#include "FreeRTOS.h"\r
-#include "task.h"\r
-\r
-/* FreeRTOS+TCP includes. */\r
-#include "FreeRTOS_IP.h"\r
-#include "FreeRTOS_Sockets.h"\r
-\r
-#define simpTINY_DELAY ( ( TickType_t ) 2 )\r
-\r
-/*\r
- * Uses a socket to send data without using the zero copy option.\r
- * prvSimpleServerTask() will receive the data.\r
- */\r
-static void prvSimpleClientTask( void *pvParameters );\r
-\r
-/*\r
- * Uses a socket to receive the data sent by the prvSimpleClientTask() task.\r
- * Does not use the zero copy option.\r
- */\r
-static void prvSimpleServerTask( void *pvParameters );\r
-\r
-/*\r
- * Uses a socket to send data using the zero copy option.\r
- * prvSimpleZeroCopyServerTask() will receive the data.\r
- */\r
-static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );\r
-\r
-/*\r
- * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()\r
- * task.  Uses the zero copy option.\r
- */\r
-static void prvSimpleZeroCopyServerTask( void *pvParameters );\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )\r
-{\r
-       /* Create the client and server tasks that do not use the zero copy\r
-       interface. */\r
-       xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
-       xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );\r
-\r
-       /* Create the client and server tasks that do use the zero copy interface. */\r
-       xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );\r
-       xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvSimpleClientTask( void *pvParameters )\r
-{\r
-Socket_t xClientSocket;\r
-struct freertos_sockaddr xDestinationAddress;\r
-uint8_t cString[ 65 ];\r
-BaseType_t lReturned;\r
-uint32_t ulCount = 0UL, ulIPAddress;\r
-const uint32_t ulLoopsPerSocket = 10UL;\r
-const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;\r
-\r
-       /* Remove compiler warning about unused parameters. */\r
-       ( void ) pvParameters;\r
-\r
-       /* It is assumed that this task is not created until the network is up,\r
-       so the IP address can be obtained immediately.  store the IP address being\r
-       used in ulIPAddress.  This is done so the socket can send to a different\r
-       port on the same IP address. */\r
-       FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
-\r
-       /* This test sends to itself, so data sent from here is received by a server\r
-       socket on the same IP address.  Setup the freertos_sockaddr structure with\r
-       this nodes IP address, and the port number being sent to.  The strange\r
-       casting is to try and remove compiler warnings on 32 bit machines. */\r
-       xDestinationAddress.sin_addr = ulIPAddress;\r
-       xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
-       xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
-\r
-       for( ;; )\r
-       {\r
-               /* Create the socket. */\r
-               xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
-               configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );\r
-\r
-               /* The count is used to differentiate between different messages sent to\r
-               the server, and to break out of the do while loop below. */\r
-               ulCount = 0UL;\r
-\r
-               do\r
-               {\r
-                       /* Create the string that is sent to the server. */\r
-                       sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );\r
-\r
-                       /* Send the string to the socket.  ulFlags is set to 0, so the zero\r
-                       copy option is not selected.  That means the data from cString[] is\r
-                       copied into a network buffer inside FreeRTOS_sendto(), and cString[]\r
-                       can be reused as soon as FreeRTOS_sendto() has returned. */\r
-                       lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );\r
-\r
-                       ulCount++;\r
-\r
-               } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );\r
-\r
-               FreeRTOS_closesocket( xClientSocket );\r
-\r
-               /* A short delay to prevent the messages printed by the server task\r
-               scrolling off the screen too quickly, and to prevent reduce the network\r
-               loading. */\r
-               vTaskDelay( x150ms );\r
-       }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvSimpleServerTask( void *pvParameters )\r
-{\r
-int32_t lBytes;\r
-uint8_t cReceivedString[ 60 ];\r
-struct freertos_sockaddr xClient, xBindAddress;\r
-uint32_t xClientLength = sizeof( xClient );\r
-Socket_t xListeningSocket;\r
-\r
-       /* Just to prevent compiler warnings. */\r
-       ( void ) pvParameters;\r
-\r
-       /* Attempt to open the socket. */\r
-       xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
-       configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );\r
-\r
-       /* This test receives data sent from a different port on the same IP\r
-       address.  Configure the freertos_sockaddr structure with the address being\r
-       bound to.  The strange casting is to try and remove     compiler warnings on 32\r
-       bit machines.  Note that this task is only created after the network is up,\r
-       so the IP address is valid here. */\r
-       xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
-       xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );\r
-\r
-       /* Bind the socket to the port that the client task will send to. */\r
-       FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );\r
-\r
-       for( ;; )\r
-       {\r
-               /* Zero out the receive array so there is NULL at the end of the string\r
-               when it is printed out. */\r
-               memset( cReceivedString, 0x00, sizeof( cReceivedString ) );\r
-\r
-               /* Receive data on the socket.  ulFlags is zero, so the zero copy option\r
-               is not set and the received data is copied into the buffer pointed to by\r
-               cReceivedString.  By default the block time is portMAX_DELAY.\r
-               xClientLength is not actually used by FreeRTOS_recvfrom(), but is set\r
-               appropriately in case future versions do use it. */\r
-               lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );\r
-\r
-               /* Error check. */\r
-               configASSERT( lBytes == ( BaseType_t ) strlen( ( const char * ) cReceivedString ) );\r
-       }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )\r
-{\r
-Socket_t xClientSocket;\r
-uint8_t *pucUDPPayloadBuffer;\r
-struct freertos_sockaddr xDestinationAddress;\r
-BaseType_t lReturned;\r
-uint32_t ulCount = 0UL, ulIPAddress;\r
-const uint32_t ulLoopsPerSocket = 10UL;\r
-const char *pcStringToSend = "Server received (using zero copy): Message number ";\r
-const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;\r
-/* 15 is added to ensure the number, \r\n and terminating zero fit. */\r
-const size_t xStringLength = strlen( pcStringToSend ) + 15;\r
-\r
-       /* Remove compiler warning about unused parameters. */\r
-       ( void ) pvParameters;\r
-\r
-       /* It is assumed that this task is not created until the network is up,\r
-       so the IP address can be obtained immediately.  store the IP address being\r
-       used in ulIPAddress.  This is done so the socket can send to a different\r
-       port on the same IP address. */\r
-       FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
-\r
-       /* This test sends to itself, so data sent from here is received by a server\r
-       socket on the same IP address.  Setup the freertos_sockaddr structure with\r
-       this nodes IP address, and the port number being sent to.  The strange\r
-       casting is to try and remove compiler warnings on 32 bit machines. */\r
-       xDestinationAddress.sin_addr = ulIPAddress;\r
-       xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
-       xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
-\r
-       for( ;; )\r
-       {\r
-               /* Create the socket. */\r
-               xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
-               configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );\r
-\r
-               /* The count is used to differentiate between different messages sent to\r
-               the server, and to break out of the do while loop below. */\r
-               ulCount = 0UL;\r
-\r
-               do\r
-               {\r
-                       /* This task is going to send using the zero copy interface.  The\r
-                       data being sent is therefore written directly into a buffer that is\r
-                       passed into, rather than copied into, the FreeRTOS_sendto()\r
-                       function.\r
-\r
-                       First obtain a buffer of adequate length from the IP stack into which\r
-                       the string will be written.  Although a max delay is used, the actual\r
-                       delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence\r
-                       the do while loop is used to ensure a buffer is obtained. */\r
-                       do\r
-                       {\r
-                       } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );\r
-\r
-                       /* A buffer was successfully obtained.  Create the string that is\r
-                       sent to the server.  First the string is filled with zeros as this will\r
-                       effectively be the null terminator when the string is received at the other\r
-                       end.  Note that the string is being written directly into the buffer\r
-                       obtained from the IP stack above. */\r
-                       memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );\r
-                       sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", pcStringToSend, ulCount );\r
-\r
-                       /* Pass the buffer into the send function.  ulFlags has the\r
-                       FREERTOS_ZERO_COPY bit set so the IP stack will take control of the\r
-                       buffer rather than copy data out of the buffer. */\r
-                       lReturned = FreeRTOS_sendto( xClientSocket,                             /* The socket being sent to. */\r
-                                                                               ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */\r
-                                                                               strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */\r
-                                                                               FREERTOS_ZERO_COPY,                     /* ulFlags with the FREERTOS_ZERO_COPY bit set. */\r
-                                                                               &xDestinationAddress,                   /* Where the data is being sent. */\r
-                                                                               sizeof( xDestinationAddress ) );\r
-\r
-                       if( lReturned == 0 )\r
-                       {\r
-                               /* The send operation failed, so this task is still responsible\r
-                               for the buffer obtained from the IP stack.  To ensure the buffer\r
-                               is not lost it must either be used again, or, as in this case,\r
-                               returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().\r
-                               pucUDPPayloadBuffer can be safely re-used after this call. */\r
-                               FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
-                       }\r
-                       else\r
-                       {\r
-                               /* The send was successful so the IP stack is now managing the\r
-                               buffer pointed to by pucUDPPayloadBuffer, and the IP stack will\r
-                               return the buffer once it has been sent.  pucUDPPayloadBuffer can\r
-                               be safely re-used. */\r
-                       }\r
-\r
-                       ulCount++;\r
-\r
-               } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );\r
-\r
-               FreeRTOS_closesocket( xClientSocket );\r
-\r
-               /* A short delay to prevent the messages scrolling off the screen too\r
-               quickly. */\r
-               vTaskDelay( x150ms );\r
-       }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvSimpleZeroCopyServerTask( void *pvParameters )\r
-{\r
-int32_t lBytes;\r
-uint8_t *pucUDPPayloadBuffer;\r
-struct freertos_sockaddr xClient, xBindAddress;\r
-uint32_t xClientLength = sizeof( xClient ), ulIPAddress;\r
-Socket_t xListeningSocket;\r
-\r
-       /* Just to prevent compiler warnings. */\r
-       ( void ) pvParameters;\r
-\r
-       /* Attempt to open the socket. */\r
-       xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
-       configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );\r
-\r
-       /* This test receives data sent from a different port on the same IP address.\r
-       Obtain the nodes IP address.  Configure the freertos_sockaddr structure with\r
-       the address being bound to.  The strange casting is to try and remove\r
-       compiler warnings on 32 bit machines.  Note that this task is only created\r
-       after the network is up, so the IP address is valid here. */\r
-       FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
-       xBindAddress.sin_addr = ulIPAddress;\r
-       xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
-       xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );\r
-\r
-       /* Bind the socket to the port that the client task will send to. */\r
-       FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );\r
-\r
-       for( ;; )\r
-       {\r
-               /* Receive data on the socket.  ulFlags has the zero copy bit set\r
-               (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the\r
-               received data should be passed out to this task using the second\r
-               parameter to the FreeRTOS_recvfrom() call.  When this is done the\r
-               IP stack is no longer responsible for releasing the buffer, and\r
-               the task *must* return the buffer to the stack when it is no longer\r
-               needed.  By default the block time is portMAX_DELAY. */\r
-               lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );\r
-\r
-               /* Print the received characters. */\r
-               if( lBytes > 0 )\r
-               {\r
-                       /* It is expected to receive one more byte than the string length as\r
-                       the NULL terminator is also transmitted. */\r
-                       configASSERT( lBytes == ( ( BaseType_t ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );\r
-               }\r
-\r
-               if( lBytes >= 0 )\r
-               {\r
-                       /* The buffer *must* be freed once it is no longer needed. */\r
-                       FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
-               }\r
-       }\r
-}\r
-\r