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