2 * FreeRTOS Kernel V10.2.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
29 * Creates two transmitting tasks and two receiving tasks. The transmitting
\r
30 * tasks send values that are received by the receiving tasks. One set of tasks
\r
31 * uses the standard API. The other set of tasks uses the zero copy API.
\r
33 * See the following web page for essential demo usage and configuration
\r
35 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
38 /* Standard includes. */
\r
42 /* FreeRTOS includes. */
\r
43 #include "FreeRTOS.h"
\r
46 /* FreeRTOS+TCP includes. */
\r
47 #include "FreeRTOS_IP.h"
\r
48 #include "FreeRTOS_Sockets.h"
\r
50 #define simpTINY_DELAY ( ( TickType_t ) 2 )
\r
53 * Uses a socket to send data without using the zero copy option.
\r
54 * prvSimpleServerTask() will receive the data.
\r
56 static void prvSimpleClientTask( void *pvParameters );
\r
59 * Uses a socket to receive the data sent by the prvSimpleClientTask() task.
\r
60 * Does not use the zero copy option.
\r
62 static void prvSimpleServerTask( void *pvParameters );
\r
65 * Uses a socket to send data using the zero copy option.
\r
66 * prvSimpleZeroCopyServerTask() will receive the data.
\r
68 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );
\r
71 * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()
\r
72 * task. Uses the zero copy option.
\r
74 static void prvSimpleZeroCopyServerTask( void *pvParameters );
\r
76 /*-----------------------------------------------------------*/
\r
78 void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
\r
80 /* Create the client and server tasks that do not use the zero copy
\r
82 xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
83 xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );
\r
85 /* Create the client and server tasks that do use the zero copy interface. */
\r
86 xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );
\r
87 xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );
\r
89 /*-----------------------------------------------------------*/
\r
91 static void prvSimpleClientTask( void *pvParameters )
\r
93 Socket_t xClientSocket;
\r
94 struct freertos_sockaddr xDestinationAddress;
\r
95 uint8_t cString[ 65 ];
\r
96 BaseType_t lReturned;
\r
97 uint32_t ulCount = 0UL, ulIPAddress;
\r
98 const uint32_t ulLoopsPerSocket = 10UL;
\r
99 const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
\r
101 /* Remove compiler warning about unused parameters. */
\r
102 ( void ) pvParameters;
\r
104 /* It is assumed that this task is not created until the network is up,
\r
105 so the IP address can be obtained immediately. store the IP address being
\r
106 used in ulIPAddress. This is done so the socket can send to a different
\r
107 port on the same IP address. */
\r
108 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
110 /* This test sends to itself, so data sent from here is received by a server
\r
111 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
112 this nodes IP address, and the port number being sent to. The strange
\r
113 casting is to try and remove compiler warnings on 32 bit machines. */
\r
114 xDestinationAddress.sin_addr = ulIPAddress;
\r
115 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
116 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
120 /* Create the socket. */
\r
121 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
122 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
124 /* The count is used to differentiate between different messages sent to
\r
125 the server, and to break out of the do while loop below. */
\r
130 /* Create the string that is sent to the server. */
\r
131 sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );
\r
133 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
134 copy option is not selected. That means the data from cString[] is
\r
135 copied into a network buffer inside FreeRTOS_sendto(), and cString[]
\r
136 can be reused as soon as FreeRTOS_sendto() has returned. */
\r
137 lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
\r
141 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
143 FreeRTOS_closesocket( xClientSocket );
\r
145 /* A short delay to prevent the messages printed by the server task
\r
146 scrolling off the screen too quickly, and to prevent reduce the network
\r
148 vTaskDelay( x150ms );
\r
151 /*-----------------------------------------------------------*/
\r
153 static void prvSimpleServerTask( void *pvParameters )
\r
156 uint8_t cReceivedString[ 60 ];
\r
157 struct freertos_sockaddr xClient, xBindAddress;
\r
158 uint32_t xClientLength = sizeof( xClient );
\r
159 Socket_t xListeningSocket;
\r
161 /* Just to prevent compiler warnings. */
\r
162 ( void ) pvParameters;
\r
164 /* Attempt to open the socket. */
\r
165 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
166 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
168 /* This test receives data sent from a different port on the same IP
\r
169 address. Configure the freertos_sockaddr structure with the address being
\r
170 bound to. The strange casting is to try and remove compiler warnings on 32
\r
171 bit machines. Note that this task is only created after the network is up,
\r
172 so the IP address is valid here. */
\r
173 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
174 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
176 /* Bind the socket to the port that the client task will send to. */
\r
177 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
181 /* Zero out the receive array so there is NULL at the end of the string
\r
182 when it is printed out. */
\r
183 memset( cReceivedString, 0x00, sizeof( cReceivedString ) );
\r
185 /* Receive data on the socket. ulFlags is zero, so the zero copy option
\r
186 is not set and the received data is copied into the buffer pointed to by
\r
187 cReceivedString. By default the block time is portMAX_DELAY.
\r
188 xClientLength is not actually used by FreeRTOS_recvfrom(), but is set
\r
189 appropriately in case future versions do use it. */
\r
190 lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );
\r
193 configASSERT( lBytes == ( BaseType_t ) strlen( ( const char * ) cReceivedString ) );
\r
196 /*-----------------------------------------------------------*/
\r
198 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )
\r
200 Socket_t xClientSocket;
\r
201 uint8_t *pucUDPPayloadBuffer;
\r
202 struct freertos_sockaddr xDestinationAddress;
\r
203 BaseType_t lReturned;
\r
204 uint32_t ulCount = 0UL, ulIPAddress;
\r
205 const uint32_t ulLoopsPerSocket = 10UL;
\r
206 const char *pcStringToSend = "Server received (using zero copy): Message number ";
\r
207 const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
\r
208 /* 15 is added to ensure the number, \r\n and terminating zero fit. */
\r
209 const size_t xStringLength = strlen( pcStringToSend ) + 15;
\r
211 /* Remove compiler warning about unused parameters. */
\r
212 ( void ) pvParameters;
\r
214 /* It is assumed that this task is not created until the network is up,
\r
215 so the IP address can be obtained immediately. store the IP address being
\r
216 used in ulIPAddress. This is done so the socket can send to a different
\r
217 port on the same IP address. */
\r
218 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
220 /* This test sends to itself, so data sent from here is received by a server
\r
221 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
222 this nodes IP address, and the port number being sent to. The strange
\r
223 casting is to try and remove compiler warnings on 32 bit machines. */
\r
224 xDestinationAddress.sin_addr = ulIPAddress;
\r
225 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
226 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
230 /* Create the socket. */
\r
231 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
232 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
234 /* The count is used to differentiate between different messages sent to
\r
235 the server, and to break out of the do while loop below. */
\r
240 /* This task is going to send using the zero copy interface. The
\r
241 data being sent is therefore written directly into a buffer that is
\r
242 passed into, rather than copied into, the FreeRTOS_sendto()
\r
245 First obtain a buffer of adequate length from the IP stack into which
\r
246 the string will be written. Although a max delay is used, the actual
\r
247 delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence
\r
248 the do while loop is used to ensure a buffer is obtained. */
\r
251 } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );
\r
253 /* A buffer was successfully obtained. Create the string that is
\r
254 sent to the server. First the string is filled with zeros as this will
\r
255 effectively be the null terminator when the string is received at the other
\r
256 end. Note that the string is being written directly into the buffer
\r
257 obtained from the IP stack above. */
\r
258 memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );
\r
259 sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", pcStringToSend, ulCount );
\r
261 /* Pass the buffer into the send function. ulFlags has the
\r
262 FREERTOS_ZERO_COPY bit set so the IP stack will take control of the
\r
263 buffer rather than copy data out of the buffer. */
\r
264 lReturned = FreeRTOS_sendto( xClientSocket, /* The socket being sent to. */
\r
265 ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */
\r
266 strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */
\r
267 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
268 &xDestinationAddress, /* Where the data is being sent. */
\r
269 sizeof( xDestinationAddress ) );
\r
271 if( lReturned == 0 )
\r
273 /* The send operation failed, so this task is still responsible
\r
274 for the buffer obtained from the IP stack. To ensure the buffer
\r
275 is not lost it must either be used again, or, as in this case,
\r
276 returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().
\r
277 pucUDPPayloadBuffer can be safely re-used after this call. */
\r
278 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
282 /* The send was successful so the IP stack is now managing the
\r
283 buffer pointed to by pucUDPPayloadBuffer, and the IP stack will
\r
284 return the buffer once it has been sent. pucUDPPayloadBuffer can
\r
285 be safely re-used. */
\r
290 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
292 FreeRTOS_closesocket( xClientSocket );
\r
294 /* A short delay to prevent the messages scrolling off the screen too
\r
296 vTaskDelay( x150ms );
\r
299 /*-----------------------------------------------------------*/
\r
301 static void prvSimpleZeroCopyServerTask( void *pvParameters )
\r
304 uint8_t *pucUDPPayloadBuffer;
\r
305 struct freertos_sockaddr xClient, xBindAddress;
\r
306 uint32_t xClientLength = sizeof( xClient ), ulIPAddress;
\r
307 Socket_t xListeningSocket;
\r
309 /* Just to prevent compiler warnings. */
\r
310 ( void ) pvParameters;
\r
312 /* Attempt to open the socket. */
\r
313 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
314 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
316 /* This test receives data sent from a different port on the same IP address.
\r
317 Obtain the nodes IP address. Configure the freertos_sockaddr structure with
\r
318 the address being bound to. The strange casting is to try and remove
\r
319 compiler warnings on 32 bit machines. Note that this task is only created
\r
320 after the network is up, so the IP address is valid here. */
\r
321 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
322 xBindAddress.sin_addr = ulIPAddress;
\r
323 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
324 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
326 /* Bind the socket to the port that the client task will send to. */
\r
327 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
331 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
332 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the
\r
333 received data should be passed out to this task using the second
\r
334 parameter to the FreeRTOS_recvfrom() call. When this is done the
\r
335 IP stack is no longer responsible for releasing the buffer, and
\r
336 the task *must* return the buffer to the stack when it is no longer
\r
337 needed. By default the block time is portMAX_DELAY. */
\r
338 lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\r
340 /* Print the received characters. */
\r
343 /* It is expected to receive one more byte than the string length as
\r
344 the NULL terminator is also transmitted. */
\r
345 configASSERT( lBytes == ( ( BaseType_t ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );
\r
350 /* The buffer *must* be freed once it is no longer needed. */
\r
351 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r