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