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