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