]> begriffs open source - cmsis-freertos/blob - Demo/Common/ethernet/lwip-1.4.0/ports/win32/ethernetif.c
Initial commit
[cmsis-freertos] / Demo / Common / ethernet / lwip-1.4.0 / ports / win32 / ethernetif.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
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.
12
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     ***************************************************************************
19
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
24
25     ***************************************************************************
26      *                                                                       *
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.                               *
31      *                                                                       *
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                              *
36      *                                                                       *
37     ***************************************************************************
38
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()?
42
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.
46
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.
51
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.
55
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.
58
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.
62
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.
66
67     1 tab == 4 spaces!
68 */
69
70 /* WinPCap includes. */
71 #define HAVE_REMOTE
72 #include "pcap.h"
73
74 /* FreeRTOS includes. */
75 #include "FreeRTOS.h"
76 #include "task.h"
77 #include "queue.h"
78
79 /* lwIP includes. */
80 #include "lwip/opt.h"
81 #include "lwip/def.h"
82 #include "lwip/mem.h"
83 #include "lwip/pbuf.h"
84 #include "lwip/sys.h"
85 #include <lwip/stats.h>
86 #include <lwip/snmp.h>
87 #include "netif/etharp.h"
88
89 /* Define those to better describe your network interface. */
90 #define IFNAME0 'w'
91 #define IFNAME1 'p'
92
93 #define netifMAX_MTU 1500
94
95 struct xEthernetIf
96 {
97         struct eth_addr *ethaddr;
98         /* Add whatever per-interface state that is needed here. */
99 };
100
101 /*
102  * Place received packet in a pbuf and send a message to the tcpip task to let
103  * it know new data has arrived.
104  */
105 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
106
107 /*
108  * Copy the received data into a pbuf.
109  */
110 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
111
112 /*
113  * Send data from a pbuf to the hardware.
114  */
115 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
116
117 /*
118  * Perform any hardware and/or driver initialisation necessary.
119  */
120 static void prvLowLevelInit( struct netif *pxNetIf );
121
122 /*
123  * Query the computer the simulation is being executed on to find the network
124  * interfaces it has installed.
125  */
126 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
127
128 /*
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.
131  */
132 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
133
134 /*
135  * Interrupts cannot truely be simulated using WinPCap.  In reality this task
136  * just polls the interface. 
137  */
138 static void prvInterruptSimulator( void *pvParameters );
139
140 /*
141  * Configure the capture filter to allow blocking reads, and to filter out
142  * packets that are not of interest to this demo.
143  */
144 static void prvConfigureCaptureBehaviour( void );
145
146 /*-----------------------------------------------------------*/
147
148 /* The WinPCap interface being used. */
149 static pcap_t *pxOpenedInterfaceHandle = NULL;
150
151 /* Parameter required for WinPCap API functions. */
152 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
153
154 /* The network interface that was opened. */
155 static struct netif *pxlwIPNetIf = NULL;
156
157 /*-----------------------------------------------------------*/
158
159 /**
160  * In this function, the hardware should be initialized.
161  * Called from ethernetif_init().
162  *
163  * @param pxNetIf the already initialized lwip network interface structure
164  *              for this ethernetif.
165  */
166 static void prvLowLevelInit( struct netif *pxNetIf )
167 {
168 pcap_if_t *pxAllNetworkInterfaces;
169   
170         /* set MAC hardware address length */
171         pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
172
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;
180
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;
184  
185         /* Query the computer the simulation is being executed on to find the 
186         network interfaces it has installed. */
187         pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
188         
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 )
195         {
196                 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
197         }
198
199         /* Remember which interface was opened as it is used in the interrupt 
200         simulator task. */
201         pxlwIPNetIf = pxNetIf;
202 }
203
204 /**
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
207  * might be chained.
208  *
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
213  *
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).
218  */
219
220 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
221 {
222
223         /* This is taken from lwIP example code and therefore does not conform
224         to the FreeRTOS coding standard. */
225
226 struct pbuf *q;
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;
233
234         ( void ) pxNetIf;
235
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);
238         #endif
239
240         /* Initiate transfer. */
241         if( p->len == p->tot_len ) 
242         {
243                 /* No pbuf chain, don't have to copy -> faster. */
244                 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
245         } 
246         else 
247         {
248                 /* pbuf chain, copy into contiguous ucBuffer. */
249                 if( p->tot_len >= sizeof( ucBuffer ) ) 
250                 {
251                         LINK_STATS_INC( link.lenerr );
252                         LINK_STATS_INC( link.drop );
253                         snmp_inc_ifoutdiscards( pxNetIf );
254                         xReturn = ERR_BUF;
255                 }
256                 else
257                 {
258                         pucChar = ucBuffer;
259
260                         for( q = p; q != NULL; q = q->next ) 
261                         {
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
264                                 variable. */
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 ) );
267                                 if( q == p ) 
268                                 {
269                                         memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
270                                         pucChar += q->len - ETH_PAD_SIZE;
271                                 } 
272                                 else 
273                                 {
274                                         memcpy( pucChar, q->payload, q->len );
275                                         pucChar += q->len;
276                                 }
277                         }
278                 }
279         }
280
281         if( xReturn == ERR_OK )
282         {
283                 /* signal that packet should be sent */
284                 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 ) 
285                 {
286                         LINK_STATS_INC( link.memerr );
287                         LINK_STATS_INC( link.drop );
288                         snmp_inc_ifoutdiscards( pxNetIf );
289                         xReturn = ERR_BUF;
290                 }
291                 else
292                 {
293                         LINK_STATS_INC( link.xmit );
294                         snmp_add_ifoutoctets( pxNetIf, usTotalLength );
295                         pxHeader = ( struct eth_hdr * )p->payload;
296
297                         if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 ) 
298                         {
299                                 /* broadcast or multicast packet*/
300                                 snmp_inc_ifoutnucastpkts( pxNetIf );
301                         } 
302                         else 
303                         {
304                                 /* unicast packet */
305                                 snmp_inc_ifoutucastpkts( pxNetIf );
306                         }
307                 }
308         }
309         
310         return xReturn;
311 }
312
313 /**
314  * Should allocate a pbuf and transfer the bytes of the incoming
315  * packet from the interface into the pbuf.
316  *
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
320  */
321 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
322 {
323 struct pbuf *p = NULL, *q;
324
325         if( lDataLength > 0 )
326         {
327                 #if ETH_PAD_SIZE
328                         len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
329                 #endif
330
331                 /* We allocate a pbuf chain of pbufs from the pool. */
332                 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
333   
334                 if( p != NULL ) 
335                 {
336                         #if ETH_PAD_SIZE
337                                 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
338                         #endif
339
340                         /* We iterate over the pbuf chain until we have read the entire
341                         * packet into the pbuf. */
342                         lDataLength = 0;
343                         for( q = p; q != NULL; q = q->next ) 
344                         {
345                                 /* Read enough bytes to fill this pbuf in the chain. The
346                                 * available data in the pbuf is given by the q->len
347                                 * variable.
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.
352                                 */
353                                 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
354                                 lDataLength += q->len;
355                         }
356
357                         #if ETH_PAD_SIZE
358                                 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
359                         #endif
360
361                         LINK_STATS_INC( link.recv );
362                 }
363         }
364
365         return p;  
366 }
367
368 /**
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.
374  *
375  * @param pxNetIf the lwip network interface structure for this ethernetif
376  */
377 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
378 {
379         /* This is taken from lwIP example code and therefore does not conform
380         to the FreeRTOS coding standard. */
381         
382 struct eth_hdr *pxHeader;
383 struct pbuf *p;
384
385         /* move received packet into a new pbuf */
386         p = prvLowLevelInput( pucInputData, lInputLength );
387
388         /* no packet could be read, silently ignore this */
389         if( p != NULL ) 
390         {
391                 /* points to packet payload, which starts with an Ethernet header */
392                 pxHeader = p->payload;
393
394                 switch( htons( pxHeader->type ) ) 
395                 {
396                         /* IP or ARP packet? */
397                         case ETHTYPE_IP:
398                         case ETHTYPE_ARP:
399                                                                 /* full packet send to tcpip_thread to process */
400                                                                 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
401                                                                 { 
402                                                                         LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
403                                                                         pbuf_free(p);
404                                                                         p = NULL;
405                                                                 }
406                                                                 break;
407
408                         default:
409                                                                 pbuf_free( p );
410                                                                 p = NULL;
411                         break;
412                 }
413         }
414 }
415
416 /**
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.
420  *
421  * This function should be passed as a parameter to netif_add().
422  *
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
427  */
428 err_t ethernetif_init( struct netif *pxNetIf )
429 {
430 err_t xReturn = ERR_OK;
431
432         /* This is taken from lwIP example code and therefore does not conform
433         to the FreeRTOS coding standard. */
434         
435 struct xEthernetIf *pxEthernetIf;
436
437         LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
438         
439         pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
440         if( pxEthernetIf == NULL ) 
441         {
442                 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
443                 xReturn = ERR_MEM;
444         }
445         else
446         {
447                 #if LWIP_NETIF_HOSTNAME
448                 {
449                         /* Initialize interface hostname */
450                         pxNetIf->hostname = "lwip";
451                 }
452                 #endif /* LWIP_NETIF_HOSTNAME */
453
454                 pxNetIf->state = pxEthernetIf;
455                 pxNetIf->name[ 0 ] = IFNAME0;
456                 pxNetIf->name[ 1 ] = IFNAME1;
457
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;
467
468                 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
469   
470                 /* initialize the hardware */
471                 prvLowLevelInit( pxNetIf );
472
473                 /* Was an interface opened? */
474                 if( pxOpenedInterfaceHandle == NULL )
475                 {
476                         /* Probably an invalid adapter number was defined in 
477                         FreeRTOSConfig.h. */
478                         xReturn = ERR_VAL;
479                         configASSERT( pxOpenedInterfaceHandle );
480                 }
481         }
482
483         return xReturn;
484 }
485 /*-----------------------------------------------------------*/
486
487 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
488 {       
489 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
490 long lInterfaceNumber = 1;
491
492         if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
493         {
494                 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
495                 pxAllNetworkInterfaces = NULL;
496         }
497
498         if( pxAllNetworkInterfaces != NULL )
499         {
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 )
503                 {
504                         printf( "%d. %s", lInterfaceNumber, xInterface->name );
505                         
506                         if( xInterface->description != NULL )
507                         {
508                                 printf( " (%s)\r\n", xInterface->description );
509                         }
510                         else
511                         {
512                                 printf( " (No description available)\r\n") ;
513                         }
514                         
515                         lInterfaceNumber++;
516                 }
517         }
518
519         if( lInterfaceNumber == 1 )
520         {
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;
525         }
526
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 );
529         
530         if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
531         {
532                 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
533                 
534                 if( pxAllNetworkInterfaces != NULL )
535                 {
536                         /* Free the device list, as no devices are going to be opened. */
537                         pcap_freealldevs( pxAllNetworkInterfaces );
538                         pxAllNetworkInterfaces = NULL;
539                 }
540         }
541
542         return pxAllNetworkInterfaces;
543 }
544 /*-----------------------------------------------------------*/
545
546 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
547 {
548 pcap_if_t *xInterface;
549 long x;
550
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++ )
554         {
555                 xInterface = xInterface->next;
556         }
557
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. */
571                                                                                         cErrorBuffer                    
572                                                                            );
573                                                                            
574         if ( pxOpenedInterfaceHandle == NULL )
575         {
576                 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
577         }
578         else
579         {
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();
583         }
584
585         /* The device list is no longer required. */
586         pcap_freealldevs( pxAllNetworkInterfaces );
587 }
588 /*-----------------------------------------------------------*/
589
590 static void prvInterruptSimulator( void *pvParameters )
591 {
592 static struct pcap_pkthdr *pxHeader;
593 const unsigned char *pucPacketData;
594 extern QueueHandle_t xEMACEventQueue;
595 long lResult;
596
597         /* Just to kill the compiler warning. */
598         ( void ) pvParameters;
599
600         for( ;; )
601         {
602                 /* Get the next packet. */
603                 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
604                 if( lResult == 1 )
605                 {
606                         if( pxlwIPNetIf != NULL )
607                         {
608                                 prvEthernetInput( pucPacketData, pxHeader->len );
609                         }
610                 }
611                 else
612                 {
613                         /* There is no real way of simulating an interrupt.  
614                         Make sure other tasks can run. */
615                         vTaskDelay( 5 );
616                 }
617         }
618 }
619 /*-----------------------------------------------------------*/
620
621 static void prvConfigureCaptureBehaviour( void )
622 {
623 struct bpf_program xFilterCode;
624 const long lMinBytesToCopy = 10L, lBlocking = 1L;
625 unsigned long ulNetMask;
626
627         /* Unblock a read as soon as anything is received. */
628         pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
629
630         /* Allow blocking. */
631         pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
632
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 );
637
638         ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
639
640         if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
641         {
642                 printf( "\r\nThe packet filter string is invalid\r\n" );
643         }
644         else
645         {       
646                 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
647                 {
648                         printf( "\r\nAn error occurred setting the packet filter.\r\n" );
649                 }
650         }
651
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
654         is available. */
655         xTaskCreate( prvInterruptSimulator, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
656 }
657