2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
70 /* WinPCap includes. */
74 /* FreeRTOS includes. */
83 #include "lwip/pbuf.h"
85 #include <lwip/stats.h>
86 #include <lwip/snmp.h>
87 #include "netif/etharp.h"
89 /* Define those to better describe your network interface. */
93 #define netifMAX_MTU 1500
97 struct eth_addr *ethaddr;
98 /* Add whatever per-interface state that is needed here. */
102 * Place received packet in a pbuf and send a message to the tcpip task to let
103 * it know new data has arrived.
105 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
108 * Copy the received data into a pbuf.
110 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
113 * Send data from a pbuf to the hardware.
115 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
118 * Perform any hardware and/or driver initialisation necessary.
120 static void prvLowLevelInit( struct netif *pxNetIf );
123 * Query the computer the simulation is being executed on to find the network
124 * interfaces it has installed.
126 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
129 * Open the network interface. The number of the interface to be opened is set
130 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
132 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
135 * Interrupts cannot truely be simulated using WinPCap. In reality this task
136 * just polls the interface.
138 static void prvInterruptSimulator( void *pvParameters );
141 * Configure the capture filter to allow blocking reads, and to filter out
142 * packets that are not of interest to this demo.
144 static void prvConfigureCaptureBehaviour( void );
146 /*-----------------------------------------------------------*/
148 /* The WinPCap interface being used. */
149 static pcap_t *pxOpenedInterfaceHandle = NULL;
151 /* Parameter required for WinPCap API functions. */
152 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
154 /* The network interface that was opened. */
155 static struct netif *pxlwIPNetIf = NULL;
157 /*-----------------------------------------------------------*/
160 * In this function, the hardware should be initialized.
161 * Called from ethernetif_init().
163 * @param pxNetIf the already initialized lwip network interface structure
164 * for this ethernetif.
166 static void prvLowLevelInit( struct netif *pxNetIf )
168 pcap_if_t *pxAllNetworkInterfaces;
170 /* set MAC hardware address length */
171 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
173 /* set MAC hardware address */
174 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
175 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
176 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
177 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
178 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
179 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
181 /* device capabilities */
182 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
183 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
185 /* Query the computer the simulation is being executed on to find the
186 network interfaces it has installed. */
187 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
189 /* Open the network interface. The number of the interface to be opened is
190 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
191 Calling this function will set the pxOpenedInterfaceHandle variable. If,
192 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
193 the interface could not be opened. */
194 if( pxAllNetworkInterfaces != NULL )
196 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
199 /* Remember which interface was opened as it is used in the interrupt
201 pxlwIPNetIf = pxNetIf;
205 * This function should do the actual transmission of the packet. The packet is
206 * contained in the pbuf that is passed to the function. This pbuf
209 * @param pxNetIf the lwip network interface structure for this ethernetif
210 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
211 * @return ERR_OK if the packet could be sent
212 * an err_t value if the packet couldn't be sent
214 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
215 * strange results. You might consider waiting for space in the DMA queue
216 * to become availale since the stack doesn't retry to send a packet
217 * dropped because of memory failure (except for the TCP timers).
220 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
223 /* This is taken from lwIP example code and therefore does not conform
224 to the FreeRTOS coding standard. */
227 static unsigned char ucBuffer[ 1520 ];
228 unsigned char *pucBuffer = ucBuffer;
229 unsigned char *pucChar;
230 struct eth_hdr *pxHeader;
231 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
232 err_t xReturn = ERR_OK;
236 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
237 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
240 /* Initiate transfer. */
241 if( p->len == p->tot_len )
243 /* No pbuf chain, don't have to copy -> faster. */
244 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
248 /* pbuf chain, copy into contiguous ucBuffer. */
249 if( p->tot_len >= sizeof( ucBuffer ) )
251 LINK_STATS_INC( link.lenerr );
252 LINK_STATS_INC( link.drop );
253 snmp_inc_ifoutdiscards( pxNetIf );
260 for( q = p; q != NULL; q = q->next )
262 /* Send the data from the pbuf to the interface, one pbuf at a
263 time. The size of the data in each pbuf is kept in the ->len
265 /* send data from(q->payload, q->len); */
266 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 ) );
269 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
270 pucChar += q->len - ETH_PAD_SIZE;
274 memcpy( pucChar, q->payload, q->len );
281 if( xReturn == ERR_OK )
283 /* signal that packet should be sent */
284 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
286 LINK_STATS_INC( link.memerr );
287 LINK_STATS_INC( link.drop );
288 snmp_inc_ifoutdiscards( pxNetIf );
293 LINK_STATS_INC( link.xmit );
294 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
295 pxHeader = ( struct eth_hdr * )p->payload;
297 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
299 /* broadcast or multicast packet*/
300 snmp_inc_ifoutnucastpkts( pxNetIf );
305 snmp_inc_ifoutucastpkts( pxNetIf );
314 * Should allocate a pbuf and transfer the bytes of the incoming
315 * packet from the interface into the pbuf.
317 * @param pxNetIf the lwip network interface structure for this ethernetif
318 * @return a pbuf filled with the received packet (including MAC header)
319 * NULL on memory error
321 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
323 struct pbuf *p = NULL, *q;
325 if( lDataLength > 0 )
328 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
331 /* We allocate a pbuf chain of pbufs from the pool. */
332 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
337 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
340 /* We iterate over the pbuf chain until we have read the entire
341 * packet into the pbuf. */
343 for( q = p; q != NULL; q = q->next )
345 /* Read enough bytes to fill this pbuf in the chain. The
346 * available data in the pbuf is given by the q->len
348 * This does not necessarily have to be a memcpy, you can also preallocate
349 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
350 * actually received size. In this case, ensure the usTotalLength member of the
351 * pbuf is the sum of the chained pbuf len members.
353 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
354 lDataLength += q->len;
358 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
361 LINK_STATS_INC( link.recv );
369 * This function should be called when a packet is ready to be read
370 * from the interface. It uses the function prvLowLevelInput() that
371 * should handle the actual reception of bytes from the network
372 * interface. Then the type of the received packet is determined and
373 * the appropriate input function is called.
375 * @param pxNetIf the lwip network interface structure for this ethernetif
377 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
379 /* This is taken from lwIP example code and therefore does not conform
380 to the FreeRTOS coding standard. */
382 struct eth_hdr *pxHeader;
385 /* move received packet into a new pbuf */
386 p = prvLowLevelInput( pucInputData, lInputLength );
388 /* no packet could be read, silently ignore this */
391 /* points to packet payload, which starts with an Ethernet header */
392 pxHeader = p->payload;
394 switch( htons( pxHeader->type ) )
396 /* IP or ARP packet? */
399 /* full packet send to tcpip_thread to process */
400 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
402 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
417 * Should be called at the beginning of the program to set up the
418 * network interface. It calls the function prvLowLevelInit() to do the
419 * actual setup of the hardware.
421 * This function should be passed as a parameter to netif_add().
423 * @param pxNetIf the lwip network interface structure for this ethernetif
424 * @return ERR_OK if the loopif is initialized
425 * ERR_MEM if private data couldn't be allocated
426 * any other err_t on error
428 err_t ethernetif_init( struct netif *pxNetIf )
430 err_t xReturn = ERR_OK;
432 /* This is taken from lwIP example code and therefore does not conform
433 to the FreeRTOS coding standard. */
435 struct xEthernetIf *pxEthernetIf;
437 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
439 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
440 if( pxEthernetIf == NULL )
442 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
447 #if LWIP_NETIF_HOSTNAME
449 /* Initialize interface hostname */
450 pxNetIf->hostname = "lwip";
452 #endif /* LWIP_NETIF_HOSTNAME */
454 pxNetIf->state = pxEthernetIf;
455 pxNetIf->name[ 0 ] = IFNAME0;
456 pxNetIf->name[ 1 ] = IFNAME1;
458 /* We directly use etharp_output() here to save a function call.
459 * You can instead declare your own function an call etharp_output()
460 * from it if you have to do some checks before sending (e.g. if link
461 * is available...) */
462 pxNetIf->output = etharp_output;
463 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
464 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
465 pxNetIf->mtu = netifMAX_MTU;
466 pxNetIf->linkoutput = prvLowLevelOutput;
468 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
470 /* initialize the hardware */
471 prvLowLevelInit( pxNetIf );
473 /* Was an interface opened? */
474 if( pxOpenedInterfaceHandle == NULL )
476 /* Probably an invalid adapter number was defined in
479 configASSERT( pxOpenedInterfaceHandle );
485 /*-----------------------------------------------------------*/
487 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
489 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
490 long lInterfaceNumber = 1;
492 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
494 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
495 pxAllNetworkInterfaces = NULL;
498 if( pxAllNetworkInterfaces != NULL )
500 /* Print out the list of network interfaces. The first in the list
501 is interface '1', not interface '0'. */
502 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
504 printf( "%d. %s", lInterfaceNumber, xInterface->name );
506 if( xInterface->description != NULL )
508 printf( " (%s)\r\n", xInterface->description );
512 printf( " (No description available)\r\n") ;
519 if( lInterfaceNumber == 1 )
521 /* The interface number was never incremented, so the above for() loop
522 did not execute meaning no interfaces were found. */
523 printf( " \r\nNo network interfaces were found.\r\n" );
524 pxAllNetworkInterfaces = NULL;
527 printf( "\r\nThe interface that will be opened is set by configNETWORK_INTERFACE_TO_USE which should be defined in FreeRTOSConfig.h\r\n" );
528 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
530 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
532 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
534 if( pxAllNetworkInterfaces != NULL )
536 /* Free the device list, as no devices are going to be opened. */
537 pcap_freealldevs( pxAllNetworkInterfaces );
538 pxAllNetworkInterfaces = NULL;
542 return pxAllNetworkInterfaces;
544 /*-----------------------------------------------------------*/
546 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
548 pcap_if_t *xInterface;
551 /* Walk the list of devices until the selected device is located. */
552 xInterface = pxAllNetworkInterfaces;
553 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
555 xInterface = xInterface->next;
558 /* Open the selected interface. */
559 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
560 netifMAX_MTU, /* The size of the packet to capture. */
561 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
562 IP address is going to be "simulated", and
563 not be the real MAC and IP address. This allows
564 trafic to the simulated IP address to be routed
565 to uIP, and trafic to the real IP address to be
566 routed to the Windows TCP/IP stack. */
567 0L, /* The read time out. This is going to block
568 until data is available. */
569 NULL, /* No authentication is required as this is
570 not a remote capture session. */
574 if ( pxOpenedInterfaceHandle == NULL )
576 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
580 /* Configure the capture filter to allow blocking reads, and to filter
581 out packets that are not of interest to this demo. */
582 prvConfigureCaptureBehaviour();
585 /* The device list is no longer required. */
586 pcap_freealldevs( pxAllNetworkInterfaces );
588 /*-----------------------------------------------------------*/
590 static void prvInterruptSimulator( void *pvParameters )
592 static struct pcap_pkthdr *pxHeader;
593 const unsigned char *pucPacketData;
594 extern QueueHandle_t xEMACEventQueue;
597 /* Just to kill the compiler warning. */
598 ( void ) pvParameters;
602 /* Get the next packet. */
603 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
606 if( pxlwIPNetIf != NULL )
608 prvEthernetInput( pucPacketData, pxHeader->len );
613 /* There is no real way of simulating an interrupt.
614 Make sure other tasks can run. */
619 /*-----------------------------------------------------------*/
621 static void prvConfigureCaptureBehaviour( void )
623 struct bpf_program xFilterCode;
624 const long lMinBytesToCopy = 10L, lBlocking = 1L;
625 unsigned long ulNetMask;
627 /* Unblock a read as soon as anything is received. */
628 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
630 /* Allow blocking. */
631 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
633 /* Set up a filter so only the packets of interest are passed to the lwIP
634 stack. cErrorBuffer is used for convenience to create the string. Don't
635 confuse this with an error message. */
636 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
638 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
640 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
642 printf( "\r\nThe packet filter string is invalid\r\n" );
646 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
648 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
652 /* Create a task that simulates an interrupt in a real system. This will
653 block waiting for packets, then send a message to the uIP task when data
655 xTaskCreate( prvInterruptSimulator, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );