2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /* WinPCap includes. */
\r
71 /* FreeRTOS includes. */
\r
72 #include "FreeRTOS.h"
\r
76 /* lwIP includes. */
\r
77 #include "lwip/opt.h"
\r
78 #include "lwip/def.h"
\r
79 #include "lwip/mem.h"
\r
80 #include "lwip/pbuf.h"
\r
81 #include "lwip/sys.h"
\r
82 #include <lwip/stats.h>
\r
83 #include <lwip/snmp.h>
\r
84 #include "netif/etharp.h"
\r
86 /* Define those to better describe your network interface. */
\r
90 #define netifMAX_MTU 1500
\r
94 struct eth_addr *ethaddr;
\r
95 /* Add whatever per-interface state that is needed here. */
\r
99 * Place received packet in a pbuf and send a message to the tcpip task to let
\r
100 * it know new data has arrived.
\r
102 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
\r
105 * Copy the received data into a pbuf.
\r
107 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
\r
110 * Send data from a pbuf to the hardware.
\r
112 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
115 * Perform any hardware and/or driver initialisation necessary.
\r
117 static void prvLowLevelInit( struct netif *pxNetIf );
\r
120 * Query the computer the simulation is being executed on to find the network
\r
121 * interfaces it has installed.
\r
123 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
126 * Open the network interface. The number of the interface to be opened is set
\r
127 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
129 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
132 * Interrupts cannot truely be simulated using WinPCap. In reality this task
\r
133 * just polls the interface.
\r
135 static void prvInterruptSimulator( void *pvParameters );
\r
138 * Configure the capture filter to allow blocking reads, and to filter out
\r
139 * packets that are not of interest to this demo.
\r
141 static void prvConfigureCaptureBehaviour( void );
\r
143 /*-----------------------------------------------------------*/
\r
145 /* The WinPCap interface being used. */
\r
146 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
148 /* Parameter required for WinPCap API functions. */
\r
149 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
151 /* The network interface that was opened. */
\r
152 static struct netif *pxlwIPNetIf = NULL;
\r
154 /*-----------------------------------------------------------*/
\r
157 * In this function, the hardware should be initialized.
\r
158 * Called from ethernetif_init().
\r
160 * @param pxNetIf the already initialized lwip network interface structure
\r
161 * for this ethernetif.
\r
163 static void prvLowLevelInit( struct netif *pxNetIf )
\r
165 pcap_if_t *pxAllNetworkInterfaces;
\r
167 /* set MAC hardware address length */
\r
168 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
170 /* set MAC hardware address */
\r
171 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
172 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
173 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
174 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
175 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
176 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
178 /* device capabilities */
\r
179 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
\r
180 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
182 /* Query the computer the simulation is being executed on to find the
\r
183 network interfaces it has installed. */
\r
184 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
186 /* Open the network interface. The number of the interface to be opened is
\r
187 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
188 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
189 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
190 the interface could not be opened. */
\r
191 if( pxAllNetworkInterfaces != NULL )
\r
193 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
196 /* Remember which interface was opened as it is used in the interrupt
\r
198 pxlwIPNetIf = pxNetIf;
\r
202 * This function should do the actual transmission of the packet. The packet is
\r
203 * contained in the pbuf that is passed to the function. This pbuf
\r
204 * might be chained.
\r
206 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
207 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
208 * @return ERR_OK if the packet could be sent
\r
209 * an err_t value if the packet couldn't be sent
\r
211 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
212 * strange results. You might consider waiting for space in the DMA queue
\r
213 * to become availale since the stack doesn't retry to send a packet
\r
214 * dropped because of memory failure (except for the TCP timers).
\r
217 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
220 /* This is taken from lwIP example code and therefore does not conform
\r
221 to the FreeRTOS coding standard. */
\r
224 static unsigned char ucBuffer[ 1520 ];
\r
225 unsigned char *pucBuffer = ucBuffer;
\r
226 unsigned char *pucChar;
\r
227 struct eth_hdr *pxHeader;
\r
228 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
229 err_t xReturn = ERR_OK;
\r
233 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
234 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
237 /* Initiate transfer. */
\r
238 if( p->len == p->tot_len )
\r
240 /* No pbuf chain, don't have to copy -> faster. */
\r
241 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
245 /* pbuf chain, copy into contiguous ucBuffer. */
\r
246 if( p->tot_len >= sizeof( ucBuffer ) )
\r
248 LINK_STATS_INC( link.lenerr );
\r
249 LINK_STATS_INC( link.drop );
\r
250 snmp_inc_ifoutdiscards( pxNetIf );
\r
255 pucChar = ucBuffer;
\r
257 for( q = p; q != NULL; q = q->next )
\r
259 /* Send the data from the pbuf to the interface, one pbuf at a
\r
260 time. The size of the data in each pbuf is kept in the ->len
\r
262 /* send data from(q->payload, q->len); */
\r
263 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
266 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
267 pucChar += q->len - ETH_PAD_SIZE;
\r
271 memcpy( pucChar, q->payload, q->len );
\r
278 if( xReturn == ERR_OK )
\r
280 /* signal that packet should be sent */
\r
281 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
\r
283 LINK_STATS_INC( link.memerr );
\r
284 LINK_STATS_INC( link.drop );
\r
285 snmp_inc_ifoutdiscards( pxNetIf );
\r
290 LINK_STATS_INC( link.xmit );
\r
291 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
292 pxHeader = ( struct eth_hdr * )p->payload;
\r
294 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
296 /* broadcast or multicast packet*/
\r
297 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
301 /* unicast packet */
\r
302 snmp_inc_ifoutucastpkts( pxNetIf );
\r
311 * Should allocate a pbuf and transfer the bytes of the incoming
\r
312 * packet from the interface into the pbuf.
\r
314 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
315 * @return a pbuf filled with the received packet (including MAC header)
\r
316 * NULL on memory error
\r
318 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
\r
320 struct pbuf *p = NULL, *q;
\r
322 if( lDataLength > 0 )
\r
325 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
328 /* We allocate a pbuf chain of pbufs from the pool. */
\r
329 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
\r
334 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
337 /* We iterate over the pbuf chain until we have read the entire
\r
338 * packet into the pbuf. */
\r
340 for( q = p; q != NULL; q = q->next )
\r
342 /* Read enough bytes to fill this pbuf in the chain. The
\r
343 * available data in the pbuf is given by the q->len
\r
345 * This does not necessarily have to be a memcpy, you can also preallocate
\r
346 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
347 * actually received size. In this case, ensure the usTotalLength member of the
\r
348 * pbuf is the sum of the chained pbuf len members.
\r
350 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
\r
351 lDataLength += q->len;
\r
355 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
358 LINK_STATS_INC( link.recv );
\r
366 * This function should be called when a packet is ready to be read
\r
367 * from the interface. It uses the function prvLowLevelInput() that
\r
368 * should handle the actual reception of bytes from the network
\r
369 * interface. Then the type of the received packet is determined and
\r
370 * the appropriate input function is called.
\r
372 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
374 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
\r
376 /* This is taken from lwIP example code and therefore does not conform
\r
377 to the FreeRTOS coding standard. */
\r
379 struct eth_hdr *pxHeader;
\r
382 /* move received packet into a new pbuf */
\r
383 p = prvLowLevelInput( pucInputData, lInputLength );
\r
385 /* no packet could be read, silently ignore this */
\r
388 /* points to packet payload, which starts with an Ethernet header */
\r
389 pxHeader = p->payload;
\r
391 switch( htons( pxHeader->type ) )
\r
393 /* IP or ARP packet? */
\r
396 /* full packet send to tcpip_thread to process */
\r
397 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
\r
399 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
414 * Should be called at the beginning of the program to set up the
\r
415 * network interface. It calls the function prvLowLevelInit() to do the
\r
416 * actual setup of the hardware.
\r
418 * This function should be passed as a parameter to netif_add().
\r
420 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
421 * @return ERR_OK if the loopif is initialized
\r
422 * ERR_MEM if private data couldn't be allocated
\r
423 * any other err_t on error
\r
425 err_t ethernetif_init( struct netif *pxNetIf )
\r
427 err_t xReturn = ERR_OK;
\r
429 /* This is taken from lwIP example code and therefore does not conform
\r
430 to the FreeRTOS coding standard. */
\r
432 struct xEthernetIf *pxEthernetIf;
\r
434 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
436 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
437 if( pxEthernetIf == NULL )
\r
439 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
444 #if LWIP_NETIF_HOSTNAME
\r
446 /* Initialize interface hostname */
\r
447 pxNetIf->hostname = "lwip";
\r
449 #endif /* LWIP_NETIF_HOSTNAME */
\r
451 pxNetIf->state = pxEthernetIf;
\r
452 pxNetIf->name[ 0 ] = IFNAME0;
\r
453 pxNetIf->name[ 1 ] = IFNAME1;
\r
455 /* We directly use etharp_output() here to save a function call.
\r
456 * You can instead declare your own function an call etharp_output()
\r
457 * from it if you have to do some checks before sending (e.g. if link
\r
458 * is available...) */
\r
459 pxNetIf->output = etharp_output;
\r
460 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
461 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
462 pxNetIf->mtu = netifMAX_MTU;
\r
463 pxNetIf->linkoutput = prvLowLevelOutput;
\r
465 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
467 /* initialize the hardware */
\r
468 prvLowLevelInit( pxNetIf );
\r
470 /* Was an interface opened? */
\r
471 if( pxOpenedInterfaceHandle == NULL )
\r
473 /* Probably an invalid adapter number was defined in
\r
474 FreeRTOSConfig.h. */
\r
476 configASSERT( pxOpenedInterfaceHandle );
\r
482 /*-----------------------------------------------------------*/
\r
484 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
486 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
487 long lInterfaceNumber = 1;
\r
489 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
491 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
492 pxAllNetworkInterfaces = NULL;
\r
495 if( pxAllNetworkInterfaces != NULL )
\r
497 /* Print out the list of network interfaces. The first in the list
\r
498 is interface '1', not interface '0'. */
\r
499 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
501 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
503 if( xInterface->description != NULL )
\r
505 printf( " (%s)\r\n", xInterface->description );
\r
509 printf( " (No description available)\r\n") ;
\r
512 lInterfaceNumber++;
\r
516 if( lInterfaceNumber == 1 )
\r
518 /* The interface number was never incremented, so the above for() loop
\r
519 did not execute meaning no interfaces were found. */
\r
520 printf( " \r\nNo network interfaces were found.\r\n" );
\r
521 pxAllNetworkInterfaces = NULL;
\r
524 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
525 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
527 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
529 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
531 if( pxAllNetworkInterfaces != NULL )
\r
533 /* Free the device list, as no devices are going to be opened. */
\r
534 pcap_freealldevs( pxAllNetworkInterfaces );
\r
535 pxAllNetworkInterfaces = NULL;
\r
539 return pxAllNetworkInterfaces;
\r
541 /*-----------------------------------------------------------*/
\r
543 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
545 pcap_if_t *xInterface;
\r
548 /* Walk the list of devices until the selected device is located. */
\r
549 xInterface = pxAllNetworkInterfaces;
\r
550 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
552 xInterface = xInterface->next;
\r
555 /* Open the selected interface. */
\r
556 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
557 netifMAX_MTU, /* The size of the packet to capture. */
\r
558 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
559 IP address is going to be "simulated", and
\r
560 not be the real MAC and IP address. This allows
\r
561 trafic to the simulated IP address to be routed
\r
562 to uIP, and trafic to the real IP address to be
\r
563 routed to the Windows TCP/IP stack. */
\r
564 0L, /* The read time out. This is going to block
\r
565 until data is available. */
\r
566 NULL, /* No authentication is required as this is
\r
567 not a remote capture session. */
\r
571 if ( pxOpenedInterfaceHandle == NULL )
\r
573 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
577 /* Configure the capture filter to allow blocking reads, and to filter
\r
578 out packets that are not of interest to this demo. */
\r
579 prvConfigureCaptureBehaviour();
\r
582 /* The device list is no longer required. */
\r
583 pcap_freealldevs( pxAllNetworkInterfaces );
\r
585 /*-----------------------------------------------------------*/
\r
587 static void prvInterruptSimulator( void *pvParameters )
\r
589 static struct pcap_pkthdr *pxHeader;
\r
590 const unsigned char *pucPacketData;
\r
591 extern xQueueHandle xEMACEventQueue;
\r
594 /* Just to kill the compiler warning. */
\r
595 ( void ) pvParameters;
\r
599 /* Get the next packet. */
\r
600 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
603 if( pxlwIPNetIf != NULL )
\r
605 prvEthernetInput( pucPacketData, pxHeader->len );
\r
610 /* There is no real way of simulating an interrupt.
\r
611 Make sure other tasks can run. */
\r
616 /*-----------------------------------------------------------*/
\r
618 static void prvConfigureCaptureBehaviour( void )
\r
620 struct bpf_program xFilterCode;
\r
621 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
622 unsigned long ulNetMask;
\r
624 /* Unblock a read as soon as anything is received. */
\r
625 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
627 /* Allow blocking. */
\r
628 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
630 /* Set up a filter so only the packets of interest are passed to the lwIP
\r
631 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
632 confuse this with an error message. */
\r
633 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
635 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
637 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
639 printf( "\r\nThe packet filter string is invalid\r\n" );
\r
643 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
645 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
649 /* Create a task that simulates an interrupt in a real system. This will
\r
650 block waiting for packets, then send a message to the uIP task when data
\r
652 xTaskCreate( prvInterruptSimulator, ( signed char * ) "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r