2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* WinPCap includes. */
\r
58 /* FreeRTOS includes. */
\r
59 #include "FreeRTOS.h"
\r
63 /* lwIP includes. */
\r
64 #include "lwip/opt.h"
\r
65 #include "lwip/def.h"
\r
66 #include "lwip/mem.h"
\r
67 #include "lwip/pbuf.h"
\r
68 #include "lwip/sys.h"
\r
69 #include <lwip/stats.h>
\r
70 #include <lwip/snmp.h>
\r
71 #include "netif/etharp.h"
\r
73 /* Define those to better describe your network interface. */
\r
77 #define netifMAX_MTU 1500
\r
81 struct eth_addr *ethaddr;
\r
82 /* Add whatever per-interface state that is needed here. */
\r
86 * Place received packet in a pbuf and send a message to the tcpip task to let
\r
87 * it know new data has arrived.
\r
89 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
\r
92 * Copy the received data into a pbuf.
\r
94 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
\r
97 * Send data from a pbuf to the hardware.
\r
99 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
102 * Perform any hardware and/or driver initialisation necessary.
\r
104 static void prvLowLevelInit( struct netif *pxNetIf );
\r
107 * Query the computer the simulation is being executed on to find the network
\r
108 * interfaces it has installed.
\r
110 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
113 * Open the network interface. The number of the interface to be opened is set
\r
114 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
116 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
119 * Interrupts cannot truely be simulated using WinPCap. In reality this task
\r
120 * just polls the interface.
\r
122 static void prvInterruptSimulator( void *pvParameters );
\r
125 * Configure the capture filter to allow blocking reads, and to filter out
\r
126 * packets that are not of interest to this demo.
\r
128 static void prvConfigureCaptureBehaviour( void );
\r
130 /*-----------------------------------------------------------*/
\r
132 /* The WinPCap interface being used. */
\r
133 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
135 /* Parameter required for WinPCap API functions. */
\r
136 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
138 /* The network interface that was opened. */
\r
139 static struct netif *pxlwIPNetIf = NULL;
\r
141 /*-----------------------------------------------------------*/
\r
144 * In this function, the hardware should be initialized.
\r
145 * Called from ethernetif_init().
\r
147 * @param pxNetIf the already initialized lwip network interface structure
\r
148 * for this ethernetif.
\r
150 static void prvLowLevelInit( struct netif *pxNetIf )
\r
152 pcap_if_t *pxAllNetworkInterfaces;
\r
154 /* set MAC hardware address length */
\r
155 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
157 /* set MAC hardware address */
\r
158 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
159 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
160 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
161 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
162 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
163 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
165 /* device capabilities */
\r
166 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
\r
167 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
169 /* Query the computer the simulation is being executed on to find the
\r
170 network interfaces it has installed. */
\r
171 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
173 /* Open the network interface. The number of the interface to be opened is
\r
174 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
175 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
176 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
177 the interface could not be opened. */
\r
178 if( pxAllNetworkInterfaces != NULL )
\r
180 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
183 /* Remember which interface was opened as it is used in the interrupt
\r
185 pxlwIPNetIf = pxNetIf;
\r
189 * This function should do the actual transmission of the packet. The packet is
\r
190 * contained in the pbuf that is passed to the function. This pbuf
\r
191 * might be chained.
\r
193 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
194 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
195 * @return ERR_OK if the packet could be sent
\r
196 * an err_t value if the packet couldn't be sent
\r
198 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
199 * strange results. You might consider waiting for space in the DMA queue
\r
200 * to become availale since the stack doesn't retry to send a packet
\r
201 * dropped because of memory failure (except for the TCP timers).
\r
204 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
207 /* This is taken from lwIP example code and therefore does not conform
\r
208 to the FreeRTOS coding standard. */
\r
211 static unsigned char ucBuffer[ 1520 ];
\r
212 unsigned char *pucBuffer = ucBuffer;
\r
213 unsigned char *pucChar;
\r
214 struct eth_hdr *pxHeader;
\r
215 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
216 err_t xReturn = ERR_OK;
\r
218 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
219 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
222 /* Initiate transfer. */
\r
223 if( p->len == p->tot_len )
\r
225 /* No pbuf chain, don't have to copy -> faster. */
\r
226 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
230 /* pbuf chain, copy into contiguous ucBuffer. */
\r
231 if( p->tot_len >= sizeof( ucBuffer ) )
\r
233 LINK_STATS_INC( link.lenerr );
\r
234 LINK_STATS_INC( link.drop );
\r
235 snmp_inc_ifoutdiscards( pxNetIf );
\r
240 pucChar = ucBuffer;
\r
242 for( q = p; q != NULL; q = q->next )
\r
244 /* Send the data from the pbuf to the interface, one pbuf at a
\r
245 time. The size of the data in each pbuf is kept in the ->len
\r
247 /* send data from(q->payload, q->len); */
\r
248 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
251 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
252 pucChar += q->len - ETH_PAD_SIZE;
\r
256 memcpy( pucChar, q->payload, q->len );
\r
263 if( xReturn == ERR_OK )
\r
265 /* signal that packet should be sent */
\r
266 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
\r
268 LINK_STATS_INC( link.memerr );
\r
269 LINK_STATS_INC( link.drop );
\r
270 snmp_inc_ifoutdiscards( pxNetIf );
\r
275 LINK_STATS_INC( link.xmit );
\r
276 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
277 pxHeader = ( struct eth_hdr * )p->payload;
\r
279 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
281 /* broadcast or multicast packet*/
\r
282 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
286 /* unicast packet */
\r
287 snmp_inc_ifoutucastpkts( pxNetIf );
\r
296 * Should allocate a pbuf and transfer the bytes of the incoming
\r
297 * packet from the interface into the pbuf.
\r
299 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
300 * @return a pbuf filled with the received packet (including MAC header)
\r
301 * NULL on memory error
\r
303 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
\r
305 struct pbuf *p = NULL, *q;
\r
307 if( lDataLength > 0 )
\r
310 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
313 /* We allocate a pbuf chain of pbufs from the pool. */
\r
314 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
\r
319 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
322 /* We iterate over the pbuf chain until we have read the entire
\r
323 * packet into the pbuf. */
\r
325 for( q = p; q != NULL; q = q->next )
\r
327 /* Read enough bytes to fill this pbuf in the chain. The
\r
328 * available data in the pbuf is given by the q->len
\r
330 * This does not necessarily have to be a memcpy, you can also preallocate
\r
331 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
332 * actually received size. In this case, ensure the usTotalLength member of the
\r
333 * pbuf is the sum of the chained pbuf len members.
\r
335 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
\r
336 lDataLength += q->len;
\r
340 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
343 LINK_STATS_INC( link.recv );
\r
351 * This function should be called when a packet is ready to be read
\r
352 * from the interface. It uses the function prvLowLevelInput() that
\r
353 * should handle the actual reception of bytes from the network
\r
354 * interface. Then the type of the received packet is determined and
\r
355 * the appropriate input function is called.
\r
357 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
359 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
\r
361 /* This is taken from lwIP example code and therefore does not conform
\r
362 to the FreeRTOS coding standard. */
\r
364 struct eth_hdr *pxHeader;
\r
367 /* move received packet into a new pbuf */
\r
368 p = prvLowLevelInput( pucInputData, lInputLength );
\r
370 /* no packet could be read, silently ignore this */
\r
373 /* points to packet payload, which starts with an Ethernet header */
\r
374 pxHeader = p->payload;
\r
376 switch( htons( pxHeader->type ) )
\r
378 /* IP or ARP packet? */
\r
381 /* full packet send to tcpip_thread to process */
\r
382 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
\r
384 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
399 * Should be called at the beginning of the program to set up the
\r
400 * network interface. It calls the function prvLowLevelInit() to do the
\r
401 * actual setup of the hardware.
\r
403 * This function should be passed as a parameter to netif_add().
\r
405 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
406 * @return ERR_OK if the loopif is initialized
\r
407 * ERR_MEM if private data couldn't be allocated
\r
408 * any other err_t on error
\r
410 err_t ethernetif_init( struct netif *pxNetIf )
\r
412 err_t xReturn = ERR_OK;
\r
414 /* This is taken from lwIP example code and therefore does not conform
\r
415 to the FreeRTOS coding standard. */
\r
417 struct xEthernetIf *pxEthernetIf;
\r
419 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
421 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
422 if( pxEthernetIf == NULL )
\r
424 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
429 #if LWIP_NETIF_HOSTNAME
\r
431 /* Initialize interface hostname */
\r
432 pxNetIf->hostname = "lwip";
\r
434 #endif /* LWIP_NETIF_HOSTNAME */
\r
436 pxNetIf->state = pxEthernetIf;
\r
437 pxNetIf->name[ 0 ] = IFNAME0;
\r
438 pxNetIf->name[ 1 ] = IFNAME1;
\r
440 /* We directly use etharp_output() here to save a function call.
\r
441 * You can instead declare your own function an call etharp_output()
\r
442 * from it if you have to do some checks before sending (e.g. if link
\r
443 * is available...) */
\r
444 pxNetIf->output = etharp_output;
\r
445 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
446 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
447 pxNetIf->mtu = netifMAX_MTU;
\r
448 pxNetIf->linkoutput = prvLowLevelOutput;
\r
450 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
452 /* initialize the hardware */
\r
453 prvLowLevelInit( pxNetIf );
\r
458 /*-----------------------------------------------------------*/
\r
460 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
462 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
463 long lInterfaceNumber = 1;
\r
465 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
467 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
468 pxAllNetworkInterfaces = NULL;
\r
471 if( pxAllNetworkInterfaces != NULL )
\r
473 /* Print out the list of network interfaces. The first in the list
\r
474 is interface '1', not interface '0'. */
\r
475 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
477 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
479 if( xInterface->description != NULL )
\r
481 printf( " (%s)\r\n", xInterface->description );
\r
485 printf( " (No description available)\r\n") ;
\r
488 lInterfaceNumber++;
\r
492 if( lInterfaceNumber == 1 )
\r
494 /* The interface number was never incremented, so the above for() loop
\r
495 did not execute meaning no interfaces were found. */
\r
496 printf( " \r\nNo network interfaces were found.\r\n" );
\r
497 pxAllNetworkInterfaces = NULL;
\r
500 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
501 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
503 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
505 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
507 if( pxAllNetworkInterfaces != NULL )
\r
509 /* Free the device list, as no devices are going to be opened. */
\r
510 pcap_freealldevs( pxAllNetworkInterfaces );
\r
511 pxAllNetworkInterfaces = NULL;
\r
515 return pxAllNetworkInterfaces;
\r
517 /*-----------------------------------------------------------*/
\r
519 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
521 pcap_if_t *xInterface;
\r
524 /* Walk the list of devices until the selected device is located. */
\r
525 xInterface = pxAllNetworkInterfaces;
\r
526 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
528 xInterface = xInterface->next;
\r
531 /* Open the selected interface. */
\r
532 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
533 netifMAX_MTU, /* The size of the packet to capture. */
\r
534 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
535 IP address is going to be "simulated", and
\r
536 not be the real MAC and IP address. This allows
\r
537 trafic to the simulated IP address to be routed
\r
538 to uIP, and trafic to the real IP address to be
\r
539 routed to the Windows TCP/IP stack. */
\r
540 0L, /* The read time out. This is going to block
\r
541 until data is available. */
\r
542 NULL, /* No authentication is required as this is
\r
543 not a remote capture session. */
\r
547 if ( pxOpenedInterfaceHandle == NULL )
\r
549 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
553 /* Configure the capture filter to allow blocking reads, and to filter
\r
554 out packets that are not of interest to this demo. */
\r
555 prvConfigureCaptureBehaviour();
\r
558 /* The device list is no longer required. */
\r
559 pcap_freealldevs( pxAllNetworkInterfaces );
\r
561 /*-----------------------------------------------------------*/
\r
563 static void prvInterruptSimulator( void *pvParameters )
\r
565 static struct pcap_pkthdr *pxHeader;
\r
566 const unsigned char *pucPacketData;
\r
567 extern xQueueHandle xEMACEventQueue;
\r
570 /* Just to kill the compiler warning. */
\r
571 ( void ) pvParameters;
\r
575 /* Get the next packet. */
\r
576 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
579 if( pxlwIPNetIf != NULL )
\r
581 prvEthernetInput( pucPacketData, pxHeader->len );
\r
586 /* There is no real way of simulating an interrupt.
\r
587 Make sure other tasks can run. */
\r
592 /*-----------------------------------------------------------*/
\r
594 static void prvConfigureCaptureBehaviour( void )
\r
596 struct bpf_program xFilterCode;
\r
597 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
598 unsigned long ulNetMask;
\r
600 /* Unblock a read as soon as anything is received. */
\r
601 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
603 /* Allow blocking. */
\r
604 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
606 /* Set up a filter so only the packets of interest are passed to the lwIP
\r
607 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
608 confuse this with an error message. */
\r
609 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
611 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
613 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
615 printf( "\r\nThe packet filter string is invalid\r\n" );
\r
619 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
621 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
625 /* Create a task that simulates an interrupt in a real system. This will
\r
626 block waiting for packets, then send a message to the uIP task when data
\r
628 xTaskCreate( prvInterruptSimulator, ( signed char * ) "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r