2 * FreeRTOS Kernel V10.0.0
\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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates two transmitting tasks and two receiving tasks. The transmitting
\r
31 * tasks send values that are received by the receiving tasks. One set of tasks
\r
32 * uses the standard API. The other set of tasks uses the zero copy API.
\r
35 /* Standard includes. */
\r
39 /* FreeRTOS includes. */
\r
40 #include "FreeRTOS.h"
\r
43 /* FreeRTOS+UDP includes. */
\r
44 #include "FreeRTOS_UDP_IP.h"
\r
45 #include "FreeRTOS_Sockets.h"
\r
47 #define simpTINY_DELAY ( ( TickType_t ) 2 )
\r
50 * Uses a socket to send data without using the zero copy option.
\r
51 * prvSimpleServerTask() will receive the data.
\r
53 static void prvSimpleClientTask( void *pvParameters );
\r
56 * Uses a socket to receive the data sent by the prvSimpleClientTask() task.
\r
57 * Does not use the zero copy option.
\r
59 static void prvSimpleServerTask( void *pvParameters );
\r
62 * Uses a socket to send data using the zero copy option.
\r
63 * prvSimpleZeroCopyServerTask() will receive the data.
\r
65 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );
\r
68 * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()
\r
69 * task. Uses the zero copy option.
\r
71 static void prvSimpleZeroCopyServerTask( void *pvParameters );
\r
73 /*-----------------------------------------------------------*/
\r
75 void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
\r
77 /* Create the client and server tasks that do not use the zero copy
\r
79 xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
80 xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );
\r
82 /* Create the client and server tasks that do use the zero copy interface. */
\r
83 xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );
\r
84 xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );
\r
86 /*-----------------------------------------------------------*/
\r
88 static void prvSimpleClientTask( void *pvParameters )
\r
90 xSocket_t xClientSocket;
\r
91 struct freertos_sockaddr xDestinationAddress;
\r
93 BaseType_t lReturned;
\r
94 uint32_t ulCount = 0UL, ulIPAddress;
\r
95 const uint32_t ulLoopsPerSocket = 10UL;
\r
96 const TickType_t x150ms = 150UL / portTICK_RATE_MS;
\r
98 /* Remove compiler warning about unused parameters. */
\r
99 ( void ) pvParameters;
\r
101 /* It is assumed that this task is not created until the network is up,
\r
102 so the IP address can be obtained immediately. store the IP address being
\r
103 used in ulIPAddress. This is done so the socket can send to a different
\r
104 port on the same IP address. */
\r
105 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
107 /* This test sends to itself, so data sent from here is received by a server
\r
108 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
109 this nodes IP address, and the port number being sent to. The strange
\r
110 casting is to try and remove compiler warnings on 32 bit machines. */
\r
111 xDestinationAddress.sin_addr = ulIPAddress;
\r
112 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
113 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
117 /* Create the socket. */
\r
118 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
119 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
121 /* The count is used to differentiate between different messages sent to
\r
122 the server, and to break out of the do while loop below. */
\r
127 /* Create the string that is sent to the server. */
\r
128 sprintf( cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );
\r
130 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
131 copy option is not selected. That means the data from cString[] is
\r
132 copied into a network buffer inside FreeRTOS_sendto(), and cString[]
\r
133 can be reused as soon as FreeRTOS_sendto() has returned. */
\r
134 lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
\r
138 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
140 FreeRTOS_closesocket( xClientSocket );
\r
142 /* A short delay to prevent the messages printed by the server task
\r
143 scrolling off the screen too quickly, and to prevent reduce the network
\r
145 vTaskDelay( x150ms );
\r
148 /*-----------------------------------------------------------*/
\r
150 static void prvSimpleServerTask( void *pvParameters )
\r
153 char cReceivedString[ 60 ];
\r
154 struct freertos_sockaddr xClient, xBindAddress;
\r
155 uint32_t xClientLength = sizeof( xClient );
\r
156 xSocket_t xListeningSocket;
\r
158 /* Just to prevent compiler warnings. */
\r
159 ( void ) pvParameters;
\r
161 /* Attempt to open the socket. */
\r
162 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
163 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
165 /* This test receives data sent from a different port on the same IP
\r
166 address. Configure the freertos_sockaddr structure with the address being
\r
167 bound to. The strange casting is to try and remove compiler warnings on 32
\r
168 bit machines. Note that this task is only created after the network is up,
\r
169 so the IP address is valid here. */
\r
170 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
171 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
173 /* Bind the socket to the port that the client task will send to. */
\r
174 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
178 /* Zero out the receive array so there is NULL at the end of the string
\r
179 when it is printed out. */
\r
180 memset( cReceivedString, 0x00, sizeof( cReceivedString ) );
\r
182 /* Receive data on the socket. ulFlags is zero, so the zero copy option
\r
183 is not set and the received data is copied into the buffer pointed to by
\r
184 cReceivedString. By default the block time is portMAX_DELAY.
\r
185 xClientLength is not actually used by FreeRTOS_recvfrom(), but is set
\r
186 appropriately in case future versions do use it. */
\r
187 lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );
\r
189 /* Print the received characters. */
\r
192 vOutputString( cReceivedString );
\r
196 configASSERT( lBytes == ( BaseType_t ) strlen( cReceivedString ) );
\r
199 /*-----------------------------------------------------------*/
\r
201 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )
\r
203 xSocket_t xClientSocket;
\r
204 uint8_t *pucUDPPayloadBuffer;
\r
205 struct freertos_sockaddr xDestinationAddress;
\r
206 BaseType_t lReturned;
\r
207 uint32_t ulCount = 0UL, ulIPAddress;
\r
208 const uint32_t ulLoopsPerSocket = 10UL;
\r
209 const char *pcStringToSend = "Server received (using zero copy): Message number ";
\r
210 const TickType_t x150ms = 150UL / portTICK_RATE_MS;
\r
211 /* 15 is added to ensure the number, \r\n and terminating zero fit. */
\r
212 const size_t xStringLength = strlen( pcStringToSend ) + 15;
\r
214 /* Remove compiler warning about unused parameters. */
\r
215 ( void ) pvParameters;
\r
217 /* It is assumed that this task is not created until the network is up,
\r
218 so the IP address can be obtained immediately. store the IP address being
\r
219 used in ulIPAddress. This is done so the socket can send to a different
\r
220 port on the same IP address. */
\r
221 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
223 /* This test sends to itself, so data sent from here is received by a server
\r
224 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
225 this nodes IP address, and the port number being sent to. The strange
\r
226 casting is to try and remove compiler warnings on 32 bit machines. */
\r
227 xDestinationAddress.sin_addr = ulIPAddress;
\r
228 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
229 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
233 /* Create the socket. */
\r
234 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
235 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
237 /* The count is used to differentiate between different messages sent to
\r
238 the server, and to break out of the do while loop below. */
\r
243 /* This task is going to send using the zero copy interface. The
\r
244 data being sent is therefore written directly into a buffer that is
\r
245 passed into, rather than copied into, the FreeRTOS_sendto()
\r
248 First obtain a buffer of adequate length from the IP stack into which
\r
249 the string will be written. Although a max delay is used, the actual
\r
250 delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence
\r
251 the do while loop is used to ensure a buffer is obtained. */
\r
254 } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );
\r
256 /* A buffer was successfully obtained. Create the string that is
\r
257 sent to the server. First the string is filled with zeros as this will
\r
258 effectively be the null terminator when the string is received at the other
\r
259 end. Note that the string is being written directly into the buffer
\r
260 obtained from the IP stack above. */
\r
261 memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );
\r
262 sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", pcStringToSend, ulCount );
\r
264 /* Pass the buffer into the send function. ulFlags has the
\r
265 FREERTOS_ZERO_COPY bit set so the IP stack will take control of the
\r
266 buffer rather than copy data out of the buffer. */
\r
267 lReturned = FreeRTOS_sendto( xClientSocket, /* The socket being sent to. */
\r
268 ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */
\r
269 strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */
\r
270 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
271 &xDestinationAddress, /* Where the data is being sent. */
\r
272 sizeof( xDestinationAddress ) );
\r
274 if( lReturned == 0 )
\r
276 /* The send operation failed, so this task is still responsible
\r
277 for the buffer obtained from the IP stack. To ensure the buffer
\r
278 is not lost it must either be used again, or, as in this case,
\r
279 returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().
\r
280 pucUDPPayloadBuffer can be safely re-used after this call. */
\r
281 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
285 /* The send was successful so the IP stack is now managing the
\r
286 buffer pointed to by pucUDPPayloadBuffer, and the IP stack will
\r
287 return the buffer once it has been sent. pucUDPPayloadBuffer can
\r
288 be safely re-used. */
\r
293 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
295 FreeRTOS_closesocket( xClientSocket );
\r
297 /* A short delay to prevent the messages scrolling off the screen too
\r
299 vTaskDelay( x150ms );
\r
302 /*-----------------------------------------------------------*/
\r
304 static void prvSimpleZeroCopyServerTask( void *pvParameters )
\r
307 uint8_t *pucUDPPayloadBuffer;
\r
308 struct freertos_sockaddr xClient, xBindAddress;
\r
309 uint32_t xClientLength = sizeof( xClient ), ulIPAddress;
\r
310 xSocket_t xListeningSocket;
\r
312 /* Just to prevent compiler warnings. */
\r
313 ( void ) pvParameters;
\r
315 /* Attempt to open the socket. */
\r
316 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
317 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
319 /* This test receives data sent from a different port on the same IP address.
\r
320 Obtain the nodes IP address. Configure the freertos_sockaddr structure with
\r
321 the address being bound to. The strange casting is to try and remove
\r
322 compiler warnings on 32 bit machines. Note that this task is only created
\r
323 after the network is up, so the IP address is valid here. */
\r
324 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
325 xBindAddress.sin_addr = ulIPAddress;
\r
326 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
327 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
329 /* Bind the socket to the port that the client task will send to. */
\r
330 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
334 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
335 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the
\r
336 received data should be passed out to this task using the second
\r
337 parameter to the FreeRTOS_recvfrom() call. When this is done the
\r
338 IP stack is no longer responsible for releasing the buffer, and
\r
339 the task *must* return the buffer to the stack when it is no longer
\r
340 needed. By default the block time is portMAX_DELAY. */
\r
341 lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\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
347 /* Print the received characters. */
\r
350 vOutputString( ( char * ) pucUDPPayloadBuffer );
\r
355 /* The buffer *must* be freed once it is no longer needed. */
\r
356 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r