2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* WinPCap includes. */
\r
69 /* FreeRTOS includes. */
\r
70 #include "FreeRTOS.h"
\r
74 /* lwIP includes. */
\r
75 #include "lwip/opt.h"
\r
76 #include "lwip/def.h"
\r
77 #include "lwip/mem.h"
\r
78 #include "lwip/pbuf.h"
\r
79 #include "lwip/sys.h"
\r
80 #include <lwip/stats.h>
\r
81 #include <lwip/snmp.h>
\r
82 #include "netif/etharp.h"
\r
84 /* Define those to better describe your network interface. */
\r
88 #define netifMAX_MTU 1500
\r
92 struct eth_addr *ethaddr;
\r
93 /* Add whatever per-interface state that is needed here. */
\r
97 * Place received packet in a pbuf and send a message to the tcpip task to let
\r
98 * it know new data has arrived.
\r
100 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
\r
103 * Copy the received data into a pbuf.
\r
105 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
\r
108 * Send data from a pbuf to the hardware.
\r
110 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
113 * Perform any hardware and/or driver initialisation necessary.
\r
115 static void prvLowLevelInit( struct netif *pxNetIf );
\r
118 * Query the computer the simulation is being executed on to find the network
\r
119 * interfaces it has installed.
\r
121 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
124 * Open the network interface. The number of the interface to be opened is set
\r
125 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
127 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
130 * Interrupts cannot truely be simulated using WinPCap. In reality this task
\r
131 * just polls the interface.
\r
133 static void prvInterruptSimulator( void *pvParameters );
\r
136 * Configure the capture filter to allow blocking reads, and to filter out
\r
137 * packets that are not of interest to this demo.
\r
139 static void prvConfigureCaptureBehaviour( void );
\r
141 /*-----------------------------------------------------------*/
\r
143 /* The WinPCap interface being used. */
\r
144 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
146 /* Parameter required for WinPCap API functions. */
\r
147 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
149 /* The network interface that was opened. */
\r
150 static struct netif *pxlwIPNetIf = NULL;
\r
152 /*-----------------------------------------------------------*/
\r
155 * In this function, the hardware should be initialized.
\r
156 * Called from ethernetif_init().
\r
158 * @param pxNetIf the already initialized lwip network interface structure
\r
159 * for this ethernetif.
\r
161 static void prvLowLevelInit( struct netif *pxNetIf )
\r
163 pcap_if_t *pxAllNetworkInterfaces;
\r
165 /* set MAC hardware address length */
\r
166 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
168 /* set MAC hardware address */
\r
169 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
170 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
171 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
172 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
173 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
174 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
176 /* device capabilities */
\r
177 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
\r
178 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
180 /* Query the computer the simulation is being executed on to find the
\r
181 network interfaces it has installed. */
\r
182 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
184 /* Open the network interface. The number of the interface to be opened is
\r
185 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
186 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
187 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
188 the interface could not be opened. */
\r
189 if( pxAllNetworkInterfaces != NULL )
\r
191 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
194 /* Remember which interface was opened as it is used in the interrupt
\r
196 pxlwIPNetIf = pxNetIf;
\r
200 * This function should do the actual transmission of the packet. The packet is
\r
201 * contained in the pbuf that is passed to the function. This pbuf
\r
202 * might be chained.
\r
204 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
205 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
206 * @return ERR_OK if the packet could be sent
\r
207 * an err_t value if the packet couldn't be sent
\r
209 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
210 * strange results. You might consider waiting for space in the DMA queue
\r
211 * to become availale since the stack doesn't retry to send a packet
\r
212 * dropped because of memory failure (except for the TCP timers).
\r
215 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
218 /* This is taken from lwIP example code and therefore does not conform
\r
219 to the FreeRTOS coding standard. */
\r
222 static unsigned char ucBuffer[ 1520 ];
\r
223 unsigned char *pucBuffer = ucBuffer;
\r
224 unsigned char *pucChar;
\r
225 struct eth_hdr *pxHeader;
\r
226 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
227 err_t xReturn = ERR_OK;
\r
231 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
232 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
235 /* Initiate transfer. */
\r
236 if( p->len == p->tot_len )
\r
238 /* No pbuf chain, don't have to copy -> faster. */
\r
239 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
243 /* pbuf chain, copy into contiguous ucBuffer. */
\r
244 if( p->tot_len >= sizeof( ucBuffer ) )
\r
246 LINK_STATS_INC( link.lenerr );
\r
247 LINK_STATS_INC( link.drop );
\r
248 snmp_inc_ifoutdiscards( pxNetIf );
\r
253 pucChar = ucBuffer;
\r
255 for( q = p; q != NULL; q = q->next )
\r
257 /* Send the data from the pbuf to the interface, one pbuf at a
\r
258 time. The size of the data in each pbuf is kept in the ->len
\r
260 /* send data from(q->payload, q->len); */
\r
261 LWIP_DEBUGF( NETIF_DEBUG, ("NETIF: send pucChar %p q->payload %p q->len %i q->next %p\n", pucChar, q->payload, ( int ) q->len, ( void* ) q->next ) );
\r
264 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
265 pucChar += q->len - ETH_PAD_SIZE;
\r
269 memcpy( pucChar, q->payload, q->len );
\r
276 if( xReturn == ERR_OK )
\r
278 /* signal that packet should be sent */
\r
279 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
\r
281 LINK_STATS_INC( link.memerr );
\r
282 LINK_STATS_INC( link.drop );
\r
283 snmp_inc_ifoutdiscards( pxNetIf );
\r
288 LINK_STATS_INC( link.xmit );
\r
289 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
290 pxHeader = ( struct eth_hdr * )p->payload;
\r
292 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
294 /* broadcast or multicast packet*/
\r
295 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
299 /* unicast packet */
\r
300 snmp_inc_ifoutucastpkts( pxNetIf );
\r
309 * Should allocate a pbuf and transfer the bytes of the incoming
\r
310 * packet from the interface into the pbuf.
\r
312 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
313 * @return a pbuf filled with the received packet (including MAC header)
\r
314 * NULL on memory error
\r
316 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
\r
318 struct pbuf *p = NULL, *q;
\r
320 if( lDataLength > 0 )
\r
323 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
326 /* We allocate a pbuf chain of pbufs from the pool. */
\r
327 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
\r
332 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
335 /* We iterate over the pbuf chain until we have read the entire
\r
336 * packet into the pbuf. */
\r
338 for( q = p; q != NULL; q = q->next )
\r
340 /* Read enough bytes to fill this pbuf in the chain. The
\r
341 * available data in the pbuf is given by the q->len
\r
343 * This does not necessarily have to be a memcpy, you can also preallocate
\r
344 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
345 * actually received size. In this case, ensure the usTotalLength member of the
\r
346 * pbuf is the sum of the chained pbuf len members.
\r
348 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
\r
349 lDataLength += q->len;
\r
353 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
356 LINK_STATS_INC( link.recv );
\r
364 * This function should be called when a packet is ready to be read
\r
365 * from the interface. It uses the function prvLowLevelInput() that
\r
366 * should handle the actual reception of bytes from the network
\r
367 * interface. Then the type of the received packet is determined and
\r
368 * the appropriate input function is called.
\r
370 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
372 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
\r
374 /* This is taken from lwIP example code and therefore does not conform
\r
375 to the FreeRTOS coding standard. */
\r
377 struct eth_hdr *pxHeader;
\r
380 /* move received packet into a new pbuf */
\r
381 p = prvLowLevelInput( pucInputData, lInputLength );
\r
383 /* no packet could be read, silently ignore this */
\r
386 /* points to packet payload, which starts with an Ethernet header */
\r
387 pxHeader = p->payload;
\r
389 switch( htons( pxHeader->type ) )
\r
391 /* IP or ARP packet? */
\r
394 /* full packet send to tcpip_thread to process */
\r
395 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
\r
397 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
412 * Should be called at the beginning of the program to set up the
\r
413 * network interface. It calls the function prvLowLevelInit() to do the
\r
414 * actual setup of the hardware.
\r
416 * This function should be passed as a parameter to netif_add().
\r
418 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
419 * @return ERR_OK if the loopif is initialized
\r
420 * ERR_MEM if private data couldn't be allocated
\r
421 * any other err_t on error
\r
423 err_t ethernetif_init( struct netif *pxNetIf )
\r
425 err_t xReturn = ERR_OK;
\r
427 /* This is taken from lwIP example code and therefore does not conform
\r
428 to the FreeRTOS coding standard. */
\r
430 struct xEthernetIf *pxEthernetIf;
\r
432 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
434 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
435 if( pxEthernetIf == NULL )
\r
437 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
442 #if LWIP_NETIF_HOSTNAME
\r
444 /* Initialize interface hostname */
\r
445 pxNetIf->hostname = "lwip";
\r
447 #endif /* LWIP_NETIF_HOSTNAME */
\r
449 pxNetIf->state = pxEthernetIf;
\r
450 pxNetIf->name[ 0 ] = IFNAME0;
\r
451 pxNetIf->name[ 1 ] = IFNAME1;
\r
453 /* We directly use etharp_output() here to save a function call.
\r
454 * You can instead declare your own function an call etharp_output()
\r
455 * from it if you have to do some checks before sending (e.g. if link
\r
456 * is available...) */
\r
457 pxNetIf->output = etharp_output;
\r
458 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
459 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
460 pxNetIf->mtu = netifMAX_MTU;
\r
461 pxNetIf->linkoutput = prvLowLevelOutput;
\r
463 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
465 /* initialize the hardware */
\r
466 prvLowLevelInit( pxNetIf );
\r
468 /* Was an interface opened? */
\r
469 if( pxOpenedInterfaceHandle == NULL )
\r
471 /* Probably an invalid adapter number was defined in
\r
472 FreeRTOSConfig.h. */
\r
474 configASSERT( pxOpenedInterfaceHandle );
\r
480 /*-----------------------------------------------------------*/
\r
482 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
484 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
485 long lInterfaceNumber = 1;
\r
487 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
489 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
490 pxAllNetworkInterfaces = NULL;
\r
493 if( pxAllNetworkInterfaces != NULL )
\r
495 /* Print out the list of network interfaces. The first in the list
\r
496 is interface '1', not interface '0'. */
\r
497 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
499 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
501 if( xInterface->description != NULL )
\r
503 printf( " (%s)\r\n", xInterface->description );
\r
507 printf( " (No description available)\r\n") ;
\r
510 lInterfaceNumber++;
\r
514 if( lInterfaceNumber == 1 )
\r
516 /* The interface number was never incremented, so the above for() loop
\r
517 did not execute meaning no interfaces were found. */
\r
518 printf( " \r\nNo network interfaces were found.\r\n" );
\r
519 pxAllNetworkInterfaces = NULL;
\r
522 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
523 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
525 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
527 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
529 if( pxAllNetworkInterfaces != NULL )
\r
531 /* Free the device list, as no devices are going to be opened. */
\r
532 pcap_freealldevs( pxAllNetworkInterfaces );
\r
533 pxAllNetworkInterfaces = NULL;
\r
537 return pxAllNetworkInterfaces;
\r
539 /*-----------------------------------------------------------*/
\r
541 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
543 pcap_if_t *xInterface;
\r
546 /* Walk the list of devices until the selected device is located. */
\r
547 xInterface = pxAllNetworkInterfaces;
\r
548 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
550 xInterface = xInterface->next;
\r
553 /* Open the selected interface. */
\r
554 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
555 netifMAX_MTU, /* The size of the packet to capture. */
\r
556 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
557 IP address is going to be "simulated", and
\r
558 not be the real MAC and IP address. This allows
\r
559 trafic to the simulated IP address to be routed
\r
560 to uIP, and trafic to the real IP address to be
\r
561 routed to the Windows TCP/IP stack. */
\r
562 0L, /* The read time out. This is going to block
\r
563 until data is available. */
\r
564 NULL, /* No authentication is required as this is
\r
565 not a remote capture session. */
\r
569 if ( pxOpenedInterfaceHandle == NULL )
\r
571 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
575 /* Configure the capture filter to allow blocking reads, and to filter
\r
576 out packets that are not of interest to this demo. */
\r
577 prvConfigureCaptureBehaviour();
\r
580 /* The device list is no longer required. */
\r
581 pcap_freealldevs( pxAllNetworkInterfaces );
\r
583 /*-----------------------------------------------------------*/
\r
585 static void prvInterruptSimulator( void *pvParameters )
\r
587 static struct pcap_pkthdr *pxHeader;
\r
588 const unsigned char *pucPacketData;
\r
589 extern xQueueHandle xEMACEventQueue;
\r
592 /* Just to kill the compiler warning. */
\r
593 ( void ) pvParameters;
\r
597 /* Get the next packet. */
\r
598 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
601 if( pxlwIPNetIf != NULL )
\r
603 prvEthernetInput( pucPacketData, pxHeader->len );
\r
608 /* There is no real way of simulating an interrupt.
\r
609 Make sure other tasks can run. */
\r
614 /*-----------------------------------------------------------*/
\r
616 static void prvConfigureCaptureBehaviour( void )
\r
618 struct bpf_program xFilterCode;
\r
619 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
620 unsigned long ulNetMask;
\r
622 /* Unblock a read as soon as anything is received. */
\r
623 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
625 /* Allow blocking. */
\r
626 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
628 /* Set up a filter so only the packets of interest are passed to the lwIP
\r
629 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
630 confuse this with an error message. */
\r
631 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
633 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
635 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
637 printf( "\r\nThe packet filter string is invalid\r\n" );
\r
641 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
643 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
647 /* Create a task that simulates an interrupt in a real system. This will
\r
648 block waiting for packets, then send a message to the uIP task when data
\r
650 xTaskCreate( prvInterruptSimulator, ( signed char * ) "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r