2 * FreeRTOS+UDP V1.0.4 (C) 2014 Real Time Engineers ltd.
\r
3 * All rights reserved
\r
5 * This file is part of the FreeRTOS+UDP distribution. The FreeRTOS+UDP license
\r
6 * terms are different to the FreeRTOS license terms.
\r
8 * FreeRTOS+UDP uses a dual license model that allows the software to be used
\r
9 * under a standard GPL open source license, or a commercial license. The
\r
10 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
11 * itself is distributed) requires that all software statically linked with
\r
12 * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.
\r
13 * Details of both license options follow:
\r
15 * - Open source licensing -
\r
16 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
17 * distributed without charge provided the user adheres to version two of the
\r
18 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
19 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
20 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
22 * - Commercial licensing -
\r
23 * Businesses and individuals that for commercial or other reasons cannot comply
\r
24 * with the terms of the GPL V2 license must obtain a commercial license before
\r
25 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
26 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
27 * and do not require any source files to be changed.
\r
29 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
30 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
31 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
32 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
33 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
34 * implied, expressed, or statutory.
\r
36 * 1 tab == 4 spaces!
\r
38 * http://www.FreeRTOS.org
\r
39 * http://www.FreeRTOS.org/udp
\r
43 /* WinPCap includes. */
\r
47 /* Standard includes. */
\r
50 /* FreeRTOS includes. */
\r
51 #include "FreeRTOS.h"
\r
56 /* FreeRTOS+UDP includes. */
\r
57 #include "FreeRTOS_UDP_IP.h"
\r
58 #include "FreeRTOS_IP_Private.h"
\r
59 #include "FreeRTOS_Sockets.h"
\r
60 #include "NetworkBufferManagement.h"
\r
62 /* Demo includes. */
\r
63 #include "NetworkInterface.h"
\r
65 /* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
\r
66 driver will filter incoming packets and only pass the stack those packets it
\r
67 considers need processing. In this case ipCONSIDER_FRAME_FOR_PROCESSING() can
\r
68 be #defined away. If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 0
\r
69 then the Ethernet driver will pass all received packets to the stack, and the
\r
70 stack must do the filtering itself. In this case ipCONSIDER_FRAME_FOR_PROCESSING
\r
71 needs to call eConsiderFrameForProcessing. */
\r
72 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1
\r
73 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
\r
75 #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
\r
78 /*-----------------------------------------------------------*/
\r
81 * Print out a numbered list of network interfaces that are available on the
\r
84 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
87 * Open the network interface. The number of the interface to be opened is set
\r
88 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
90 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
93 * Configure the capture filter to allow blocking reads, and to filter out
\r
94 * packets that are not of interest to this demo.
\r
96 static void prvConfigureCaptureBehaviour( void );
\r
99 * A function that simulates Ethernet interrupts by periodically polling the
\r
100 * WinPCap interface for new data.
\r
102 static void prvInterruptSimulatorTask( void *pvParameters );
\r
104 /* The interface being used by WinPCap. */
\r
105 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
107 /*-----------------------------------------------------------*/
\r
109 /* Required by the WinPCap library. */
\r
110 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
112 /* When statically allocated network buffers are used (as opposed to having
\r
113 the buffer payloads allocated and freed as required) the actual buffer storage
\r
114 areas must be defined in the portable layer. This is because different
\r
115 microcontrollers have different location, size and alignment requirements. In
\r
116 this case the network buffers are declared in NetworkInterface.c because, as
\r
117 this file is only used on Windows machines, wasting a few bytes in buffers that
\r
118 never get used does not matter (the buffers will not get used if the dynamic
\r
119 payload allocation file is included in the project). */
\r
120 static uint8_t ucBuffers[ ipconfigNUM_NETWORK_BUFFERS ][ ipTOTAL_ETHERNET_FRAME_SIZE ];
\r
122 /* The queue used to communicate Ethernet events with the IP task. */
\r
123 extern xQueueHandle xNetworkEventQueue;
\r
125 /* Protect the PCAP interface as it is accessed from two tasks (an interrupt
\r
126 simulator is used as real interrupts cannot be obtained from the Ethernet as
\r
127 would normally be the case). */
\r
128 xSemaphoreHandle xPCAPMutex = NULL;
\r
130 /*-----------------------------------------------------------*/
\r
132 BaseType_t xNetworkInterfaceInitialise( void )
\r
134 BaseType_t xReturn = pdFALSE;
\r
135 pcap_if_t *pxAllNetworkInterfaces;
\r
137 if( xPCAPMutex == NULL )
\r
139 xPCAPMutex = xSemaphoreCreateMutex();
\r
140 configASSERT( xPCAPMutex );
\r
143 /* Query the computer the simulation is being executed on to find the
\r
144 network interfaces it has installed. */
\r
145 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
147 /* Open the network interface. The number of the interface to be opened is
\r
148 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
149 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
150 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
151 the interface could not be opened. */
\r
152 if( pxAllNetworkInterfaces != NULL )
\r
154 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
157 if( pxOpenedInterfaceHandle != NULL )
\r
164 /*-----------------------------------------------------------*/
\r
166 #if updconfigLOOPBACK_ETHERNET_PACKETS == 1
\r
168 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
170 xEthernetHeader_t *pxEthernetHeader;
\r
171 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
172 extern uint8_t xDefaultPartUDPPacketHeader[];
\r
173 static const xMACAddress_t xBroadcastMACAddress = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
\r
174 BaseType_t xCanLoopback;
\r
176 pxEthernetHeader = ( xEthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
\r
178 if( memcmp( ( void * ) &( pxEthernetHeader->xDestinationAddress ), ( void * ) &xBroadcastMACAddress, sizeof( xMACAddress_t ) ) == 0 )
\r
180 /* This is a broadcast. */
\r
181 xCanLoopback = pdTRUE;
\r
183 else if( memcmp( ( void * ) &( pxEthernetHeader->xDestinationAddress ), ( void * ) xDefaultPartUDPPacketHeader, sizeof( xMACAddress_t ) ) == 0 )
\r
185 /* This is being sent to itself. */
\r
186 xCanLoopback = pdTRUE;
\r
190 /* This is being sent externally. */
\r
191 xCanLoopback = pdFALSE;
\r
194 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
196 if( xCanLoopback == pdTRUE )
\r
198 /* Just loop the frame back to the input queue. Here the loopback
\r
199 is sending a message to itself, so a block time cannot be used for
\r
200 fear of deadlocking. */
\r
201 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
202 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )
\r
204 vNetworkBufferRelease( pxNetworkBuffer );
\r
205 iptraceETHERNET_RX_EVENT_LOST();
\r
209 iptraceNETWORK_INTERFACE_RECEIVE();
\r
214 /* Send the packet. */
\r
215 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
217 pcap_sendpacket( pxOpenedInterfaceHandle, pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength );
\r
219 xSemaphoreGive( xPCAPMutex );
\r
221 /* The buffer has been transmitted so can be released. */
\r
222 vNetworkBufferRelease( pxNetworkBuffer );
\r
228 #else /* updconfigLOOPBACK_ETHERNET_PACKETS == 1 */
\r
230 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
232 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
234 iptraceNETWORK_INTERFACE_TRANSMIT();
\r
235 pcap_sendpacket( pxOpenedInterfaceHandle, pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength );
\r
237 xSemaphoreGive( xPCAPMutex );
\r
239 /* The buffer has been transmitted so can be released. */
\r
240 vNetworkBufferRelease( pxNetworkBuffer );
\r
245 #endif /* updconfigLOOPBACK_ETHERNET_PACKETS == 1 */
\r
246 /*-----------------------------------------------------------*/
\r
248 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
250 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
251 long lInterfaceNumber = 1;
\r
253 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
255 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
256 pxAllNetworkInterfaces = NULL;
\r
259 if( pxAllNetworkInterfaces != NULL )
\r
261 /* Print out the list of network interfaces. The first in the list
\r
262 is interface '1', not interface '0'. */
\r
263 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
265 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
267 if( xInterface->description != NULL )
\r
269 printf( " (%s)\r\n", xInterface->description );
\r
273 printf( " (No description available)\r\n") ;
\r
276 lInterfaceNumber++;
\r
280 if( lInterfaceNumber == 1 )
\r
282 /* The interface number was never incremented, so the above for() loop
\r
283 did not execute meaning no interfaces were found. */
\r
284 printf( " \r\nNo network interfaces were found.\r\n" );
\r
285 pxAllNetworkInterfaces = NULL;
\r
288 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
289 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
291 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
293 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
295 if( pxAllNetworkInterfaces != NULL )
\r
297 /* Free the device list, as no devices are going to be opened. */
\r
298 pcap_freealldevs( pxAllNetworkInterfaces );
\r
299 pxAllNetworkInterfaces = NULL;
\r
303 return pxAllNetworkInterfaces;
\r
305 /*-----------------------------------------------------------*/
\r
307 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
309 pcap_if_t *xInterface;
\r
312 /* Walk the list of devices until the selected device is located. */
\r
313 xInterface = pxAllNetworkInterfaces;
\r
314 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
316 xInterface = xInterface->next;
\r
319 /* Open the selected interface. */
\r
320 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
321 ipTOTAL_ETHERNET_FRAME_SIZE, /* The size of the packet to capture. */
\r
322 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
323 IP address is going to be "simulated", and
\r
324 not be the real MAC and IP address. This allows
\r
325 trafic to the simulated IP address to be routed
\r
326 to uIP, and trafic to the real IP address to be
\r
327 routed to the Windows TCP/IP stack. */
\r
328 0x00L, /* The read time out. */
\r
329 NULL, /* No authentication is required as this is
\r
330 not a remote capture session. */
\r
334 if ( pxOpenedInterfaceHandle == NULL )
\r
336 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
340 /* Configure the capture filter to allow blocking reads, and to filter
\r
341 out packets that are not of interest to this demo. */
\r
342 prvConfigureCaptureBehaviour();
\r
345 /* The device list is no longer required. */
\r
346 pcap_freealldevs( pxAllNetworkInterfaces );
\r
348 /*-----------------------------------------------------------*/
\r
350 static void prvConfigureCaptureBehaviour( void )
\r
352 struct bpf_program xFilterCode;
\r
353 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
354 unsigned long ulNetMask;
\r
356 /* Unblock a read as soon as anything is received. */
\r
357 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
359 /* Allow blocking. */
\r
360 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
362 /* Set up a filter so only the packets of interest are passed to the IP
\r
363 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
364 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
365 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
367 /*_RB_ Constants should not be used, but passed through a generic network API. */
\r
368 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
370 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
372 printf("\r\nThe packet filter string is invalid\r\n" );
\r
376 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
378 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
382 /* Create a task that simulates an interrupt in a real system. This will
\r
383 block waiting for packets, then send a message to the uIP task when data
\r
385 xTaskCreate( prvInterruptSimulatorTask, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r
387 /*-----------------------------------------------------------*/
\r
389 static void prvInterruptSimulatorTask( void *pvParameters )
\r
391 static struct pcap_pkthdr *pxHeader;
\r
392 const uint8_t *pucPacketData;
\r
394 xNetworkBufferDescriptor_t *pxNetworkBuffer;
\r
395 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
396 eFrameProcessingResult_t eResult;
\r
398 /* Just to kill the compiler warning. */
\r
399 ( void ) pvParameters;
\r
403 /* Get the next packet. */
\r
404 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
406 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
408 xSemaphoreGive( xPCAPMutex );
\r
412 eResult = ipCONSIDER_FRAME_FOR_PROCESSING( pucPacketData );
\r
413 if( eResult == eProcessBuffer )
\r
415 /* Will the data fit into the frame buffer? */
\r
416 if( pxHeader->len <= ipTOTAL_ETHERNET_FRAME_SIZE )
\r
418 /* Obtain a buffer into which the data can be placed. This
\r
419 is only an interrupt simulator, not a real interrupt, so it
\r
420 is ok to call the task level function here. */
\r
421 xSemaphoreTake( xPCAPMutex, portMAX_DELAY );
\r
423 pxNetworkBuffer = pxNetworkBufferGet( pxHeader->len, 0 );
\r
425 xSemaphoreGive( xPCAPMutex );
\r
427 if( pxNetworkBuffer != NULL )
\r
429 memcpy( pxNetworkBuffer->pucEthernetBuffer, pucPacketData, pxHeader->len );
\r
430 pxNetworkBuffer->xDataLength = ( size_t ) pxHeader->len;
\r
431 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
433 /* Data was received and stored. Send a message to the IP
\r
434 task to let it know. */
\r
435 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )
\r
437 /* The buffer could not be sent to the stack so
\r
438 must be released again. This is only an interrupt
\r
439 simulator, not a real interrupt, so it is ok to use
\r
440 the task level function here. */
\r
441 vNetworkBufferRelease( pxNetworkBuffer );
\r
442 iptraceETHERNET_RX_EVENT_LOST();
\r
446 iptraceNETWORK_INTERFACE_RECEIVE();
\r
451 iptraceETHERNET_RX_EVENT_LOST();
\r
456 /* Log that a packet was dropped because it would have
\r
457 overflowed the buffer. */
\r
463 /* There is no real way of simulating an interrupt. Make sure
\r
464 other tasks can run. */
\r
465 vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );
\r
469 /*-----------------------------------------------------------*/
\r
471 #if configUSE_STATIC_BUFFERS == 1
\r
472 void vNetworkInterfaceAllocateRAMToBuffers( xNetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ] )
\r
476 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
478 pxNetworkBuffers[ x ].pucEthernetBuffer = &( ucBuffers[ x ][ 0 ] );
\r
482 /*-----------------------------------------------------------*/
\r