2 FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* WinPCap includes. */
\r
70 /* FreeRTOS includes. */
\r
71 #include "FreeRTOS.h"
\r
75 /* lwIP includes. */
\r
76 #include "lwip/opt.h"
\r
77 #include "lwip/def.h"
\r
78 #include "lwip/mem.h"
\r
79 #include "lwip/pbuf.h"
\r
80 #include "lwip/sys.h"
\r
81 #include <lwip/stats.h>
\r
82 #include <lwip/snmp.h>
\r
83 #include "netif/etharp.h"
\r
85 /* Define those to better describe your network interface. */
\r
89 #define netifMAX_MTU 1500
\r
93 struct eth_addr *ethaddr;
\r
94 /* Add whatever per-interface state that is needed here. */
\r
98 * Place received packet in a pbuf and send a message to the tcpip task to let
\r
99 * it know new data has arrived.
\r
101 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
\r
104 * Copy the received data into a pbuf.
\r
106 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
\r
109 * Send data from a pbuf to the hardware.
\r
111 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
114 * Perform any hardware and/or driver initialisation necessary.
\r
116 static void prvLowLevelInit( struct netif *pxNetIf );
\r
119 * Query the computer the simulation is being executed on to find the network
\r
120 * interfaces it has installed.
\r
122 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
125 * Open the network interface. The number of the interface to be opened is set
\r
126 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
128 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
131 * Interrupts cannot truely be simulated using WinPCap. In reality this task
\r
132 * just polls the interface.
\r
134 static void prvInterruptSimulator( void *pvParameters );
\r
137 * Configure the capture filter to allow blocking reads, and to filter out
\r
138 * packets that are not of interest to this demo.
\r
140 static void prvConfigureCaptureBehaviour( void );
\r
142 /*-----------------------------------------------------------*/
\r
144 /* The WinPCap interface being used. */
\r
145 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
147 /* Parameter required for WinPCap API functions. */
\r
148 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
150 /* The network interface that was opened. */
\r
151 static struct netif *pxlwIPNetIf = NULL;
\r
153 /*-----------------------------------------------------------*/
\r
156 * In this function, the hardware should be initialized.
\r
157 * Called from ethernetif_init().
\r
159 * @param pxNetIf the already initialized lwip network interface structure
\r
160 * for this ethernetif.
\r
162 static void prvLowLevelInit( struct netif *pxNetIf )
\r
164 pcap_if_t *pxAllNetworkInterfaces;
\r
166 /* set MAC hardware address length */
\r
167 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
169 /* set MAC hardware address */
\r
170 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
171 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
172 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
173 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
174 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
175 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
177 /* device capabilities */
\r
178 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
\r
179 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
181 /* Query the computer the simulation is being executed on to find the
\r
182 network interfaces it has installed. */
\r
183 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
185 /* Open the network interface. The number of the interface to be opened is
\r
186 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
187 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
188 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
189 the interface could not be opened. */
\r
190 if( pxAllNetworkInterfaces != NULL )
\r
192 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
195 /* Remember which interface was opened as it is used in the interrupt
\r
197 pxlwIPNetIf = pxNetIf;
\r
201 * This function should do the actual transmission of the packet. The packet is
\r
202 * contained in the pbuf that is passed to the function. This pbuf
\r
203 * might be chained.
\r
205 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
206 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
207 * @return ERR_OK if the packet could be sent
\r
208 * an err_t value if the packet couldn't be sent
\r
210 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
211 * strange results. You might consider waiting for space in the DMA queue
\r
212 * to become availale since the stack doesn't retry to send a packet
\r
213 * dropped because of memory failure (except for the TCP timers).
\r
216 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
219 /* This is taken from lwIP example code and therefore does not conform
\r
220 to the FreeRTOS coding standard. */
\r
223 static unsigned char ucBuffer[ 1520 ];
\r
224 unsigned char *pucBuffer = ucBuffer;
\r
225 unsigned char *pucChar;
\r
226 struct eth_hdr *pxHeader;
\r
227 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
228 err_t xReturn = ERR_OK;
\r
232 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
233 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
236 /* Initiate transfer. */
\r
237 if( p->len == p->tot_len )
\r
239 /* No pbuf chain, don't have to copy -> faster. */
\r
240 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
244 /* pbuf chain, copy into contiguous ucBuffer. */
\r
245 if( p->tot_len >= sizeof( ucBuffer ) )
\r
247 LINK_STATS_INC( link.lenerr );
\r
248 LINK_STATS_INC( link.drop );
\r
249 snmp_inc_ifoutdiscards( pxNetIf );
\r
254 pucChar = ucBuffer;
\r
256 for( q = p; q != NULL; q = q->next )
\r
258 /* Send the data from the pbuf to the interface, one pbuf at a
\r
259 time. The size of the data in each pbuf is kept in the ->len
\r
261 /* send data from(q->payload, q->len); */
\r
262 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
265 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
266 pucChar += q->len - ETH_PAD_SIZE;
\r
270 memcpy( pucChar, q->payload, q->len );
\r
277 if( xReturn == ERR_OK )
\r
279 /* signal that packet should be sent */
\r
280 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
\r
282 LINK_STATS_INC( link.memerr );
\r
283 LINK_STATS_INC( link.drop );
\r
284 snmp_inc_ifoutdiscards( pxNetIf );
\r
289 LINK_STATS_INC( link.xmit );
\r
290 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
291 pxHeader = ( struct eth_hdr * )p->payload;
\r
293 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
295 /* broadcast or multicast packet*/
\r
296 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
300 /* unicast packet */
\r
301 snmp_inc_ifoutucastpkts( pxNetIf );
\r
310 * Should allocate a pbuf and transfer the bytes of the incoming
\r
311 * packet from the interface into the pbuf.
\r
313 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
314 * @return a pbuf filled with the received packet (including MAC header)
\r
315 * NULL on memory error
\r
317 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
\r
319 struct pbuf *p = NULL, *q;
\r
321 if( lDataLength > 0 )
\r
324 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
327 /* We allocate a pbuf chain of pbufs from the pool. */
\r
328 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
\r
333 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
336 /* We iterate over the pbuf chain until we have read the entire
\r
337 * packet into the pbuf. */
\r
339 for( q = p; q != NULL; q = q->next )
\r
341 /* Read enough bytes to fill this pbuf in the chain. The
\r
342 * available data in the pbuf is given by the q->len
\r
344 * This does not necessarily have to be a memcpy, you can also preallocate
\r
345 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
346 * actually received size. In this case, ensure the usTotalLength member of the
\r
347 * pbuf is the sum of the chained pbuf len members.
\r
349 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
\r
350 lDataLength += q->len;
\r
354 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
357 LINK_STATS_INC( link.recv );
\r
365 * This function should be called when a packet is ready to be read
\r
366 * from the interface. It uses the function prvLowLevelInput() that
\r
367 * should handle the actual reception of bytes from the network
\r
368 * interface. Then the type of the received packet is determined and
\r
369 * the appropriate input function is called.
\r
371 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
373 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
\r
375 /* This is taken from lwIP example code and therefore does not conform
\r
376 to the FreeRTOS coding standard. */
\r
378 struct eth_hdr *pxHeader;
\r
381 /* move received packet into a new pbuf */
\r
382 p = prvLowLevelInput( pucInputData, lInputLength );
\r
384 /* no packet could be read, silently ignore this */
\r
387 /* points to packet payload, which starts with an Ethernet header */
\r
388 pxHeader = p->payload;
\r
390 switch( htons( pxHeader->type ) )
\r
392 /* IP or ARP packet? */
\r
395 /* full packet send to tcpip_thread to process */
\r
396 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
\r
398 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
413 * Should be called at the beginning of the program to set up the
\r
414 * network interface. It calls the function prvLowLevelInit() to do the
\r
415 * actual setup of the hardware.
\r
417 * This function should be passed as a parameter to netif_add().
\r
419 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
420 * @return ERR_OK if the loopif is initialized
\r
421 * ERR_MEM if private data couldn't be allocated
\r
422 * any other err_t on error
\r
424 err_t ethernetif_init( struct netif *pxNetIf )
\r
426 err_t xReturn = ERR_OK;
\r
428 /* This is taken from lwIP example code and therefore does not conform
\r
429 to the FreeRTOS coding standard. */
\r
431 struct xEthernetIf *pxEthernetIf;
\r
433 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
435 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
436 if( pxEthernetIf == NULL )
\r
438 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
443 #if LWIP_NETIF_HOSTNAME
\r
445 /* Initialize interface hostname */
\r
446 pxNetIf->hostname = "lwip";
\r
448 #endif /* LWIP_NETIF_HOSTNAME */
\r
450 pxNetIf->state = pxEthernetIf;
\r
451 pxNetIf->name[ 0 ] = IFNAME0;
\r
452 pxNetIf->name[ 1 ] = IFNAME1;
\r
454 /* We directly use etharp_output() here to save a function call.
\r
455 * You can instead declare your own function an call etharp_output()
\r
456 * from it if you have to do some checks before sending (e.g. if link
\r
457 * is available...) */
\r
458 pxNetIf->output = etharp_output;
\r
459 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
460 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
461 pxNetIf->mtu = netifMAX_MTU;
\r
462 pxNetIf->linkoutput = prvLowLevelOutput;
\r
464 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
466 /* initialize the hardware */
\r
467 prvLowLevelInit( pxNetIf );
\r
469 /* Was an interface opened? */
\r
470 if( pxOpenedInterfaceHandle == NULL )
\r
472 /* Probably an invalid adapter number was defined in
\r
473 FreeRTOSConfig.h. */
\r
475 configASSERT( pxOpenedInterfaceHandle );
\r
481 /*-----------------------------------------------------------*/
\r
483 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
485 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
486 long lInterfaceNumber = 1;
\r
488 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
490 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
491 pxAllNetworkInterfaces = NULL;
\r
494 if( pxAllNetworkInterfaces != NULL )
\r
496 /* Print out the list of network interfaces. The first in the list
\r
497 is interface '1', not interface '0'. */
\r
498 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
500 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
502 if( xInterface->description != NULL )
\r
504 printf( " (%s)\r\n", xInterface->description );
\r
508 printf( " (No description available)\r\n") ;
\r
511 lInterfaceNumber++;
\r
515 if( lInterfaceNumber == 1 )
\r
517 /* The interface number was never incremented, so the above for() loop
\r
518 did not execute meaning no interfaces were found. */
\r
519 printf( " \r\nNo network interfaces were found.\r\n" );
\r
520 pxAllNetworkInterfaces = NULL;
\r
523 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
524 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
526 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
528 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
530 if( pxAllNetworkInterfaces != NULL )
\r
532 /* Free the device list, as no devices are going to be opened. */
\r
533 pcap_freealldevs( pxAllNetworkInterfaces );
\r
534 pxAllNetworkInterfaces = NULL;
\r
538 return pxAllNetworkInterfaces;
\r
540 /*-----------------------------------------------------------*/
\r
542 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
544 pcap_if_t *xInterface;
\r
547 /* Walk the list of devices until the selected device is located. */
\r
548 xInterface = pxAllNetworkInterfaces;
\r
549 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
551 xInterface = xInterface->next;
\r
554 /* Open the selected interface. */
\r
555 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
556 netifMAX_MTU, /* The size of the packet to capture. */
\r
557 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
558 IP address is going to be "simulated", and
\r
559 not be the real MAC and IP address. This allows
\r
560 trafic to the simulated IP address to be routed
\r
561 to uIP, and trafic to the real IP address to be
\r
562 routed to the Windows TCP/IP stack. */
\r
563 0L, /* The read time out. This is going to block
\r
564 until data is available. */
\r
565 NULL, /* No authentication is required as this is
\r
566 not a remote capture session. */
\r
570 if ( pxOpenedInterfaceHandle == NULL )
\r
572 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
576 /* Configure the capture filter to allow blocking reads, and to filter
\r
577 out packets that are not of interest to this demo. */
\r
578 prvConfigureCaptureBehaviour();
\r
581 /* The device list is no longer required. */
\r
582 pcap_freealldevs( pxAllNetworkInterfaces );
\r
584 /*-----------------------------------------------------------*/
\r
586 static void prvInterruptSimulator( void *pvParameters )
\r
588 static struct pcap_pkthdr *pxHeader;
\r
589 const unsigned char *pucPacketData;
\r
590 extern QueueHandle_t xEMACEventQueue;
\r
593 /* Just to kill the compiler warning. */
\r
594 ( void ) pvParameters;
\r
598 /* Get the next packet. */
\r
599 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
602 if( pxlwIPNetIf != NULL )
\r
604 prvEthernetInput( pucPacketData, pxHeader->len );
\r
609 /* There is no real way of simulating an interrupt.
\r
610 Make sure other tasks can run. */
\r
615 /*-----------------------------------------------------------*/
\r
617 static void prvConfigureCaptureBehaviour( void )
\r
619 struct bpf_program xFilterCode;
\r
620 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
621 unsigned long ulNetMask;
\r
623 /* Unblock a read as soon as anything is received. */
\r
624 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
626 /* Allow blocking. */
\r
627 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
629 /* Set up a filter so only the packets of interest are passed to the lwIP
\r
630 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
631 confuse this with an error message. */
\r
632 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
634 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
636 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
638 printf( "\r\nThe packet filter string is invalid\r\n" );
\r
642 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
644 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
648 /* Create a task that simulates an interrupt in a real system. This will
\r
649 block waiting for packets, then send a message to the uIP task when data
\r
651 xTaskCreate( prvInterruptSimulator, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r