2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* WinPCap includes. */
\r
79 /* FreeRTOS includes. */
\r
80 #include "FreeRTOS.h"
\r
84 /* lwIP includes. */
\r
85 #include "lwip/opt.h"
\r
86 #include "lwip/def.h"
\r
87 #include "lwip/mem.h"
\r
88 #include "lwip/pbuf.h"
\r
89 #include "lwip/sys.h"
\r
90 #include <lwip/stats.h>
\r
91 #include <lwip/snmp.h>
\r
92 #include "netif/etharp.h"
\r
94 /* Define those to better describe your network interface. */
\r
98 #define netifMAX_MTU 1500
\r
102 struct eth_addr *ethaddr;
\r
103 /* Add whatever per-interface state that is needed here. */
\r
107 * Place received packet in a pbuf and send a message to the tcpip task to let
\r
108 * it know new data has arrived.
\r
110 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
\r
113 * Copy the received data into a pbuf.
\r
115 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
\r
118 * Send data from a pbuf to the hardware.
\r
120 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
123 * Perform any hardware and/or driver initialisation necessary.
\r
125 static void prvLowLevelInit( struct netif *pxNetIf );
\r
128 * Query the computer the simulation is being executed on to find the network
\r
129 * interfaces it has installed.
\r
131 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
134 * Open the network interface. The number of the interface to be opened is set
\r
135 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
137 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
140 * Interrupts cannot truely be simulated using WinPCap. In reality this task
\r
141 * just polls the interface.
\r
143 static void prvInterruptSimulator( void *pvParameters );
\r
146 * Configure the capture filter to allow blocking reads, and to filter out
\r
147 * packets that are not of interest to this demo.
\r
149 static void prvConfigureCaptureBehaviour( void );
\r
151 /*-----------------------------------------------------------*/
\r
153 /* The WinPCap interface being used. */
\r
154 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
156 /* Parameter required for WinPCap API functions. */
\r
157 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
159 /* The network interface that was opened. */
\r
160 static struct netif *pxlwIPNetIf = NULL;
\r
162 /*-----------------------------------------------------------*/
\r
165 * In this function, the hardware should be initialized.
\r
166 * Called from ethernetif_init().
\r
168 * @param pxNetIf the already initialized lwip network interface structure
\r
169 * for this ethernetif.
\r
171 static void prvLowLevelInit( struct netif *pxNetIf )
\r
173 pcap_if_t *pxAllNetworkInterfaces;
\r
175 /* set MAC hardware address length */
\r
176 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
178 /* set MAC hardware address */
\r
179 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
180 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
181 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
182 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
183 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
184 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
186 /* device capabilities */
\r
187 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
\r
188 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
190 /* Query the computer the simulation is being executed on to find the
\r
191 network interfaces it has installed. */
\r
192 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
194 /* Open the network interface. The number of the interface to be opened is
\r
195 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
196 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
197 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
198 the interface could not be opened. */
\r
199 if( pxAllNetworkInterfaces != NULL )
\r
201 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
204 /* Remember which interface was opened as it is used in the interrupt
\r
206 pxlwIPNetIf = pxNetIf;
\r
210 * This function should do the actual transmission of the packet. The packet is
\r
211 * contained in the pbuf that is passed to the function. This pbuf
\r
212 * might be chained.
\r
214 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
215 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
216 * @return ERR_OK if the packet could be sent
\r
217 * an err_t value if the packet couldn't be sent
\r
219 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
220 * strange results. You might consider waiting for space in the DMA queue
\r
221 * to become availale since the stack doesn't retry to send a packet
\r
222 * dropped because of memory failure (except for the TCP timers).
\r
225 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
228 /* This is taken from lwIP example code and therefore does not conform
\r
229 to the FreeRTOS coding standard. */
\r
232 static unsigned char ucBuffer[ 1520 ];
\r
233 unsigned char *pucBuffer = ucBuffer;
\r
234 unsigned char *pucChar;
\r
235 struct eth_hdr *pxHeader;
\r
236 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
237 err_t xReturn = ERR_OK;
\r
241 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
242 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
245 /* Initiate transfer. */
\r
246 if( p->len == p->tot_len )
\r
248 /* No pbuf chain, don't have to copy -> faster. */
\r
249 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
253 /* pbuf chain, copy into contiguous ucBuffer. */
\r
254 if( p->tot_len >= sizeof( ucBuffer ) )
\r
256 LINK_STATS_INC( link.lenerr );
\r
257 LINK_STATS_INC( link.drop );
\r
258 snmp_inc_ifoutdiscards( pxNetIf );
\r
263 pucChar = ucBuffer;
\r
265 for( q = p; q != NULL; q = q->next )
\r
267 /* Send the data from the pbuf to the interface, one pbuf at a
\r
268 time. The size of the data in each pbuf is kept in the ->len
\r
270 /* send data from(q->payload, q->len); */
\r
271 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
274 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
275 pucChar += q->len - ETH_PAD_SIZE;
\r
279 memcpy( pucChar, q->payload, q->len );
\r
286 if( xReturn == ERR_OK )
\r
288 /* signal that packet should be sent */
\r
289 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
\r
291 LINK_STATS_INC( link.memerr );
\r
292 LINK_STATS_INC( link.drop );
\r
293 snmp_inc_ifoutdiscards( pxNetIf );
\r
298 LINK_STATS_INC( link.xmit );
\r
299 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
300 pxHeader = ( struct eth_hdr * )p->payload;
\r
302 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
304 /* broadcast or multicast packet*/
\r
305 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
309 /* unicast packet */
\r
310 snmp_inc_ifoutucastpkts( pxNetIf );
\r
319 * Should allocate a pbuf and transfer the bytes of the incoming
\r
320 * packet from the interface into the pbuf.
\r
322 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
323 * @return a pbuf filled with the received packet (including MAC header)
\r
324 * NULL on memory error
\r
326 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
\r
328 struct pbuf *p = NULL, *q;
\r
330 if( lDataLength > 0 )
\r
333 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
336 /* We allocate a pbuf chain of pbufs from the pool. */
\r
337 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
\r
342 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
345 /* We iterate over the pbuf chain until we have read the entire
\r
346 * packet into the pbuf. */
\r
348 for( q = p; q != NULL; q = q->next )
\r
350 /* Read enough bytes to fill this pbuf in the chain. The
\r
351 * available data in the pbuf is given by the q->len
\r
353 * This does not necessarily have to be a memcpy, you can also preallocate
\r
354 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
355 * actually received size. In this case, ensure the usTotalLength member of the
\r
356 * pbuf is the sum of the chained pbuf len members.
\r
358 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
\r
359 lDataLength += q->len;
\r
363 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
366 LINK_STATS_INC( link.recv );
\r
374 * This function should be called when a packet is ready to be read
\r
375 * from the interface. It uses the function prvLowLevelInput() that
\r
376 * should handle the actual reception of bytes from the network
\r
377 * interface. Then the type of the received packet is determined and
\r
378 * the appropriate input function is called.
\r
380 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
382 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
\r
384 /* This is taken from lwIP example code and therefore does not conform
\r
385 to the FreeRTOS coding standard. */
\r
387 struct eth_hdr *pxHeader;
\r
390 /* move received packet into a new pbuf */
\r
391 p = prvLowLevelInput( pucInputData, lInputLength );
\r
393 /* no packet could be read, silently ignore this */
\r
396 /* points to packet payload, which starts with an Ethernet header */
\r
397 pxHeader = p->payload;
\r
399 switch( htons( pxHeader->type ) )
\r
401 /* IP or ARP packet? */
\r
404 /* full packet send to tcpip_thread to process */
\r
405 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
\r
407 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
422 * Should be called at the beginning of the program to set up the
\r
423 * network interface. It calls the function prvLowLevelInit() to do the
\r
424 * actual setup of the hardware.
\r
426 * This function should be passed as a parameter to netif_add().
\r
428 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
429 * @return ERR_OK if the loopif is initialized
\r
430 * ERR_MEM if private data couldn't be allocated
\r
431 * any other err_t on error
\r
433 err_t ethernetif_init( struct netif *pxNetIf )
\r
435 err_t xReturn = ERR_OK;
\r
437 /* This is taken from lwIP example code and therefore does not conform
\r
438 to the FreeRTOS coding standard. */
\r
440 struct xEthernetIf *pxEthernetIf;
\r
442 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
444 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
445 if( pxEthernetIf == NULL )
\r
447 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
452 #if LWIP_NETIF_HOSTNAME
\r
454 /* Initialize interface hostname */
\r
455 pxNetIf->hostname = "lwip";
\r
457 #endif /* LWIP_NETIF_HOSTNAME */
\r
459 pxNetIf->state = pxEthernetIf;
\r
460 pxNetIf->name[ 0 ] = IFNAME0;
\r
461 pxNetIf->name[ 1 ] = IFNAME1;
\r
463 /* We directly use etharp_output() here to save a function call.
\r
464 * You can instead declare your own function an call etharp_output()
\r
465 * from it if you have to do some checks before sending (e.g. if link
\r
466 * is available...) */
\r
467 pxNetIf->output = etharp_output;
\r
468 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
469 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
470 pxNetIf->mtu = netifMAX_MTU;
\r
471 pxNetIf->linkoutput = prvLowLevelOutput;
\r
473 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
475 /* initialize the hardware */
\r
476 prvLowLevelInit( pxNetIf );
\r
478 /* Was an interface opened? */
\r
479 if( pxOpenedInterfaceHandle == NULL )
\r
481 /* Probably an invalid adapter number was defined in
\r
482 FreeRTOSConfig.h. */
\r
484 configASSERT( pxOpenedInterfaceHandle );
\r
490 /*-----------------------------------------------------------*/
\r
492 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
494 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
495 long lInterfaceNumber = 1;
\r
497 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
499 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
500 pxAllNetworkInterfaces = NULL;
\r
503 if( pxAllNetworkInterfaces != NULL )
\r
505 /* Print out the list of network interfaces. The first in the list
\r
506 is interface '1', not interface '0'. */
\r
507 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
509 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
511 if( xInterface->description != NULL )
\r
513 printf( " (%s)\r\n", xInterface->description );
\r
517 printf( " (No description available)\r\n") ;
\r
520 lInterfaceNumber++;
\r
524 if( lInterfaceNumber == 1 )
\r
526 /* The interface number was never incremented, so the above for() loop
\r
527 did not execute meaning no interfaces were found. */
\r
528 printf( " \r\nNo network interfaces were found.\r\n" );
\r
529 pxAllNetworkInterfaces = NULL;
\r
532 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
533 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
535 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
537 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
539 if( pxAllNetworkInterfaces != NULL )
\r
541 /* Free the device list, as no devices are going to be opened. */
\r
542 pcap_freealldevs( pxAllNetworkInterfaces );
\r
543 pxAllNetworkInterfaces = NULL;
\r
547 return pxAllNetworkInterfaces;
\r
549 /*-----------------------------------------------------------*/
\r
551 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
553 pcap_if_t *xInterface;
\r
556 /* Walk the list of devices until the selected device is located. */
\r
557 xInterface = pxAllNetworkInterfaces;
\r
558 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
560 xInterface = xInterface->next;
\r
563 /* Open the selected interface. */
\r
564 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
565 netifMAX_MTU, /* The size of the packet to capture. */
\r
566 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
567 IP address is going to be "simulated", and
\r
568 not be the real MAC and IP address. This allows
\r
569 trafic to the simulated IP address to be routed
\r
570 to uIP, and trafic to the real IP address to be
\r
571 routed to the Windows TCP/IP stack. */
\r
572 0L, /* The read time out. This is going to block
\r
573 until data is available. */
\r
574 NULL, /* No authentication is required as this is
\r
575 not a remote capture session. */
\r
579 if ( pxOpenedInterfaceHandle == NULL )
\r
581 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
585 /* Configure the capture filter to allow blocking reads, and to filter
\r
586 out packets that are not of interest to this demo. */
\r
587 prvConfigureCaptureBehaviour();
\r
590 /* The device list is no longer required. */
\r
591 pcap_freealldevs( pxAllNetworkInterfaces );
\r
593 /*-----------------------------------------------------------*/
\r
595 static void prvInterruptSimulator( void *pvParameters )
\r
597 static struct pcap_pkthdr *pxHeader;
\r
598 const unsigned char *pucPacketData;
\r
599 extern xQueueHandle xEMACEventQueue;
\r
602 /* Just to kill the compiler warning. */
\r
603 ( void ) pvParameters;
\r
607 /* Get the next packet. */
\r
608 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
611 if( pxlwIPNetIf != NULL )
\r
613 prvEthernetInput( pucPacketData, pxHeader->len );
\r
618 /* There is no real way of simulating an interrupt.
\r
619 Make sure other tasks can run. */
\r
624 /*-----------------------------------------------------------*/
\r
626 static void prvConfigureCaptureBehaviour( void )
\r
628 struct bpf_program xFilterCode;
\r
629 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
630 unsigned long ulNetMask;
\r
632 /* Unblock a read as soon as anything is received. */
\r
633 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
635 /* Allow blocking. */
\r
636 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
638 /* Set up a filter so only the packets of interest are passed to the lwIP
\r
639 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
640 confuse this with an error message. */
\r
641 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
643 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
645 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
647 printf( "\r\nThe packet filter string is invalid\r\n" );
\r
651 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
653 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
657 /* Create a task that simulates an interrupt in a real system. This will
\r
658 block waiting for packets, then send a message to the uIP task when data
\r
660 xTaskCreate( prvInterruptSimulator, ( signed char * ) "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r