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