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