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