2 * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.
\r
4 * FreeRTOS+UDP is an add-on component to FreeRTOS. It is not, in itself, part
\r
5 * of the FreeRTOS kernel. FreeRTOS+UDP is licensed separately from FreeRTOS,
\r
6 * and uses a different license to FreeRTOS. FreeRTOS+UDP uses a dual license
\r
7 * model, information on which is provided below:
\r
9 * - Open source licensing -
\r
10 * FreeRTOS+UDP is a free download and may be used, modified and distributed
\r
11 * without charge provided the user adheres to version two of the GNU General
\r
12 * Public license (GPL) and does not remove the copyright notice or this text.
\r
13 * The GPL V2 text is available on the gnu.org web site, and on the following
\r
14 * URL: http://www.FreeRTOS.org/gpl-2.0.txt
\r
16 * - Commercial licensing -
\r
17 * Businesses and individuals who wish to incorporate FreeRTOS+UDP into
\r
18 * proprietary software for redistribution in any form must first obtain a
\r
19 * (very) low cost commercial license - and in-so-doing support the maintenance,
\r
20 * support and further development of the FreeRTOS+UDP product. Commercial
\r
21 * licenses can be obtained from http://shop.freertos.org and do not require any
\r
22 * source files to be changed.
\r
24 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
25 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
26 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
27 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
28 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
29 * implied, expressed, or statutory.
\r
31 * 1 tab == 4 spaces!
\r
33 * http://www.FreeRTOS.org
\r
34 * http://www.FreeRTOS.org/udp
\r
38 /* WinPCap includes. */
\r
42 /* Standard includes. */
\r
45 /* FreeRTOS includes. */
\r
46 #include "FreeRTOS.h"
\r
51 /* FreeRTOS+UDP includes. */
\r
52 #include "FreeRTOS_UDP_IP.h"
\r
53 #include "FreeRTOS_IP_Private.h"
\r
54 #include "FreeRTOS_Sockets.h"
\r
55 #include "NetworkBufferManagement.h"
\r
57 /* Demo includes. */
\r
58 #include "NetworkInterface.h"
\r
60 /* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
\r
61 driver will filter incoming packets and only pass the stack those packets it
\r
62 considers need processing. In this case ipCONSIDER_FRAME_FOR_PROCESSING() can
\r
63 be #defined away. If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 0
\r
64 then the Ethernet driver will pass all received packets to the stack, and the
\r
65 stack must do the filtering itself. In this case ipCONSIDER_FRAME_FOR_PROCESSING
\r
66 needs to call eConsiderFrameForProcessing. */
\r
67 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1
\r
68 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
\r
70 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
\r
73 /*-----------------------------------------------------------*/
\r
76 * Print out a numbered list of network interfaces that are available on the
\r
79 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
82 * Open the network interface. The number of the interface to be opened is set
\r
83 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
85 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
88 * Configure the capture filter to allow blocking reads, and to filter out
\r
89 * packets that are not of interest to this demo.
\r
91 static void prvConfigureCaptureBehaviour( void );
\r
94 * A function that simulates Ethernet interrupts by periodically polling the
\r
95 * WinPCap interface for new data.
\r
97 static void prvInterruptSimulatorTask( void *pvParameters );
\r
99 /* The interface being used by WinPCap. */
\r
100 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
102 /*-----------------------------------------------------------*/
\r
104 /* Required by the WinPCap library. */
\r
105 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
107 /* When statically allocated network buffers are used (as opposed to having
\r
108 the buffer payloads allocated and freed as required) the actual buffer storage
\r
109 areas must be defined in the portable layer. This is because different
\r
110 microcontrollers have different location, size and alignment requirements. In
\r
111 this case the network buffers are declared in NetworkInterface.c because, as
\r
112 this file is only used on Windows machines, wasting a few bytes in buffers that
\r
113 never get used does not matter (the buffers will not get used if the dynamic
\r
114 payload allocation file is included in the project). */
\r
115 static uint8_t ucBuffers[ ipconfigNUM_NETWORK_BUFFERS ][ ipTOTAL_ETHERNET_FRAME_SIZE ];
\r
117 /* The queue used to communicate Ethernet events with the IP task. */
\r
118 extern xQueueHandle xNetworkEventQueue;
\r
120 /* Protect the PCAP interface as it is accessed from two tasks (an interrupt
\r
121 simulator is used as real interrupts cannot be obtained from the Ethernet as
\r
122 would normally be the case). */
\r
123 xSemaphoreHandle xPCAPMutex = NULL;
\r
125 /*-----------------------------------------------------------*/
\r
127 portBASE_TYPE xNetworkInterfaceInitialise( void )
\r
129 portBASE_TYPE xReturn = pdFALSE;
\r
130 pcap_if_t *pxAllNetworkInterfaces;
\r
132 if( xPCAPMutex == NULL )
\r
134 xPCAPMutex = xSemaphoreCreateMutex();
\r
135 configASSERT( xPCAPMutex );
\r
138 /* Query the computer the simulation is being executed on to find the
\r
139 network interfaces it has installed. */
\r
140 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
142 /* Open the network interface. The number of the interface to be opened is
\r
143 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
144 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
145 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
146 the interface could not be opened. */
\r
147 if( pxAllNetworkInterfaces != NULL )
\r
149 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
152 if( pxOpenedInterfaceHandle != NULL )
\r
159 /*-----------------------------------------------------------*/
\r
161 #if updconfigLOOPBACK_ETHERNET_PACKETS == 1
\r
163 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
165 xEthernetHeader_t *pxEthernetHeader;
\r
166 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
167 extern uint8_t xDefaultPartUDPPacketHeader[];
\r
168 static const xMACAddress_t xBroadcastMACAddress = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
\r
169 portBASE_TYPE xCanLoopback;
\r
171 pxEthernetHeader = ( xEthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
\r
173 if( memcmp( ( void * ) &( pxEthernetHeader->xDestinationAddress ), ( void * ) &xBroadcastMACAddress, sizeof( xMACAddress_t ) ) == 0 )
\r
175 /* This is a broadcast. */
\r
176 xCanLoopback = pdTRUE;
\r
178 else if( memcmp( ( void * ) &( pxEthernetHeader->xDestinationAddress ), ( void * ) xDefaultPartUDPPacketHeader, sizeof( xMACAddress_t ) ) == 0 )
\r
180 /* This is being sent to itself. */
\r
181 xCanLoopback = pdTRUE;
\r
185 /* This is being sent externally. */
\r
186 xCanLoopback = pdFALSE;
\r
189 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
191 if( xCanLoopback == pdTRUE )
\r
193 /* Just loop the frame back to the input queue. Here the loopback
\r
194 is sending a message to itself, so a block time cannot be used for
\r
195 fear of deadlocking. */
\r
196 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
197 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )
\r
199 vNetworkBufferRelease( pxNetworkBuffer );
\r
200 iptraceETHERNET_RX_EVENT_LOST();
\r
205 /* Send the packet. */
\r
206 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
208 pcap_sendpacket( pxOpenedInterfaceHandle, pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength );
\r
210 xSemaphoreGive( xPCAPMutex );
\r
212 /* The buffer has been transmitted so can be released. */
\r
213 vNetworkBufferRelease( pxNetworkBuffer );
\r
219 #else /* updconfigLOOPBACK_ETHERNET_PACKETS == 1 */
\r
221 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
223 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
225 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
226 pcap_sendpacket( pxOpenedInterfaceHandle, pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength );
\r
228 xSemaphoreGive( xPCAPMutex );
\r
230 /* The buffer has been transmitted so can be released. */
\r
231 vNetworkBufferRelease( pxNetworkBuffer );
\r
236 #endif /* updconfigLOOPBACK_ETHERNET_PACKETS == 1 */
\r
237 /*-----------------------------------------------------------*/
\r
239 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
241 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
242 long lInterfaceNumber = 1;
\r
244 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
246 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
247 pxAllNetworkInterfaces = NULL;
\r
250 if( pxAllNetworkInterfaces != NULL )
\r
252 /* Print out the list of network interfaces. The first in the list
\r
253 is interface '1', not interface '0'. */
\r
254 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
256 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
258 if( xInterface->description != NULL )
\r
260 printf( " (%s)\r\n", xInterface->description );
\r
264 printf( " (No description available)\r\n") ;
\r
267 lInterfaceNumber++;
\r
271 if( lInterfaceNumber == 1 )
\r
273 /* The interface number was never incremented, so the above for() loop
\r
274 did not execute meaning no interfaces were found. */
\r
275 printf( " \r\nNo network interfaces were found.\r\n" );
\r
276 pxAllNetworkInterfaces = NULL;
\r
279 printf( "\r\nThe interface that will be opened is set by configNETWORK_INTERFACE_TO_USE which should be defined in FreeRTOSConfig.h\r\n" );
\r
280 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
282 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
284 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
286 if( pxAllNetworkInterfaces != NULL )
\r
288 /* Free the device list, as no devices are going to be opened. */
\r
289 pcap_freealldevs( pxAllNetworkInterfaces );
\r
290 pxAllNetworkInterfaces = NULL;
\r
294 return pxAllNetworkInterfaces;
\r
296 /*-----------------------------------------------------------*/
\r
298 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
300 pcap_if_t *xInterface;
\r
303 /* Walk the list of devices until the selected device is located. */
\r
304 xInterface = pxAllNetworkInterfaces;
\r
305 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
307 xInterface = xInterface->next;
\r
310 /* Open the selected interface. */
\r
311 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
312 ipTOTAL_ETHERNET_FRAME_SIZE, /* The size of the packet to capture. */
\r
313 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
314 IP address is going to be "simulated", and
\r
315 not be the real MAC and IP address. This allows
\r
316 trafic to the simulated IP address to be routed
\r
317 to uIP, and trafic to the real IP address to be
\r
318 routed to the Windows TCP/IP stack. */
\r
319 0x00L, /* The read time out. */
\r
320 NULL, /* No authentication is required as this is
\r
321 not a remote capture session. */
\r
325 if ( pxOpenedInterfaceHandle == NULL )
\r
327 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
331 /* Configure the capture filter to allow blocking reads, and to filter
\r
332 out packets that are not of interest to this demo. */
\r
333 prvConfigureCaptureBehaviour();
\r
336 /* The device list is no longer required. */
\r
337 pcap_freealldevs( pxAllNetworkInterfaces );
\r
339 /*-----------------------------------------------------------*/
\r
341 static void prvConfigureCaptureBehaviour( void )
\r
343 struct bpf_program xFilterCode;
\r
344 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
345 unsigned long ulNetMask;
\r
347 /* Unblock a read as soon as anything is received. */
\r
348 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
350 /* Allow blocking. */
\r
351 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
353 /* Set up a filter so only the packets of interest are passed to the IP
\r
354 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
355 confuse this with an error message. *//*_RB_ This should not use the #defined constants. *//*_RB_ Constants should not be used, but passed through a generic network API. */
\r
356 sprintf( cErrorBuffer, "broadcast or multicast or ether host %x:%x:%x:%x:%x:%x", configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 );
\r
358 /*_RB_ Constants should not be used, but passed through a generic network API. */
\r
359 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
361 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
363 printf("\r\nThe packet filter string is invalid\r\n" );
\r
367 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
369 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
373 /* Create a task that simulates an interrupt in a real system. This will
\r
374 block waiting for packets, then send a message to the uIP task when data
\r
376 xTaskCreate( prvInterruptSimulatorTask, ( signed char * ) "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r
378 /*-----------------------------------------------------------*/
\r
380 static void prvInterruptSimulatorTask( void *pvParameters )
\r
382 static struct pcap_pkthdr *pxHeader;
\r
383 const uint8_t *pucPacketData;
\r
385 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
386 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
387 eFrameProcessingResult_t eResult;
\r
389 /* Just to kill the compiler warning. */
\r
390 ( void ) pvParameters;
\r
394 /* Get the next packet. */
\r
395 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
397 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
399 xSemaphoreGive( xPCAPMutex );
\r
403 eResult = ipCONSIDER_FRAME_FOR_PROCESSING( pucPacketData );
\r
404 if( eResult == eProcessBuffer )
\r
406 /* Will the data fit into the frame buffer? */
\r
407 if( pxHeader->len <= ipTOTAL_ETHERNET_FRAME_SIZE )
\r
409 /* Obtain a buffer into which the data can be placed. This
\r
410 is only an interrupt simulator, not a real interrupt, so it
\r
411 is ok to call the task level function here. */
\r
412 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
414 pxNetworkBuffer = pxNetworkBufferGet( pxHeader->len, 0 );
\r
416 xSemaphoreGive( xPCAPMutex );
\r
418 if( pxNetworkBuffer != NULL )
\r
420 memcpy( pxNetworkBuffer->pucEthernetBuffer, pucPacketData, pxHeader->len );
\r
421 pxNetworkBuffer->xDataLength = ( size_t ) pxHeader->len;
\r
422 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
424 /* Data was received and stored. Send a message to the IP
\r
425 task to let it know. */
\r
426 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )
\r
428 /* The buffer could not be sent to the stack so
\r
429 must be released again. This is only an interrupt
\r
430 simulator, not a real interrupt, so it is ok to use
\r
431 the task level function here. */
\r
432 vNetworkBufferRelease( pxNetworkBuffer );
\r
433 iptraceETHERNET_RX_EVENT_LOST();
\r
438 iptraceETHERNET_RX_EVENT_LOST();
\r
443 /* Log that a packet was dropped because it would have
\r
444 overflowed the buffer. */
\r
450 /* There is no real way of simulating an interrupt. Make sure
\r
451 other tasks can run. */
\r
452 vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );
\r
456 /*-----------------------------------------------------------*/
\r
458 #if configUSE_STATIC_BUFFERS == 1
\r
459 void vNetworkInterfaceAllocateRAMToBuffers( xNetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ] )
\r
463 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
465 pxNetworkBuffers[ x ].pucEthernetBuffer = &( ucBuffers[ x ][ 0 ] );
\r
469 /*-----------------------------------------------------------*/
\r