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