]> git.sur5r.net Git - freertos/blob - Demo/Common/ethernet/lwip-1.4.0/ports/win32/ethernetif.c
Update a few util functions in the common demo area.
[freertos] / Demo / Common / ethernet / lwip-1.4.0 / ports / win32 / ethernetif.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* WinPCap includes. */\r
55 #define HAVE_REMOTE\r
56 #include "pcap.h"\r
57 \r
58 /* FreeRTOS includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "queue.h"\r
62 \r
63 /* lwIP includes. */\r
64 #include "lwip/opt.h"\r
65 #include "lwip/def.h"\r
66 #include "lwip/mem.h"\r
67 #include "lwip/pbuf.h"\r
68 #include "lwip/sys.h"\r
69 #include <lwip/stats.h>\r
70 #include <lwip/snmp.h>\r
71 #include "netif/etharp.h"\r
72 \r
73 /* Define those to better describe your network interface. */\r
74 #define IFNAME0 'w'\r
75 #define IFNAME1 'p'\r
76 \r
77 #define netifMAX_MTU 1500\r
78 \r
79 struct xEthernetIf\r
80 {\r
81         struct eth_addr *ethaddr;\r
82         /* Add whatever per-interface state that is needed here. */\r
83 };\r
84 \r
85 /*\r
86  * Place received packet in a pbuf and send a message to the tcpip task to let\r
87  * it know new data has arrived.\r
88  */\r
89 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );\r
90 \r
91 /*\r
92  * Copy the received data into a pbuf.\r
93  */\r
94 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );\r
95 \r
96 /*\r
97  * Send data from a pbuf to the hardware.\r
98  */\r
99 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );\r
100 \r
101 /*\r
102  * Perform any hardware and/or driver initialisation necessary.\r
103  */\r
104 static void prvLowLevelInit( struct netif *pxNetIf );\r
105 \r
106 /*\r
107  * Query the computer the simulation is being executed on to find the network\r
108  * interfaces it has installed.\r
109  */\r
110 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );\r
111 \r
112 /*\r
113  * Open the network interface.  The number of the interface to be opened is set\r
114  * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.\r
115  */\r
116 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );\r
117 \r
118 /*\r
119  * Interrupts cannot truely be simulated using WinPCap.  In reality this task\r
120  * just polls the interface. \r
121  */\r
122 static void prvInterruptSimulator( void *pvParameters );\r
123 \r
124 /*\r
125  * Configure the capture filter to allow blocking reads, and to filter out\r
126  * packets that are not of interest to this demo.\r
127  */\r
128 static void prvConfigureCaptureBehaviour( void );\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /* The WinPCap interface being used. */\r
133 static pcap_t *pxOpenedInterfaceHandle = NULL;\r
134 \r
135 /* Parameter required for WinPCap API functions. */\r
136 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];\r
137 \r
138 /* The network interface that was opened. */\r
139 static struct netif *pxlwIPNetIf = NULL;\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /**\r
144  * In this function, the hardware should be initialized.\r
145  * Called from ethernetif_init().\r
146  *\r
147  * @param pxNetIf the already initialized lwip network interface structure\r
148  *              for this ethernetif.\r
149  */\r
150 static void prvLowLevelInit( struct netif *pxNetIf )\r
151 {\r
152 pcap_if_t *pxAllNetworkInterfaces;\r
153   \r
154         /* set MAC hardware address length */\r
155         pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;\r
156 \r
157         /* set MAC hardware address */\r
158         pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;\r
159         pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;\r
160         pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;\r
161         pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;\r
162         pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;\r
163         pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;\r
164 \r
165         /* device capabilities */\r
166         /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */\r
167         pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;\r
168  \r
169         /* Query the computer the simulation is being executed on to find the \r
170         network interfaces it has installed. */\r
171         pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();\r
172         \r
173         /* Open the network interface.  The number of the interface to be opened is \r
174         set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.\r
175         Calling this function will set the pxOpenedInterfaceHandle variable.  If,\r
176         after calling this function, pxOpenedInterfaceHandle is equal to NULL, then\r
177         the interface could not be opened. */\r
178         if( pxAllNetworkInterfaces != NULL )\r
179         {\r
180                 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );\r
181         }\r
182 \r
183         /* Remember which interface was opened as it is used in the interrupt \r
184         simulator task. */\r
185         pxlwIPNetIf = pxNetIf;\r
186 }\r
187 \r
188 /**\r
189  * This function should do the actual transmission of the packet. The packet is\r
190  * contained in the pbuf that is passed to the function. This pbuf\r
191  * might be chained.\r
192  *\r
193  * @param pxNetIf the lwip network interface structure for this ethernetif\r
194  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)\r
195  * @return ERR_OK if the packet could be sent\r
196  *               an err_t value if the packet couldn't be sent\r
197  *\r
198  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to\r
199  *         strange results. You might consider waiting for space in the DMA queue\r
200  *         to become availale since the stack doesn't retry to send a packet\r
201  *         dropped because of memory failure (except for the TCP timers).\r
202  */\r
203 \r
204 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )\r
205 {\r
206 \r
207         /* This is taken from lwIP example code and therefore does not conform\r
208         to the FreeRTOS coding standard. */\r
209 \r
210 struct pbuf *q;\r
211 static unsigned char ucBuffer[ 1520 ];\r
212 unsigned char *pucBuffer = ucBuffer;\r
213 unsigned char *pucChar;\r
214 struct eth_hdr *pxHeader;\r
215 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;\r
216 err_t xReturn = ERR_OK;\r
217 \r
218         #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF\r
219                 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);\r
220         #endif\r
221 \r
222         /* Initiate transfer. */\r
223         if( p->len == p->tot_len ) \r
224         {\r
225                 /* No pbuf chain, don't have to copy -> faster. */\r
226                 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];\r
227         } \r
228         else \r
229         {\r
230                 /* pbuf chain, copy into contiguous ucBuffer. */\r
231                 if( p->tot_len >= sizeof( ucBuffer ) ) \r
232                 {\r
233                         LINK_STATS_INC( link.lenerr );\r
234                         LINK_STATS_INC( link.drop );\r
235                         snmp_inc_ifoutdiscards( pxNetIf );\r
236                         xReturn = ERR_BUF;\r
237                 }\r
238                 else\r
239                 {\r
240                         pucChar = ucBuffer;\r
241 \r
242                         for( q = p; q != NULL; q = q->next ) \r
243                         {\r
244                                 /* Send the data from the pbuf to the interface, one pbuf at a\r
245                                 time. The size of the data in each pbuf is kept in the ->len\r
246                                 variable. */\r
247                                 /* send data from(q->payload, q->len); */\r
248                                 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
249                                 if( q == p ) \r
250                                 {\r
251                                         memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );\r
252                                         pucChar += q->len - ETH_PAD_SIZE;\r
253                                 } \r
254                                 else \r
255                                 {\r
256                                         memcpy( pucChar, q->payload, q->len );\r
257                                         pucChar += q->len;\r
258                                 }\r
259                         }\r
260                 }\r
261         }\r
262 \r
263         if( xReturn == ERR_OK )\r
264         {\r
265                 /* signal that packet should be sent */\r
266                 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 ) \r
267                 {\r
268                         LINK_STATS_INC( link.memerr );\r
269                         LINK_STATS_INC( link.drop );\r
270                         snmp_inc_ifoutdiscards( pxNetIf );\r
271                         xReturn = ERR_BUF;\r
272                 }\r
273                 else\r
274                 {\r
275                         LINK_STATS_INC( link.xmit );\r
276                         snmp_add_ifoutoctets( pxNetIf, usTotalLength );\r
277                         pxHeader = ( struct eth_hdr * )p->payload;\r
278 \r
279                         if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 ) \r
280                         {\r
281                                 /* broadcast or multicast packet*/\r
282                                 snmp_inc_ifoutnucastpkts( pxNetIf );\r
283                         } \r
284                         else \r
285                         {\r
286                                 /* unicast packet */\r
287                                 snmp_inc_ifoutucastpkts( pxNetIf );\r
288                         }\r
289                 }\r
290         }\r
291         \r
292         return xReturn;\r
293 }\r
294 \r
295 /**\r
296  * Should allocate a pbuf and transfer the bytes of the incoming\r
297  * packet from the interface into the pbuf.\r
298  *\r
299  * @param pxNetIf the lwip network interface structure for this ethernetif\r
300  * @return a pbuf filled with the received packet (including MAC header)\r
301  *               NULL on memory error\r
302  */\r
303 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )\r
304 {\r
305 struct pbuf *p = NULL, *q;\r
306 \r
307         if( lDataLength > 0 )\r
308         {\r
309                 #if ETH_PAD_SIZE\r
310                         len += ETH_PAD_SIZE; /* allow room for Ethernet padding */\r
311                 #endif\r
312 \r
313                 /* We allocate a pbuf chain of pbufs from the pool. */\r
314                 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );\r
315   \r
316                 if( p != NULL ) \r
317                 {\r
318                         #if ETH_PAD_SIZE\r
319                                 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */\r
320                         #endif\r
321 \r
322                         /* We iterate over the pbuf chain until we have read the entire\r
323                         * packet into the pbuf. */\r
324                         lDataLength = 0;\r
325                         for( q = p; q != NULL; q = q->next ) \r
326                         {\r
327                                 /* Read enough bytes to fill this pbuf in the chain. The\r
328                                 * available data in the pbuf is given by the q->len\r
329                                 * variable.\r
330                                 * This does not necessarily have to be a memcpy, you can also preallocate\r
331                                 * pbufs for a DMA-enabled MAC and after receiving truncate it to the\r
332                                 * actually received size. In this case, ensure the usTotalLength member of the\r
333                                 * pbuf is the sum of the chained pbuf len members.\r
334                                 */\r
335                                 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );\r
336                                 lDataLength += q->len;\r
337                         }\r
338 \r
339                         #if ETH_PAD_SIZE\r
340                                 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */\r
341                         #endif\r
342 \r
343                         LINK_STATS_INC( link.recv );\r
344                 }\r
345         }\r
346 \r
347         return p;  \r
348 }\r
349 \r
350 /**\r
351  * This function should be called when a packet is ready to be read\r
352  * from the interface. It uses the function prvLowLevelInput() that\r
353  * should handle the actual reception of bytes from the network\r
354  * interface. Then the type of the received packet is determined and\r
355  * the appropriate input function is called.\r
356  *\r
357  * @param pxNetIf the lwip network interface structure for this ethernetif\r
358  */\r
359 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )\r
360 {\r
361         /* This is taken from lwIP example code and therefore does not conform\r
362         to the FreeRTOS coding standard. */\r
363         \r
364 struct eth_hdr *pxHeader;\r
365 struct pbuf *p;\r
366 \r
367         /* move received packet into a new pbuf */\r
368         p = prvLowLevelInput( pucInputData, lInputLength );\r
369 \r
370         /* no packet could be read, silently ignore this */\r
371         if( p != NULL ) \r
372         {\r
373                 /* points to packet payload, which starts with an Ethernet header */\r
374                 pxHeader = p->payload;\r
375 \r
376                 switch( htons( pxHeader->type ) ) \r
377                 {\r
378                         /* IP or ARP packet? */\r
379                         case ETHTYPE_IP:\r
380                         case ETHTYPE_ARP:\r
381                                                                 /* full packet send to tcpip_thread to process */\r
382                                                                 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )\r
383                                                                 { \r
384                                                                         LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );\r
385                                                                         pbuf_free(p);\r
386                                                                         p = NULL;\r
387                                                                 }\r
388                                                                 break;\r
389 \r
390                         default:\r
391                                                                 pbuf_free( p );\r
392                                                                 p = NULL;\r
393                         break;\r
394                 }\r
395         }\r
396 }\r
397 \r
398 /**\r
399  * Should be called at the beginning of the program to set up the\r
400  * network interface. It calls the function prvLowLevelInit() to do the\r
401  * actual setup of the hardware.\r
402  *\r
403  * This function should be passed as a parameter to netif_add().\r
404  *\r
405  * @param pxNetIf the lwip network interface structure for this ethernetif\r
406  * @return ERR_OK if the loopif is initialized\r
407  *               ERR_MEM if private data couldn't be allocated\r
408  *               any other err_t on error\r
409  */\r
410 err_t ethernetif_init( struct netif *pxNetIf )\r
411 {\r
412 err_t xReturn = ERR_OK;\r
413 \r
414         /* This is taken from lwIP example code and therefore does not conform\r
415         to the FreeRTOS coding standard. */\r
416         \r
417 struct xEthernetIf *pxEthernetIf;\r
418 \r
419         LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );\r
420         \r
421         pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );\r
422         if( pxEthernetIf == NULL ) \r
423         {\r
424                 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );\r
425                 xReturn = ERR_MEM;\r
426         }\r
427         else\r
428         {\r
429                 #if LWIP_NETIF_HOSTNAME\r
430                 {\r
431                         /* Initialize interface hostname */\r
432                         pxNetIf->hostname = "lwip";\r
433                 }\r
434                 #endif /* LWIP_NETIF_HOSTNAME */\r
435 \r
436                 pxNetIf->state = pxEthernetIf;\r
437                 pxNetIf->name[ 0 ] = IFNAME0;\r
438                 pxNetIf->name[ 1 ] = IFNAME1;\r
439 \r
440                 /* We directly use etharp_output() here to save a function call.\r
441                 * You can instead declare your own function an call etharp_output()\r
442                 * from it if you have to do some checks before sending (e.g. if link\r
443                 * is available...) */\r
444                 pxNetIf->output = etharp_output;\r
445                 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;\r
446                 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;\r
447                 pxNetIf->mtu = netifMAX_MTU;\r
448                 pxNetIf->linkoutput = prvLowLevelOutput;\r
449 \r
450                 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );\r
451   \r
452                 /* initialize the hardware */\r
453                 prvLowLevelInit( pxNetIf );\r
454         }\r
455 \r
456         return xReturn;\r
457 }\r
458 /*-----------------------------------------------------------*/\r
459 \r
460 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )\r
461 {       \r
462 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;\r
463 long lInterfaceNumber = 1;\r
464 \r
465         if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )\r
466         {\r
467                 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );\r
468                 pxAllNetworkInterfaces = NULL;\r
469         }\r
470 \r
471         if( pxAllNetworkInterfaces != NULL )\r
472         {\r
473                 /* Print out the list of network interfaces.  The first in the list\r
474                 is interface '1', not interface '0'. */\r
475                 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )\r
476                 {\r
477                         printf( "%d. %s", lInterfaceNumber, xInterface->name );\r
478                         \r
479                         if( xInterface->description != NULL )\r
480                         {\r
481                                 printf( " (%s)\r\n", xInterface->description );\r
482                         }\r
483                         else\r
484                         {\r
485                                 printf( " (No description available)\r\n") ;\r
486                         }\r
487                         \r
488                         lInterfaceNumber++;\r
489                 }\r
490         }\r
491 \r
492         if( lInterfaceNumber == 1 )\r
493         {\r
494                 /* The interface number was never incremented, so the above for() loop\r
495                 did not execute meaning no interfaces were found. */\r
496                 printf( " \r\nNo network interfaces were found.\r\n" );\r
497                 pxAllNetworkInterfaces = NULL;\r
498         }\r
499 \r
500         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
501         printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );\r
502         \r
503         if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )\r
504         {\r
505                 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );\r
506                 \r
507                 if( pxAllNetworkInterfaces != NULL )\r
508                 {\r
509                         /* Free the device list, as no devices are going to be opened. */\r
510                         pcap_freealldevs( pxAllNetworkInterfaces );\r
511                         pxAllNetworkInterfaces = NULL;\r
512                 }\r
513         }\r
514 \r
515         return pxAllNetworkInterfaces;\r
516 }\r
517 /*-----------------------------------------------------------*/\r
518 \r
519 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )\r
520 {\r
521 pcap_if_t *xInterface;\r
522 long x;\r
523 \r
524         /* Walk the list of devices until the selected device is located. */\r
525         xInterface = pxAllNetworkInterfaces;\r
526         for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )\r
527         {\r
528                 xInterface = xInterface->next;\r
529         }\r
530 \r
531         /* Open the selected interface. */\r
532         pxOpenedInterfaceHandle = pcap_open(    xInterface->name,                       /* The name of the selected interface. */\r
533                                                                                         netifMAX_MTU,                           /* The size of the packet to capture. */\r
534                                                                                         PCAP_OPENFLAG_PROMISCUOUS,      /* Open in promiscious mode as the MAC and \r
535                                                                                                                                                 IP address is going to be "simulated", and \r
536                                                                                                                                                 not be the real MAC and IP address.  This allows\r
537                                                                                                                                                 trafic to the simulated IP address to be routed\r
538                                                                                                                                                 to uIP, and trafic to the real IP address to be\r
539                                                                                                                                                 routed to the Windows TCP/IP stack. */\r
540                                                                                         0L,                                             /* The read time out.  This is going to block\r
541                                                                                                                                                 until data is available. */\r
542                                                                                         NULL,                                           /* No authentication is required as this is\r
543                                                                                                                                                 not a remote capture session. */\r
544                                                                                         cErrorBuffer                    \r
545                                                                            );\r
546                                                                            \r
547         if ( pxOpenedInterfaceHandle == NULL )\r
548         {\r
549                 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );\r
550         }\r
551         else\r
552         {\r
553                 /* Configure the capture filter to allow blocking reads, and to filter \r
554                 out packets that are not of interest to this demo. */\r
555                 prvConfigureCaptureBehaviour();\r
556         }\r
557 \r
558         /* The device list is no longer required. */\r
559         pcap_freealldevs( pxAllNetworkInterfaces );\r
560 }\r
561 /*-----------------------------------------------------------*/\r
562 \r
563 static void prvInterruptSimulator( void *pvParameters )\r
564 {\r
565 static struct pcap_pkthdr *pxHeader;\r
566 const unsigned char *pucPacketData;\r
567 extern xQueueHandle xEMACEventQueue;\r
568 long lResult;\r
569 \r
570         /* Just to kill the compiler warning. */\r
571         ( void ) pvParameters;\r
572 \r
573         for( ;; )\r
574         {\r
575                 /* Get the next packet. */\r
576                 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );\r
577                 if( lResult == 1 )\r
578                 {\r
579                         if( pxlwIPNetIf != NULL )\r
580                         {\r
581                                 prvEthernetInput( pucPacketData, pxHeader->len );\r
582                         }\r
583                 }\r
584                 else\r
585                 {\r
586                         /* There is no real way of simulating an interrupt.  \r
587                         Make sure other tasks can run. */\r
588                         vTaskDelay( 5 );\r
589                 }\r
590         }\r
591 }\r
592 /*-----------------------------------------------------------*/\r
593 \r
594 static void prvConfigureCaptureBehaviour( void )\r
595 {\r
596 struct bpf_program xFilterCode;\r
597 const long lMinBytesToCopy = 10L, lBlocking = 1L;\r
598 unsigned long ulNetMask;\r
599 \r
600         /* Unblock a read as soon as anything is received. */\r
601         pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );\r
602 \r
603         /* Allow blocking. */\r
604         pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );\r
605 \r
606         /* Set up a filter so only the packets of interest are passed to the lwIP\r
607         stack.  cErrorBuffer is used for convenience to create the string.  Don't\r
608         confuse this with an error message. */\r
609         sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
610 \r
611         ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;\r
612 \r
613         if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )\r
614         {\r
615                 printf( "\r\nThe packet filter string is invalid\r\n" );\r
616         }\r
617         else\r
618         {       \r
619                 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )\r
620                 {\r
621                         printf( "\r\nAn error occurred setting the packet filter.\r\n" );\r
622                 }\r
623         }\r
624 \r
625         /* Create a task that simulates an interrupt in a real system.  This will\r
626         block waiting for packets, then send a message to the uIP task when data\r
627         is available. */\r
628         xTaskCreate( prvInterruptSimulator, ( signed char * ) "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );\r
629 }\r
630 \r